Support ircu's XQUERY and XRESPONSE.
[srvx.git] / src / proto-common.c
index ee089ad4ecaa9368b3d28fdccbbab1bfabc1c384..6039d759158228af5520aa5ea91ccd8a576723e7 100644 (file)
@@ -57,7 +57,7 @@ extern new_user_func_t *nuf_list;
 extern unsigned int nuf_size, nuf_used;
 extern del_user_func_t *duf_list;
 extern unsigned int duf_size, duf_used;
-extern time_t boot_time;
+extern unsigned long boot_time;
 
 void received_ping(void);
 
@@ -105,7 +105,7 @@ void replay_event_loop(void)
     while (!quit_services) {
         if (!replay_connected) {
             /* this time fudging is to get some of the logging right */
-            self->link = self->boot = now;
+            self->link_time = self->boot = now;
             cManager.uplink->state = AUTHENTICATING;
             irc_introduce(cManager.uplink->password);
             replay_connected = 1;
@@ -155,7 +155,7 @@ void
 replay_read_line(void)
 {
     struct tm timestamp;
-    time_t new_time;
+    unsigned long new_time;
 
     if (replay_line[0]) return;
   read_line:
@@ -185,7 +185,7 @@ replay_read_line(void)
     timestamp.tm_year = strtoul(replay_line+16, NULL, 10) - 1900;
     timestamp.tm_isdst = 0;
     new_time = mktime(&timestamp);
-    if (new_time == -1) {
+    if (new_time == (unsigned long)-1) {
         log_module(MAIN_LOG, LOG_ERROR, "Unable to parse time struct tm_sec=%d tm_min=%d tm_hour=%d tm_mday=%d tm_mon=%d tm_year=%d", timestamp.tm_sec, timestamp.tm_min, timestamp.tm_hour, timestamp.tm_mday, timestamp.tm_mon, timestamp.tm_year);
     } else {
         now = new_time;
@@ -353,7 +353,8 @@ static CMD_FUNC(cmd_stats)
         return 0;
     switch (argv[1][0]) {
     case 'u': {
-        unsigned int uptime = now - boot_time;
+        unsigned long uptime;
+        uptime = now - boot_time;
         irc_numeric(un, RPL_STATSUPTIME, ":Server Up %d days %d:%02d:%02d",
                     uptime/(24*60*60), (uptime/(60*60))%24, (uptime/60)%60, uptime%60);
         irc_numeric(un, RPL_MAXCONNECTIONS, ":Highest connection count: %d (%d clients)",
@@ -429,6 +430,7 @@ struct privmsg_desc {
 static void
 privmsg_chan_helper(struct chanNode *cn, void *data)
 {
+    extern unsigned short offchannel_allowed[256];
     struct privmsg_desc *pd = data;
     struct modeNode *mn;
     struct chanmsg_func *cf;
@@ -441,8 +443,10 @@ privmsg_chan_helper(struct chanNode *cn, void *data)
 
     /* Never send a NOTICE to a channel to one of the services */
     cf = &chanmsg_funcs[(unsigned char)pd->text[0]];
-    if (!pd->is_notice && cf->func && GetUserMode(cn, cf->service) && !IsDeaf(cf->service))
-        cf->func(pd->user, cn, pd->text+1, cf->service);
+    if (cf->func && !pd->is_notice
+        && (offchannel_allowed[(unsigned char)pd->text[0]]
+            || (GetUserMode(cn, cf->service) && !IsDeaf(cf->service))))
+        cf->func(pd->user, cn, pd->text+1, cf->service, pd->is_notice);
 
     /* This catches *all* text sent to the channel that the services server sees */
     for (x = 0; x < ALLCHANMSG_FUNCS_MAX; x++) {
@@ -450,7 +454,7 @@ privmsg_chan_helper(struct chanNode *cn, void *data)
        if (!cf->func)
            break; /* end of list */
        else
-           cf->func(pd->user, cn, pd->text, cf->service);
+           cf->func(pd->user, cn, pd->text, cf->service, pd->is_notice);
     }
 }
 
@@ -536,6 +540,64 @@ reg_mode_change_func(mode_change_func_t handler)
     mcf_list[mcf_used++] = handler;
 }
 
+static oper_func_t *of_list;
+static unsigned int of_size = 0, of_used = 0;
+
+void
+reg_oper_func(oper_func_t handler)
+{
+    if (of_used == of_size) {
+        if (of_size) {
+            of_size <<= 1;
+            of_list = realloc(of_list, of_size*sizeof(oper_func_t));
+        } else {
+            of_size = 8;
+            of_list = malloc(of_size*sizeof(oper_func_t));
+        }
+    }
+    of_list[of_used++] = handler;
+}
+
+static void
+call_oper_funcs(struct userNode *user)
+{
+    unsigned int n;
+    if (IsLocal(user))
+        return;
+    for (n=0; (n<of_used) && !user->dead; n++)
+    {
+        of_list[n](user);
+    }
+}
+
+static xquery_func_t *xqf_list;
+static unsigned int xqf_size = 0, xqf_used = 0;
+
+void
+reg_xquery_func(xquery_func_t handler)
+{
+    if (xqf_used == xqf_size) {
+        if (xqf_size) {
+            xqf_size <<= 1;
+            xqf_list = realloc(xqf_list, xqf_size*sizeof(xquery_func_t));
+        } else {
+            xqf_size = 8;
+            xqf_list = malloc(xqf_size*sizeof(xquery_func_t));
+        }
+    }
+    xqf_list[xqf_used++] = handler;
+}
+
+static void
+call_xquery_funcs(struct server *source, const char routing[], const char query[])
+{
+    unsigned int n;
+    for (n=0; n < xqf_used; n++)
+    {
+        xqf_list[n](source, routing, query);
+    }
+}
+
 struct mod_chanmode *
 mod_chanmode_alloc(unsigned int argc)
 {
@@ -703,6 +765,8 @@ generate_hostmask(struct userNode *user, int options)
         ident = user->ident;
     else if (options & GENMASK_ANY_IDENT)
         ident = "*";
+    else if (IsFakeIdent(user) && IsHiddenHost(user) && !(options & GENMASK_NO_HIDING))
+        ident = user->fakeident;
     else {
         ident = alloca(strlen(user->ident)+2);
         ident[0] = '*';
@@ -726,7 +790,7 @@ generate_hostmask(struct userNode *user, int options)
             sprintf(hostname, "%d.%d.*", user->ip.in6_8[12], user->ip.in6_8[13]);
         } else if (irc_in_addr_is_ipv6(user->ip)) {
             /* Who knows what the default mask should be?  Use a /48 to start with. */
-            sprintf(hostname, "%x:%x:%x:*", user->ip.in6[0], user->ip.in6[1], user->ip.in6[2]);
+            sprintf(hostname, "%x:%x:%x:*", ntohs(user->ip.in6[0]), ntohs(user->ip.in6[1]), ntohs(user->ip.in6[2]));
         } else {
             /* Unknown type; just copy IP directly. */
             irc_ntop(hostname, IRC_NTOP_MAX_SIZE, &user->ip);
@@ -784,3 +848,20 @@ IsChannelName(const char *name) {
     }
     return 1;
 }
+
+unsigned int
+irc_user_modes(const struct userNode *user, char modes[], size_t length)
+{
+    unsigned int ii, jj;
+
+    for (ii = jj = 0; (jj < length) && (irc_user_mode_chars[ii] != '\0'); ++ii) {
+        if ((user->modes & (1 << ii)) && (irc_user_mode_chars[ii] != ' '))
+            modes[jj++] = irc_user_mode_chars[ii];
+    }
+
+    ii = jj;
+    while (jj < length)
+        modes[jj++] = '\0';
+
+    return ii;
+}