]>
code.delx.au - pymsnt/blob - src/contact.py
1 # Copyright 2005 James Bunton <james@delx.cjb.net>
2 # Licensed for distribution under the GPL version 2, check COPYING for details
5 from twisted
.internet
import reactor
6 if(utils
.checkTwisted()):
7 from twisted
.xish
.domish
import Element
9 from tlib
.domish
import Element
10 from debug
import LogEvent
, INFO
, WARN
, ERROR
20 """ Represents a Jabber contact """
21 def __init__(self
, jid
, sub
, contactList
):
23 self
.contactList
= contactList
30 self
.ptype
= "unavailable"
33 """ Destroys this object. Does not remove the contact from the server's list. """
34 self
.contactList
= None
37 def syncContactGrantedAuth(self
):
38 """ Since last using the transport the user has been granted authorisation by this contact.
39 Call this to synchronise the user's Jabber list with their legacy list after logon. """
40 if(self
.sub
== "none"):
42 elif(self
.sub
== "from"):
46 self
.updateRoster("subscribe")
48 def syncContactRemovedAuth(self
):
49 """ Since last using the transport the user has been blocked by this contact.
50 Call this to synchronise the user's Jabber list with their legacy list after logon. """
53 elif(self
.sub
== "both"):
57 self
.updateRoster("unsubscribed")
59 def syncUserGrantedAuth(self
):
60 """ Since last using the transport the user has granted authorisation to this contact.
61 Call this to synchronise the user's Jabber list with their legacy list after logon. """
62 if(self
.sub
== "none"):
64 elif(self
.sub
== "to"):
68 self
.updateRoster("subscribe")
70 def syncUserRemovedAuth(self
):
71 """ Since last using the transport the user has removed this contact's authorisation.
72 Call this to synchronise the user's Jabber list with their legacy list after logon. """
73 if(self
.sub
== "from"):
75 elif(self
.sub
== "both"):
79 self
.updateRoster("unsubscribe")
81 def syncGroups(self
, groups
, push
=True):
82 """ Set the groups that this contact is in on the legacy service.
83 By default this pushes the groups out with a presence subscribed packet. """
85 if push
: self
.updateRoster("subscribed");
87 def contactGrantsAuth(self
):
88 """ Live roster event """
89 if(self
.sub
== "none"):
91 elif(self
.sub
== "from"):
93 self
.sendSub("subscribed")
96 def contactRemovesAuth(self
):
97 """ Live roster event """
100 elif(self
.sub
== "both"):
102 self
.sendSub("unsubscribed")
104 def contactRequestsAuth(self
):
105 """ Live roster event """
106 self
.sendSub("subscribe")
108 def contactDerequestsAuth(self
):
109 """ Live roster event """
110 self
.sendSub("unsubscribe")
112 def jabberSubscriptionReceived(self
, subtype
):
113 """ Updates the subscription state internally and pushes the update to the legacy server """
114 if subtype
== "subscribe":
115 if self
.sub
== "to" or self
.sub
== "both":
116 self
.sendSub("subscribed")
117 self
.contactList
.legacyList
.addContact(self
.jid
)
119 elif subtype
== "subscribed":
120 if self
.sub
== "none":
124 self
.contactList
.legacyList
.authContact(self
.jid
)
126 elif subtype
== "unsubscribe":
127 if self
.sub
== "none" or self
.sub
== "from":
128 self
.sendSub("unsubscribed")
129 if self
.sub
== "both":
133 self
.contactList
.legacyList
.removeContact(self
.jid
)
135 elif(subtype
== "unsubscribed"):
136 if(self
.sub
== "both"):
138 if(self
.sub
== "from"):
140 self
.contactList
.legacyList
.deauthContact(self
.jid
)
142 def updateNickname(self
, nickname
, push
=True):
143 if(self
.nickname
!= nickname
):
144 self
.nickname
= nickname
145 if(push
): self
.sendPresence()
147 def updatePresence(self
, show
, status
, ptype
, force
=False):
148 updateFlag
= (self
.show
!= show
or self
.status
!= status
or self
.ptype
!= ptype
or force
)
155 def updateAvatar(self
, avatar
=None, push
=True):
156 if(self
.avatar
== avatar
): return
158 if(push
): self
.sendPresence()
160 def sendSub(self
, ptype
):
161 self
.contactList
.session
.sendPresence(to
=self
.contactList
.session
.jabberID
, fro
=self
.jid
, ptype
=ptype
)
163 def sendPresence(self
, tojid
=""):
166 avatarHash
= self
.avatar
.getImageHash()
167 caps
= Element((None, "c"))
168 caps
.attributes
["xmlns"] = disco
.CAPS
169 caps
.attributes
["node"] = legacy
.url
+ "/protocol/caps"
170 caps
.attributes
["ver"] = legacy
.version
172 tojid
=self
.contactList
.session
.jabberID
173 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
])
175 def updateRoster(self
, ptype
):
176 self
.contactList
.session
.sendRosterImport(jid
=self
.jid
, ptype
=ptype
, sub
=self
.sub
, groups
=self
.groups
, name
=self
.nickname
)
180 """ Represents the Jabber contact list """
181 def __init__(self
, session
):
182 LogEvent(INFO
, session
.jabberID
)
183 self
.session
= session
187 """ Cleanly removes the object """
188 LogEvent(INFO
, self
.session
.jabberID
)
189 for jid
in self
.contacts
:
190 self
.contacts
[jid
].updatePresence("", "", "unavailable")
191 self
.contacts
[jid
].removeMe()
194 self
.legacyList
= None
196 def resendLists(self
, tojid
=""):
197 for jid
in self
.contacts
:
198 if(self
.contacts
[jid
].status
!= "unavailable"):
199 self
.contacts
[jid
].sendPresence(tojid
)
200 LogEvent(INFO
, self
.session
.jabberID
)
202 def createContact(self
, jid
, sub
):
203 """ Creates a contact object. Use this to initialise the contact list
204 Returns a Contact object which you can call sync* methods on to synchronise
205 the user's legacy contact list with their Jabber list """
206 LogEvent(INFO
, self
.session
.jabberID
)
207 c
= Contact(jid
, sub
, self
)
208 self
.contacts
[jid
] = c
211 def getContact(self
, jid
):
212 """ Finds the contact. If one doesn't exist then a new one is created, with sub set to "none" """
213 if(not self
.contacts
.has_key(jid
)):
214 self
.contacts
[jid
] = Contact(jid
, "none", self
)
215 return self
.contacts
[jid
]
217 def findContact(self
, jid
):
218 if(self
.contacts
.has_key(jid
)):
219 return self
.contacts
[jid
]
222 def jabberSubscriptionReceived(self
, jid
, subtype
):
223 self
.getContact(jid
).jabberSubscriptionReceived(subtype
)