]> jfr.im git - irc/evilnet/x3.git/blobdiff - src/global.c
Couple of srvx updates.
[irc/evilnet/x3.git] / src / global.c
index 75e82457480d43f2195c583a1d403a283e9b25d5..c0a8e5ff898c5a4d03180e0f1e770684f5275fb7 100644 (file)
@@ -1,11 +1,11 @@
 /* global.c - Global notice service
  * Copyright 2000-2004 srvx Development Team
  *
- * This file is part of srvx.
+ * This file is part of x3.
  *
- * srvx is free software; you can redistribute it and/or modify
+ * x3 is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -20,6 +20,7 @@
 
 #include "conf.h"
 #include "global.h"
+#include "hash.h"
 #include "modcmd.h"
 #include "nickserv.h"
 #include "saxdb.h"
@@ -55,21 +56,81 @@ static const struct message_entry msgtab[] = {
     { "GMSG_ID_INVALID", "$b%s$b is an invalid message ID." },
     { "GMSG_MESSAGE_COUNT", "$b%d$b messages found." },
     { "GMSG_NO_MESSAGES", "There are no messages for you." },
-    { "GMSG_NOTICE_SOURCE", "[$b%s$b] Notice from %s:" },
-    { "GMSG_MESSAGE_SOURCE", "[$b%s$b] Notice from %s, posted %s:" },
-    { "GMSG_MOTD_HEADER", "$b------------- MESSAGE(S) OF THE DAY --------------$b" },
-    { "GMSG_MOTD_FOOTER", "$b---------- END OF MESSAGE(S) OF THE DAY ----------$b" },
+    { "GMSG_NOTICE_SOURCE", "Notice to [$b%s$b] from %s:" },
+    { "GMSG_MESSAGE_SOURCE", "Notice to [$b%s$b] from %s, posted %s:" },
+    //{ "GMSG_MOTD_HEADER", "$b------------- MESSAGE(S) OF THE DAY --------------$b" },
+    { "GMSG_MOTD_HEADER", "$bNetwork Announcements$b" },
+    { "GMSG_MOTD_BAR",    "---------------------------------------" },
+    { "GMSG_MOTD_FOOTER", "--------------- Thank You--------------" },
+
+ /* These definitions are for other files that make use of global
+  * notices. Make sure you grep for them if you ever add args
+  * to the notice.
+  */
+    /* chanserv.c */
+    { "CSMSG_REGISTERED_TO", "%s registered to %s by %s." },
+    { "CSMSG_CHANNEL_MOVED", "%s moved to %s by %s." },
+    { "CSMSG_SUSPENSION_MODIFIED", "%s suspension modified by %s." },
+    { "CSMSG_SUSPENDED_BY", "%s suspended by %s." },
+    { "CSMSG_UNSUSPENDED_BY", "%s unsuspended by %s." },
+    { "CSMSG_OWNERSHIP_TRANSFERRED", "%s ownership transferred to %s by %s." },
+
+    /* mod-helpserv.c */
+    { "HSMSG_BOT_RENAMED", "HelpServ bot %s (in %s) renamed to %s by %s." },
+    { "HSMSG_BOT_MOVED", "HelpServ %s (%s) moved to %s by %s." },
+    { "HSMSG_BOT_REGISTERED", "HelpServ %s (%s) registered to %s by %s." },
+    { "HSMSG_BOT_EXPIRED", "HelpServ %s (%s) expired at request of %s." },
+    { "HSMSG_BOT_UNREGISTERED", "HelpServ %s (%s) unregistered by %s." },
+    { "HSMSG_SUSPENDED_BY", "%s suspended by %s. (HelpServ)" },
+    { "HSMSG_UNSUSPENDED_BY", "%s unsuspended by %s. (HelpServ)" },
+
+    /* nickserv.c */
+    { "NSMSG_ACCOUNT_RENAMED", "%s renamed account %s to %s." },
+    { "NSMSG_ACCOUNT_MERGED", "%s (%s) merged account %s into %s." },
+
+    /* opserv.c */
+    { "DEFCON_NETWORK_CHANGED", "Network DefCon level has changed to level %d" },
+    { "DEFCON_OPER_LEVEL_CHANGE", "%s is changing the DefCon level to %d" },
+    { "DEFCON_TIMEOUT_LEVEL_CHANGE", "The DefCon has changed back to level %d (timeout)" },
+    { "OSMSG_CHANNEL_ACTIVITY_WARN", "Channel activity warning for channel %s: %s" },
+
+    /* spamserv.c */
+    { "SSMSG_CHANNEL_MERGED", "$X (channel %s) merged into %s by %s." },
+    { "SSMSG_CHANNEL_MOVED", "$X (channel %s) moved into %s by %s." },
+    { "SSMSG_UNREG_MANUAL", "$X (channel %s) %s by %s." },
+    { "SSMSG_REG_EXPIRED", "$X (channel %s) registration expired." },
+    { "SSMSG_LOST_ALL_USERS", "$X (channel %s) lost all users." },
+    { "SSMSG_REGISTERED_BY", "$X (channel %s) registered by %s." },
+    { "SSMSG_UNREGISTERED_BY", "$X (channel %s) unregistered by %s." },
+
     { NULL, NULL }
 };
 
