gcfavorites / dircproxy

Automatically exported from code.google.com/p/dircproxy
GNU General Public License v2.0
0 stars 0 forks source link

nickserv_password is too narrow, drop this option and add generic sending of arbitrary commands upon connect #66

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
nickserv_password is nice for networks with nickserv, but what about other 
services (X, Q, etc.)?

I think it would be better to implement a generic solution that can be 
used for any service + whatever else.

My quick and very dirty hack implements options "connect_command1 to 
connect_command7" and removes nickserv_password.

Additionally any occurrence of "$nick" in those options gets replaced by 
the current nickname so commands that require a nickname can be used as 
well.

Example of option usage:

connect_command1 "PRIVMSG NickServ :IDENTIFY <user> <pass>"
connect_command2 "OPER <user> :<pass>"
connect_command3 "CHGIDENT $nick :<newident>"

or 

connect_command1 "PRIVMSG X@channels.undernet.org :LOGIN <user> <pass>"

The default config file could have examples of commands for the different 
major networks and their services.

A better way to implement this would be to make connect_command a list and 
allow duplication of the option in the config file for as much commands as 
needed.

Diff:

--- ./dircproxy-1.2.0-RC1/src/irc_server.c      2009-01-15 
21:27:28.000000000 +0100
+++ ./dircproxy-1.2.0-RC1+raphmod-0.2/src/irc_server.c  2009-03-19 
15:03:19.000000000 +0100
@@ -530,6 +530,34 @@
     if (p->modes)
       ircserver_send_command(p, "MODE", "%s +%s", p->nickname, p->modes);

+      /* Send arbitrary command on connect1 */
+      if (p->conn_class->connect_command1)
+         ircserver_send_command(p, "", "%s",p->conn_class-
>connect_command1);
+
+      /* Send arbitrary command on connect2 */
+      if (p->conn_class->connect_command2)
+         ircserver_send_command(p, "", "%s",p->conn_class-
>connect_command2);
+
+      /* Send arbitrary command on connect3 */
+      if (p->conn_class->connect_command3)
+         ircserver_send_command(p, "", "%s",p->conn_class-
>connect_command3);
+
+      /* Send arbitrary command on connect4 */
+      if (p->conn_class->connect_command4)
+         ircserver_send_command(p, "", "%s",p->conn_class-
>connect_command4);
+
+      /* Send arbitrary command on connect5 */
+      if (p->conn_class->connect_command5)
+         ircserver_send_command(p, "", "%s",p->conn_class-
>connect_command5);
+
+      /* Send arbitrary command on connect6 */
+      if (p->conn_class->connect_command6)
+         ircserver_send_command(p, "", "%s",p->conn_class-
>connect_command6);
+
+      /* Send arbitrary command on connect7 */
+      if (p->conn_class->connect_command7)
+         ircserver_send_command(p, "", "%s",p->conn_class-
>connect_command7);
+
     /* Restore the away message */
     if (p->awaymessage) {
       ircserver_send_command(p, "AWAY", ":%s", p->awaymessage);
@@ -1718,6 +1742,24 @@
   return 1;
 }

