-# Copyright 2005 James Bunton <james@delx.cjb.net>
+# Copyright 2005-2006 James Bunton <james@delx.cjb.net>
# Licensed for distribution under the GPL version 2, check COPYING for details
-import utils
-from twisted.internet import reactor
-from tlib.xmlw import Element
from debug import LogEvent, INFO, WARN, ERROR
+
+from twisted.internet import reactor
+from twisted.words.xish.domish import Element
+
import disco
import legacy
-import jabw
-import config
-import lang
-import sha
class Contact:
self.jid = jid
self.contactList = contactList
self.groups = []
+ self.origsub = sub
self.sub = sub
self.nickname = ""
self.avatar = None
def syncContactGrantedAuth(self):
""" Since last using the transport the user has been granted authorisation by this contact.
Call this to synchronise the user's Jabber list with their legacy list after logon. """
- if(self.sub == "none"):
+ if self.sub == "none":
self.sub = "to"
- elif(self.sub == "from"):
+ elif self.sub == "from":
self.sub = "both"
else:
return
- self.updateRoster("subscribe")
def syncContactRemovedAuth(self):
""" Since last using the transport the user has been blocked by this contact.
Call this to synchronise the user's Jabber list with their legacy list after logon. """
- if(self.sub == "to"):
+ if self.sub == "to":
self.sub = "none"
- elif(self.sub == "both"):
+ elif self.sub == "both":
self.sub = "from"
else:
return
- self.updateRoster("unsubscribed")
def syncUserGrantedAuth(self):
""" Since last using the transport the user has granted authorisation to this contact.
Call this to synchronise the user's Jabber list with their legacy list after logon. """
- if(self.sub == "none"):
+ if self.sub == "none":
self.sub = "from"
- elif(self.sub == "to"):
+ elif self.sub == "to":
self.sub = "both"
else:
return
- self.updateRoster("subscribe")
def syncUserRemovedAuth(self):
""" Since last using the transport the user has removed this contact's authorisation.
Call this to synchronise the user's Jabber list with their legacy list after logon. """
- if(self.sub == "from"):
+ if self.sub == "from":
self.sub = "none"
- elif(self.sub == "both"):
+ elif self.sub == "both":
self.sub = "to"
else:
return
- self.updateRoster("unsubscribe")
def syncGroups(self, groups, push=True):
""" Set the groups that this contact is in on the legacy service.
By default this pushes the groups out with a presence subscribed packet. """
self.groups = groups
- if push: self.updateRoster("subscribed");
-
+ if push: self.syncRoster(ptype="subscribed");
+
+ syncChoice = {
+ ("none", "none") : "", #
+ ("none", "to" ) : "subscribe", # User+ Contact
+ ("none", "from") : "subscribe", # User Contact+
+ ("none", "both") : "subscribe", # User+ Contact+
+ ("to" , "none") : "unsubscribed", # User- Contact
+ ("to" , "to" ) : "", #
+ ("to" , "from") : "unsubscribe", # User- Contact+ **
+ ("to" , "both") : "subscribe", # User Contact+
+ ("from", "none") : "unsubscribe", # User Contact-
+ ("from", "to" ) : "subscribe", # User+ Contact- *
+ ("from", "from") : "", #
+ ("from", "both") : "subscribe", # User+ Contact
+ ("both", "none") : "unsubscribed", # User- Contact- *
+ ("both", "to" ) : "unsubscribe", # User Contact-
+ ("both", "from") : "unsubscribed", # User- Contact
+ ("both", "both") : "" #
+ }
+
+ def syncRoster(self, ptype=""):
+ if not ptype:
+ ptype = self.syncChoice.get((self.origsub, self.sub))
+ if ptype:
+ self.contactList.session.sendRosterImport(jid=self.jid, ptype=ptype, sub=self.sub, groups=self.groups, name=self.nickname)
+
def contactGrantsAuth(self):
""" Live roster event """
- if(self.sub == "none"):
+ if self.sub == "none":
self.sub = "to"
- elif(self.sub == "from"):
+ elif self.sub == "from":
self.sub = "both"
self.sendSub("subscribed")
self.sendPresence()
def contactRemovesAuth(self):
""" Live roster event """
- if(self.sub == "to"):
+ if self.sub == "to":
self.sub = "none"
- elif(self.sub == "both"):
+ elif self.sub == "both":
self.sub = "from"
self.sendSub("unsubscribed")
if subtype == "subscribe":
if self.sub == "to" or self.sub == "both":
self.sendSub("subscribed")
- self.contactList.legacyList.addContact(self.jid)
+ else:
+ self.contactList.legacyList.addContact(self.jid)
elif subtype == "subscribed":
if self.sub == "none":
self.sub = "none"
self.contactList.legacyList.removeContact(self.jid)
- elif(subtype == "unsubscribed"):
- if(self.sub == "both"):
+ elif subtype == "unsubscribed":
+ if self.sub == "both":
self.sub = "to"
- if(self.sub == "from"):
+ if self.sub == "from":
self.sub = "none"
self.contactList.legacyList.deauthContact(self.jid)
def updateNickname(self, nickname, push=True):
- if(self.nickname != nickname):
+ if self.nickname != nickname:
self.nickname = nickname
- if(push): self.sendPresence()
+ if push: self.sendPresence()
def updatePresence(self, show, status, ptype, force=False):
updateFlag = (self.show != show or self.status != status or self.ptype != ptype or force)
self.show = show
self.status = status
self.ptype = ptype
- if(updateFlag):
+ if updateFlag:
self.sendPresence()
def updateAvatar(self, avatar=None, push=True):
- if(self.avatar == avatar): return
+ if self.avatar == avatar:
+ return
self.avatar = avatar
- if(push): self.sendPresence()
+ if push:
+ self.sendPresence()
def sendSub(self, ptype):
- self.contactList.session.sendPresence(to=self.contactList.session.jabberID, fro=self.jid, ptype=ptype)
+ self.contactList.session.sendPresence(to=self.contactList.session.jabberID, fro=self.jid + "/" + legacy.id, ptype=ptype)
def sendPresence(self, tojid=""):
avatarHash = ""
- if(self.avatar):
+ if self.avatar:
avatarHash = self.avatar.getImageHash()
caps = Element((None, "c"))
caps.attributes["xmlns"] = disco.CAPS
caps.attributes["ver"] = legacy.version
if not tojid:
tojid=self.contactList.session.jabberID
- self.contactList.session.sendPresence(to=tojid, fro=self.jid, ptype=self.ptype, show=self.show, status=self.status, avatarHash=avatarHash, nickname=self.nickname, payload=[caps])
+ self.contactList.session.sendPresence(to=tojid, fro=self.jid + "/" + legacy.id, ptype=self.ptype, show=self.show, status=self.status, avatarHash=avatarHash, nickname=self.nickname, payload=[caps])
- def updateRoster(self, ptype):
- self.contactList.session.sendRosterImport(jid=self.jid, ptype=ptype, sub=self.sub, groups=self.groups, name=self.nickname)
class ContactList:
def resendLists(self, tojid=""):
for jid in self.contacts:
- if(self.contacts[jid].status != "unavailable"):
+ if self.contacts[jid].ptype != "unavailable" :
self.contacts[jid].sendPresence(tojid)
LogEvent(INFO, self.session.jabberID)
def getContact(self, jid):
""" Finds the contact. If one doesn't exist then a new one is created, with sub set to "none" """
- if(not self.contacts.has_key(jid)):
+ if not self.contacts.has_key(jid):
self.contacts[jid] = Contact(jid, "none", self)
return self.contacts[jid]
def findContact(self, jid):
- if(self.contacts.has_key(jid)):
+ if self.contacts.has_key(jid):
return self.contacts[jid]
return None