class LegacyConnection(msn.MSNConnection):
""" A glue class that connects to the legacy network """
def __init__(self, username, password, session):
+ self.jabberID = session.jabberID
+
self.session = session
self.listSynced = False
self.initialListVersion = 0
msnContact.msnobjGot = True
c.updateAvatar(av)
else:
- def updateAvatar((imageData,)):
+ def updateAvatarCB((imageData,)):
av = self.session.pytrans.avatarCache.setAvatar(imageData)
c.updateAvatar(av)
d = self.sendAvatarRequest(userHandle)
if d:
- d.addCallback(updateAvatar)
+ d.addCallback(updateAvatarCB)
else:
# They've turned off their avatar
global defaultAvatar
self.session.pytrans.send(el)
+ def gotAvatarImageData(self, userHandle, imageData):
+ LogEvent(INFO, self.session.jabberID)
+ av = self.session.pytrans.avatarCache.setAvatar(imageData)
+ jid = msn2jid(userHandle)
+ c = self.session.contactList.findContact(jid)
+ c.updateAvatar(av)
+
return s + (n-len(s))*c
if sys.byteorder == "little":
- def utf16net(s):
- """ Encodes to utf-16 and ensures network byte order. Strips the BOM """
- a = array.array("h", s.encode("utf-16")[2:])
- a.byteswap()
- return a.tostring()
+ def utf16net(s):
+ """ Encodes to utf-16 and ensures network byte order. Strips the BOM """
+ a = array.array("h", s.encode("utf-16")[2:])
+ a.byteswap()
+ return a.tostring()
else:
- def utf16net(s):
- """ Encodes to utf-16 and ensures network byte order. Strips the BOM """
- return s.encode("utf-16")[2:]
+ def utf16net(s):
+ """ Encodes to utf-16 and ensures network byte order. Strips the BOM """
+ return s.encode("utf-16")[2:]
def b64enc(s):
return base64.encodestring(s).replace("\n", "")
d.callback((data,))
buffer = StringBuffer(bufferClosed)
slpLink = SLPLink_AvatarReceive(remoteUser=msnContact.userHandle, switchboard=self, consumer=buffer, context=msnContact.msnobj.text)
- slpLink.avatarDataBuffer = buffer
self.slpLinks[slpLink.sessionID] = slpLink
return d
self.filesize = 0
def pack(self):
- if MSNP2PDEBUG: print "FileContext packing:", self.filename, self.filesize
+ if MSNP2PDEBUG: log.msg("FileContext packing:", self.filename, self.filesize)
data = struct.pack("<LLQL", 638, 0x03, self.filesize, 0x01)
data = data[:-1] # Uck, weird, but it works
data += utf16net(self.filename)
chunk = packet[19:540]
chunk = chunk[:chunk.find('\x00\x00')]
self.filename = unicode((codecs.BOM_UTF16_BE + chunk).decode("utf-16"))
- if MSNP2PDEBUG: print "FileContext parsed:", self.filesize, self.filename
+ if MSNP2PDEBUG: log.msg("FileContext parsed:", self.filesize, self.filename)
class BinaryFields:
self.fields = struct.unpack("<LLQQLLLLQ", packet[0:48])
self.fields += struct.unpack(">L", packet[len(packet)-4:])
if MSNP2PDEBUG:
- print "Unpacked fields:",
+ out = "Unpacked fields: "
for i in self.fields:
- print hex(i),
- print
+ out += hex(i) + ' '
+ log.msg(out)
def packHeaders(self):
f = tuple(self.fields)
if MSNP2PDEBUG:
- print "Packed fields:",
+ out = "Packed fields: "
for i in self.fields:
- print hex(i),
- print
+ out += hex(i) + ' '
+ log.msg(out)
return struct.pack("<LLQQLLLLQ", f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8])
def packFooter(self):
# Get the MSNSLP method or status
msnslp = lines[0].split(" ")
- if MSNP2PDEBUG: print "Parsing MSNSLPMessage", len(s), s
+ if MSNP2PDEBUG: log.msg("Parsing MSNSLPMessage %s %s" % (len(s), s))
if msnslp[0] in ("INVITE", "BYE"):
self.method = msnslp[0].strip()
else:
self.branch = line[1].split(";")[1].split("=")[1].strip()
except:
if MSNP2PDEBUG:
- print "Error parsing MSNSLP message."
+ log.msg("Error parsing MSNSLP message.")
raise
def __str__(self):
def warn(self, text):
if MSNP2PDEBUG:
- print "Warning in transfer: ", self, text
+ log.msg("Warning in transfer: %s %s" % (self, text))
def sendP2PACK(self, ackHeaders):
binaryFields = BinaryFields()
self.data = ""
def send_dataprep(self):
- if MSNP2PDEBUG: print "send_dataprep"
+ if MSNP2PDEBUG: log.msg("send_dataprep")
binaryFields = BinaryFields()
binaryFields[0] = self.sessionID
binaryFields[1] = self.seqID.next()
self.sendP2PMessage(binaryFields, chr(0) * 4)
def write(self, data):
- if MSNP2PDEBUG: print "write"
+ if MSNP2PDEBUG: log.msg("write")
i = 0
length = len(data)
while i < length:
return
def _writeChunk(self, chunk):
- print "writing chunk"
+ log.msg("writing chunk")
binaryFields = BinaryFields()
binaryFields[0] = self.sessionID
if self.offset == 0:
self.killLink()
def wait_data_ack(self, packet):
- if MSNP2PDEBUG: print "wait_data_ack"
+ if MSNP2PDEBUG: log.msg("wait_data_ack")
binaryFields = BinaryFields()
binaryFields.unpackFields(packet)
self.pos = 0
def wait_dataprep(self, packet):
- if MSNP2PDEBUG: print "wait_dataprep"
+ if MSNP2PDEBUG: log.msg("wait_dataprep")
binaryFields = BinaryFields()
binaryFields.unpackFields(packet)
self.handlePacket = self.wait_data
def wait_data(self, packet):
- if MSNP2PDEBUG: print "wait_data"
+ if MSNP2PDEBUG: log.msg("wait_data")
binaryFields = BinaryFields()
binaryFields.unpackFields(packet)
""" An MSN Alert (http://alerts.msn.com) was received. Body is the
text of the alert. 'action' is a url for more information,
'subscr' is a url to modify your your alerts subscriptions. """
+
+ def gotAvatarImageData(self, userHandle, imageData):
+ """ An contact's avatar has been received because a switchboard
+ session with them was started. """
class SavedEvents:
def __init__(self, msncon):
msn.SwitchboardClient.__init__(self)
self.msncon = msncon
+ self.msnobj = msncon.notificationClient.msnobj
self.userHandle = msncon.username
self.ident = (msncon.ident, "INVALID!!")
self.messageBuffer = []
def failedMessage(self, *ignored):
raise NotImplementedError
- def sendMessage(self, text, noerror=False):
+ def sendClientCaps(self):
+ message = msn.MSNMessage()
+ message.setHeader("Content-Type", "text/x-clientcaps")
+ message.setHeader("Client-Name", "PyMSNt")
+ if hasattr(self.msncon, "jabberID"):
+ message.setHeader("JabberID", str(self.msncon.jabberID))
+ self.sendMessage(message)
+
+ def sendMessage(self, message, noerror=False):
+ # Check to make sure that clientcaps only gets sent after
+ # the first text type message.
+ if isinstance(message, msn.MSNMessage) and message.getHeader("Content-Type").startswith("text"):
+ self.sendMessage = self.sendMessageReal
+ self.sendClientCaps()
+ return self.sendMessage(message, noerror)
+ else:
+ return self.sendMessageReal(message, noerror)
+
+ def sendMessageReal(self, text, noerror=False):
if not isinstance(text, basestring):
msn.SwitchboardClient.sendMessage(self, text)
return
def failedMessage(self, text):
self.groupchat.gotMessage("BOUNCE", text)
- def sendMessage(self, text, noerror):
+ def sendMessage(self, text, noerror=False):
""" Used to send a mesage to the groupchat. Can be called immediately
after instantiation. """
if self.contactCount > 0:
self._switchToMulti(remoteUser)
self.userJoined(remoteUser)
self.userJoined(userHandle)
+ else:
+ def updateAvatarCB((imageData, )):
+ if self.msncon:
+ self.msncon.gotAvatarImageData(self.remoteUser, imageData)
+ d = self.sendAvatarRequest()
+ if d:
+ d.addCallback(updateAvatarCB)
def userLeft(self, userHandle):
def wait():
def gotMessage(self, message):
LogEvent(INFO, self.ident)
- self.msncon.gotMessage(self.remoteUser, message.getMessage())
+ cTypes = [s.strip() for s in message.getHeader("Content-Type").split(';')]
+ if "text/plain" == cTypes[0]:
+ try:
+ if len(cTypes) > 1 and cTypes[1].lower().find("utf-8") >= 0:
+ text = message.getMessage().decode("utf-8")
+ else:
+ text = message.getMessage()
+ self.msncon.gotMessage(self.remoteUser, text)
+ except:
+ self.msncon.gotMessage("A message was lost.")
+ raise
+ elif "text/x-clientcaps" == cTypes[0]:
+ if message.hasHeader("JabberID"):
+ jid = message.getHeader("JabberID")
+ self.switchboardSession.msncon.userMapping(message.userHandle, jid)
+ else:
+ LogEvent(INFO, self.ident, "Discarding unknown message type.")
def gotFileReceive(self, fileReceive):
LogEvent(INFO, self.ident)
if not (msnContact and msnContact.caps & self.CAPS and msnContact.msnobj): return
if msnContact.msnobjGot: return
msnContact.msnobjGot = True # This is deliberately set before we get the avatar. So that we don't try to reget failed avatars over & over
- msn.SwitchboardClient.sendAvatarRequest(self, msnContact)
+ return msn.SwitchboardClient.sendAvatarRequest(self, msnContact)
def sendFile(self, msnContact, filename, filesize):
def doSendFile(ignored=None):
self.funcBuffer.append(doSendFile)
return d
-