core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles

- added profile argument to askConfirmation, actionResult, actionResultExt, entityDataUpdated, confirmationAnswer, getProgress
    - core, frontends: fixed calls/signals according to new bridge API
    - user of proper profile namespace for progression indicators and dialogs
    - memory: getParam* now return bool when param type is bool
    - memory: added getStringParam* to return string instead of typed value
    - core, memory, storage, quick_frontend: getHistory now manage properly multi-profiles
    - plugins XEP-0047, XEP-0054, XEP-0065, XEP-0077, XEP-0096; multi-profiles proper handling
This commit is contained in:
Goffi 2012-11-10 16:38:16 +01:00
commit fa38394401
20 changed files with 570 additions and 400 deletions

View file

@ -75,8 +75,8 @@ class DBusBridgeFrontend(BridgeFrontend):
def callMenu(self, category, name, menu_type, profile_key): def callMenu(self, category, name, menu_type, profile_key):
return unicode(self.db_core_iface.callMenu(category, name, menu_type, profile_key)) return unicode(self.db_core_iface.callMenu(category, name, menu_type, profile_key))
def confirmationAnswer(self, id, accepted, data): def confirmationAnswer(self, id, accepted, data, profile):
return self.db_core_iface.confirmationAnswer(id, accepted, data) return self.db_core_iface.confirmationAnswer(id, accepted, data, profile)
def connect(self, profile_key="@DEFAULT@"): def connect(self, profile_key="@DEFAULT@"):
return self.db_core_iface.connect(profile_key) return self.db_core_iface.connect(profile_key)
@ -105,8 +105,8 @@ class DBusBridgeFrontend(BridgeFrontend):
def getEntityData(self, jid, keys, profile): def getEntityData(self, jid, keys, profile):
return self.db_core_iface.getEntityData(jid, keys, profile) return self.db_core_iface.getEntityData(jid, keys, profile)
def getHistory(self, from_jid, to_jid, limit, between=True, callback=None, errback=None): def getHistory(self, from_jid, to_jid, limit, between=True, profile="@NONE@", callback=None, errback=None):
return self.db_core_iface.getHistory(from_jid, to_jid, limit, between, reply_handler=callback, error_handler=lambda err:errback(err._dbus_error_name[len(const_ERROR_PREFIX)+1:])) return self.db_core_iface.getHistory(from_jid, to_jid, limit, between, profile, reply_handler=callback, error_handler=lambda err:errback(err._dbus_error_name[len(const_ERROR_PREFIX)+1:]))
def getLastResource(self, contact_jid, profile_key="@DEFAULT@"): def getLastResource(self, contact_jid, profile_key="@DEFAULT@"):
return unicode(self.db_core_iface.getLastResource(contact_jid, profile_key)) return unicode(self.db_core_iface.getLastResource(contact_jid, profile_key))
@ -141,8 +141,8 @@ class DBusBridgeFrontend(BridgeFrontend):
def getProfilesList(self, ): def getProfilesList(self, ):
return self.db_core_iface.getProfilesList() return self.db_core_iface.getProfilesList()
def getProgress(self, id): def getProgress(self, id, profile):
return self.db_core_iface.getProgress(id) return self.db_core_iface.getProgress(id, profile)
def getVersion(self, ): def getVersion(self, ):
return unicode(self.db_core_iface.getVersion()) return unicode(self.db_core_iface.getVersion())

View file

@ -53,7 +53,6 @@ import sys
import os import os
from os.path import abspath, basename, dirname from os.path import abspath, basename, dirname
from optparse import OptionParser from optparse import OptionParser
import pdb
from sat.tools.jid import JID from sat.tools.jid import JID
import gobject import gobject
from sat_frontends.bridge.DBus import DBusBridgeFrontend,BridgeExceptionNoService from sat_frontends.bridge.DBus import DBusBridgeFrontend,BridgeExceptionNoService
@ -79,7 +78,7 @@ class JP():
print(_(u"Can't connect to SàT backend, are you sure it's launched ?")) print(_(u"Can't connect to SàT backend, are you sure it's launched ?"))
import sys import sys
sys.exit(1) sys.exit(1)
self.transfer_id = None self.transfer_data = None
def check_options(self): def check_options(self):
"""Check command line options""" """Check command line options"""
@ -242,7 +241,7 @@ class JP():
for file in self.files: for file in self.files:
if not os.path.exists(file): if not os.path.exists(file):
error (_("File [%s] doesn't exist !") % file) error (_(u"File [%s] doesn't exist !") % file)
exit(1) exit(1)
if not self.options.bz2 and os.path.isdir(file): if not self.options.bz2 and os.path.isdir(file):
error (_("[%s] is a dir ! Please send files inside or use compression") % file) error (_("[%s] is a dir ! Please send files inside or use compression") % file)
@ -265,11 +264,11 @@ class JP():
bz2.close() bz2.close()
info(_("OK !")) info(_("OK !"))
path = abspath(tmpfile) path = abspath(tmpfile)
self.transfer_id = self.bridge.sendFile(full_dest_jid, path, {}, profile_key=self.profile) self.transfer_data = self.bridge.sendFile(full_dest_jid, path, {}, profile_key=self.profile)
else: else:
for file in self.files: for file in self.files:
path = abspath(file) path = abspath(file)
self.transfer_id = self.bridge.sendFile(full_dest_jid, path, {}, profile_key=self.profile) #FIXME: show progress only for last transfer_id self.transfer_data = self.bridge.sendFile(full_dest_jid, path, {}, profile_key=self.profile) #FIXME: show progress only for last transfer_id
def _getFullJid(self, param_jid): def _getFullJid(self, param_jid):
@ -283,8 +282,11 @@ class JP():
return param_jid return param_jid
def askConfirmation(self, type, id, data): def askConfirmation(self, type, confirm_id, data, profile):
"""CB used for file transfer, accept files depending on parameters""" """CB used for file transfer, accept files depending on parameters"""
if profile != self.profile:
debug("Ask confirmation ignored: not our profile")
return
answer_data={} answer_data={}
if type == "FILE_TRANSFER": if type == "FILE_TRANSFER":
if not self.options.wait_file: if not self.options.wait_file:
@ -295,11 +297,11 @@ class JP():
answer_data["dest_path"] = os.getcwd()+'/'+data['filename'] answer_data["dest_path"] = os.getcwd()+'/'+data['filename']
if self.options.force or not os.path.exists(answer_data["dest_path"]): if self.options.force or not os.path.exists(answer_data["dest_path"]):
self.bridge.confirmationAnswer(id, True, answer_data) self.bridge.confirmationAnswer(confirm_id, True, answer_data, profile)
info(_("Accepted file [%(filename)s] from %(sender)s") % {'filename':data['filename'], 'sender':data['from']}) info(_("Accepted file [%(filename)s] from %(sender)s") % {'filename':data['filename'], 'sender':data['from']})
self.transfer_id = id self.transfer_data = confirm_id
else: else:
self.bridge.confirmationAnswer(id, False, answer_data) self.bridge.confirmationAnswer(confirm_id, False, answer_data, profile)
warning(_("Refused file [%(filename)s] from %(sender)s: a file with the same name already exist") % {'filename':data['filename'], 'sender':data['from']}) warning(_("Refused file [%(filename)s] from %(sender)s: a file with the same name already exist") % {'filename':data['filename'], 'sender':data['from']})
@ -316,14 +318,14 @@ class JP():
fifopath = os.path.join(tmp_dir,"pipe_in") fifopath = os.path.join(tmp_dir,"pipe_in")
answer_data["dest_path"] = fifopath answer_data["dest_path"] = fifopath
os.mkfifo(fifopath) os.mkfifo(fifopath)
self.bridge.confirmationAnswer(id, True, answer_data) self.bridge.confirmationAnswer(confirm_id, True, answer_data, profile)
with open(fifopath, 'r') as f: with open(fifopath, 'r') as f:
shutil.copyfileobj(f, sys.stdout) shutil.copyfileobj(f, sys.stdout)
shutil.rmtree(tmp_dir) shutil.rmtree(tmp_dir)
self.loop.quit() self.loop.quit()
def actionResult(self, type, id, data): def actionResult(self, action_type, action_id, data, profile):
#FIXME #FIXME
info (_("FIXME: actionResult not implemented")) info (_("FIXME: actionResult not implemented"))
@ -332,8 +334,9 @@ class JP():
self.bridge.register("askConfirmation", self.askConfirmation) self.bridge.register("askConfirmation", self.askConfirmation)
def progressCB(self): def progressCB(self):
if self.transfer_id: if self.transfer_data:
data = self.bridge.getProgress(self.transfer_id) transfer_id = self.transfer_data
data = self.bridge.getProgress(transfer_id, self.profile)
if data: if data:
if not data['position']: if not data['position']:
data['position'] = '0' data['position'] = '0'

View file

@ -21,7 +21,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import urwid import urwid
from urwid_satext import sat_widgets from urwid_satext import sat_widgets
from sat.tools.jid import JID
class Progress(urwid.WidgetWrap): class Progress(urwid.WidgetWrap):
@ -38,47 +37,48 @@ class Progress(urwid.WidgetWrap):
main_wid = sat_widgets.FocusFrame(listbox, footer=buttons_wid) main_wid = sat_widgets.FocusFrame(listbox, footer=buttons_wid)
urwid.WidgetWrap.__init__(self, main_wid) urwid.WidgetWrap.__init__(self, main_wid)
def addProgress(self, id, message): def addProgress(self, progress_id, message):
profile = self.host.profile # TODO: manage multiple profiles
mess_wid = urwid.Text(message) mess_wid = urwid.Text(message)
progr_wid = urwid.ProgressBar('progress_normal', 'progress_complete') progr_wid = urwid.ProgressBar('progress_normal', 'progress_complete')
column = urwid.Columns([mess_wid, progr_wid]) column = urwid.Columns([mess_wid, progr_wid])
self.progress_dict[id] = {'full':column,'progress':progr_wid,'state':'init'} self.progress_dict[(progress_id, profile)] = {'full':column,'progress':progr_wid,'state':'init'}
self.progress_list.append(column) self.progress_list.append(column)
self.progressCB(self.host.loop, (id, message)) self.progressCB(self.host.loop, (progress_id, message, profile))
def progressCB(self, loop, data): def progressCB(self, loop, data):
id, message = data progress_id, message, profile = data
data = self.host.bridge.getProgress(id) data = self.host.bridge.getProgress(progress_id, profile)
pbar = self.progress_dict[id]['progress'] pbar = self.progress_dict[(progress_id, profile)]['progress'] #FIXME: must manage profiles
if data: if data:
if self.progress_dict[id]['state'] == 'init': if self.progress_dict[(progress_id, profile)]['state'] == 'init':
#first answer, we must construct the bar #first answer, we must construct the bar
self.progress_dict[id]['state'] = 'progress' self.progress_dict[(progress_id, profile)]['state'] = 'progress'
pbar.done = float(data['size']) pbar.done = float(data['size'])
pbar.set_completion(float(data['position'])) pbar.set_completion(float(data['position']))
self.updateNotBar() self.updateNotBar()
else: else:
if self.progress_dict[id]['state'] == 'progress': if self.progress_dict[(progress_id, profile)]['state'] == 'progress':
self.progress_dict[id]['state'] = 'done' self.progress_dict[(progress_id, profile)]['state'] = 'done'
pbar.set_completion(pbar.done) pbar.set_completion(pbar.done)
self.updateNotBar() self.updateNotBar()
return return
loop.set_alarm_in(1,self.progressCB, (id, message)) loop.set_alarm_in(1,self.progressCB, (progress_id, message, profile))
def __removeBar(self, id): def __removeBar(self, progress_id, profile):
wid = self.progress_dict[id]['full'] wid = self.progress_dict[(progress_id, profile)]['full']
self.progress_list.remove(wid) self.progress_list.remove(wid)
del(self.progress_dict[id]) del(self.progress_dict[(progress_id, profile)])
def __onClear(self, button): def __onClear(self, button):
to_remove = [] to_remove = []
for id in self.progress_dict: for progress_id, profile in self.progress_dict:
if self.progress_dict[id]['state'] == 'done': if self.progress_dict[(progress_id, profile)]['state'] == 'done':
to_remove.append(id) to_remove.append((progress_id, profile))
for id in to_remove: for progress_id, profile in to_remove:
self.__removeBar(id) self.__removeBar(progress_id, profile)
self.updateNotBar() self.updateNotBar()
def updateNotBar(self): def updateNotBar(self):
@ -87,8 +87,8 @@ class Progress(urwid.WidgetWrap):
return return
progress = 0 progress = 0
nb_bars = 0 nb_bars = 0
for id in self.progress_dict: for progress_id, profile in self.progress_dict:
pbar = self.progress_dict[id]['progress'] pbar = self.progress_dict[(progress_id, profile)]['progress']
progress += pbar.current/pbar.done*100 progress += pbar.current/pbar.done*100
nb_bars+=1 nb_bars+=1
av_progress = progress/float(nb_bars) av_progress = progress/float(nb_bars)

View file

@ -114,7 +114,7 @@ class QuickApp(object):
self.options.profile = self.options.profile.decode('utf-8') self.options.profile = self.options.profile.decode('utf-8')
return args return args
def _getParamError(self): def _getParamError(self, ignore):
error(_("Can't get profile parameter")) error(_("Can't get profile parameter"))
def plug_profile(self, profile_key='@DEFAULT@'): def plug_profile(self, profile_key='@DEFAULT@'):
@ -546,7 +546,7 @@ class QuickApp(object):
self.contact_list.setCache(jid, 'avatar', filename) self.contact_list.setCache(jid, 'avatar', filename)
self.contact_list.replace(jid) self.contact_list.replace(jid)
def askConfirmation(self, type, id, data): def askConfirmation(self, type, id, data, profile):
raise NotImplementedError raise NotImplementedError
def actionResult(self, type, id, data): def actionResult(self, type, id, data):

View file

@ -265,7 +265,7 @@ class Chat(wx.Frame, QuickChat):
else: else:
full_jid = self.target full_jid = self.target
id = self.host.bridge.sendFile(full_jid, filename, {}, self.host.profile) id = self.host.bridge.sendFile(full_jid, filename, {}, self.host.profile)
self.host.waitProgress(id, _("File Transfer"), _("Copying %s") % os.path.basename(filename)) self.host.waitProgress(id, _("File Transfer"), _("Copying %s") % os.path.basename(filename), self.host.profile)
def onStartTarot(self, e): def onStartTarot(self, e):
debug (_("Starting Tarot game")) debug (_("Starting Tarot game"))

View file

