annotate sat.tac @ 12:ef8060d365cb

whitespace ping to avoid disconnection (was disconnected after 6 min of inactivity with openfire)
author Goffi <goffi@goffi.org>
date Wed, 28 Oct 2009 00:39:29 +0100
parents 5799493fa548
children bd9e9997d540
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
goffi@necton2
parents:
diff changeset
1 #!/usr/bin/python
goffi@necton2
parents:
diff changeset
2 # -*- coding: utf-8 -*-
goffi@necton2
parents:
diff changeset
3
goffi@necton2
parents:
diff changeset
4 """
goffi@necton2
parents:
diff changeset
5 SAT: a jabber client
goffi@necton2
parents:
diff changeset
6 Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org)
goffi@necton2
parents:
diff changeset
7
goffi@necton2
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
goffi@necton2
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
goffi@necton2
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
goffi@necton2
parents:
diff changeset
11 (at your option) any later version.
goffi@necton2
parents:
diff changeset
12
goffi@necton2
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
goffi@necton2
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
goffi@necton2
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
goffi@necton2
parents:
diff changeset
16 GNU General Public License for more details.
goffi@necton2
parents:
diff changeset
17
goffi@necton2
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
goffi@necton2
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
goffi@necton2
parents:
diff changeset
20 """
goffi@necton2
parents:
diff changeset
21
goffi@necton2
parents:
diff changeset
22
2
c49345fd7737 refactoring: moved sat to sat.tac, now a twisted application so we can use twistd.
Goffi <goffi@goffi.org>
parents: 1
diff changeset
23 from twisted.application import internet, service
12
ef8060d365cb whitespace ping to avoid disconnection (was disconnected after 6 min of inactivity with openfire)
Goffi <goffi@goffi.org>
parents: 6
diff changeset
24 from twisted.internet import glib2reactor, protocol, task
0
goffi@necton2
parents:
diff changeset
25 glib2reactor.install()
goffi@necton2
parents:
diff changeset
26
goffi@necton2
parents:
diff changeset
27 from twisted.words.protocols.jabber import client, jid, xmlstream, error
goffi@necton2
parents:
diff changeset
28 from twisted.words.xish import domish
goffi@necton2
parents:
diff changeset
29
goffi@necton2
parents:
diff changeset
30 from twisted.internet import reactor
goffi@necton2
parents:
diff changeset
31 import pdb
goffi@necton2
parents:
diff changeset
32
goffi@necton2
parents:
diff changeset
33 from sat_bridge.DBus import DBusBridge
goffi@necton2
parents:
diff changeset
34 import logging
goffi@necton2
parents:
diff changeset
35 from logging import debug, info, error
goffi@necton2
parents:
diff changeset
36
goffi@necton2
parents:
diff changeset
37 import signal, sys
goffi@necton2
parents:
diff changeset
38 import os.path
goffi@necton2
parents:
diff changeset
39
goffi@necton2
parents:
diff changeset
40 from tools.memory import Memory
goffi@necton2
parents:
diff changeset
41 from glob import glob
goffi@necton2
parents:
diff changeset
42
goffi@necton2
parents:
diff changeset
43
goffi@necton2
parents:
diff changeset
44 ### logging configuration FIXME: put this elsewhere ###
goffi@necton2
parents:
diff changeset
45 logging.basicConfig(level=logging.DEBUG,
goffi@necton2
parents:
diff changeset
46 format='%(message)s')
goffi@necton2
parents:
diff changeset
47 ###
goffi@necton2
parents:
diff changeset
48
goffi@necton2
parents:
diff changeset
49
goffi@necton2
parents:
diff changeset
50
goffi@necton2
parents:
diff changeset
51
goffi@necton2
parents:
diff changeset
52 class SAT:
goffi@necton2
parents:
diff changeset
53
goffi@necton2
parents:
diff changeset
54 def __init__(self):
2
c49345fd7737 refactoring: moved sat to sat.tac, now a twisted application so we can use twistd.
Goffi <goffi@goffi.org>
parents: 1
diff changeset
55 #self.reactor=reactor
0
goffi@necton2
parents:
diff changeset
56 self.memory=Memory()
goffi@necton2
parents:
diff changeset
57 self.server_features=[] #XXX: temp dic, need to be transfered into self.memory in the future
goffi@necton2
parents:
diff changeset
58 self.connected=False #FIXME: use twisted var instead
goffi@necton2
parents:
diff changeset
59
goffi@necton2
parents:
diff changeset
60 self._iq_cb_map = {} #callback called when ns is found on IQ
goffi@necton2
parents:
diff changeset
61 self._waiting_conf = {} #callback called when a confirmation is received
goffi@necton2
parents:
diff changeset
62 self._progress_cb_map = {} #callback called when a progress is requested (key = progress id)
goffi@necton2
parents:
diff changeset
63 self.plugins = {}
goffi@necton2
parents:
diff changeset
64
goffi@necton2
parents:
diff changeset
65 self.bridge=DBusBridge()
goffi@necton2
parents:
diff changeset
66 self.bridge.register("connect", self.connect)
1
a06a151fc31f Disconnect first draft
Goffi <goffi@goffi.org>
parents: 0
diff changeset
67 self.bridge.register("disconnect", self.disconnect)
0
goffi@necton2
parents:
diff changeset
68 self.bridge.register("getContacts", self.memory.getContacts)
goffi@necton2
parents:
diff changeset
69 self.bridge.register("getPresenceStatus", self.memory.getPresenceStatus)
goffi@necton2
parents:
diff changeset
70 self.bridge.register("sendMessage", self.sendMessage)
goffi@necton2
parents:
diff changeset
71 self.bridge.register("setParam", self.setParam)
goffi@necton2
parents:
diff changeset
72 self.bridge.register("getParam", self.memory.getParam)
goffi@necton2
parents:
diff changeset
73 self.bridge.register("getParams", self.memory.getParams)
goffi@necton2
parents:
diff changeset
74 self.bridge.register("getParamsCategories", self.memory.getParamsCategories)
goffi@necton2
parents:
diff changeset
75 self.bridge.register("getHistory", self.memory.getHistory)
goffi@necton2
parents:
diff changeset
76 self.bridge.register("setPresence", self.setPresence)
goffi@necton2
parents:
diff changeset
77 self.bridge.register("addContact", self.addContact)
goffi@necton2
parents:
diff changeset
78 self.bridge.register("delContact", self.delContact)
goffi@necton2
parents:
diff changeset
79 self.bridge.register("isConnected", self.isConnected)
goffi@necton2
parents:
diff changeset
80 self.bridge.register("confirmationAnswer", self.confirmationAnswer)
goffi@necton2
parents:
diff changeset
81 self.bridge.register("getProgress", self.getProgress)
goffi@necton2
parents:
diff changeset
82
goffi@necton2
parents:
diff changeset
83 self._import_plugins()
5
c0c92129a54b connect and disconnect management
Goffi <goffi@goffi.org>
parents: 2
diff changeset
84 #self.connect()
0
goffi@necton2
parents:
diff changeset
85
goffi@necton2
parents:
diff changeset
86
goffi@necton2
parents:
diff changeset
87 def _import_plugins(self):
goffi@necton2
parents:
diff changeset
88 """Import all plugins found in plugins directory"""
goffi@necton2
parents:
diff changeset
89 #TODO: manage dependencies
goffi@necton2
parents:
diff changeset
90 plug_lst = [os.path.splitext(plugin)[0] for plugin in map(os.path.basename,glob ("plugins/plugin*.py"))]
goffi@necton2
parents:
diff changeset
91
goffi@necton2
parents:
diff changeset
92 for plug in plug_lst:
goffi@necton2
parents:
diff changeset
93 plug_path = 'plugins.'+plug
goffi@necton2
parents:
diff changeset
94 __import__(plug_path)
goffi@necton2
parents:
diff changeset
95 mod = sys.modules[plug_path]
goffi@necton2
parents:
diff changeset
96 plug_info = mod.PLUGIN_INFO
goffi@necton2
parents:
diff changeset
97 info ("importing plugin: %s", plug_info['name'])
goffi@necton2
parents:
diff changeset
98 self.plugins[plug_info['import_name']] = getattr(mod, plug_info['main'])(self)
goffi@necton2
parents:
diff changeset
99
goffi@necton2
parents:
diff changeset
100 def connect(self):
6
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
101 if (self.connected):
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
102 info("already connected !")
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
103 return
2
c49345fd7737 refactoring: moved sat to sat.tac, now a twisted application so we can use twistd.
Goffi <goffi@goffi.org>
parents: 1
diff changeset
104 print "connecting..."
5
c0c92129a54b connect and disconnect management
Goffi <goffi@goffi.org>
parents: 2
diff changeset
105 reactor.connectTCP(self.memory.getParamV("Server", "Connection"), 5222, self.factory)
2
c49345fd7737 refactoring: moved sat to sat.tac, now a twisted application so we can use twistd.
Goffi <goffi@goffi.org>
parents: 1
diff changeset
106
6
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
107 def disconnect(self):
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
108 if (not self.connected):
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
109 info("not connected !")
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
110 return
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
111 info("Disconnecting...")
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
112 self.factory.stopTrying()
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
113 if self.xmlstream:
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
114 self.xmlstream.sendFooter()
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
115
2
c49345fd7737 refactoring: moved sat to sat.tac, now a twisted application so we can use twistd.
Goffi <goffi@goffi.org>
parents: 1
diff changeset
116 def getService(self):
c49345fd7737 refactoring: moved sat to sat.tac, now a twisted application so we can use twistd.
Goffi <goffi@goffi.org>
parents: 1
diff changeset
117 print "GetService !"
c49345fd7737 refactoring: moved sat to sat.tac, now a twisted application so we can use twistd.
Goffi <goffi@goffi.org>
parents: 1
diff changeset
118 """if (self.connected):
0
goffi@necton2
parents:
diff changeset
119 info("already connected !")
2
c49345fd7737 refactoring: moved sat to sat.tac, now a twisted application so we can use twistd.
Goffi <goffi@goffi.org>
parents: 1
diff changeset
120 return"""
5
c0c92129a54b connect and disconnect management
Goffi <goffi@goffi.org>
parents: 2
diff changeset
121 info("Getting service...")
0
goffi@necton2
parents:
diff changeset
122 self.me = jid.JID(self.memory.getParamV("JabberID", "Connection"))
goffi@necton2
parents:
diff changeset
123 self.factory = client.XMPPClientFactory(self.me, self.memory.getParamV("Password", "Connection"))
6
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
124 self.factory.clientConnectionLost = self.connectionLost
0
goffi@necton2
parents:
diff changeset
125 self.factory.addBootstrap(xmlstream.STREAM_AUTHD_EVENT,self.authd)
goffi@necton2
parents:
diff changeset
126 self.factory.addBootstrap(xmlstream.INIT_FAILED_EVENT,self.failed)
2
c49345fd7737 refactoring: moved sat to sat.tac, now a twisted application so we can use twistd.
Goffi <goffi@goffi.org>
parents: 1
diff changeset
127 return internet.TCPClient(self.memory.getParamV("Server", "Connection"), 5222, self.factory)
0
goffi@necton2
parents:
diff changeset
128
goffi@necton2
parents:
diff changeset
129 def run(self):
goffi@necton2
parents:
diff changeset
130 debug("running app")
goffi@necton2
parents:
diff changeset
131 reactor.run()
goffi@necton2
parents:
diff changeset
132
goffi@necton2
parents:
diff changeset
133 def stop(self):
goffi@necton2
parents:
diff changeset
134 debug("stopping app")
goffi@necton2
parents:
diff changeset
135 reactor.stop()
goffi@necton2
parents:
diff changeset
136
goffi@necton2
parents:
diff changeset
137 def authd(self,xmlstream):
6
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
138 self.xmlstream=xmlstream
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
139 roster=client.IQ(xmlstream,'get')
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
140 roster.addElement(('jabber:iq:roster', 'query'))
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
141 roster.addCallback(self.rosterCb)
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
142 roster.send()
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
143 debug("server = %s",self.memory.getParamV("Server", "Connection"))
0
goffi@necton2
parents:
diff changeset
144
6
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
145 ###FIXME: tmp disco ###
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
146 self.memory.registerFeature("http://jabber.org/protocol/disco#info")
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
147 self.disco(self.memory.getParamV("Server", "Connection"), self.serverDisco)
0
goffi@necton2
parents:
diff changeset
148
goffi@necton2
parents:
diff changeset
149
6
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
150 #we now send our presence status
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
151 self.setPresence(status="Online")
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
152
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
153 # add a callback for the messages
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
154 xmlstream.addObserver('/message', self.gotMessage)
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
155 xmlstream.addObserver('/presence', self.presenceCb)
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
156 xmlstream.addObserver("/iq[@type='set' or @type='get']", self.iqCb)
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
157 print "********** CONNECTED **********"
12
ef8060d365cb whitespace ping to avoid disconnection (was disconnected after 6 min of inactivity with openfire)
Goffi <goffi@goffi.org>
parents: 6
diff changeset
158 self.connected=True
ef8060d365cb whitespace ping to avoid disconnection (was disconnected after 6 min of inactivity with openfire)
Goffi <goffi@goffi.org>
parents: 6
diff changeset
159 self.keep_alife = task.LoopingCall(self.xmlstream.send, " ") #Needed to avoid disconnection (specially with openfire)
ef8060d365cb whitespace ping to avoid disconnection (was disconnected after 6 min of inactivity with openfire)
Goffi <goffi@goffi.org>
parents: 6
diff changeset
160 self.keep_alife.start(180)
ef8060d365cb whitespace ping to avoid disconnection (was disconnected after 6 min of inactivity with openfire)
Goffi <goffi@goffi.org>
parents: 6
diff changeset
161
6
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
162 #reactor.callLater(2,self.sendFile,"goffi2@jabber.goffi.int/Psi", "/tmp/fakefile")
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
163
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
164 def connectionLost(self, connector, unused_reason):
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
165 print "********** DISCONNECTED **********"
12
ef8060d365cb whitespace ping to avoid disconnection (was disconnected after 6 min of inactivity with openfire)
Goffi <goffi@goffi.org>
parents: 6
diff changeset
166 if self.keep_alife:
ef8060d365cb whitespace ping to avoid disconnection (was disconnected after 6 min of inactivity with openfire)
Goffi <goffi@goffi.org>
parents: 6
diff changeset
167 self.keep_alife.stop()
6
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
168 self.connected=False
5799493fa548 connection and disconnection management
Goffi <goffi@goffi.org>
parents: 5
diff changeset
169
0
goffi@necton2
parents:
diff changeset
170
goffi@necton2
parents:
diff changeset
171 def sendMessage(self,to,msg,type='chat'):
goffi@necton2
parents:
diff changeset
172 #FIXME: check validity of recipient
goffi@necton2
parents:
diff changeset
173 debug("Sending jabber message to %s...", to)
goffi@necton2
parents:
diff changeset
174 message = domish.Element(('jabber:client','message'))
goffi@necton2
parents:
diff changeset
175 message["to"] = jid.JID(to).full()
goffi@necton2
parents:
diff changeset
176 message["from"] = self.me.full()
goffi@necton2
parents:
diff changeset
177 message["type"] = type
goffi@necton2
parents:
diff changeset
178 message.addElement("body", "jabber:client", msg)
goffi@necton2
parents:
diff changeset
179 self.xmlstream.send(message)
goffi@necton2
parents:
diff changeset
180 self.memory.addToHistory(self.me, self.me, jid.JID(to), message["type"], unicode(msg))
goffi@necton2
parents:
diff changeset
181 self.bridge.newMessage(message['from'], unicode(msg), to=message['to']) #We send back the message, so all clients are aware of it
goffi@necton2
parents:
diff changeset
182
goffi@necton2
parents:
diff changeset
183 def setParam(self, name, value, namespace):
goffi@necton2
parents:
diff changeset
184 """set wanted paramater and notice observers"""
goffi@necton2
parents:
diff changeset
185 info ("setting param: %s=%s in namespace %s", name, value, namespace)
goffi@necton2
parents:
diff changeset
186 self.memory.setParam(name, value, namespace)
goffi@necton2
parents:
diff changeset
187 self.bridge.paramUpdate(name, value, namespace)
goffi@necton2
parents:
diff changeset
188
goffi@necton2
parents:
diff changeset
189 def setRoster(self, to):
goffi@necton2
parents:
diff changeset
190 """Add a contact to roster list"""
goffi@necton2
parents:
diff changeset
191 to_jid=jid.JID(to)
goffi@necton2
parents:
diff changeset
192 roster=client.IQ(self.xmlstream,'set')
goffi@necton2
parents:
diff changeset
193 query=roster.addElement(('jabber:iq:roster', 'query'))
goffi@necton2
parents:
diff changeset
194 item=query.addElement("item")
goffi@necton2
parents:
diff changeset
195 item.attributes["jid"]=to_jid.userhost()
goffi@necton2
parents:
diff changeset
196 roster.send()
goffi@necton2
parents:
diff changeset
197 #TODO: check IQ result
goffi@necton2
parents:
diff changeset
198
goffi@necton2
parents:
diff changeset
199 def delRoster(self, to):
goffi@necton2
parents:
diff changeset
200 """Remove a contact from roster list"""
goffi@necton2
parents:
diff changeset
201 to_jid=jid.JID(to)
goffi@necton2
parents:
diff changeset
202 roster=client.IQ(self.xmlstream,'set')
goffi@necton2
parents:
diff changeset
203 query=roster.addElement(('jabber:iq:roster', 'query'))
goffi@necton2
parents:
diff changeset
204 item=query.addElement("item")
goffi@necton2
parents:
diff changeset
205 item.attributes["jid"]=to_jid.userhost()
goffi@necton2
parents:
diff changeset
206 item.attributes["subscription"]="remove"
goffi@necton2
parents:
diff changeset
207 roster.send()
goffi@necton2
parents:
diff changeset
208 #TODO: check IQ result
goffi@necton2
parents:
diff changeset
209
goffi@necton2
parents:
diff changeset
210
goffi@necton2
parents:
diff changeset
211 def failed(self,xmlstream):
goffi@necton2
parents:
diff changeset
212 debug("failed: %s", xmlstream.getErrorMessage())
goffi@necton2
parents:
diff changeset
213 debug("failed: %s", dir(xmlstream))
goffi@necton2
parents:
diff changeset
214
goffi@necton2
parents:
diff changeset
215 def isConnected(self):
goffi@necton2
parents:
diff changeset
216 return self.connected
goffi@necton2
parents:
diff changeset
217
goffi@necton2
parents:
diff changeset
218 ## jabber methods ##
goffi@necton2
parents:
diff changeset
219
goffi@necton2
parents:
diff changeset
220 def disco (self, item, callback, node=None):
goffi@necton2
parents:
diff changeset
221 """XEP-0030 Service discovery Feature."""
goffi@necton2
parents:
diff changeset
222 disco=client.IQ(self.xmlstream,'get')
goffi@necton2
parents:
diff changeset
223 disco["from"]=self.me.full()
goffi@necton2
parents:
diff changeset
224 disco["to"]=item
goffi@necton2
parents:
diff changeset
225 disco.addElement(('http://jabber.org/protocol/disco#info', 'query'))
goffi@necton2
parents:
diff changeset
226 disco.addCallback(callback)
goffi@necton2
parents:
diff changeset
227 disco.send()
goffi@necton2
parents:
diff changeset
228
goffi@necton2
parents:
diff changeset
229
goffi@necton2
parents:
diff changeset
230 def setPresence(self, to="", type="", show="", status="", priority=0):
goffi@necton2
parents:
diff changeset
231 """Send our presence information"""
goffi@necton2
parents:
diff changeset
232 presence = domish.Element(('jabber:client', 'presence'))
goffi@necton2
parents:
diff changeset
233 if not type in ["", "unavailable", "subscribed", "subscribe",
goffi@necton2
parents:
diff changeset
234 "unsubscribe", "unsubscribed", "prob", "error"]:
goffi@necton2
parents:
diff changeset
235 error("Type error !")
goffi@necton2
parents:
diff changeset
236 #TODO: throw an error
goffi@necton2
parents:
diff changeset
237 return
goffi@necton2
parents:
diff changeset
238
goffi@necton2
parents:
diff changeset
239 if to:
goffi@necton2
parents:
diff changeset
240 presence.attributes["to"]=to
goffi@necton2
parents:
diff changeset
241 if type:
goffi@necton2
parents:
diff changeset
242 presence.attributes["type"]=type
goffi@necton2
parents:
diff changeset
243
goffi@necton2
parents:
diff changeset
244 for element in ["show", "status", "priority"]:
goffi@necton2
parents:
diff changeset
245 if locals()[element]:
goffi@necton2
parents:
diff changeset
246 presence.addElement(element).addContent(unicode(locals()[element]))
goffi@necton2
parents:
diff changeset
247
goffi@necton2
parents:
diff changeset
248 self.xmlstream.send(presence)
goffi@necton2
parents:
diff changeset
249
goffi@necton2
parents:
diff changeset
250 def addContact(self, to):
goffi@necton2
parents:
diff changeset
251 """Add a contact in roster list"""
goffi@necton2
parents:
diff changeset
252 to_jid=jid.JID(to)
goffi@necton2
parents:
diff changeset
253 self.setRoster(to_jid.userhost())
goffi@necton2
parents:
diff changeset
254 self.setPresence(to_jid.userhost(), "subscribe")
goffi@necton2
parents:
diff changeset
255
goffi@necton2
parents:
diff changeset
256 def delContact(self, to):
goffi@necton2
parents:
diff changeset
257 """Remove contact from roster list"""
goffi@necton2
parents:
diff changeset
258 to_jid=jid.JID(to)
goffi@necton2
parents:
diff changeset
259 self.delRoster(to_jid.userhost())
goffi@necton2
parents:
diff changeset
260 self.bridge.contactDeleted(to)
goffi@necton2
parents:
diff changeset
261
goffi@necton2
parents:
diff changeset
262 def gotMessage(self,message):
goffi@necton2
parents:
diff changeset
263 debug (u"got_message from: %s", message["from"])
goffi@necton2
parents:
diff changeset
264 for e in message.elements():
goffi@necton2
parents:
diff changeset
265 if e.name == "body":
goffi@necton2
parents:
diff changeset
266 self.bridge.newMessage(message["from"], e.children[0])
goffi@necton2
parents:
diff changeset
267 self.memory.addToHistory(self.me, jid.JID(message["from"]), self.me, "chat", e.children[0])
goffi@necton2
parents:
diff changeset
268 break
goffi@necton2
parents:
diff changeset
269
goffi@necton2
parents:
diff changeset
270 ## callbacks ##
goffi@necton2
parents:
diff changeset
271
goffi@necton2
parents:
diff changeset
272 def add_IQ_cb(self, ns, cb):
goffi@necton2
parents:
diff changeset
273 """Add an IQ callback on namespace ns"""
goffi@necton2
parents:
diff changeset
274 debug ("Registered callback for namespace %s", ns)
goffi@necton2
parents:
diff changeset
275 self._iq_cb_map[ns]=cb
goffi@necton2
parents:
diff changeset
276
goffi@necton2
parents:
diff changeset
277 def iqCb(self, stanza):
goffi@necton2
parents:
diff changeset
278 info ("iqCb")
goffi@necton2
parents:
diff changeset
279 debug ("="*20)
goffi@necton2
parents:
diff changeset
280 debug ("DEBUG:\n")
goffi@necton2
parents:
diff changeset
281 debug (stanza.toXml().encode('utf-8'))
goffi@necton2
parents:
diff changeset
282 debug ("="*20)
goffi@necton2
parents:
diff changeset
283 #FIXME: temporary ugly code
goffi@necton2
parents:
diff changeset
284 uri = stanza.firstChildElement().uri
goffi@necton2
parents:
diff changeset
285 if self._iq_cb_map.has_key(uri):
goffi@necton2
parents:
diff changeset
286 self._iq_cb_map[uri](stanza)
goffi@necton2
parents:
diff changeset
287 #TODO: manage errors stanza
goffi@necton2
parents:
diff changeset
288
goffi@necton2
parents:
diff changeset
289 def presenceCb(self, elem):
goffi@necton2
parents:
diff changeset
290 info ("presence update for [%s]", elem.getAttribute("from"))
goffi@necton2
parents:
diff changeset
291 debug("\n\nXML=\n%s\n\n", elem.toXml())
goffi@necton2
parents:
diff changeset
292 presence={}
goffi@necton2
parents:
diff changeset
293 presence["jid"]=elem.getAttribute("from")
goffi@necton2
parents:
diff changeset
294 presence["type"]=elem.getAttribute("type") or ""
goffi@necton2
parents:
diff changeset
295 presence["show"]=""
goffi@necton2
parents:
diff changeset
296 presence["status"]=""
goffi@necton2
parents:
diff changeset
297 presence["priority"]=0
goffi@necton2
parents:
diff changeset
298
goffi@necton2
parents:
diff changeset
299 for item in elem.elements():
goffi@necton2
parents:
diff changeset
300 if presence.has_key(item.name):
goffi@necton2
parents:
diff changeset
301 presence[item.name]=item.children[0]
goffi@necton2
parents:
diff changeset
302
goffi@necton2
parents:
diff changeset
303 ### we check if the status is not about subscription ###
goffi@necton2
parents:
diff changeset
304 #TODO: check that from jid is one we wants to subscribe (ie: check a recent subscription asking)
goffi@necton2
parents:
diff changeset
305 if jid.JID(presence["jid"]).userhost()!=self.me.userhost():
goffi@necton2
parents:
diff changeset
306 if presence["type"]=="subscribed":
goffi@necton2
parents:
diff changeset
307 debug ("subscription answer")
goffi@necton2
parents:
diff changeset
308 elif presence["type"]=="unsubscribed":
goffi@necton2
parents:
diff changeset
309 debug ("unsubscription answer")
goffi@necton2
parents:
diff changeset
310 elif presence["type"]=="subscribe":
goffi@necton2
parents:
diff changeset
311 #FIXME: auto answer for subscribe request, must be checked !
goffi@necton2
parents:
diff changeset
312 debug ("subscription request")
goffi@necton2
parents:
diff changeset
313 self.setPresence(to=presence["jid"], type="subscribed")
goffi@necton2
parents:
diff changeset
314 else:
goffi@necton2
parents:
diff changeset
315 #We keep presence information only if it is not for subscription
goffi@necton2
parents:
diff changeset
316 self.memory.addPresenceStatus(presence["jid"], presence["type"], presence["show"],
goffi@necton2
parents:
diff changeset
317 presence["status"], int(presence["priority"]))
goffi@necton2
parents:
diff changeset
318
goffi@necton2
parents:
diff changeset
319 #now it's time to notify frontends
goffi@necton2
parents:
diff changeset
320 self.bridge.presenceUpdate(presence["jid"], presence["type"], presence["show"],
goffi@necton2
parents:
diff changeset
321 presence["status"], int(presence["priority"]))
goffi@necton2
parents:
diff changeset
322
goffi@necton2
parents:
diff changeset
323 def rosterCb(self,roster):
goffi@necton2
parents:
diff changeset
324 for contact in roster.firstChildElement().elements():
goffi@necton2
parents:
diff changeset
325 info ("new contact in roster list: %s", contact['jid'])
goffi@necton2
parents:
diff changeset
326 #and now the groups
goffi@necton2
parents:
diff changeset
327 groups=[]
goffi@necton2
parents:
diff changeset
328 for group in contact.elements():
goffi@necton2
parents:
diff changeset
329 if group.name!="group":
goffi@necton2
parents:
diff changeset
330 error("Unexpected element !")
goffi@necton2
parents:
diff changeset
331 break
goffi@necton2
parents:
diff changeset
332 groups.append(str(group))
goffi@necton2
parents:
diff changeset
333 self.memory.addContact(contact['jid'], contact.attributes, groups)
goffi@necton2
parents:
diff changeset
334 self.bridge.newContact(contact['jid'], contact.attributes, groups)
goffi@necton2
parents:
diff changeset
335
goffi@necton2
parents:
diff changeset
336 def serverDisco(self, disco):
goffi@necton2
parents:
diff changeset
337 """xep-0030 Discovery Protocol."""
goffi@necton2
parents:
diff changeset
338 for element in disco.firstChildElement().elements():
goffi@necton2
parents:
diff changeset
339 if element.name=="feature":
goffi@necton2
parents:
diff changeset
340 debug ("Feature dectetee: %s",element["var"])
goffi@necton2
parents:
diff changeset
341 self.server_features.append(element["var"])
goffi@necton2
parents:
diff changeset
342 elif element.name=="identity":
goffi@necton2
parents:
diff changeset
343 debug ("categorie= %s",element["category"])
goffi@necton2
parents:
diff changeset
344 debug ("features= %s",self.server_features)
goffi@necton2
parents:
diff changeset
345
goffi@necton2
parents:
diff changeset
346 ## Generic HMI ##
goffi@necton2
parents:
diff changeset
347
goffi@necton2
parents:
diff changeset
348 def askConfirmation(self, id, type, data, cb):
goffi@necton2
parents:
diff changeset
349 """Add a confirmation callback"""
goffi@necton2
parents:
diff changeset
350 if self._waiting_conf.has_key(id):
goffi@necton2
parents:
diff changeset
351 error ("Attempt to register two callbacks for the same confirmation")
goffi@necton2
parents:
diff changeset
352 else:
goffi@necton2
parents:
diff changeset
353 self._waiting_conf[id] = cb
goffi@necton2
parents:
diff changeset
354 self.bridge.askConfirmation(type, id, data)
goffi@necton2
parents:
diff changeset
355
goffi@necton2
parents:
diff changeset
356
goffi@necton2
parents:
diff changeset
357 def confirmationAnswer(self, id, accepted, data):
goffi@necton2
parents:
diff changeset
358 """Called by frontends to answer confirmation requests"""
goffi@necton2
parents:
diff changeset
359 debug ("Received confirmation answer for id [%s]: %s", id, "accepted" if accepted else "refused")
goffi@necton2
parents:
diff changeset
360 if not self._waiting_conf.has_key(id):
goffi@necton2
parents:
diff changeset
361 error ("Received an unknown confirmation")
goffi@necton2
parents:
diff changeset
362 else:
goffi@necton2
parents:
diff changeset
363 cb = self._waiting_conf[id]
goffi@necton2
parents:
diff changeset
364 del self._waiting_conf[id]
goffi@necton2
parents:
diff changeset
365 cb(id, accepted, data)
goffi@necton2
parents:
diff changeset
366
goffi@necton2
parents:
diff changeset
367 def registerProgressCB(self, id, CB):
goffi@necton2
parents:
diff changeset
368 """Register a callback called when progress is requested for id"""
goffi@necton2
parents:
diff changeset
369 self._progress_cb_map[id] = CB
goffi@necton2
parents:
diff changeset
370
goffi@necton2
parents:
diff changeset
371 def removeProgressCB(self, id):
goffi@necton2
parents:
diff changeset
372 """Remove a progress callback"""
goffi@necton2
parents:
diff changeset
373 if not self._progress_cb_map.has_key(id):
goffi@necton2
parents:
diff changeset
374 error ("Trying to remove an unknow progress callback")
goffi@necton2
parents:
diff changeset
375 else:
goffi@necton2
parents:
diff changeset
376 del self._progress_cb_map[id]
goffi@necton2
parents:
diff changeset
377
goffi@necton2
parents:
diff changeset
378 def getProgress(self, id):
goffi@necton2
parents:
diff changeset
379 """Return a dict with progress information
goffi@necton2
parents:
diff changeset
380 data['position'] : current possition
goffi@necton2
parents:
diff changeset
381 data['size'] : end_position
goffi@necton2
parents:
diff changeset
382 """
goffi@necton2
parents:
diff changeset
383 data = {}
goffi@necton2
parents:
diff changeset
384 try:
goffi@necton2
parents:
diff changeset
385 self._progress_cb_map[id](data)
goffi@necton2
parents:
diff changeset
386 except KeyError:
goffi@necton2
parents:
diff changeset
387 pass
goffi@necton2
parents:
diff changeset
388 #debug("Requested progress for unknown id")
goffi@necton2
parents:
diff changeset
389 return data
goffi@necton2
parents:
diff changeset
390
goffi@necton2
parents:
diff changeset
391
2
c49345fd7737 refactoring: moved sat to sat.tac, now a twisted application so we can use twistd.
Goffi <goffi@goffi.org>
parents: 1
diff changeset
392 application = service.Application('SàT')
c49345fd7737 refactoring: moved sat to sat.tac, now a twisted application so we can use twistd.
Goffi <goffi@goffi.org>
parents: 1
diff changeset
393 sat = SAT()
c49345fd7737 refactoring: moved sat to sat.tac, now a twisted application so we can use twistd.
Goffi <goffi@goffi.org>
parents: 1
diff changeset
394 service = sat.getService()
c49345fd7737 refactoring: moved sat to sat.tac, now a twisted application so we can use twistd.
Goffi <goffi@goffi.org>
parents: 1
diff changeset
395 service.setServiceParent(application)
0
goffi@necton2
parents:
diff changeset
396
2
c49345fd7737 refactoring: moved sat to sat.tac, now a twisted application so we can use twistd.
Goffi <goffi@goffi.org>
parents: 1
diff changeset
397
c49345fd7737 refactoring: moved sat to sat.tac, now a twisted application so we can use twistd.
Goffi <goffi@goffi.org>
parents: 1
diff changeset
398 #app.memory.save() #FIXME: not the best place
c49345fd7737 refactoring: moved sat to sat.tac, now a twisted application so we can use twistd.
Goffi <goffi@goffi.org>
parents: 1
diff changeset
399 #debug("Good Bye")