Added idletime to remote whois.
[srvx.git] / src / global.c
index cd6a43c1b081ce4a9a6f0470eeec267cdcc7aee6..c7494142797547d71bd82af1e954c4a36f94ec4f 100644 (file)
@@ -1,11 +1,12 @@
 /* global.c - Global notice service
  * Copyright 2000-2004 srvx Development Team
  *
- * This program is free software; you can redistribute it and/or modify
+ * This file is part of srvx.
+ *
+ * srvx 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
- * (at your option) any later version.  Important limitations are
- * listed in the COPYING file that accompanies this software.
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -13,7 +14,8 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, email srvx-maintainers@srvx.net.
+ * along with srvx; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
  */
 
 #include "conf.h"
@@ -30,7 +32,6 @@
 
 /* Global options */
 #define KEY_DB_BACKUP_FREQ     "db_backup_freq"
-#define KEY_ANNOUNCEMENTS_DEFAULT "announcements_default"
 #define KEY_NICK               "nick"
 
 /* Message data */
@@ -63,6 +64,22 @@ static const struct message_entry msgtab[] = {
 #define GLOBAL_SYNTAX()   svccmd_send_help(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;
@@ -75,7 +92,6 @@ static struct log_type *G_LOG;
 static struct
 {
     unsigned long db_backup_frequency;
-    unsigned int announcements_default : 1;
 } global_conf;
 
 #define global_notice(target, format...) send_message(target , global , ## format)
@@ -86,9 +102,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;
@@ -101,11 +117,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;
 
@@ -150,9 +171,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)
@@ -184,14 +208,14 @@ 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], "announcement") || !irccasecmp(argv[i], "announce")) {
-                flags |= MESSAGE_RECIPIENT_ANNOUNCE;
            } else {
                global_notice(user, "GMSG_INVALID_TARGET", argv[i]);
                return NULL;
            }
        } 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;
@@ -208,7 +232,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 *
@@ -222,10 +246,6 @@ messageType(const struct globalMessage *message)
     {
        return "staff";
     }
-    else if(message->flags & MESSAGE_RECIPIENT_ANNOUNCE)
-    {
-        return "announcement";
-    }
     else if(message->flags & MESSAGE_RECIPIENT_OPERS)
     {
        return "opers";
@@ -255,12 +275,7 @@ 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);
        }
     }
 
@@ -282,7 +297,6 @@ message_send(struct globalMessage *message)
 {
     struct userNode *user;
     unsigned long n;
-    dict_iterator_t it;
 
     if(message->flags & MESSAGE_RECIPIENT_CHANNELS)
     {
@@ -295,20 +309,6 @@ message_send(struct globalMessage *message)
        return;
     }
 
-    if(message->flags & MESSAGE_RECIPIENT_ANNOUNCE)
-    {
-        char announce;
-
-        for (it = dict_first(clients); it; it = iter_next(it)) {
-            user = iter_data(it);
-            if (user->uplink == self) continue;
-            announce = user->handle_info ? user->handle_info->announcements : '?';
-            if (announce == 'n') continue;
-            if ((announce == '?') && !global_conf.announcements_default) continue;
-            notice_target(user->nick, message);
-        }
-    }
-
     if(message->flags & MESSAGE_RECIPIENT_OPERS)
     {
        for(n = 0; n < curr_opers.used; n++)
@@ -354,9 +354,11 @@ 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")) {
@@ -367,25 +369,29 @@ static GLOBAL_FUNC(cmd_notice)
        target = MESSAGE_RECIPIENT_OPERS;
     } else if(!irccasecmp(argv[1], "staff") || !irccasecmp(argv[1], "privileged")) {
        target |= MESSAGE_RECIPIENT_HELPERS | MESSAGE_RECIPIENT_OPERS;
-    } else if(!irccasecmp(argv[1], "announcement") || !irccasecmp(argv[1], "announce")) {
-        target |= MESSAGE_RECIPIENT_ANNOUNCE;
     } else if(!irccasecmp(argv[1], "channels")) {
        target = MESSAGE_RECIPIENT_CHANNELS;
     } 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);
 
@@ -440,13 +446,9 @@ static GLOBAL_FUNC(cmd_list)
         table.contents[nn][0] = strdup(buffer);
         table.contents[nn][1] = messageType(message);
         if(message->duration)
-        {
-            intervalString(buffer, message->posted + message->duration - now);
-        }
+            intervalString(buffer, message->posted + message->duration - now, user->handle_info);
         else
-        {
             strcpy(buffer, "Never.");
-        }
         table.contents[nn][2] = strdup(buffer);
         table.contents[nn][3] = message->from;
        length = strlen(message->message);
@@ -587,8 +589,6 @@ global_conf_read(void)
 
     str = database_get_data(conf_node, KEY_DB_BACKUP_FREQ, RECDB_QSTRING);
     global_conf.db_backup_frequency = str ? ParseInterval(str) : 7200;
-    str = database_get_data(conf_node, KEY_ANNOUNCEMENTS_DEFAULT, RECDB_QSTRING);
-    global_conf.announcements_default = str ? enabled_string(str) : 1;
 
     str = database_get_data(conf_node, KEY_NICK, RECDB_QSTRING);
     if(global && str)
@@ -676,8 +676,9 @@ init_global(const char *nick)
 
     if(nick)
     {
-        global = AddService(nick, "Global Services");
-        global_service = service_register(global, 0);
+        const char *modes = conf_get_data("services/global/modes", RECDB_QSTRING);
+        global = AddLocalUser(nick, nick, NULL, "Global Services", modes);
+        global_service = service_register(global);
     }
     saxdb_register("Global", global_saxdb_read, global_saxdb_write);
     reg_exit_func(global_db_cleanup);