annotate sat.tac @ 14:a62d7d453f22

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