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