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 |
|
23 from bridge import Bridge |
|
24 import dbus |
|
25 import dbus.service |
|
26 import dbus.mainloop.glib |
|
27 import pdb |
|
28 from logging import debug, info, error |
|
29 |
|
30 class DbusObject(dbus.service.Object): |
|
31 |
|
32 def __init__(self, bus, path): |
|
33 dbus.service.Object.__init__(self, bus, path) |
|
34 debug("Init DbusObject...") |
|
35 self.cb={} |
|
36 |
|
37 def register(self, name, cb): |
|
38 self.cb[name]=cb |
|
39 |
|
40 ### signals ### |
|
41 |
|
42 @dbus.service.signal("org.goffi.SAT.communication", |
|
43 signature='sa{ss}as') |
|
44 def newContact(self, contact, attributes, groups): |
|
45 debug("new contact signal (%s) sended", contact) |
|
46 |
|
47 @dbus.service.signal("org.goffi.SAT.communication", |
|
48 signature='ssss') |
|
49 def newMessage(self, from_jid, msg, type='chat', to=''): |
|
50 debug("new message signal (from:%s msg:%s type:%s to:%s) sended", from_jid, msg, type, to) |
|
51 |
|
52 @dbus.service.signal("org.goffi.SAT.communication", |
|
53 signature='ssssi') |
|
54 def presenceUpdate(self, jid, type, show, status, priority): |
|
55 debug("presence update signal (from:%s type: %s show:%s status:\"%s\" priority:%d) sended" , jid, type, show, status, priority) |
|
56 |
|
57 @dbus.service.signal("org.goffi.SAT.communication", |
|
58 signature='sss') |
|
59 def paramUpdate(self, name, value, namespace): |
|
60 debug("param update signal: %s=%s in namespace %s", name, value, namespace) |
|
61 |
|
62 @dbus.service.signal("org.goffi.SAT.communication", |
|
63 signature='s') |
|
64 def contactDeleted(self, jid): |
|
65 debug("contact deleted signal: %s", jid) |
|
66 |
|
67 @dbus.service.signal("org.goffi.SAT.request", |
|
68 signature='ssa{ss}') |
|
69 def askConfirmation(self, type, id, data): |
|
70 debug("asking for confirmation: id = [%s] type = %s data = %s", id, type, data) |
|
71 |
|
72 |
|
73 |
|
74 ### methods ### |
|
75 |
|
76 @dbus.service.method("org.goffi.SAT.communication", |
|
77 in_signature='', out_signature='') |
|
78 def connect(self): |
|
79 info ("Connection asked") |
|
80 return self.cb["connect"]() |
|
81 |
|
82 @dbus.service.method("org.goffi.SAT.communication", |
1
|
83 in_signature='', out_signature='') |
|
84 def disconnect(self): |
|
85 info ("Disconnection asked") |
|
86 return self.cb["disconnect"]() |
|
87 |
|
88 @dbus.service.method("org.goffi.SAT.communication", |
0
|
89 in_signature='', out_signature='a(sa{ss}as)') |
|
90 def getContacts(self): |
|
91 debug("getContacts...") |
|
92 return self.cb["getContacts"]() |
|
93 |
|
94 @dbus.service.method("org.goffi.SAT.communication", |
|
95 in_signature='', out_signature='a(ssssi)') |
|
96 def getPresenceStatus(self): |
|
97 debug("getPresenceStatus...") |
|
98 return self.cb["getPresenceStatus"]() |
|
99 |
|
100 @dbus.service.method("org.goffi.SAT.communication", |
|
101 in_signature='ss', out_signature='') |
|
102 def sendMessage(self, to, message): |
|
103 debug("sendMessage...") |
|
104 self.cb["sendMessage"](to, message) |
|
105 |
|
106 @dbus.service.method("org.goffi.SAT.communication", |
|
107 in_signature='ss', out_signature='s') |
|
108 def sendFile(self, to, path): |
|
109 debug("sendFile...") |
|
110 return self.cb["sendFile"](to, path) |
|
111 |
|
112 @dbus.service.method("org.goffi.SAT.communication", |
|
113 in_signature='ssssi', out_signature='') |
|
114 def setPresence(self, to="", type="", show="", status="", priority=0): |
|
115 self.cb["setPresence"](to, type, show, status, priority) |
|
116 |
|
117 |
|
118 @dbus.service.method("org.goffi.SAT.communication", |
|
119 in_signature='sss', out_signature='') |
|
120 def setParam(self, name, value, namespace="default"): |
|
121 self.cb["setParam"](name, str(value), namespace) |
|
122 |
|
123 @dbus.service.method("org.goffi.SAT.communication", |
|
124 in_signature='ss', out_signature='(ss)') |
|
125 def getParam(self, name, namespace="default"): |
|
126 return self.cb["getParam"](name, namespace) |
|
127 |
|
128 @dbus.service.method("org.goffi.SAT.communication", |
|
129 in_signature='s', out_signature='a(sss)') |
|
130 def getParams(self, namespace): |
|
131 return self.cb["getParams"](namespace) |
|
132 |
|
133 @dbus.service.method("org.goffi.SAT.communication", |
|
134 in_signature='', out_signature='as') |
|
135 def getParamsCategories(self): |
|
136 return self.cb["getParamsCategories"]() |
|
137 |
|
138 @dbus.service.method("org.goffi.SAT.communication", |
|
139 in_signature='ssi', out_signature='a{i(ss)}') |
|
140 def getHistory(self, from_jid, to_jid, size): |
|
141 debug("History asked for %s", to_jid) |
|
142 return self.cb["getHistory"](from_jid, to_jid, size) |
|
143 |
|
144 @dbus.service.method("org.goffi.SAT.communication", |
|
145 in_signature='s', out_signature='') |
|
146 def addContact(self, jid): |
|
147 debug("Subscription asked for %s", jid) |
|
148 return self.cb["addContact"](jid) |
|
149 |
|
150 @dbus.service.method("org.goffi.SAT.communication", |
|
151 in_signature='s', out_signature='') |
|
152 def delContact(self, jid): |
|
153 debug("Unsubscription asked for %s", jid) |
|
154 return self.cb["delContact"](jid) |
|
155 |
|
156 @dbus.service.method("org.goffi.SAT.communication", |
|
157 in_signature='', out_signature='b') |
|
158 def isConnected(self): |
|
159 debug("Connection status requested") |
|
160 return self.cb["isConnected"]() |
|
161 |
|
162 @dbus.service.method("org.goffi.SAT.request", |
|
163 in_signature='sba{ss}', out_signature='') |
|
164 def confirmationAnswer(self, id, accepted, data): |
|
165 debug("Answer for confirmation [%s]: %s", id, "Accepted" if accepted else "Refused") |
|
166 return self.cb["confirmationAnswer"](id, accepted, data) |
|
167 |
|
168 @dbus.service.method("org.goffi.SAT.request", |
|
169 in_signature='s', out_signature='a{ss}') |
|
170 def getProgress(self, id): |
|
171 #debug("Progress asked for %s", id) |
|
172 return self.cb["getProgress"](id) |
|
173 |
|
174 class DBusBridge(Bridge): |
|
175 def __init__(self): |
|
176 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
|
177 Bridge.__init__(self) |
|
178 info ("Init DBus...") |
|
179 self.session_bus = dbus.SessionBus() |
|
180 self.dbus_name = dbus.service.BusName("org.goffi.SAT", self.session_bus) |
|
181 self.dbus_bridge = DbusObject(self.session_bus, '/org/goffi/SAT/bridge') |
|
182 |
|
183 def newContact(self, contact, attributes, groups): |
|
184 self.dbus_bridge.newContact(contact, attributes, groups) |
|
185 |
|
186 def newMessage(self,from_jid,msg,type='chat', to=''): |
|
187 debug("sending message...") |
|
188 self.dbus_bridge.newMessage(from_jid, msg, type, to) |
|
189 |
|
190 def presenceUpdate(self, jid, type, show, status, priority): |
|
191 debug("updating presence for %s",jid) |
|
192 self.dbus_bridge.presenceUpdate(jid, type, show, status, priority) |
|
193 |
|
194 def paramUpdate(self, name, value, namespace): |
|
195 debug("updating param [%s] %s ", namespace, name) |
|
196 self.dbus_bridge.paramUpdate(name, value, namespace) |
|
197 |
|
198 def contactDeleted(self, jid): |
|
199 debug("sending contact deleted signal %s ", jid) |
|
200 self.dbus_bridge.contactDeleted(jid) |
|
201 |
|
202 def askConfirmation(self, type, id, data): |
|
203 self.dbus_bridge.askConfirmation(type, id, data) |
|
204 |
|
205 def register(self, name, callback): |
|
206 debug("enregistrement de %s",name) |
|
207 self.dbus_bridge.register(name, callback) |
|
208 |