"use strict";
var q = require("q");

// The max length of <realname> in USER commands
var MAX_REAL_NAME_LENGTH = 48;
// The max length of <username> in USER commands
var MAX_USER_NAME_LENGTH = 32;

module.exports = {
    /**
     * Get the IRC name info for this user.
     * @param {IrcServer} server The IRC server being connected to.
     * @param {Object} ircUser The IRC user to connect as.
     * @param {Object} matrixUser Optional. The matrix user.
     * @return {Promise} Resolves to {
     *   nick: 'nick_to_use',
     *   username: 'username_to_use',
     *   realname: 'realname_to_use'
     * }
     */
    getIrcNames: function(ircUser, matrixUser) {
        var info = {};
        // strip illegal chars according to RFC 1459 Sect 2.3.1
        // but allow _ because most IRC servers allow that.
        info.nick = ircUser.nick.replace(/[^A-Za-z0-9\]\[\^\\\{\}\-`_]/g, "");
        var username = matrixUser ? matrixUser.userId : ircUser.username;
        // real name can be any old ASCII
        var realname = username.replace(/[^\x00-\x7F]/g, "");
        // strip out bad characters in the username (will need to do something
        // better like punycode with win95 style LONGNAM~1 in the future)
        username = username.replace(/:/g, "__");
        username = username.replace(/[^A-Za-z0-9\]\[\^\\\{\}\-`_]/g, "");

        info.username = username.substring(0, MAX_USER_NAME_LENGTH);
        info.realname = realname.substring(0, MAX_REAL_NAME_LENGTH);
        return q(info);
    }
};

@@ -190,10 +190,520 @@
 H = 
-32
+10;%0A%0Avar getIdentUsername = function(domain, userId) %7B%0A    var uname = sanitiseUsername(userId.replace(/:/g, %22__%22));%0A    return uname;%0A%7D;%0A%0Avar sanitiseUsername = function(username) %7B%0A    // strip illegal chars according to RFC 1459 Sect 2.3.1%0A    // (technically it's any %3Cnonwhite%3E ascii for %3Cuser%3E but meh)%0A    return username.replace(/%5B%5EA-Za-z0-9%5C%5D%5C%5B%5C%5E%5C%5C%5C%7B%5C%7D%5C-%60_%5D/g, %22%22);%0A%7D;%0A%0Avar sanitiseRealname = function(realname) %7B%0A    // real name can be any old ASCII%0A    return realname.replace(/%5B%5E%5Cx00-%5Cx7F%5D/g, %22%22);%0A%7D
 ;%0A%0Am
@@ -720,16 +720,17 @@
 rts = %7B%0A
+%0A
     /**%0A
@@ -1409,56 +1409,111 @@
     
-var username = matrixUser ? matrixUser.userId : 
+if (matrixUser) %7B%0A            if (ircUser.username) %7B%0A                info.username = sanitiseUsername(
 ircU
@@ -1524,16 +1524,17 @@
 username
+)
 ;%0A      
@@ -1539,265 +1539,296 @@
     
+ 
-// real name can be any old ASCII%0A        var realname = username.replace(/%5B%5E%5Cx00-%5Cx7F%5D/g, %22%22);%0A        // strip out bad characters in the username (will need to do something%0A        // better like punycode with win95 style LONGNAM~1 in the future)%0A
+   %7D%0A            else %7B // generate one.%0A                info.username = getIdentUsername(%0A                    ircUser.server.domain, matrixUser.userId%0A                );%0A            %7D%0A            info.realname = sanitiseRealname(matrixUser.userId);%0A        %7D%0A        else %7B%0A    
         
 user
@@ -1815,32 +1815,37 @@
 e %7B%0A            
+info.
 username = usern
@@ -1843,35 +1843,106 @@
 e = 
-username.replace(/:/g, %22__%22
+sanitiseUsername(%0A                ircUser.username // the bridge won't have a mx user%0A            
 );%0A 
@@ -1952,73 +1952,70 @@
     
+ 
-username = username.replace(/%5B%5EA-Za-z0-9%5C%5D%5C%5B%5C%5E%5C%5C%5C%7B%5C%7D%5C-%60_%5D/g, %22%22);
+   info.realname = sanitiseRealname(info.username);%0A        %7D
 %0A%0A  
@@ -2036,16 +2036,21 @@
 rname = 
+info.
 username
@@ -2110,16 +2110,21 @@
 lname = 
+info.
 realname
@@ -2152,32 +2152,33 @@
 L_NAME_LENGTH);%0A
+%0A
         return q

Refactor names module