Пример #1
0
def commandDispatch(command):
    """
    this function returns a subclass of TiffanyCommand according to 
    command string you specified.

    caller of commandDispatch needs to call run method of the returned
    TiffanyCommand object.
    """
    if not command.startswith(COMMAND_PREFIX):
        ret_command = TiffanyNotCommand(command)
        return ret_command
    else:
        # try to find suitable command
        (prefix, split, command_string) = command.partition(COMMAND_PREFIX)
        command_name = command_string.split()[0]
        command_args = command_string.split()[1:]
        commands_objs = [c(command, command_args) for c in valid_commands]
        commands_strings = [c.command() for c in commands_objs]
        debug_print("command_string => %s" % (commands_strings))
        if command_name in commands_strings:
            verbose_print("%s is found in valid_commands" % (command_name))
            proper_command = commands_objs[commands_strings.index(command_name)]
            return proper_command
        else:
            no_command = TiffanyNoCommand(command, command_args)
            return no_command
Пример #2
0
 def __init__(self, di, chat, db_instance = None, current_topic = None):
     """
     skype is an instance of TiffanySkype and chat
     is an instance of SkypeChat.
     """
     verbose_print("creating TiffanyChatRoom/%s" % (chat))
     self._chat = chat
     self._current_topic = current_topic
     (room_name, topic_name) = parseChatName(chat.FriendlyName)
     if not db_instance:
         # if no db_instance is specified,
         # create db_instance and add it into DB.
         self._db_instance = ChatData(room_name)
         di.add(self.DBInstance())
     else:
         self._db_instance = db_instance
     
     if self.DBInstance().room_name == "":
         self.invokeChatHelp()
     if topic_name == "":
         self.invokeTopicHelp()
     else:
         # create topic
         topic_datum = di.findTopicFromNameAndRoomID(topic_name,
                                                     self.DBInstance().getID())
         if topic_datum:
             self._current_topic = buildChatTopicFromDBInstance(di,
                                                                topic_datum[0])
         else:
             self._current_topic = TiffanyChatTopic(di, topic_name,
                                                    chat = self)
     # here we need to create topic?
     self.verifyTopic()
     return
Пример #3
0
 def ChatMemberRoleChanged(self, member, role):
     """
     callback method called when  a role of memberis changed.
     this method is used to create a TiffanyChat instance and
     add it into self._chats
     """
     verbose_print("ChatMemberRoleChanged, member=%s, role=%s" %
                   (member.Handle, role))
     return
Пример #4
0
 def sendMail(self, room_id, topic_id):
     # search the all messages from room_id and topic_id
     verbose_print("topic_id = %s" % (topic_id))
     messages = self.findMessageFromTopicID(topic_id)
     room = self.findChatFromID(room_id)[0]
     topic = self.findTopicFromID(topic_id)[0]
     body = u"\n".join([u'%s "%s"' % (m.getUserName(), m.getMessageText())
                       for m in messages])
     sendMail(room.getRoomName(), topic.getTopicName(), body)
Пример #5
0
 def storeMessage(self, di, msg, chat):
     """
     store a msg into DB.
     """
     if chat.currentTopic():
         msg_data = MessageData(msg.Body,
                                chat.currentTopic().DBInstance().getID(),
                                msg.FromHandle)
         verbose_print("msg => %s" % (msg_data))
         di.add(msg_data)
         return msg_data
     else:
         return chat.invokeTopicHelp()
Пример #6
0
 def ChatMembersChanged(self, chat, member):
     """
     callback method if a member has joined or left from chat room.
     
     this method is used to create a TiffanyChat instance and
     add it into self._chats.
     """
     verbose_print("ChatMembersChanged, chat=%s, member=%s"
                   % (chat, member))
     if chat not in self.chats():
         # need to create TiffanyChatRoom
         debug_print("adding new chat: %s" % (chat))
         self.addChat(chat)
     else:                   # already there
         verbose_print("%s is already there" % (chat))
     return
Пример #7
0
 def changeTopic(self, di, topic_name):
     """
     change the current topic.
     if chat room has a topic already, send an email to ML and
     create new one.
     """
     verbose_print("change topic!")
     if self.currentTopic():
         room_id = self.DBInstance().id
         topic_id = self.currentTopic().DBInstance().id
         di.sendMail(room_id, topic_id)
     self._current_topic = TiffanyChatTopic(di, topic = topic_name,
                                            chat = self)
     # change room name
     room_name = buildChatName(self.DBInstance().room_name,
                               self.currentTopic().DBInstance().topic_name)
     #verbose_print("topic => %s" % (self._chat.Topic))
     self._chat.Topic = room_name
     return
Пример #8
0
 def changeRoom(self, di, room_name):
     """
     change the name of room.
     if chat room has a topic already, send an email to ML.
     """
     verbose_print("change room!")
     if self.currentTopic() and self.DBInstance():
         room_id = self.DBInstance().id
         topic_id = self.currentTopic().DBInstance().id
         di.sendMail(room_id, topic_id)
     # change FriendlyName
     friendly_name = ""
     if self.currentTopic():
         friendly_name = buildChatName(room_name,
                                       self.currentTopic().DBInstance().topic_name)
     else:
         friendly_name = buildChatName(room_name, "")
     self._chat.Topic = friendly_name
     self._db_instance = None
     self.__init__(di, self._chat, current_topic = self.currentTopic())
     return
