]>
code.delx.au - pymsnt/blob - src/register.py
1 # Copyright 2004 James Bunton <james@delx.cjb.net>
2 # Licensed for distribution under the GPL version 2, check COPYING for details
5 if(utils
.checkTwisted()):
6 from twisted
.xish
.domish
import Element
7 from twisted
.words
.protocols
.jabber
import jid
9 from tlib
.domish
import Element
10 from tlib
.jabber
import jid
19 XMPP_STANZAS
= 'urn:ietf:params:xml:ns:xmpp-stanzas'
21 class RegisterManager
:
22 def __init__(self
, pytrans
):
23 self
.pytrans
= pytrans
24 if config
.allowRegister
:
25 self
.pytrans
.discovery
.addFeature("jabber:iq:register", self
.incomingRegisterIq
)
26 debug
.log("RegisterManager: Created")
28 def removeRegInfo(self
, jabberID
):
29 debug
.log("RegisterManager: removeRegInfo(\"%s\")" % (jabberID
))
31 # If the session is active then send offline presences
32 session
= self
.pytrans
.sessions
[jabberID
]
37 self
.pytrans
.xdb
.remove(jabberID
)
38 debug
.log("RegisterManager: removeRegInfo(\"%s\") - done" % (jabberID
))
41 def setRegInfo(self
, jabberID
, username
, password
, nickname
):
42 debug
.log("RegisterManager: setRegInfo(\"%s\", \"%s\", \"%s\", \"%s\")" % (jabberID
, username
, password
, nickname
))
43 if(len(password
) == 0):
44 (blah1
, password
, blah3
) = self
.getRegInfo(jabberID
)
46 reginfo
= legacy
.formRegEntry(username
, password
, nickname
)
47 self
.pytrans
.xdb
.set(jid
.JID(jabberID
).full(), legacy
.namespace
, reginfo
)
49 def getRegInfo(self
, jabberID
):
50 debug
.log("RegisterManager: getRegInfo(\"%s\")" % (jabberID
))
51 result
= self
.pytrans
.xdb
.request(jid
.JID(jabberID
).full(), legacy
.namespace
)
53 debug
.log("RegisterManager: getRegInfo(\"%s\") - not registered!" % (jabberID
))
56 username
, password
, nickname
= legacy
.getAttributes(result
)
58 if(username
and password
and len(username
) > 0 and len(password
) > 0):
59 debug
.log("RegisterManager: getRegInfo(\"%s\") - returning reg info \"%s\" \"%s\" \"%s\"!" % (jabberID
, username
, password
, utils
.latin1(nickname
)))
60 return (username
, password
, nickname
)
62 debug
.log("RegisterManager: getRegInfo(\"%s\") - invalid registration data! %s %s %s" % (jabberID
, username
, password
, utils
.latin1(nickname
)))
65 def incomingRegisterIq(self
, incoming
):
66 # Check what type the Iq is..
67 itype
= incoming
.getAttribute("type")
68 debug
.log("RegisterManager: In-band registration type \"%s\" received" % (itype
))
70 self
.sendRegistrationFields(incoming
)
72 self
.updateRegistration(incoming
)
74 def sendRegistrationFields(self
, incoming
):
75 # Construct a reply with the fields they must fill out
76 debug
.log("RegisterManager: sendRegistrationFields() for \"%s\" \"%s\"" % (incoming
.getAttribute("from"), incoming
.getAttribute("id")))
77 reply
= Element((None, "iq"))
78 reply
.attributes
["from"] = config
.jid
79 reply
.attributes
["to"] = incoming
.getAttribute("from")
80 reply
.attributes
["id"] = incoming
.getAttribute("id")
81 reply
.attributes
["type"] = "result"
82 query
= reply
.addElement("query")
83 query
.attributes
["xmlns"] = "jabber:iq:register"
84 instructions
= query
.addElement("instructions")
85 ulang
= utils
.getLang(incoming
)
86 instructions
.addContent(lang
.get(ulang
).registerText
)
87 userEl
= query
.addElement("username")
88 passEl
= query
.addElement("password")
89 nickEl
= query
.addElement("nick")
91 # Check to see if they're registered
92 barefrom
= jid
.JID(incoming
.getAttribute("from")).userhost()
93 result
= self
.getRegInfo(barefrom
)
95 username
, password
, nickname
= result
96 userEl
.addContent(username
)
97 if(nickname
and len(nickname
) > 0):
98 nickEl
.addContent(nickname
)
99 query
.addElement("registered")
101 self
.pytrans
.send(reply
)
103 def updateRegistration(self
, incoming
):
104 # Grab the username, password and nickname
105 debug
.log("RegisterManager: updateRegistration() for \"%s\" \"%s\"" % (incoming
.getAttribute("from"), incoming
.getAttribute("id")))
106 source
= jid
.JID(incoming
.getAttribute("from")).userhost()
107 ulang
= utils
.getLang(incoming
)
112 for queryFind
in incoming
.elements():
113 if(queryFind
.name
== "query"):
114 for child
in queryFind
.elements():
116 if(child
.name
== "username"):
117 username
= child
.__str
__()
118 elif(child
.name
== "password"):
119 password
= child
.__str
__()
120 elif(child
.name
== "nick"):
121 nickname
= child
.__str
__()
122 elif(child
.name
== "remove"):
123 # The user wants to unregister the transport! Gasp!
124 debug
.log("RegisterManager: Session \"%s\" is about to be unregistered" % (source
))
126 self
.removeRegInfo(source
)
127 self
.successReply(incoming
)
129 self
.xdbErrorReply(incoming
)
131 debug
.log("RegisterManager: Session \"%s\" has been unregistered" % (source
))
133 except AttributeError, TypeError:
134 continue # Ignore any errors, we'll check everything below
136 if(username
and password
and len(username
) > 0 and len(password
) > 0):
137 # Valid registration data
138 debug
.log("RegisterManager: Valid registration data was received. Attempting to update XDB")
140 self
.setRegInfo(source
, username
, password
, nickname
)
141 debug
.log("RegisterManager: Updated XDB successfully")
142 self
.successReply(incoming
)
143 debug
.log("RegisterManager: Sent off a result Iq")
144 # If they're in a session right now we update their nick, otherwise request their auth
145 if(self
.pytrans
.sessions
.has_key(source
)):
146 s
= self
.pytrans
.sessions
[source
]
147 s
.updateNickname(nickname
)
149 (user
, host
, res
) = jid
.parse(incoming
.getAttribute("from"))
150 jabw
.sendPresence(self
.pytrans
, to
=user
+ "@" + host
, fro
=config
.jid
, ptype
="subscribe")
151 if(config
.registerMessage
):
152 jabw
.sendMessage(self
.pytrans
, to
=incoming
.getAttribute("from"), fro
=config
.jid
, body
=config
.registerMessage
)
154 self
.xdbErrorReply(incoming
)
158 self
.badRequestReply(incoming
)
160 def badRequestReply(self
, incoming
):
161 debug
.log("RegisterManager: Invalid registration data was sent to us. Or the removal failed.")
164 reply
.swapAttributeValues("to", "from")
165 reply
.attributes
["type"] = "error"
166 error
= reply
.addElement("error")
167 error
.attributes
["type"] = "modify"
168 interror
= error
.addElement("bad-request")
169 interror
["xmlns"] = XMPP_STANZAS
170 self
.pytrans
.send(reply
)
172 def xdbErrorReply(self
, incoming
):
173 debug
.log("RegisterManager: Failure in updating XDB or sending result Iq")
176 reply
.swapAttributeValues("to", "from")
177 reply
.attributes
["type"] = "error"
178 error
= reply
.addElement("error")
179 error
.attributes
["type"] = "wait"
180 interror
= error
.addElement("internal-server-error")
181 interror
["xmlns"] = XMPP_STANZAS
182 self
.pytrans
.send(reply
)
184 def successReply(self
, incoming
):
185 reply
= Element((None, "iq"))
186 reply
.attributes
["type"] = "result"
187 ID
= incoming
.getAttribute("id")
188 if(ID
): reply
.attributes
["id"] = ID
189 reply
.attributes
["from"] = config
.jid
190 reply
.attributes
["to"] = incoming
.getAttribute("from")
191 self
.pytrans
.send(reply
)