Mercurial > libervia-backend
annotate sat.tac @ 15:218ec9984fa5
wokkel integration part III + memory saved again
- disco handler (plugins handled)
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 31 Oct 2009 00:18:35 +0100 |
parents | a62d7d453f22 |
children | 0a024d5e0cd0 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 SAT: a jabber client | |
6 Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org) | |
7 | |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
14 | 22 client_name = u'SàT (Salut à toi)' |
23 client_version = '0.0.1' | |
0 | 24 |
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
|
25 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
|
26 from twisted.internet import glib2reactor, protocol, task |
0 | 27 glib2reactor.install() |
28 | |
13
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
29 from twisted.words.protocols.jabber import jid, xmlstream, error |
0 | 30 from twisted.words.xish import domish |
31 | |
32 from twisted.internet import reactor | |
33 import pdb | |
34 | |
14 | 35 from wokkel import client, disco, xmppim, generic |
13
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
36 |
0 | 37 from sat_bridge.DBus import DBusBridge |
38 import logging | |
39 from logging import debug, info, error | |
40 | |
41 import signal, sys | |
42 import os.path | |
43 | |
44 from tools.memory import Memory | |
45 from glob import glob | |
46 | |
15
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
47 try: |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
48 from twisted.words.protocols.xmlstream import XMPPHandler |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
49 except ImportError: |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
50 from wokkel.subprotocols import XMPPHandler |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
51 |
0 | 52 |
53 ### logging configuration FIXME: put this elsewhere ### | |
54 logging.basicConfig(level=logging.DEBUG, | |
55 format='%(message)s') | |
56 ### | |
57 | |
58 | |
13
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
59 class SatXMPPClient(client.XMPPClient): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
60 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
61 def __init__(self, jid, password, host=None, port=5222): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
62 client.XMPPClient.__init__(self, jid, password, host, port) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
63 self.factory.clientConnectionLost = self.connectionLost |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
64 self.__connected=False |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
65 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
66 def _authd(self, xmlstream): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
67 print "SatXMPPClient" |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
68 client.XMPPClient._authd(self, xmlstream) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
69 self.__connected=True |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
70 print "********** CONNECTED **********" |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
71 self.streamInitialized() |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
72 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
73 def streamInitialized(self): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
74 """Called after _authd""" |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
75 self.keep_alife = task.LoopingCall(self.xmlstream.send, " ") #Needed to avoid disconnection (specially with openfire) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
76 self.keep_alife.start(180) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
77 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
78 def isConnected(self): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
79 return self.__connected |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
80 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
81 def connectionLost(self, connector, unused_reason): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
82 print "********** DISCONNECTED **********" |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
83 try: |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
84 self.keep_alife.stop() |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
85 except AttributeError: |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
86 debug("No keep_alife") |
0 | 87 |
88 | |
13
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
89 class SatMessageProtocol(xmppim.MessageProtocol): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
90 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
91 def __init__(self, host): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
92 xmppim.MessageProtocol.__init__(self) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
93 self.host = host |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
94 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
95 def onMessage(self, message): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
96 debug (u"got_message from: %s", message["from"]) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
97 for e in message.elements(): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
98 if e.name == "body": |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
99 self.host.bridge.newMessage(message["from"], e.children[0]) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
100 self.host.memory.addToHistory(self.host.me, jid.JID(message["from"]), self.host.me, "chat", e.children[0]) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
101 break |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
102 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
103 class SatRosterProtocol(xmppim.RosterClientProtocol): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
104 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
105 def __init__(self, host): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
106 xmppim.RosterClientProtocol.__init__(self) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
107 self.host = host |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
108 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
109 def rosterCb(self, roster): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
110 for jid, item in roster.iteritems(): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
111 info ("new contact in roster list: %s", jid) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
112 #FIXME: fill attributes |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
113 self.host.memory.addContact(jid, {}, item.groups) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
114 self.host.bridge.newContact(jid, {}, item.groups) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
115 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
116 def requestRoster(self): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
117 """ ask the server for Roster list """ |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
118 debug("requestRoster") |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
119 self.getRoster().addCallback(self.rosterCb) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
120 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
121 def removeItem(self, to): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
122 """Remove a contact from roster list""" |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
123 to_jid=jid.JID(to) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
124 xmppim.RosterClientProtocol.removeItem(self, to_jid) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
125 #TODO: check IQ result |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
126 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
127 def addItem(self, to): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
128 """Add a contact to roster list""" |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
129 to_jid=jid.JID(to) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
130 xmppim.RosterClientProtocol.addItem(self, to_jid) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
131 #TODO: check IQ result |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
132 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
133 class SatPresenceProtocol(xmppim.PresenceClientProtocol): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
134 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
135 def __init__(self, host): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
136 xmppim.PresenceClientProtocol.__init__(self) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
137 self.host = host |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
138 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
139 def availableReceived(self, entity, show=None, statuses=None, priority=0): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
140 info ("presence update for [%s]", entity) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
141 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
142 ### we check if the status is not about subscription ### |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
143 #FIXME: type is not needed anymore |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
144 #TODO: management of differents statuses (differents languages) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
145 status = statuses.values()[0] if len(statuses) else "" |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
146 self.host.memory.addPresenceStatus(entity.full(), "", show or "", |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
147 status or "", int(priority)) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
148 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
149 #now it's time to notify frontends |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
150 self.host.bridge.presenceUpdate(entity.full(), "", show or "", |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
151 status or "", int(priority)) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
152 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
153 def unavailableReceived(self, entity, statuses=None): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
154 #TODO: management of differents statuses (differents languages) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
155 status = statuses.values()[0] if len(statuses) else "" |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
156 self.host.memory.addPresenceStatus(entity.full(), "unavailable", "", |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
157 status or "", 0) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
158 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
159 #now it's time to notify frontends |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
160 self.host.bridge.presenceUpdate(entity.full(), "unavailable", "", |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
161 status or "", 0) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
162 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
163 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
164 def subscribedReceived(self, entity): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
165 debug ("subscription approved for [%s]" % entity) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
166 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
167 def unsubscribedReceived(self, entity): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
168 debug ("unsubscription confirmed for [%s]" % entity) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
169 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
170 def subscribeReceived(self, entity): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
171 #FIXME: auto answer for subscribe request, must be checked ! |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
172 debug ("subscription request for [%s]" % entity) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
173 self.subscribed(entity) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
174 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
175 def unsubscribeReceived(self, entity): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
176 debug ("unsubscription asked for [%s]" % entity) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
177 |
14 | 178 class SatDiscoProtocol(disco.DiscoClientProtocol): |
179 def __init__(self, host): | |
180 disco.DiscoClientProtocol.__init__(self) | |
181 | |
15
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
182 class SatFallbackHandler(generic.FallbackHandler): |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
183 def __init__(self, host): |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
184 generic.FallbackHandler.__init__(self) |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
185 |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
186 def iqFallback(self, iq): |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
187 #pdb.set_trace() |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
188 print "iqFallback: xml = [%s], handled=%s" % (iq.toXml(), "True" if iq.handled else "False") |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
189 generic.FallbackHandler.iqFallback(self, iq) |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
190 |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
191 |
13
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
192 class SAT(service.Service): |
0 | 193 |
194 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
|
195 #self.reactor=reactor |
0 | 196 self.memory=Memory() |
197 self.server_features=[] #XXX: temp dic, need to be transfered into self.memory in the future | |
198 | |
199 self._waiting_conf = {} #callback called when a confirmation is received | |
200 self._progress_cb_map = {} #callback called when a progress is requested (key = progress id) | |
201 self.plugins = {} | |
202 | |
203 self.bridge=DBusBridge() | |
204 self.bridge.register("connect", self.connect) | |
1 | 205 self.bridge.register("disconnect", self.disconnect) |
0 | 206 self.bridge.register("getContacts", self.memory.getContacts) |
207 self.bridge.register("getPresenceStatus", self.memory.getPresenceStatus) | |
208 self.bridge.register("sendMessage", self.sendMessage) | |
209 self.bridge.register("setParam", self.setParam) | |
210 self.bridge.register("getParam", self.memory.getParam) | |
211 self.bridge.register("getParams", self.memory.getParams) | |
212 self.bridge.register("getParamsCategories", self.memory.getParamsCategories) | |
213 self.bridge.register("getHistory", self.memory.getHistory) | |
214 self.bridge.register("setPresence", self.setPresence) | |
215 self.bridge.register("addContact", self.addContact) | |
216 self.bridge.register("delContact", self.delContact) | |
217 self.bridge.register("isConnected", self.isConnected) | |
218 self.bridge.register("confirmationAnswer", self.confirmationAnswer) | |
219 self.bridge.register("getProgress", self.getProgress) | |
220 | |
221 self._import_plugins() | |
5 | 222 #self.connect() |
0 | 223 |
224 | |
225 def _import_plugins(self): | |
226 """Import all plugins found in plugins directory""" | |
227 #TODO: manage dependencies | |
228 plug_lst = [os.path.splitext(plugin)[0] for plugin in map(os.path.basename,glob ("plugins/plugin*.py"))] | |
229 | |
230 for plug in plug_lst: | |
231 plug_path = 'plugins.'+plug | |
232 __import__(plug_path) | |
233 mod = sys.modules[plug_path] | |
234 plug_info = mod.PLUGIN_INFO | |
235 info ("importing plugin: %s", plug_info['name']) | |
236 self.plugins[plug_info['import_name']] = getattr(mod, plug_info['main'])(self) | |
237 | |
238 def connect(self): | |
13
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
239 if (self.isConnected()): |
6
5799493fa548
connection and disconnection management
Goffi <goffi@goffi.org>
parents:
5
diff
changeset
|
240 info("already connected !") |
5799493fa548
connection and disconnection management
Goffi <goffi@goffi.org>
parents:
5
diff
changeset
|
241 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
|
242 print "connecting..." |
13
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
243 self.me = jid.JID(self.memory.getParamV("JabberID", "Connection")) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
244 self.xmppclient = SatXMPPClient(self.me, self.memory.getParamV("Password", "Connection"), |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
245 self.memory.getParamV("Server", "Connection"), 5222) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
246 self.xmppclient.streamInitialized = self.streamInitialized |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
247 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
248 self.messageProt = SatMessageProtocol(self) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
249 self.messageProt.setHandlerParent(self.xmppclient) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
250 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
251 self.roster = SatRosterProtocol(self) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
252 self.roster.setHandlerParent(self.xmppclient) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
253 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
254 self.presence = SatPresenceProtocol(self) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
255 self.presence.setHandlerParent(self.xmppclient) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
256 |
15
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
257 self.fallBack = SatFallbackHandler(self) |
14 | 258 self.fallBack.setHandlerParent(self.xmppclient) |
259 | |
260 self.versionHandler = generic.VersionHandler(unicode(client_name), client_version) | |
261 self.versionHandler.setHandlerParent(self.xmppclient) | |
262 | |
15
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
263 debug ("setting plugins parents") |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
264 for plugin in self.plugins.iteritems(): |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
265 if isinstance(plugin[1], XMPPHandler): |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
266 plugin[1].setHandlerParent(self.xmppclient) |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
267 |
13
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
268 self.xmppclient.startService() |
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
|
269 |
6
5799493fa548
connection and disconnection management
Goffi <goffi@goffi.org>
parents:
5
diff
changeset
|
270 def disconnect(self): |
13
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
271 if (not self.isConnected()): |
6
5799493fa548
connection and disconnection management
Goffi <goffi@goffi.org>
parents:
5
diff
changeset
|
272 info("not connected !") |
5799493fa548
connection and disconnection management
Goffi <goffi@goffi.org>
parents:
5
diff
changeset
|
273 return |
5799493fa548
connection and disconnection management
Goffi <goffi@goffi.org>
parents:
5
diff
changeset
|
274 info("Disconnecting...") |
13
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
275 self.xmppclient.stopService() |
6
5799493fa548
connection and disconnection management
Goffi <goffi@goffi.org>
parents:
5
diff
changeset
|
276 |
13
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
277 def startService(self): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
278 info("Salut à toi ô mon frère !") |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
279 self.connect() |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
280 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
281 def stopService(self): |
15
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
282 self.memory.save() |
13
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
283 info("Salut aussi à Rantanplan") |
0 | 284 |
285 def run(self): | |
286 debug("running app") | |
287 reactor.run() | |
288 | |
289 def stop(self): | |
290 debug("stopping app") | |
291 reactor.stop() | |
292 | |
13
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
293 def streamInitialized(self): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
294 """Called when xmlstream is OK""" |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
295 SatXMPPClient.streamInitialized(self.xmppclient) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
296 debug ("XML stream is initialized") |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
297 self.xmlstream = self.xmppclient.xmlstream |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
298 self.me = self.xmppclient.jid #in case of the ressource has changed |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
299 |
15
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
300 self.disco = SatDiscoProtocol(self) |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
301 self.disco.setHandlerParent(self.xmppclient) |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
302 self.discoHandler = disco.DiscoHandler() |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
303 self.discoHandler.setHandlerParent(self.xmppclient) |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
304 |
13
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
305 self.roster.requestRoster() |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
306 |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
307 self.presence.available() |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
308 |
14 | 309 self.disco.requestInfo(jid.JID(self.memory.getParamV("Server", "Connection"))).addCallback(self.serverDisco) |
6
5799493fa548
connection and disconnection management
Goffi <goffi@goffi.org>
parents:
5
diff
changeset
|
310 |
0 | 311 |
312 def sendMessage(self,to,msg,type='chat'): | |
313 #FIXME: check validity of recipient | |
314 debug("Sending jabber message to %s...", to) | |
315 message = domish.Element(('jabber:client','message')) | |
316 message["to"] = jid.JID(to).full() | |
317 message["from"] = self.me.full() | |
318 message["type"] = type | |
319 message.addElement("body", "jabber:client", msg) | |
320 self.xmlstream.send(message) | |
321 self.memory.addToHistory(self.me, self.me, jid.JID(to), message["type"], unicode(msg)) | |
322 self.bridge.newMessage(message['from'], unicode(msg), to=message['to']) #We send back the message, so all clients are aware of it | |
323 | |
324 def setParam(self, name, value, namespace): | |
325 """set wanted paramater and notice observers""" | |
326 info ("setting param: %s=%s in namespace %s", name, value, namespace) | |
327 self.memory.setParam(name, value, namespace) | |
328 self.bridge.paramUpdate(name, value, namespace) | |
329 | |
330 def failed(self,xmlstream): | |
331 debug("failed: %s", xmlstream.getErrorMessage()) | |
332 debug("failed: %s", dir(xmlstream)) | |
333 | |
334 def isConnected(self): | |
13
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
335 try: |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
336 if self.xmppclient.isConnected(): |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
337 return True |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
338 except AttributeError: |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
339 #xmppclient not available |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
340 pass |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
341 return False |
0 | 342 |
343 ## jabber methods ## | |
344 | |
345 def setPresence(self, to="", type="", show="", status="", priority=0): | |
346 """Send our presence information""" | |
347 if not type in ["", "unavailable", "subscribed", "subscribe", | |
348 "unsubscribe", "unsubscribed", "prob", "error"]: | |
349 error("Type error !") | |
350 #TODO: throw an error | |
351 return | |
13
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
352 to_jid=jid.JID(to) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
353 #TODO: refactor subscription bridge API |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
354 if type=="": |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
355 self.presence.available(to_jid, show, status, priority) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
356 elif type=="subscribe": |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
357 self.presence.subscribe(to_jid) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
358 elif type=="subscribed": |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
359 self.presence.subscribed(to_jid) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
360 elif type=="unsubscribe": |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
361 self.presence.unsubscribe(to_jid) |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
362 elif type=="unsubscribed": |
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
363 self.presence.unsubscribed(to_jid) |
0 | 364 |
365 | |
366 def addContact(self, to): | |
367 """Add a contact in roster list""" | |
368 to_jid=jid.JID(to) | |
13
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
369 self.roster.addItem(to_jid.userhost()) |
0 | 370 self.setPresence(to_jid.userhost(), "subscribe") |
371 | |
372 def delContact(self, to): | |
373 """Remove contact from roster list""" | |
374 to_jid=jid.JID(to) | |
13
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
375 self.roster.removeItem(to_jid.userhost()) |
0 | 376 self.bridge.contactDeleted(to) |
377 | |
378 | |
379 ## callbacks ## | |
380 | |
381 def serverDisco(self, disco): | |
382 """xep-0030 Discovery Protocol.""" | |
14 | 383 for feature in disco.features: |
384 debug ("Feature found: %s",feature) | |
385 self.server_features.append(feature) | |
386 for cat, type in disco.identities: | |
387 debug ("Identity found: [%s/%s] %s" % (cat, type, disco.identities[(cat,type)])) | |
0 | 388 |
389 ## Generic HMI ## | |
390 | |
391 def askConfirmation(self, id, type, data, cb): | |
392 """Add a confirmation callback""" | |
393 if self._waiting_conf.has_key(id): | |
394 error ("Attempt to register two callbacks for the same confirmation") | |
395 else: | |
396 self._waiting_conf[id] = cb | |
397 self.bridge.askConfirmation(type, id, data) | |
398 | |
399 | |
400 def confirmationAnswer(self, id, accepted, data): | |
401 """Called by frontends to answer confirmation requests""" | |
402 debug ("Received confirmation answer for id [%s]: %s", id, "accepted" if accepted else "refused") | |
403 if not self._waiting_conf.has_key(id): | |
404 error ("Received an unknown confirmation") | |
405 else: | |
406 cb = self._waiting_conf[id] | |
407 del self._waiting_conf[id] | |
408 cb(id, accepted, data) | |
409 | |
410 def registerProgressCB(self, id, CB): | |
411 """Register a callback called when progress is requested for id""" | |
412 self._progress_cb_map[id] = CB | |
413 | |
414 def removeProgressCB(self, id): | |
415 """Remove a progress callback""" | |
416 if not self._progress_cb_map.has_key(id): | |
417 error ("Trying to remove an unknow progress callback") | |
418 else: | |
419 del self._progress_cb_map[id] | |
420 | |
421 def getProgress(self, id): | |
422 """Return a dict with progress information | |
423 data['position'] : current possition | |
424 data['size'] : end_position | |
425 """ | |
426 data = {} | |
427 try: | |
428 self._progress_cb_map[id](data) | |
429 except KeyError: | |
430 pass | |
431 #debug("Requested progress for unknown id") | |
432 return data | |
433 | |
434 | |
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
|
435 application = service.Application('SàT') |
13
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
12
diff
changeset
|
436 service = SAT() |
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
|
437 service.setServiceParent(application) |