Пример #9
0
 def addChat(self, chat):
     """
     chat is a SkypeChat instance.
     """
     verbose_print("try to add chat")
     # here, we need to check the chat has been created on DB or not?
     (room_name, topic_name) = parseChatName(chat.FriendlyName)
     verbose_print("%s/room_name, topic_name => %s, %s"
                   % (chat.FriendlyName, room_name, topic_name))
     chat_candidates = self.DBInterface().findChatFromName(room_name)
     verbose_print("chat_candidates => %s" % (chat_candidates))
     new = None
     if len(chat_candidates) == 0: # no candidates, we need to create it
         new = TiffanyChatRoom(self.DBInterface(), chat)
     else:                   # we already have it, create it from DB
         if len(chat_candidates) != 1:
             warn_print("more than one candidates... it may be a bug")
         chat_data = chat_candidates[0]
         new = buildChatRoomFromDBInstance(self.DBInterface(),
                                           self.skype(),
                                           chat_data,
                                           topic = topic_name)
     self._chats.append(new)
     new.inviteFriends(self.skype())
     return new
Пример #10
0
 def MessageStatus(self, msg, status):
     """callback method when a message is received."""
     verbose_print("MessageStatus, status=%s" % (status))
     if status == "RECEIVED": # only process if status is RECEIVED
         # check if TiffanySkype has the chat of msg in self._chats
         if msg.Chat not in [c.chat() for c in self.chats()]:
             self.addChat(msg.Chat)
         command = commandDispatch(msg.Body)
         debug_print("command = %s" % (command.command()))
         target_chat = self.findChat(msg.Chat)
         # check the chat have topic
         if command:
             command.run(target_chat, self.DBInterface())
         if not target_chat.currentTopic():
             target_chat.invokeTopicHelp()
         else:
             self.storeMessage(self.DBInterface(), msg, target_chat)
         # print "[%s: %s] %s" % (msg.Chat.FriendlyName,
         #                        msg.FromHandle,
         #                        msg.Body)
         #print msg
     return
Пример #11
0
def buildChatRoomFromDBInstance(di, skype, chat_data, topic = ""):
    """
    create an instance of TiffanyChatRoom from DB object, that is an instance
    of ChatData.
    """
    verbose_print("create TiffanyChatRoom from DBInstance")
    current_topic = None
    # try to find topic
    if not topic == "":
        # if topic is not an empty string,
        # we try to look it up from DB
        topic_datum = di.findTopicFromNameAndRoomID(topic, chat_data.getID())
        if topic_datum:
            # error check
            if len(topic_datum) > 1:
                warn_print("one more topics are finds! it may be a bug")
            current_topic = buildChatTopicFromDBInstance(di, topic_datum[0])
    chat_room_name = buildChatName(chat_data.room_name, topic)
    verbose_print("room_name => %s" % (chat_room_name))
    verbose_print("chats => %s" % [c.FriendlyName for c in skype.Chats])
    return TiffanyChatRoom(di, findChatFromSkype(skype, chat_room_name)[0],
                           db_instance = chat_data,
                           current_topic = current_topic)
Пример #12
0
import sys
# local library
from db import DBInterface
from config import DB_NAME
from skype import SkypeEventHandler
from color import verbose_print
import time

class TiffanyBot(object):
    "application class of tiffanybot."
    _skype = None
    
    def __init__(self):
        """constructor of TiffanyBot. make an instance of Skype4Py
        to register itself"""
        self._skype = SkypeEventHandler(DBInterface(DB_NAME))
        return
    
    def main(self):
        """main function of tiffanybot. it runs main-loop of
        this application."""
        while True:
            time.sleep(1.0)
            print "[%s] tick" % (time.localtime(time.time()))

if __name__ == "__main__":
    bot = TiffanyBot()
    verbose_print("connected!")
    bot.main()
Пример #13
0
# local library
from db import DBInterface
from config import DB_NAME
from skype import SkypeEventHandler
from color import verbose_print
import time


class TiffanyBot(object):
    "application class of tiffanybot."
    _skype = None

    def __init__(self):
        """constructor of TiffanyBot. make an instance of Skype4Py
        to register itself"""
        self._skype = SkypeEventHandler(DBInterface(DB_NAME))
        return

    def main(self):
        """main function of tiffanybot. it runs main-loop of
        this application."""
        while True:
            time.sleep(1.0)
            print "[%s] tick" % (time.localtime(time.time()))


if __name__ == "__main__":
    bot = TiffanyBot()
    verbose_print("connected!")
    bot.main()
Пример #14
0
 def AttachmentStatus(self, status): # ??
     "callback method for attachment event"
     verbose_print("AttachmentStatus")
     return
Пример #15
0
 def run(self, chat, di):
     verbose_print("nothing to do")
     return