src/modcmd.c: Allow certain commands to run if chanserv does not exist.
[srvx.git] / src / modcmd.c
index 006eb4c233d948bcc46296b1185cdb9843cd8bc3..4fb5a7a516b069be668ab93993b29c6887304e40 100644 (file)
@@ -1,5 +1,5 @@
 /* modcmd.c - Generalized module command support
- * Copyright 2002-2006 srvx Development Team
+ * Copyright 2002-2007 srvx Development Team
  *
  * This file is part of srvx.
  *
@@ -18,7 +18,6 @@
  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
  */
 
-#include "git-version.h"
 #include "chanserv.h"
 #include "conf.h"
 #include "modcmd.h"
@@ -74,8 +73,8 @@ static const struct message_entry msgtab[] = {
     { "MCMSG_HELPFILE_UNBOUND", "Since that was the last command from module %s on the service, the helpfile for %s was removed." },
     { "MCMSG_NO_HELPFILE", "Module %s does not have a help file." },
     { "MCMSG_HELPFILE_ERROR", "Syntax error reading %s; help contents not changed." },
-    { "MCMSG_HELPFILE_READ", "Read %s help database in "FMT_TIME_T".%03lu seconds." },
-    { "MCMSG_COMMAND_TIME", "Command $b%s$b finished in "FMT_TIME_T".%06lu seconds." },
+    { "MCMSG_HELPFILE_READ", "Read %s help database in %lu.%03lu seconds." },
+    { "MCMSG_COMMAND_TIME", "Command $b%s$b finished in %lu.%06lu seconds." },
     { "MCMSG_NEED_OPSERV_LEVEL", "You must have $O access of at least $b%u$b." },
     { "MCMSG_NEED_CHANSERV_LEVEL", "You must have $C access of at least $b%u$b in the channel." },
     { "MCMSG_NEED_ACCOUNT_FLAGS", "You must have account flags $b%s$b." },
@@ -174,8 +173,8 @@ flags_qsort(const void *a, const void *b) {
     return irccasecmp(fa->name, fb->name);
 }
 
-DEFINE_LIST(svccmd_list, struct svccmd*);
-DEFINE_LIST(module_list, struct module*);
+DEFINE_LIST(svccmd_list, struct svccmd*)
+DEFINE_LIST(module_list, struct module*)
 
 static void
 free_service_command(void *data) {
@@ -434,6 +433,7 @@ modcmd_register(struct module *module, const char *name, modcmd_func_t func, uns
  */
 int
 svccmd_can_invoke(struct userNode *user, struct userNode *bot, struct svccmd *cmd, struct chanNode *channel, int options) {
+    extern struct userNode *chanserv;
     unsigned int uData_checked = 0;
     struct userData *uData = NULL;
     int rflags = 0, flags = cmd->effective_flags;
@@ -467,15 +467,17 @@ svccmd_can_invoke(struct userNode *user, struct userNode *bot, struct svccmd *cm
             return 0;
         }
         if (flags & MODCMD_REQUIRE_REGCHAN) {
-            if (!channel->channel_info) {
+            if (!chanserv) {
+                /* Just use the inferred MODCMD_REQUIRE_CHANNEL. */
+            } else if (!channel->channel_info) {
                 if (options & SVCCMD_NOISY)
                     send_message(user, bot, "MCMSG_CHAN_NOT_REGISTERED", channel->name);
                 return 0;
             } else if (IsSuspended(channel->channel_info) && !(flags & MODCMD_IGNORE_CSUSPEND)) {
-                /* allow security-override users to always ignore channel suspensions, but flag it as a staff command */
-                if (!user->handle_info
-                    || !HANDLE_FLAGGED(user->handle_info, HELPING)
-                    || (flags & MODCMD_NEVER_CSUSPEND)) {
+                /* Allow security-override users to ignore most channel
+                 * suspensions, but flag that use as a staff command.
+                 */
+                if (!IsHelping(user) || (flags & MODCMD_NEVER_CSUSPEND)) {
                     if (options & SVCCMD_NOISY)
                         send_message(user, bot, "MCMSG_CHAN_SUSPENDED", channel->name, channel->channel_info->suspended->reason);
                     return 0;
@@ -486,7 +488,9 @@ svccmd_can_invoke(struct userNode *user, struct userNode *bot, struct svccmd *cm
         if (flags & MODCMD_REQUIRE_CHANUSER) {
             if (!uData_checked)
                 uData = _GetChannelUser(channel->channel_info, user->handle_info, 1, 0), uData_checked = 1;
-            if (!uData) {
+            if (!chanserv) {
+                /* Assume someone knows what they're doing. */
+            } else if (!uData) {
                 if (options & SVCCMD_NOISY)
                     send_message(user, bot, "MCMSG_NO_CHANNEL_ACCESS", channel->name);
                 return 0;
@@ -863,9 +867,9 @@ modcmd_privmsg(struct userNode *user, struct userNode *bot, const char *text, in
                 irc_notice_user(bot,user, "\x01PING\x01");
             }
         } else if (!irccasecmp(text, "TIME")) {
-            struct tm tm;
-            localtime_r(&now, &tm);
-            strftime(response, sizeof(response), "\x01TIME %a %b %d %H:%M:%S %Y\x01", &tm);
+            time_t feh;
+            feh = now;
+            strftime(response, sizeof(response), "\x01TIME %a %b %d %H:%M:%S %Y\x01", localtime(&feh));
             irc_notice_user(bot, user, response);
         } else if (!irccasecmp(text, "USERINFO")) {
             snprintf(response, sizeof(response), "\x01USERINFO %s\x01", bot->info);
@@ -877,7 +881,7 @@ modcmd_privmsg(struct userNode *user, struct userNode *bot, const char *text, in
              * users, except to add copyright information pertaining
              * to changes you make to srvx.
              */
-            snprintf(response, sizeof(response), "\x01VERSION %s (%s) %s\x01", PACKAGE_STRING, CODENAME, GIT_VERSION);
+            snprintf(response, sizeof(response), "\x01VERSION %s (%s) %s\x01", PACKAGE_STRING, CODENAME, git_version);
             irc_notice_user(bot, user, response);
         }
         return;
@@ -889,10 +893,12 @@ modcmd_privmsg(struct userNode *user, struct userNode *bot, const char *text, in
 }
 
 void
-modcmd_chanmsg(struct userNode *user, struct chanNode *chan, const char *text, struct userNode *bot) {
+modcmd_chanmsg(struct userNode *user, struct chanNode *chan, const char *text, struct userNode *bot, unsigned int is_notice) {
     struct service *service;
-    if (!(service = dict_find(services, bot->nick, NULL))) return;
+    if (!(service = dict_find(services, bot->nick, NULL)))
+        return;
     svccmd_invoke(user, service, chan, text, 0);
+    (void)is_notice;
 }
 
 struct service *
@@ -1213,10 +1219,10 @@ static MODCMD_FUNC(cmd_readhelp) {
     stop.tv_sec -= start.tv_sec;
     stop.tv_usec -= start.tv_usec;
     if (stop.tv_usec < 0) {
-       stop.tv_sec -= 1;
-       stop.tv_usec += 1000000;
+        stop.tv_sec -= 1;
+        stop.tv_usec += 1000000;
     }
-    reply("MCMSG_HELPFILE_READ", module->name, stop.tv_sec, stop.tv_usec/1000);
+    reply("MCMSG_HELPFILE_READ", module->name, (unsigned long)stop.tv_sec, (unsigned long)stop.tv_usec/1000);
     return 1;
 }
 
@@ -1238,10 +1244,10 @@ static MODCMD_FUNC(cmd_timecmd) {
     stop.tv_sec -= start.tv_sec;
     stop.tv_usec -= start.tv_usec;
     if (stop.tv_usec < 0) {
-       stop.tv_sec -= 1;
-       stop.tv_usec += 1000000;
+        stop.tv_sec -= 1;
+        stop.tv_usec += 1000000;
     }
-    reply("MCMSG_COMMAND_TIME", cmd_text, stop.tv_sec, stop.tv_usec);
+    reply("MCMSG_COMMAND_TIME", cmd_text, (unsigned long)stop.tv_sec, (unsigned long)stop.tv_usec);
     return 1;
 }
 
@@ -1833,17 +1839,14 @@ static MODCMD_FUNC(cmd_dump_messages) {
         reply("MSG_INTERNAL_FAILURE");
         return 0;
     }
-    if ((res = setjmp(ctx->jbuf)) != 0) {
-        ctx->complex.used = 0; /* to avoid false assert()s in close */
-        saxdb_close_context(ctx);
-        fclose(pf);
+    if ((res = setjmp(*saxdb_jmp_buf(ctx))) != 0) {
+        saxdb_close_context(ctx, 1);
         reply("MCMSG_MESSAGE_DUMP_FAILED", strerror(res));
         return 0;
     } else {
         for (it = dict_first(lang_C->messages); it; it = iter_next(it))
             saxdb_write_string(ctx, iter_key(it), iter_data(it));
-        saxdb_close_context(ctx);
-        fclose(pf);
+        saxdb_close_context(ctx, 1);
         reply("MCMSG_MESSAGES_DUMPED", fname);
         return 1;
     }
@@ -1855,9 +1858,9 @@ static MODCMD_FUNC(cmd_version) {
      * command or its accessibility to normal IRC users, except to add
      * copyright information pertaining to changes you make to srvx.
      */
-    send_message_type(4, user, cmd->parent->bot, "$b"PACKAGE_STRING"$b ("CODENAME"), Built: "__DATE__", "__TIME__".  Copyright 2000-2006 srvx Development Team.");
+    send_message_type(4, user, cmd->parent->bot, "$b"PACKAGE_STRING"$b ("CODENAME"), Built: "__DATE__", "__TIME__".  Copyright 2000-2007 srvx Development Team.");
     if (argc > 1)
-        send_message_type(4, user, cmd->parent->bot, "%s", GIT_VERSION);
+        send_message_type(4, user, cmd->parent->bot, "%s", git_version);
     else
         send_message_type(12, user, cmd->parent->bot, "The srvx Development Team includes Paul Chang, Adrian Dewhurst, Miles Peterson, Michael Poole and others.\nThe srvx Development Team can be reached at http://sf.net/projects/srvx/ or in #srvx on irc.gamesurge.net.");
     return 1;