]> jfr.im git - irc/quakenet/qwebirc.git/commitdiff
Add nickname validation.
authorChris Porter <redacted>
Sun, 20 Feb 2011 02:43:27 +0000 (02:43 +0000)
committerChris Porter <redacted>
Sun, 20 Feb 2011 02:43:27 +0000 (02:43 +0000)
bin/optionsgen.py
bin/pages.py
config.py.example
js/irc/nicknamevalidator.js [new file with mode: 0644]
js/qwebircinterface.js
js/ui/panes/connect.js
js/ui/panes/embed.js

index 584c884b64e85df424961a00849e24b203f2bda3..f9982835abb99f015af279f250a435585a0110e7 100644 (file)
@@ -2,5 +2,23 @@ import config
 import qwebirc.util.qjson as json
 
 def get_options():
-  options = dict(networkName=config.NETWORK_NAME, networkServices=[config.AUTH_SERVICE], loginRegex=config.  AUTH_OK_REGEX, appTitle=config.APP_TITLE, baseURL=config.BASE_URL, staticBaseURL=config.STATIC_BASE_URL, dynamicBaseURL=config.DYNAMIC_BASE_URL)
+  options = dict(
+    networkName=config.NETWORK_NAME,
+    networkServices=[config.AUTH_SERVICE],
+    loginRegex=config.AUTH_OK_REGEX,
+    appTitle=config.APP_TITLE,
+    baseURL=config.BASE_URL,
+    staticBaseURL=config.STATIC_BASE_URL,
+    dynamicBaseURL=config.DYNAMIC_BASE_URL,
+    validateNickname=False
+  )
+  
+  if hasattr(config, "NICKNAME_VALIDATE") and config.NICKNAME_VALIDATE:
+    options["nickValidation"] = dict(
+      minLen=config.NICKNAME_MINIMUM_LENGTH,
+      maxLen=config.NICKNAME_MAXIMUM_LENGTH,
+      validFirstChar=config.NICKNAME_VALID_FIRST_CHAR,
+      validSubChars=config.NICKNAME_VALID_SUBSEQUENT_CHARS
+    )
+    
   return json.dumps(options)
index 7d62b3330914689b16acd808e13b87ba278afc73..105d771e12aec5baa026d9d5cc12b7a29cda7180 100644 (file)
@@ -1,5 +1,5 @@
 #!/usr/bin/env python
-IRC_BASE = ["ircconnection", "irclib", "numerics", "baseircclient", "irctracker", "commandparser", "commands", "ircclient", "commandhistory"]
+IRC_BASE = ["ircconnection", "irclib", "numerics", "baseircclient", "irctracker", "commandparser", "commands", "ircclient", "commandhistory", "nicknamevalidator"]
 PANES = ["connect", "embed", "options", "about", "privacypolicy", "feedback", "faq"]
 UI_BASE = ["menuitems", "baseui", "baseuiwindow", "colour", "url", "theme", "notifications", "tabcompleter", "style"]
 UI_BASE.extend(["panes/%s" % x for x in PANES])
index 485b0f3a5157aa7fff07bc3b01aa873a68f2d7d8..ff33b1179b8a844128a6723d0b648881336380aa 100644 (file)
@@ -96,6 +96,36 @@ NETWORK_NAME = "FooNet"
 #         The title of the application in the web browser.
 APP_TITLE = NETWORK_NAME + " Web IRC"
 
+# NICKNAME VALIDATION OPTIONS
+# ---------------------------------------------------------------------
+#
+# OPTION: NICKNAME_VALIDATE
+#         If True then user nicknames will be validated according to
+#         the configuration below, otherwise they will be passed
+#         directly to the ircd.
+NICKNAME_VALIDATE = True
+
+# OPTION: NICKNAME_VALID_FIRST_CHAR
+#         A string containing valid characters for the first letter of
+#         a nickname.
+#         Default is as in RFC1459.
+import string
+NICKNAME_VALID_FIRST_CHAR = string.letters + "_[]{}`^\\|"
+
+# OPTION: NICKNAME_VALID_SUBSEQUENT_CHAR
+#         A string containing valid characters for the rest of the
+#         nickname.
+NICKNAME_VALID_SUBSEQUENT_CHARS = NICKNAME_VALID_FIRST_CHAR + string.digits + "-"
+
+# OPTION: NICKNAME_MINIMUM_LENGTH
+#         Minimum characters permitted in a nickname on your network.
+NICKNAME_MINIMUM_LENGTH = 2
+
+# OPTION: NICKNAME_MAXIMUM_LENGTH
+#         Maximum characters permitted in a nickname on your network.
+#         Ideally we'd extract this from the ircd, but we need to know
+#         before we connect.
+NICKNAME_MAXIMUM_LENGTH = 15
 
 # FEEDBACK OPTIONS
 # ---------------------------------------------------------------------
