]>
code.delx.au - pymsnt/blob - 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
5 if utils
.checkTwisted():
6 from twisted
.xish
.domish
import Element
8 from tlib
.domish
import Element
10 from legacy
import glue
11 from debug
import LogEvent
, INFO
, WARN
, ERROR
16 # Default avatar for MSN contacts
17 f
= open("src/legacy/defaultAvatar.png")
18 defaultAvatarData
= f
.read()
20 defaultAvatar
= avatar
.AvatarCache().setAvatar(defaultAvatarData
)
23 def getGroupNames(msnContact
, msnContactList
):
25 for groupGUID
in msnContact
.groups
:
27 groups
.append(msnContactList
.groups
[groupGUID
])
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
:
36 elif lists
& msn
.REVERSE_LIST
:
38 elif lists
& msn
.FORWARD_LIST
:
44 def jabsub2msnlist(sub
):
45 """ Converts a Jabber subscription state into the corresponding MSN contact lists ORed together """
47 return msn
.FORWARD_LIST
49 return msn
.REVERSE_LIST
51 return (msn
.FORWARD_LIST | msn
.REVERSE_LIST
)
58 def __init__(self
, session
):
59 self
.session
= session
60 self
.subscriptionBuffer
= []
63 self
.subscriptionBuffer
= None
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()
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
)
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
)
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
)
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
)
95 def syncJabberLegacyLists(self
):
96 """ Synchronises the MSN contact list on server with the Jabber contact list """
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()
106 for item
in result
.elements():
107 user
= item
.getAttribute("jid")
108 sub
= item
.getAttribute("subscription")
109 lists
= item
.getAttribute("lists")
111 lists
= jabsub2msnlist(sub
) # Backwards compatible
113 contact
= msn
.MSNContact(userHandle
=user
, screenName
="", lists
=lists
)
114 oldContactList
.addContact(contact
)
116 newXDB
= Element((None, "query"))
117 newXDB
.attributes
["xmlns"] = disco
.IQROSTER
119 contactList
= self
.session
.legacycon
.getContacts()
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
))
128 for contact
in contactList
.contacts
.values():
129 # Compare with the XDB <item/> entry
130 oldContact
= oldContactList
.getContact(contact
.userHandle
)
131 if oldContact
== None:
134 oldLists
= oldContact
.lists
135 lists
= contact
.lists
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)
143 if addedToList(msn
.FORWARD_LIST
):
144 jabContact
.syncGroups(getGroupNames(contact
, contactList
), push
=False)
145 jabContact
.syncContactGrantedAuth()
147 if removedFromList(msn
.FORWARD_LIST
):
148 jabContact
.syncContactRemovedAuth()
150 if addedToList(msn
.ALLOW_LIST
):
151 jabContact
.syncUserGrantedAuth()
153 if addedToList(msn
.BLOCK_LIST
) or removedFromList(msn
.ALLOW_LIST
):
154 jabContact
.syncUserRemovedAuth()
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()
159 if removedFromList(msn
.REVERSE_LIST
):
160 jabContact
.contactDerequestsAuth()
162 item
= newXDB
.addElement("item")
163 item
.attributes
["jid"] = contact
.userHandle
164 item
.attributes
["subscription"] = msnlist2jabsub(lists
)
165 item
.attributes
["lists"] = str(lists
)
168 self
.session
.pytrans
.xdb
.set(self
.session
.jabberID
, disco
.IQROSTER
, newXDB
)
169 LogEvent(INFO
, self
.session
.jabberID
, "End.")
171 def saveLegacyList(self
):
172 contactList
= self
.session
.legacycon
.getContacts()
173 if not contactList
: return
175 newXDB
= Element((None, "query"))
176 newXDB
.attributes
["xmlns"] = disco
.IQROSTER
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
)
184 self
.session
.pytrans
.xdb
.set(self
.session
.jabberID
, disco
.IQROSTER
, newXDB
)
185 LogEvent(INFO
, self
.session
.jabberID
, "Finished saving list.")