Commits

Ian Lewis committed d158d94

Added async SendMessage support. fixes #1

  • Participants
  • Parent commits c108016

Comments (0)

Files changed (2)

File Skype4Py/chat.py

 
 from utils import *
 from user import *
+from api import Command
 from errors import SkypeError
 
 
         return ChatMessage(self._Owner, chop(self._Owner._DoCommand('CHATMESSAGE %s %s' % (self.Name,
             tounicode(MessageText))), 2)[1])
 
+    def AsyncSendMessage(self, MessageText):
+        """Asynchronously sends a chat message.
+
+        :Parameters:
+          MessageText : unicode
+            Message text
+
+        :return: A message identifier. It will be passed along with the results to the
+                 `SkypeEvents.AsyncSendMessageFinished` event after the search is completed.
+        :rtype: int
+        """
+        if not hasattr(self._Owner, '_AsyncSendMessageCommands'):
+            self._Owner._AsyncSendMessageCommands = []
+            self._Owner.RegisterEventHandler('Reply', self._Owner._AsyncSendMessageReplyHandler)
+        command = Command("CHATMESSAGE %s %s" % (self.Name, MessageText), '',
+                          False, self._Owner.Timeout)
+
+        self._Owner._AsyncSendMessageCommands.append(command)
+        self._Owner.SendCommand(command)
+        # return pCookie - message command identifier
+        return command.Id
+
     def SetPassword(self, Password, Hint=''):
         """Sets the chat password.
 

File Skype4Py/skype.py

             self.ResetCache()
             raise
 
+    def _AsyncSendMessageReplyHandler(self, Command):
+        """
+        Reply handler for AsyncSendMessage()
+        """
+        if Command in self._AsyncSendMessageCommands:
+            self._AsyncSendMessageCommands.remove(Command)
+            self._CallEventHandler('AsyncSendMessageFinished', Command.Id,
+                ChatMessage(self, chop(Command.Reply, 2)[1]))
+
+            if len(self._AsyncSendMessageCommands) == 0:
+                self.UnregisterEventHandler('Reply', self._AsyncSendMessageReplyHandler)
+                del self._AsyncSendMessageCommands
+
+    def AsyncSendMessage(self, Username, Text):
+        """Asynchronously sends a chat message.
+
+        :Parameters:
+          Username : str
+            Skypename of the user.
+          Text : unicode
+            Body of the message.
+
+        :return: A message identifier. It will be passed along with the results to the
+                 `SkypeEvents.AsyncSendMessageFinished` event after the search is completed.
+        :rtype: int
+        """
+        return self.CreateChatWith(Username).AsyncSendMessage(Text)
+
     def SendMessage(self, Username, Text):
         """Sends a chat message.
 
         :see: `Skype.AsyncSearchUsers`
         """
 
+    def AsyncSendMessageFinished(self, Cookie, ChatMessage):
+        """This event occurs when an asynchronous send message is completed.
+
+        :Parameters:
+          Cookie : int
+            Message identifier as returned by `Skype.AsyncSendMessage` or
+            `Chat.AsyncSendMessage`.
+          ChatMessage: `ChatMessage`
+            The sent chat message.
+
+        :see: `Skype.AsyncSendMessage`
+        :see: `Chat.AsyncSendMessage`
+        """
+
     def AttachmentStatus(self, Status):
         """This event is caused by a change in the status of an attachment to the Skype API.