# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals

import sys
import logging
import hashlib
import time
from collections import OrderedDict
import Skype4Py
from inspect import getmembers, isclass, ismethod

from sevabot.bot import modules
from sevabot.bot import handlers
try:
    from sevabot.bot import custom_handlers
except ImportError:
    custom_handlers = None

logger = logging.getLogger("sevabot")

class Sevabot:
    """
    Skype bot interface handler.
    """

    def __init__(self):
        self.cmds = {}
        self.chats = {}
        self.handlers = {}

    def start(self):

        if sys.platform == "linux2":
            self.skype = Skype4Py.Skype(Transport='x11')
        else:
            # OSX
            self.skype = Skype4Py.Skype()

        logger.debug("Attaching to Skype")
        self.skype.Attach()

        logger.debug("Skype API connection established")
        self.skype.OnMessageStatus = self.handleMessages

        self.cacheChats()
        self.cacheHandlers()

    def cacheChats(self):
        """
        Scan all chats on initial connect.
        """
        logger.debug("Async cacheChats() -- this may take a while")
        self.chats = OrderedDict()

        # First get all fresh chats
        chats = []
        for chat in self.skype.Chats:

            # filter chats older than 6 months
            if time.time() - chat.ActivityTimestamp > 3600 * 24 * 180:
                continue

            chats.append(chat)

        chats = sorted(chats, key=lambda c: c.ActivityTimestamp, reverse=True)

        for chat in chats:
            # Encode ids in b64 so they are easier to pass in URLs
            m = hashlib.md5()
            m.update(chat.Name)
            self.chats[m.hexdigest()] = chat

    def cacheHandlers(self):
        """
        Scan all defined handlers.
        """

        def collect_handlers(module):

            def wanted(member):
                return (isclass(member) and
                        issubclass(member, handlers.HandlerBase) and
                        member.__name__.endswith('Handler'))

            m = {}
            for name, obj in getmembers(module, wanted):
                m[name] = obj(self.skype)
                m[name].init()
            return m

        self.handlers = collect_handlers(handlers)
        if custom_handlers:
            self.handlers.update(collect_handlers(custom_handlers))

    def getOpenChats(self):
        """
        Get list of id -> chat object of all chats which are open.
        """

        # Make sure we get refresh chat list every time
        self.cacheChats()
        for chat_id, chat in self.chats.items():
            yield chat_id, chat

    def handleMessages(self, msg, status):
        """
        Handle incoming messages.
        """

        logger.debug("%s - %s - %s: %s" % (status, msg.Chat.FriendlyName,
                                           msg.FromHandle, msg.Body))

        if status in ["RECEIVED", "SENT"] and msg.Body:
            for handler in self.handlers.values():
                handler.handle(msg, status)

    def sendMsg(self, chat, msg):
        """
        Send a message to chat.

        :param chat: Chat id as a string

        :param msg: Message as UTF-8 encoded string
        """
        try:
            self.chats[chat].SendMessage(msg)
            return "Message sent\n"
        except KeyError:
            raise RuntimeError("No chat %s" % chat)

6c767fda2b8359cb94756da91ba393fd983c09d3
<mask token>