+const char *replace_str(char *str, const char *orig, const char *rep)
+{
+  static char buffer[4096];
+  char *p;
+
+//  debug("!! '%s %s'", str, orig, rep);
+
+  if(!(p = strstr(str, orig)))
+    return str;
+
+  strncpy(buffer, str, p-str); // Copy characters from 'str' start 
to 'orig' st$
+  buffer[p-str] = '\0';
+
+  sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
+
+  return buffer;
+}
+ 
 /* send a command to the server with no prefix */
 int ircserver_send_command(struct ircproxy *p, const char *command,
                                    const char *format, ...) {
@@ -1728,9 +1770,14 @@
   va_start(ap, format);
   msg = x_vsprintf(format, ap);
   va_end(ap);
-
-  ret = net_send(p->server_sock, "%s %s\r\n", command, msg);
-  debug("-> '%s %s'", command, msg);
+  
+  if (strlen(command)==0) {
+    ret = net_send(p->server_sock, "%s\r\n", replace_str(msg, "$nick", p-
>nickname)); 
+    debug("-> '%s'", replace_str(msg, "$nick", p->nickname));
+  } else {
+    ret = net_send(p->server_sock, "%s %s\r\n", command, msg);
+    debug("-> '%s %s'", command, msg);
+  }

   free(msg);
   return ret;
--- ./dircproxy-1.2.0-RC1/src/irc_client.c      2009-01-15 
21:27:28.000000000 +0100
+++ ./dircproxy-1.2.0-RC1+raphmod-0.2/src/irc_client.c  2009-03-14 
16:05:10.000000000 +0100
@@ -780,10 +780,6 @@
       free(tmp_p->oldnickname);
       tmp_p->oldnickname = 0;

-      /* Notify nickserv */ 
-      if (tmp_p->conn_class->nickserv_password) 
-        ircserver_send_command(tmp_p, "PRIVMSG", " %s :IDENTIFY %
s", "NICKSERV",tmp_p->conn_class->nickserv_password); 
-
       /* Unset any away message if we set one */
       if (!tmp_p->awaymessage && (tmp_p->server_status == 
IRC_SERVER_ACTIVE)
           && tmp_p->conn_class->away_message)
@@ -915,10 +911,6 @@
       if (p->conn_class->initial_modes)M
         ircclient_change_mode(p, p->conn_class->initial_modes);

-      /* Notify nickserv */
-      // currently broken i will look in next revision
-      /* if (p->conn_class->nickserv_password) */
-      /* ircserver_send_command(p, "PRIVMSG", " %s :IDENTIFY %
s", "NICKSERV",p->conn_class->nickserv_password); */
     }

     return 0;
--- ./dircproxy-1.2.0-RC1/src/cfgfile.c 2009-01-15 21:27:28.000000000 +0100
+++ ./dircproxy-1.2.0-RC1+raphmod-0.2/src/cfgfile.c     2009-03-14 
14:36:18.000000000 +0100
@@ -102,7 +102,13 @@
   def->detach_nickname = (DEFAULT_DETACH_NICKNAME
                           ? x_strdup(DEFAULT_DETACH_NICKNAME) : 0);
   def->nick_keep = DEFAULT_NICK_KEEP;
-  def->nickserv_password = NULL;
+  def->connect_command1 = NULL;
+  def->connect_command2 = NULL;
+  def->connect_command3 = NULL;
+  def->connect_command4 = NULL;
+  def->connect_command5 = NULL;
+  def->connect_command6 = NULL;
+  def->connect_command7 = NULL;
   def->ctcp_replies = DEFAULT_CTCP_REPLIES;  
   def->log_timestamp = DEFAULT_LOG_TIMESTAMP;
   def->log_relativetime = DEFAULT_LOG_RELATIVETIME;
@@ -520,23 +526,104 @@
         free((class ? class : def)->detach_nickname);
         (class ? class : def)->detach_nickname = str;

-      } else if  (!strcasecmp(key, "nickserv_password")) {           
-        /* nickserv_password none
-        * nickserv_password ""    # same as none
-        * nickserv_password "identify_me" */
-       char *str;
+      } else if  (!strcasecmp(key, "connect_command1")) {
+        char *str;
+
+        if (_cfg_read_string(&buf, &str))
+          UNMATCHED_QUOTE;

-       if (_cfg_read_string(&buf, &str))
-         UNMATCHED_QUOTE;
+        if (!strcasecmp(str, "none") || !strlen(str)) {
+           free(str);
+           str = 0;  
+        }
+
+       free((class ? class : def)->connect_command1);
+       (class ? class : def)->connect_command1 = str;
+
+      } else if  (!strcasecmp(key, "connect_command2")) {
+        char *str;
+
+        if (_cfg_read_string(&buf, &str))
+          UNMATCHED_QUOTE;

-       if (!strcasecmp(str, "none") || !strlen(str)) {
-          free(str);
-          str = 0;  
-       }
-              
-       free((class ? class : def)->nickserv_password);
-       (class ? class : def)->nickserv_password = str;
-        
+        if (!strcasecmp(str, "none") || !strlen(str)) {
+           free(str);
+           str = 0;  
+        }
+
+       free((class ? class : def)->connect_command2);
+       (class ? class : def)->connect_command2 = str;
+
+      } else if  (!strcasecmp(key, "connect_command3")) {
+        char *str;
+
+        if (_cfg_read_string(&buf, &str))
+          UNMATCHED_QUOTE;
+       
+        if (!strcasecmp(str, "none") || !strlen(str)) {
+           free(str);
+           str = 0;  
+        }
+
+       free((class ? class : def)->connect_command3);
+       (class ? class : def)->connect_command3 = str;
+
+      } else if  (!strcasecmp(key, "connect_command4")) {
+        char *str;
+
+        if (_cfg_read_string(&buf, &str))
+          UNMATCHED_QUOTE;
+       
+        if (!strcasecmp(str, "none") || !strlen(str)) {
+           free(str);
+           str = 0;  
+        }
+
+       free((class ? class : def)->connect_command4);
+       (class ? class : def)->connect_command4 = str;
+
+      } else if  (!strcasecmp(key, "connect_command5")) {
+        char *str;
+
+        if (_cfg_read_string(&buf, &str))
+          UNMATCHED_QUOTE;
+       
+        if (!strcasecmp(str, "none") || !strlen(str)) {
+           free(str);
+           str = 0;  
+        }
+
+       free((class ? class : def)->connect_command5);
+       (class ? class : def)->connect_command5 = str;
+
+      } else if  (!strcasecmp(key, "connect_command6")) {
+        char *str;
+
+        if (_cfg_read_string(&buf, &str))
+          UNMATCHED_QUOTE;
+       
+        if (!strcasecmp(str, "none") || !strlen(str)) {
+           free(str);
+           str = 0;  
+        }
+
+       free((class ? class : def)->connect_command6);
+       (class ? class : def)->connect_command6 = str;
+
+      } else if  (!strcasecmp(key, "connect_command7")) {
+        char *str;
+
+        if (_cfg_read_string(&buf, &str))
+          UNMATCHED_QUOTE;
+       
+        if (!strcasecmp(str, "none") || !strlen(str)) {
+           free(str);
+           str = 0;  
+        }
+
+       free((class ? class : def)->connect_command7);
+       (class ? class : def)->connect_command7 = str;
+
       }         else if (!strcasecmp(key, "nick_keep")) {
         /* nick_keep yes
            nick_keep no */
--- ./dircproxy-1.2.0-RC1/src/irc_net.c 2009-01-15 21:27:28.000000000 +0100
+++ ./dircproxy-1.2.0-RC1+raphmod-0.2/src/irc_net.c     2009-03-14 
14:38:39.000000000 +0100
@@ -518,7 +531,13 @@
   free(class->motd_file);

   free(class->orig_local_address);
-  free(class->nickserv_password); 
+  free(class->connect_command1);  
+  free(class->connect_command2);  
+  free(class->connect_command3);  
+  free(class->connect_command4);  
+  free(class->connect_command5);  
+  free(class->connect_command6);  
+  free(class->connect_command7);  
   free(class->password);
   s = class->servers;   
   while (s) {
--- ./dircproxy-1.2.0-RC1/src/irc_net.h 2009-01-15 21:27:28.000000000 +0100
+++ ./dircproxy-1.2.0-RC1+raphmod-0.2/src/irc_net.h     2009-03-14 
16:04:05.000000000 +0100
@@ -77,7 +77,13 @@

   int nick_keep;

-  char *nickserv_password;
+  char *connect_command1; 
+  char *connect_command2;
+  char *connect_command3;
+  char *connect_command4; 
+  char *connect_command5; 
+  char *connect_command6; 
+  char *connect_command7; 

   int ctcp_replies;

Original issue reported on code.google.com by raphidae on 19 Mar 2009 at 2:22

GoogleCodeExporter commented 9 years ago
I registered another account for googlecode (this one).

Original comment by r...@mediamonks.net on 19 Mar 2009 at 2:32

GoogleCodeExporter commented 9 years ago
Great idea. but i dont like *1, *2, *3,*4

Currently we can have multiple config with the same name (using linked list, as
server and others) so, maybe we can only have
connect_command "command 1"
connect_command "command 2"
...
connect_command "command 42"

we execute the commands, in the same orders from the config file.

I Will look at the patch

Thanks

Original comment by francois...@gtempaccount.com on 27 Mar 2009 at 4:26

GoogleCodeExporter commented 9 years ago
Yeah, that was I was meaning to do, but not I just made it a quick hack to get 
what 
I want. Shall I make it so it takes multiple connect_commands and send in the 
patch?

Original comment by raphidae on 27 Mar 2009 at 7:33

GoogleCodeExporter commented 9 years ago
I consider this a request for enhancement, rather than being a defect :)

Original comment by noel.w8tvi on 1 Apr 2009 at 12:46