Package spade :: Package xmppd :: Package modules :: Module roster
[hide private]
[frames] | no frames]

Source Code for Module spade.xmppd.modules.roster

  1  # -*- coding: UTF-8 -*- 
  2  # Distributed under the terms of GPL version 2 or any later 
  3  # Copyright (C) Kristopher Tate/BlueBridge technologies, 2005 
  4  # Roster module for xmppd.py 
  5   
  6  from xmpp import * 
  7   
8 -class ROSTER(PlugIn):
9 NS = NS_ROSTER
10 - def plugin(self,server):
11 server.Dispatcher.RegisterHandler('iq',self.RosterIqHandler,typ='set',ns=NS_ROSTER,xmlns=NS_CLIENT) 12 server.Dispatcher.RegisterHandler('iq',self.RosterIqHandler,typ='get',ns=NS_ROSTER,xmlns=NS_CLIENT)
13
14 - def RosterAdd(self,session,stanza,ask_subscribe=False):
15 s_split_jid = session.getSplitJID() 16 the_roster = session.getRoster() 17 if stanza.getType() == 'set' and stanza.getTag('query').kids != []: 18 for kid in stanza.getTag('query').kids: 19 split_jid = self._owner.tool_split_jid(kid.getAttr('jid')) 20 if split_jid == None: raise NodeProcessed 21 if kid.getName() == 'item' and kid.getAttr('subscription') != 'remove': 22 info = {} 23 name = kid.getAttr('name') 24 if name != None: info.update({'name':name}) 25 26 subscription = kid.getAttr('subscription') 27 if subscription != None: info.update({'subscription':subscription}) 28 elif kid.getAttr('jid') not in the_roster.keys() or the_roster[kid.getAttr('jid')].has_key('subscription') == False: 29 self.DEBUG('###ROSTER+: Wow, subscription is not active -- better create one pronto!','warn') 30 #kid.setAttr('subscription','none') 31 info.update({'subscription':'none'}) 32 33 ask = kid.getAttr('ask') 34 if ask != None or ask_subscribe == True: 35 info.update({'ask':ask}) 36 elif ask == 'InternalDelete': 37 kid.delAttr('ask') 38 print "### ROSTER: INTERNAL DELETE" 39 self._owner.DB.del_from_roster_jid(s_split_jid[1],s_split_jid[0],split_jid[0]+'@'+split_jid[1],'ask') 40 41 #self.DEBUG(unicode(info).encode('utf-8'),'error') 42 self._owner.DB.save_to_roster(s_split_jid[1],s_split_jid[0],split_jid[0]+'@'+split_jid[1],info) 43 if kid.kids != []: 44 group_list = [] 45 for grandkid in kid.kids: 46 if grandkid.getName() == 'group': 47 group_list += [grandkid.getData()] 48 49 self._owner.DB.save_groupie(s_split_jid[1],s_split_jid[0],split_jid[0]+'@'+split_jid[1],group_list) 50 print "### RA: ENDED WITH INFO " + str(info)
51
52 - def RosterRemove(self,session,stanza):
53 s_split_jid = session.getSplitJID() 54 if stanza.getType() == 'set' and stanza.getTag('query').kids != []: 55 for kid in stanza.getTag('query').kids: 56 if kid.getName() == 'item' and kid.getAttr('subscription') == 'remove': 57 #split_jid = self._owner.tool_split_jid(kid.getAttr('jid')) 58 p = Presence(to=kid.getAttr('jid'),frm=session.getBareJID(),typ='unsubscribe') 59 session.dispatch(p) 60 split_jid = self._owner.tool_split_jid(kid.getAttr('jid')) 61 p = Presence(to=kid.getAttr('jid'),frm=session.getBareJID(),typ='unsubscribed') 62 session.dispatch(p) 63 64 session.enqueue(stanza) 65 66 self._owner.DB.del_from_roster(s_split_jid[1],s_split_jid[0],kid.getAttr('jid')) 67 self._owner.DB.del_groupie(s_split_jid[1],s_split_jid[0],kid.getAttr('jid')) 68 69 #Tell 'em we just road-off into the sunset 70 split_jid = self._owner.tool_split_jid(kid.getAttr('jid')) 71 p = Presence(to=kid.getAttr('jid'),frm=session.peer,typ='unavailable') 72 session.dispatch(p)
73
74 - def RosterPushOneToClient(self, contact, to, to_session=None, mode='set',options=None):
75 self.DEBUG('#ROSTER#: Pushing one out to client!','warn') 76 #Stanza Stuff 77 to=JID(to) 78 if not to: return # Not for us. 79 if to_session: 80 session = to_session 81 else: 82 session = self._owner.getsession(str(to)) 83 84 to_node=to.getNode() 85 #if not to_node: return # Yep, not for us. 86 to_domain=to.getDomain() 87 if not self._owner.Router.isFromOutside(to_domain): 88 #if to_domain in self._owner.servernames: 89 #bareto=to_node+'@'+to_domain 90 #to_roster=self._owner.DB.get(to_domain,to_node,'roster') 91 item = self._owner.DB.pull_roster( to_domain, to_node, str(contact)) 92 """ 93 <iq type='set'> 94 <query xmlns='jabber:iq:roster'> 95 <item 96 jid='contact@example.org' 97 subscription='none' 98 ask='subscribe' 99 name='MyContact'> 100 <group>MyBuddies</group> 101 </item> 102 </query> 103 </iq> 104 """ 105 out=Iq(typ=mode) 106 out.NT.query.setNamespace(NS_ROSTER) 107 atag = out.T.query.NT.item 108 atag.setAttr('jid',str(contact)) 109 if item: 110 for key,value in item.items(): 111 if key <> 'state': 112 try: atag.setAttr(key,value) 113 except: pass 114 """ 115 try: 116 for x,y in session.getRoster()[bareto].iteritems(): 117 atag.setAttr(x,y) 118 except: 119 pass 120 """ 121 122 barejid = session.getBareJID() 123 try: 124 for resource in self._owner.Router._data[barejid].keys(): 125 s=self._owner.getsession(barejid+'/'+resource) 126 s.send(out) 127 except: 128 pass 129 self.DEBUG('#ROSTER#: Pushing one out to client %s! [COMPLETE]'%(barejid),'warn')
130 131 132
133 - def RosterPushOne(self,session,stanza,mode='set',options=None):
134 self.DEBUG('#ROSTER#: Pushing one out!','warn') 135 #Stanza Stuff 136 to=stanza['to'] 137 if not to: return # Not for us. 138 to_node=to.getNode() 139 if not to_node: return # Yep, not for us. 140 to_domain=to.getDomain() 141 if not self._owner.Router.isFromOutside(to_domain): 142 #if to_domain in self._owner.servernames: 143 bareto=to_node+'@'+to_domain 144 to_roster=self._owner.DB.get(to_domain,to_node,'roster') 145 """ 146 <iq type='set'> 147 <query xmlns='jabber:iq:roster'> 148 <item 149 jid='contact@example.org' 150 subscription='none' 151 ask='subscribe' 152 name='MyContact'> 153 <group>MyBuddies</group> 154 </item> 155 </query> 156 </iq> 157 """ 158 out=Iq(typ=mode) 159 out.NT.query.setNamespace(NS_ROSTER) 160 atag = out.T.query.NT.item 161 s_split_jid = session.getSplitJID() 162 split_jid = self._owner.tool_split_jid(bareto) 163 name = self._owner.DB.get(split_jid[1],split_jid[0],'name') 164 groups = session.getGroups() 165 atag.setAttr('jid',bareto) 166 try: 167 for x,y in session.getRoster()[bareto].iteritems(): 168 atag.setAttr(x,y) 169 except: 170 pass 171 if options == {} and options.has_key('attr'): 172 for ok,od in options['attr']: 173 atag.setAttr(ok,od) 174 if atag.getAttr('name') == None and name != None: atag.setAttr('name',name) 175 176 ask = atag.getAttr('ask') 177 if ask == 'InternalDelete': 178 atag.delAttr('ask') 179 self._owner.DB.del_from_roster_jid(s_split_jid[1],s_split_jid[0],bareto,'ask') 180 181 if groups != None: 182 for gn,gm in groups.iteritems(): 183 if bareto in gm: 184 atag.T.group.setData(gn) 185 break 186 else: 187 atag.T.group.setData('My Friends') 188 barejid = session.getBareJID() 189 for resource in self._owner.Router._data[barejid].keys(): 190 s=self._owner.getsession(barejid+'/'+resource) 191 s.send(out) 192 self.DEBUG('#ROSTER#: Pushing one out! [COMPLETE]','warn')
193
194 - def RosterPush(self,session,stanza,mode='result'):
195 rep=stanza.buildReply(mode) 196 the_roster_guy = session.getRoster() 197 if the_roster_guy == None: return 198 for k,v in the_roster_guy.iteritems(): 199 atag = rep.T.query.NT.item 200 split_jid = self._owner.tool_split_jid(k) 201 if split_jid != None: 202 name = self._owner.DB.get(split_jid[1],split_jid[0],'name') 203 else: 204 name = None 205 groups = session.getGroups() 206 atag.setAttr('jid',k) 207 for x,y in v.iteritems(): 208 atag.setAttr(x,y) 209 if atag.getAttr('name') == None and name != None: atag.setAttr('name',name) 210 211 if groups != None: 212 for gn,gm in groups.iteritems(): 213 for igm in gm: 214 if igm == k: 215 atag.T.group.setData(gn) 216 break 217 else: 218 atag.T.group.setData('My Friends') 219 session.send(rep)
220
221 - def RosterPushToClient(self, bareto, to_session=None):
222 if not to_session: 223 session = self._owner.getsession(bareto) 224 else: 225 session = to_session 226 227 if not session: 228 self.DEBUG("Could not find suitable 'to' session", "error") 229 return 230 231 if self._owner.Router.isFromOutside(JID(bareto).getDomain()): 232 #if JID(bareto).getDomain() not in self._owner.servernames: 233 self.DEBUG("Client not in a local server. Returning", "warn") 234 return 235 236 rep = Iq(typ="set", queryNS = NS_ROSTER) 237 the_roster_guy = session.getRoster() 238 if the_roster_guy == None: return 239 #for k,v in the_roster_guy.iteritems(): 240 for k,v in the_roster_guy.items(): 241 atag = rep.T.query.NT.item 242 split_jid = self._owner.tool_split_jid(k) 243 if split_jid != None: 244 name = self._owner.DB.get(split_jid[1],split_jid[0],'name') 245 else: 246 name = None 247 groups = session.getGroups() 248 atag.setAttr('jid',k) 249 for x,y in v.iteritems(): 250 atag.setAttr(x,y) 251 if atag.getAttr('name') == None and name != None: atag.setAttr('name',name) 252 253 if groups != None: 254 for gn,gm in groups.iteritems(): 255 for igm in gm: 256 if igm == k: 257 atag.T.group.setData(gn) 258 break 259 else: 260 atag.T.group.setData('My Friends') 261 session.send(rep)
262 263
264 - def RosterIqHandler(self,session,stanza):
265 self.DEBUG("Roster Iq handler called","info") 266 #print "session info:", dir(session) 267 s_split_jid = self._owner.tool_split_jid(session.peer) 268 if stanza.getType() == 'set' and stanza.getTag('query').kids != []: 269 for kid in stanza.getTag('query').kids: 270 split_jid = self._owner.tool_split_jid(kid.getAttr('jid')) 271 if kid.getName() == 'item' and kid.getAttr('subscription') != 'remove': 272 self.RosterAdd(session,stanza) 273 elif kid.getName() == 'item' and kid.getAttr('subscription') == 'remove': 274 self.RosterRemove(session,stanza) 275 self.RosterPush(session,stanza,'set') #Push it out, will ya? 276 IQ = Iq(typ='result',to=session.peer) 277 IQ.setAttr('id',stanza.getID()) 278 session.send(IQ) 279 elif stanza.getType() == 'get' and stanza.getTag('query').kids == []: 280 self.RosterPush(session,stanza,'result') #How's the result??? 281 282 raise NodeProcessed
283