Commits

Konstantine Rybnikov committed 8b68ad1

ok. Now NICK command works

Comments (0)

Files changed (4)

openju/bl/user.py

 #!/usr/bin/python
 # -*- coding: utf-8 -*-
 
+from openju.util import get_mongo_db
+from openju.validators import validate_nickname
+
 def set_nickname(jid, nick):
-    print u'> user %s changes nickname to %s' % (jid, nick)
+    validate_nickname(nick)
+    with get_mongo_db() as db:
+        db.users.update(
+            { '_id': jid },
+            { '_id': jid,
+              'nick': nick },
+            upsert=True)
 
 def post_reply(jid, message_id, reply):
     print u'> user %s is posting reply to %s. Reply is: %s' % (jid, message_id, reply)

openju/messagecb.py

 from openju import g
 import re
 
+from openju.util import UserAlertError
 import openju.controllers as controllers
 
+def error_handler(f):
+    def new_f(conn, *args, **kw):
+        try:
+            f(conn, *args, **kw)
+        except UserAlertError, e:
+            conn.send(xmpp.Message(g.user, unicode(e)))
+    return new_f
+
+@error_handler
 def message_cb(conn, mess):
     """ kind a routing thing """
     
         cmd_text = u'NICK'
         if text.startswith(cmd_text):
             nick = text[len(cmd_text) + 1:].strip()
-            controllers.user.new_nick(nick)
+            return controllers.user.new_nick(nick)
             # bl.user.set_nickname(user_jid, new_nick)
-            return False
         return True
     
     def cmd_post_reply(user_jid, text):
                 if type(res) == unicode:
                     conn.send(xmpp.Message(user, res))
                     return
+        g.user = user
         g.text = text
         g.user_jid = user_jid
-        
+
         call_list(
             cmd_new_nick,
             cmd_post_reply,
 
 def get_mongo_db():
     return mongo_db_wrapper()
+
+class UserAlertError(Exception): pass

openju/validators.py

+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+import re
+from openju.util import UserAlertError
+
+class ValidNicknameError(UserAlertError): pass
+
+def validate_nickname(nick):
+    regexp = ur'^[a-zA-Z0-9\-_]+$'
+    reg = re.compile(regexp)
+    if not reg.match(nick):
+        raise ValidNicknameError(u'Wrong. Nickname regexp is: %s' % regexp)