class Sevabot:
    <mask token>

    def __init__(self):
        self.cmds = {}
        self.chats = {}
        self.handlers = {}

    def start(self):
        if sys.platform == 'linux2':
            self.skype = Skype4Py.Skype(Transport='x11')
        else:
            self.skype = Skype4Py.Skype()
        logger.debug('Attaching to Skype')
        self.skype.Attach()
        logger.debug('Skype API connection established')
        self.skype.OnMessageStatus = self.handleMessages
        self.cacheChats()
        self.cacheHandlers()

    def cacheChats(self):
        """
        Scan all chats on initial connect.
        """
        logger.debug('Async cacheChats() -- this may take a while')
        self.chats = OrderedDict()
        chats = []
        for chat in self.skype.Chats:
            if time.time() - chat.ActivityTimestamp > 3600 * 24 * 180:
                continue
            chats.append(chat)
        chats = sorted(chats, key=lambda c: c.ActivityTimestamp, reverse=True)
        for chat in chats:
            m = hashlib.md5()
            m.update(chat.Name)
            self.chats[m.hexdigest()] = chat

    def cacheHandlers(self):
        """
        Scan all defined handlers.
        """

        def collect_handlers(module):

            def wanted(member):
                return isclass(member) and issubclass(member, handlers.
                    HandlerBase) and member.__name__.endswith('Handler')
            m = {}
            for name, obj in getmembers(module, wanted):
                m[name] = obj(self.skype)
                m[name].init()
            return m
        self.handlers = collect_handlers(handlers)
        if custom_handlers:
            self.handlers.update(collect_handlers(custom_handlers))

    def getOpenChats(self):
        """
        Get list of id -> chat object of all chats which are open.
        """
        self.cacheChats()
        for chat_id, chat in self.chats.items():
            yield chat_id, chat

    def handleMessages(self, msg, status):
        """
        Handle incoming messages.
        """
        logger.debug('%s - %s - %s: %s' % (status, msg.Chat.FriendlyName,
            msg.FromHandle, msg.Body))
        if status in ['RECEIVED', 'SENT'] and msg.Body:
            for handler in self.handlers.values():
                handler.handle(msg, status)

    def sendMsg(self, chat, msg):
        """
        Send a message to chat.

        :param chat: Chat id as a string

        :param msg: Message as UTF-8 encoded string
        """
        try:
            self.chats[chat].SendMessage(msg)
            return 'Message sent\n'
        except KeyError:
            raise RuntimeError('No chat %s' % chat)

<mask token>
try:
    from sevabot.bot import custom_handlers
except ImportError:
    custom_handlers = None
<mask token>


class Sevabot:
    """
    Skype bot interface handler.
    """

    def __init__(self):
        self.cmds = {}
        self.chats = {}
        self.handlers = {}

    def start(self):
        if sys.platform == 'linux2':
            self.skype = Skype4Py.Skype(Transport='x11')
        else:
            self.skype = Skype4Py.Skype()
        logger.debug('Attaching to Skype')
        self.skype.Attach()
        logger.debug('Skype API connection established')
        self.skype.OnMessageStatus = self.handleMessages
        self.cacheChats()
        self.cacheHandlers()

    def cacheChats(self):
        """
        Scan all chats on initial connect.
        """
        logger.debug('Async cacheChats() -- this may take a while')
        self.chats = OrderedDict()
        chats = []
        for chat in self.skype.Chats:
            if time.time() - chat.ActivityTimestamp > 3600 * 24 * 180:
                continue
            chats.append(chat)
        chats = sorted(chats, key=lambda c: c.ActivityTimestamp, reverse=True)
        for chat in chats:
            m = hashlib.md5()
            m.update(chat.Name)
            self.chats[m.hexdigest()] = chat

    def cacheHandlers(self):
        """
        Scan all defined handlers.
        """

        def collect_handlers(module):

            def wanted(member):
                return isclass(member) and issubclass(member, handlers.
                    HandlerBase) and member.__name__.endswith('Handler')
            m = {}
            for name, obj in getmembers(module, wanted):
                m[name] = obj(self.skype)
                m[name].init()
            return m
        self.handlers = collect_handlers(handlers)
        if custom_handlers:
            self.handlers.update(collect_handlers(custom_handlers))

    def getOpenChats(self):
        """
        Get list of id -> chat object of all chats which are open.
        """
        self.cacheChats()
        for chat_id, chat in self.chats.items():
            yield chat_id, chat

    def handleMessages(self, msg, status):
        """
        Handle incoming messages.
        """
        logger.debug('%s - %s - %s: %s' % (status, msg.Chat.FriendlyName,
            msg.FromHandle, msg.Body))
        if status in ['RECEIVED', 'SENT'] and msg.Body:
            for handler in self.handlers.values():
                handler.handle(msg, status)

    def sendMsg(self, chat, msg):
        """
        Send a message to chat.

        :param chat: Chat id as a string

        :param msg: Message as UTF-8 encoded string
        """
        try:
            self.chats[chat].SendMessage(msg)
            return 'Message sent\n'
        except KeyError:
            raise RuntimeError('No chat %s' % chat)

<mask token>
try:
    from sevabot.bot import custom_handlers
except ImportError:
    custom_handlers = None
logger = logging.getLogger('sevabot')


