1 # Twisted, the Framework of Your Internet
2 # Copyright (C) 2001-2002 Matthew W. Lefkowitz
3 # Copyright (C) 2004-2005 James C. Bunton
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of version 2.1 of the GNU Lesser General Public
7 # License as published by the Free Software Foundation.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 MSNP11 Protocol (client only) - semi-experimental
24 This module provides support for clients using the MSN Protocol (MSNP11).
25 There are basically 3 servers involved in any MSN session:
29 The DispatchClient class handles connections to the
30 dispatch server, which basically delegates users to a
31 suitable notification server.
33 You will want to subclass this and handle the gotNotificationReferral
36 I{Notification Server}
38 The NotificationClient class handles connections to the
39 notification server, which acts as a session server
40 (state updates, message negotiation etc...)
44 The SwitchboardClient handles connections to switchboard
45 servers which are used to conduct conversations with other users.
47 There are also two classes (FileSend and FileReceive) used
50 Clients handle events in two ways.
52 - each client request requiring a response will return a Deferred,
53 the callback for same will be fired when the server sends the
55 - Events which are not in response to any client request have
56 respective methods which should be overridden and handled in
59 Most client request callbacks require more than one argument,
60 and since Deferreds can only pass the callback one result,
61 most of the time the callback argument will be a tuple of
62 values (documented in the respective request method).
63 To make reading/writing code easier, callbacks can be defined in
64 a number of ways to handle this 'cleanly'. One way would be to
65 define methods like: def callBack(self, (arg1, arg2, arg)): ...
66 another way would be to do something like:
67 d.addCallback(lambda result: myCallback(*result)).
69 If the server sends an error response to a client request,
70 the errback of the corresponding Deferred will be called,
71 the argument being the corresponding error code.
74 Due to the lack of an official spec for MSNP11, extra checking
75 than may be deemed necessary often takes place considering the
76 server is never 'wrong'. Thus, if gotBadLine (in any of the 3
77 main clients) is called, or an MSNProtocolError is raised, it's
78 probably a good idea to submit a bug report. ;)
79 Use of this module requires that PyOpenSSL is installed.
81 @author: U{Sam Jordan<mailto:sam@twistedmatrix.com>}
82 @author: U{James Bunton<mailto:james@delx.cjb.net>}
85 from __future__
import nested_scopes
88 from twisted
.protocols
.basic
import LineReceiver
90 from twisted
.web
.http
import HTTPClient
93 from twisted
.protocols
.http
import HTTPClient
95 print "Couldn't find a HTTPClient. If you're using Twisted 2.0 make sure you've installed twisted.web"
100 from twisted
.internet
import reactor
, task
101 from twisted
.internet
.defer
import Deferred
102 from twisted
.internet
.protocol
import ClientFactory
103 from twisted
.internet
.ssl
import ClientContextFactory
104 from twisted
.python
import failure
, log
105 from twisted
.xish
.domish
import unescapeFromXml
108 from tlib
import xmlw
111 import types
, operator
, os
, sys
, base64
, random
, struct
, random
, sha
, base64
, StringIO
, array
, codecs
112 from urllib
import quote
, unquote
115 MSN_PROTOCOL_VERSION
= "MSNP11 CVR0" # protocol version
116 MSN_PORT
= 1863 # default dispatch server port
117 MSN_MAX_MESSAGE
= 1664 # max message length
118 MSN_CVR_STR
= "0x040c winnt 5.1 i386 MSNMSGR 7.0.0777 msmsgs"
119 MSN_AVATAR_GUID
= "{A4268EEC-FEC5-49E5-95C3-F126696BDBF6}"
120 MSN_MSNFTP_GUID
= "{5D3E02AB-6190-11D3-BBBB-00C04F795683}"
142 STATUS_ONLINE
= 'NLN'
143 STATUS_OFFLINE
= 'FLN'
144 STATUS_HIDDEN
= 'HDN'
165 P2PSEQ
= [-3, -2, 0, -1, 1, 2, 3, 4, 5, 6, 7, 8]
174 return inp
.split('=')[1]
186 userHandle
= getVal(p
)
188 screenName
= unquote(getVal(p
))
193 else: # Must be the groups
195 groups
= p
.split(',')
197 raise MSNProtocolError
, "Unknown LST/ADC response" + str(params
) # debug
199 return userHandle
, screenName
, userGuid
, lists
, groups
202 """ Needed for Python 2.3 compatibility """
203 return s
+ (n
-len(s
))*c
205 if sys
.byteorder
== "little":
207 """ Encodes to utf-16 and ensures network byte order. Strips the BOM """
208 a
= array
.array("h", s
.encode("utf-16")[2:])
213 """ Encodes to utf-16 and ensures network byte order. Strips the BOM """
214 return s
.encode("utf-16")[2:]
217 return base64
.encodestring(s
).replace("\n", "")
220 for pad
in ["", "=", "==", "A", "A=", "A=="]: # Stupid MSN client!
222 return base64
.decodestring(s
+ pad
)
225 raise ValueError("Got some very bad base64!")
228 format
= "{%4X%4X-%4X-%4X-%4X-%4X%4X%4X}"
231 data
.append(random
.random() * 0xAAFF + 0x1111)
236 def checkParamLen(num
, expected
, cmd
, error
=None):
237 if error
== None: error
= "Invalid Number of Parameters for %s" % cmd
238 if num
!= expected
: raise MSNProtocolError
, error
240 def _parseHeader(h
, v
):
242 Split a certin number of known
243 header values with the format:
244 field1=val,field2=val,field3=val into
245 a dict mapping fields to values.
246 @param h: the header's key
247 @param v: the header's value as a string
250 if h
in ('passporturls','authentication-info','www-authenticate'):
251 v
= v
.replace('Passport1.4','').lstrip()
253 for fieldPair
in v
.split(','):
255 field
,value
= fieldPair
.split('=',1)
256 fields
[field
.lower()] = value
258 fields
[field
.lower()] = ''
262 def _parsePrimitiveHost(host
):
264 h
,p
= host
.replace('https://','').split('/',1)
268 def _login(userHandle
, passwd
, nexusServer
, cached
=0, authData
=''):
270 This function is used internally and should not ever be called
274 def _cb(server
, auth
):
275 loginFac
= ClientFactory()
276 loginFac
.protocol
= lambda : PassportLogin(cb
, userHandle
, passwd
, server
, auth
)
277 reactor
.connectSSL(_parsePrimitiveHost(server
)[0], 443, loginFac
, ClientContextFactory())
280 _cb(nexusServer
, authData
)
282 fac
= ClientFactory()
284 d
.addCallbacks(_cb
, callbackArgs
=(authData
,))
285 d
.addErrback(lambda f
: cb
.errback(f
))
286 fac
.protocol
= lambda : PassportNexus(d
, nexusServer
)
287 reactor
.connectSSL(_parsePrimitiveHost(nexusServer
)[0], 443, fac
, ClientContextFactory())
291 class PassportNexus(HTTPClient
):
294 Used to obtain the URL of a valid passport
297 This class is used internally and should
298 not be instantiated directly -- that is,
299 The passport logging in process is handled
300 transparantly by NotificationClient.
303 def __init__(self
, deferred
, host
):
304 self
.deferred
= deferred
305 self
.host
, self
.path
= _parsePrimitiveHost(host
)
307 def connectionMade(self
):
308 HTTPClient
.connectionMade(self
)
309 self
.sendCommand('GET', self
.path
)
310 self
.sendHeader('Host', self
.host
)
314 def handleHeader(self
, header
, value
):
316 self
.headers
[h
] = _parseHeader(h
, value
)
318 def handleEndHeaders(self
):
319 if self
.connected
: self
.transport
.loseConnection()
320 if not self
.headers
.has_key('passporturls') or not self
.headers
['passporturls'].has_key('dalogin'):
321 self
.deferred
.errback(failure
.Failure(failure
.DefaultException("Invalid Nexus Reply")))
323 self
.deferred
.callback('https://' + self
.headers
['passporturls']['dalogin'])
325 def handleResponse(self
, r
): pass
327 class PassportLogin(HTTPClient
):
329 This class is used internally to obtain
330 a login ticket from a passport HTTPS
331 server -- it should not be used directly.
336 def __init__(self
, deferred
, userHandle
, passwd
, host
, authData
):
337 self
.deferred
= deferred
338 self
.userHandle
= userHandle
340 self
.authData
= authData
341 self
.host
, self
.path
= _parsePrimitiveHost(host
)
343 def connectionMade(self
):
344 self
.sendCommand('GET', self
.path
)
345 self
.sendHeader('Authorization', 'Passport1.4 OrgVerb=GET,OrgURL=http://messenger.msn.com,' +
346 'sign-in=%s,pwd=%s,%s' % (quote(self
.userHandle
), self
.passwd
,self
.authData
))
347 self
.sendHeader('Host', self
.host
)
351 def handleHeader(self
, header
, value
):
353 self
.headers
[h
] = _parseHeader(h
, value
)
355 def handleEndHeaders(self
):
356 if self
._finished
: return
357 self
._finished
= 1 # I think we need this because of HTTPClient
358 if self
.connected
: self
.transport
.loseConnection()
359 authHeader
= 'authentication-info'
360 _interHeader
= 'www-authenticate'
361 if self
.headers
.has_key(_interHeader
): authHeader
= _interHeader
363 info
= self
.headers
[authHeader
]
364 status
= info
['da-status']
365 handler
= getattr(self
, 'login_%s' % (status
,), None)
368 else: raise Exception()
370 self
.deferred
.errback(failure
.Failure(e
))
372 def handleResponse(self
, r
): pass
374 def login_success(self
, info
):
375 ticket
= info
['from-pp']
376 ticket
= ticket
[1:len(ticket
)-1]
377 self
.deferred
.callback((LOGIN_SUCCESS
, ticket
))
379 def login_failed(self
, info
):
380 self
.deferred
.callback((LOGIN_FAILURE
, unquote(info
['cbtxt'])))
382 def login_redir(self
, info
):
383 self
.deferred
.callback((LOGIN_REDIRECT
, self
.headers
['location'], self
.authData
))
385 class MSNProtocolError(Exception):
387 This Exception is basically used for debugging
388 purposes, as the official MSN server should never
389 send anything _wrong_ and nobody in their right
390 mind would run their B{own} MSN server.
391 If it is raised by default command handlers
392 (handle_BLAH) the error will be logged.
399 I am the class used to represent an 'instant' message.
401 @ivar userHandle: The user handle (passport) of the sender
402 (this is only used when receiving a message)
403 @ivar screenName: The screen name of the sender (this is only used
404 when receiving a message)
405 @ivar message: The message
406 @ivar headers: The message headers
408 @ivar length: The message length (including headers and line endings)
409 @ivar ack: This variable is used to tell the server how to respond
410 once the message has been sent. If set to MESSAGE_ACK
411 (default) the server will respond with an ACK upon receiving
412 the message, if set to MESSAGE_NACK the server will respond
413 with a NACK upon failure to receive the message.
414 If set to MESSAGE_ACK_NONE the server will do nothing.
415 This is relevant for the return value of
416 SwitchboardClient.sendMessage (which will return
417 a Deferred if ack is set to either MESSAGE_ACK or MESSAGE_NACK
418 and will fire when the respective ACK or NACK is received).
419 If set to MESSAGE_ACK_NONE sendMessage will return None.
422 MESSAGE_ACK_FAT
= 'D'
424 MESSAGE_ACK_NONE
= 'U'
428 def __init__(self
, length
=0, userHandle
="", screenName
="", message
="", specialMessage
=False):
429 self
.userHandle
= userHandle
430 self
.screenName
= screenName
431 self
.specialMessage
= specialMessage
432 self
.message
= message
433 self
.headers
= {'MIME-Version' : '1.0', 'Content-Type' : 'text/plain'}
437 def _calcMessageLen(self
):
439 used to calculte the number to send
440 as the message length when sending a message.
442 return reduce(operator
.add
, [len(x
[0]) + len(x
[1]) + 4 for x
in self
.headers
.items()]) + len(self
.message
) + 2
444 def setHeader(self
, header
, value
):
445 """ set the desired header """
446 self
.headers
[header
] = value
448 def getHeader(self
, header
):
450 get the desired header value
451 @raise KeyError: if no such header exists.
453 return self
.headers
[header
]
455 def hasHeader(self
, header
):
456 """ check to see if the desired header exists """
457 return self
.headers
.has_key(header
)
459 def getMessage(self
):
460 """ return the message - not including headers """
463 def setMessage(self
, message
):
464 """ set the message text """
465 self
.message
= message
470 Used to represent a MSNObject. This can be currently only be an avatar.
472 @ivar creator: The userHandle of the creator of this picture.
473 @ivar imageData: The PNG image data (only for our own avatar)
474 @ivar type: Always set to 3, for avatar.
475 @ivar size: The size of the image.
476 @ivar location: The filename of the image.
477 @ivar friendly: Unknown.
478 @ivar text: The textual representation of this MSNObject.
480 def __init__(self
, s
=""):
481 """ Pass a XML MSNObject string to parse it, or pass no arguments for a null MSNObject to be created. """
487 def setData(self
, creator
, imageData
):
488 """ Set the creator and imageData for this object """
489 self
.creator
= creator
490 self
.imageData
= imageData
491 self
.size
= len(imageData
)
493 self
.location
= "TMP" + str(random
.randint(1000,9999))
494 self
.friendly
= "AAA="
495 self
.sha1d
= b64enc(sha
.sha(imageData
).digest())
509 """ Makes a textual representation of this MSNObject. Stores it in self.text """
512 h
.append(self
.creator
)
514 h
.append(str(self
.size
))
516 h
.append(str(self
.type))
518 h
.append(self
.location
)
520 h
.append(self
.friendly
)
523 sha1c
= b64enc(sha
.sha("".join(h
)).digest())
524 self
.text
= '<msnobj Creator="%s" Size="%s" Type="%s" Location="%s" Friendly="%s" SHA1D="%s" SHA1C="%s"/>' % (self
.creator
, str(self
.size
), str(self
.type), self
.location
, self
.friendly
, self
.sha1d
, sha1c
)
527 e
= xmlw
.parseText(s
, True)
528 self
.creator
= e
.getAttribute("Creator")
529 self
.size
= int(e
.getAttribute("Size"))
530 self
.type = int(e
.getAttribute("Type"))
531 self
.location
= e
.getAttribute("Location")
532 self
.friendly
= e
.getAttribute("Friendly")
533 self
.sha1d
= e
.getAttribute("SHA1D")
540 This class represents a contact (user).
542 @ivar userGuid: The contact's user guid (unique string)
543 @ivar userHandle: The contact's user handle (passport).
544 @ivar screenName: The contact's screen name.
545 @ivar groups: A list of all the group IDs which this
547 @ivar lists: An integer representing the sum of all lists
548 that this contact belongs to.
549 @ivar caps: int, The capabilities of this client
550 @ivar msnobj: The MSNObject representing the contact's avatar
551 @ivar status: The contact's status code.
552 @type status: str if contact's status is known, None otherwise.
553 @ivar personal: The contact's personal message .
554 @type personal: str if contact's personal message is known, None otherwise.
556 @ivar homePhone: The contact's home phone number.
557 @type homePhone: str if known, otherwise None.
558 @ivar workPhone: The contact's work phone number.
559 @type workPhone: str if known, otherwise None.
560 @ivar mobilePhone: The contact's mobile phone number.
561 @type mobilePhone: str if known, otherwise None.
562 @ivar hasPager: Whether or not this user has a mobile pager
563 @ivar hasBlog: Whether or not this user has a MSN Spaces blog
571 def __init__(self
, userGuid
="", userHandle
="", screenName
="", lists
=0, caps
=0, msnobj
=None, groups
={}, status
=None, personal
=""):
572 self
.userGuid
= userGuid
573 self
.userHandle
= userHandle
574 self
.screenName
= screenName
578 self
.msnobjGot
= True
579 self
.groups
= [] # if applicable
580 self
.status
= status
# current status
581 self
.personal
= personal
584 self
.homePhone
= None
585 self
.workPhone
= None
586 self
.mobilePhone
= None
590 def setPhone(self
, phoneType
, value
):
592 set phone numbers/values for this specific user.
593 for phoneType check the *_PHONE constants and HAS_PAGER
596 t
= phoneType
.upper()
597 if t
== HOME_PHONE
: self
.homePhone
= value
598 elif t
== WORK_PHONE
: self
.workPhone
= value
599 elif t
== MOBILE_PHONE
: self
.mobilePhone
= value
600 elif t
== HAS_PAGER
: self
.hasPager
= value
601 elif t
== HAS_BLOG
: self
.hasBlog
= value
602 #else: raise ValueError, "Invalid Phone Type: " + t
604 def addToList(self
, listType
):
606 Update the lists attribute to
607 reflect being part of the
610 self
.lists |
= listType
612 def removeFromList(self
, listType
):
614 Update the lists attribute to
615 reflect being removed from the
618 self
.lists ^
= listType
620 class MSNContactList
:
622 This class represents a basic MSN contact list.
624 @ivar contacts: All contacts on my various lists
625 @type contacts: dict (mapping user handles to MSNContact objects)
626 @ivar groups: a mapping of group ids to group names
627 (groups can only exist on the forward list)
631 This is used only for storage and doesn't effect the
632 server's contact list.
642 def _getContactsFromList(self
, listType
):
644 Obtain all contacts which belong
645 to the given list type.
647 return dict([(uH
,obj
) for uH
,obj
in self
.contacts
.items() if obj
.lists
& listType
])
649 def addContact(self
, contact
):
653 self
.contacts
[contact
.userHandle
] = contact
655 def remContact(self
, userHandle
):
660 del self
.contacts
[userHandle
]
661 except KeyError: pass
663 def getContact(self
, userHandle
):
665 Obtain the MSNContact object
666 associated with the given
668 @return: the MSNContact object if
669 the user exists, or None.
672 return self
.contacts
[userHandle
]
676 def getBlockedContacts(self
):
678 Obtain all the contacts on my block list
680 return self
._getContactsFromList
(BLOCK_LIST
)
682 def getAuthorizedContacts(self
):
684 Obtain all the contacts on my auth list.
685 (These are contacts which I have verified
686 can view my state changes).
688 return self
._getContactsFromList
(ALLOW_LIST
)
690 def getReverseContacts(self
):
692 Get all contacts on my reverse list.
693 (These are contacts which have added me
694 to their forward list).
696 return self
._getContactsFromList
(REVERSE_LIST
)
698 def getContacts(self
):
700 Get all contacts on my forward list.
701 (These are the contacts which I have added
704 return self
._getContactsFromList
(FORWARD_LIST
)
706 def setGroup(self
, id, name
):
708 Keep a mapping from the given id
711 self
.groups
[id] = name
713 def remGroup(self
, id):
715 Removed the stored group
716 mapping for the given id.
720 except KeyError: pass
721 for c
in self
.contacts
:
722 if id in c
.groups
: c
.groups
.remove(id)
725 class MSNEventBase(LineReceiver
):
727 This class provides support for handling / dispatching events and is the
728 base class of the three main client protocols (DispatchClient,
729 NotificationClient, SwitchboardClient)
733 self
.ids
= {} # mapping of ids to Deferreds
737 self
.currentMessage
= None
739 def connectionLost(self
, reason
):
743 def connectionMade(self
):
746 def _fireCallback(self
, id, *args
):
748 Fire the callback for the given id
749 if one exists and return 1, else return false
751 if self
.ids
.has_key(id):
752 self
.ids
[id][0].callback(args
)
757 def _nextTransactionID(self
):
758 """ return a usable transaction ID """
760 if self
.currentID
> 1000: self
.currentID
= 1
761 return self
.currentID
763 def _createIDMapping(self
, data
=None):
765 return a unique transaction ID that is mapped internally to a
766 deferred .. also store arbitrary data if it is needed
768 id = self
._nextTransactionID
()
770 self
.ids
[id] = (d
, data
)
773 def checkMessage(self
, message
):
775 process received messages to check for file invitations and
776 typing notifications and other control type messages
778 raise NotImplementedError
780 def sendLine(self
, line
):
781 if LINEDEBUG
: log
.msg(">> " + line
)
782 LineReceiver
.sendLine(self
, line
)
784 def lineReceived(self
, line
):
785 if LINEDEBUG
: log
.msg("<< " + line
)
786 if self
.currentMessage
:
787 self
.currentMessage
.readPos
+= len(line
+"\r\n")
789 header
, value
= line
.split(':')
790 self
.currentMessage
.setHeader(header
, unquote(value
).lstrip())
793 #raise MSNProtocolError, "Invalid Message Header"
795 if line
== "" or self
.currentMessage
.specialMessage
:
797 if self
.currentMessage
.readPos
== self
.currentMessage
.length
: self
.rawDataReceived("") # :(
800 cmd
, params
= line
.split(' ', 1)
802 raise MSNProtocolError
, "Invalid Message, %s" % repr(line
)
804 if len(cmd
) != 3: raise MSNProtocolError
, "Invalid Command, %s" % repr(cmd
)
806 if self
.ids
.has_key(params
.split(' ')[0]):
807 self
.ids
[id].errback(int(cmd
))
810 else: # we received an error which doesn't map to a sent command
811 self
.gotError(int(cmd
))
814 handler
= getattr(self
, "handle_%s" % cmd
.upper(), None)
816 try: handler(params
.split(' '))
817 except MSNProtocolError
, why
: self
.gotBadLine(line
, why
)
819 self
.handle_UNKNOWN(cmd
, params
.split(' '))
821 def rawDataReceived(self
, data
):
823 self
.currentMessage
.readPos
+= len(data
)
824 diff
= self
.currentMessage
.readPos
- self
.currentMessage
.length
826 self
.currentMessage
.message
+= data
[:-diff
]
829 self
.currentMessage
.message
+= data
831 self
.currentMessage
.message
+= data
833 del self
.currentMessage
.readPos
834 m
= self
.currentMessage
835 self
.currentMessage
= None
836 if MESSAGEDEBUG
: log
.msg(m
.message
)
837 if not self
.checkMessage(m
):
838 self
.setLineMode(extra
)
841 self
.setLineMode(extra
)
843 ### protocol command handlers - no need to override these.
845 def handle_MSG(self
, params
):
846 checkParamLen(len(params
), 3, 'MSG')
848 messageLen
= int(params
[2])
849 except ValueError: raise MSNProtocolError
, "Invalid Parameter for MSG length argument"
850 self
.currentMessage
= MSNMessage(length
=messageLen
, userHandle
=params
[0], screenName
=unquote(params
[1]))
852 def handle_UNKNOWN(self
, cmd
, params
):
853 """ implement me in subclasses if you want to handle unknown events """
854 log
.msg("Received unknown command (%s), params: %s" % (cmd
, params
))
858 def gotBadLine(self
, line
, why
):
859 """ called when a handler notifies me that this line is broken """
860 log
.msg('Error in line: %s (%s)' % (line
, why
))
862 def gotError(self
, errorCode
):
864 called when the server sends an error which is not in
865 response to a sent command (ie. it has no matching transaction ID)
867 log
.msg('Error %s' % (errorCodes
[errorCode
]))
870 class DispatchClient(MSNEventBase
):
872 This class provides support for clients connecting to the dispatch server
873 @ivar userHandle: your user handle (passport) needed before connecting.
876 def connectionMade(self
):
877 MSNEventBase
.connectionMade(self
)
878 self
.sendLine('VER %s %s' % (self
._nextTransactionID
(), MSN_PROTOCOL_VERSION
))
880 ### protocol command handlers ( there is no need to override these )
882 def handle_VER(self
, params
):
883 versions
= params
[1:]
884 if versions
is None or ' '.join(versions
) != MSN_PROTOCOL_VERSION
:
885 self
.transport
.loseConnection()
886 raise MSNProtocolError
, "Invalid version response"
887 id = self
._nextTransactionID
()
888 self
.sendLine("CVR %s %s %s" % (id, MSN_CVR_STR
, self
.factory
.userHandle
))
890 def handle_CVR(self
, params
):
891 self
.sendLine("USR %s TWN I %s" % (self
._nextTransactionID
(), self
.factory
.userHandle
))
893 def handle_XFR(self
, params
):
894 if len(params
) < 4: raise MSNProtocolError
, "Invalid number of parameters for XFR"
895 id, refType
, addr
= params
[:3]
896 # was addr a host:port pair?
898 host
, port
= addr
.split(':')
903 self
.gotNotificationReferral(host
, int(port
))
907 def gotNotificationReferral(self
, host
, port
):
909 called when we get a referral to the notification server.
911 @param host: the notification server's hostname
912 @param port: the port to connect to
917 class DispatchFactory(ClientFactory
):
919 This class keeps the state for the DispatchClient.
921 @ivar userHandle: the userHandle to request a notification
924 protocol
= DispatchClient
929 class NotificationClient(MSNEventBase
):
931 This class provides support for clients connecting
932 to the notification server.
935 factory
= None # sssh pychecker
937 def __init__(self
, currentID
=0):
938 MSNEventBase
.__init
__(self
)
939 self
.currentID
= currentID
940 self
._state
= ['DISCONNECTED', {}]
942 self
.pingCheckTask
= None
943 self
.msnobj
= MSNObject()
945 def _setState(self
, state
):
946 self
._state
[0] = state
949 return self
._state
[0]
951 def _getStateData(self
, key
):
952 return self
._state
[1][key
]
954 def _setStateData(self
, key
, value
):
955 self
._state
[1][key
] = value
957 def _remStateData(self
, *args
):
958 for key
in args
: del self
._state
[1][key
]
960 def connectionMade(self
):
961 MSNEventBase
.connectionMade(self
)
962 self
._setState
('CONNECTED')
963 self
.sendLine("VER %s %s" % (self
._nextTransactionID
(), MSN_PROTOCOL_VERSION
))
965 def connectionLost(self
, reason
):
966 self
._setState
('DISCONNECTED')
968 if self
.pingCheckTask
:
969 self
.pingCheckTask
.stop()
970 self
.pingCheckTask
= None
971 MSNEventBase
.connectionLost(self
, reason
)
973 def _getEmailFields(self
, message
):
974 fields
= message
.getMessage().strip().split('\n')
978 if len(a
) != 2: continue
985 def _gotInitialEmailNotification(self
, message
):
986 values
= self
._getEmailFields
(message
)
988 inboxunread
= int(values
["Inbox-Unread"])
989 foldersunread
= int(values
["Folders-Unread"])
992 if foldersunread
+ inboxunread
> 0: # For some reason MSN sends notifications about empty inboxes sometimes?
993 self
.gotInitialEmailNotification(inboxunread
, foldersunread
)
995 def _gotEmailNotification(self
, message
):
996 values
= self
._getEmailFields
(message
)
998 mailfrom
= values
["From"]
999 fromaddr
= values
["From-Addr"]
1000 subject
= values
["Subject"]
1001 junkbeginning
= "=?\"us-ascii\"?Q?"
1003 subject
= subject
.replace(junkbeginning
, "").replace(junkend
, "").replace("_", " ")
1005 # If any of the fields weren't found then it's not a big problem. We just ignore the message
1007 self
.gotRealtimeEmailNotification(mailfrom
, fromaddr
, subject
)
1009 def _gotMSNAlert(self
, message
):
1010 notification
= xmlw
.parseText(message
.message
, beExtremelyLenient
=True)
1011 siteurl
= notification
.getAttribute("siteurl")
1012 notid
= notification
.getAttribute("id")
1015 for e
in notification
.elements():
1021 msgid
= msg
.getAttribute("id")
1026 for e
in msg
.elements():
1027 if e
.name
== "ACTION":
1028 action
= e
.getAttribute("url")
1029 if e
.name
== "SUBSCR":
1030 subscr
= e
.getAttribute("url")
1031 if e
.name
== "BODY":
1032 for e2
in e
.elements():
1033 if e2
.name
== "TEXT":
1034 bodytext
= e2
.__str__()
1035 if not (action
and subscr
and bodytext
): return
1037 actionurl
= "%s¬ification_id=%s&message_id=%s&agent=messenger" % (action
, notid
, msgid
) # Used to have $siteurl// at the beginning, but it seems to not work with that now. Weird
1038 subscrurl
= "%s¬ification_id=%s&message_id=%s&agent=messenger" % (subscr
, notid
, msgid
)
1040 self
.gotMSNAlert(bodytext
, actionurl
, subscrurl
)
1042 def _gotUBX(self
, message
):
1043 lm
= message
.message
.lower()
1044 p1
= lm
.find("<psm>") + 5
1045 p2
= lm
.find("</psm>")
1046 if p1
>= 0 and p2
>= 0:
1047 personal
= unescapeFromXml(message
.message
[p1
:p2
])
1048 msnContact
= self
.factory
.contacts
.getContact(message
.userHandle
)
1049 if not msnContact
: return
1050 msnContact
.personal
= personal
1051 self
.contactPersonalChanged(message
.userHandle
, personal
)
1053 self
.contactPersonalChanged(message
.userHandle
, '')
1055 def checkMessage(self
, message
):
1056 """ hook used for detecting specific notification messages """
1057 cTypes
= [s
.lstrip() for s
in message
.getHeader('Content-Type').split(';')]
1058 if 'text/x-msmsgsprofile' in cTypes
:
1059 self
.gotProfile(message
)
1061 elif "text/x-msmsgsinitialemailnotification" in cTypes
:
1062 self
._gotInitialEmailNotification
(message
)
1064 elif "text/x-msmsgsemailnotification" in cTypes
:
1065 self
._gotEmailNotification
(message
)
1067 elif "NOTIFICATION" == message
.userHandle
and message
.specialMessage
== True:
1068 self
._gotMSNAlert
(message
)
1070 elif "UBX" == message
.screenName
and message
.specialMessage
== True:
1071 self
._gotUBX
(message
)
1075 ### protocol command handlers - no need to override these
1077 def handle_VER(self
, params
):
1078 versions
= params
[1:]
1079 if versions
is None or ' '.join(versions
) != MSN_PROTOCOL_VERSION
:
1080 self
.transport
.loseConnection()
1081 raise MSNProtocolError
, "Invalid version response"
1082 self
.sendLine("CVR %s %s %s" % (self
._nextTransactionID
(), MSN_CVR_STR
, self
.factory
.userHandle
))
1084 def handle_CVR(self
, params
):
1085 self
.sendLine("USR %s TWN I %s" % (self
._nextTransactionID
(), self
.factory
.userHandle
))
1087 def handle_USR(self
, params
):
1088 if not (4 <= len(params
) <= 6):
1089 raise MSNProtocolError
, "Invalid Number of Parameters for USR"
1091 mechanism
= params
[1]
1092 if mechanism
== "OK":
1093 self
.loggedIn(params
[2], int(params
[3]))
1094 elif params
[2].upper() == "S":
1095 # we need to obtain auth from a passport server
1097 d
= _login(f
.userHandle
, f
.password
, f
.passportServer
, authData
=params
[3])
1098 d
.addCallback(self
._passportLogin
)
1099 d
.addErrback(self
._passportError
)
1101 def _passportLogin(self
, result
):
1102 if result
[0] == LOGIN_REDIRECT
:
1103 d
= _login(self
.factory
.userHandle
, self
.factory
.password
,
1104 result
[1], cached
=1, authData
=result
[2])
1105 d
.addCallback(self
._passportLogin
)
1106 d
.addErrback(self
._passportError
)
1107 elif result
[0] == LOGIN_SUCCESS
:
1108 self
.sendLine("USR %s TWN S %s" % (self
._nextTransactionID
(), result
[1]))
1109 elif result
[0] == LOGIN_FAILURE
:
1110 self
.loginFailure(result
[1])
1112 def _passportError(self
, failure
):
1113 self
.loginFailure("Exception while authenticating: %s" % failure
)
1115 def handle_CHG(self
, params
):
1117 if not self
._fireCallback
(id, params
[1]):
1118 if self
.factory
: self
.factory
.status
= params
[1]
1119 self
.statusChanged(params
[1])
1121 def handle_ILN(self
, params
):
1122 #checkParamLen(len(params), 6, 'ILN')
1123 msnContact
= self
.factory
.contacts
.getContact(params
[2])
1124 if not msnContact
: return
1125 msnContact
.status
= params
[1]
1126 msnContact
.screenName
= unquote(params
[3])
1127 if len(params
) > 4: msnContact
.caps
= int(params
[4])
1129 self
.handleAvatarHelper(msnContact
, params
[5])
1131 self
.handleAvatarGoneHelper(msnContact
)
1132 self
.gotContactStatus(params
[2], params
[1], unquote(params
[3]))
1134 def handleAvatarGoneHelper(self
, msnContact
):
1135 if msnContact
.msnobj
:
1136 msnContact
.msnobj
= None
1137 msnContact
.msnobjGot
= True
1138 self
.contactAvatarChanged(msnContact
.userHandle
, "")
1140 def handleAvatarHelper(self
, msnContact
, msnobjStr
):
1141 msnobj
= MSNObject(unquote(msnobjStr
))
1142 if not msnContact
.msnobj
or msnobj
.sha1d
!= msnContact
.msnobj
.sha1d
:
1143 if MSNP2PDEBUG
: log
.msg("Updated MSNObject received!" + msnobjStr
)
1144 msnContact
.msnobj
= msnobj
1145 msnContact
.msnobjGot
= False
1146 self
.contactAvatarChanged(msnContact
.userHandle
, msnContact
.msnobj
.sha1d
)
1148 def handle_CHL(self
, params
):
1149 checkParamLen(len(params
), 2, 'CHL')
1150 response
= msnp11chl
.doChallenge(params
[1])
1151 self
.sendLine("QRY %s %s %s" % (self
._nextTransactionID
(), msnp11chl
.MSNP11_PRODUCT_ID
, len(response
)))
1152 self
.transport
.write(response
)
1154 def handle_QRY(self
, params
):
1157 def handle_NLN(self
, params
):
1158 if not self
.factory
: return
1159 msnContact
= self
.factory
.contacts
.getContact(params
[1])
1160 if not msnContact
: return
1161 msnContact
.status
= params
[0]
1162 msnContact
.screenName
= unquote(params
[2])
1163 if len(params
) > 3: msnContact
.caps
= int(params
[3])
1165 self
.handleAvatarHelper(msnContact
, params
[4])
1167 self
.handleAvatarGoneHelper(msnContact
)
1168 self
.contactStatusChanged(params
[1], params
[0], unquote(params
[2]))
1170 def handle_FLN(self
, params
):
1171 checkParamLen(len(params
), 1, 'FLN')
1172 msnContact
= self
.factory
.contacts
.getContact(params
[0])
1174 msnContact
.status
= STATUS_OFFLINE
1175 self
.contactOffline(params
[0])
1177 def handle_LST(self
, params
):
1178 if self
._getState
() != 'SYNC': return
1180 userHandle
, screenName
, userGuid
, lists
, groups
= getVals(params
)
1182 if not userHandle
or lists
< 1:
1183 raise MSNProtocolError
, "Unknown LST " + str(params
) # debug
1184 contact
= MSNContact(userGuid
, userHandle
, screenName
, lists
)
1185 if contact
.lists
& FORWARD_LIST
:
1186 contact
.groups
.extend(map(str, groups
))
1187 self
._getStateData
('list').addContact(contact
)
1188 self
._setStateData
('last_contact', contact
)
1189 sofar
= self
._getStateData
('lst_sofar') + 1
1190 if sofar
== self
._getStateData
('lst_reply'):
1191 # this is the best place to determine that
1192 # a syn realy has finished - msn _may_ send
1193 # BPR information for the last contact
1194 # which is unfortunate because it means
1195 # that the real end of a syn is non-deterministic.
1196 # to handle this we'll keep 'last_contact' hanging
1197 # around in the state data and update it if we need
1199 self
._setState
('SESSION')
1200 contacts
= self
._getStateData
('list')
1201 phone
= self
._getStateData
('phone')
1202 id = self
._getStateData
('synid')
1203 self
._remStateData
('lst_reply', 'lsg_reply', 'lst_sofar', 'phone', 'synid', 'list')
1204 self
._fireCallback
(id, contacts
, phone
)
1206 self
._setStateData
('lst_sofar',sofar
)
1208 def handle_BLP(self
, params
):
1209 # check to see if this is in response to a SYN
1210 if self
._getState
() == 'SYNC':
1211 self
._getStateData
('list').privacy
= listCodeToID
[params
[0].lower()]
1214 self
.factory
.contacts
.privacy
= listCodeToID
[params
[1].lower()]
1215 self
._fireCallback
(id, params
[1])
1217 def handle_GTC(self
, params
):
1218 # check to see if this is in response to a SYN
1219 if self
._getState
() == 'SYNC':
1220 if params
[0].lower() == "a": self
._getStateData
('list').autoAdd
= 0
1221 elif params
[0].lower() == "n": self
._getStateData
('list').autoAdd
= 1
1222 else: raise MSNProtocolError
, "Invalid Paramater for GTC" # debug
1225 if params
[1].lower() == "a": self
._fireCallback
(id, 0)
1226 elif params
[1].lower() == "n": self
._fireCallback
(id, 1)
1227 else: raise MSNProtocolError
, "Invalid Paramater for GTC" # debug
1229 def handle_SYN(self
, params
):
1231 self
._setStateData
('phone', []) # Always needs to be set
1232 if params
[3] == 0: # No LST will be received. New account?
1233 self
._setState
('SESSION')
1234 self
._fireCallback
(id, None, None)
1236 contacts
= MSNContactList()
1237 self
._setStateData
('list', contacts
)
1238 self
._setStateData
('lst_reply', int(params
[3]))
1239 self
._setStateData
('lsg_reply', int(params
[4]))
1240 self
._setStateData
('lst_sofar', 0)
1242 def handle_LSG(self
, params
):
1243 if self
._getState
() == 'SYNC':
1244 self
._getStateData
('list').groups
[params
[1]] = unquote(params
[0])
1246 def handle_PRP(self
, params
):
1247 if params
[1] == "MFN":
1248 self
._fireCallback
(int(params
[0]))
1249 elif self
._getState
() == 'SYNC':
1250 self
._getStateData
('phone').append((params
[0], unquote(params
[1])))
1252 self
._fireCallback
(int(params
[0]), int(params
[1]), unquote(params
[3]))
1254 def handle_BPR(self
, params
):
1255 numParams
= len(params
)
1256 if numParams
== 2: # part of a syn
1257 self
._getStateData
('last_contact').setPhone(params
[0], unquote(params
[1]))
1258 elif numParams
== 4:
1259 if not self
.factory
.contacts
: raise MSNProtocolError
, "handle_BPR called with no contact list" # debug
1260 self
.factory
.contacts
.version
= int(params
[0])
1261 userHandle
, phoneType
, number
= params
[1], params
[2], unquote(params
[3])
1262 self
.factory
.contacts
.getContact(userHandle
).setPhone(phoneType
, number
)
1263 self
.gotPhoneNumber(userHandle
, phoneType
, number
)
1266 def handle_ADG(self
, params
):
1267 checkParamLen(len(params
), 5, 'ADG')
1269 if not self
._fireCallback
(id, int(params
[1]), unquote(params
[2]), int(params
[3])):
1270 raise MSNProtocolError
, "ADG response does not match up to a request" # debug
1272 def handle_RMG(self
, params
):
1273 checkParamLen(len(params
), 3, 'RMG')
1275 if not self
._fireCallback
(id, int(params
[1]), int(params
[2])):
1276 raise MSNProtocolError
, "RMG response does not match up to a request" # debug
1278 def handle_REG(self
, params
):
1279 checkParamLen(len(params
), 5, 'REG')
1281 if not self
._fireCallback
(id, int(params
[1]), int(params
[2]), unquote(params
[3])):
1282 raise MSNProtocolError
, "REG response does not match up to a request" # debug
1284 def handle_ADC(self
, params
):
1285 if not self
.factory
.contacts
: raise MSNProtocolError
, "handle_ADC called with no contact list"
1286 numParams
= len(params
)
1287 if numParams
< 3 or params
[1].upper() not in ('AL','BL','RL','FL','PL'):
1288 raise MSNProtocolError
, "Invalid Paramaters for ADC" # debug
1290 listType
= params
[1].lower()
1291 userHandle
, screenName
, userGuid
, ignored1
, groups
= getVals(params
[2:])
1293 if groups
and listType
.upper() != FORWARD_LIST
:
1294 raise MSNProtocolError
, "Only forward list can contain groups" # debug
1296 if not self
._fireCallback
(id, listCodeToID
[listType
], userGuid
, userHandle
, screenName
):
1297 c
= self
.factory
.contacts
.getContact(userHandle
)
1299 c
= MSNContact(userGuid
=userGuid
, userHandle
=userHandle
, screenName
=screenName
)
1300 self
.factory
.contacts
.addContact(c
)
1301 c
.addToList(PENDING_LIST
)
1302 self
.userAddedMe(userGuid
, userHandle
, screenName
)
1304 def handle_REM(self
, params
):
1305 if not self
.factory
.contacts
: raise MSNProtocolError
, "handle_REM called with no contact list available!"
1306 numParams
= len(params
)
1307 if numParams
< 3 or params
[1].upper() not in ('AL','BL','FL','RL','PL'):
1308 raise MSNProtocolError
, "Invalid Paramaters for REM" # debug
1310 listType
= params
[1].lower()
1311 userHandle
= params
[2]
1314 if params
[1] != "FL": raise MSNProtocolError
, "Only forward list can contain groups" # debug
1315 groupID
= int(params
[3])
1316 if not self
._fireCallback
(id, listCodeToID
[listType
], userHandle
, groupID
):
1317 if listType
.upper() != "RL": return
1318 c
= self
.factory
.contacts
.getContact(userHandle
)
1320 c
.removeFromList(REVERSE_LIST
)
1321 if c
.lists
== 0: self
.factory
.contacts
.remContact(c
.userHandle
)
1322 self
.userRemovedMe(userHandle
)
1324 def handle_XFR(self
, params
):
1325 checkParamLen(len(params
), 5, 'XFR')
1327 # check to see if they sent a host/port pair
1329 host
, port
= params
[2].split(':')
1334 if not self
._fireCallback
(id, host
, int(port
), params
[4]):
1335 raise MSNProtocolError
, "Got XFR (referral) that I didn't ask for .. should this happen?" # debug
1337 def handle_RNG(self
, params
):
1338 checkParamLen(len(params
), 6, 'RNG')
1339 # check for host:port pair
1341 host
, port
= params
[1].split(":")
1346 self
.gotSwitchboardInvitation(int(params
[0]), host
, port
, params
[3], params
[4],
1349 def handle_NOT(self
, params
):
1350 checkParamLen(len(params
), 1, 'NOT')
1352 messageLen
= int(params
[0])
1353 except ValueError: raise MSNProtocolError
, "Invalid Parameter for NOT length argument"
1354 self
.currentMessage
= MSNMessage(length
=messageLen
, userHandle
="NOTIFICATION", specialMessage
=True)
1357 def handle_UBX(self
, params
):
1358 checkParamLen(len(params
), 2, 'UBX')
1360 messageLen
= int(params
[1])
1361 except ValueError: raise MSNProtocolError
, "Invalid Parameter for UBX length argument"
1363 self
.currentMessage
= MSNMessage(length
=messageLen
, userHandle
=params
[0], screenName
="UBX", specialMessage
=True)
1366 self
.contactPersonalChanged(params
[0], '')
1368 def handle_UUX(self
, params
):
1369 checkParamLen(len(params
), 2, 'UUX')
1370 if params
[1] != '0': return
1372 self
._fireCallback
(id)
1374 def handle_OUT(self
, params
):
1375 checkParamLen(len(params
), 1, 'OUT')
1376 if params
[0] == "OTH": self
.multipleLogin()
1377 elif params
[0] == "SSD": self
.serverGoingDown()
1378 else: raise MSNProtocolError
, "Invalid Parameters received for OUT" # debug
1380 def handle_QNG(self
, params
):
1381 self
.pingCounter
= 0 # They replied to a ping. We'll forgive them for any they may have missed, because they're alive again now
1385 def pingChecker(self
):
1386 if self
.pingCounter
> 5:
1387 # The server has ignored 5 pings, lets kill the connection
1388 self
.transport
.loseConnection()
1390 self
.sendLine("PNG")
1391 self
.pingCounter
+= 1
1393 def pingCheckerStart(self
, *args
):
1394 self
.pingCheckTask
= task
.LoopingCall(self
.pingChecker
)
1395 self
.pingCheckTask
.start(PINGSPEED
)
1397 def loggedIn(self
, userHandle
, verified
):
1399 Called when the client has logged in.
1400 The default behaviour of this method is to
1401 update the factory with our screenName and
1402 to sync the contact list (factory.contacts).
1403 When this is complete self.listSynchronized
1406 @param userHandle: our userHandle
1407 @param verified: 1 if our passport has been (verified), 0 if not.
1408 (i'm not sure of the significace of this)
1412 d
.addCallback(self
.listSynchronized
)
1413 d
.addCallback(self
.pingCheckerStart
)
1415 def loginFailure(self
, message
):
1417 Called when the client fails to login.
1419 @param message: a message indicating the problem that was encountered
1423 def gotProfile(self
, message
):
1425 Called after logging in when the server sends an initial
1426 message with MSN/passport specific profile information
1427 such as country, number of kids, etc.
1428 Check the message headers for the specific values.
1430 @param message: The profile message
1434 def listSynchronized(self
, *args
):
1436 Lists are now synchronized by default upon logging in, this
1437 method is called after the synchronization has finished
1438 and the factory now has the up-to-date contacts.
1442 def contactAvatarChanged(self
, userHandle
, hash):
1444 Called when we receive the first, or a new <msnobj/> from a
1447 @param userHandle: contact who's msnobj has been changed
1448 @param hash: sha1 hash of their avatar
1451 def statusChanged(self
, statusCode
):
1453 Called when our status changes and its not in response to a
1456 @param statusCode: 3-letter status code
1460 def gotContactStatus(self
, userHandle
, statusCode
, screenName
):
1462 Called when we receive a list of statuses upon login.
1464 @param userHandle: the contact's user handle (passport)
1465 @param statusCode: 3-letter status code
1466 @param screenName: the contact's screen name
1470 def contactStatusChanged(self
, userHandle
, statusCode
, screenName
):
1472 Called when we're notified that a contact's status has changed.
1474 @param userHandle: the contact's user handle (passport)
1475 @param statusCode: 3-letter status code
1476 @param screenName: the contact's screen name
1480 def contactPersonalChanged(self
, userHandle
, personal
):
1482 Called when a contact's personal message changes.
1484 @param userHandle: the contact who changed their personal message
1485 @param personal : the new personal message
1489 def contactOffline(self
, userHandle
):
1491 Called when a contact goes offline.
1493 @param userHandle: the contact's user handle
1497 def gotMessage(self
, message
):
1499 Called when there is a message from the notification server
1500 that is not understood by default.
1502 @param message: the MSNMessage.
1506 def gotMSNAlert(self
, body
, action
, subscr
):
1508 Called when the server sends an MSN Alert (http://alerts.msn.com)
1510 @param body : the alert text
1511 @param action: a URL with more information for the user to view
1512 @param subscr: a URL the user can use to modify their alert subscription
1516 def gotInitialEmailNotification(self
, inboxunread
, foldersunread
):
1518 Called when the server sends you details about your hotmail
1519 inbox. This is only ever called once, on login.
1521 @param inboxunread : the number of unread items in your inbox
1522 @param foldersunread: the number of unread items in other folders
1526 def gotRealtimeEmailNotification(self
, mailfrom
, fromaddr
, subject
):
1528 Called when the server sends us realtime email
1529 notification. This means that you have received
1530 a new email in your hotmail inbox.
1532 @param mailfrom: the sender of the email
1533 @param fromaddr: the sender of the email (I don't know :P)
1534 @param subject : the email subject
1538 def gotPhoneNumber(self
, userHandle
, phoneType
, number
):
1540 Called when the server sends us phone details about
1541 a specific user (for example after a user is added
1542 the server will send their status, phone details etc.
1544 @param userHandle: the contact's user handle (passport)
1545 @param phoneType: the specific phoneType
1546 (*_PHONE constants or HAS_PAGER)
1547 @param number: the value/phone number.
1551 def userAddedMe(self
, userGuid
, userHandle
, screenName
):
1553 Called when a user adds me to their list. (ie. they have been added to
1556 @param userHandle: the userHandle of the user
1557 @param screenName: the screen name of the user
1561 def userRemovedMe(self
, userHandle
):
1563 Called when a user removes us from their contact list
1564 (they are no longer on our reverseContacts list.
1566 @param userHandle: the contact's user handle (passport)
1570 def gotSwitchboardInvitation(self
, sessionID
, host
, port
,
1571 key
, userHandle
, screenName
):
1573 Called when we get an invitation to a switchboard server.
1574 This happens when a user requests a chat session with us.
1576 @param sessionID: session ID number, must be remembered for logging in
1577 @param host: the hostname of the switchboard server
1578 @param port: the port to connect to
1579 @param key: used for authorization when connecting
1580 @param userHandle: the user handle of the person who invited us
1581 @param screenName: the screen name of the person who invited us
1585 def multipleLogin(self
):
1587 Called when the server says there has been another login
1588 under our account, the server should disconnect us right away.
1592 def serverGoingDown(self
):
1594 Called when the server has notified us that it is going down for
1601 def changeStatus(self
, status
):
1603 Change my current status. This method will add
1604 a default callback to the returned Deferred
1605 which will update the status attribute of the
1608 @param status: 3-letter status code (as defined by
1609 the STATUS_* constants)
1610 @return: A Deferred, the callback of which will be
1611 fired when the server confirms the change
1612 of status. The callback argument will be
1613 a tuple with the new status code as the
1617 id, d
= self
._createIDMapping
()
1618 self
.sendLine("CHG %s %s %s %s" % (id, status
, str(MSNContact
.MSNC1 | MSNContact
.MSNC2 | MSNContact
.MSNC3 | MSNContact
.MSNC4
), quote(self
.msnobj
.text
)))
1620 self
.factory
.status
= r
[0]
1622 return d
.addCallback(_cb
)
1624 def setPrivacyMode(self
, privLevel
):
1626 Set my privacy mode on the server.
1629 This only keeps the current privacy setting on
1630 the server for later retrieval, it does not
1631 effect the way the server works at all.
1633 @param privLevel: This parameter can be true, in which
1634 case the server will keep the state as
1635 'al' which the official client interprets
1636 as -> allow messages from only users on
1637 the allow list. Alternatively it can be
1638 false, in which case the server will keep
1639 the state as 'bl' which the official client
1640 interprets as -> allow messages from all
1641 users except those on the block list.
1643 @return: A Deferred, the callback of which will be fired when
1644 the server replies with the new privacy setting.
1645 The callback argument will be a tuple, the only element
1646 of which being either 'al' or 'bl' (the new privacy setting).
1649 id, d
= self
._createIDMapping
()
1650 if privLevel
: self
.sendLine("BLP %s AL" % id)
1651 else: self
.sendLine("BLP %s BL" % id)
1656 Used for keeping an up-to-date contact list.
1657 A callback is added to the returned Deferred
1658 that updates the contact list on the factory
1659 and also sets my state to STATUS_ONLINE.
1662 This is called automatically upon signing
1663 in using the version attribute of
1664 factory.contacts, so you may want to persist
1665 this object accordingly. Because of this there
1666 is no real need to ever call this method
1669 @return: A Deferred, the callback of which will be
1670 fired when the server sends an adequate reply.
1671 The callback argument will be a tuple with two
1672 elements, the new list (MSNContactList) and
1673 your current state (a dictionary). If the version
1674 you sent _was_ the latest list version, both elements
1675 will be None. To just request the list send a version of 0.
1678 self
._setState
('SYNC')
1679 id, d
= self
._createIDMapping
(data
=None)
1680 self
._setStateData
('synid',id)
1681 self
.sendLine("SYN %s %s %s" % (id, 0, 0))
1683 self
.changeStatus(STATUS_ONLINE
)
1684 if r
[0] is not None:
1685 self
.factory
.contacts
= r
[0]
1687 return d
.addCallback(_cb
)
1689 def setPhoneDetails(self
, phoneType
, value
):
1691 Set/change my phone numbers stored on the server.
1693 @param phoneType: phoneType can be one of the following
1694 constants - HOME_PHONE, WORK_PHONE,
1695 MOBILE_PHONE, HAS_PAGER.
1696 These are pretty self-explanatory, except
1697 maybe HAS_PAGER which refers to whether or
1698 not you have a pager.
1699 @param value: for all of the *_PHONE constants the value is a
1700 phone number (str), for HAS_PAGER accepted values
1701 are 'Y' (for yes) and 'N' (for no).
1703 @return: A Deferred, the callback for which will be fired when
1704 the server confirms the change has been made. The
1705 callback argument will be a tuple with 2 elements, the
1706 first being the new list version (int) and the second
1707 being the new phone number value (str).
1709 raise "ProbablyDoesntWork"
1710 # XXX: Add a default callback which updates
1711 # factory.contacts.version and the relevant phone
1713 id, d
= self
._createIDMapping
()
1714 self
.sendLine("PRP %s %s %s" % (id, phoneType
, quote(value
)))
1717 def addListGroup(self
, name
):
1719 Used to create a new list group.
1720 A default callback is added to the
1721 returned Deferred which updates the
1722 contacts attribute of the factory.
1724 @param name: The desired name of the new group.
1726 @return: A Deferred, the callbacck for which will be called
1727 when the server clarifies that the new group has been
1728 created. The callback argument will be a tuple with 3
1729 elements: the new list version (int), the new group name
1730 (str) and the new group ID (int).
1733 raise "ProbablyDoesntWork"
1734 id, d
= self
._createIDMapping
()
1735 self
.sendLine("ADG %s %s 0" % (id, quote(name
)))
1737 if self
.factory
.contacts
:
1738 self
.factory
.contacts
.version
= r
[0]
1739 self
.factory
.contacts
.setGroup(r
[1], r
[2])
1741 return d
.addCallback(_cb
)
1743 def remListGroup(self
, groupID
):
1745 Used to remove a list group.
1746 A default callback is added to the
1747 returned Deferred which updates the
1748 contacts attribute of the factory.
1750 @param groupID: the ID of the desired group to be removed.
1752 @return: A Deferred, the callback for which will be called when
1753 the server clarifies the deletion of the group.
1754 The callback argument will be a tuple with 2 elements:
1755 the new list version (int) and the group ID (int) of
1759 raise "ProbablyDoesntWork"
1760 id, d
= self
._createIDMapping
()
1761 self
.sendLine("RMG %s %s" % (id, groupID
))
1763 self
.factory
.contacts
.version
= r
[0]
1764 self
.factory
.contacts
.remGroup(r
[1])
1766 return d
.addCallback(_cb
)
1768 def renameListGroup(self
, groupID
, newName
):
1770 Used to rename an existing list group.
1771 A default callback is added to the returned
1772 Deferred which updates the contacts attribute
1775 @param groupID: the ID of the desired group to rename.
1776 @param newName: the desired new name for the group.
1778 @return: A Deferred, the callback for which will be called
1779 when the server clarifies the renaming.
1780 The callback argument will be a tuple of 3 elements,
1781 the new list version (int), the group id (int) and
1782 the new group name (str).
1785 raise "ProbablyDoesntWork"
1786 id, d
= self
._createIDMapping
()
1787 self
.sendLine("REG %s %s %s 0" % (id, groupID
, quote(newName
)))
1789 self
.factory
.contacts
.version
= r
[0]
1790 self
.factory
.contacts
.setGroup(r
[1], r
[2])
1792 return d
.addCallback(_cb
)
1794 def addContact(self
, listType
, userHandle
):
1796 Used to add a contact to the desired list.
1797 A default callback is added to the returned
1798 Deferred which updates the contacts attribute of
1799 the factory with the new contact information.
1800 If you are adding a contact to the forward list
1801 and you want to associate this contact with multiple
1802 groups then you will need to call this method for each
1803 group you would like to add them to, changing the groupID
1804 parameter. The default callback will take care of updating
1805 the group information on the factory's contact list.
1807 @param listType: (as defined by the *_LIST constants)
1808 @param userHandle: the user handle (passport) of the contact
1811 @return: A Deferred, the callback for which will be called when
1812 the server has clarified that the user has been added.
1813 The callback argument will be a tuple with 4 elements:
1814 the list type, the contact's user handle, the new list
1815 version, and the group id (if relevant, otherwise it
1819 id, d
= self
._createIDMapping
()
1820 try: # Make sure the contact isn't actually on the list
1821 if self
.factory
.contacts
.getContact(userHandle
).lists
& listType
: return
1822 except AttributeError: pass
1823 listType
= listIDToCode
[listType
].upper()
1824 if listType
== "FL":
1825 self
.sendLine("ADC %s %s N=%s F=%s" % (id, listType
, userHandle
, userHandle
))
1827 self
.sendLine("ADC %s %s N=%s" % (id, listType
, userHandle
))
1830 if not self
.factory
: return
1831 c
= self
.factory
.contacts
.getContact(r
[2])
1833 c
= MSNContact(userGuid
=r
[1], userHandle
=r
[2], screenName
=r
[3])
1834 self
.factory
.contacts
.addContact(c
)
1835 #if r[3]: c.groups.append(r[3])
1838 return d
.addCallback(_cb
)
1840 def remContact(self
, listType
, userHandle
):
1842 Used to remove a contact from the desired list.
1843 A default callback is added to the returned deferred
1844 which updates the contacts attribute of the factory
1845 to reflect the new contact information.
1847 @param listType: (as defined by the *_LIST constants)
1848 @param userHandle: the user handle (passport) of the
1849 contact being removed
1851 @return: A Deferred, the callback for which will be called when
1852 the server has clarified that the user has been removed.
1853 The callback argument will be a tuple of 3 elements:
1854 the list type, the contact's user handle and the group ID
1855 (if relevant, otherwise it will be None)
1858 id, d
= self
._createIDMapping
()
1859 try: # Make sure the contact is actually on this list
1860 if not (self
.factory
.contacts
.getContact(userHandle
).lists
& listType
): return
1861 except AttributeError: return
1862 listType
= listIDToCode
[listType
].upper()
1863 if listType
== "FL":
1865 c
= self
.factory
.contacts
.getContact(userHandle
)
1866 userGuid
= c
.userGuid
1867 except AttributeError: return
1868 self
.sendLine("REM %s FL %s" % (id, userGuid
))
1870 self
.sendLine("REM %s %s %s" % (id, listType
, userHandle
))
1873 if listType
== "FL":
1874 r
= (r
[0], userHandle
, r
[2]) # make sure we always get a userHandle
1875 l
= self
.factory
.contacts
1876 c
= l
.getContact(r
[1])
1880 if group
: # they may not have been removed from the list
1881 c
.groups
.remove(group
)
1882 if c
.groups
: shouldRemove
= 0
1884 c
.removeFromList(r
[0])
1885 if c
.lists
== 0: l
.remContact(c
.userHandle
)
1887 return d
.addCallback(_cb
)
1889 def changeScreenName(self
, newName
):
1891 Used to change your current screen name.
1892 A default callback is added to the returned
1893 Deferred which updates the screenName attribute
1894 of the factory and also updates the contact list
1897 @param newName: the new screen name
1899 @return: A Deferred, the callback for which will be called
1900 when the server acknowledges the change.
1901 The callback argument will be an empty tuple.
1904 id, d
= self
._createIDMapping
()
1905 self
.sendLine("PRP %s MFN %s" % (id, quote(newName
)))
1907 self
.factory
.screenName
= newName
1909 return d
.addCallback(_cb
)
1911 def changePersonalMessage(self
, personal
):
1913 Used to change your personal message.
1915 @param personal: the new screen name
1917 @return: A Deferred, the callback for which will be called
1918 when the server acknowledges the change.
1919 The callback argument will be a tuple of 1 element,
1920 the personal message.
1923 id, d
= self
._createIDMapping
()
1926 data
= "<Data><PSM>" + personal
+ "</PSM><CurrentMedia></CurrentMedia></Data>"
1927 self
.sendLine("UUX %s %s" % (id, len(data
)))
1928 self
.transport
.write(data
)
1930 self
.factory
.personal
= personal
1932 return d
.addCallback(_cb
)
1934 def changeAvatar(self
, imageData
, push
):
1936 Used to change the avatar that other users see.
1938 @param imageData: the PNG image data to set as the avatar
1939 @param push : whether to push the update to the server
1940 (it will otherwise be sent with the next
1943 @return: If push==True, a Deferred, the callback for which
1944 will be called when the server acknowledges the change.
1945 The callback argument will be the same as for changeStatus.
1948 if self
.msnobj
and imageData
== self
.msnobj
.imageData
: return
1950 self
.msnobj
.setData(self
.factory
.userHandle
, imageData
)
1952 self
.msnobj
.setNull()
1953 if push
: return self
.changeStatus(self
.factory
.status
) # Push to server
1956 def requestSwitchboardServer(self
):
1958 Used to request a switchboard server to use for conversations.
1960 @return: A Deferred, the callback for which will be called when
1961 the server responds with the switchboard information.
1962 The callback argument will be a tuple with 3 elements:
1963 the host of the switchboard server, the port and a key
1964 used for logging in.
1967 id, d
= self
._createIDMapping
()
1968 self
.sendLine("XFR %s SB" % id)
1973 Used to log out of the notification server.
1974 After running the method the server is expected
1975 to close the connection.
1978 if self
.pingCheckTask
:
1979 self
.pingCheckTask
.stop()
1980 self
.pingCheckTask
= None
1981 self
.sendLine("OUT")
1982 self
.transport
.loseConnection()
1984 class NotificationFactory(ClientFactory
):
1986 Factory for the NotificationClient protocol.
1987 This is basically responsible for keeping
1988 the state of the client and thus should be used
1989 in a 1:1 situation with clients.
1991 @ivar contacts: An MSNContactList instance reflecting
1992 the current contact list -- this is
1993 generally kept up to date by the default
1995 @ivar userHandle: The client's userHandle, this is expected
1996 to be set by the client and is used by the
1997 protocol (for logging in etc).
1998 @ivar screenName: The client's current screen-name -- this is
1999 generally kept up to date by the default
2001 @ivar password: The client's password -- this is (obviously)
2002 expected to be set by the client.
2003 @ivar passportServer: This must point to an msn passport server
2004 (the whole URL is required)
2005 @ivar status: The status of the client -- this is generally kept
2006 up to date by the default command handlers
2013 passportServer
= 'https://nexus.passport.com/rdr/pprdr.asp'
2015 protocol
= NotificationClient
2018 class SwitchboardClient(MSNEventBase
):
2020 This class provides support for clients connecting to a switchboard server.
2022 Switchboard servers are used for conversations with other people
2023 on the MSN network. This means that the number of conversations at
2024 any given time will be directly proportional to the number of
2025 connections to varioius switchboard servers.
2027 MSN makes no distinction between single and group conversations,
2028 so any number of users may be invited to join a specific conversation
2029 taking place on a switchboard server.
2031 @ivar key: authorization key, obtained when receiving
2032 invitation / requesting switchboard server.
2033 @ivar userHandle: your user handle (passport)
2034 @ivar sessionID: unique session ID, used if you are replying
2035 to a switchboard invitation
2036 @ivar reply: set this to 1 in connectionMade or before to signifiy
2037 that you are replying to a switchboard invitation.
2038 @ivar msnobj: the MSNObject for the user's avatar. So that the
2039 switchboard can distribute it to anyone who asks.
2051 MSNEventBase
.__init
__(self
)
2052 self
.pendingUsers
= {}
2053 self
.cookies
= {'iCookies' : {}} # will maybe be moved to a factory in the future
2056 def connectionMade(self
):
2057 MSNEventBase
.connectionMade(self
)
2060 def connectionLost(self
, reason
):
2061 self
.cookies
['iCookies'] = {}
2062 MSNEventBase
.connectionLost(self
, reason
)
2064 def _sendInit(self
):
2066 send initial data based on whether we are replying to an invitation
2069 id = self
._nextTransactionID
()
2071 self
.sendLine("USR %s %s %s" % (id, self
.userHandle
, self
.key
))
2073 self
.sendLine("ANS %s %s %s %s" % (id, self
.userHandle
, self
.key
, self
.sessionID
))
2075 def _newInvitationCookie(self
):
2077 if self
._iCookie
> 1000: self
._iCookie
= 1
2078 return self
._iCookie
2080 def _checkTyping(self
, message
, cTypes
):
2081 """ helper method for checkMessage """
2082 if 'text/x-msmsgscontrol' in cTypes
and message
.hasHeader('TypingUser'):
2083 self
.gotContactTyping(message
)
2086 def _checkFileInvitation(self
, message
, info
):
2087 """ helper method for checkMessage """
2088 if not info
.get('Application-GUID', '').upper() == MSN_MSNFTP_GUID
: return 0
2090 cookie
= info
['Invitation-Cookie']
2091 filename
= info
['Application-File']
2092 filesize
= int(info
['Application-FileSize'])
2093 connectivity
= (info
.get('Connectivity').lower() == 'y')
2095 log
.msg('Received munged file transfer request ... ignoring.')
2097 raise NotImplementedError
2098 self
.gotSendRequest(msnft
.MSNFTP_Receive(filename
, filesize
, message
.userHandle
, cookie
, connectivity
, self
))
2101 def _handleP2PMessage(self
, message
):
2102 """ helper method for msnslp messages (file transfer & avatars) """
2103 if not message
.getHeader("P2P-Dest") == self
.userHandle
: return
2104 packet
= message
.message
2105 binaryFields
= BinaryFields(packet
=packet
)
2106 if binaryFields
[0] != 0:
2107 slpLink
= self
.slpLinks
.get(binaryFields
[0])
2109 # Link has been killed. Ignore
2111 if slpLink
.remoteUser
== message
.userHandle
:
2112 slpLink
.handlePacket(packet
)
2113 if binaryFields
[5] == BinaryFields
.ACK
or binaryFields
[5] == BinaryFields
.BYEGOT
:
2114 pass # Ignore the ACKs to SLP messages
2116 slpMessage
= MSNSLPMessage(packet
)
2118 # Always try and give a slpMessage to a slpLink first.
2119 # If none can be found, and it was INVITE, then create
2120 # one to handle the session.
2121 for slpLink
in self
.slpLinks
.values():
2122 if slpLink
.sessionGuid
== slpMessage
.sessionGuid
:
2123 slpLink
.handleSLPMessage(slpMessage
)
2126 slpLink
= None # Was not handled
2128 if not slpLink
and slpMessage
.method
== "INVITE":
2129 if slpMessage
.euf_guid
== MSN_MSNFTP_GUID
:
2130 context
= FileContext(slpMessage
.context
)
2131 slpLink
= SLPLink_FileReceive(remoteUser
=slpMessage
.fro
, switchboard
=self
, filename
=context
.filename
, filesize
=context
.filesize
, sessionID
=slpMessage
.sessionID
, sessionGuid
=slpMessage
.sessionGuid
, branch
=slpMessage
.branch
)
2132 self
.slpLinks
[slpMessage
.sessionID
] = slpLink
2133 self
.gotFileReceive(slpLink
)
2134 elif slpMessage
.euf_guid
== MSN_AVATAR_GUID
:
2135 # Check that we have an avatar to send
2137 slpLink
= SLPLink_AvatarSend(remoteUser
=slpMessage
.fro
, switchboard
=self
, filesize
=self
.msnobj
.size
, sessionID
=slpMessage
.sessionID
, sessionGuid
=slpMessage
.sessionGuid
)
2138 slpLink
.write(self
.msnobj
.imageData
)
2141 # They shouldn't have sent a request if we have
2142 # no avatar. So we'll just ignore them.
2143 # FIXME We should really send an error
2146 self
.slpLinks
[slpMessage
.sessionID
] = slpLink
2148 # Always need to ACK these packets if we can
2149 slpLink
.sendP2PACK(binaryFields
)
2152 def checkMessage(self
, message
):
2154 hook for detecting any notification type messages
2155 (e.g. file transfer)
2157 cTypes
= [s
.lstrip() for s
in message
.getHeader('Content-Type').split(';')]
2158 if self
._checkTyping
(message
, cTypes
): return 0
2159 if 'text/x-msmsgsinvite' in cTypes
:
2160 # header like info is sent as part of the message body.
2162 for line
in message
.message
.split('\r\n'):
2164 key
, val
= line
.split(':')
2165 info
[key
] = val
.lstrip()
2166 except ValueError: continue
2167 if self
._checkFileInvitation
(message
, info
): return 0
2168 elif 'application/x-msnmsgrp2p' in cTypes
:
2169 self
._handleP
2PMessage
(message
)
2174 def handle_USR(self
, params
):
2175 checkParamLen(len(params
), 4, 'USR')
2176 if params
[1] == "OK":
2180 def handle_CAL(self
, params
):
2181 checkParamLen(len(params
), 3, 'CAL')
2183 if params
[1].upper() == "RINGING":
2184 self
._fireCallback
(id, int(params
[2])) # session ID as parameter
2187 def handle_JOI(self
, params
):
2188 checkParamLen(len(params
), 2, 'JOI')
2189 self
.userJoined(params
[0], unquote(params
[1]))
2191 # users participating in the current chat
2192 def handle_IRO(self
, params
):
2193 checkParamLen(len(params
), 5, 'IRO')
2194 self
.pendingUsers
[params
[3]] = unquote(params
[4])
2195 if params
[1] == params
[2]:
2196 self
.gotChattingUsers(self
.pendingUsers
)
2197 self
.pendingUsers
= {}
2199 # finished listing users
2200 def handle_ANS(self
, params
):
2201 checkParamLen(len(params
), 2, 'ANS')
2202 if params
[1] == "OK":
2205 def handle_ACK(self
, params
):
2206 checkParamLen(len(params
), 1, 'ACK')
2207 self
._fireCallback
(int(params
[0]), None)
2209 def handle_NAK(self
, params
):
2210 checkParamLen(len(params
), 1, 'NAK')
2211 self
._fireCallback
(int(params
[0]), None)
2213 def handle_BYE(self
, params
):
2214 #checkParamLen(len(params), 1, 'BYE') # i've seen more than 1 param passed to this
2215 self
.userLeft(params
[0])
2221 called when all login details have been negotiated.
2222 Messages can now be sent, or new users invited.
2226 def gotChattingUsers(self
, users
):
2228 called after connecting to an existing chat session.
2230 @param users: A dict mapping user handles to screen names
2231 (current users taking part in the conversation)
2235 def userJoined(self
, userHandle
, screenName
):
2237 called when a user has joined the conversation.
2239 @param userHandle: the user handle (passport) of the user
2240 @param screenName: the screen name of the user
2244 def userLeft(self
, userHandle
):
2246 called when a user has left the conversation.
2248 @param userHandle: the user handle (passport) of the user.
2252 def gotMessage(self
, message
):
2254 called when we receive a message.
2256 @param message: the associated MSNMessage object
2260 def gotFileReceive(self
, fileReceive
):
2262 called when we receive a file send request from a contact.
2263 Default action is to reject the file.
2265 @param fileReceive: msnft.MSNFTReceive_Base instance
2267 fileReceive
.reject()
2270 def gotSendRequest(self
, fileReceive
):
2272 called when we receive a file send request from a contact
2274 @param fileReceive: msnft.MSNFTReceive_Base instance
2278 def gotContactTyping(self
, message
):
2280 called when we receive the special type of message notifying
2281 us that a contact is typing a message.
2283 @param message: the associated MSNMessage object
2289 def inviteUser(self
, userHandle
):
2291 used to invite a user to the current switchboard server.
2293 @param userHandle: the user handle (passport) of the desired user.
2295 @return: A Deferred, the callback for which will be called
2296 when the server notifies us that the user has indeed
2297 been invited. The callback argument will be a tuple
2298 with 1 element, the sessionID given to the invited user.
2299 I'm not sure if this is useful or not.
2302 id, d
= self
._createIDMapping
()
2303 self
.sendLine("CAL %s %s" % (id, userHandle
))
2306 def sendMessage(self
, message
):
2308 used to send a message.
2310 @param message: the corresponding MSNMessage object.
2312 @return: Depending on the value of message.ack.
2313 If set to MSNMessage.MESSAGE_ACK or
2314 MSNMessage.MESSAGE_NACK a Deferred will be returned,
2315 the callback for which will be fired when an ACK or
2316 NACK is received - the callback argument will be
2317 (None,). If set to MSNMessage.MESSAGE_ACK_NONE then
2318 the return value is None.
2321 if message
.ack
not in ('A','N','D'): id, d
= self
._nextTransactionID
(), None
2322 else: id, d
= self
._createIDMapping
()
2323 if message
.length
== 0: message
.length
= message
._calcMessageLen
()
2324 self
.sendLine("MSG %s %s %s" % (id, message
.ack
, message
.length
))
2325 # apparently order matters with at least MIME-Version and Content-Type
2326 self
.sendLine('MIME-Version: %s' % message
.getHeader('MIME-Version'))
2327 self
.sendLine('Content-Type: %s' % message
.getHeader('Content-Type'))
2328 # send the rest of the headers
2329 for header
in [h
for h
in message
.headers
.items() if h
[0].lower() not in ('mime-version','content-type')]:
2330 self
.sendLine("%s: %s" % (header
[0], header
[1]))
2331 self
.transport
.write("\r\n")
2332 self
.transport
.write(message
.message
)
2333 if MESSAGEDEBUG
: log
.msg(message
.message
)
2336 def sendAvatarRequest(self
, msnContact
):
2338 used to request an avatar from a user in this switchboard
2341 @param msnContact: the msnContact object to request an avatar for
2343 @return: A Deferred, the callback for which will be called
2344 when the avatar transfer succeeds.
2345 The callback argument will be a tuple with one element,
2346 the PNG avatar data.
2348 if not msnContact
.msnobj
: return
2350 def bufferClosed(data
):
2352 buffer = StringBuffer(bufferClosed
)
2353 slpLink
= SLPLink_AvatarReceive(remoteUser
=msnContact
.userHandle
, switchboard
=self
, consumer
=buffer, context
=msnContact
.msnobj
.text
)
2354 self
.slpLinks
[slpLink
.sessionID
] = slpLink
2357 def sendFile(self
, msnContact
, filename
, filesize
):
2359 used to send a file to a contact.
2361 @param msnContact: the MSNContact object to send a file to.
2362 @param filename: the name of the file to send.
2363 @param filesize: the size of the file to send.
2365 @return: (fileSend, d) A FileSend object and a Deferred.
2366 The Deferred will pass one argument in a tuple,
2367 whether or not the transfer is accepted. If you
2368 receive a True, then you can call write() on the
2369 fileSend object to send your file. Call close()
2370 when the file is done.
2371 NOTE: You MUST write() exactly as much as you
2372 declare in filesize.
2374 if not msnContact
.userHandle
: return
2375 # FIXME, check msnContact.caps to see if we should use old-style
2376 fileSend
= SLPLink_FileSend(remoteUser
=msnContact
.userHandle
, switchboard
=self
, filename
=filename
, filesize
=filesize
)
2377 self
.slpLinks
[fileSend
.sessionID
] = fileSend
2378 return fileSend
, fileSend
.acceptDeferred
2380 def sendTypingNotification(self
):
2382 Used to send a typing notification. Upon receiving this
2383 message the official client will display a 'user is typing'
2384 message to all other users in the chat session for 10 seconds.
2385 You should send one of these every 5 seconds as long as the
2389 m
.ack
= m
.MESSAGE_ACK_NONE
2390 m
.setHeader('Content-Type', 'text/x-msmsgscontrol')
2391 m
.setHeader('TypingUser', self
.userHandle
)
2395 def sendFileInvitation(self
, fileName
, fileSize
):
2397 send an notification that we want to send a file.
2399 @param fileName: the file name
2400 @param fileSize: the file size
2402 @return: A Deferred, the callback of which will be fired
2403 when the user responds to this invitation with an
2404 appropriate message. The callback argument will be
2405 a tuple with 3 elements, the first being 1 or 0
2406 depending on whether they accepted the transfer
2407 (1=yes, 0=no), the second being an invitation cookie
2408 to identify your follow-up responses and the third being
2409 the message 'info' which is a dict of information they
2410 sent in their reply (this doesn't really need to be used).
2411 If you wish to proceed with the transfer see the
2412 sendTransferInfo method.
2414 cookie
= self
._newInvitationCookie
()
2417 m
.setHeader('Content-Type', 'text/x-msmsgsinvite; charset=UTF-8')
2418 m
.message
+= 'Application-Name: File Transfer\r\n'
2419 m
.message
+= 'Application-GUID: %s\r\n' % MSN_MSNFTP_GUID
2420 m
.message
+= 'Invitation-Command: INVITE\r\n'
2421 m
.message
+= 'Invitation-Cookie: %s\r\n' % str(cookie
)
2422 m
.message
+= 'Application-File: %s\r\n' % fileName
2423 m
.message
+= 'Application-FileSize: %s\r\n\r\n' % str(fileSize
)
2424 m
.ack
= m
.MESSAGE_ACK_NONE
2426 self
.cookies
['iCookies'][cookie
] = (d
, m
)
2429 def sendTransferInfo(self
, accept
, iCookie
, authCookie
, ip
, port
):
2431 send information relating to a file transfer session.
2433 @param accept: whether or not to go ahead with the transfer
2435 @param iCookie: the invitation cookie of previous replies
2436 relating to this transfer
2437 @param authCookie: the authentication cookie obtained from
2438 an FileSend instance
2440 @param port: the port on which an FileSend protocol is listening.
2443 m
.setHeader('Content-Type', 'text/x-msmsgsinvite; charset=UTF-8')
2444 m
.message
+= 'Invitation-Command: %s\r\n' % (accept
and 'ACCEPT' or 'CANCEL')
2445 m
.message
+= 'Invitation-Cookie: %s\r\n' % iCookie
2446 m
.message
+= 'IP-Address: %s\r\n' % ip
2447 m
.message
+= 'Port: %s\r\n' % port
2448 m
.message
+= 'AuthCookie: %s\r\n' % authCookie
2450 m
.ack
= m
.MESSAGE_NACK
2455 def __init__(self
, filename
, filesize
, userHandle
):
2456 self
.consumer
= None
2457 self
.finished
= False
2460 self
.filename
, self
.filesize
, self
.userHandle
= filename
, filesize
, userHandle
2463 raise NotImplementedError
2465 def accept(self
, consumer
):
2466 if self
.consumer
: raise "AlreadyAccepted"
2467 self
.consumer
= consumer
2468 for data
in self
.buffer:
2469 self
.consumer
.write(data
)
2472 self
.consumer
.close()
2474 self
.consumer
.error()
2476 def write(self
, data
):
2477 if self
.error
or self
.finished
:
2478 raise IOError, "Attempt to write in an invalid state"
2480 self
.consumer
.write(data
)
2482 self
.buffer.append(data
)
2485 self
.finished
= True
2487 self
.consumer
.close()
2490 """ Represents the Context field for P2P file transfers """
2491 def __init__(self
, data
=""):
2499 if MSNP2PDEBUG
: log
.msg("FileContext packing:", self
.filename
, self
.filesize
)
2500 data
= struct
.pack("<LLQL", 638, 0x03, self
.filesize
, 0x01)
2501 data
= data
[:-1] # Uck, weird, but it works
2502 data
+= utf16net(self
.filename
)
2503 data
= ljust(data
, 570, '\0')
2504 data
+= struct
.pack("<L", 0xFFFFFFFFL
)
2505 data
= ljust(data
, 638, '\0')
2508 def parse(self
, packet
):
2509 self
.filesize
= struct
.unpack("<Q", packet
[8:16])[0]
2510 chunk
= packet
[19:540]
2511 chunk
= chunk
[:chunk
.find('\x00\x00')]
2512 self
.filename
= unicode((codecs
.BOM_UTF16_BE
+ chunk
).decode("utf-16"))
2513 if MSNP2PDEBUG
: log
.msg("FileContext parsed:", self
.filesize
, self
.filename
)
2517 """ Utility class for the binary header & footer in p2p messages """
2526 def __init__(self
, fields
=None, packet
=None):
2528 self
.fields
= fields
2530 self
.fields
= [0] * 10
2532 self
.unpackFields(packet
)
2534 def __getitem__(self
, key
):
2535 return self
.fields
[key
]
2537 def __setitem__(self
, key
, value
):
2538 self
.fields
[key
] = value
2540 def unpackFields(self
, packet
):
2541 self
.fields
= struct
.unpack("<LLQQLLLLQ", packet
[0:48])
2542 self
.fields
+= struct
.unpack(">L", packet
[len(packet
)-4:])
2544 out
= "Unpacked fields: "
2545 for i
in self
.fields
:
2549 def packHeaders(self
):
2550 f
= tuple(self
.fields
)
2552 out
= "Packed fields: "
2553 for i
in self
.fields
:
2556 return struct
.pack("<LLQQLLLLQ", f
[0], f
[1], f
[2], f
[3], f
[4], f
[5], f
[6], f
[7], f
[8])
2558 def packFooter(self
):
2559 return struct
.pack(">L", self
.fields
[9])
2562 class MSNSLPMessage
:
2563 """ Representation of a single MSNSLP message """
2564 def __init__(self
, packet
=None):
2571 self
.sessionGuid
= ""
2572 self
.sessionID
= None
2574 self
.data
= "\r\n" + chr(0)
2578 def create(self
, method
=None, status
=None, to
=None, fro
=None, branch
=None, cseq
=0, sessionGuid
=None, data
=None):
2579 self
.method
= method
2580 self
.status
= status
2583 self
.branch
= branch
2585 self
.sessionGuid
= sessionGuid
2586 if data
: self
.data
= data
2588 def setData(self
, ctype
, data
):
2591 order
= ["EUF-GUID", "SessionID", "AppID", "Context", "Bridge", "Listening","Bridges", "NetID", "Conn-Type", "UPnPNat", "ICF", "Hashed-Nonce"]
2593 if key
== "Context" and data
.has_key(key
):
2594 s
.append("Context: %s\r\n" % b64enc(data
[key
]))
2595 elif data
.has_key(key
):
2596 s
.append("%s: %s\r\n" % (key
, str(data
[key
])))
2597 s
.append("\r\n"+chr(0))
2599 self
.data
= "".join(s
)
2603 if s
.find("MSNSLP/1.0") < 0: return
2605 lines
= s
.split("\r\n")
2607 # Get the MSNSLP method or status
2608 msnslp
= lines
[0].split(" ")
2609 if MSNP2PDEBUG
: log
.msg("Parsing MSNSLPMessage %s %s" % (len(s
), s
))
2610 if msnslp
[0] in ("INVITE", "BYE"):
2611 self
.method
= msnslp
[0].strip()
2613 self
.status
= msnslp
[1].strip()
2615 lines
.remove(lines
[0])
2618 line
= line
.split(":")
2619 if len(line
) < 1: continue
2621 if len(line
) > 2 and line
[0] == "To":
2622 self
.to
= line
[2][:line
[2].find('>')]
2623 elif len(line
) > 2 and line
[0] == "From":
2624 self
.fro
= line
[2][:line
[2].find('>')]
2625 elif line
[0] == "Call-ID":
2626 self
.sessionGuid
= line
[1].strip()
2627 elif line
[0] == "CSeq":
2628 self
.cseq
= int(line
[1].strip())
2629 elif line
[0] == "SessionID":
2630 self
.sessionID
= int(line
[1].strip())
2631 elif line
[0] == "EUF-GUID":
2632 self
.euf_guid
= line
[1].strip()
2633 elif line
[0] == "Content-Type":
2634 self
.ctype
= line
[1].strip()
2635 elif line
[0] == "Context":
2636 self
.context
= b64dec(line
[1])
2637 elif line
[0] == "Via":
2638 self
.branch
= line
[1].split(";")[1].split("=")[1].strip()
2641 log
.msg("Error parsing MSNSLP message.")
2647 s
.append("%s MSNMSGR:%s MSNSLP/1.0\r\n" % (self
.method
, self
.to
))
2649 if self
.status
== "200": status
= "200 OK"
2650 elif self
.status
== "603": status
= "603 Decline"
2651 s
.append("MSNSLP/1.0 %s\r\n" % status
)
2652 s
.append("To: <msnmsgr:%s>\r\n" % self
.to
)
2653 s
.append("From: <msnmsgr:%s>\r\n" % self
.fro
)
2654 s
.append("Via: MSNSLP/1.0/TLP ;branch=%s\r\n" % self
.branch
)
2655 s
.append("CSeq: %s \r\n" % str(self
.cseq
))
2656 s
.append("Call-ID: %s\r\n" % self
.sessionGuid
)
2657 s
.append("Max-Forwards: 0\r\n")
2658 s
.append("Content-Type: %s\r\n" % self
.ctype
)
2659 s
.append("Content-Length: %s\r\n\r\n" % len(self
.data
))
2664 """ Utility for handling the weird sequence IDs in p2p messages """
2665 def __init__(self
, baseID
=None):
2667 self
.baseID
= baseID
2669 self
.baseID
= random
.randint(1000, sys
.maxint
)
2673 return p2pseq
[self
.pos
] + self
.baseID
2680 class StringBuffer(StringIO
.StringIO
):
2681 def __init__(self
, notifyFunc
=None):
2682 self
.notifyFunc
= notifyFunc
2683 StringIO
.StringIO
.__init
__(self
)
2687 self
.notifyFunc(self
.getvalue())
2688 self
.notifyFunc
= None
2689 StringIO
.StringIO
.close(self
)
2693 def __init__(self
, remoteUser
, switchboard
, sessionID
, sessionGuid
):
2696 sessionID
= random
.randint(1000, sys
.maxint
)
2698 sessionGuid
= random_guid()
2699 self
.remoteUser
= remoteUser
2700 self
.switchboard
= switchboard
2701 self
.sessionID
= sessionID
2702 self
.sessionGuid
= sessionGuid
2703 self
.seqID
= SeqID()
2707 if not self
.switchboard
: return
2708 del self
.switchboard
.slpLinks
[self
.sessionID
]
2709 self
.switchboard
= None
2710 # This is so that handleP2PMessage can still use the SLPLink
2711 # one last time, for ACKing BYEs and 601s.
2712 reactor
.callLater(0, kill
)
2714 def warn(self
, text
):
2716 log
.msg("Warning in transfer: %s %s" % (self
, text
))
2718 def sendP2PACK(self
, ackHeaders
):
2719 binaryFields
= BinaryFields()
2720 binaryFields
[0] = ackHeaders
[0]
2721 binaryFields
[1] = self
.seqID
.next()
2722 binaryFields
[3] = ackHeaders
[3]
2723 binaryFields
[5] = BinaryFields
.ACK
2724 binaryFields
[6] = ackHeaders
[1]
2725 binaryFields
[7] = ackHeaders
[6]
2726 binaryFields
[8] = ackHeaders
[3]
2727 self
.sendP2PMessage(binaryFields
, "")
2729 def sendSLPMessage(self
, cmd
, ctype
, data
, branch
=None):
2730 msg
= MSNSLPMessage()
2732 msg
.create(status
=cmd
, to
=self
.remoteUser
, fro
=self
.switchboard
.userHandle
, branch
=branch
, cseq
=1, sessionGuid
=self
.sessionGuid
)
2734 msg
.create(method
=cmd
, to
=self
.remoteUser
, fro
=self
.switchboard
.userHandle
, branch
=random_guid(), cseq
=0, sessionGuid
=self
.sessionGuid
)
2735 msg
.setData(ctype
, data
)
2737 binaryFields
= BinaryFields()
2738 binaryFields
[1] = self
.seqID
.next()
2739 binaryFields
[3] = len(msgStr
)
2740 binaryFields
[4] = binaryFields
[3]
2741 binaryFields
[6] = random
.randint(1000, sys
.maxint
)
2742 self
.sendP2PMessage(binaryFields
, msgStr
)
2744 def sendP2PMessage(self
, binaryFields
, msgStr
):
2745 packet
= binaryFields
.packHeaders() + msgStr
+ binaryFields
.packFooter()
2747 message
= MSNMessage(message
=packet
)
2748 message
.setHeader("Content-Type", "application/x-msnmsgrp2p")
2749 message
.setHeader("P2P-Dest", self
.remoteUser
)
2750 message
.ack
= MSNMessage
.MESSAGE_ACK_FAT
2751 self
.switchboard
.sendMessage(message
)
2753 def handleSLPMessage(self
, slpMessage
):
2754 raise NotImplementedError
2760 class SLPLink_Send(SLPLink
):
2761 def __init__(self
, remoteUser
, switchboard
, filesize
, sessionID
=None, sessionGuid
=None):
2762 SLPLink
.__init
__(self
, remoteUser
, switchboard
, sessionID
, sessionGuid
)
2763 self
.handlePacket
= None
2765 self
.filesize
= filesize
2768 def send_dataprep(self
):
2769 if MSNP2PDEBUG
: log
.msg("send_dataprep")
2770 binaryFields
= BinaryFields()
2771 binaryFields
[0] = self
.sessionID
2772 binaryFields
[1] = self
.seqID
.next()
2775 binaryFields
[6] = random
.randint(1000, sys
.maxint
)
2777 self
.sendP2PMessage(binaryFields
, chr(0) * 4)
2779 def write(self
, data
):
2780 if MSNP2PDEBUG
: log
.msg("write")
2784 if i
+ 1202 < length
:
2785 self
._writeChunk
(data
[i
:i
+1202])
2788 self
.data
+= data
[i
:]
2789 if len(self
.data
) >= 1202:
2795 def _writeChunk(self
, chunk
):
2796 log
.msg("writing chunk")
2797 binaryFields
= BinaryFields()
2798 binaryFields
[0] = self
.sessionID
2799 if self
.offset
== 0:
2800 binaryFields
[1] = self
.seqID
.next()
2802 binaryFields
[1] = self
.seqID
.get()
2803 binaryFields
[2] = self
.offset
2804 binaryFields
[3] = self
.filesize
2805 binaryFields
[4] = len(chunk
)
2806 binaryFields
[5] = self
.dataFlag
2807 binaryFields
[6] = random
.randint(1000, sys
.maxint
)
2809 self
.offset
+= len(chunk
)
2810 self
.sendP2PMessage(binaryFields
, chunk
)
2814 self
._writeChunk
(self
.data
)
2819 # FIXME, should send 601 or something
2821 class SLPLink_FileSend(SLPLink_Send
):
2822 def __init__(self
, remoteUser
, switchboard
, filename
, filesize
):
2823 SLPLink_Send
.__init
__(self
, remoteUser
=remoteUser
, switchboard
=switchboard
, filesize
=filesize
)
2824 self
.dataFlag
= BinaryFields
.DATAFT
2825 # Send invite & wait for 200OK before sending dataprep
2826 context
= FileContext()
2827 context
.filename
= filename
2828 context
.filesize
= filesize
2829 data
= {"EUF-GUID" : MSN_MSNFTP_GUID
,\
2830 "SessionID": self
.sessionID
,\
2832 "Context" : context
.pack() }
2833 self
.sendSLPMessage("INVITE", "application/x-msnmsgr-sessionreqbody", data
)
2834 self
.acceptDeferred
= Deferred()
2836 def handleSLPMessage(self
, slpMessage
):
2837 if slpMessage
.status
== "200":
2838 if slpMessage
.ctype
== "application/x-msnmsgr-sessionreqbody":
2839 data
= {"Bridges" : "TRUDPv1 TCPv1",\
2841 "Conn-Type" : "Firewall",\
2842 "UPnPNat" : "false",\
2844 #"Hashed-Nonce": random_guid()}
2845 self
.sendSLPMessage("INVITE", "application/x-msnmsgr-transreqbody", data
)
2846 elif slpMessage
.ctype
== "application/x-msnmsgr-transrespbody":
2847 self
.acceptDeferred
.callback((True,))
2848 self
.handlePacket
= self
.wait_data_ack
2850 if slpMessage
.status
== "603":
2851 self
.acceptDeferred
.callback((False,))
2852 # SLPLink is over due to decline, error or BYE
2855 def wait_data_ack(self
, packet
):
2856 if MSNP2PDEBUG
: log
.msg("wait_data_ack")
2857 binaryFields
= BinaryFields()
2858 binaryFields
.unpackFields(packet
)
2860 if binaryFields
[5] != BinaryFields
.ACK
:
2861 self
.warn("field5," + str(binaryFields
[5]))
2864 self
.sendSLPMessage("BYE", "application/x-msnmsgr-sessionclosebody", {})
2865 self
.handlePacket
= None
2868 self
.handlePacket
= self
.wait_data_ack
2869 SLPLink_Send
.close(self
)
2872 class SLPLink_AvatarSend(SLPLink_Send
):
2873 def __init__(self
, remoteUser
, switchboard
, filesize
, sessionID
=None, sessionGuid
=None):
2874 SLPLink_Send
.__init
__(self
, remoteUser
=remoteUser
, switchboard
=switchboard
, filesize
=filesize
, sessionID
=sessionID
, sessionGuid
=sessionGuid
)
2875 self
.dataFlag
= BinaryFields
.DATA
2876 self
.sendSLPMessage("200", "application/x-msnmsgr-sessionreqbody", {"SessionID":self
.sessionID
})
2877 self
.send_dataprep()
2878 self
.handlePacket
= lambda packet
: None
2880 def handleSLPMessage(self
, slpMessage
):
2881 self
.killLink() # BYE or error
2884 SLPLink_Send
.close(self
)
2885 # Keep the link open to wait for a BYE
2887 class SLPLink_Receive(SLPLink
):
2888 def __init__(self
, remoteUser
, switchboard
, consumer
, context
=None, sessionID
=None, sessionGuid
=None):
2889 SLPLink
.__init
__(self
, remoteUser
, switchboard
, sessionID
, sessionGuid
)
2890 self
.handlePacket
= None
2891 self
.consumer
= consumer
2894 def wait_dataprep(self
, packet
):
2895 if MSNP2PDEBUG
: log
.msg("wait_dataprep")
2896 binaryFields
= BinaryFields()
2897 binaryFields
.unpackFields(packet
)
2899 if binaryFields
[3] != 4:
2900 self
.warn("field3," + str(binaryFields
[3]))
2902 if binaryFields
[4] != 4:
2903 self
.warn("field4," + str(binaryFields
[4]))
2905 if binaryFields
[9] != 1:
2906 self
.warn("field9," + str(binaryFields
[9]))
2909 self
.sendP2PACK(binaryFields
)
2910 self
.handlePacket
= self
.wait_data
2912 def wait_data(self
, packet
):
2913 if MSNP2PDEBUG
: log
.msg("wait_data")
2914 binaryFields
= BinaryFields()
2915 binaryFields
.unpackFields(packet
)
2917 if binaryFields
[5] != self
.dataFlag
:
2918 self
.warn("field5," + str(binaryFields
[5]))
2920 if binaryFields
[9] != 1:
2921 self
.warn("field9," + str(binaryFields
[9]))
2923 offset
= binaryFields
[2]
2924 total
= binaryFields
[3]
2925 length
= binaryFields
[4]
2927 data
= packet
[48:-4]
2928 if offset
!= self
.pos
:
2929 self
.warn("Received packet out of order")
2930 self
.consumer
.error()
2932 if len(data
) != length
:
2933 self
.warn("Received bad length of slp")
2934 self
.consumer
.error()
2939 self
.consumer
.write(str(data
))
2941 if self
.pos
== total
:
2942 self
.sendP2PACK(binaryFields
)
2943 self
.consumer
.close()
2944 self
.handlePacket
= None
2947 def doFinished(self
):
2948 raise NotImplementedError
2951 class SLPLink_FileReceive(SLPLink_Receive
, FileReceive
):
2952 def __init__(self
, remoteUser
, switchboard
, filename
, filesize
, sessionID
, sessionGuid
, branch
):
2953 SLPLink_Receive
.__init
__(self
, remoteUser
=remoteUser
, switchboard
=switchboard
, consumer
=self
, sessionID
=sessionID
, sessionGuid
=sessionGuid
)
2954 self
.dataFlag
= BinaryFields
.DATAFT
2955 self
.initialBranch
= branch
2956 FileReceive
.__init
__(self
, filename
, filesize
, remoteUser
)
2959 # Send a 603 decline
2960 self
.sendSLPMessage("603", "application/x-msnmsgr-sessionreqbody", {"SessionID":self
.sessionID
}, branch
=self
.initialBranch
)
2963 def accept(self
, consumer
):
2964 FileReceive
.accept(self
, consumer
)
2965 self
.sendSLPMessage("200", "application/x-msnmsgr-sessionreqbody", {"SessionID":self
.sessionID
}, branch
=self
.initialBranch
)
2967 def handleSLPMessage(self
, slpMessage
):
2968 if slpMessage
.method
== "INVITE": # The second invite
2969 data
= {"Bridge" : "TCPv1",\
2970 "Listening" : "false",\
2971 "Hashed-Nonce": "{00000000-0000-0000-0000-000000000000}"}
2972 self
.sendSLPMessage("200", "application/x-msnmsgr-transrespbody", data
, branch
=slpMessage
.branch
)
2973 self
.handlePacket
= self
.wait_data
2975 self
.killLink() # It's either a BYE or an error
2976 # FIXME, do some error handling if it was an error
2978 def doFinished(self
):
2979 #self.sendSLPMessage("BYE", "application/x-msnmsgr-sessionclosebody", {})
2981 # Wait for BYE? #FIXME
2984 class SLPLink_AvatarReceive(SLPLink_Receive
):
2985 def __init__(self
, remoteUser
, switchboard
, consumer
, context
):
2986 SLPLink_Receive
.__init
__(self
, remoteUser
=remoteUser
, switchboard
=switchboard
, consumer
=consumer
, context
=context
)
2987 self
.dataFlag
= BinaryFields
.DATA
2988 data
= {"EUF-GUID" : MSN_AVATAR_GUID
,\
2989 "SessionID": self
.sessionID
,\
2991 "Context" : context
}
2992 self
.sendSLPMessage("INVITE", "application/x-msnmsgr-sessionreqbody", data
)
2994 def handleSLPMessage(self
, slpMessage
):
2995 if slpMessage
.status
== "200":
2996 self
.handlePacket
= self
.wait_dataprep
2998 # SLPLink is over due to error or BYE
3001 def doFinished(self
):
3002 self
.sendSLPMessage("BYE", "application/x-msnmsgr-sessionclosebody", {})
3004 # mapping of error codes to error messages
3007 200 : "Syntax error",
3008 201 : "Invalid parameter",
3009 205 : "Invalid user",
3010 206 : "Domain name missing",
3011 207 : "Already logged in",
3012 208 : "Invalid username",
3013 209 : "Invalid screen name",
3014 210 : "User list full",
3015 215 : "User already there",
3016 216 : "User already on list",
3017 217 : "User not online",
3018 218 : "Already in mode",
3019 219 : "User is in the opposite list",
3020 223 : "Too many groups",
3021 224 : "Invalid group",
3022 225 : "User not in group",
3023 229 : "Group name too long",
3024 230 : "Cannot remove group 0",
3025 231 : "Invalid group",
3026 280 : "Switchboard failed",
3027 281 : "Transfer to switchboard failed",
3029 300 : "Required field missing",
3030 301 : "Too many FND responses",
3031 302 : "Not logged in",
3033 402 : "Error accessing contact list",
3034 403 : "Error accessing contact list",
3036 500 : "Internal server error",
3037 501 : "Database server error",
3038 502 : "Command disabled",
3039 510 : "File operation failed",
3040 520 : "Memory allocation failed",
3041 540 : "Wrong CHL value sent to server",
3043 600 : "Server is busy",
3044 601 : "Server is unavaliable",
3045 602 : "Peer nameserver is down",
3046 603 : "Database connection failed",
3047 604 : "Server is going down",
3048 605 : "Server unavailable",
3050 707 : "Could not create connection",
3051 710 : "Invalid CVR parameters",
3052 711 : "Write is blocking",
3053 712 : "Session is overloaded",
3054 713 : "Too many active users",
3055 714 : "Too many sessions",
3056 715 : "Not expected",
3057 717 : "Bad friend file",
3058 731 : "Not expected",
3060 800 : "Requests too rapid",
3062 910 : "Server too busy",
3063 911 : "Authentication failed",
3064 912 : "Server too busy",
3065 913 : "Not allowed when offline",
3066 914 : "Server too busy",
3067 915 : "Server too busy",
3068 916 : "Server too busy",
3069 917 : "Server too busy",
3070 918 : "Server too busy",
3071 919 : "Server too busy",
3072 920 : "Not accepting new users",
3073 921 : "Server too busy",
3074 922 : "Server too busy",
3075 923 : "No parent consent",
3076 924 : "Passport account not yet verified"
3080 # mapping of status codes to readable status format
3083 STATUS_ONLINE
: "Online",
3084 STATUS_OFFLINE
: "Offline",
3085 STATUS_HIDDEN
: "Appear Offline",
3086 STATUS_IDLE
: "Idle",
3087 STATUS_AWAY
: "Away",
3088 STATUS_BUSY
: "Busy",
3089 STATUS_BRB
: "Be Right Back",
3090 STATUS_PHONE
: "On the Phone",
3091 STATUS_LUNCH
: "Out to Lunch"
3095 # mapping of list ids to list codes
3098 FORWARD_LIST
: 'fl',
3101 REVERSE_LIST
: 'rl',
3106 # mapping of list codes to list ids
3108 for id,code
in listIDToCode
.items():
3109 listCodeToID
[code
] = id