-#define GLOBAL_SYNTAX()   svccmd_send_help(user, global, cmd)
+#define GLOBAL_SYNTAX()   svccmd_send_help_brief(user, global, cmd)
 #define GLOBAL_FUNC(NAME) MODCMD_FUNC(NAME)
 
+struct globalMessage
+{
+    unsigned long                              id;
+    long                               flags;
+
+    time_t                              posted;
+    char                               posted_s[24];
+    unsigned long                      duration;
+
+    char                               *from;
+    char                               *message;
+
+    struct globalMessage               *prev;
+    struct globalMessage               *next;
+};
+
 struct userNode *global;
 
 static struct module *global_module;
 static struct service *global_service;
 static struct globalMessage *messageList;
+extern struct string_list *autojoin_channels;
 static long messageCount;
 static time_t last_max_alert;
 static struct log_type *G_LOG;
@@ -80,7 +141,11 @@ static struct
     unsigned int announcements_default : 1;
 } global_conf;
 
-#define global_notice(target, format...) send_message(target , global , ## format)
+#if defined(GCC_VARMACROS)
+# define global_notice(target, ARGS...) send_message(target, global, ARGS)
+#elif defined(C99_VARMACROS)
+# define global_notice(target, ...) send_message(target, global, __VA_ARGS__)
+#endif
 
 void message_expire(void *data);
 
