267 lines
10 KiB
Python
Executable file
267 lines
10 KiB
Python
Executable file
#!/usr/bin/python
|
|
#-*- coding: utf-8 -*-
|
|
|
|
"""
|
|
SAT: a jabber client
|
|
Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org)
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
"""
|
|
|
|
|
|
from bridge import Bridge
|
|
import dbus
|
|
import dbus.service
|
|
import dbus.mainloop.glib
|
|
import pdb
|
|
from logging import debug, info, error
|
|
|
|
const_INT_PREFIX = "org.goffi.SAT" #Interface prefix
|
|
const_COMM_SUFFIX = ".communication"
|
|
const_REQ_SUFFIX = ".request"
|
|
|
|
class DbusObject(dbus.service.Object):
|
|
|
|
def __init__(self, bus, path):
|
|
dbus.service.Object.__init__(self, bus, path)
|
|
debug("Init DbusObject...")
|
|
self.cb={}
|
|
|
|
def register(self, name, cb):
|
|
self.cb[name]=cb
|
|
|
|
### signals ###
|
|
|
|
@dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX,
|
|
signature='sa{ss}as')
|
|
def newContact(self, contact, attributes, groups):
|
|
debug("new contact signal (%s) sended", contact)
|
|
|
|
@dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX,
|
|
signature='ssss')
|
|
def newMessage(self, from_jid, msg, type='chat', to=''):
|
|
debug("new message signal (from:%s msg:%s type:%s to:%s) sended", from_jid, msg, type, to)
|
|
|
|
@dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX,
|
|
signature='ssssi')
|
|
def presenceUpdate(self, jid, type, show, status, priority):
|
|
debug("presence update signal (from:%s type: %s show:%s status:\"%s\" priority:%d) sended" , jid, type, show, status, priority)
|
|
|
|
@dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX,
|
|
signature='sss')
|
|
def paramUpdate(self, name, value, category):
|
|
debug("param update signal: %s=%s in category %s", name, value, category)
|
|
|
|
@dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX,
|
|
signature='s')
|
|
def contactDeleted(self, jid):
|
|
debug("contact deleted signal: %s", jid)
|
|
|
|
@dbus.service.signal(const_INT_PREFIX+const_REQ_SUFFIX,
|
|
signature='ssa{ss}')
|
|
def askConfirmation(self, type, id, data):
|
|
debug("asking for confirmation: id = [%s] type = %s data = %s", id, type, data)
|
|
|
|
@dbus.service.signal(const_INT_PREFIX+const_REQ_SUFFIX,
|
|
signature='ssa{ss}')
|
|
def actionResult(self, type, id, data):
|
|
debug("result of action: id = [%s] type = %s data = %s", id, type, data)
|
|
|
|
### methods ###
|
|
|
|
@dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
|
|
in_signature='sssi', out_signature='s')
|
|
def registerNewAccount(self, login, password, host, port=5222):
|
|
info ("New account registration asked")
|
|
return self.cb["registerNewAccount"](login, password, host, port)
|
|
|
|
@dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
|
|
in_signature='', out_signature='')
|
|
def connect(self):
|
|
info ("Connection asked")
|
|
return self.cb["connect"]()
|
|
|
|
@dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
|
|
in_signature='', out_signature='')
|
|
def disconnect(self):
|
|
info ("Disconnection asked")
|
|
return self.cb["disconnect"]()
|
|
|
|
@dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
|
|
in_signature='', out_signature='a(sa{ss}as)')
|
|
def getContacts(self):
|
|
debug("getContacts...")
|
|
return self.cb["getContacts"]()
|
|
|
|
@dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
|
|
in_signature='', out_signature='a(ssssi)')
|
|
def getPresenceStatus(self):
|
|
debug("getPresenceStatus...")
|
|
return self.cb["getPresenceStatus"]()
|
|
|
|
@dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
|
|
in_signature='ss', out_signature='')
|
|
def sendMessage(self, to, message):
|
|
debug("sendMessage...")
|
|
self.cb["sendMessage"](to, message)
|
|
|
|
@dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
|
|
in_signature='ssssi', out_signature='')
|
|
def setPresence(self, to="", type="", show="", status="", priority=0):
|
|
self.cb["setPresence"](to, type, show, status, priority)
|
|
|
|
@dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
|
|
in_signature='sss', out_signature='')
|
|
def setParam(self, name, value, category):
|
|
self.cb["setParam"](name, str(value), category)
|
|
|
|
@dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
|
|
in_signature='ss', out_signature='s')
|
|
def getParamA(self, name, category="default"):
|
|
return self.cb["getParamA"](name, category)
|
|
|
|
@dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
|
|
in_signature='', out_signature='s')
|
|
def getParams(self):
|
|
return self.cb["getParams"]()
|
|
|
|
@dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
|
|
in_signature='s', out_signature='s')
|
|
def getParamsForCategory(self, category):
|
|
return self.cb["getParamsForCategory"](category)
|
|
|
|
@dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
|
|
in_signature='', out_signature='as')
|
|
def getParamsCategories(self):
|
|
return self.cb["getParamsCategories"]()
|
|
|
|
@dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
|
|
in_signature='ssi', out_signature='a{i(ss)}')
|
|
def getHistory(self, from_jid, to_jid, size):
|
|
debug("History asked for %s", to_jid)
|
|
return self.cb["getHistory"](from_jid, to_jid, size)
|
|
|
|
@dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
|
|
in_signature='s', out_signature='')
|
|
def addContact(self, jid):
|
|
debug("Subscription asked for %s", jid)
|
|
return self.cb["addContact"](jid)
|
|
|
|
@dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
|
|
in_signature='s', out_signature='')
|
|
def delContact(self, jid):
|
|
debug("Unsubscription asked for %s", jid)
|
|
return self.cb["delContact"](jid)
|
|
|
|
@dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
|
|
in_signature='', out_signature='b')
|
|
def isConnected(self):
|
|
debug("Connection status requested")
|
|
return self.cb["isConnected"]()
|
|
|
|
@dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
|
|
in_signature='sa{ss}', out_signature='s')
|
|
def launchAction(self, type, data):
|
|
return self.cb["launchAction"](type, data)
|
|
|
|
@dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
|
|
in_signature='sba{ss}', out_signature='')
|
|
def confirmationAnswer(self, id, accepted, data):
|
|
debug("Answer for confirmation [%s]: %s", id, "Accepted" if accepted else "Refused")
|
|
return self.cb["confirmationAnswer"](id, accepted, data)
|
|
|
|
@dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
|
|
in_signature='s', out_signature='a{ss}')
|
|
def getProgress(self, id):
|
|
#debug("Progress asked for %s", id)
|
|
return self.cb["getProgress"](id)
|
|
|
|
def __attribute_string(self, in_sign):
|
|
i=0
|
|
idx=0
|
|
attr_string=""
|
|
while i<len(in_sign):
|
|
if in_sign[i] not in ['b','y','n','i','x','q','u','t','d','s','a']:
|
|
raise Exception #FIXME: create an exception here (unmanaged attribute type)
|
|
|
|
attr_string += ("" if idx==0 else ",") + ("arg_%i" % idx)
|
|
idx+=1
|
|
|
|
if in_sign[i] == 'a':
|
|
while (True):
|
|
i+=1
|
|
if i>=len(in_sign):
|
|
raise Exception #FIXME: create an exception here (the '}' is not presend)
|
|
if in_sign[i] == '}':
|
|
break
|
|
i+=1
|
|
return attr_string
|
|
|
|
|
|
|
|
def addMethod(self, name, int_suffix, in_sign, out_sign):
|
|
"""Dynamically add a method to Dbus Bridge"""
|
|
#FIXME: Better way ???
|
|
attributes = self.__attribute_string(in_sign)
|
|
|
|
code = compile ('def '+name+' (self,'+attributes+'): return self.cb["'+name+'"]('+attributes+')', '<DBus bridge>','exec')
|
|
exec (code)
|
|
method = locals()[name]
|
|
setattr(DbusObject, name, dbus.service.method(
|
|
const_INT_PREFIX+int_suffix, in_signature=in_sign, out_signature=out_sign)(method))
|
|
|
|
class DBusBridge(Bridge):
|
|
def __init__(self):
|
|
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
|
|
Bridge.__init__(self)
|
|
info ("Init DBus...")
|
|
self.session_bus = dbus.SessionBus()
|
|
self.dbus_name = dbus.service.BusName(const_INT_PREFIX, self.session_bus)
|
|
self.dbus_bridge = DbusObject(self.session_bus, '/org/goffi/SAT/bridge')
|
|
|
|
def newContact(self, contact, attributes, groups):
|
|
self.dbus_bridge.newContact(contact, attributes, groups)
|
|
|
|
def newMessage(self,from_jid,msg,type='chat', to=''):
|
|
debug("sending message...")
|
|
self.dbus_bridge.newMessage(from_jid, msg, type, to)
|
|
|
|
def presenceUpdate(self, jid, type, show, status, priority):
|
|
debug("updating presence for %s",jid)
|
|
self.dbus_bridge.presenceUpdate(jid, type, show, status, priority)
|
|
|
|
def paramUpdate(self, name, value, category):
|
|
debug("updating param [%s] %s ", category, name)
|
|
self.dbus_bridge.paramUpdate(name, value, category)
|
|
|
|
def contactDeleted(self, jid):
|
|
debug("sending contact deleted signal %s ", jid)
|
|
self.dbus_bridge.contactDeleted(jid)
|
|
|
|
def askConfirmation(self, type, id, data):
|
|
self.dbus_bridge.askConfirmation(type, id, data)
|
|
|
|
def actionResult(self, type, id, data):
|
|
self.dbus_bridge.actionResult(type, id, data)
|
|
|
|
def register(self, name, callback):
|
|
debug("registering DBus bridge method [%s]",name)
|
|
self.dbus_bridge.register(name, callback)
|
|
|
|
def addMethod(self, name, int_suffix, in_sign, out_sign, method):
|
|
"""Dynamically add a method to Dbus Bridge"""
|
|
print ("Adding method [%s] to DBus bridge" % name)
|
|
self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign)
|
|
self.register(name, method)
|
|
|