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