]>
code.delx.au - pymsnt/blob - 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
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
11 from debug
import LogEvent
, INFO
, WARN
, ERROR
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
)
27 def removeRegInfo(self
, jabberID
):
30 # If the session is active then send offline presences
31 session
= self
.pytrans
.sessions
[jabberID
]
36 self
.pytrans
.xdb
.remove(jabberID
)
37 LogEvent(INFO
, "", "done")
40 def setRegInfo(self
, jabberID
, username
, password
):
42 if(len(password
) == 0):
43 (blah1
, password
, blah3
) = self
.getRegInfo(jabberID
)
45 reginfo
= legacy
.formRegEntry(username
, password
)
46 self
.pytrans
.xdb
.set(jid
.JID(jabberID
).userhost(), legacy
.namespace
, reginfo
)
48 def getRegInfo(self
, jabberID
):
50 result
= self
.pytrans
.xdb
.request(jid
.JID(jabberID
).userhost(), legacy
.namespace
)
52 LogEvent(INFO
, "", "Not registered!")
55 username
, password
= legacy
.getAttributes(result
)
57 if(username
and password
and len(username
) > 0 and len(password
) > 0):
58 LogEvent(INFO
, "", "Returning reg info.")
59 return (username
, password
)
61 LogEvent(WARN
, "", "Registration data corrupted!")
64 def incomingRegisterIq(self
, incoming
):
65 # Check what type the Iq is..
66 itype
= incoming
.getAttribute("type")
69 self
.sendRegistrationFields(incoming
)
71 self
.updateRegistration(incoming
)
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")
78 reply
= Element((None, "iq"))
79 reply
.attributes
["from"] = config
.jid
80 reply
.attributes
["to"] = fro
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")
92 # Check to see if they're registered
93 result
= self
.getRegInfo(incoming
.getAttribute("from"))
95 username
, password
= result
96 userEl
.addContent(username
)
97 query
.addElement("registered")
99 self
.pytrans
.send(reply
)
101 def updateRegistration(self
, incoming
):
102 # Grab the username, password
103 ID
= incoming
.getAttribute("id")
104 fro
= incoming
.getAttribute("from")
106 source
= jid
.JID(fro
).userhost()
107 ulang
= utils
.getLang(incoming
)
111 for queryFind
in incoming
.elements():
112 if(queryFind
.name
== "query"):
113 for child
in queryFind
.elements():
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.")
123 self
.removeRegInfo(source
)
124 self
.successReply(incoming
)
126 self
.xdbErrorReply(incoming
)
128 LogEvent(INFO
, "", "Unregistered!")
130 except AttributeError, TypeError:
131 continue # Ignore any errors, we'll check everything below
133 if(username
and password
and len(username
) > 0 and len(password
) > 0):
134 # Valid registration data
135 LogEvent(INFO
, "", "Updating XDB")
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
)
146 self
.xdbErrorReply(incoming
)
150 self
.badRequestReply(incoming
)
152 def badRequestReply(self
, incoming
):
154 # Invalid registration data was sent to us. Or the removal failed
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
)
165 def xdbErrorReply(self
, incoming
):
167 # Failure in updating XDB or sending result Iq
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
)
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
)