@@ -88,9 +153,9 @@ static struct globalMessage*
 message_add(long flags, time_t posted, unsigned long duration, char *from, const char *msg)
 {
     struct globalMessage *message;
+    struct tm tm;
 
     message = malloc(sizeof(struct globalMessage));
-
     if(!message)
     {
        return NULL;
@@ -103,11 +168,16 @@ message_add(long flags, time_t posted, unsigned long duration, char *from, const
     message->from = strdup(from);
     message->message = strdup(msg);
 
+    if ((flags & MESSAGE_OPTION_IMMEDIATE) == 0) {
+        localtime_r(&message->posted, &tm);
+        strftime(message->posted_s, sizeof(message->posted_s),
+                 "%I:%M %p, %m/%d/%Y", &tm);
+    }
+
     if(messageList)
     {
        messageList->prev = message;
     }
-
     message->prev = NULL;
     message->next = messageList;
 
@@ -152,9 +222,12 @@ message_create(struct userNode *user, unsigned int argc, char *argv[])
 {
     unsigned long duration = 0;
     char *text = NULL;
+    char *sender;
     long flags = 0;
     unsigned int i;
 
+    sender = user->handle_info->handle;
+
     for(i = 0; i < argc; i++)
     {
        if((i + 1) > argc)
@@ -176,6 +249,8 @@ message_create(struct userNode *user, unsigned int argc, char *argv[])
 
            if(!irccasecmp(argv[i], "all")) {
                flags |= MESSAGE_RECIPIENT_ALL;
+            } else if(!irccasecmp(argv[i], "authed")) {
+                flags |= MESSAGE_RECIPIENT_AUTHED;
            } else if(!irccasecmp(argv[i], "users")) {
                flags |= MESSAGE_RECIPIENT_LUSERS;
            } else if(!irccasecmp(argv[i], "helpers")) {
@@ -186,6 +261,8 @@ message_create(struct userNode *user, unsigned int argc, char *argv[])
                flags |= MESSAGE_RECIPIENT_STAFF;
            } else if(!irccasecmp(argv[i], "channels")) {
                flags |= MESSAGE_RECIPIENT_CHANNELS;
+           } else if(!irccasecmp(argv[i], "rchannels")) {
+               flags |= MESSAGE_RECIPIENT_RCHANNELS;
             } else if(!irccasecmp(argv[i], "announcement") || !irccasecmp(argv[i], "announce")) {
                 flags |= MESSAGE_RECIPIENT_ANNOUNCE;
            } else {
@@ -194,6 +271,8 @@ message_create(struct userNode *user, unsigned int argc, char *argv[])
            }
        } else if (irccasecmp(argv[i], "duration") == 0) {
            duration = ParseInterval(argv[++i]);
+        } else if (irccasecmp(argv[i], "from") == 0) {
+            sender = argv[++i];
        } else {
            global_notice(user, "MSG_INVALID_CRITERIA", argv[i]);
            return NULL;
@@ -210,7 +289,7 @@ message_create(struct userNode *user, unsigned int argc, char *argv[])
        return NULL;
     }
 
-    return message_add(flags, now, duration, user->handle_info->handle, text);
+    return message_add(flags, now, duration, sender, text);
 }
 
 static const char *
@@ -240,6 +319,14 @@ messageType(const struct globalMessage *message)
     {
        return "users";
     }
+    else if(message->flags & MESSAGE_RECIPIENT_AUTHED)
+    {
+        return "authed";
+    }
+    else if(message->flags & MESSAGE_RECIPIENT_RCHANNELS)
+    {
+        return "rchannels";
+    }
     else
     {
        return "channels";
@@ -257,28 +344,13 @@ notice_target(const char *target, struct globalMessage *message)
        }
        else
        {
-           char posted[24];
-           struct tm tm;
-
-           localtime_r(&message->posted, &tm);
-           strftime(posted, sizeof(posted), "%I:%M %p, %m/%d/%Y", &tm);
-           send_target_message(0, target, global, "GMSG_MESSAGE_SOURCE", messageType(message), message->from, posted);
+           send_target_message(0, target, global, "GMSG_MESSAGE_SOURCE", messageType(message), message->from, message->posted_s);
        }
     }
 
     send_target_message(4, target, global, "%s", message->message);
 }
 
-static int
-notice_channel(const char *key, void *data, void *extra)
-{
-    struct chanNode *channel = data;
-    /* It should be safe to assume channel is not NULL. */
-    if(channel->channel_info)
-       notice_target(key, extra);
-    return 0;
-}
-
 static void
 message_send(struct globalMessage *message)
 {
@@ -288,7 +360,25 @@ message_send(struct globalMessage *message)
 
     if(message->flags & MESSAGE_RECIPIENT_CHANNELS)
     {
-       dict_foreach(channels, notice_channel, message);
+        dict_iterator_t it;
+
+        for (it = dict_first(channels); it; it = iter_next(it)) {
+            struct chanNode *chan = iter_data(it);
+
+            notice_target(chan->name, message);
+        }
+    }
+
+    if(message->flags & MESSAGE_RECIPIENT_RCHANNELS)
+    {
+        dict_iterator_t it;
+
+        for (it = dict_first(channels); it; it = iter_next(it)) {
+            struct chanNode *chan = iter_data(it);
+
+            if (chan->channel_info)
+                notice_target(chan->name, message);
+        }
     }
 
     if(message->flags & MESSAGE_RECIPIENT_LUSERS)
@@ -334,6 +424,83 @@ message_send(struct globalMessage *message)
            notice_target(user->nick, message);
        }
     }
