]> code.delx.au - pymsnt/blob - src/legacy/legacylist.py
Reimport and tags (0.10.1)
[pymsnt] / src / legacy / legacylist.py
1 # Copyright 2004-2005 James Bunton <james@delx.cjb.net>
2 # Licensed for distribution under the GPL version 2, check COPYING for details
3
4 import utils
5 if utils.checkTwisted():
6 from twisted.xish.domish import Element
7 else:
8 from tlib.domish import Element
9 from tlib import msn
10 from legacy import glue
11 from debug import LogEvent, INFO, WARN, ERROR
12 import avatar
13 import disco
14
15
16 # Default avatar for MSN contacts
17 f = open("src/legacy/defaultAvatar.png")
18 defaultAvatarData = f.read()
19 f.close()
20 defaultAvatar = avatar.AvatarCache().setAvatar(defaultAvatarData)
21
22
23 def getGroupNames(msnContact, msnContactList):
24 groups = []
25 for groupGUID in msnContact.groups:
26 try:
27 groups.append(msnContactList.groups[groupGUID])
28 except KeyError:
29 pass
30 return groups
31
32 def msnlist2jabsub(lists):
33 """ Converts MSN contact lists ORed together into the corresponding Jabber subscription state """
34 if lists & msn.FORWARD_LIST and lists & msn.REVERSE_LIST:
35 return "both"
36 elif lists & msn.REVERSE_LIST:
37 return "from"
38 elif lists & msn.FORWARD_LIST:
39 return "to"
40 else:
41 return "none"
42
43
44 def jabsub2msnlist(sub):
45 """ Converts a Jabber subscription state into the corresponding MSN contact lists ORed together """
46 if sub == "to":
47 return msn.FORWARD_LIST
48 elif sub == "from":
49 return msn.REVERSE_LIST
50 elif sub == "both":
51 return (msn.FORWARD_LIST | msn.REVERSE_LIST)
52 else:
53 return 0
54
55
56
57 class LegacyList:
58 def __init__(self, session):
59 self.session = session
60 self.subscriptionBuffer = []
61
62 def removeMe(self):
63 self.subscriptionBuffer = None
64 self.session = None
65
66 def addContact(self, jid):
67 LogEvent(INFO, self.session.jabberID)
68 userHandle = glue.jid2msn(jid)
69 self.session.legacycon.addContact(msn.FORWARD_LIST, userHandle)
70 self.session.contactList.getContact(jid).contactGrantsAuth()
71
72 def removeContact(self, jid):
73 LogEvent(INFO, self.session.jabberID)
74 jid = glue.jid2msn(jid)
75 self.session.legacycon.remContact(msn.FORWARD_LIST, jid)
76
77
78 def authContact(self, jid):
79 LogEvent(INFO, self.session.jabberID)
80 jid = glue.jid2msn(jid)
81 d = self.session.legacycon.remContact(msn.PENDING_LIST, jid)
82 if d:
83 self.session.legacycon.addContact(msn.REVERSE_LIST, jid)
84 self.session.legacycon.remContact(msn.BLOCK_LIST, jid)
85 self.session.legacycon.addContact(msn.ALLOW_LIST, jid)
86
87 def deauthContact(self, jid):
88 LogEvent(INFO, self.session.jabberID)
89 jid = glue.jid2msn(jid)
90 self.session.legacycon.remContact(msn.ALLOW_LIST, jid)
91 self.session.legacycon.addContact(msn.BLOCK_LIST, jid)
92
93
94
95 def syncJabberLegacyLists(self):
96 """ Synchronises the MSN contact list on server with the Jabber contact list """
97
98 global defaultAvatar
99
100 # We have to make an MSNContactList from the XDB data, then compare it with the one the server sent
101 # Any subscription changes must be sent to the client, as well as changed in the XDB
102 LogEvent(INFO, self.session.jabberID, "Start.")
103 result = self.session.pytrans.xdb.request(self.session.jabberID, disco.IQROSTER)
104 oldContactList = msn.MSNContactList()
105 if result:
106 for item in result.elements():
107 user = item.getAttribute("jid")
108 sub = item.getAttribute("subscription")
109 lists = item.getAttribute("lists")
110 if not lists:
111 lists = jabsub2msnlist(sub) # Backwards compatible
112 lists = int(lists)
113 contact = msn.MSNContact(userHandle=user, screenName="", lists=lists)
114 oldContactList.addContact(contact)
115
116 newXDB = Element((None, "query"))
117 newXDB.attributes["xmlns"] = disco.IQROSTER
118
119 contactList = self.session.legacycon.getContacts()
120
121
122 # Convienence functions
123 def addedToList(num):
124 return (not (oldLists & num) and (lists & num))
125 def removedFromList(num):
126 return ((oldLists & num) and not (lists & num))
127
128 for contact in contactList.contacts.values():
129 # Compare with the XDB <item/> entry
130 oldContact = oldContactList.getContact(contact.userHandle)
131 if oldContact == None:
132 oldLists = 0
133 else:
134 oldLists = oldContact.lists
135 lists = contact.lists
136
137 # Create the Jabber representation of the
138 # contact base on the old list data and then
139 # sync it with current
140 jabContact = self.session.contactList.createContact(glue.msn2jid(contact.userHandle), msnlist2jabsub(oldLists))
141 jabContact.updateAvatar(defaultAvatar, push=False)
142
143 if addedToList(msn.FORWARD_LIST):
144 jabContact.syncGroups(getGroupNames(contact, contactList), push=False)
145 jabContact.syncContactGrantedAuth()
146
147 if removedFromList(msn.FORWARD_LIST):
148 jabContact.syncContactRemovedAuth()
149
150 if addedToList(msn.ALLOW_LIST):
151 jabContact.syncUserGrantedAuth()
152
153 if addedToList(msn.BLOCK_LIST) or removedFromList(msn.ALLOW_LIST):
154 jabContact.syncUserRemovedAuth()
155
156 if (not (lists & msn.ALLOW_LIST) and not (lists & msn.BLOCK_LIST) and (lists & msn.REVERSE_LIST)) or (lists & msn.PENDING_LIST):
157 jabContact.contactRequestsAuth()
158
159 if removedFromList(msn.REVERSE_LIST):
160 jabContact.contactDerequestsAuth()
161
162 item = newXDB.addElement("item")
163 item.attributes["jid"] = contact.userHandle
164 item.attributes["subscription"] = msnlist2jabsub(lists)
165 item.attributes["lists"] = str(lists)
166
167 # Update the XDB
168 self.session.pytrans.xdb.set(self.session.jabberID, disco.IQROSTER, newXDB)
169 LogEvent(INFO, self.session.jabberID, "End.")
170
171 def saveLegacyList(self):
172 contactList = self.session.legacycon.getContacts()
173 if not contactList: return
174
175 newXDB = Element((None, "query"))
176 newXDB.attributes["xmlns"] = disco.IQROSTER
177
178 for contact in contactList.contacts.values():
179 item = newXDB.addElement("item")
180 item.attributes["jid"] = contact.userHandle
181 item.attributes["subscription"] = msnlist2jabsub(contact.lists) # Backwards compat
182 item.attributes["lists"] = str(contact.lists)
183
184 self.session.pytrans.xdb.set(self.session.jabberID, disco.IQROSTER, newXDB)
185 LogEvent(INFO, self.session.jabberID, "Finished saving list.")
186
187