@ -223,11 +223,13 @@ class MainWindow(wx.Frame, QuickApp):
self.tools.Disable() self.tools.Disable()
return return
def askConfirmation(self, type, id, data): def askConfirmation(self, confirmation_type, confirmation_id, data, profile):
#TODO: refactor this in QuickApp #TODO: refactor this in QuickApp
if not self.check_profile(profile):
return
debug (_("Confirmation asked")) debug (_("Confirmation asked"))
answer_data={} answer_data={}
if type == "FILE_TRANSFER": if confirmation_type == "FILE_TRANSFER":
debug (_("File transfer confirmation asked")) debug (_("File transfer confirmation asked"))
dlg = wx.MessageDialog(self, _("The contact %(jid)s wants to send you the file %(filename)s\nDo you accept ?") % {'jid':data["from"], 'filename':data["filename"]}, dlg = wx.MessageDialog(self, _("The contact %(jid)s wants to send you the file %(filename)s\nDo you accept ?") % {'jid':data["from"], 'filename':data["filename"]},
_('File Request'), _('File Request'),
@ -238,16 +240,16 @@ class MainWindow(wx.Frame, QuickApp):
filename = wx.FileSelector(_("Where do you want to save the file ?"), flags = wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) filename = wx.FileSelector(_("Where do you want to save the file ?"), flags = wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
if filename: if filename:
answer_data["dest_path"] = filename answer_data["dest_path"] = filename
self.bridge.confirmationAnswer(id, True, answer_data) self.bridge.confirmationAnswer(confirmation_id, True, answer_data, profile)
self.waitProgress(id, _("File Transfer"), _("Copying %s") % os.path.basename(filename)) self.waitProgress(confirmation_id, _("File Transfer"), _("Copying %s") % os.path.basename(filename), profile)
else: else:
answer = wx.ID_NO answer = wx.ID_NO
if answer==wx.ID_NO: if answer==wx.ID_NO:
self.bridge.confirmationAnswer(id, False, answer_data) self.bridge.confirmationAnswer(confirmation_id, False, answer_data, profile)
dlg.Destroy() dlg.Destroy()
elif type == "YES/NO": elif confirmation_type == "YES/NO":
debug (_("Yes/No confirmation asked")) debug (_("Yes/No confirmation asked"))
dlg = wx.MessageDialog(self, data["message"], dlg = wx.MessageDialog(self, data["message"],
_('Confirmation'), _('Confirmation'),
@ -255,13 +257,15 @@ class MainWindow(wx.Frame, QuickApp):
) )
answer=dlg.ShowModal() answer=dlg.ShowModal()
if answer==wx.ID_YES: if answer==wx.ID_YES:
self.bridge.confirmationAnswer(id, True, {}) self.bridge.confirmationAnswer(confirmation_id, True, {}, profile)
if answer==wx.ID_NO: if answer==wx.ID_NO:
self.bridge.confirmationAnswer(id, False, {}) self.bridge.confirmationAnswer(confirmation_id, False, {}, profile)
dlg.Destroy() dlg.Destroy()
def actionResult(self, type, id, data): def actionResult(self, type, id, data, profile):
if not self.check_profile(profile):
return
debug (_("actionResult: type = [%(type)s] id = [%(id)s] data = [%(data)s]") % {'type':type, 'id':id, 'data':data}) debug (_("actionResult: type = [%(type)s] id = [%(id)s] data = [%(data)s]") % {'type':type, 'id':id, 'data':data})
if not id in self.current_action_ids: if not id in self.current_action_ids:
debug (_('unknown id, ignoring')) debug (_('unknown id, ignoring'))
@ -313,8 +317,8 @@ class MainWindow(wx.Frame, QuickApp):
def progressCB(self, id, title, message): def progressCB(self, progress_id, title, message, profile):
data = self.bridge.getProgress(id) data = self.bridge.getProgress(progress_id, profile)
if data: if data:
if not self.pbar: if not self.pbar:
#first answer, we must construct the bar #first answer, we must construct the bar
@ -327,11 +331,11 @@ class MainWindow(wx.Frame, QuickApp):
self.pbar.Update(self.pbar.finish_value) self.pbar.Update(self.pbar.finish_value)
return return
wx.CallLater(10, self.progressCB, id, title, message) wx.CallLater(10, self.progressCB, progress_id, title, message, profile)
def waitProgress (self, id, title, message): def waitProgress (self, progress_id, title, message, profile):
self.pbar = None self.pbar = None
wx.CallLater(10, self.progressCB, id, title, message) wx.CallLater(10, self.progressCB, progress_id, title, message, profile)

View file

@ -105,18 +105,18 @@ class DbusObject(dbus.service.Object):
pass pass
@dbus.service.signal(const_INT_PREFIX+const_CORE_SUFFIX, @dbus.service.signal(const_INT_PREFIX+const_CORE_SUFFIX,
signature='ssa{ss}') signature='ssa{ss}s')
def actionResult(self, answer_type, id, data): def actionResult(self, answer_type, id, data, profile):
pass pass
@dbus.service.signal(const_INT_PREFIX+const_CORE_SUFFIX, @dbus.service.signal(const_INT_PREFIX+const_CORE_SUFFIX,
signature='ssa{sa{ss}}') signature='ssa{sa{ss}}s')
def actionResultExt(self, answer_type, id, data): def actionResultExt(self, answer_type, id, data, profile):
pass pass
@dbus.service.signal(const_INT_PREFIX+const_CORE_SUFFIX, @dbus.service.signal(const_INT_PREFIX+const_CORE_SUFFIX,
signature='ssa{ss}') signature='ssa{ss}s')
def askConfirmation(self, conf_type, id, data): def askConfirmation(self, conf_type, id, data, profile):
pass pass
@dbus.service.signal(const_INT_PREFIX+const_CORE_SUFFIX, @dbus.service.signal(const_INT_PREFIX+const_CORE_SUFFIX,
@ -208,10 +208,10 @@ class DbusObject(dbus.service.Object):
return self._callback("callMenu", unicode(category), unicode(name), unicode(menu_type), unicode(profile_key)) return self._callback("callMenu", unicode(category), unicode(name), unicode(menu_type), unicode(profile_key))
@dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX, @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX,
in_signature='sba{ss}', out_signature='', in_signature='sba{ss}s', out_signature='',
async_callbacks=None) async_callbacks=None)
def confirmationAnswer(self, id, accepted, data): def confirmationAnswer(self, id, accepted, data, profile):
return self._callback("confirmationAnswer", unicode(id), accepted, data) return self._callback("confirmationAnswer", unicode(id), accepted, data, unicode(profile))
@dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX, @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX,
in_signature='s', out_signature='', in_signature='s', out_signature='',
@ -268,10 +268,10 @@ class DbusObject(dbus.service.Object):
return self._callback("getEntityData", unicode(jid), keys, unicode(profile)) return self._callback("getEntityData", unicode(jid), keys, unicode(profile))
@dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX, @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX,
in_signature='ssib', out_signature='a(dssss)', in_signature='ssibs', out_signature='a(dssss)',
async_callbacks=('callback', 'errback')) async_callbacks=('callback', 'errback'))
def getHistory(self, from_jid, to_jid, limit, between=True, callback=None, errback=None): def getHistory(self, from_jid, to_jid, limit, between=True, profile="@NONE@", callback=None, errback=None):
return self._callback("getHistory", unicode(from_jid), unicode(to_jid), limit, between, callback=callback, errback=errback) return self._callback("getHistory", unicode(from_jid), unicode(to_jid), limit, between, unicode(profile), callback=callback, errback=errback)
@dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX, @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX,
in_signature='ss', out_signature='s', in_signature='ss', out_signature='s',
@ -340,10 +340,10 @@ class DbusObject(dbus.service.Object):
return self._callback("getProfilesList", ) return self._callback("getProfilesList", )
@dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX, @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX,
in_signature='s', out_signature='a{ss}', in_signature='ss', out_signature='a{ss}',
async_callbacks=None) async_callbacks=None)
def getProgress(self, id): def getProgress(self, id, profile):
return self._callback("getProgress", unicode(id)) return self._callback("getProgress", unicode(id), unicode(profile))
@dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX, @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX,
in_signature='', out_signature='s', in_signature='', out_signature='s',
@ -503,14 +503,14 @@ class DBusBridge(Bridge):
self.dbus_name = dbus.service.BusName(const_INT_PREFIX, self.session_bus) self.dbus_name = dbus.service.BusName(const_INT_PREFIX, self.session_bus)
self.dbus_bridge = DbusObject(self.session_bus, const_OBJ_PATH) self.dbus_bridge = DbusObject(self.session_bus, const_OBJ_PATH)
def actionResult(self, answer_type, id, data): def actionResult(self, answer_type, id, data, profile):
self.dbus_bridge.actionResult(answer_type, id, data) self.dbus_bridge.actionResult(answer_type, id, data, profile)
def actionResultExt(self, answer_type, id, data): def actionResultExt(self, answer_type, id, data, profile):
self.dbus_bridge.actionResultExt(answer_type, id, data) self.dbus_bridge.actionResultExt(answer_type, id, data, profile)
def askConfirmation(self, conf_type, id, data): def askConfirmation(self, conf_type, id, data, profile):
self.dbus_bridge.askConfirmation(conf_type, id, data) self.dbus_bridge.askConfirmation(conf_type, id, data, profile)
def connected(self, profile): def connected(self, profile):
self.dbus_bridge.connected(profile) self.dbus_bridge.connected(profile)

View file

@ -106,18 +106,19 @@ doc_param_1=%(doc_profile)s
[askConfirmation] [askConfirmation]
type=signal type=signal
category=core category=core
sig_in=ssa{ss} sig_in=ssa{ss}s
doc=A confirmation is needed for an action doc=A confirmation is needed for an action
doc_param_0=conf_type: Type of the confirmation, can be: doc_param_0=conf_type: Type of the confirmation, can be:
- YES/NO: A question which need a yes or no answer - YES/NO: A question which need a yes or no answer
- FILE_TRANSFER: A confirmation is needed before transfering a file - FILE_TRANSFER: A confirmation is needed before transfering a file
doc_param_1=id: Id of the confirmation query doc_param_1=id: Id of the confirmation query
doc_param_2=data: conf_type dependent data doc_param_2=data: conf_type dependent data
doc_param_3=%(doc_profile)s
[actionResult] [actionResult]
type=signal type=signal
category=core category=core
sig_in=ssa{ss} sig_in=ssa{ss}s
doc=Requested result of an action doc=Requested result of an action
doc_param_0=answer_type: Type of the answer, can be: doc_param_0=answer_type: Type of the answer, can be:
- SUPPRESS: The action is managed, the id MUST be removed from queue - SUPPRESS: The action is managed, the id MUST be removed from queue
@ -126,16 +127,18 @@ doc_param_0=answer_type: Type of the answer, can be:
- RESULT: General result, interpretation depend of the action - RESULT: General result, interpretation depend of the action
doc_param_1=id: Id of the action doc_param_1=id: Id of the action
doc_param_2=data: answer_type specific data doc_param_2=data: answer_type specific data
doc_param_3=%(doc_profile)s
[actionResultExt] [actionResultExt]
type=signal type=signal
category=core category=core
sig_in=ssa{sa{ss}} sig_in=ssa{sa{ss}}s
doc=Requested result of an action (Extended) doc=Requested result of an action (Extended)
doc_param_0=answer_type: Same as for [actionResult] but with the following additional one: doc_param_0=answer_type: Same as for [actionResult] but with the following additional one:
- DICT_DICT: As RESULT, but returned as a dictionary of dictionary - DICT_DICT: As RESULT, but returned as a dictionary of dictionary
doc_param_1=id: Id of the action doc_param_1=id: Id of the action
doc_param_2=data: answer_type specific data doc_param_2=data: answer_type specific data
doc_param_3=%(doc_profile)s
[entityDataUpdated] [entityDataUpdated]
type=signal type=signal
@ -462,14 +465,16 @@ doc_return=list of categories
async= async=
type=method type=method
category=core category=core
sig_in=ssib sig_in=ssibs
sig_out=a(dssss) sig_out=a(dssss)
param_3_default=True param_3_default=True
param_4_default="@NONE@"
doc=Get history of a communication between two entities doc=Get history of a communication between two entities
doc_param_0=from_jid: source JID (bare jid for catch all, full jid else) doc_param_0=from_jid: source JID (bare jid for catch all, full jid else)
doc_param_1=to_jid: dest JID (bare jid for catch all, full jid else) doc_param_1=to_jid: dest JID (bare jid for catch all, full jid else)
doc_param_2=limit: max number of history elements to get (0 for the whole history) doc_param_2=limit: max number of history elements to get (0 for the whole history)
doc_param_3=between: True if we want history between the two jids (in both direction), False if we only want messages from from_jid to to_jid doc_param_3=between: True if we want history between the two jids (in both direction), False if we only want messages from from_jid to to_jid
doc_param_4=%(doc_profile)s
doc_return=Ordered list (by timestamp) of tuples (timestamp, full from_jid, full to_jid, message, type) doc_return=Ordered list (by timestamp) of tuples (timestamp, full from_jid, full to_jid, message, type)
[addContact] [addContact]
@ -519,20 +524,22 @@ doc_param_2=%(doc_profile_key)s
[confirmationAnswer] [confirmationAnswer]
type=method type=method
category=core category=core
sig_in=sba{ss} sig_in=sba{ss}s
sig_out= sig_out=
doc=Give answer to a confirmation request doc=Give answer to a confirmation request
doc_param_0=id: id of the confirmation request doc_param_0=id: id of the confirmation request
doc_param_1=accepted: True if the action is confirmed doc_param_1=accepted: True if the action is confirmed
doc_param_2=data: action specific data doc_param_2=data: action specific data
doc_param_3=%(doc_profile)s
[getProgress] [getProgress]
type=method type=method
category=core category=core
sig_in=s sig_in=ss
sig_out=a{ss} sig_out=a{ss}
doc=Get progress information for an action doc=Get progress information for an action
doc_param_0=id: id of the progression status doc_param_0=id: id of the progression status
doc_param_1=%(doc_profile)s
doc_return=dict with progress information: doc_return=dict with progress information:
- position: current position - position: current position
- size: end position - size: end position

View file

@ -33,3 +33,6 @@ class UnknownEntityError(Exception):
class UnknownGroupError(Exception): class UnknownGroupError(Exception):
pass pass
class NotFound(Exception):
pass

View file

