]>
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 from tlib
.xmlw
import Element
, jid
6 from debug
import LogEvent
, INFO
, WARN
, ERROR
15 class RegisterManager
:
16 def __init__(self
, pytrans
):
17 self
.pytrans
= pytrans
18 if config
.allowRegister
:
19 self
.pytrans
.discovery
.addFeature(disco
.IQREGISTER
, self
.incomingRegisterIq
, config
.jid
)
22 def removeRegInfo(self
, jabberID
):
25 # If the session is active then send offline presences
26 session
= self
.pytrans
.sessions
[jabberID
]
31 self
.pytrans
.xdb
.remove(jabberID
)
32 LogEvent(INFO
, "", "done")
35 def setRegInfo(self
, jabberID
, username
, password
):
37 if(len(password
) == 0):
38 (blah1
, password
, blah3
) = self
.getRegInfo(jabberID
)
40 reginfo
= legacy
.formRegEntry(username
, password
)
41 self
.pytrans
.xdb
.set(jid
.intern(jabberID
).userhost(), legacy
.namespace
, reginfo
)
43 def getRegInfo(self
, jabberID
):
45 result
= self
.pytrans
.xdb
.request(jid
.intern(jabberID
).userhost(), legacy
.namespace
)
47 LogEvent(INFO
, "", "Not registered!")
50 username
, password
= legacy
.getAttributes(result
)
52 if(username
and password
and len(username
) > 0 and len(password
) > 0):
53 LogEvent(INFO
, "", "Returning reg info.")
54 return (username
, password
)
56 LogEvent(WARN
, "", "Registration data corrupted!")
59 def incomingRegisterIq(self
, incoming
):
60 # Check what type the Iq is..
61 itype
= incoming
.getAttribute("type")
64 self
.sendRegistrationFields(incoming
)
66 self
.updateRegistration(incoming
)
68 def sendRegistrationFields(self
, incoming
):
69 # Construct a reply with the fields they must fill out
70 ID
= incoming
.getAttribute("id")
71 fro
= incoming
.getAttribute("from")
73 reply
= Element((None, "iq"))
74 reply
.attributes
["from"] = config
.jid
75 reply
.attributes
["to"] = fro
77 reply
.attributes
["id"] = ID
78 reply
.attributes
["type"] = "result"
79 query
= reply
.addElement("query")
80 query
.attributes
["xmlns"] = "jabber:iq:register"
81 instructions
= query
.addElement("instructions")
82 ulang
= utils
.getLang(incoming
)
83 instructions
.addContent(lang
.get(ulang
).registerText
)
84 userEl
= query
.addElement("username")
85 passEl
= query
.addElement("password")
87 # Check to see if they're registered
88 result
= self
.getRegInfo(incoming
.getAttribute("from"))
90 username
, password
= result
91 userEl
.addContent(username
)
92 query
.addElement("registered")
94 self
.pytrans
.send(reply
)
96 def updateRegistration(self
, incoming
):
97 # Grab the username, password
98 ID
= incoming
.getAttribute("id")
99 fro
= incoming
.getAttribute("from")
101 source
= jid
.intern(fro
).userhost()
102 ulang
= utils
.getLang(incoming
)
106 for queryFind
in incoming
.elements():
107 if(queryFind
.name
== "query"):
108 for child
in queryFind
.elements():
110 if(child
.name
== "username"):
111 username
= child
.__str
__()
112 elif(child
.name
== "password"):
113 password
= child
.__str
__()
114 elif(child
.name
== "remove"):
115 # The user wants to unregister the transport! Gasp!
116 LogEvent(INFO
, "", "Unregistering.")
118 self
.removeRegInfo(source
)
119 self
.successReply(incoming
)
121 self
.xdbErrorReply(incoming
)
123 LogEvent(INFO
, "", "Unregistered!")
125 except AttributeError, TypeError:
126 continue # Ignore any errors, we'll check everything below
128 if(username
and password
and len(username
) > 0 and len(password
) > 0):
129 # Valid registration data
130 LogEvent(INFO
, "", "Updating XDB")
132 self
.setRegInfo(source
, username
, password
)
133 LogEvent(INFO
, "", "Updated XDB")
134 self
.successReply(incoming
)
135 LogEvent(INFO
, "", "Sent a result Iq")
136 (user
, host
, res
) = jid
.parse(incoming
.getAttribute("from"))
137 jabw
.sendPresence(self
.pytrans
, to
=user
+ "@" + host
, fro
=config
.jid
, ptype
="subscribe")
138 if(config
.registerMessage
):
139 jabw
.sendMessage(self
.pytrans
, to
=incoming
.getAttribute("from"), fro
=config
.jid
, body
=config
.registerMessage
)
141 self
.xdbErrorReply(incoming
)
145 self
.badRequestReply(incoming
)
147 def badRequestReply(self
, incoming
):
149 # Invalid registration data was sent to us. Or the removal failed
152 reply
.swapAttributeValues("to", "from")
153 reply
.attributes
["type"] = "error"
154 error
= reply
.addElement("error")
155 error
.attributes
["type"] = "modify"
156 interror
= error
.addElement("bad-request")
157 interror
["xmlns"] = disco
.XMPP_STANZAS
158 self
.pytrans
.send(reply
)
160 def xdbErrorReply(self
, incoming
):
162 # Failure in updating XDB or sending result Iq
165 reply
.swapAttributeValues("to", "from")
166 reply
.attributes
["type"] = "error"
167 error
= reply
.addElement("error")
168 error
.attributes
["type"] = "wait"
169 interror
= error
.addElement("internal-server-error")
170 interror
["xmlns"] = disco
.XMPP_STANZAS
171 self
.pytrans
.send(reply
)
173 def successReply(self
, incoming
):
174 reply
= Element((None, "iq"))
175 reply
.attributes
["type"] = "result"
176 ID
= incoming
.getAttribute("id")
177 if(ID
): reply
.attributes
["id"] = ID
178 reply
.attributes
["from"] = config
.jid
179 reply
.attributes
["to"] = incoming
.getAttribute("from")
180 self
.pytrans
.send(reply
)