def test_authorized_user_parsing(self): """ test the base authorized user code """ b = bot.BenderJab() user1 = util.toJID('*****@*****.**') user2 = util.toJID('*****@*****.**') baduser = util.toJID('*****@*****.**') # by default authorize everything self.failUnlessEqual(b.check_authorization(user1), True) self.failUnlessEqual(b.check_authorization(user2), True) self.failUnlessEqual(b.check_authorization(baduser), True) # empty list is deny everyone b.authorized_users = [] self.failUnlessEqual(b.check_authorization(user1), False) self.failUnlessEqual(b.check_authorization(user2), False) self.failUnlessEqual(b.check_authorization(baduser), False) # now make sure user_list = "[email protected] [email protected]" b.authorized_users = b._parse_user_list(user_list) self.failUnlessEqual(b.check_authorization(user1), True) self.failUnlessEqual(b.check_authorization(user2), True) self.failUnlessEqual(b.check_authorization(baduser), False)
def disco_info(args): if not args.who: return self.subparser.format_usage() who = toJID(args.who) q = xmpp.Iq(typ="get", queryNS="http://jabber.org/protocol/disco#items", to=who) if args.node: q.setTagAttr("query", "node", args.node[0]) args.bot.cl.send(q) return "Sent items query"
def disco_info(args): if not args.who: return self.subparser.format_usage() who = toJID(args.who) q = xmpp.Iq(typ="get", queryNS='http://jabber.org/protocol/disco#items', to=who) if args.node: q.setTagAttr('query', 'node', args.node[0]) args.bot.cl.send(q) return "Sent items query"
def make_iq(tojid, typ, marshaled, msgid=None): """ Wrap XML-RPC marshaled data in an XMPP jabber:iq:rpc message """ tojid = toJID(tojid) iq = xmpp.Iq(typ=typ, to=tojid, xmlns=None) if msgid is not None: iq.setID(msgid) query = iq.addChild('query', namespace=xmpp.NS_RPC) query.addChild(node=simplexml.XML2Node(marshaled)) return iq
def test_authorized_message(self): b = bot.BenderJab() b.jid = "*****@*****.**" user_list = "[email protected] [email protected]" b.authorized_users = b._parse_user_list(user_list) b.cl = MockClient() b.configure_logging() fromjid = util.toJID('*****@*****.**') tojid = util.toJID('*****@*****.**') body = "test message" msg=xmpp.protocol.Message(tojid,body=body,typ='chat', frm=fromjid) b.messageCB(b.cl, msg) response = b.cl.msgs[-1].getBody() valid = 'I have no idea what "test message" means.' self.failUnlessEqual(valid, response) fromjid = util.toJID('*****@*****.**') msg=xmpp.protocol.Message(tojid,body=body,typ='chat', frm=fromjid) b.messageCB(b.cl, msg) response = b.cl.msgs[-1].getBody() self.failUnlessEqual('Authorization Error.', response)
def disco_info(args): if not args.who: return self.subparser.format_usage() who = toJID(args.who) q = xmpp.Iq(typ="get", queryNS="http://jabber.org/protocol/disco#info", to=who) if args.node: q.setTagAttr("query", "node", args.node[0]) tree = fromstring(str(q)) print("--disco-info--") dump(tree) args.bot.cl.send(q) return "Sent info query"
def test_authorized_message(self): b = bot.BenderJab() b.jid = "*****@*****.**" user_list = "[email protected] [email protected]" b.authorized_users = b._parse_user_list(user_list) b.cl = MockClient() b.configure_logging() fromjid = util.toJID('*****@*****.**') tojid = util.toJID('*****@*****.**') body = "test message" msg = xmpp.protocol.Message(tojid, body=body, typ='chat', frm=fromjid) b.messageCB(b.cl, msg) response = b.cl.msgs[-1].getBody() valid = 'I have no idea what "test message" means.' self.failUnlessEqual(valid, response) fromjid = util.toJID('*****@*****.**') msg = xmpp.protocol.Message(tojid, body=body, typ='chat', frm=fromjid) b.messageCB(b.cl, msg) response = b.cl.msgs[-1].getBody() self.failUnlessEqual('Authorization Error.', response)
def _parse_address(self, address): if type(address) in str: if address.startswith(MAILTO_PROTO): return MAILTO_PROTO, address[len(MAILTO_PROTO):] elif address.startswith(JABBER_PROTO): return JABBER_PROTO, util.toJID(address[len(JABBER_PROTO):]) else: return JABBER_PROTO, address elif isinstance(address, xmpp.JID): return JABBER_PROTO, address else: self.log.error("Unrecognized address %s" % (str(address))) return None, None
def disco_info(args): if not args.who: return self.subparser.format_usage() who = toJID(args.who) q = xmpp.Iq(typ="get", queryNS='http://jabber.org/protocol/disco#info', to=who) if args.node: q.setTagAttr('query', 'node', args.node[0]) tree = fromstring(str(q)) print("--disco-info--") dump(tree) args.bot.cl.send(q) return "Sent info query"
def logon(self, jid=None, password=None, resource=None): """connect to server""" if jid is not None: self.cfg['jid'] = util.toJID(jid) if password is not None: self.cfg['password'] = password if resource is not None: self.cfg['resource'] = resource if self.cfg['jid'] is None: raise ValueError("please set a jabber ID before logging in") if self.cfg['password'] is None: raise ValueError("please set a password before logging in") jid = util.toJID(self.cfg['jid']) self.cl = xmpp.Client(jid.getDomain(), debug=[]) # if you have dnspython installed and use_srv is True # the dns service discovery lookup seems to fail. self.cl.connect(use_srv=False) auth_state = self.cl.auth(jid.getNode(), self.cfg['password'], self.cfg['resource']) if auth_state is None: # auth failed self.log.error("couldn't authenticate with"+str(self.jid)) # probably want a better exception here raise RuntimeError(self.cl.lastErr) # tell the xmpp client that we're ready to handle things self.cl.RegisterHandler('message', self.messageCB) self.cl.RegisterHandler('presence', self.presenceCB) # announce our existence to the server self.cl.getRoster() self.cl.sendInitPresence() # not needed but lets me muck around with the client from interpreter return self.cl
def _parse_user_list(self, user_list, require_resource=False): """ Convert a space separated list of users into a list of JIDs """ if user_list is None: return None parsed_list = [] for user in user_list.split(): proto, address = self._parse_address(user) if proto == MAILTO_PROTO: parsed_list.append(address) elif proto == JABBER_PROTO: jid = util.toJID(address) if require_resource and len(jid.resource) == 0: msg = 'need a resource identifier for the Jabber ID' raise JIDMissingResource(msg) parsed_list.append(jid) return parsed_list
def connect(profile=None): """ Connect to the server for our jabber id """ jidparams = get_config(profile) # if we have no credentials, don't bother logging in if jidparams is None: return myjid=toJID(jidparams['jid']) # construct a client instance, logging into the JIDs servername # xmpp's default debug didn't work when I started using it cl=xmpp.Client(myjid.getDomain(),debug=[]) connection_type = '' connection_tries = 3 # if use_srv is true, xmpp will try to use dnspython to look up # the right server via a DNS SRV request, this doesn't work right # for my server while connection_type == '' and connection_tries > 0: connection_type = cl.connect(use_srv=False) # wait a random length of time between 2.5 and 7.5 seconds # if we didn't manage to connect if connection_type == '': time.sleep( 5 + (random.random()*5 - 2.5)) # connection failed if connection_type == '': raise IOError("unable to connect to" + str(cl.Server)) # try logging in if cl.auth(myjid.getNode(),jidparams['password'], 'xsend') is None: raise IOError("Couldn't auth:"+str(cl.lastErr)) return cl
def _set_jid(self, jid): if self.cl is None: self.cfg['jid'] = util.toJID(jid) else: raise ValueError("Already logged in, can't change jabber ID")
def _get_jid(self): if self.cfg['jid']: return util.toJID(self.cfg['jid'])