Subject: [PATCH] Adding per-server email-userid support

From: George Kadianakis <[email-masked]>
Date: Wed, 12 May 2010 16:21:22 +0300

Package: erc
Tags: patch

The attached patch adds support for per-server configuration of the
erc-email-userid variable.
For example: (erc :server "my.irc.net" :port 6667 :nick "test" :full-name "test" :email-userid "test")
will set the email-userid to "test" for the my.irc.net server.

diff --git a/erc-backend.el b/erc-backend.el
index 01b9527..d9e9ec4 100644
--- a/erc-backend.el
+++ b/erc-backend.el
@@ -539,7 +539,7 @@ Make sure you are in an ERC buffer when running this."
       (setq erc-server-last-sent-time 0)
       (setq erc-server-lines-sent 0)
       (erc-open erc-session-server erc-session-port erc-server-current-nick
-                erc-session-user-full-name t erc-session-password))))
+                erc-session-email-userid erc-session-user-full-name t erc-session-password))))
 
 (defun erc-server-filter-function (process string)
   "The process filter for the ERC server."
@@ -1187,7 +1187,7 @@ add things to `%s' instead."
                      ((erc-current-nick-p nick)
                       (setq buffer (erc-open erc-session-server erc-session-port
                                              nick erc-session-user-full-name
-                                             nil nil
+                                             erc-session-email-userid nil nil
                                              erc-default-recipients chnl
                                              erc-server-process))
                       (when buffer
diff --git a/erc.el b/erc.el
index 6e37f36..cae091a 100644
--- a/erc.el
+++ b/erc.el
@@ -1762,6 +1762,10 @@ all channel buffers on all servers."
   "Full name of the user on the current server.")
 (make-variable-buffer-local 'erc-session-user-full-name)
 
+(defvar erc-session-email-userid nil
+  "User identifier of the user on the current server.")
+(make-variable-buffer-local 'erc-session-email-userid)
+
 (defvar erc-channel-user-limit nil
   "Limit of users per channel.")
 (make-variable-buffer-local 'erc-channel-user-limit)
@@ -1934,7 +1938,7 @@ removed from the list will be disabled."
 	     (display-buffer buffer)
 	   (switch-to-buffer buffer)))))
 
-(defun erc-open (&optional server port nick full-name
+(defun erc-open (&optional server port nick full-name email-userid
 			   connect passwd tgt-list channel process)
   "Connect to SERVER on PORT as NICK with FULL-NAME.
 
@@ -2025,7 +2029,7 @@ Returns the buffer for the given server or channel."
       (erc-display-prompt)
       (goto-char (point-max)))
 
-    (erc-determine-parameters server port nick full-name)
+    (erc-determine-parameters server port nick full-name email-userid)
 
     ;; Saving log file on exit
     (run-hook-with-args 'erc-connect-pre-hook buffer)
@@ -2148,6 +2152,7 @@ functions in here get called with the parameters SERVER and NICK."
 (defun* erc (&key (server (erc-compute-server))
 		  (port   (erc-compute-port))
 		  (nick   (erc-compute-nick))
+		  (email-userid (erc-compute-email-userid))
 		  password
 		  (full-name (erc-compute-full-name)))
   "ERC is a powerful, modular, and extensible IRC client.
@@ -2159,6 +2164,7 @@ Non-interactively, it takes the keyword arguments
    (server (erc-compute-server))
    (port   (erc-compute-port))
    (nick   (erc-compute-nick))
+   (email-userid (erc-compute-email-userid))
    password
    (full-name (erc-compute-full-name)))
 
@@ -2167,10 +2173,10 @@ That is, if called with
    (erc :server \"irc.freenode.net\" :full-name \"Harry S Truman\")
 
 then the server and full-name will be set to those values, whereas
-`erc-compute-port', `erc-compute-nick' and `erc-compute-full-name' will
-be invoked for the values of the other parameters."
+`erc-compute-port', `erc-compute-nick', `erc-compute-email-userid' 
+and `erc-compute-full-name' will be invoked for the values of the other parameters."
   (interactive (erc-select-read-args))
-  (erc-open server port nick full-name t password))
+  (erc-open server port nick full-name email-userid t password))
 
 ;;;###autoload
 (defalias 'erc-select 'erc)
@@ -3875,6 +3881,7 @@ To change how this query window is displayed, use `let' to bind
 		       erc-session-port
 		       (erc-current-nick)
 		       erc-session-user-full-name
+		       erc-session-email-userid
 		       nil
 		       nil
 		       (list target)
@@ -5638,14 +5645,12 @@ user input."
   (erc-server-send
    (format "USER %s %s %s :%s"
 	   ;; hacked - S.B.
-	   (if erc-anonymous-login erc-email-userid (user-login-name))
-	   "0" "*"
-	   erc-session-user-full-name))
+	   erc-session-email-userid "0" "*" erc-session-user-full-name))
   (erc-update-mode-line))
 
 ;; connection properties' heuristics
 
-(defun erc-determine-parameters (&optional server port nick name)
+(defun erc-determine-parameters (&optional server port nick name userid)
   "Determine the connection and authentication parameters.
 Sets the buffer local variables:
 
@@ -5655,7 +5660,8 @@ Sets the buffer local variables:
 - `erc-server-current-nick'"
   (setq erc-session-server (erc-compute-server server)
 	erc-session-port (or port erc-default-port)
-	erc-session-user-full-name (erc-compute-full-name name))
+	erc-session-user-full-name (erc-compute-full-name name)
+	erc-session-email-userid (erc-compute-email-userid userid))
   (erc-set-current-nick (erc-compute-nick nick)))
 
 (defun erc-compute-server (&optional server)
@@ -5705,6 +5711,21 @@ non-nil value is found.
       (if erc-anonymous-login "unknown" nil)
       (user-full-name)))
 
+(defun erc-compute-email-userid (&optional email-userid)
+  "Return user's IRC userid
+
+This tries a number of increasingly more default methods until a
+non-nil value is found.
+
+- EMAIL-USERID (the argument passed to this function)
+- The `erc-email-userid' option
+- The value of the IRCEMAILUSERID environment variable
+- The result from the `user-login-name' function"
+  (or email-userid
+      erc-email-userid
+      (getenv "IRCEMAILUSERID")
+      (user-login-name)))
+
 (defun erc-compute-port (&optional port)
   "Return a port for an IRC server.
 
@@ -6511,7 +6532,7 @@ Otherwise, connect to HOST:PORT as USER and /join CHANNEL."
       (if (and server-buffer channel)
 	  (erc-cmd-JOIN channel)
 	(erc-open host port (or user (erc-compute-nick)) (erc-compute-full-name)
-		  (not server-buffer) password nil channel
+		  (erc-compute-email-userid) (not server-buffer) password nil channel
 		  (when server-buffer
 		    (get-buffer-process server-buffer)))))))