@ -44,7 +44,7 @@ import sys
import os.path import os.path
from sat.core import xmpp from sat.core import xmpp
from sat.core.exceptions import ProfileUnknownError, UnknownEntityError from sat.core.exceptions import ProfileUnknownError, UnknownEntityError, ProfileNotInCacheError
from sat.memory.memory import Memory from sat.memory.memory import Memory
from sat.tools.xml_tools import tupleList2dataForm from sat.tools.xml_tools import tupleList2dataForm
from sat.tools.misc import TriggerManager from sat.tools.misc import TriggerManager
@ -98,8 +98,6 @@ class SAT(service.Service):
def __init__(self): def __init__(self):
#TODO: standardize callback system #TODO: standardize callback system
self.__waiting_conf = {} #callback called when a confirmation is received
self.__progress_cb_map = {} #callback called when a progress is requested (key = progress id)
self.__general_cb_map = {} #callback called for general reasons (key = name) self.__general_cb_map = {} #callback called for general reasons (key = name)
self.__private_data = {} #used for internal callbacks (key = id) self.__private_data = {} #used for internal callbacks (key = id)
self.profiles = {} self.profiles = {}
@ -134,8 +132,8 @@ class SAT(service.Service):
self.bridge.register("sendMessage", self.sendMessage) self.bridge.register("sendMessage", self.sendMessage)
self.bridge.register("getConfig", self.memory.getConfig) self.bridge.register("getConfig", self.memory.getConfig)
self.bridge.register("setParam", self.setParam) self.bridge.register("setParam", self.setParam)
self.bridge.register("getParamA", self.memory.getParamA) self.bridge.register("getParamA", self.memory.getStringParamA)
self.bridge.register("asyncGetParamA", self.memory.asyncGetParamA) self.bridge.register("asyncGetParamA", self.memory.asyncGetStringParamA)
self.bridge.register("getParamsUI", self.memory.getParamsUI) self.bridge.register("getParamsUI", self.memory.getParamsUI)
self.bridge.register("getParams", self.memory.getParams) self.bridge.register("getParams", self.memory.getParams)
self.bridge.register("getParamsForCategory", self.memory.getParamsForCategory) self.bridge.register("getParamsForCategory", self.memory.getParamsForCategory)
@ -336,11 +334,13 @@ class SAT(service.Service):
return None return None
return self.profiles[profile] return self.profiles[profile]
def registerNewAccount(self, login, password, email, server, port = 5222, id = None): def registerNewAccount(self, login, password, email, server, port = 5222, id = None, profile_key = '@DEFAULT@'):
"""Connect to a server and create a new account using in-band registration""" """Connect to a server and create a new account using in-band registration"""
profile = self.memory.getProfileName(profile_key)
assert(profile)
next_id = id or sat_next_id() #the id is used to send server's answer next_id = id or sat_next_id() #the id is used to send server's answer
serverRegistrer = xmlstream.XmlStreamFactory(xmpp.RegisteringAuthenticator(self, server, login, password, email, next_id)) serverRegistrer = xmlstream.XmlStreamFactory(xmpp.RegisteringAuthenticator(self, server, login, password, email, next_id, profile))
connector = reactor.connectTCP(server, port, serverRegistrer) connector = reactor.connectTCP(server, port, serverRegistrer)
serverRegistrer.clientConnectionLost = lambda conn, reason: connector.disconnect() serverRegistrer.clientConnectionLost = lambda conn, reason: connector.disconnect()
@ -354,7 +354,7 @@ class SAT(service.Service):
if not user or not password or not server: if not user or not password or not server:
info (_('No user or server given')) info (_('No user or server given'))
#TODO: a proper error message must be sent to frontend #TODO: a proper error message must be sent to frontend
self.actionResult(id, "ERROR", {'message':_("No user, password or server given, can't register new account.")}) self.actionResult(id, "ERROR", {'message':_("No user, password or server given, can't register new account.")}, profile)
return return
confirm_id = sat_next_id() confirm_id = sat_next_id()
@ -362,12 +362,12 @@ class SAT(service.Service):
self.askConfirmation(confirm_id, "YES/NO", self.askConfirmation(confirm_id, "YES/NO",
{"message":_("Are you sure to register new account [%(user)s] to server %(server)s ?") % {'user':user, 'server':server, 'profile':profile}}, {"message":_("Are you sure to register new account [%(user)s] to server %(server)s ?") % {'user':user, 'server':server, 'profile':profile}},
self.regisConfirmCB) self.regisConfirmCB, profile)
print ("===============+++++++++++ REGISTER NEW ACCOUNT++++++++++++++============") print ("===============+++++++++++ REGISTER NEW ACCOUNT++++++++++++++============")
print "id=",id print "id=",id
print "data=",data print "data=",data
def regisConfirmCB(self, id, accepted, data): def regisConfirmCB(self, id, accepted, data, profile):
print _("register Confirmation CB ! (%s)") % str(accepted) print _("register Confirmation CB ! (%s)") % str(accepted)
action_id,profile = self.__private_data[id] action_id,profile = self.__private_data[id]
del self.__private_data[id] del self.__private_data[id]
@ -377,7 +377,7 @@ class SAT(service.Service):
server = self.memory.getParamA("Server", "Connection", profile_key=profile) server = self.memory.getParamA("Server", "Connection", profile_key=profile)
self.registerNewAccount(user, password, None, server, id=action_id) self.registerNewAccount(user, password, None, server, id=action_id)
else: else:
self.actionResult(action_id, "SUPPRESS", {}) self.actionResult(action_id, "SUPPRESS", {}, profile)
def submitForm(self, action, target, fields, profile_key): def submitForm(self, action, target, fields, profile_key):
"""submit a form """submit a form
@ -601,73 +601,88 @@ class SAT(service.Service):
## Generic HMI ## ## Generic HMI ##
def actionResult(self, id, type, data): def actionResult(self, action_id, action_type, data, profile):
"""Send the result of an action """Send the result of an action
@param id: same id used with action @param action_id: same action_id used with action
@param type: result type ("PARAM", "SUCCESS", "ERROR", "XMLUI") @param action_type: result action_type ("PARAM", "SUCCESS", "ERROR", "XMLUI")
@param data: dictionary @param data: dictionary
""" """
self.bridge.actionResult(type, id, data) self.bridge.actionResult(action_type, action_id, data, profile)
def actionResultExt(self, id, type, data): def actionResultExt(self, action_id, action_type, data, profile):
"""Send the result of an action, extended version """Send the result of an action, extended version
@param id: same id used with action @param action_id: same action_id used with action
@param type: result type /!\ only "DICT_DICT" for this method @param action_type: result action_type /!\ only "DICT_DICT" for this method
@param data: dictionary of dictionaries @param data: dictionary of dictionaries
""" """
if type != "DICT_DICT": if action_type != "DICT_DICT":
error(_("type for actionResultExt must be DICT_DICT, fixing it")) error(_("action_type for actionResultExt must be DICT_DICT, fixing it"))
type = "DICT_DICT" action_type = "DICT_DICT"
self.bridge.actionResultExt(type, id, data) self.bridge.actionResultExt(action_type, action_id, data, profile)
def askConfirmation(self, id, type, data, cb): def askConfirmation(self, conf_id, conf_type, data, cb, profile):
"""Add a confirmation callback """Add a confirmation callback
@param id: id used to get answer @param conf_id: conf_id used to get answer
@param type: confirmation type ("YES/NO", "FILE_TRANSFER") @param conf_type: confirmation conf_type ("YES/NO", "FILE_TRANSFER")
@param data: data (depend of confirmation type) @param data: data (depend of confirmation conf_type)
@param cb: callback called with the answer @param cb: callback called with the answer
""" """
if self.__waiting_conf.has_key(id): client = self.getClient(profile)
if not client:
raise ProfileUnknownError(_("Asking confirmation a non-existant profile"))
if client._waiting_conf.has_key(conf_id):
error (_("Attempt to register two callbacks for the same confirmation")) error (_("Attempt to register two callbacks for the same confirmation"))
else: else:
self.__waiting_conf[id] = cb client._waiting_conf[conf_id] = cb
self.bridge.askConfirmation(type, id, data) self.bridge.askConfirmation(conf_type, conf_id, data, profile)
def confirmationAnswer(self, id, accepted, data): def confirmationAnswer(self, conf_id, accepted, data, profile):
"""Called by frontends to answer confirmation requests""" """Called by frontends to answer confirmation requests"""
debug (_("Received confirmation answer for id [%(id)s]: %(success)s") % {'id': id, 'success':_("accepted") if accepted else _("refused")}) client = self.getClient(profile)
if not self.__waiting_conf.has_key(id): if not client:
raise ProfileUnknownError(_("Confirmation answer from a non-existant profile"))
debug (_("Received confirmation answer for conf_id [%(conf_id)s]: %(success)s") % {'conf_id': conf_id, 'success':_("accepted") if accepted else _("refused")})
if not client._waiting_conf.has_key(conf_id):
error (_("Received an unknown confirmation")) error (_("Received an unknown confirmation"))
else: else:
cb = self.__waiting_conf[id] cb = client._waiting_conf[conf_id]
del self.__waiting_conf[id] del client._waiting_conf[conf_id]
cb(id, accepted, data) cb(conf_id, accepted, data, profile)
def registerProgressCB(self, id, CB): def registerProgressCB(self, progress_id, CB, profile):
"""Register a callback called when progress is requested for id""" """Register a callback called when progress is requested for id"""
self.__progress_cb_map[id] = CB client = self.getClient(profile)
if not client:
raise ProfileUnknownError
client._progress_cb_map[progress_id] = CB
def removeProgressCB(self, id): def removeProgressCB(self, progress_id, profile):
"""Remove a progress callback""" """Remove a progress callback"""
if not self.__progress_cb_map.has_key(id): client = self.getClient(profile)
if not client:
raise ProfileUnknownError
if not client._progress_cb_map.has_key(progress_id):
error (_("Trying to remove an unknow progress callback")) error (_("Trying to remove an unknow progress callback"))
else: else:
del self.__progress_cb_map[id] del client._progress_cb_map[progress_id]
def getProgress(self, id): def getProgress(self, progress_id, profile):
"""Return a dict with progress information """Return a dict with progress information
data['position'] : current possition data['position'] : current possition
data['size'] : end_position data['size'] : end_position
""" """
client = self.getClient(profile)
if not profile:
raise ProfileNotInCacheError
data = {} data = {}
try: try:
self.__progress_cb_map[id](id, data) client._progress_cb_map[progress_id](progress_id, data, profile)
except KeyError: except KeyError:
pass pass
#debug("Requested progress for unknown id") #debug("Requested progress for unknown progress_id")
return data return data
def registerGeneralCB(self, name, CB): def registerGeneralCB(self, name, CB):

View file

@ -37,6 +37,9 @@ class SatXMPPClient(client.XMPPClient):
self.host_app = host_app self.host_app = host_app
self.client_initialized = defer.Deferred() self.client_initialized = defer.Deferred()
self.conn_deferred = defer.Deferred() self.conn_deferred = defer.Deferred()
self._waiting_conf = {} #callback called when a confirmation is received
self._progress_cb_map = {} #callback called when a progress is requested (key = progress id)
def getConnectionDeferred(self): def getConnectionDeferred(self):
"""Return a deferred which fire when the client is connected""" """Return a deferred which fire when the client is connected"""
@ -360,7 +363,7 @@ class SatFallbackHandler(generic.FallbackHandler):
class RegisteringAuthenticator(xmlstream.ConnectAuthenticator): class RegisteringAuthenticator(xmlstream.ConnectAuthenticator):
def __init__(self, host, jabber_host, user_login, user_pass, email, answer_id): def __init__(self, host, jabber_host, user_login, user_pass, email, answer_id, profile):
xmlstream.ConnectAuthenticator.__init__(self, jabber_host) xmlstream.ConnectAuthenticator.__init__(self, jabber_host)
self.host = host self.host = host
self.jabber_host = jabber_host self.jabber_host = jabber_host
@ -368,6 +371,7 @@ class RegisteringAuthenticator(xmlstream.ConnectAuthenticator):
self.user_pass = user_pass self.user_pass = user_pass
self.user_email = email self.user_email = email
self.answer_id = answer_id self.answer_id = answer_id
self.profile = profile
print _("Registration asked for"),user_login, user_pass, jabber_host print _("Registration asked for"),user_login, user_pass, jabber_host
def connectionMade(self): def connectionMade(self):
@ -392,7 +396,7 @@ class RegisteringAuthenticator(xmlstream.ConnectAuthenticator):
debug (_("registration answer: %s") % answer.toXml()) debug (_("registration answer: %s") % answer.toXml())
answer_type = "SUCCESS" answer_type = "SUCCESS"
answer_data={"message":_("Registration successfull")} answer_data={"message":_("Registration successfull")}
self.host.bridge.actionResult(answer_type, self.answer_id, answer_data) self.host.bridge.actionResult(answer_type, self.answer_id, answer_data, self.profile)
self.xmlstream.sendFooter() self.xmlstream.sendFooter()
def registrationFailure(self, failure): def registrationFailure(self, failure):
@ -405,7 +409,7 @@ class RegisteringAuthenticator(xmlstream.ConnectAuthenticator):
else: else:
answer_data['reason'] = 'unknown' answer_data['reason'] = 'unknown'
answer_data={"message":_("Registration failed (%s)") % str(failure.value.condition)} answer_data={"message":_("Registration failed (%s)") % str(failure.value.condition)}
self.host.bridge.actionResult(answer_type, self.answer_id, answer_data) self.host.bridge.actionResult(answer_type, self.answer_id, answer_data, self.profile)
self.xmlstream.sendFooter() self.xmlstream.sendFooter()
class SatVersionHandler(generic.VersionHandler): class SatVersionHandler(generic.VersionHandler):

View file