+
+    if(message->flags & MESSAGE_RECIPIENT_AUTHED)
+    {
+        dict_iterator_t it;
+        for (it = dict_first(clients); it; it = iter_next(it)) {
+            struct userNode *luser = iter_data(it);
+            if (luser->handle_info)
+              notice_target(luser->nick, message);
+        }
+    }
+}
+
+void
+global_message_args(long targets, const char *language_entry, ...)
+{
+    struct globalMessage *message = NULL;
+    va_list arg_list;
+    dict_iterator_t it;
+    char response[MAXLEN];
+    const char *fmt;
+
+    if(!targets || !global)
+       return;
+
+    fmt = strdup(language_entry);
+
+    /* Notice users/opers/helpers */
+    for (it = dict_first(clients); it; it = iter_next(it)) {
+        struct userNode *luser = iter_data(it);
+
+        language_entry = user_find_message(luser, fmt);
+
+        va_start(arg_list, language_entry);
+        vsnprintf(response, MAXLEN-2, language_entry, arg_list);
+        response[MAXLEN-1] = 0;
+
+        if (message)
+            message_del(message);
+
+        message = message_add(targets | MESSAGE_OPTION_SOURCELESS, now, 0, "", response);
+        if (!message)
+           continue;
+
+        /* opers */
+        if(message->flags & MESSAGE_RECIPIENT_OPERS && IsOper(luser)) {
+            if(luser->uplink != self)
+                notice_target(luser->nick, message);
+
+            if ((message->flags & MESSAGE_RECIPIENT_LUSERS) || (message->flags & MESSAGE_RECIPIENT_HELPERS) || 
+                (message->flags & MESSAGE_RECIPIENT_AUTHED))
+                continue;
+        }
+
+        /* helpers */
+        if (message->flags & MESSAGE_RECIPIENT_HELPERS && IsHelper(luser)) {
+           notice_target(luser->nick, message);
+
+            if ((message->flags & MESSAGE_RECIPIENT_LUSERS) || (message->flags & MESSAGE_RECIPIENT_AUTHED))
+                continue;
+       }
+
+        /* authed */
+        if ((message->flags & MESSAGE_RECIPIENT_AUTHED) && luser->handle_info) {
+            notice_target(luser->nick, message);
+
+            if (message->flags & MESSAGE_RECIPIENT_LUSERS)
+                continue;
+        }
+
+        /* users */
+        if (message->flags & MESSAGE_RECIPIENT_LUSERS) {
+           notice_target(luser->nick, message);
+
+        }
+    }
+
+    message_del(message);
 }
 
 void