class Sevabot:
    """
    Skype bot interface handler.
    """

    def __init__(self):
        self.cmds = {}
        self.chats = {}
        self.handlers = {}

    def start(self):
        if sys.platform == 'linux2':
            self.skype = Skype4Py.Skype(Transport='x11')
        else:
            self.skype = Skype4Py.Skype()
        logger.debug('Attaching to Skype')
        self.skype.Attach()
        logger.debug('Skype API connection established')
        self.skype.OnMessageStatus = self.handleMessages
        self.cacheChats()
        self.cacheHandlers()

    def cacheChats(self):
        """
        Scan all chats on initial connect.
        """
        logger.debug('Async cacheChats() -- this may take a while')
        self.chats = OrderedDict()
        chats = []
        for chat in self.skype.Chats:
            if time.time() - chat.ActivityTimestamp > 3600 * 24 * 180:
                continue
            chats.append(chat)
        chats = sorted(chats, key=lambda c: c.ActivityTimestamp, reverse=True)
        for chat in chats:
            m = hashlib.md5()
            m.update(chat.Name)
            self.chats[m.hexdigest()] = chat

    def cacheHandlers(self):
        """
        Scan all defined handlers.
        """

        def collect_handlers(module):

            def wanted(member):
                return isclass(member) and issubclass(member, handlers.
                    HandlerBase) and member.__name__.endswith('Handler')
            m = {}
            for name, obj in getmembers(module, wanted):
                m[name] = obj(self.skype)
                m[name].init()
            return m
        self.handlers = collect_handlers(handlers)
        if custom_handlers:
            self.handlers.update(collect_handlers(custom_handlers))

    def getOpenChats(self):
        """
        Get list of id -> chat object of all chats which are open.
        """
        self.cacheChats()
        for chat_id, chat in self.chats.items():
            yield chat_id, chat

    def handleMessages(self, msg, status):
        """
        Handle incoming messages.
        """
        logger.debug('%s - %s - %s: %s' % (status, msg.Chat.FriendlyName,
            msg.FromHandle, msg.Body))
        if status in ['RECEIVED', 'SENT'] and msg.Body:
            for handler in self.handlers.values():
                handler.handle(msg, status)

    def sendMsg(self, chat, msg):
        """
        Send a message to chat.

        :param chat: Chat id as a string

        :param msg: Message as UTF-8 encoded string
        """
        try:
            self.chats[chat].SendMessage(msg)
            return 'Message sent\n'
        except KeyError:
            raise RuntimeError('No chat %s' % chat)

from __future__ import absolute_import, division, print_function, unicode_literals
import sys
import logging
import hashlib
import time
from collections import OrderedDict
import Skype4Py
from inspect import getmembers, isclass, ismethod
from sevabot.bot import modules
from sevabot.bot import handlers
try:
    from sevabot.bot import custom_handlers
except ImportError:
    custom_handlers = None
logger = logging.getLogger('sevabot')


class Sevabot:
    """
    Skype bot interface handler.
    """

    def __init__(self):
        self.cmds = {}
        self.chats = {}
        self.handlers = {}

    def start(self):
        if sys.platform == 'linux2':
            self.skype = Skype4Py.Skype(Transport='x11')
        else:
            self.skype = Skype4Py.Skype()
        logger.debug('Attaching to Skype')
        self.skype.Attach()
        logger.debug('Skype API connection established')
        self.skype.OnMessageStatus = self.handleMessages
        self.cacheChats()
        self.cacheHandlers()

    def cacheChats(self):
        """
        Scan all chats on initial connect.
        """
        logger.debug('Async cacheChats() -- this may take a while')
        self.chats = OrderedDict()
        chats = []
        for chat in self.skype.Chats:
            if time.time() - chat.ActivityTimestamp > 3600 * 24 * 180:
                continue
            chats.append(chat)
        chats = sorted(chats, key=lambda c: c.ActivityTimestamp, reverse=True)
        for chat in chats:
            m = hashlib.md5()
            m.update(chat.Name)
            self.chats[m.hexdigest()] = chat

    def cacheHandlers(self):
        """
        Scan all defined handlers.
        """

        def collect_handlers(module):

            def wanted(member):
                return isclass(member) and issubclass(member, handlers.
                    HandlerBase) and member.__name__.endswith('Handler')
            m = {}
            for name, obj in getmembers(module, wanted):
                m[name] = obj(self.skype)
                m[name].init()
            return m
        self.handlers = collect_handlers(handlers)
        if custom_handlers:
            self.handlers.update(collect_handlers(custom_handlers))

    def getOpenChats(self):
        """
        Get list of id -> chat object of all chats which are open.
        """
        self.cacheChats()
        for chat_id, chat in self.chats.items():
            yield chat_id, chat

    def handleMessages(self, msg, status):
        """
        Handle incoming messages.
        """
        logger.debug('%s - %s - %s: %s' % (status, msg.Chat.FriendlyName,
            msg.FromHandle, msg.Body))
        if status in ['RECEIVED', 'SENT'] and msg.Body:
            for handler in self.handlers.values():
                handler.handle(msg, status)

    def sendMsg(self, chat, msg):
        """
        Send a message to chat.

        :param chat: Chat id as a string

        :param msg: Message as UTF-8 encoded string
        """
        try:
            self.chats[chat].SendMessage(msg)
            return 'Message sent\n'
        except KeyError:
            raise RuntimeError('No chat %s' % chat)

# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals

import sys
import logging
import hashlib
import time
from collections import OrderedDict
import Skype4Py
from inspect import getmembers, isclass, ismethod

from sevabot.bot import modules
from sevabot.bot import handlers
try:
    from sevabot.bot import custom_handlers
except ImportError:
    custom_handlers = None

logger = logging.getLogger("sevabot")

class Sevabot:
    """
    Skype bot interface handler.
    """

    def __init__(self):
        self.cmds = {}
        self.chats = {}
        self.handlers = {}

    def start(self):

        if sys.platform == "linux2":
            self.skype = Skype4Py.Skype(Transport='x11')
        else:
            # OSX
            self.skype = Skype4Py.Skype()

        logger.debug("Attaching to Skype")
        self.skype.Attach()

        logger.debug("Skype API connection established")
        self.skype.OnMessageStatus = self.handleMessages

        self.cacheChats()
        self.cacheHandlers()

    def cacheChats(self):
        """
        Scan all chats on initial connect.
        """
        logger.debug("Async cacheChats() -- this may take a while")
        self.chats = OrderedDict()

        # First get all fresh chats
        chats = []
        for chat in self.skype.Chats:

            # filter chats older than 6 months
            if time.time() - chat.ActivityTimestamp > 3600 * 24 * 180:
                continue

            chats.append(chat)

        chats = sorted(chats, key=lambda c: c.ActivityTimestamp, reverse=True)

        for chat in chats:
            # Encode ids in b64 so they are easier to pass in URLs
            m = hashlib.md5()
            m.update(chat.Name)
            self.chats[m.hexdigest()] = chat

    def cacheHandlers(self):
        """
        Scan all defined handlers.
        """

        def collect_handlers(module):

            def wanted(member):
                return (isclass(member) and
                        issubclass(member, handlers.HandlerBase) and
                        member.__name__.endswith('Handler'))

            m = {}
            for name, obj in getmembers(module, wanted):
                m[name] = obj(self.skype)
                m[name].init()
            return m

        self.handlers = collect_handlers(handlers)
        if custom_handlers:
            self.handlers.update(collect_handlers(custom_handlers))

    def getOpenChats(self):
        """
        Get list of id -> chat object of all chats which are open.
        """

        # Make sure we get refresh chat list every time
        self.cacheChats()
        for chat_id, chat in self.chats.items():
            yield chat_id, chat

    def handleMessages(self, msg, status):
        """
        Handle incoming messages.
        """

        logger.debug("%s - %s - %s: %s" % (status, msg.Chat.FriendlyName,
                                           msg.FromHandle, msg.Body))

        if status in ["RECEIVED", "SENT"] and msg.Body:
            for handler in self.handlers.values():
                handler.handle(msg, status)

    def sendMsg(self, chat, msg):
        """
        Send a message to chat.

        :param chat: Chat id as a string

        :param msg: Message as UTF-8 encoded string
        """
        try:
            self.chats[chat].SendMessage(msg)
            return "Message sent\n"
        except KeyError:
            raise RuntimeError("No chat %s" % chat)