@ -227,6 +227,28 @@ class Params():
d.addCallback(self.__default_ok, name, category) d.addCallback(self.__default_ok, name, category)
d.addErrback(errback or self.__default_ko, name, category) d.addErrback(errback or self.__default_ko, name, category)
def __getAttr(self, node, attr, value):
""" get attribute value
@param node: XML param node
@param attr: name of the attribute to get (e.g.: 'value' or 'type')
@param value: user defined value"""
if attr == 'value':
value_to_use = value if value!=None else node.getAttribute(attr) #we use value (user defined) if it exist, else we use node's default value
if node.getAttribute('type') == 'bool':
return value_to_use.lower() not in ('false','0')
return value_to_use
return node.getAttribute(attr)
def __type_to_string(self, result):
""" convert result to string, according to its type """
if isinstance(result,bool):
return "true" if result else "false"
return result
def getStringParamA(self, name, category, attr="value", profile_key="@DEFAULT@"):
""" Same as getParamA but for bridge: convert non string value to string """
return self.__type_to_string(self.getParamA(name, category, attr, profile_key))
def getParamA(self, name, category, attr="value", profile_key="@DEFAULT@"): def getParamA(self, name, category, attr="value", profile_key="@DEFAULT@"):
"""Helper method to get a specific attribute """Helper method to get a specific attribute
@param name: name of the parameter @param name: name of the parameter
@ -235,31 +257,35 @@ class Params():
@param profile: owner of the param (@ALL@ for everyone) @param profile: owner of the param (@ALL@ for everyone)
@return: attribute""" @return: attribute"""
#FIXME: looks really dirty and buggy, need to be reviewed/refactored
node = self.__getParamNode(name, category) node = self.__getParamNode(name, category)
if not node: if not node:
error(_("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name':name, 'category':category}) error(_("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name':name, 'category':category})
return "" raise exceptions.NotFound
if node[0] == 'general': if node[0] == 'general':
value = self.__getParam(None, category, name, 'general') value = self.__getParam(None, category, name, 'general')
return value if value!=None else node[1].getAttribute(attr) return self.__getAttr(node[1], attr, value)
assert(node[0] == 'individual') assert(node[0] == 'individual')
profile = self.getProfileName(profile_key) profile = self.getProfileName(profile_key)
if not profile: if not profile:
error(_('Requesting a param for an non-existant profile')) error(_('Requesting a param for an non-existant profile'))
return "" raise exceptions.ProfileUnknownError
if profile not in self.params: if profile not in self.params:
error(_('Requesting synchronous param for not connected profile')) error(_('Requesting synchronous param for not connected profile'))
return "" raise exceptions.ConnectedProfileError
if attr == "value": if attr == "value":
value = self.__getParam(profile, category, name) value = self.__getParam(profile, category, name)
return value if value!=None else node[1].getAttribute(attr) return self.__getAttr(node[1], attr, value)
else:
return node[1].getAttribute(attr) def asyncGetStringParamA(self, name, category, attr="value", profile_key="@DEFAULT@"):
d = self.asyncGetParamA(name, category, attr, profile_key)
d.addCallback(self.__type_to_string)
return d
def asyncGetParamA(self, name, category, attr="value", profile_key="@DEFAULT@"): def asyncGetParamA(self, name, category, attr="value", profile_key="@DEFAULT@"):
"""Helper method to get a specific attribute """Helper method to get a specific attribute
@ -274,7 +300,7 @@ class Params():
if node[0] == 'general': if node[0] == 'general':
value = self.__getParam(None, category, name, 'general') value = self.__getParam(None, category, name, 'general')
return defer.succeed(value if value!=None else node[1].getAttribute(attr)) return defer.succeed(self.__getAttr(node[1], attr, value))
assert(node[0] == 'individual') assert(node[0] == 'individual')
@ -285,21 +311,20 @@ class Params():
if attr != "value": if attr != "value":
return defer.succeed(node[1].getAttribute(attr)) return defer.succeed(node[1].getAttribute(attr))
default = node[1].getAttribute(attr)
try: try:
value = self.__getParam(profile, category, name) value = self.__getParam(profile, category, name)
return defer.succeed(value if value!=None else default) return defer.succeed(self.__getAttr(node[1], attr, value))
except exceptions.ProfileNotInCacheError: except exceptions.ProfileNotInCacheError:
#We have to ask data to the storage manager #We have to ask data to the storage manager
d = self.storage.getIndParam(category, name, profile) d = self.storage.getIndParam(category, name, profile)
return d.addCallback(lambda value: value if value!=None else default) return d.addCallback(lambda value: self.__getAttr(node[1], attr, value))
def __getParam(self, profile, category, name, _type='individual', cache=None): def __getParam(self, profile, category, name, _type='individual', cache=None):
"""Return the param, or None if it doesn't exist """Return the param, or None if it doesn't exist
@param profile: the profile name (not profile key, i.e. name and not something like @DEFAULT@) @param profile: the profile name (not profile key, i.e. name and not something like @DEFAULT@)
@param category: param category @param category: param category
@param name: param name @param name: param name
@param type: "general" or "individual" @param _type: "general" or "individual"
@param cache: temporary cache, to use when profile is not logged @param cache: temporary cache, to use when profile is not logged
@return: param value or None if it doesn't exist @return: param value or None if it doesn't exist
""" """
@ -614,8 +639,9 @@ class Memory:
assert(profile!="@NONE@") assert(profile!="@NONE@")
return self.storage.addToHistory(from_jid, to_jid, message, _type, timestamp, profile) return self.storage.addToHistory(from_jid, to_jid, message, _type, timestamp, profile)
def getHistory(self, from_jid, to_jid, limit=0, between=True): def getHistory(self, from_jid, to_jid, limit=0, between=True, profile="@NONE@"):
return self.storage.getHistory(jid.JID(from_jid), jid.JID(to_jid), limit, between) assert(profile != "@NONE@")
return self.storage.getHistory(jid.JID(from_jid), jid.JID(to_jid), limit, between, profile)
def addServerFeature(self, feature, profile): def addServerFeature(self, feature, profile):
"""Add a feature discovered from server """Add a feature discovered from server
@ -788,11 +814,14 @@ class Memory:
return self.subscriptions[profile] return self.subscriptions[profile]
def getStringParamA(self, name, category, attr="value", profile_key='@DEFAULT@'):
return self.params.getStringParamA(name, category, attr, profile_key)
def getParamA(self, name, category, attr="value", profile_key='@DEFAULT@'): def getParamA(self, name, category, attr="value", profile_key='@DEFAULT@'):
return self.params.getParamA(name, category, attr, profile_key) return self.params.getParamA(name, category, attr, profile_key)
def asyncGetParamA(self, name, category, attr="value", profile_key='@DEFAULT@'): def asyncGetStringParamA(self, name, category, attr="value", profile_key='@DEFAULT@'):
return self.params.asyncGetParamA(name, category, attr, profile_key) return self.params.asyncGetStringParamA(name, category, attr, profile_key)
def getParamsUI(self, profile_key): def getParamsUI(self, profile_key):
return self.params.getParamsUI(profile_key) return self.params.getParamsUI(profile_key)

View file

@ -183,7 +183,7 @@ class SqliteStorage():
@param _type: message type (see RFC 6121 §5.2.2) @param _type: message type (see RFC 6121 §5.2.2)
@param timestamp: timestamp in seconds since epoch, or None to use current time @param timestamp: timestamp in seconds since epoch, or None to use current time
""" """
assert(profile!=None) assert(profile)
d = self.dbpool.runQuery("INSERT INTO history(source, source_res, dest, dest_res, timestamp, message, type, profile_id) VALUES (?,?,?,?,?,?,?,?)", d = self.dbpool.runQuery("INSERT INTO history(source, source_res, dest, dest_res, timestamp, message, type, profile_id) VALUES (?,?,?,?,?,?,?,?)",
(from_jid.userhost(), from_jid.resource, to_jid.userhost(), to_jid.resource, timestamp or time.time(), (from_jid.userhost(), from_jid.resource, to_jid.userhost(), to_jid.resource, timestamp or time.time(),
message, _type, self.profiles[profile])) message, _type, self.profiles[profile]))
@ -191,12 +191,13 @@ class SqliteStorage():
{"from_jid":from_jid.full(), "to_jid":to_jid.full(), "message":message}))) {"from_jid":from_jid.full(), "to_jid":to_jid.full(), "message":message})))
return d return d
def getHistory(self, from_jid, to_jid, limit=0, between=True): def getHistory(self, from_jid, to_jid, limit=0, between=True, profile=None):
"""Store a new message in history """Store a new message in history
@param from_jid: source JID (full, or bare for catchall @param from_jid: source JID (full, or bare for catchall
@param to_jid: dest JID (full, or bare for catchall @param to_jid: dest JID (full, or bare for catchall
@param size: maximum number of messages to get, or 0 for unlimited @param size: maximum number of messages to get, or 0 for unlimited
""" """
assert(profile)
def sqliteToDict(query_result): def sqliteToDict(query_result):
query_result.reverse() query_result.reverse()
result = [] result = []
@ -208,8 +209,8 @@ class SqliteStorage():
return result return result
query_parts = ["SELECT timestamp, source, source_res, dest, dest_res, message, type FROM history WHERE"] query_parts = ["SELECT timestamp, source, source_res, dest, dest_res, message, type FROM history WHERE profile_id=? AND"]
values = [] values = [self.profiles[profile]]
def test_jid(_type,_jid): def test_jid(_type,_jid):
values.append(_jid.userhost()) values.append(_jid.userhost())

View file

@ -21,16 +21,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
from logging import debug, info, warning, error from logging import debug, info, warning, error
from twisted.words.xish import domish from twisted.words.xish import domish
from twisted.internet import protocol from twisted.words.protocols.jabber import jid
from twisted.words.protocols.jabber import client, jid
from twisted.words.protocols.jabber import error as jab_error from twisted.words.protocols.jabber import error as jab_error
import os, os.path import os, os.path
from twisted.internet import reactor from twisted.internet import reactor
import pdb from sat.core.exceptions import ProfileNotInCacheError
from zope.interface import implements from wokkel import data_form
from wokkel import disco, iwokkel, data_form
IQ_SET = '/iq[@type="set"]' IQ_SET = '/iq[@type="set"]'
PROFILE_NAME = "pipe-transfer" PROFILE_NAME = "pipe-transfer"
@ -53,19 +50,23 @@ class Exp_Pipe():
def __init__(self, host): def __init__(self, host):
info(_("Plugin Pipe initialization")) info(_("Plugin Pipe initialization"))
self.host = host self.host = host
self._waiting_for_approval = {} #key = id, value = [transfer data, IdelayedCall Reactor timeout,
# current stream method, [failed stream methods], profile]
self.managed_stream_m = [self.host.plugins["XEP-0065"].NAMESPACE, self.managed_stream_m = [self.host.plugins["XEP-0065"].NAMESPACE,
self.host.plugins["XEP-0047"].NAMESPACE] #Stream methods managed self.host.plugins["XEP-0047"].NAMESPACE] #Stream methods managed
self.host.plugins["XEP-0095"].registerSIProfile(PROFILE_NAME, self.transferRequest) self.host.plugins["XEP-0095"].registerSIProfile(PROFILE_NAME, self.transferRequest)
host.bridge.addMethod("pipeOut", ".plugin", in_sign='ssa{ss}s', out_sign='s', method=self.pipeOut) host.bridge.addMethod("pipeOut", ".plugin", in_sign='ssa{ss}s', out_sign='s', method=self.pipeOut)
def _kill_id(self, approval_id): def profileConnected(self, profile):
client = self.host.getClient(profile)
client._pipe_waiting_for_approval = {} #key = id, value = [transfer data, IdelayedCall Reactor timeout,
# current stream method, [failed stream methods], profile]
def _kill_id(self, approval_id, profile):
"""Delete a waiting_for_approval id, called after timeout """Delete a waiting_for_approval id, called after timeout
@param approval_id: id of _waiting_for_approval""" @param approval_id: id of _pipe_waiting_for_approval"""
info(_("SI Pipe Transfer: TimeOut reached for id %s") % approval_id); info(_("SI Pipe Transfer: TimeOut reached for id %s") % approval_id);
try: try:
del self._waiting_for_approval[approval_id] client = self.host.getClient(profile)
del client._pipe_waiting_for_approval[approval_id]
except KeyError: except KeyError:
warning(_("kill id called on a non existant approval id")) warning(_("kill id called on a non existant approval id"))
@ -79,6 +80,9 @@ class Exp_Pipe():
@param profile: %(doc_profile)s""" @param profile: %(doc_profile)s"""
info (_("EXP-PIPE file transfer requested")) info (_("EXP-PIPE file transfer requested"))
debug(si_el.toXml()) debug(si_el.toXml())
client = self.host.getClient(profile)
if not client:
raise ProfileNotInCacheError
pipe_elts = filter(lambda elt: elt.name == 'pipe', si_el.elements()) pipe_elts = filter(lambda elt: elt.name == 'pipe', si_el.elements())
feature_elts = self.host.plugins["XEP-0020"].getFeatureElt(si_el) feature_elts = self.host.plugins["XEP-0020"].getFeatureElt(si_el)
@ -107,17 +111,20 @@ class Exp_Pipe():
#if we are here, the transfer can start, we just need user's agreement #if we are here, the transfer can start, we just need user's agreement
data={ "id": iq_id, "from":from_jid } data={ "id": iq_id, "from":from_jid }
self._waiting_for_approval[si_id] = [data, reactor.callLater(300, self._kill_id, si_id), stream_method, [], profile] client._pipe_waiting_for_approval[si_id] = [data, reactor.callLater(300, self._kill_id, si_id), stream_method, [], profile]
self.host.askConfirmation(si_id, "PIPE_TRANSFER", data, self.confirmationCB) self.host.askConfirmation(si_id, "PIPE_TRANSFER", data, self.confirmationCB, profile)
def confirmationCB(self, sid, accepted, frontend_data): def confirmationCB(self, sid, accepted, frontend_data, profile):
"""Called on confirmation answer """Called on confirmation answer
@param sid: file transfer session id @param sid: file transfer session id
@param accepted: True if file transfer is accepted @param accepted: True if file transfer is accepted
@param frontend_data: data sent by frontend""" @param frontend_data: data sent by frontend"""
data, timeout, stream_method, failed_methods, profile = self._waiting_for_approval[sid] client = self.host.getClient(profile)
if not client:
raise ProfileNotInCacheError
data, timeout, stream_method, failed_methods, profile = client._pipe_waiting_for_approval[sid]
if accepted: if accepted:
if timeout.active(): if timeout.active():
timeout.cancel() timeout.cancel()
@ -125,17 +132,17 @@ class Exp_Pipe():
dest_path = frontend_data['dest_path'] dest_path = frontend_data['dest_path']
except KeyError: except KeyError:
error(_('dest path not found in frontend_data')) error(_('dest path not found in frontend_data'))
del(self._waiting_for_approval[sid]) del(client._pipe_waiting_for_approval[sid])
return return
if stream_method == self.host.plugins["XEP-0065"].NAMESPACE: if stream_method == self.host.plugins["XEP-0065"].NAMESPACE:
file_obj = open(dest_path, 'w+') file_obj = open(dest_path, 'w+')
self.host.plugins["XEP-0065"].prepareToReceive(jid.JID(data['from']), sid, file_obj, None, self._transferSucceeded, self._transferFailed) self.host.plugins["XEP-0065"].prepareToReceive(jid.JID(data['from']), sid, file_obj, None, self._transferSucceeded, self._transferFailed, profile)
elif stream_method == self.host.plugins["XEP-0047"].NAMESPACE: elif stream_method == self.host.plugins["XEP-0047"].NAMESPACE:
file_obj = open(dest_path, 'w+') file_obj = open(dest_path, 'w+')
self.host.plugins["XEP-0047"].prepareToReceive(jid.JID(data['from']), sid, file_obj, None, self._transferSucceeded, self._transferFailed) self.host.plugins["XEP-0047"].prepareToReceive(jid.JID(data['from']), sid, file_obj, None, self._transferSucceeded, self._transferFailed, profile)
else: else:
error(_("Unknown stream method, this should not happen at this stage, cancelling transfer")) error(_("Unknown stream method, this should not happen at this stage, cancelling transfer"))
del(self._waiting_for_approval[sid]) del(client._pipe_waiting_for_approval[sid])
return return
#we can send the iq result #we can send the iq result
@ -146,29 +153,35 @@ class Exp_Pipe():
else: else:
debug (_("Transfer [%s] refused"), sid) debug (_("Transfer [%s] refused"), sid)
self.host.plugins["XEP-0095"].sendRejectedError (data["id"], data['from'], profile=profile) self.host.plugins["XEP-0095"].sendRejectedError (data["id"], data['from'], profile=profile)
del(self._waiting_for_approval[sid]) del(client._pipe_waiting_for_approval[sid])
def _transferSucceeded(self, sid, file_obj, stream_method): def _transferSucceeded(self, sid, file_obj, stream_method, profile):
"""Called by the stream method when transfer successfuly finished """Called by the stream method when transfer successfuly finished
@param id: stream id""" @param id: stream id"""
client = self.host.getClient(profile)
if not client:
raise ProfileNotInCacheError
file_obj.close() file_obj.close()
info(_('Transfer %s successfuly finished') % sid) info(_('Transfer %s successfuly finished') % sid)
del(self._waiting_for_approval[sid]) del(client._pipe_waiting_for_approval[sid])
def _transferFailed(self, sid, file_obj, stream_method, reason): def _transferFailed(self, sid, file_obj, stream_method, reason, profile):
"""Called when something went wrong with the transfer """Called when something went wrong with the transfer
@param id: stream id @param id: stream id
@param reason: can be TIMEOUT, IO_ERROR, PROTOCOL_ERROR""" @param reason: can be TIMEOUT, IO_ERROR, PROTOCOL_ERROR"""
data, timeout, stream_method, failed_methods, profile = self._waiting_for_approval[sid] client = self.host.getClient(profile)
if not client:
raise ProfileNotInCacheError
data, timeout, stream_method, failed_methods, profile = client._pipe_waiting_for_approval[sid]
warning(_('Transfer %(id)s failed with stream method %(s_method)s') % { 'id': sid, warning(_('Transfer %(id)s failed with stream method %(s_method)s') % { 'id': sid,
's_method': stream_method }) 's_method': stream_method })
filepath = file_obj.name filepath = file_obj.name
file_obj.close() file_obj.close()
#TODO: session remenber (within a time limit) when a stream method fail, and avoid that stream method with full jid for the rest of the session #TODO: session remenber (within a time limit) when a stream method fail, and avoid that stream method with full jid for the rest of the session
warning(_("All stream methods failed, can't transfer the file")) warning(_("All stream methods failed, can't transfer the file"))
del(self._waiting_for_approval[sid]) del(client._pipe_waiting_for_approval[sid])
def pipeCb(self, profile, filepath, sid, IQ): def pipeCb(self, filepath, sid, profile, IQ):
if IQ['type'] == "error": if IQ['type'] == "error":
stanza_err = jab_error.exceptionFromStanza(IQ) stanza_err = jab_error.exceptionFromStanza(IQ)
if stanza_err.code == '403' and stanza_err.condition == 'forbidden': if stanza_err.code == '403' and stanza_err.condition == 'forbidden':
@ -230,13 +243,14 @@ class Exp_Pipe():
pipe_transfer_elts.append(pipe_elt) pipe_transfer_elts.append(pipe_elt)
sid, offer = self.host.plugins["XEP-0095"].proposeStream(jid.JID(to_jid), PROFILE, feature_elt, pipe_transfer_elts, profile_key = profile) sid, offer = self.host.plugins["XEP-0095"].proposeStream(jid.JID(to_jid), PROFILE, feature_elt, pipe_transfer_elts, profile_key = profile)
offer.addCallback(self.pipeCb, profile, filepath, sid) offer.addCallback(self.pipeCb, filepath, sid, profile)
return sid return sid
def sendSuccessCb(self, sid, file_obj, stream_method): def sendSuccessCb(self, sid, file_obj, stream_method, profile):
info(_('Transfer %s successfuly finished') % sid) info(_('Transfer %s successfuly finished') % sid)
file_obj.close() file_obj.close()
def sendFailureCb(self, sid, file_obj, stream_method, reason): def sendFailureCb(self, sid, file_obj, stream_method, reason, profile):
file_obj.close() file_obj.close()
warning(_('Transfer %(id)s failed with stream method %(s_method)s') % { 'id': sid, "s_method": stream_method }) warning(_('Transfer %(id)s failed with stream method %(s_method)s %(profile)s') % { 'id': sid, "s_method": stream_method, "profile": profile })

View file

@ -74,7 +74,7 @@ class XmlLog():
#bridge #bridge
host.bridge.addSignal("xmlLog", ".plugin", signature='sss') #args: direction("IN" or "OUT"), xml_data, profile host.bridge.addSignal("xmlLog", ".plugin", signature='sss') #args: direction("IN" or "OUT"), xml_data, profile
do_log = bool(self.host.memory.getParamA("Xml log", "Debug")) do_log = self.host.memory.getParamA("Xml log", "Debug")
if do_log: if do_log:
info(_("XML log activated")) info(_("XML log activated"))
host.trigger.add("XML Initialized", self.logXml) host.trigger.add("XML Initialized", self.logXml)

View file

@ -20,11 +20,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
from logging import debug, info, warning, error from logging import debug, info, warning, error
from twisted.words.protocols.jabber import client, jid from twisted.words.protocols.jabber import client as jabber_client, jid
from twisted.words.protocols.jabber import error as jab_error
from twisted.words.xish import domish from twisted.words.xish import domish
import twisted.internet.error import twisted.internet.error
from twisted.internet import reactor from twisted.internet import reactor
from sat.core.exceptions import ProfileNotInCacheError
from wokkel import disco, iwokkel from wokkel import disco, iwokkel
@ -63,66 +63,82 @@ class XEP_0047():
def __init__(self, host): def __init__(self, host):
info(_("In-Band Bytestreams plugin initialization")) info(_("In-Band Bytestreams plugin initialization"))
self.host = host self.host = host
self.current_stream = {} #key: stream_id, value: data(dict)
def getHandler(self, profile): def getHandler(self, profile):
return XEP_0047_handler(self) return XEP_0047_handler(self)
def _timeOut(self, sid): def profileConnected(self, profile):
"""Delecte current_stream id, called after timeout client = self.host.getClient(profile)
@param id: id of self.current_stream""" if not client:
info(_("In-Band Bytestream: TimeOut reached for id %s") % sid); raise ProfileNotInCacheError
self._killId(sid, False, "TIMEOUT") client.xep_0047_current_stream = {} #key: stream_id, value: data(dict)
def _killId(self, sid, success=False, failure_reason="UNKNOWN"): def _timeOut(self, sid, profile):
"""Delecte current_stream id, called after timeout
@param id: id of client.xep_0047_current_stream"""
info(_("In-Band Bytestream: TimeOut reached for id %s [%s]") % (sid, profile));
self._killId(sid, False, "TIMEOUT", profile)
def _killId(self, sid, success=False, failure_reason="UNKNOWN", profile=None):
"""Delete an current_stream id, clean up associated observers """Delete an current_stream id, clean up associated observers
@param sid: id of self.current_stream""" @param sid: id of client.xep_0047_current_stream"""
if not self.current_stream.has_key(sid): assert(profile)
client = self.host.getClient(profile)
if not client:
warning(_("Client no more in cache"))
return
if not client.xep_0047_current_stream.has_key(sid):
warning(_("kill id called on a non existant id")) warning(_("kill id called on a non existant id"))
return return
if self.current_stream[sid].has_key("observer_cb"): if client.xep_0047_current_stream[sid].has_key("observer_cb"):
xmlstream = self.current_stream[sid]["xmlstream"] client.xmlstream.removeObserver(client.xep_0047_current_stream[sid]["event_data"], client.xep_0047_current_stream[sid]["observer_cb"])
xmlstream.removeObserver(self.current_stream[sid]["event_data"], self.current_stream[sid]["observer_cb"]) if client.xep_0047_current_stream[sid]['timer'].active():
if self.current_stream[sid]['timer'].active(): client.xep_0047_current_stream[sid]['timer'].cancel()
self.current_stream[sid]['timer'].cancel() if client.xep_0047_current_stream[sid].has_key("size"):
if self.current_stream[sid].has_key("size"): self.host.removeProgressCB(sid, profile)
self.host.removeProgressCB(sid)
file_obj = self.current_stream[sid]['file_obj'] file_obj = client.xep_0047_current_stream[sid]['file_obj']
success_cb = self.current_stream[sid]['success_cb'] success_cb = client.xep_0047_current_stream[sid]['success_cb']
failure_cb = self.current_stream[sid]['failure_cb'] failure_cb = client.xep_0047_current_stream[sid]['failure_cb']
del self.current_stream[sid] del client.xep_0047_current_stream[sid]
if success: if success:
success_cb(sid, file_obj, NS_IBB) success_cb(sid, file_obj, NS_IBB, profile)
else: else:
failure_cb(sid, file_obj, NS_IBB, failure_reason) failure_cb(sid, file_obj, NS_IBB, failure_reason, profile)
def getProgress(self, sid, data): def getProgress(self, sid, data, profile):
"""Fill data with position of current transfer""" """Fill data with position of current transfer"""
client = self.host.getClient(profile)
if not client:
raise ProfileNotInCacheError
try: try:
file_obj = self.current_stream[sid]["file_obj"] file_obj = client.xep_0047_current_stream[sid]["file_obj"]
data["position"] = str(file_obj.tell()) data["position"] = str(file_obj.tell())
data["size"] = str(self.current_stream[sid]["size"]) data["size"] = str(client.xep_0047_current_stream[sid]["size"])
except: except:
pass pass
def prepareToReceive(self, from_jid, sid, file_obj, size, success_cb, failure_cb): def prepareToReceive(self, from_jid, sid, file_obj, size, success_cb, failure_cb, profile):
"""Called when a bytestream is imminent """Called when a bytestream is imminent
@param from_jid: jid of the sender @param from_jid: jid of the sender
@param sid: Stream id @param sid: Stream id
@param file_obj: File object where data will be written @param file_obj: File object where data will be written
@param size: full size of the data, or None if unknown @param size: full size of the data, or None if unknown
@param success_cb: method to call when successfuly finished @param success_cb: method to call when successfuly finished
@param failure_cb: method to call when something goes wrong""" @param failure_cb: method to call when something goes wrong
data = self.current_stream[sid] = {} @param profile: %(doc_profile)s"""
client = self.host.getClient(profile)
if not client:
raise ProfileNotInCacheError
data = client.xep_0047_current_stream[sid] = {}
data["from"] = from_jid data["from"] = from_jid
data["file_obj"] = file_obj data["file_obj"] = file_obj
data["seq"] = -1 data["seq"] = -1
if size: if size:
data["size"] = size data["size"] = size
self.host.registerProgressCB(sid, self.getProgress) self.host.registerProgressCB(sid, self.getProgress, profile)
data["timer"] = reactor.callLater(TIMEOUT, self._timeOut, sid) data["timer"] = reactor.callLater(TIMEOUT, self._timeOut, sid)
data["success_cb"] = success_cb data["success_cb"] = success_cb
data["failure_cb"] = failure_cb data["failure_cb"] = failure_cb
@ -130,47 +146,51 @@ class XEP_0047():
def streamOpening(self, IQ, profile): def streamOpening(self, IQ, profile):
debug(_("IBB stream opening")) debug(_("IBB stream opening"))
IQ.handled=True IQ.handled=True
profile_jid, xmlstream = self.host.getJidNStream(profile) client = self.host.getClient(profile)
if not client:
raise ProfileNotInCacheError
open_elt = IQ.firstChildElement() open_elt = IQ.firstChildElement()
block_size = open_elt.getAttribute('block-size') block_size = open_elt.getAttribute('block-size')
sid = open_elt.getAttribute('sid') sid = open_elt.getAttribute('sid')
stanza = open_elt.getAttribute('stanza', 'iq') stanza = open_elt.getAttribute('stanza', 'iq')
if not sid or not block_size or int(block_size)>65535: if not sid or not block_size or int(block_size)>65535:
warning(_("malformed IBB transfer: %s" % IQ['id'])) warning(_("malformed IBB transfer: %s" % IQ['id']))
self.sendNotAcceptableError(IQ['id'], IQ['from'], xmlstream) self.sendNotAcceptableError(IQ['id'], IQ['from'], client.xmlstream)
return return
if not sid in self.current_stream: if not sid in client.xep_0047_current_stream:
warning(_("Ignoring unexpected IBB transfer: %s" % sid)) warning(_("Ignoring unexpected IBB transfer: %s" % sid))
self.sendNotAcceptableError(IQ['id'], IQ['from'], xmlstream) self.sendNotAcceptableError(IQ['id'], IQ['from'], client.xmlstream)
return return
if self.current_stream[sid]["from"] != jid.JID(IQ['from']): if client.xep_0047_current_stream[sid]["from"] != jid.JID(IQ['from']):
warning(_("sended jid inconsistency (man in the middle attack attempt ?)")) warning(_("sended jid inconsistency (man in the middle attack attempt ?)"))
self.sendNotAcceptableError(IQ['id'], IQ['from'], xmlstream) self.sendNotAcceptableError(IQ['id'], IQ['from'], client.xmlstream)
self._killId(sid, False, "PROTOCOL_ERROR") self._killId(sid, False, "PROTOCOL_ERROR", profile=profile)
return return
#at this stage, the session looks ok and will be accepted #at this stage, the session looks ok and will be accepted
#we reset the timeout: #we reset the timeout:
self.current_stream[sid]["timer"].reset(TIMEOUT) client.xep_0047_current_stream[sid]["timer"].reset(TIMEOUT)
#we save the xmlstream, events and observer data to allow observer removal #we save the xmlstream, events and observer data to allow observer removal
self.current_stream[sid]["xmlstream"] = xmlstream client.xep_0047_current_stream[sid]["event_data"] = event_data = (IBB_MESSAGE_DATA if stanza=='message' else IBB_IQ_DATA) % sid
self.current_stream[sid]["event_data"] = event_data = (IBB_MESSAGE_DATA if stanza=='message' else IBB_IQ_DATA) % sid client.xep_0047_current_stream[sid]["observer_cb"] = observer_cb = self.messageData if stanza=='message' else self.iqData
self.current_stream[sid]["observer_cb"] = observer_cb = self.messageData if stanza=='message' else self.iqData
event_close = IBB_CLOSE % sid event_close = IBB_CLOSE % sid
#we now set the stream observer to look after data packet #we now set the stream observer to look after data packet
xmlstream.addObserver(event_data, observer_cb, profile = profile) client.xmlstream.addObserver(event_data, observer_cb, profile = profile)
xmlstream.addOnetimeObserver(event_close, self.streamClosing, profile = profile) client.xmlstream.addOnetimeObserver(event_close, self.streamClosing, profile = profile)
#finally, we send the accept stanza #finally, we send the accept stanza
result = domish.Element((None, 'iq')) result = domish.Element((None, 'iq'))
result['type'] = 'result' result['type'] = 'result'
result['id'] = IQ['id'] result['id'] = IQ['id']
result['to'] = IQ['from'] result['to'] = IQ['from']
xmlstream.send(result) client.xmlstream.send(result)
def streamClosing(self, IQ, profile): def streamClosing(self, IQ, profile):
IQ.handled=True IQ.handled=True
client = self.host.getClient(profile)
if not client:
raise ProfileNotInCacheError
debug(_("IBB stream closing")) debug(_("IBB stream closing"))
data_elt = IQ.firstChildElement() data_elt = IQ.firstChildElement()
sid = data_elt.getAttribute('sid') sid = data_elt.getAttribute('sid')
@ -178,55 +198,60 @@ class XEP_0047():
result['type'] = 'result' result['type'] = 'result'
result['id'] = IQ['id'] result['id'] = IQ['id']
result['to'] = IQ['from'] result['to'] = IQ['from']
self.current_stream[sid]["xmlstream"].send(result) client.xmlstream.send(result)
self._killId(sid, success=True) self._killId(sid, success=True, profile=profile)
def iqData(self, IQ, profile): def iqData(self, IQ, profile):
IQ.handled=True IQ.handled=True
client = self.host.getClient(profile)
if not client:
raise ProfileNotInCacheError
data_elt = IQ.firstChildElement() data_elt = IQ.firstChildElement()
if self._manageDataElt(data_elt, 'iq', IQ['id'], jid.JID(IQ['from'])): if self._manageDataElt(data_elt, 'iq', IQ['id'], jid.JID(IQ['from']), profile):
#and send a success answer #and send a success answer
result = domish.Element((None, 'iq')) result = domish.Element((None, 'iq'))
result['type'] = 'result' result['type'] = 'result'
result['id'] = IQ['id'] result['id'] = IQ['id']
result['to'] = IQ['from'] result['to'] = IQ['from']
_jid, xmlstream = self.host.getJidNStream(profile)
xmlstream.send(result) client.xmlstream.send(result)
def messageData(self, message_elt, profile): def messageData(self, message_elt, profile):
data_elt = message_elt.firstChildElement() data_elt = message_elt.firstChildElement()
sid = message_elt.getAttribute('id','') sid = message_elt.getAttribute('id','')
self._manageDataElt(message_elt, 'message', sid, jid.JID(message_elt['from'])) self._manageDataElt(message_elt, 'message', sid, jid.JID(message_elt['from']), profile)
def _manageDataElt(self, data_elt, stanza, sid, stanza_from_jid): def _manageDataElt(self, data_elt, stanza, sid, stanza_from_jid, profile):
"""Manage the data elelement (check validity and write to the file_obj) """Manage the data elelement (check validity and write to the file_obj)
@param data_elt: "data" domish element @param data_elt: "data" domish element
@return: True if success""" @return: True if success"""
client = self.host.getClient(profile)
if not client:
raise ProfileNotInCacheError
sid = data_elt.getAttribute('sid') sid = data_elt.getAttribute('sid')
if sid not in self.current_stream: if sid not in client.xep_0047_current_stream:
error(_("Received data for an unknown session id")) error(_("Received data for an unknown session id"))
return False return False
xmlstream = self.current_stream[sid]["xmlstream"]
from_jid = self.current_stream[sid]["from"] from_jid = client.xep_0047_current_stream[sid]["from"]
file_obj = self.current_stream[sid]["file_obj"] file_obj = client.xep_0047_current_stream[sid]["file_obj"]
if stanza_from_jid != from_jid: if stanza_from_jid != from_jid:
warning(_("sended jid inconsistency (man in the middle attack attempt ?)")) warning(_("sended jid inconsistency (man in the middle attack attempt ?)"))
if stanza=='iq': if stanza=='iq':
self.sendNotAcceptableError(sid, from_jid, xmlstream) self.sendNotAcceptableError(sid, from_jid, client.xmlstream)
return False return False
self.current_stream[sid]["seq"]+=1 client.xep_0047_current_stream[sid]["seq"]+=1
if int(data_elt.getAttribute("seq",-1)) != self.current_stream[sid]["seq"]: if int(data_elt.getAttribute("seq",-1)) != client.xep_0047_current_stream[sid]["seq"]:
warning(_("Sequence error")) warning(_("Sequence error"))
if stanza=='iq': if stanza=='iq':
self.sendNotAcceptableError(IQ["id"], from_jid, xmlstream) self.sendNotAcceptableError(data_elt["id"], from_jid, client.xmlstream)
return False return False
#we reset the timeout: #we reset the timeout:
self.current_stream[sid]["timer"].reset(TIMEOUT) client.xep_0047_current_stream[sid]["timer"].reset(TIMEOUT)
#we can now decode the data #we can now decode the data
try: try:
@ -235,7 +260,7 @@ class XEP_0047():
#The base64 data is invalid #The base64 data is invalid
warning(_("Invalid base64 data")) warning(_("Invalid base64 data"))
if stanza=='iq': if stanza=='iq':
self.sendNotAcceptableError(IQ["id"], from_jid, xmlstream) self.sendNotAcceptableError(data_elt["id"], from_jid, client.xmlstream)
return False return False
return True return True
@ -253,7 +278,7 @@ class XEP_0047():
error_el.addElement(('urn:ietf:params:xml:ns:xmpp-stanzas','not-acceptable')) error_el.addElement(('urn:ietf:params:xml:ns:xmpp-stanzas','not-acceptable'))
xmlstream.send(result) xmlstream.send(result)
def startStream(self, file_obj, to_jid, sid, length, successCb, failureCb, size = None, profile='@NONE@'): def startStream(self, file_obj, to_jid, sid, length, successCb, failureCb, size = None, profile=None):
"""Launch the stream workflow """Launch the stream workflow
@param file_obj: file_obj to send @param file_obj: file_obj to send
@param to_jid: JID of the recipient @param to_jid: JID of the recipient
@ -262,34 +287,38 @@ class XEP_0047():
@param successCb: method to call when stream successfuly finished @param successCb: method to call when stream successfuly finished
@param failureCb: method to call when something goes wrong @param failureCb: method to call when something goes wrong
@param profile: %(doc_profile)s""" @param profile: %(doc_profile)s"""
client = self.host.getClient(profile)
if not client:
raise ProfileNotInCacheError
if length != None: if length != None:
error(_('stream length not managed yet')) error(_('stream length not managed yet'))
return; return;
profile_jid, xmlstream = self.host.getJidNStream(profile) data = client.xep_0047_current_stream[sid] = {}
data = self.current_stream[sid] = {}
data["timer"] = reactor.callLater(TIMEOUT, self._timeOut, sid) data["timer"] = reactor.callLater(TIMEOUT, self._timeOut, sid)
data["file_obj"] = file_obj data["file_obj"] = file_obj
data["to"] = to_jid data["to"] = to_jid
data["success_cb"] = successCb data["success_cb"] = successCb
data["failure_cb"] = failureCb data["failure_cb"] = failureCb
data["xmlstream"] = xmlstream
data["block_size"] = BLOCK_SIZE data["block_size"] = BLOCK_SIZE
if size: if size:
data["size"] = size data["size"] = size
self.host.registerProgressCB(sid, self.getProgress) self.host.registerProgressCB(sid, self.getProgress, profile)
iq_elt = client.IQ(xmlstream,'set') iq_elt = jabber_client.IQ(client.xmlstream,'set')
iq_elt['from'] = profile_jid.full() iq_elt['from'] = client.jid.full()
iq_elt['to'] = to_jid.full() iq_elt['to'] = to_jid.full()
open_elt = iq_elt.addElement('open',NS_IBB) open_elt = iq_elt.addElement('open',NS_IBB)
open_elt['block-size'] = str(BLOCK_SIZE) open_elt['block-size'] = str(BLOCK_SIZE)
open_elt['sid'] = sid open_elt['sid'] = sid
open_elt['stanza'] = 'iq' open_elt['stanza'] = 'iq'
iq_elt.addCallback(self.iqResult, sid, 0, length) iq_elt.addCallback(self.iqResult, sid, 0, length, profile)
iq_elt.send() iq_elt.send()
def iqResult(self, sid, seq, length, iq_elt): def iqResult(self, sid, seq, length, profile, iq_elt):
"""Called when the result of open iq is received""" """Called when the result of open iq is received"""
data = self.current_stream[sid] client = self.host.getClient(profile)
if not client:
raise ProfileNotInCacheError
data = client.xep_0047_current_stream[sid]
if iq_elt["type"] == "error": if iq_elt["type"] == "error":
warning(_("Transfer failed")) warning(_("Transfer failed"))
self.terminateStream(sid, "IQ_ERROR") self.terminateStream(sid, "IQ_ERROR")
@ -300,18 +329,18 @@ class XEP_0047():
buffer = data["file_obj"].read(data["block_size"]) buffer = data["file_obj"].read(data["block_size"])
if buffer: if buffer:
next_iq_elt = client.IQ(data["xmlstream"],'set') next_iq_elt = jabber_client.IQ(client.xmlstream,'set')
next_iq_elt['to'] = data["to"].full() next_iq_elt['to'] = data["to"].full()
data_elt = next_iq_elt.addElement('data', NS_IBB) data_elt = next_iq_elt.addElement('data', NS_IBB)
data_elt['seq'] = str(seq) data_elt['seq'] = str(seq)
data_elt['sid'] = sid data_elt['sid'] = sid
data_elt.addContent(base64.b64encode(buffer)) data_elt.addContent(base64.b64encode(buffer))
next_iq_elt.addCallback(self.iqResult, sid, seq+1, length) next_iq_elt.addCallback(self.iqResult, sid, seq+1, length, profile)
next_iq_elt.send() next_iq_elt.send()
else: else:
self.terminateStream(sid) self.terminateStream(sid, profile=profile)
def terminateStream(self, sid, failure_reason = None): def terminateStream(self, sid, failure_reason = None, profile=None):
"""Terminate the stream session """Terminate the stream session
@param to_jid: recipient @param to_jid: recipient
@param sid: Session id @param sid: Session id
@ -320,17 +349,20 @@ class XEP_0047():
@param progress_cb: True if we have to remove the progress callback @param progress_cb: True if we have to remove the progress callback
@param callback: method to call after finishing @param callback: method to call after finishing
@param failure_reason: reason of the failure, or None if steam was successful""" @param failure_reason: reason of the failure, or None if steam was successful"""
data = self.current_stream[sid] client = self.host.getClient(profile)
iq_elt = client.IQ(data["xmlstream"],'set') if not client:
raise ProfileNotInCacheError
data = client.xep_0047_current_stream[sid]
iq_elt = jabber_client.IQ(client.xmlstream,'set')
iq_elt['to'] = data["to"].full() iq_elt['to'] = data["to"].full()
close_elt = iq_elt.addElement('close',NS_IBB) close_elt = iq_elt.addElement('close',NS_IBB)
close_elt['sid'] = sid close_elt['sid'] = sid
iq_elt.send() iq_elt.send()
self.host.removeProgressCB(sid) self.host.removeProgressCB(sid, profile)
if failure_reason: if failure_reason:
self._killId(sid, False, failure_reason) self._killId(sid, False, failure_reason, profile=profile)
else: else:
self._killId(sid, True) self._killId(sid, True, profile=profile)
class XEP_0047_handler(XMPPHandler): class XEP_0047_handler(XMPPHandler):
implements(iwokkel.IDisco) implements(iwokkel.IDisco)

View file

@ -157,15 +157,15 @@ class XEP_0054():
if answer.firstChildElement().name == "vCard": if answer.firstChildElement().name == "vCard":
d = self.vCard2Dict(answer.firstChildElement(), jid.JID(answer["from"]), profile) d = self.vCard2Dict(answer.firstChildElement(), jid.JID(answer["from"]), profile)
d.addCallback(lambda data: self.host.bridge.actionResult("RESULT", answer['id'], data)) d.addCallback(lambda data: self.host.bridge.actionResult("RESULT", answer['id'], data, profile))
else: else:
error (_("FIXME: vCard not found as first child element")) error (_("FIXME: vCard not found as first child element"))
self.host.bridge.actionResult("SUPPRESS", answer['id'], {}) #FIXME: maybe an error message would be better self.host.bridge.actionResult("SUPPRESS", answer['id'], {}, profile) #FIXME: maybe an error message would be better
def vcard_err(self, failure): def vcard_err(self, failure, profile):
"""Called when something is wrong with registration""" """Called when something is wrong with registration"""
error (_("Can't find VCard of %s") % failure.value.stanza['from']) error (_("Can't find VCard of %s") % failure.value.stanza['from'])
self.host.bridge.actionResult("SUPPRESS", failure.value.stanza['id'], {}) #FIXME: maybe an error message would be better self.host.bridge.actionResult("SUPPRESS", failure.value.stanza['id'], {}, profile) #FIXME: maybe an error message would be better
def getCard(self, target, profile_key='@DEFAULT@'): def getCard(self, target, profile_key='@DEFAULT@'):
"""Ask server for VCard """Ask server for VCard
@ -182,7 +182,7 @@ class XEP_0054():
reg_request["from"]=current_jid.full() reg_request["from"]=current_jid.full()
reg_request["to"] = to_jid.userhost() reg_request["to"] = to_jid.userhost()
reg_request.addElement('vCard', NS_VCARD) reg_request.addElement('vCard', NS_VCARD)
reg_request.send(to_jid.userhost()).addCallbacks(self.vcard_ok, self.vcard_err, [profile]) reg_request.send(to_jid.userhost()).addCallbacks(self.vcard_ok, self.vcard_err, callbackArgs=[profile], errbackArgs=[profile])
return reg_request["id"] return reg_request["id"]
def getAvatarFile(self, hash): def getAvatarFile(self, hash):

View file

@ -58,13 +58,13 @@ THE SOFTWARE.
from logging import debug, info, warning, error from logging import debug, info, warning, error
from twisted.internet import protocol, reactor from twisted.internet import protocol, reactor
from twisted.internet import error as jab_error from twisted.internet import error as jab_error
from twisted.words.protocols.jabber import client, jid from twisted.words.protocols.jabber import jid, client as jabber_client
from twisted.protocols.basic import FileSender from twisted.protocols.basic import FileSender
from twisted.words.xish import domish from twisted.words.xish import domish
from twisted.web.client import getPage from twisted.web.client import getPage
from sat.core.exceptions import ProfileNotInCacheError
import struct import struct
import urllib import hashlib
import hashlib, pdb
from zope.interface import implements from zope.interface import implements
@ -298,10 +298,10 @@ class SOCKSv5(protocol.Protocol, FileSender):
if self.factory.proxy: if self.factory.proxy:
self.state = STATE_READY self.state = STATE_READY
self.factory.activateCb(self.sid, self.factory.iq_id, self.startTransfer) self.factory.activateCb(self.sid, self.factory.iq_id, self.startTransfer, self.profile)
else: else:
self.state = STATE_TARGET_READY self.state = STATE_TARGET_READY
self.factory.activateCb(self.sid, self.factory.iq_id) self.factory.activateCb(self.sid, self.factory.iq_id, self.profile)
except struct.error, why: except struct.error, why:
return None return None
@ -311,6 +311,7 @@ class SOCKSv5(protocol.Protocol, FileSender):
if isinstance(self.factory, Socks5ClientFactory): if isinstance(self.factory, Socks5ClientFactory):
self.sid = self.factory.sid self.sid = self.factory.sid
self.profile = self.factory.profile
self.data = self.factory.data self.data = self.factory.data
self.state = STATE_TARGET_INITIAL self.state = STATE_TARGET_INITIAL
self._startNegotiation() self._startNegotiation()
@ -318,13 +319,16 @@ class SOCKSv5(protocol.Protocol, FileSender):
def connectRequested(self, addr, port): def connectRequested(self, addr, port):
debug("connectRequested") debug("connectRequested")
# Check that this session if expected # Check that this session is expected
if not self.factory.hash_sid_map.has_key(addr): if not self.factory.hash_sid_map.has_key(addr):
#no: we refuse it #no: we refuse it
self.sendErrorReply(socks5.REPLY_CONN_REFUSED) self.sendErrorReply(REPLY_CONN_REFUSED)
return return
self.sid = self.factory.hash_sid_map[addr] self.sid, self.profile = self.factory.hash_sid_map[addr]
self.factory.current_stream[self.sid]["start_transfer_cb"] = self.startTransfer client = self.factory.host.getClient(self.profile)
if not client:
raise ProfileNotInCacheError
client.xep_0065_current_stream[self.sid]["start_transfer_cb"] = self.startTransfer
self.connectCompleted(addr, 0) self.connectCompleted(addr, 0)
self.transport.stopReading() self.transport.stopReading()
@ -336,7 +340,7 @@ class SOCKSv5(protocol.Protocol, FileSender):
def fileTransfered(self, d): def fileTransfered(self, d):
info(_("File transfer completed, closing connection")) info(_("File transfer completed, closing connection"))
self.transport.loseConnection() self.transport.loseConnection()
self.factory.finishedCb(self.sid, True) self.factory.finishedCb(self.sid, True, self.profile)
def connectCompleted(self, remotehost, remoteport): def connectCompleted(self, remotehost, remoteport):
debug("connectCompleted") debug("connectCompleted")
@ -395,8 +399,8 @@ class SOCKSv5(protocol.Protocol, FileSender):
class Socks5ServerFactory(protocol.ServerFactory): class Socks5ServerFactory(protocol.ServerFactory):
protocol = SOCKSv5 protocol = SOCKSv5
def __init__(self, current_stream, hash_sid_map, finishedCb): def __init__(self, host, hash_sid_map, finishedCb):
self.current_stream = current_stream self.host = host
self.hash_sid_map = hash_sid_map self.hash_sid_map = hash_sid_map
self.finishedCb = finishedCb self.finishedCb = finishedCb
@ -409,27 +413,30 @@ class Socks5ServerFactory(protocol.ServerFactory):
class Socks5ClientFactory(protocol.ClientFactory): class Socks5ClientFactory(protocol.ClientFactory):
protocol = SOCKSv5 protocol = SOCKSv5
def __init__(self, current_stream, sid, iq_id, activateCb, finishedCb, proxy=False): def __init__(self, current_stream, sid, iq_id, activateCb, finishedCb, proxy=False, profile=None):
"""Init the Client Factory """Init the Client Factory
@param current_stream: current streams data @param current_stream: current streams data
@param sid: Session ID @param sid: Session ID
@param iq_id: iq id used to initiate the stream @param iq_id: iq id used to initiate the stream
@param activateCb: method to call to activate the stream @param activateCb: method to call to activate the stream
@param finishedCb: method to call when the stream session is finished @param finishedCb: method to call when the stream session is finished
@param proxy: True if we are connecting throught a proxy (and we are a requester)""" @param proxy: True if we are connecting throught a proxy (and we are a requester)
@param profile: %(doc_profile)s"""
assert(profile)
self.data = current_stream[sid] self.data = current_stream[sid]
self.sid = sid self.sid = sid
self.iq_id = iq_id self.iq_id = iq_id
self.activateCb = activateCb self.activateCb = activateCb
self.finishedCb = finishedCb self.finishedCb = finishedCb
self.proxy = proxy self.proxy = proxy
self.profile = profile
def startedConnecting(self, connector): def startedConnecting(self, connector):
debug (_("Socks 5 client connection started")) debug (_("Socks 5 client connection started"))
def clientConnectionLost(self, connector, reason): def clientConnectionLost(self, connector, reason):
debug (_("Socks 5 client connection lost")) debug (_("Socks 5 client connection lost (reason: %s)"), reason)
self.finishedCb(self.sid, reason.type == jab_error.ConnectionDone) #TODO: really check if the state is actually successful self.finishedCb(self.sid, reason.type == jab_error.ConnectionDone, self.profile) #TODO: really check if the state is actually successful
class XEP_0065(): class XEP_0065():
@ -458,12 +465,11 @@ class XEP_0065():
info(_("Plugin XEP_0065 initialization")) info(_("Plugin XEP_0065 initialization"))
#session data #session data
self.current_stream = {} #key: stream_id, value: data(dict) self.hash_sid_map = {} #key: hash of the transfer session, value: (session id, profile)
self.hash_sid_map = {} #key: hash of the transfer session, value: session id
self.host = host self.host = host
debug(_("registering")) debug(_("registering"))
self.server_factory = Socks5ServerFactory(self.current_stream, self.hash_sid_map, self._killId) self.server_factory = Socks5ServerFactory(host, self.hash_sid_map, lambda sid, success, profile: self._killId(sid, success, profile=profile))
#parameters #parameters
host.memory.importParams(XEP_0065.params) host.memory.importParams(XEP_0065.params)
@ -476,53 +482,69 @@ class XEP_0065():
def getHandler(self, profile): def getHandler(self, profile):
return XEP_0065_handler(self) return XEP_0065_handler(self)
def profileConnected(self, profile):
client = self.host.getClient(profile)
if not client:
raise ProfileNotInCacheError
client.xep_0065_current_stream = {} #key: stream_id, value: data(dict)
def getExternalIP(self): def getExternalIP(self):
"""Return IP visible from outside, by asking to a website""" """Return IP visible from outside, by asking to a website"""
return getPage("http://www.goffi.org/sat_tools/get_ip.php") return getPage("http://www.goffi.org/sat_tools/get_ip.php")
def getProgress(self, sid, data): def getProgress(self, sid, data, profile):
"""Fill data with position of current transfer""" """Fill data with position of current transfer"""
client = self.host.getClient(profile)
if not client:
raise ProfileNotInCacheError
try: try:
file_obj = self.current_stream[sid]["file_obj"] file_obj = client.xep_0065_current_stream[sid]["file_obj"]
data["position"] = str(file_obj.tell()) data["position"] = str(file_obj.tell())
data["size"] = str(self.current_stream[sid]["size"]) data["size"] = str(client.xep_0065_current_stream[sid]["size"])
except: except:
pass pass
def _timeOut(self, sid): def _timeOut(self, sid, profile):
"""Delecte current_stream id, called after timeout """Delecte current_stream id, called after timeout
@param id: id of self.current_stream""" @param id: id of client.xep_0065_current_stream"""
info(_("Socks5 Bytestream: TimeOut reached for id %s") % sid); info(_("Socks5 Bytestream: TimeOut reached for id %s [%s]") % (sid, profile));
self._killId(sid, False, "TIMEOUT") self._killId(sid, False, "TIMEOUT", profile)
def _killId(self, sid, success=False, failure_reason="UNKNOWN"): def _killId(self, sid, success=False, failure_reason="UNKNOWN", profile=None):
"""Delete an current_stream id, clean up associated observers """Delete an current_stream id, clean up associated observers
@param sid: id of self.current_stream""" @param sid: id of client.xep_0065_current_stream"""
if not self.current_stream.has_key(sid): assert(profile)
client = self.host.getClient(profile)
if not client:
warning(_("Client no more in cache"))
return
if not client.xep_0065_current_stream.has_key(sid):
warning(_("kill id called on a non existant id")) warning(_("kill id called on a non existant id"))
return return
if self.current_stream[sid].has_key("observer_cb"): if client.xep_0065_current_stream[sid].has_key("observer_cb"):
xmlstream = self.current_stream[sid]["xmlstream"] xmlstream = client.xep_0065_current_stream[sid]["xmlstream"]
xmlstream.removeObserver(self.current_stream[sid]["event_data"], self.current_stream[sid]["observer_cb"]) xmlstream.removeObserver(client.xep_0065_current_stream[sid]["event_data"], client.xep_0065_current_stream[sid]["observer_cb"])
if self.current_stream[sid]['timer'].active(): if client.xep_0065_current_stream[sid]['timer'].active():
self.current_stream[sid]['timer'].cancel() client.xep_0065_current_stream[sid]['timer'].cancel()
if self.current_stream[sid].has_key("size"): if client.xep_0065_current_stream[sid].has_key("size"):
self.host.removeProgressCB(sid) self.host.removeProgressCB(sid, profile)
file_obj = self.current_stream[sid]['file_obj'] file_obj = client.xep_0065_current_stream[sid]['file_obj']
success_cb = self.current_stream[sid]['success_cb'] success_cb = client.xep_0065_current_stream[sid]['success_cb']
failure_cb = self.current_stream[sid]['failure_cb'] failure_cb = client.xep_0065_current_stream[sid]['failure_cb']
del self.current_stream[sid] session_hash = client.xep_0065_current_stream[sid].get('hash')
if self.hash_sid_map.has_key(sid): del client.xep_0065_current_stream[sid]
del self.hash_sid_map[sid] if session_hash in self.hash_sid_map:
#FIXME: check that self.hash_sid_map is correctly cleaned in all cases (timeout, normal flow, etc).
del self.hash_sid_map[session_hash]
if success: if success:
success_cb(sid, file_obj, NS_BS) success_cb(sid, file_obj, NS_BS, profile)
else: else:
failure_cb(sid, file_obj, NS_BS, failure_reason) failure_cb(sid, file_obj, NS_BS, failure_reason, profile)
def startStream(self, file_obj, to_jid, sid, length, successCb, failureCb, size = None, profile='@NONE@'): def startStream(self, file_obj, to_jid, sid, length, successCb, failureCb, size = None, profile=None):
"""Launch the stream workflow """Launch the stream workflow
@param file_obj: file_obj to send @param file_obj: file_obj to send
@param to_jid: JID of the recipient @param to_jid: JID of the recipient
@ -531,16 +553,21 @@ class XEP_0065():
@param successCb: method to call when stream successfuly finished @param successCb: method to call when stream successfuly finished
@param failureCb: method to call when something goes wrong @param failureCb: method to call when something goes wrong
@param profile: %(doc_profile)s""" @param profile: %(doc_profile)s"""
assert(profile)
client = self.host.getClient(profile)
if not client:
error(_("Unknown profile, this should not happen"))
raise ProfileNotInCacheError
if length != None: if length != None:
error(_('stream length not managed yet')) error(_('stream length not managed yet'))
return; return;
profile_jid, xmlstream = self.host.getJidNStream(profile)
if not profile_jid or not xmlstream: profile_jid = client.jid
error(_("Unknown profile, this should not happen")) xmlstream = client.xmlstream
return;
data = self.current_stream[sid] = {} data = client.xep_0065_current_stream[sid] = {}
data["profile"] = profile data["timer"] = reactor.callLater(TIMEOUT, self._timeOut, sid, profile)
data["timer"] = reactor.callLater(TIMEOUT, self._timeOut, sid)
data["file_obj"] = file_obj data["file_obj"] = file_obj
data["from"] = profile_jid data["from"] = profile_jid
data["to"] = to_jid data["to"] = to_jid
@ -548,11 +575,11 @@ class XEP_0065():
data["failure_cb"] = failureCb data["failure_cb"] = failureCb
data["xmlstream"] = xmlstream data["xmlstream"] = xmlstream
data["hash"] = calculateHash(profile_jid, to_jid, sid) data["hash"] = calculateHash(profile_jid, to_jid, sid)
self.hash_sid_map[data["hash"]] = sid self.hash_sid_map[data["hash"]] = (sid, profile)
if size: if size:
data["size"] = size data["size"] = size
self.host.registerProgressCB(sid, self.getProgress) self.host.registerProgressCB(sid, self.getProgress, profile)
iq_elt = client.IQ(xmlstream,'set') iq_elt = jabber_client.IQ(xmlstream,'set')
iq_elt["from"] = profile_jid.full() iq_elt["from"] = profile_jid.full()
iq_elt["to"] = to_jid.full() iq_elt["to"] = to_jid.full()
query_elt = iq_elt.addElement('query', NS_BS) query_elt = iq_elt.addElement('query', NS_BS)
@ -570,20 +597,21 @@ class XEP_0065():
streamhost['port'] = self.host.memory.getParamA("Proxy port", "File Transfer", profile_key=profile) streamhost['port'] = self.host.memory.getParamA("Proxy port", "File Transfer", profile_key=profile)
streamhost['jid'] = self.host.memory.getParamA("Proxy", "File Transfer", profile_key=profile) streamhost['jid'] = self.host.memory.getParamA("Proxy", "File Transfer", profile_key=profile)
iq_elt.addCallback(self.iqResult, sid) iq_elt.addCallback(self.iqResult, sid, profile)
iq_elt.send() iq_elt.send()
def iqResult(self, sid, iq_elt): def iqResult(self, sid, profile, iq_elt):
"""Called when the result of open iq is received""" """Called when the result of open iq is received"""
if iq_elt["type"] == "error": if iq_elt["type"] == "error":
warning(_("Transfer failed")) warning(_("Transfer failed"))
return return
client = self.host.getClient(profile)
if not client:
raise ProfileNotInCacheError
try: try:
data = self.current_stream[sid] data = client.xep_0065_current_stream[sid]
file_obj = data["file_obj"] file_obj = data["file_obj"]
timer = data["timer"] timer = data["timer"]
profile = data["profile"]
except KeyError: except KeyError:
error(_("Internal error, can't do transfer")) error(_("Internal error, can't do transfer"))
return return
@ -607,15 +635,17 @@ class XEP_0065():
if proxy_jid != streamhost_jid: if proxy_jid != streamhost_jid:
warning(_("Proxy jid is not the same as in parameters, this should not happen")) warning(_("Proxy jid is not the same as in parameters, this should not happen"))
return return
factory = Socks5ClientFactory(self.current_stream, sid, None, self.activateProxyStream, self._killId, True) factory = Socks5ClientFactory(client.xep_0065_current_stream, sid, None, self.activateProxyStream, lambda sid, success, profile: self._killId(sid, success, profile=profile), True, profile)
reactor.connectTCP(proxy_host, int(proxy_port), factory) reactor.connectTCP(proxy_host, int(proxy_port), factory)
else: else:
data["start_transfer_cb"](file_obj) #We now activate the stream data["start_transfer_cb"](file_obj) #We now activate the stream
def activateProxyStream(self, sid, iq_id, start_transfer_cb): def activateProxyStream(self, sid, iq_id, start_transfer_cb, profile):
debug(_("activating stream")) debug(_("activating stream"))
data = self.current_stream[sid] client = self.host.getClient(profile)
profile = data['profile'] if not client:
raise ProfileNotInCacheError
data = client.xep_0065_current_stream[sid]
profile_jid, xmlstream = self.host.getJidNStream(profile) profile_jid, xmlstream = self.host.getJidNStream(profile)
iq_elt = client.IQ(xmlstream,'set') iq_elt = client.IQ(xmlstream,'set')
@ -634,22 +664,26 @@ class XEP_0065():
else: else:
start_transfer_cb(file_obj) start_transfer_cb(file_obj)
def prepareToReceive(self, from_jid, sid, file_obj, size, success_cb, failure_cb): def prepareToReceive(self, from_jid, sid, file_obj, size, success_cb, failure_cb, profile):
"""Called when a bytestream is imminent """Called when a bytestream is imminent
@param from_jid: jid of the sender @param from_jid: jid of the sender
@param sid: Stream id @param sid: Stream id
@param file_obj: File object where data will be written @param file_obj: File object where data will be written
@param size: full size of the data, or None if unknown @param size: full size of the data, or None if unknown
@param success_cb: method to call when successfuly finished @param success_cb: method to call when successfuly finished
@param failure_cb: method to call when something goes wrong""" @param failure_cb: method to call when something goes wrong
data = self.current_stream[sid] = {} @param profile: %(doc_profile)s"""
client = self.host.getClient(profile)
if not client:
raise ProfileNotInCacheError
data = client.xep_0065_current_stream[sid] = {}
data["from"] = from_jid data["from"] = from_jid
data["file_obj"] = file_obj data["file_obj"] = file_obj
data["seq"] = -1 data["seq"] = -1
if size: if size:
data["size"] = size data["size"] = size
self.host.registerProgressCB(sid, self.getProgress) self.host.registerProgressCB(sid, self.getProgress, profile)
data["timer"] = reactor.callLater(TIMEOUT, self._timeOut, sid) data["timer"] = reactor.callLater(TIMEOUT, self._timeOut, sid, profile)
data["success_cb"] = success_cb data["success_cb"] = success_cb
data["failure_cb"] = failure_cb data["failure_cb"] = failure_cb
@ -657,20 +691,26 @@ class XEP_0065():
def streamQuery(self, iq_elt, profile): def streamQuery(self, iq_elt, profile):
"""Get file using byte stream""" """Get file using byte stream"""
debug(_("BS stream query")) debug(_("BS stream query"))
profile_jid, xmlstream = self.host.getJidNStream(profile) client = self.host.getClient(profile)
if not client:
raise ProfileNotInCacheError
xmlstream = client.xmlstream
iq_elt.handled = True iq_elt.handled = True
query_elt = iq_elt.firstChildElement() query_elt = iq_elt.firstChildElement()
sid = query_elt.getAttribute("sid") sid = query_elt.getAttribute("sid")
streamhost_elts = filter(lambda elt: elt.name == 'streamhost', query_elt.elements()) streamhost_elts = filter(lambda elt: elt.name == 'streamhost', query_elt.elements())
if not sid in self.current_stream: if not sid in client.xep_0065_current_stream:
warning(_("Ignoring unexpected BS transfer: %s" % sid)) warning(_("Ignoring unexpected BS transfer: %s" % sid))
self.sendNotAcceptableError(iq_elt['id'], iq_elt['from'], xmlstream) self.sendNotAcceptableError(iq_elt['id'], iq_elt['from'], xmlstream)
return return
self.current_stream[sid]['timer'].cancel() client.xep_0065_current_stream[sid]['timer'].cancel()
self.current_stream[sid]["to"] = jid.JID(iq_elt["to"]) client.xep_0065_current_stream[sid]["to"] = jid.JID(iq_elt["to"])
self.current_stream[sid]["xmlstream"] = xmlstream client.xep_0065_current_stream[sid]["xmlstream"] = xmlstream
if not streamhost_elts: if not streamhost_elts:
warning(_("No streamhost found in stream query %s" % sid)) warning(_("No streamhost found in stream query %s" % sid))
@ -686,16 +726,19 @@ class XEP_0065():
self.sendBadRequestError(iq_elt['id'], iq_elt['from'], xmlstream) self.sendBadRequestError(iq_elt['id'], iq_elt['from'], xmlstream)
return return
self.current_stream[sid]["streamhost"] = (sh_host, sh_port, sh_jid) client.xep_0065_current_stream[sid]["streamhost"] = (sh_host, sh_port, sh_jid)
info (_("Stream proposed: host=[%(host)s] port=[%(port)s]") % {'host':sh_host, 'port':sh_port}) info (_("Stream proposed: host=[%(host)s] port=[%(port)s]") % {'host':sh_host, 'port':sh_port})
factory = Socks5ClientFactory(self.current_stream, sid, iq_elt["id"], self.activateStream, self._killId) factory = Socks5ClientFactory(client.xep_0065_current_stream, sid, iq_elt["id"], self.activateStream, lambda sid, success, profile: self._killId(sid, success, profile=profile), profile=profile)
reactor.connectTCP(sh_host, int(sh_port), factory) reactor.connectTCP(sh_host, int(sh_port), factory)
def activateStream(self, sid, iq_id): def activateStream(self, sid, iq_id, profile):
client = self.host.getClient(profile)
if not client:
raise ProfileNotInCacheError
debug(_("activating stream")) debug(_("activating stream"))
result = domish.Element((None, 'iq')) result = domish.Element((None, 'iq'))
data = self.current_stream[sid] data = client.xep_0065_current_stream[sid]
result['type'] = 'result' result['type'] = 'result'
result['id'] = iq_id result['id'] = iq_id
result['from'] = data["to"].full() result['from'] = data["to"].full()
@ -769,7 +812,7 @@ class XEP_0065_handler(XMPPHandler):
if not proxy_ent: if not proxy_ent:
debug(_("No proxy found on this server")) debug(_("No proxy found on this server"))
return return
iq_elt = client.IQ(self.parent.xmlstream,'get') iq_elt = jabber_client.IQ(self.parent.xmlstream,'get')
iq_elt["to"] = proxy_ent.full() iq_elt["to"] = proxy_ent.full()
query_elt = iq_elt.addElement('query', NS_BS) query_elt = iq_elt.addElement('query', NS_BS)
iq_elt.addCallback(self._proxyDataResult) iq_elt.addCallback(self._proxyDataResult)

View file

@ -54,7 +54,7 @@ class XEP_0077():
"""Add a callback which is called when registration to target is successful""" """Add a callback which is called when registration to target is successful"""
self.triggers[target] = (cb, profile) self.triggers[target] = (cb, profile)
def reg_ok(self, answer): def reg_ok(self, answer, profile):
"""Called after the first get IQ""" """Called after the first get IQ"""
try: try:
x_elem = filter (lambda x:x.name == "x", answer.firstChildElement().elements())[0] #We only want the "x" element (data form) x_elem = filter (lambda x:x.name == "x", answer.firstChildElement().elements())[0] #We only want the "x" element (data form)
@ -63,47 +63,47 @@ class XEP_0077():
#TODO: manage registration without data form #TODO: manage registration without data form
answer_data={"reason": "unmanaged", "message":_("This gateway can't be managed by SàT, sorry :(")} answer_data={"reason": "unmanaged", "message":_("This gateway can't be managed by SàT, sorry :(")}
answer_type = "ERROR" answer_type = "ERROR"
self.host.bridge.actionResult(answer_type, answer['id'], answer_data) self.host.bridge.actionResult(answer_type, answer['id'], answer_data, profile)
return return
form = data_form.Form.fromElement(x_elem) form = data_form.Form.fromElement(x_elem)
xml_data = dataForm2xml(form) xml_data = dataForm2xml(form)
self.host.bridge.actionResult("XMLUI", answer['id'], {"target":answer["from"], "type":"registration", "xml":xml_data}) self.host.bridge.actionResult("XMLUI", answer['id'], {"target":answer["from"], "type":"registration", "xml":xml_data}, profile)
def reg_err(self, failure): def reg_err(self, failure, profile):
"""Called when something is wrong with registration""" """Called when something is wrong with registration"""
info (_("Registration failure: %s") % str(failure.value)) info (_("Registration failure: %s") % str(failure.value))
answer_data = {} answer_data = {}
answer_data['reason'] = 'unknown' answer_data['reason'] = 'unknown'
answer_data={"message":"%s [code: %s]" % (failure.value.condition, unicode(failure.value))} answer_data={"message":"%s [code: %s]" % (failure.value.condition, unicode(failure.value))}
answer_type = "ERROR" answer_type = "ERROR"
self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data) self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data, profile)
def unregistrationAnswer(self, answer): def unregistrationAnswer(self, answer, profile):
debug (_("registration answer: %s") % answer.toXml()) debug (_("registration answer: %s") % answer.toXml())
answer_type = "SUCCESS" answer_type = "SUCCESS"
answer_data={"message":_("Your are now unregistred")} answer_data={"message":_("Your are now unregistred")}
self.host.bridge.actionResult(answer_type, answer['id'], answer_data) self.host.bridge.actionResult(answer_type, answer['id'], answer_data, profile)
def unregistrationFailure(self, failure): def unregistrationFailure(self, failure, profile):
info (_("Unregistration failure: %s") % str(failure.value)) info (_("Unregistration failure: %s") % str(failure.value))
answer_type = "ERROR" answer_type = "ERROR"
answer_data = {} answer_data = {}
answer_data['reason'] = 'unknown' answer_data['reason'] = 'unknown'
answer_data={"message":_("Unregistration failed: %s") % failure.value.condition} answer_data={"message":_("Unregistration failed: %s") % failure.value.condition}
self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data) self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data, profile)
def registrationAnswer(self, answer): def registrationAnswer(self, answer, profile):
debug (_("registration answer: %s") % answer.toXml()) debug (_("registration answer: %s") % answer.toXml())
answer_type = "SUCCESS" answer_type = "SUCCESS"
answer_data={"message":_("Registration successfull")} answer_data={"message":_("Registration successfull")}
self.host.bridge.actionResult(answer_type, answer['id'], answer_data) self.host.bridge.actionResult(answer_type, answer['id'], answer_data, profile)
if self.triggers.has_key(answer["from"]): if self.triggers.has_key(answer["from"]):
callback,profile = self.triggers[answer["from"]] callback,profile = self.triggers[answer["from"]]
callback(answer["from"], profile) callback(answer["from"], profile)
del self.triggers[answer["from"]] del self.triggers[answer["from"]]
def registrationFailure(self, failure): def registrationFailure(self, failure, profile):
info (_("Registration failure: %s") % str(failure.value)) info (_("Registration failure: %s") % str(failure.value))
print failure.value.stanza.toXml() print failure.value.stanza.toXml()
answer_type = "ERROR" answer_type = "ERROR"
@ -114,7 +114,7 @@ class XEP_0077():
else: else:
answer_data['reason'] = 'unknown' answer_data['reason'] = 'unknown'
answer_data={"message":_("Registration failed")} answer_data={"message":_("Registration failed")}
self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data) self.host.bridge.actionResult(answer_type, failure.value.stanza['id'], answer_data, profile)
if self.triggers.has_key(answer["from"]): if self.triggers.has_key(answer["from"]):
del self.triggers[answer["from"]] del self.triggers[answer["from"]]
@ -122,22 +122,22 @@ class XEP_0077():
"""Submit a form for registration, using data_form""" """Submit a form for registration, using data_form"""
id, deferred = self.host.submitForm(action, target, fields, profile) id, deferred = self.host.submitForm(action, target, fields, profile)
if action == 'CANCEL': if action == 'CANCEL':
deferred.addCallbacks(self.unregistrationAnswer, self.unregistrationFailure) deferred.addCallbacks(self.unregistrationAnswer, self.unregistrationFailure, callbackArgs=[profile], errbackArgs=[profile])
else: else:
deferred.addCallbacks(self.registrationAnswer, self.registrationFailure) deferred.addCallbacks(self.registrationAnswer, self.registrationFailure, callbackArgs=[profile], errbackArgs=[profile])
return id return id
def in_band_register(self, target, profile_key='@DEFAULT@'): def in_band_register(self, target, profile_key='@DEFAULT@'):
"""register to a target JID""" """register to a target JID"""
current_jid, xmlstream = self.host.getJidNStream(profile_key) client = self.host.getClient(profile_key)
if not xmlstream: if not client:
error (_('Asking for an non-existant or not connected profile')) error (_('Asking for an non-existant or not connected profile'))
return "" return ""
to_jid = jid.JID(target) to_jid = jid.JID(target)
debug(_("Asking registration for [%s]") % to_jid.full()) debug(_("Asking registration for [%s]") % to_jid.full())
reg_request=IQ(xmlstream,'get') reg_request=IQ(client.xmlstream,'get')
reg_request["from"]=current_jid.full() reg_request["from"]=client.jid.full()
reg_request["to"] = to_jid.full() reg_request["to"] = to_jid.full()
query=reg_request.addElement('query', NS_REG) query=reg_request.addElement('query', NS_REG)
reg_request.send(to_jid.full()).addCallbacks(self.reg_ok, self.reg_err) reg_request.send(to_jid.full()).addCallbacks(self.reg_ok, self.reg_err, callbackArgs=[profile], errbackArgs=[profile])
return reg_request["id"] return reg_request["id"]

