def main(self): yield self.initComponents() while 1: while self.dataReady("control"): mes = self.recv("control") if isinstance(mes, shutdownMicroprocess) or isinstance(mes, producerFinished): self.send(producerFinished(), "signal") break while self.dataReady("jid"): self.from_jid = self.recv('jid') while self.dataReady("inbox"): m = self.recv("inbox") # in this first case, we want to send the message # typed in the console. # The message is of the form: # contant_jid message if isinstance(m, (str, unicode)) and m != '': #print self.name + ' received string!' try: contact_jid, message = m.split(' ', 1) except ValueError: print "Messages format: contact_jid message" continue m = Message(unicode(self.from_jid), unicode(contact_jid), type=u'chat', stanza_id=generate_unique()) m.event = Event.composing # note the composing event status m.bodies.append(Body(unicode(message))) self.send(m, "outbox") # Right after we sent the first message # we send another one reseting the event status m = Message(unicode(self.from_jid), unicode(contact_jid), type=u'chat', stanza_id=generate_unique()) self.send(m, "outbox") # In this case we actually received a message # from a contact, we print it. elif isinstance(m, Message): #b_list = [str(body) for body in m.bodies] #abody = ''.join(b_list) #print 'body = ', abody self.send(m, 'proto') else: print 'unknown instance received!' if not self.anyReady(): self.pause() yield 1
def main(self): while 1: while self.dataReady("control"): mes = self.recv("control") if isinstance(mes, shutdownMicroprocess) or isinstance(mes, producerFinished): self.send(producerFinished(), "signal") break while self.dataReady("inbox"): r = self.recv('inbox') if r.registered: print "'%s' is already a registered username." % self.username elif self.registration_id == r.stanza_id: print "'%s' is now a registered user."\ "Please restart the client without the register flag." % self.username else: if 'username' in r.infos and 'password' in r.infos: self.registration_id = generate_unique() r = Registration(type=u'set', stanza_id=self.registration_id) r.infos[u'username'] = self.username r.infos[u'password'] = self.password self.send(r, 'outbox') while self.dataReady("error"): r = self.recv('error') print r.error if not self.anyReady(): self.pause() yield 1
def fetch(self, e): stanza_id = generate_unique() iq = Stanza.get_iq(self.client.jid, u"firehoser.superfeedr.com", stanza_id=stanza_id) pubsub = E(u"pubsub", namespace=XMPP_PUBSUB_NS, parent=iq) A(u"superfeedr", prefix=u"xmlns", namespace=XMLNS_NS, parent=pubsub, value=u"http://superfeedr.com/xmpp-pubsub-ext") sub = E(u"subscriptions", attributes={u"jid": unicode(self.client.jid)}, parent=pubsub, namespace=XMPP_PUBSUB_NS) A(u"page", value=u"1", prefix=u"superfeedr", parent=sub, namespace=u"http://superfeedr.com/xmpp-pubsub-ext") self.client.register_on_iq(self.subscriptions, type="result", id=stanza_id, once=True) self.client.send_stanza(iq)
def unsubscribe(self, url=URL): stanza_id = generate_unique() iq = Stanza.set_iq(self.client.jid, u"firehoser.superfeedr.com", stanza_id=stanza_id) pubsub = E(u"pubsub", namespace=XMPP_PUBSUB_NS, parent=iq) A(u"superfeedr", prefix=u"xmlns", namespace=XMLNS_NS, parent=pubsub, value=u"http://superfeedr.com/xmpp-pubsub-ext") E(u"unsubscribe", attributes={ u"jid": self.client.jid.nodeid(), u"node": url }, parent=pubsub, namespace=XMPP_PUBSUB_NS) self.client.register_on_iq(self.subscribe, type="result", id=stanza_id, once=True) self.client.send_stanza(iq)
def main(self): while 1: if self.dataReady("control"): mes = self.recv("control") if isinstance(mes, shutdownMicroprocess) or isinstance(mes, producerFinished): self.send(producerFinished(), "signal") break if self.dataReady("inbox"): r = self.recv('inbox') if r.registered: print "'%s' is already a registered username." % self.username elif self.registration_id == r.stanza_id: print "'%s' is now a registered user."\ "Please restart the client without the register flag." % self.username else: if 'username' in r.infos and 'password' in r.infos: self.registration_id = generate_unique() r = Registration(type=u'set', stanza_id=self.registration_id) r.infos[u'username'] = self.username r.infos[u'password'] = self.password self.send(r, 'outbox') if self.dataReady("error"): r = self.recv('error') print r.error if not self.anyReady(): self.pause() yield 1
def unregister(self): stanza_id = generate_unique() iq = Stanza.set_iq(stanza_id=stanza_id) query = E(u'query', namespace=XMPP_IBR_NS, parent=iq) E(u'remove', namespace=XMPP_IBR_NS, parent=query) self.client.register_on_iq(self.unregistered, type=u'result', id=stanza_id, once=True) self.client.send_stanza(iq)
def __init__(self, name, from_jid=None, to_jid=None, type=None, stanza_id=None, lang=None): self.stanza = name self.from_jid = from_jid self.to_jid = to_jid self.type = type self.stanza_id = stanza_id or generate_unique() self.lang = lang self.ns = XMPP_CLIENT_NS
def ask_roster(self): """ Creates and returns the IQ stanza to query the entity's roster. """ iq = Stanza.get_iq(from_jid=unicode(self.jid), stanza_id=generate_unique()) E(u'query', namespace=XMPP_ROSTER_NS, parent=iq) return iq
def _handle_binding(self, e): self.log(e) iq = Iq.create_set_iq(stanza_id=generate_unique()) bind = E(u'bind', namespace=XMPP_BIND_NS, parent=iq) if self.jid.resource != None: E(u'resource', content=self.jid.resource, namespace=XMPP_BIND_NS, parent=bind) self.propagate(element=iq)
def main(self): yield self.initComponents() while 1: if self.dataReady("control"): mes = self.recv("control") if isinstance(mes, shutdownMicroprocess) or isinstance(mes, producerFinished): self.send(producerFinished(), "signal") break if self.dataReady("_response"): self.recv("_response") if self.dataReady("inbox"): r = self.recv('inbox') if r.registered: self.send("'%s' is already a registered username." % r.infos[u'username'], 'log') c = Client.Sessions[r.infos[u'username']] c.shutdown() del Client.Sessions[r.infos[u'username']] elif self.registration_id == r.stanza_id: c = Client.Sessions[r.infos[u'username']] c.shutdown() del Client.Sessions[r.infos[u'username']] if '.microblogging' not in r.infos[u'username']: body = self.profile.xml() params = {'url': 'http://localhost:8080/profile/', 'method': 'POST', 'postbody': body, 'extraheaders': {'content-type': 'application/xml', 'slug': self.profile.username, 'content-length': len(body)}} self.send(params, '_request') else: if 'username' in r.infos and 'password' in r.infos: self.registration_id = generate_unique() r = Registration(type=u'set', stanza_id=self.registration_id) r.infos[u'username'] = self.username r.infos[u'password'] = self.password self.send(r, 'outbox') if self.dataReady("error"): r = self.recv('error') if r.error.code == '409': self.send("'%s' is already a registered username." % r.infos[u'username'], 'log') c = Client.Sessions[r.infos[u'username']] c.shutdown() del Client.Sessions[r.infos[u'username']] self.send(r.error, 'log') if not self.anyReady(): self.pause() yield 1
def handle_binding(self, e): """ Handle the JID binding request by returning the full JID. """ iq = Stanza.set_iq(stanza_id=generate_unique()) bind = E(u'bind', namespace=XMPP_BIND_NS, parent=iq) if self.jid.resource != None: E(u'resource', content=self.jid.resource, namespace=XMPP_BIND_NS, parent=bind) return iq
def register(self, e): """ Handler called when the `<register xmlns="http://jabber.org/features/iq-register" />` stanza is received and the client required the registration process. Returns a stanza indicating the client is indeed requesting the registration process from the server. ``e`` :class:`bridge.Element` instance representing the dispatched stanza """ iq = Stanza.get_iq(stanza_id=generate_unique()) E(u'register', namespace=XMPP_IBR_NS, parent=iq) return iq
def unsubscribe(self, url=URL): stanza_id = generate_unique() iq = Stanza.set_iq(self.client.jid, u"firehoser.superfeedr.com", stanza_id=stanza_id) pubsub = E(u"pubsub", namespace=XMPP_PUBSUB_NS, parent=iq) A(u"superfeedr", prefix=u"xmlns", namespace=XMLNS_NS, parent=pubsub, value=u"http://superfeedr.com/xmpp-pubsub-ext") E(u"unsubscribe", attributes={u"jid": self.client.jid.nodeid(), u"node": url}, parent=pubsub, namespace=XMPP_PUBSUB_NS) self.client.register_on_iq(self.subscribe, type="result", id=stanza_id, once=True) self.client.send_stanza(iq)
def _handle_jid(self, e): self.log(e) self.jid = JID.parse(e.xml_text) self.status = ACTIVE self.send(self.jid, 'jid') # Sends the initial presence information to the server self.propagate(element=Stanza.to_element(Stanza(u'presence'))) # Asks immediatly for the client's roster list iq = Iq.create_get_iq(from_jid=unicode(self.jid), stanza_id=generate_unique()) E(u'query', namespace=XMPP_ROSTER_NS, parent=iq) self.propagate(element=iq)
def main(self): yield self.initComponents() while 1: if self.dataReady("control"): mes = self.recv("control") if isinstance(mes, shutdownMicroprocess) or isinstance(mes, producerFinished): self.send(producerFinished(), "signal") break if self.dataReady("jid"): self.from_jid = self.recv('jid') if self.dataReady("roster-received"): self.roster = self.recv("roster-received") if self.dataReady("inbox"): m = self.recv("inbox") if isinstance(m, str) and m != '': pass # In this case we actually received a message # from a contact, we print it. elif isinstance(m, Message): for body in m.bodies: message = remove_BOM(body.plain_body).strip() print "Received message: %s" % repr(message) try: action, data = message.split(' ', 1) except ValueError: action = 'PI' data = message if action in self.outboxes: self.send(data, action) if self.roster: for nodeid in self.roster.items: m = Message(unicode(self.from_jid), unicode(nodeid), type=u'chat', stanza_id=generate_unique()) m.event = Event.composing m.bodies.append(Body(unicode(data))) self.send(m, "outbox") if not self.anyReady(): self.pause() yield 1
def handle_session(self, e): """ Handles the session elements received by the server. If the type of the response is `result` it raises :class:`headstock.error.HeadstockAuthenticationSuccess` instance handled by the client indicating the session is ready. Otherwise returns the session stanza indicating the client wishes to start a session. """ if e.xml_parent and e.xml_parent.get_attribute_value('type') == 'result': raise HeadstockSessionBound() iq = Stanza.set_iq(stanza_id=generate_unique()) E(u'session', namespace=XMPP_SESSION_NS, parent=iq) return iq
def handle_session(self, e): """ Handles the session elements received by the server. If the type of the response is `result` it raises :class:`headstock.error.HeadstockAuthenticationSuccess` instance handled by the client indicating the session is ready. Otherwise returns the session stanza indicating the client wishes to start a session. """ if e.xml_parent and e.xml_parent.get_attribute_value( 'type') == 'result': raise HeadstockSessionBound() iq = Stanza.set_iq(stanza_id=generate_unique()) E(u'session', namespace=XMPP_SESSION_NS, parent=iq) return iq
def main(self): yield self.initComponents() while 1: if self.dataReady("control"): mes = self.recv("control") if isinstance(mes, shutdownMicroprocess) or isinstance(mes, producerFinished): self.send(producerFinished(), "signal") break if self.dataReady("jid"): self.from_jid = self.recv('jid') if self.dataReady("pushed"): roster = self.recv('pushed') for nodeid in roster.items: self.send(Roster(from_jid=self.from_jid, to_jid=nodeid, type=u'result', stanza_id=generate_unique()), 'result') if self.dataReady("inbox"): roster = self.recv("inbox") self.roster = roster print "Your contacts:" for nodeid in roster.items: contact = roster.items[nodeid] print " ", contact.jid if self.dataReady('ask-activity'): self.recv('ask-activity') if self.roster: for nodeid in self.roster.items: contact = roster.items[nodeid] a = Activity(unicode(self.from_jid), unicode(contact.jid)) self.send(a, 'activity') if not self.anyReady(): self.pause() yield 1
def main(self): yield self.initComponents() while 1: while self.dataReady("control"): mes = self.recv("control") if isinstance(mes, shutdownMicroprocess) or isinstance(mes, producerFinished): self.send(producerFinished(), "signal") break while self.dataReady("jid"): self.from_jid = self.recv('jid') while self.dataReady("pushed"): roster = self.recv('pushed') for nodeid in roster.items: self.send(Roster(from_jid=self.from_jid, to_jid=nodeid, type=u'result', stanza_id=generate_unique()), 'result') while self.dataReady("inbox"): roster = self.recv("inbox") self.roster = roster print "Your contacts:" for nodeid in roster.items: contact = roster.items[nodeid] print " ", contact.jid while self.dataReady('ask-activity'): self.recv('ask-activity') if self.roster: for nodeid in self.roster.items: contact = roster.items[nodeid] a = Activity(unicode(self.from_jid), unicode(contact.jid)) self.send(a, 'activity') if not self.anyReady(): self.pause() yield 1
def __init__(self, atompub, username, password, domain, resource=u"headstock-client1", server=u'localhost', port=5222, usetls=False, register=False, session_id=None, profile=None): super(Client, self).__init__() self.running = False self.connected = False self.atompub = atompub if not session_id: session_id = generate_unique() self.session_id = session_id self.backplanes = [] self.username = username self.password = password self.jid = JID(self.username, domain, '%s!%s' % (resource, session_id)) self.server = server self.port = port self.client = None self.graph = None self.domain = domain self.usetls = usetls self.register = register self.restartable = False self.profile = profile
def _handle_session(self, e): self.log(e) self.status = BOUND iq = Iq.create_set_iq(stanza_id=generate_unique()) session = E(u'session', namespace=XMPP_SESSION_NS, parent=iq) self.propagate(element=iq)
def __init__(self, from_jid, to_jid, type=u"get", stanza_id=None, seconds=None, message=None): Entity.__init__(self, from_jid, to_jid) self.seconds = seconds self.message = message self.type = type self.stanza_id = stanza_id or generate_unique()
def send_message(self, jid, text): m = E(u"message", attributes={u'from': unicode(self.client.jid), u'to': unicode(jid), u'type': u'chat', u'id': generate_unique()}, namespace=XMPP_CLIENT_NS) E(u'body', content=text, namespace=XMPP_CLIENT_NS, parent=m) self.client.send_stanza(m)
def register(self, e): iq = Stanza.get_iq(stanza_id=generate_unique()) E(u'register', namespace=XMPP_IBR_NS, parent=iq) self.client.send_stanza(iq)
def __init__(self, from_jid=None, to_jid=None, type=u'get', stanza_id=None): self.from_jid = from_jid self.to_jid = to_jid self.type = type self.stanza_id = stanza_id or generate_unique() self.error = None