class TestVCard(unittest.TestCase): def setUp(self): self.VC = VCard(dispatcher=dispatcherEmul('jid')) def test_init(self): hand=[(dispatcherEmul('jid'), '2'),] self.VC.init(handlers=hand) hand.insert(0, (MyVCardQuery, self.VC)) self.assertEqual(self.VC.dispatcher._handlers, hand) def test_get(self): to = 'somejid' result = self.VC.get(to) self.assertTrue(isinstance(result, Deferred)) result = self.VC.dispatcher.data[0] self.assertEqual(result.type_, 'get') self.assertEqual(result.to, JID(to)) self.assertEqual(result.from_, JID('jid')) self.assertTrue(isinstance(result, Iq)) def test_set(self): card = VCardQuery(full_name='John') result = self.VC.set(card) self.assertTrue(isinstance(result, Deferred)) result = self.VC.dispatcher.data[0] self.assertEqual(result.type_, 'set') self.assertTrue(isinstance(result, Iq))
class jtranslateComponent(TwilixComponent): """ Master class for the jabber-translate service. """ def __init__(self, version, jid, conf): """ Sets info about translate service. :param version: version of your service. :param jid: jid of your service. :param conf: instance of ConfigParser that binds configuration file. """ TwilixComponent.__init__(self, jid) self.VERSION = version self.startTime = None def init(self): """ Method initializing all needed services and handlers. """ self.startTime = time.time() self.disco = Disco(self.dispatcher) self.version = ClientVersion(self.dispatcher, 'jabber translate service', self.VERSION, 'Linux') self.version.init(self.disco) self.myvcard = VCardQuery(nickname='jtranslate', jid=self.myjid, description='\ Jabber translate service') self.vcard = VCard(self.dispatcher, myvcard=self.myvcard) self.vcard.init(self.disco) api = MultitranAPI() items = [DiscoItem(jid=u'%s@%s' % (code, self.myjid), iname='Translate to %s'%lang) for code, lang in api.get_languages().items()] self.disco.root_items.addItems(items) def validate(code): if code in api.get_languages(): return True return False self.subscription = Subscription(self.dispatcher, validate) self.subscription.init() self.mtrans = MessageTranslation(self.dispatcher, api) self.mtrans.init() self.disco.init() print 'Connected!'
def init(self): """ Method initializing all needed services and handlers. """ self.startTime = time.time() self.disco = Disco(self.dispatcher) self.version = ClientVersion(self.dispatcher, 'jabber translate service', self.VERSION, 'Linux') self.version.init(self.disco) self.myvcard = VCardQuery(nickname='jtranslate', jid=self.myjid, description='\ Jabber translate service') self.vcard = VCard(self.dispatcher, myvcard=self.myvcard) self.vcard.init(self.disco) api = MultitranAPI() items = [DiscoItem(jid=u'%s@%s' % (code, self.myjid), iname='Translate to %s'%lang) for code, lang in api.get_languages().items()] self.disco.root_items.addItems(items) def validate(code): if code in api.get_languages(): return True return False self.subscription = Subscription(self.dispatcher, validate) self.subscription.init() self.mtrans = MessageTranslation(self.dispatcher, api) self.mtrans.init() self.disco.init() print 'Connected!'
def init(self): """ Method initializing all needed services and handlers. """ self.startTime = time.time() self.dispatcher.registerHandler((Presence, self)) self.dispatcher.registerHandler((Message, self)) self.dispatcher.registerHandler((SubscrHandler, self)) self.dispatcher.registerHandler((PresenceHandler, self)) self.disco = Disco(self.dispatcher) self.version = ClientVersion(self.dispatcher, 'j2j transport', self.VERSION, 'Linux') self.version.init(self.disco) self.myvcard = VCardQuery(nickname='j2j', jid=self.myjid, description='\ Jabber to jabber gateway') self.vcard = VCard(self.dispatcher, myvcard=self.myvcard) self.vcard.init(self.disco) self.register = Register(self.dispatcher) self.register.init((RegisterHandler, self), self.disco) self.gateway = ClientGateway(self.dispatcher, 'Please enter the Jabber Screen Name of \ the person you would like to contact', 'Contact ID') self.gateway.init(self.disco) self.disco.init() print 'Connected!'
def componentConnected(self, xs): self.startTime = time.time() self.xmlstream = xs self.dispatcher = Dispatcher(xs, self.cJid) self.dispatcher.registerHandler((MyPresence, self)) self.dispatcher.registerHandler((Message, self)) self.version = ClientVersion(self.dispatcher, 'Google Weather transport', self.VERSION, 'Linux') self.version.init(handlers=((WeatherVersionQuery, self.version),)) self.myvcard = VCardQuery(nickname='gweather', jid=self.cJid, description='\ Google Weather XMPP translation service') self.vcard = VCard(self.dispatcher, myvcard=self.myvcard) self.vcard.init(handlers=((WeatherVCardQuery, self.vcard),)) self.getOnline() self.lc = task.LoopingCall(self.updateStatus) self.lc.start(900) print 'Connected'
def setUp(self): self.VC = VCard(dispatcher=dispatcherEmul('jid'))
class WeatherComponent(component.Service): def __init__(self, reactor, version, config, cJid): self.config = config self.reactor = reactor self.VERSION = version self.cJid = internJID(cJid) self.startTime = None self.xmlstream = None self.subscribed = SubscribedList(config) self.wbase = WeatherBase() self.online = [] def componentConnected(self, xs): self.startTime = time.time() self.xmlstream = xs self.dispatcher = Dispatcher(xs, self.cJid) self.dispatcher.registerHandler((MyPresence, self)) self.dispatcher.registerHandler((Message, self)) self.version = ClientVersion(self.dispatcher, 'Google Weather transport', self.VERSION, 'Linux') self.version.init(handlers=((WeatherVersionQuery, self.version),)) self.myvcard = VCardQuery(nickname='gweather', jid=self.cJid, description='\ Google Weather XMPP translation service') self.vcard = VCard(self.dispatcher, myvcard=self.myvcard) self.vcard.init(handlers=((WeatherVCardQuery, self.vcard),)) self.getOnline() self.lc = task.LoopingCall(self.updateStatus) self.lc.start(900) print 'Connected' def addSubscr(self, from_, to): self.subscribed.add_subscr(from_, to) def rmSubscr(self, from_, to): self.subscribed.rm_subscr(from_, to) def addOnlinesubscr(self, from_, to): if (from_, to) not in self.online: self.online.append((from_, to)) def rmOnlinesubscr(self, from_, to): if (from_, to) in self.online: self.online.remove((from_, to)) def getOnline(self): for from_, to in self.subscribed.subscr_list: reply = Presence( to=from_, from_=to, type_='probe', ) self.dispatcher.send(reply) def updateStatus(self): for from_, to in self.online: deff = self.wbase.get_condition(to.user) deff.addCallback(self._result, from_, to) def _result(self, respond, from_, to): reply = Presence( to=from_, from_=to, status=respond, ) self.dispatcher.send(reply)
class j2jComponent(TwilixComponent): """ Master class for the jabber-to-jabber service. """ def __init__(self, version, jid, basepath, basetype): """ Sets info about transport. :param version: version of your transport. :param jid: jid of your transport. :param basepath: path to your users database. :param basetype: type of your users database """ TwilixComponent.__init__(self, jid) self.config = config self.VERSION = version self.startTime = None self.basetype = basetype _tmp = __import__('%sBase' % basetype, globals(), locals(), ['UserBase'], -1) self.dbase = _tmp.UserBase(basepath) self.pool = ClientPool() def init(self): """ Method initializing all needed services and handlers. """ self.startTime = time.time() self.dispatcher.registerHandler((Presence, self)) self.dispatcher.registerHandler((Message, self)) self.dispatcher.registerHandler((SubscrHandler, self)) self.dispatcher.registerHandler((PresenceHandler, self)) self.disco = Disco(self.dispatcher) self.version = ClientVersion(self.dispatcher, 'j2j transport', self.VERSION, 'Linux') self.version.init(self.disco) self.myvcard = VCardQuery(nickname='j2j', jid=self.myjid, description='\ Jabber to jabber gateway') self.vcard = VCard(self.dispatcher, myvcard=self.myvcard) self.vcard.init(self.disco) self.register = Register(self.dispatcher) self.register.init((RegisterHandler, self), self.disco) self.gateway = ClientGateway(self.dispatcher, 'Please enter the Jabber Screen Name of \ the person you would like to contact', 'Contact ID') self.gateway.init(self.disco) self.disco.init() print 'Connected!' def componentDisconnected(self): """ If type of your database is 'shelve', this method will close your database properly. """ if self.basetype == 'shelve': self.dbase.close()