1
2
3
4
5
6 from xmpp import *
7
9 NS = NS_ROSTER
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
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
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
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
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
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
75 self.DEBUG('#ROSTER#: Pushing one out to client!','warn')
76
77 to=JID(to)
78 if not to: return
79 if to_session:
80 session = to_session
81 else:
82 session = self._owner.getsession(str(to))
83
84 to_node=to.getNode()
85
86 to_domain=to.getDomain()
87 if not self._owner.Router.isFromOutside(to_domain):
88
89
90
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
134 self.DEBUG('#ROSTER#: Pushing one out!','warn')
135
136 to=stanza['to']
137 if not to: return
138 to_node=to.getNode()
139 if not to_node: return
140 to_domain=to.getDomain()
141 if not self._owner.Router.isFromOutside(to_domain):
142
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'):
220
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
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
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
283