-# Copyright 2004 James Bunton <james@delx.cjb.net>
+# Copyright 2004-2006 James Bunton <james@delx.cjb.net>
# Licensed for distribution under the GPL version 2, check COPYING for details
import utils
-if(utils.checkTwisted()):
- from twisted.xish.domish import Element
- from twisted.words.protocols.jabber import jid
-else:
- from tlib.domish import Element
- from tlib.jabber import jid
-
+from twisted.words.xish.domish import Element
+from twisted.words.protocols.jabber.jid import internJID
+from debug import LogEvent, INFO, WARN, ERROR
+import disco
import session
import config
-import debug
import lang
import jabw
import legacy
-XMPP_STANZAS = 'urn:ietf:params:xml:ns:xmpp-stanzas'
class RegisterManager:
def __init__(self, pytrans):
self.pytrans = pytrans
if config.allowRegister:
- self.pytrans.discovery.addFeature("jabber:iq:register", self.incomingRegisterIq)
- debug.log("RegisterManager: Created")
+ self.pytrans.discovery.addFeature(disco.IQREGISTER, self.incomingRegisterIq, config.jid)
+ LogEvent(INFO)
def removeRegInfo(self, jabberID):
- debug.log("RegisterManager: removeRegInfo(\"%s\")" % (jabberID))
+ LogEvent(INFO)
try:
# If the session is active then send offline presences
session = self.pytrans.sessions[jabberID]
pass
self.pytrans.xdb.remove(jabberID)
- debug.log("RegisterManager: removeRegInfo(\"%s\") - done" % (jabberID))
+ LogEvent(INFO, "", "done")
- def setRegInfo(self, jabberID, username, password, nickname):
- debug.log("RegisterManager: setRegInfo(\"%s\", \"%s\", \"%s\", \"%s\")" % (jabberID, username, password, nickname))
+ def setRegInfo(self, jabberID, username, password):
+ LogEvent(INFO)
if(len(password) == 0):
(blah1, password, blah3) = self.getRegInfo(jabberID)
- reginfo = legacy.formRegEntry(username, password, nickname)
- self.pytrans.xdb.set(jid.JID(jabberID).full(), legacy.namespace, reginfo)
+ reginfo = legacy.formRegEntry(username, password)
+ self.pytrans.xdb.set(internJID(jabberID).userhost(), legacy.namespace, reginfo)
def getRegInfo(self, jabberID):
- debug.log("RegisterManager: getRegInfo(\"%s\")" % (jabberID))
- result = self.pytrans.xdb.request(jid.JID(jabberID).full(), legacy.namespace)
+ LogEvent(INFO)
+ result = self.pytrans.xdb.request(internJID(jabberID).userhost(), legacy.namespace)
if(result == None):
- debug.log("RegisterManager: getRegInfo(\"%s\") - not registered!" % (jabberID))
+ LogEvent(INFO, "", "Not registered!")
return None
- username, password, nickname = legacy.getAttributes(result)
+ username, password = legacy.getAttributes(result)
if(username and password and len(username) > 0 and len(password) > 0):
- debug.log("RegisterManager: getRegInfo(\"%s\") - returning reg info \"%s\" \"%s\" \"%s\"!" % (jabberID, username, password, utils.latin1(nickname)))
- return (username, password, nickname)
+ LogEvent(INFO, "", "Returning reg info.")
+ return (username, password)
else:
- debug.log("RegisterManager: getRegInfo(\"%s\") - invalid registration data! %s %s %s" % (jabberID, username, password, utils.latin1(nickname)))
+ LogEvent(WARN, "", "Registration data corrupted!")
return None
def incomingRegisterIq(self, incoming):
# Check what type the Iq is..
itype = incoming.getAttribute("type")
- debug.log("RegisterManager: In-band registration type \"%s\" received" % (itype))
+ LogEvent(INFO)
if(itype == "get"):
self.sendRegistrationFields(incoming)
elif(itype == "set"):
def sendRegistrationFields(self, incoming):
# Construct a reply with the fields they must fill out
- debug.log("RegisterManager: sendRegistrationFields() for \"%s\" \"%s\"" % (incoming.getAttribute("from"), incoming.getAttribute("id")))
+ ID = incoming.getAttribute("id")
+ fro = incoming.getAttribute("from")
+ LogEvent(INFO)
reply = Element((None, "iq"))
reply.attributes["from"] = config.jid
- reply.attributes["to"] = incoming.getAttribute("from")
- reply.attributes["id"] = incoming.getAttribute("id")
+ reply.attributes["to"] = fro
+ if ID:
+ reply.attributes["id"] = ID
reply.attributes["type"] = "result"
query = reply.addElement("query")
query.attributes["xmlns"] = "jabber:iq:register"
instructions.addContent(lang.get(ulang).registerText)
userEl = query.addElement("username")
passEl = query.addElement("password")
- nickEl = query.addElement("nick")
# Check to see if they're registered
- barefrom = jid.JID(incoming.getAttribute("from")).userhost()
- result = self.getRegInfo(barefrom)
+ result = self.getRegInfo(incoming.getAttribute("from"))
if(result):
- username, password, nickname = result
+ username, password = result
userEl.addContent(username)
- if(nickname and len(nickname) > 0):
- nickEl.addContent(nickname)
query.addElement("registered")
self.pytrans.send(reply)
def updateRegistration(self, incoming):
- # Grab the username, password and nickname
- debug.log("RegisterManager: updateRegistration() for \"%s\" \"%s\"" % (incoming.getAttribute("from"), incoming.getAttribute("id")))
- source = jid.JID(incoming.getAttribute("from")).userhost()
+ # Grab the username, password
+ ID = incoming.getAttribute("id")
+ fro = incoming.getAttribute("from")
+ LogEvent(INFO)
+ source = internJID(fro).userhost()
ulang = utils.getLang(incoming)
username = None
password = None
- nickname = None
for queryFind in incoming.elements():
if(queryFind.name == "query"):
username = child.__str__()
elif(child.name == "password"):
password = child.__str__()
- elif(child.name == "nick"):
- nickname = child.__str__()
elif(child.name == "remove"):
# The user wants to unregister the transport! Gasp!
- debug.log("RegisterManager: Session \"%s\" is about to be unregistered" % (source))
+ LogEvent(INFO, "", "Unregistering.")
try:
self.removeRegInfo(source)
self.successReply(incoming)
except:
self.xdbErrorReply(incoming)
return
- debug.log("RegisterManager: Session \"%s\" has been unregistered" % (source))
+ LogEvent(INFO, "", "Unregistered!")
return
except AttributeError, TypeError:
continue # Ignore any errors, we'll check everything below
if(username and password and len(username) > 0 and len(password) > 0):
# Valid registration data
- debug.log("RegisterManager: Valid registration data was received. Attempting to update XDB")
+ LogEvent(INFO, "", "Updating XDB")
try:
- self.setRegInfo(source, username, password, nickname)
- debug.log("RegisterManager: Updated XDB successfully")
+ self.setRegInfo(source, username, password)
+ LogEvent(INFO, "", "Updated XDB")
self.successReply(incoming)
- debug.log("RegisterManager: Sent off a result Iq")
- # If they're in a session right now we update their nick, otherwise request their auth
- if(self.pytrans.sessions.has_key(source)):
- s = self.pytrans.sessions[source]
- s.updateNickname(nickname)
- else:
- (user, host, res) = jid.parse(incoming.getAttribute("from"))
- jabw.sendPresence(self.pytrans, to=user + "@" + host, fro=config.jid, ptype="subscribe")
+ LogEvent(INFO, "", "Sent a result Iq")
+ to = internJID(incoming.getAttribute("from")).userhost()
+ jabw.sendPresence(self.pytrans, to=to, fro=config.jid, ptype="subscribe")
if(config.registerMessage):
jabw.sendMessage(self.pytrans, to=incoming.getAttribute("from"), fro=config.jid, body=config.registerMessage)
except:
self.badRequestReply(incoming)
def badRequestReply(self, incoming):
- debug.log("RegisterManager: Invalid registration data was sent to us. Or the removal failed.")
+ LogEvent(INFO)
+ # Invalid registration data was sent to us. Or the removal failed
# Send an error Iq
reply = incoming
reply.swapAttributeValues("to", "from")
error = reply.addElement("error")
error.attributes["type"] = "modify"
interror = error.addElement("bad-request")
- interror["xmlns"] = XMPP_STANZAS
+ interror["xmlns"] = disco.XMPP_STANZAS
self.pytrans.send(reply)
def xdbErrorReply(self, incoming):
- debug.log("RegisterManager: Failure in updating XDB or sending result Iq")
+ LogEvent(INFO)
+ # Failure in updating XDB or sending result Iq
# send an error Iq
reply = incoming
reply.swapAttributeValues("to", "from")
error = reply.addElement("error")
error.attributes["type"] = "wait"
interror = error.addElement("internal-server-error")
- interror["xmlns"] = XMPP_STANZAS
+ interror["xmlns"] = disco.XMPP_STANZAS
self.pytrans.send(reply)
def successReply(self, incoming):