Fix several bugs; make off-channel a per-channel option
[srvx.git] / src / proto-common.c
index 3fcadd7b5c0b3f2fcd13ec3b26897e8f2e46f879..8f5f1b9a791fd0f5d43a2a3815f1c0d038fb825b 100644 (file)
@@ -81,7 +81,8 @@ uplink_readable(struct io_fd *fd) {
         close_socket();
         return;
     }
-    if ((eol = strpbrk(buffer, "\r\n"))) *eol = 0;
+    if ((eol = strpbrk(buffer, "\r\n")))
+        *eol = 0;
     log_replay(MAIN_LOG, false, buffer);
     if (cManager.uplink->state != DISCONNECTED)
         parse_line(buffer, 0);
@@ -95,7 +96,8 @@ socket_destroyed(struct io_fd *fd)
         log_module(MAIN_LOG, LOG_ERROR, "Connection to server lost.");
     socket_io_fd = NULL;
     cManager.uplink->state = DISCONNECTED;
-    if (self->uplink) DelServer(self->uplink, 0, NULL);
+    if (self->uplink)
+        DelServer(self->uplink, 0, NULL);
 }
 
 void replay_event_loop(void)
@@ -364,34 +366,6 @@ static CMD_FUNC(cmd_stats)
     return 1;
 }
 
-static CMD_FUNC(cmd_whois)
-{
-    struct userNode *from;
-    struct userNode *who;
-
-    if (argc < 3)
-        return 0;
-    if (!(from = GetUserH(origin))) {
-        log_module(MAIN_LOG, LOG_ERROR, "Could not find WHOIS origin user %s", origin);
-        return 0;
-    }
-    if(!(who = GetUserH(argv[2]))) {
-        irc_numeric(from, ERR_NOSUCHNICK, "%s@%s :No such nick", argv[2], self->name);
-        return 1;
-    }
-    if (IsHiddenHost(who) && !IsOper(from)) {
-        /* Just stay quiet. */
-        return 1;
-    }
-    irc_numeric(from, RPL_WHOISUSER, "%s %s %s * :%s", who->nick, who->ident, who->hostname, who->info);
-    irc_numeric(from, RPL_WHOISSERVER, "%s %s :%s", who->nick, who->uplink->name, who->uplink->description);
-    if (IsOper(who)) {
-        irc_numeric(from, RPL_WHOISOPERATOR, "%s :is a megalomaniacal power hungry tyrant", who->nick);
-    }
-    irc_numeric(from, RPL_ENDOFWHOIS, "%s :End of /WHOIS list", who->nick);
-    return 1;
-}
-
 static CMD_FUNC(cmd_version)
 {
     struct userNode *user;
@@ -440,6 +414,11 @@ static struct chanmsg_func {
     struct userNode *service;
 } chanmsg_funcs[256]; /* indexed by trigger character */
 
+static struct allchanmsg_func {
+    chanmsg_func_t func;
+    struct userNode *service;
+} allchanmsg_funcs[ALLCHANMSG_FUNCS_MAX];
+
 struct privmsg_desc {
     struct userNode *user;
     char *text;
@@ -453,6 +432,7 @@ privmsg_chan_helper(struct chanNode *cn, void *data)
     struct privmsg_desc *pd = data;
     struct modeNode *mn;
     struct chanmsg_func *cf = &chanmsg_funcs[(unsigned char)pd->text[0]];
+    int x;
 
     /* Don't complain if it can't find the modeNode because the channel might
      * be -n */
@@ -462,6 +442,15 @@ privmsg_chan_helper(struct chanNode *cn, void *data)
     /* Never send a NOTICE to a channel to one of the services */
     if (!pd->is_notice && cf->func && GetUserMode(cn, cf->service))
         cf->func(pd->user, cn, pd->text+1, cf->service);
+
+    /* This catches *all* text sent to the channel that the services server sees */
+    for (x = 0; x < ALLCHANMSG_FUNCS_MAX; x++) {
+       cf = (struct chanmsg_func *)&allchanmsg_funcs[x];
+       if (!cf->func)
+         break; /* end of list */
+       else
+       cf->func(pd->user, cn, pd->text, cf->service);
+    }
 }
 
 static void
@@ -489,6 +478,19 @@ reg_chanmsg_func(unsigned char prefix, struct userNode *service, chanmsg_func_t
     chanmsg_funcs[prefix].service = service;
 }
 
+void
+reg_allchanmsg_func(struct userNode *service, chanmsg_func_t handler)
+{
+    int x;
+    for (x = 0; x < ALLCHANMSG_FUNCS_MAX; x++) {
+       if (allchanmsg_funcs[x].func)
+         continue;
+       allchanmsg_funcs[x].func = handler;
+       allchanmsg_funcs[x].service = service;
+       break;
+    }
+}
+
 struct userNode *
 get_chanmsg_bot(unsigned char prefix)
 {
@@ -635,8 +637,8 @@ mod_chanmode(struct userNode *who, struct chanNode *channel, char **modes, unsig
 int
 irc_make_chanmode(struct chanNode *chan, char *out) {
     struct mod_chanmode change;
+    mod_chanmode_init(&change);
     change.modes_set = chan->modes;
-    change.modes_clear = change.argc = 0;
     change.new_limit = chan->limit;
     safestrncpy(change.new_key, chan->key, sizeof(change.new_key));
     return strlen(mod_chanmode_format(&change, out));