]> code.delx.au - pymsnt/blob - src/register.py
Trunk is broken. Don't use!
[pymsnt] / src / register.py
1 # Copyright 2004-2005 James Bunton <james@delx.cjb.net>
2 # Licensed for distribution under the GPL version 2, check COPYING for details
3
4 import utils
5 if(utils.checkTwisted()):
6 from twisted.xish.domish import Element
7 from twisted.words.protocols.jabber import jid
8 else:
9 from tlib.domish import Element
10 from tlib.jabber import jid
11 from debug import LogEvent, INFO, WARN, ERROR
12 import disco
13 import session
14 import config
15 import lang
16 import jabw
17 import legacy
18
19
20 class RegisterManager:
21 def __init__(self, pytrans):
22 self.pytrans = pytrans
23 if config.allowRegister:
24 self.pytrans.discovery.addFeature(disco.IQREGISTER, self.incomingRegisterIq, config.jid)
25 LogEvent(INFO)
26
27 def removeRegInfo(self, jabberID):
28 LogEvent(INFO)
29 try:
30 # If the session is active then send offline presences
31 session = self.pytrans.sessions[jabberID]
32 session.removeMe()
33 except KeyError:
34 pass
35
36 self.pytrans.xdb.remove(jabberID)
37 LogEvent(INFO, "", "done")
38
39
40 def setRegInfo(self, jabberID, username, password):
41 LogEvent(INFO)
42 if(len(password) == 0):
43 (blah1, password, blah3) = self.getRegInfo(jabberID)
44
45 reginfo = legacy.formRegEntry(username, password)
46 self.pytrans.xdb.set(jid.JID(jabberID).userhost(), legacy.namespace, reginfo)
47
48 def getRegInfo(self, jabberID):
49 LogEvent(INFO)
50 result = self.pytrans.xdb.request(jid.JID(jabberID).userhost(), legacy.namespace)
51 if(result == None):
52 LogEvent(INFO, "", "Not registered!")
53 return None
54
55 username, password = legacy.getAttributes(result)
56
57 if(username and password and len(username) > 0 and len(password) > 0):
58 LogEvent(INFO, "", "Returning reg info.")
59 return (username, password)
60 else:
61 LogEvent(WARN, "", "Registration data corrupted!")
62 return None
63
64 def incomingRegisterIq(self, incoming):
65 # Check what type the Iq is..
66 itype = incoming.getAttribute("type")
67 LogEvent(INFO)
68 if(itype == "get"):
69 self.sendRegistrationFields(incoming)
70 elif(itype == "set"):
71 self.updateRegistration(incoming)
72
73 def sendRegistrationFields(self, incoming):
74 # Construct a reply with the fields they must fill out
75 ID = incoming.getAttribute("id")
76 fro = incoming.getAttribute("from")
77 LogEvent(INFO)
78 reply = Element((None, "iq"))
79 reply.attributes["from"] = config.jid
80 reply.attributes["to"] = fro
81 if ID:
82 reply.attributes["id"] = ID
83 reply.attributes["type"] = "result"
84 query = reply.addElement("query")
85 query.attributes["xmlns"] = "jabber:iq:register"
86 instructions = query.addElement("instructions")
87 ulang = utils.getLang(incoming)
88 instructions.addContent(lang.get(ulang).registerText)
89 userEl = query.addElement("username")
90 passEl = query.addElement("password")
91
92 # Check to see if they're registered
93 result = self.getRegInfo(incoming.getAttribute("from"))
94 if(result):
95 username, password = result
96 userEl.addContent(username)
97 query.addElement("registered")
98
99 self.pytrans.send(reply)
100
101 def updateRegistration(self, incoming):
102 # Grab the username, password
103 ID = incoming.getAttribute("id")
104 fro = incoming.getAttribute("from")
105 LogEvent(INFO)
106 source = jid.JID(fro).userhost()
107 ulang = utils.getLang(incoming)
108 username = None
109 password = None
110
111 for queryFind in incoming.elements():
112 if(queryFind.name == "query"):
113 for child in queryFind.elements():
114 try:
115 if(child.name == "username"):
116 username = child.__str__()
117 elif(child.name == "password"):
118 password = child.__str__()
119 elif(child.name == "remove"):
120 # The user wants to unregister the transport! Gasp!
121 LogEvent(INFO, "", "Unregistering.")
122 try:
123 self.removeRegInfo(source)
124 self.successReply(incoming)
125 except:
126 self.xdbErrorReply(incoming)
127 return
128 LogEvent(INFO, "", "Unregistered!")
129 return
130 except AttributeError, TypeError:
131 continue # Ignore any errors, we'll check everything below
132
133 if(username and password and len(username) > 0 and len(password) > 0):
134 # Valid registration data
135 LogEvent(INFO, "", "Updating XDB")
136 try:
137 self.setRegInfo(source, username, password)
138 LogEvent(INFO, "", "Updated XDB")
139 self.successReply(incoming)
140 LogEvent(INFO, "", "Sent a result Iq")
141 (user, host, res) = jid.parse(incoming.getAttribute("from"))
142 jabw.sendPresence(self.pytrans, to=user + "@" + host, fro=config.jid, ptype="subscribe")
143 if(config.registerMessage):
144 jabw.sendMessage(self.pytrans, to=incoming.getAttribute("from"), fro=config.jid, body=config.registerMessage)
145 except:
146 self.xdbErrorReply(incoming)
147 raise
148
149 else:
150 self.badRequestReply(incoming)
151
152 def badRequestReply(self, incoming):
153 LogEvent(INFO)
154 # Invalid registration data was sent to us. Or the removal failed
155 # Send an error Iq
156 reply = incoming
157 reply.swapAttributeValues("to", "from")
158 reply.attributes["type"] = "error"
159 error = reply.addElement("error")
160 error.attributes["type"] = "modify"
161 interror = error.addElement("bad-request")
162 interror["xmlns"] = disco.XMPP_STANZAS
163 self.pytrans.send(reply)
164
165 def xdbErrorReply(self, incoming):
166 LogEvent(INFO)
167 # Failure in updating XDB or sending result Iq
168 # send an error Iq
169 reply = incoming
170 reply.swapAttributeValues("to", "from")
171 reply.attributes["type"] = "error"
172 error = reply.addElement("error")
173 error.attributes["type"] = "wait"
174 interror = error.addElement("internal-server-error")
175 interror["xmlns"] = disco.XMPP_STANZAS
176 self.pytrans.send(reply)
177
178 def successReply(self, incoming):
179 reply = Element((None, "iq"))
180 reply.attributes["type"] = "result"
181 ID = incoming.getAttribute("id")
182 if(ID): reply.attributes["id"] = ID
183 reply.attributes["from"] = config.jid
184 reply.attributes["to"] = incoming.getAttribute("from")
185 self.pytrans.send(reply)
186