View file

@ -21,16 +21,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
from logging import debug, info, warning, error from logging import debug, info, warning, error
from twisted.words.xish import domish from twisted.words.xish import domish
from twisted.internet import protocol from twisted.words.protocols.jabber import jid
from twisted.words.protocols.jabber import client, jid
from twisted.words.protocols.jabber import error as jab_error from twisted.words.protocols.jabber import error as jab_error
import os, os.path import os, os.path
from twisted.internet import reactor from twisted.internet import reactor
import pdb from sat.core.exceptions import ProfileNotInCacheError
from zope.interface import implements
from wokkel import disco, iwokkel, data_form from wokkel import data_form
IQ_SET = '/iq[@type="set"]' IQ_SET = '/iq[@type="set"]'
PROFILE_NAME = "file-transfer" PROFILE_NAME = "file-transfer"
@ -52,19 +50,23 @@ class XEP_0096():
def __init__(self, host): def __init__(self, host):
info(_("Plugin XEP_0096 initialization")) info(_("Plugin XEP_0096 initialization"))
self.host = host self.host = host
self._waiting_for_approval = {} #key = id, value = [transfer data, IdelayedCall Reactor timeout, self.managed_stream_m = [#self.host.plugins["XEP-0065"].NAMESPACE,
# current stream method, [failed stream methods], profile]
self.managed_stream_m = [self.host.plugins["XEP-0065"].NAMESPACE,
self.host.plugins["XEP-0047"].NAMESPACE] #Stream methods managed self.host.plugins["XEP-0047"].NAMESPACE] #Stream methods managed
self.host.plugins["XEP-0095"].registerSIProfile(PROFILE_NAME, self.transferRequest) self.host.plugins["XEP-0095"].registerSIProfile(PROFILE_NAME, self.transferRequest)
host.bridge.addMethod("sendFile", ".plugin", in_sign='ssa{ss}s', out_sign='s', method=self.sendFile) host.bridge.addMethod("sendFile", ".plugin", in_sign='ssa{ss}s', out_sign='s', method=self.sendFile)
def _kill_id(self, approval_id): def profileConnected(self, profile):
client = self.host.getClient(profile)
client._xep_0096_waiting_for_approval = {} #key = id, value = [transfer data, IdelayedCall Reactor timeout,
# current stream method, [failed stream methods], profile]
def _kill_id(self, approval_id, profile):
"""Delete a waiting_for_approval id, called after timeout """Delete a waiting_for_approval id, called after timeout
@param approval_id: id of _waiting_for_approval""" @param approval_id: id of _xep_0096_waiting_for_approval"""
info(_("SI File Transfer: TimeOut reached for id %s") % approval_id); info(_("SI File Transfer: TimeOut reached for id %s") % approval_id);
try: try:
del self._waiting_for_approval[approval_id] client = self.host.getClient(profile)
del client._xep_0096_waiting_for_approval[approval_id]
except KeyError: except KeyError:
warning(_("kill id called on a non existant approval id")) warning(_("kill id called on a non existant approval id"))
@ -78,6 +80,9 @@ class XEP_0096():
@param profile: %(doc_profile)s""" @param profile: %(doc_profile)s"""
info (_("XEP-0096 file transfer requested")) info (_("XEP-0096 file transfer requested"))
debug(si_el.toXml()) debug(si_el.toXml())
client = self.host.getClient(profile)
if not client:
raise ProfileNotInCacheError
filename = "" filename = ""
file_size = "" file_size = ""
file_date = None file_date = None
@ -124,9 +129,9 @@ class XEP_0096():
#if we are here, the transfer can start, we just need user's agreement #if we are here, the transfer can start, we just need user's agreement
data={ "filename":filename, "id": iq_id, "from":from_jid, "size":file_size, "date":file_date, "hash":file_hash, "desc":file_desc, "can_range": str(can_range) } data={ "filename":filename, "id": iq_id, "from":from_jid, "size":file_size, "date":file_date, "hash":file_hash, "desc":file_desc, "can_range": str(can_range) }
self._waiting_for_approval[si_id] = [data, reactor.callLater(300, self._kill_id, si_id), stream_method, [], profile] client._xep_0096_waiting_for_approval[si_id] = [data, reactor.callLater(300, self._kill_id, si_id, profile), stream_method, []]
self.host.askConfirmation(si_id, "FILE_TRANSFER", data, self.confirmationCB) self.host.askConfirmation(si_id, "FILE_TRANSFER", data, self.confirmationCB, profile)
def _getFileObject(self, dest_path, can_range = False): def _getFileObject(self, dest_path, can_range = False):
@ -136,12 +141,15 @@ class XEP_0096():
@return: File Object""" @return: File Object"""
return open(dest_path, "ab" if can_range else "wb") return open(dest_path, "ab" if can_range else "wb")
def confirmationCB(self, sid, accepted, frontend_data): def confirmationCB(self, sid, accepted, frontend_data, profile):
"""Called on confirmation answer """Called on confirmation answer
@param sid: file transfer session id @param sid: file transfer session id
@param accepted: True if file transfer is accepted @param accepted: True if file transfer is accepted
@param frontend_data: data sent by frontend""" @param frontend_data: data sent by frontend"""
data, timeout, stream_method, failed_methods, profile = self._waiting_for_approval[sid] client = self.host.getClient(profile)
if not client:
raise ProfileNotInCacheError
data, timeout, stream_method, failed_methods = client._xep_0096_waiting_for_approval[sid]
can_range = data['can_range'] == "True" can_range = data['can_range'] == "True"
range_offset = 0 range_offset = 0
if accepted: if accepted:
@ -151,19 +159,19 @@ class XEP_0096():
dest_path = frontend_data['dest_path'] dest_path = frontend_data['dest_path']
except KeyError: except KeyError:
error(_('dest path not found in frontend_data')) error(_('dest path not found in frontend_data'))
del(self._waiting_for_approval[sid]) del(client._xep_0096_waiting_for_approval[sid])
return return
if stream_method == self.host.plugins["XEP-0065"].NAMESPACE: if stream_method == self.host.plugins["XEP-0065"].NAMESPACE:
file_obj = self._getFileObject(dest_path, can_range) file_obj = self._getFileObject(dest_path, can_range)
range_offset = file_obj.tell() range_offset = file_obj.tell()
self.host.plugins["XEP-0065"].prepareToReceive(jid.JID(data['from']), sid, file_obj, int(data["size"]), self._transferSucceeded, self._transferFailed) self.host.plugins["XEP-0065"].prepareToReceive(jid.JID(data['from']), sid, file_obj, int(data["size"]), self._transferSucceeded, self._transferFailed, profile)
elif stream_method == self.host.plugins["XEP-0047"].NAMESPACE: elif stream_method == self.host.plugins["XEP-0047"].NAMESPACE:
file_obj = self._getFileObject(dest_path, can_range) file_obj = self._getFileObject(dest_path, can_range)
range_offset = file_obj.tell() range_offset = file_obj.tell()
self.host.plugins["XEP-0047"].prepareToReceive(jid.JID(data['from']), sid, file_obj, int(data["size"]), self._transferSucceeded, self._transferFailed) self.host.plugins["XEP-0047"].prepareToReceive(jid.JID(data['from']), sid, file_obj, int(data["size"]), self._transferSucceeded, self._transferFailed, profile)
else: else:
error(_("Unknown stream method, this should not happen at this stage, cancelling transfer")) error(_("Unknown stream method, this should not happen at this stage, cancelling transfer"))
del(self._waiting_for_approval[sid]) del(client._xep_0096_waiting_for_approval[sid])
return return
#we can send the iq result #we can send the iq result
@ -179,30 +187,37 @@ class XEP_0096():
else: else:
debug (_("Transfer [%s] refused"), sid) debug (_("Transfer [%s] refused"), sid)
self.host.plugins["XEP-0095"].sendRejectedError (data["id"], data['from'], profile=profile) self.host.plugins["XEP-0095"].sendRejectedError (data["id"], data['from'], profile=profile)
del(self._waiting_for_approval[sid]) del(client._xep_0096_waiting_for_approval[sid])
def _transferSucceeded(self, sid, file_obj, stream_method): def _transferSucceeded(self, sid, file_obj, stream_method, profile):
"""Called by the stream method when transfer successfuly finished """Called by the stream method when transfer successfuly finished
@param id: stream id""" @param id: stream id"""
client = self.host.getClient(profile)
if not client:
raise ProfileNotInCacheError
file_obj.close() file_obj.close()
info(_('Transfer %s successfuly finished') % sid) info(_('Transfer %s successfuly finished') % sid)
del(self._waiting_for_approval[sid]) del(client._xep_0096_waiting_for_approval[sid])
def _transferFailed(self, sid, file_obj, stream_method, reason): def _transferFailed(self, sid, file_obj, stream_method, reason, profile):
"""Called when something went wrong with the transfer """Called when something went wrong with the transfer
@param id: stream id @param id: stream id
@param reason: can be TIMEOUT, IO_ERROR, PROTOCOL_ERROR""" @param reason: can be TIMEOUT, IO_ERROR, PROTOCOL_ERROR"""
data, timeout, stream_method, failed_methods, profile = self._waiting_for_approval[sid] client = self.host.getClient(profile)
warning(_('Transfer %(id)s failed with stream method %(s_method)s') % { 'id': sid, if not client:
's_method': stream_method }) raise ProfileNotInCacheError
data, timeout, stream_method, failed_methods = client._xep_0096_waiting_for_approval[sid]
warning(_('Transfer %(id)s failed with stream method %(s_method)s: %(reason)s') % { 'id': sid,
's_method': stream_method,
'reason': reason})
filepath = file_obj.name filepath = file_obj.name
file_obj.close() file_obj.close()
os.remove(filepath) os.remove(filepath)
#TODO: session remenber (within a time limit) when a stream method fail, and avoid that stream method with full jid for the rest of the session #TODO: session remenber (within a time limit) when a stream method fail, and avoid that stream method with full jid for the rest of the session
warning(_("All stream methods failed, can't transfer the file")) warning(_("All stream methods failed, can't transfer the file"))
del(self._waiting_for_approval[sid]) del(client._xep_0096_waiting_for_approval[sid])
def fileCb(self, profile, filepath, sid, size, IQ): def fileCb(self, filepath, sid, size, profile, IQ):
if IQ['type'] == "error": if IQ['type'] == "error":
stanza_err = jab_error.exceptionFromStanza(IQ) stanza_err = jab_error.exceptionFromStanza(IQ)
if stanza_err.code == '403' and stanza_err.condition == 'forbidden': if stanza_err.code == '403' and stanza_err.condition == 'forbidden':
@ -252,7 +267,7 @@ class XEP_0096():
else: else:
warning(_("Invalid stream method received")) warning(_("Invalid stream method received"))
def sendFile(self, to_jid, filepath, data={}, profile_key='@DEFAULT@'): def sendFile(self, to_jid, filepath, data={}, profile_key='@NONE@'):
"""send a file using XEP-0096 """send a file using XEP-0096
@to_jid: recipient @to_jid: recipient
@filepath: absolute path to the file to send @filepath: absolute path to the file to send
@ -278,13 +293,13 @@ class XEP_0096():
file_transfer_elts.append(domish.Element((None,'range'))) file_transfer_elts.append(domish.Element((None,'range')))
sid, offer = self.host.plugins["XEP-0095"].proposeStream(jid.JID(to_jid), PROFILE, feature_elt, file_transfer_elts, profile_key = profile) sid, offer = self.host.plugins["XEP-0095"].proposeStream(jid.JID(to_jid), PROFILE, feature_elt, file_transfer_elts, profile_key = profile)
offer.addCallback(self.fileCb, profile, filepath, sid, size) offer.addCallback(self.fileCb, filepath, sid, size, profile)
return sid return sid
def sendSuccessCb(self, sid, file_obj, stream_method): def sendSuccessCb(self, sid, file_obj, stream_method, profile):
info(_('Transfer %s successfuly finished') % sid) info(_('Transfer %s successfuly finished [%s]') % (sid, profile))
file_obj.close() file_obj.close()
def sendFailureCb(self, sid, file_obj, stream_method, reason): def sendFailureCb(self, sid, file_obj, stream_method, reason, profile):
file_obj.close() file_obj.close()
warning(_('Transfer %(id)s failed with stream method %(s_method)s') % { 'id': sid, "s_method": stream_method }) warning(_('Transfer %(id)s failed with stream method %(s_method)s: %(reason)s [%(profile)s') % { 'id': sid, "s_method": stream_method, 'reason': reason, 'profile': profile })