@@ -356,13 +523,17 @@ static GLOBAL_FUNC(cmd_notice)
 {
     struct globalMessage *message = NULL;
     const char *recipient = NULL, *text;
+    char *sender;
     long target = 0;
 
     assert(argc >= 3);
+    sender = user->handle_info->handle;
     if(!irccasecmp(argv[1], "all")) {
        target = MESSAGE_RECIPIENT_ALL;
     } else if(!irccasecmp(argv[1], "users")) {
        target = MESSAGE_RECIPIENT_LUSERS;
+    } else if(!irccasecmp(argv[1], "authed")) {
+        target = MESSAGE_RECIPIENT_AUTHED;
     } else if(!irccasecmp(argv[1], "helpers")) {
        target = MESSAGE_RECIPIENT_HELPERS;
     } else if(!irccasecmp(argv[1], "opers")) {
@@ -373,21 +544,29 @@ static GLOBAL_FUNC(cmd_notice)
         target |= MESSAGE_RECIPIENT_ANNOUNCE;
     } else if(!irccasecmp(argv[1], "channels")) {
        target = MESSAGE_RECIPIENT_CHANNELS;
+    } else if(!irccasecmp(argv[1], "rchannels")) {
+       target = MESSAGE_RECIPIENT_RCHANNELS;
     } else {
        global_notice(user, "GMSG_INVALID_TARGET", argv[1]);
        return 0;
     }
+    if(!irccasecmp(argv[2], "from")) {
+        if (argc < 5) {
+            reply("MSG_MISSING_PARAMS", argv[0]);
+            GLOBAL_SYNTAX();
+            return 0;
+        }
+        sender = argv[3];
+        text = unsplit_string(argv + 4, argc - 4, NULL);
+    } else {
+        text = unsplit_string(argv + 2, argc - 2, NULL);
+    }
 
-    text = unsplit_string(argv + 2, argc - 2, NULL);
-    message = message_add(target | MESSAGE_OPTION_IMMEDIATE, now, 0, user->handle_info->handle, text);
-
+    message = message_add(target | MESSAGE_OPTION_IMMEDIATE, now, 0, sender, text);
     if(!message)
-    {
        return 0;
-    }
 
     recipient = messageType(message);
-
     message_send(message);
     message_del(message);
 
@@ -503,7 +682,10 @@ send_messages(struct userNode *user, long mask, int obstreperize)
        if(message->flags & mask)
        {
             if (obstreperize && !count)
+           {
                 send_target_message(0, user->nick, global, "GMSG_MOTD_HEADER");
+                send_target_message(0, user->nick, global, "GMSG_MOTD_BAR");
+           }
            notice_target(user->nick, message);
            count++;
        }
@@ -517,7 +699,7 @@ send_messages(struct userNode *user, long mask, int obstreperize)
 
 static GLOBAL_FUNC(cmd_messages)
 {
-    long mask = MESSAGE_RECIPIENT_LUSERS | MESSAGE_RECIPIENT_CHANNELS;
+    long mask = MESSAGE_RECIPIENT_AUTHED | MESSAGE_RECIPIENT_LUSERS | MESSAGE_RECIPIENT_CHANNELS | MESSAGE_RECIPIENT_RCHANNELS;
     unsigned int count;
 
     if(IsOper(user))
@@ -536,7 +718,7 @@ static GLOBAL_FUNC(cmd_messages)
 }
 
 static int
-global_process_user(struct userNode *user)
+global_process_user(struct userNode *user, UNUSED_ARG(void *extra))
 {
     if(IsLocal(user) || self->uplink->burst || user->uplink->burst)
         return 0;
@@ -558,14 +740,14 @@ global_process_user(struct userNode *user)
 }
 
 static void
-global_process_auth(struct userNode *user, UNUSED_ARG(struct handle_info *old_handle))
+global_process_auth(struct userNode *user, UNUSED_ARG(struct handle_info *old_handle), UNUSED_ARG(void *extra))
 {
     if(IsHelper(user))
        send_messages(user, MESSAGE_RECIPIENT_HELPERS, 0);
 }
 
 static void
-global_process_oper(struct userNode *user)
+global_process_oper(struct userNode *user, UNUSED_ARG(void *extra))
 {
     if(user->uplink->burst)
         return;
@@ -649,7 +831,7 @@ global_saxdb_write(struct saxdb_context *ctx)
 }
 
 static void
-global_db_cleanup(void)
+global_db_cleanup(UNUSED_ARG(void *extra))
 {
     while(messageList)
         message_del(messageList);
@@ -658,10 +840,12 @@ global_db_cleanup(void)
 void
 init_global(const char *nick)
 {
+    struct chanNode *chan;
+    unsigned int i;
     G_LOG = log_register_type("Global", "file:global.log");
-    reg_new_user_func(global_process_user);
-    reg_auth_func(global_process_auth);
-    reg_oper_func(global_process_oper);
+    reg_new_user_func(global_process_user, NULL);
+    reg_auth_func(global_process_auth, NULL);
+    reg_oper_func(global_process_oper, NULL);
 
     conf_register_reload(global_conf_read);
 
@@ -675,10 +859,18 @@ init_global(const char *nick)
     if(nick)
     {
         const char *modes = conf_get_data("services/global/modes", RECDB_QSTRING);
-        global = AddService(nick, modes ? modes : NULL, "Global Services", NULL);
+        global = AddLocalUser(nick, nick, NULL, "Global Services", modes);
         global_service = service_register(global);
     }
+
+    if(autojoin_channels && global) {
+        for (i = 0; i < autojoin_channels->used; i++) {
+            chan = AddChannel(autojoin_channels->list[i], now, "+nt", NULL, NULL);
+            AddChannelUser(global, chan)->modes |= MODE_CHANOP;
+        }    
+    }
+
     saxdb_register("Global", global_saxdb_read, global_saxdb_write);
-    reg_exit_func(global_db_cleanup);
+    reg_exit_func(global_db_cleanup, NULL);
     message_register_table(msgtab);
 }