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 onAuthenticated(self, xs): """ xmlstream.STREAM_AUTHD_EVENT handler. Calls when client authenticated on server. Setup dispatcher and any features for client """ self.dispatcher = Dispatcher(xs, self.client_jid) plugins.register(self.dispatcher, self) self.disco = Disco(self.dispatcher) self.disco.init() p = Presence(status="Use me plz!") self.roster = Roster(self.dispatcher, p) self.roster.init() self.version = ClientVersion(self.dispatcher, "XmppBot", 'v%s' % version, 'Linux') self.version.init(self.disco) #set handlers for roster's signals dispatcher.connect(self.onSubscribe, self.roster.subscribe) dispatcher.connect(self.onRosterGot, self.roster.roster_got) dispatcher.connect(self.onAvailable, self.roster.resource_available) dispatcher.connect(self.onUnvailable, self.roster.resource_unavailable)
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!'
class Client(object): """main class for client to server connection""" def __init__(self, reactor, client_jid, server, secret, port): """Setup handler and connect to server""" self.reactor = reactor self.client_jid = client_jid a = twisted_client.XMPPAuthenticator(client_jid, secret) self.f = XmlStreamFactory(a) #set handlers for xmlstream's events self.f.addBootstrap(xmlstream.STREAM_CONNECTED_EVENT, self.onConnected) self.f.addBootstrap(xmlstream.STREAM_END_EVENT, self.onDisconnected) self.f.addBootstrap(xmlstream.STREAM_AUTHD_EVENT, self.onAuthenticated) self.f.addBootstrap(xmlstream.INIT_FAILED_EVENT, self.onInitFailed) self.connector = XMPPClientConnector(reactor, server, self.f, port) self.connector.connect() self.xmlstream = None #handlers for xmlstream's events def onConnected(self, xs): """ xmlstream.STREAM_CONNECTED_EVENT handler. Calls when client connected to server """ self.xmlstream = xs self.xmlstream.rawDataInFn = self.rawIn self.xmlstream.rawDataOutFn = self.rawOut def rawIn(self,data): """data is the input stanza""" pass def rawOut(self,data): """data is the output stanza""" pass def onDisconnected(self, xs): """ xmlstream.STREAM_END_EVENT handler. Calls when client disconnected from server """ pass def onAuthenticated(self, xs): """ xmlstream.STREAM_AUTHD_EVENT handler. Calls when client authenticated on server. Setup dispatcher and any features for client """ self.dispatcher = Dispatcher(xs, self.client_jid) plugins.register(self.dispatcher, self) self.disco = Disco(self.dispatcher) self.disco.init() p = Presence(status="Use me plz!") self.roster = Roster(self.dispatcher, p) self.roster.init() self.version = ClientVersion(self.dispatcher, "XmppBot", 'v%s' % version, 'Linux') self.version.init(self.disco) #set handlers for roster's signals dispatcher.connect(self.onSubscribe, self.roster.subscribe) dispatcher.connect(self.onRosterGot, self.roster.roster_got) dispatcher.connect(self.onAvailable, self.roster.resource_available) dispatcher.connect(self.onUnvailable, self.roster.resource_unavailable) def onInitFailed(self, xs): """ xmlstream.STREAM_INIT_FAILED_EVENT handler. Calls when client authenticated on server was failed. """ pass #handlers for roster's signals def onAvailable(self, sender, item, presence): """roster.resourse_available handler.""" pass def onUnvailable(self, sender, item, presence): """roster.resourse_unavailable handler.""" pass def onRosterGot(self, sender): """roster.roster_got handler.""" pass def onSubscribe(self, sender, presence): """roster.subscribe handler.""" presence.type_ = 'subscribed' presence.to = presence.from_ presence.from_ = None self.dispatcher.send(presence) presence.type_ = 'subscribe' self.dispatcher.send(presence)
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()