def resultReceived(result): items = [item.attributes for item in result.query.children] if items[0].has_key('node'): for item in reversed(items): iq = IQ(client.admin.xmlstream, 'get') iq['to'] = getXMPPDomain(site) query = iq.addElement((NS_DISCO_ITEMS, 'query')) query['node'] = item['node'] iq.send().addCallbacks(resultReceived) else: subscribe_jids = [item['jid'] for item in items] if settings.admin_jid in subscribe_jids: subscribe_jids.remove(settings.admin_jid) if subscribe_jids: getJID = lambda uid: JID( "%s@%s" % (escapeNode(uid), settings.xmpp_domain)) roster_jids = [getJID(user_id.split('@')[0]) for user_id in subscribe_jids] for member_jid in member_jids: client.chat.sendRosterItemAddSuggestion(member_jid, roster_jids, site) log.info('Roster suggestion sent for %s' % member_jid) # XXX: Somehow the last user's roster suggestions is # dropped, unless we rest here for a bit. time.sleep(3) return result
def resultReceived(result): items = [item.attributes for item in result.query.children] if items[0].has_key('node'): for item in reversed(items): iq = IQ(client.admin.xmlstream, 'get') iq['to'] = getXMPPDomain(site) query = iq.addElement((NS_DISCO_ITEMS, 'query')) query['node'] = item['node'] iq.send().addCallbacks(resultReceived) else: subscribe_jids = [item['jid'] for item in items] if settings.admin_jid in subscribe_jids: subscribe_jids.remove(settings.admin_jid) if subscribe_jids: getJID = lambda uid: JID("%s@%s" % (escapeNode(uid), settings.xmpp_domain)) roster_jids = [ getJID(user_id.split('@')[0]) for user_id in subscribe_jids ] for member_jid in member_jids: client.chat.sendRosterItemAddSuggestion( member_jid, roster_jids, site) log.info('Roster suggestion sent for %s' % member_jid) # XXX: Somehow the last user's roster suggestions is # dropped, unless we rest here for a bit. time.sleep(3) return result
def getRegisteredUsers(self, portal=None): """ XXX: This is ejabberd specific. ejabberd does not implement the #get-registered-users-list command, instead does it with an iq/get. """ iq = IQ(self.xmlstream, 'get') iq['to'] = users.getXMPPDomain(portal) query = iq.addElement((NS_DISCO_ITEMS, 'query')) query['node'] = 'all users' d = iq.send() return d
def getRegisteredUsers(self, portal=None): """ XXX: This is ejabberd specific. ejabberd does not implement the #get-registered-users-list command, instead does it with an iq/get. """ iq = IQ(self.xmlstream, "get") iq["to"] = users.getXMPPDomain(portal) query = iq.addElement((NS_DISCO_ITEMS, "query")) query["node"] = "all users" d = iq.send() return d
def sendAnnouncement(self, body, subject="Announce"): """ Send an announement to all users. """ def resultReceived(iq): log.info("Sent announcement %s." % body) return True def formReceived(iq): command = iq.command sessionid = command["sessionid"] form = data_form.findForm(command, NODE_ADMIN) # from twisted.words.protocols.jabber.xmlstream import toResponse # response = toResponse(iq, 'set') response = IQ(self.xmlstream, "set") response["to"] = iq["from"] response["id"] = iq["id"] command = response.addElement((NS_COMMANDS, "command")) command["node"] = NODE_ADMIN_ANNOUNCE command["sessionid"] = sessionid form.formType = "submit" form.fields["subject"].value = subject form.fields["body"].value = body command.addChild(form.toElement()) d = response.send() d.addCallbacks(resultReceived, error) return d def error(failure): # TODO: Handle gracefully? log.error(failure.getTraceback()) return False iq = IQ(self.xmlstream, "set") iq["to"] = users.getXMPPDomain() command = iq.addElement((NS_COMMANDS, "command")) command["action"] = "execute" command["node"] = NODE_ADMIN_ANNOUNCE d = iq.send() d.addCallbacks(formReceived, error) return d
def sendAnnouncement(self, body, subject='Announce'): """ Send an announement to all users. """ def resultReceived(iq): log.info("Sent announcement %s." % body) return True def formReceived(iq): command = iq.command sessionid = command['sessionid'] form = data_form.findForm(command, NODE_ADMIN) #from twisted.words.protocols.jabber.xmlstream import toResponse #response = toResponse(iq, 'set') response = IQ(self.xmlstream, 'set') response['to'] = iq['from'] response['id'] = iq['id'] command = response.addElement((NS_COMMANDS, 'command')) command['node'] = NODE_ADMIN_ANNOUNCE command['sessionid'] = sessionid form.formType = 'submit' form.fields['subject'].value = subject form.fields['body'].value = body command.addChild(form.toElement()) d = response.send() d.addCallbacks(resultReceived, error) return d def error(failure): # TODO: Handle gracefully? log.error(failure.getTraceback()) return False iq = IQ(self.xmlstream, 'set') iq['to'] = users.getXMPPDomain() command = iq.addElement((NS_COMMANDS, 'command')) command['action'] = 'execute' command['node'] = NODE_ADMIN_ANNOUNCE d = iq.send() d.addCallbacks(formReceived, error) return d
def addUser(self, userjid, password): """ Add a user """ def resultReceived(iq): log.info("Added user %s" % userjid) return True def formReceived(iq): command = iq.command sessionid = command["sessionid"] form = data_form.findForm(command, NODE_ADMIN) response = IQ(self.xmlstream, "set") response["to"] = iq["from"] response["id"] = iq["id"] command = response.addElement((NS_COMMANDS, "command")) command["node"] = NODE_ADMIN_ADD_USER command["sessionid"] = sessionid form.formType = "submit" form.fields["accountjid"].value = userjid form.fields["password"].value = password form.fields["password-verify"].value = password command.addChild(form.toElement()) d = response.send() d.addCallbacks(resultReceived, error) return d def error(failure): # TODO: Handle gracefully? log.error(failure.getTraceback()) return False iq = IQ(self.xmlstream, "set") iq["to"] = users.getXMPPDomain() command = iq.addElement((NS_COMMANDS, "command")) command["action"] = "execute" command["node"] = NODE_ADMIN_ADD_USER d = iq.send() d.addCallbacks(formReceived, error) return d
def addUser(self, userjid, password): """ Add a user """ def resultReceived(iq): log.info("Added user %s" % userjid) return True def formReceived(iq): command = iq.command sessionid = command['sessionid'] form = data_form.findForm(command, NODE_ADMIN) response = IQ(self.xmlstream, 'set') response['to'] = iq['from'] response['id'] = iq['id'] command = response.addElement((NS_COMMANDS, 'command')) command['node'] = NODE_ADMIN_ADD_USER command['sessionid'] = sessionid form.formType = 'submit' form.fields['accountjid'].value = userjid form.fields['password'].value = password form.fields['password-verify'].value = password command.addChild(form.toElement()) d = response.send() d.addCallbacks(resultReceived, error) return d def error(failure): # TODO: Handle gracefully? log.error(failure.getTraceback()) return False iq = IQ(self.xmlstream, 'set') iq['to'] = users.getXMPPDomain() command = iq.addElement((NS_COMMANDS, 'command')) command['action'] = 'execute' command['node'] = NODE_ADMIN_ADD_USER d = iq.send() d.addCallbacks(formReceived, error) return d
def deleteUsers(self, userjids): """ """ def resultReceived(iq): log.info("Deleted users %s" % userjids) return True def formReceived(iq): command = iq.command sessionid = command["sessionid"] form = data_form.findForm(command, NODE_ADMIN) response = IQ(self.xmlstream, "set") response["to"] = iq["from"] response["id"] = iq["id"] command = response.addElement((NS_COMMANDS, "command")) command["node"] = NODE_ADMIN_DELETE_USER command["sessionid"] = sessionid form.formType = "submit" form.fields["accountjids"].values = userjids command.addChild(form.toElement()) d = response.send() d.addCallbacks(resultReceived, error) return d def error(failure): # TODO: Handle gracefully? log.error(failure.getTraceback()) return False if isinstance(userjids, basestring): userjids = [userjids] iq = IQ(self.xmlstream, "set") iq["to"] = users.getXMPPDomain() command = iq.addElement((NS_COMMANDS, "command")) command["action"] = "execute" command["node"] = NODE_ADMIN_DELETE_USER d = iq.send() d.addCallbacks(formReceived, error) return d
def deleteUsers(self, userjids): """ """ def resultReceived(iq): log.info("Deleted users %s" % userjids) return True def formReceived(iq): command = iq.command sessionid = command['sessionid'] form = data_form.findForm(command, NODE_ADMIN) response = IQ(self.xmlstream, 'set') response['to'] = iq['from'] response['id'] = iq['id'] command = response.addElement((NS_COMMANDS, 'command')) command['node'] = NODE_ADMIN_DELETE_USER command['sessionid'] = sessionid form.formType = 'submit' form.fields['accountjids'].values = userjids command.addChild(form.toElement()) d = response.send() d.addCallbacks(resultReceived, error) return d def error(failure): # TODO: Handle gracefully? log.error(failure.getTraceback()) return False if isinstance(userjids, basestring): userjids = [userjids] iq = IQ(self.xmlstream, 'set') iq['to'] = users.getXMPPDomain() command = iq.addElement((NS_COMMANDS, 'command')) command['action'] = 'execute' command['node'] = NODE_ADMIN_DELETE_USER d = iq.send() d.addCallbacks(formReceived, error) return d