diff --git a/js/irc/nicknamevalidator.js b/js/irc/nicknamevalidator.js
new file mode 100644 (file)
index 0000000..b751b90
--- /dev/null
@@ -0,0 +1,31 @@
+qwebirc.irc.DummyNicknameValidator = new Class({\r
+  validate: function(x) {\r
+    return x;\r
+  }\r
+});\r
+\r
+qwebirc.irc.NicknameValidator = new Class({\r
+  initialize: function(options) {\r
+    this.options = options;\r
+  },\r
+  validate: function(nick, permitDot) {\r
+    var r = [];\r
+    \r
+    var max = Math.min(this.options.maxLen, nick.length);\r
+    var exploded = nick.split("");\r
+    for(var i=0;i<max;i++) {\r
+      var c = exploded[i];\r
+      \r
+      var valid = i == 0 ? this.options.validFirstChar : this.options.validSubChars;\r
+      if(valid.indexOf(c) != -1 || permitDot && c == ".") {\r
+        r.push(c);\r
+      } else {\r
+        r.push("_"); /* yeah we assume this is valid... */\r
+      }\r
+    }\r
+\r
+    while(r.length < this.options.minLen)\r
+      r.push("_");  /* yeah we assume this is valid... */\r
+    return r.join("");\r
+  }\r
+});\r
index 046ddc681a65f058659c1bf3a1a9741b5e4d0414..5d5035b715d8ae5d09ad0dc83aea309ba327f730 100644 (file)
@@ -22,13 +22,19 @@ qwebirc.ui.Interface = new Class({
     saturation: null,
     lightness: null,
     uiOptionsArg: null,
+    nickValidation: null,
     dynamicBaseURL: "/",
     staticBaseURL: "/"
   },
   initialize: function(element, ui, options) {
-    qwebirc.global = {dynamicBaseURL: options.dynamicBaseURL, staticBaseURL: options.staticBaseURL}; /* HACK */
-
     this.setOptions(options);
+    
+    /* HACK */
+    qwebirc.global = {
+      dynamicBaseURL: options.dynamicBaseURL,
+      staticBaseURL: options.staticBaseURL,
+      nicknameValidator: $defined(options.nickValidation) ? new qwebirc.irc.NicknameValidator(options.nickValidation) : new qwebirc.irc.DummyNicknameValidator()
+    };
 
     window.addEvent("domready", function() {
       var callback = function(options) {
index fe128001c16991ec9dcaad44a841802a3b95f644..c7004d7c5f155be3ca17bd4da8affe8c9795dec8 100644 (file)
@@ -202,7 +202,14 @@ qwebirc.ui.LoginBox = function(parentElement, callback, initialNickname, initial
       nick.focus();
       return;
     }
-
+    var stripped = qwebirc.global.nicknameValidator.validate(nickname);
+    if(stripped != nickname) {
+      nick.value = stripped;
+      alert("Your nickname was invalid and has been corrected; please check your altered nickname and press Connect again.");
+      nick.focus();
+      return;
+    }
+    
     var data = {"nickname": nickname, "autojoin": chans};
     if(qwebirc.auth.enabled()) {
       if(qwebirc.auth.passAuth() && authCheckBox.checked) {
index 56518a2177660aa8969ee2a4b2370fdbdfa79693..baf4cba3fbd3df562679a15522ff592aaba12738 100644 (file)
@@ -184,6 +184,13 @@ qwebirc.ui.EmbedWizard = new Class({
           this.nicknameBox.focus();
           return false;
         }
+        var v = qwebirc.global.nicknameValidator.validate(this.nicknameBox.value, true);
+        if(v != this.nicknameBox.value) {
+          this.nicknameBox.value = v;
+          alert("The supplied nickname was invalid and has been corrected.");
+          this.nicknameBox.focus();
+          return false;
+         }
         return true;
       }.bind(this),
       middle: this.nicknameBox,