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 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)