]> jfr.im git - irc/rqf/shadowircd.git/blobdiff - modules/m_resv.c
Put back resv_forcepart.
[irc/rqf/shadowircd.git] / modules / m_resv.c
index 84a0183dfb1db57c44678de21b6942a5a6dcaaa8..5101023452cda99664a84111f1ba00148428a2c5 100644 (file)
@@ -20,7 +20,7 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
  *  USA
  *
- *  $Id: m_resv.c 3045 2006-12-26 23:16:57Z jilles $
+ *  $Id$
  */
 
 #include "stdinc.h"
@@ -36,8 +36,8 @@
 #include "s_conf.h"
 #include "s_newconf.h"
 #include "hash.h"
-#include "s_log.h"
-#include "sprintf_irc.h"
+#include "logger.h"
+#include "bandbi.h"
 
 static int mo_resv(struct Client *, struct Client *, int, const char **);
 static int ms_resv(struct Client *, struct Client *, int, const char **);
@@ -50,24 +50,26 @@ struct Message resv_msgtab = {
        "RESV", 0, 0, 0, MFLG_SLOW | MFLG_UNREG,
        {mg_ignore, mg_not_oper, {ms_resv, 4}, {ms_resv, 4}, {me_resv, 5}, {mo_resv, 3}}
 };
+
 struct Message unresv_msgtab = {
        "UNRESV", 0, 0, 0, MFLG_SLOW | MFLG_UNREG,
        {mg_ignore, mg_not_oper, {ms_unresv, 3}, {ms_unresv, 3}, {me_unresv, 2}, {mo_unresv, 2}}
 };
 
-mapi_clist_av1 resv_clist[] = {        &resv_msgtab, &unresv_msgtab, NULL };
-DECLARE_MODULE_AV1(resv, NULL, NULL, resv_clist, NULL, NULL, "$Revision: 3045 $");
+mapi_clist_av1 resv_clist[] = { &resv_msgtab, &unresv_msgtab, NULL };
+
+DECLARE_MODULE_AV1(resv, NULL, NULL, resv_clist, NULL, NULL, "$Revision$");
 
 static void parse_resv(struct Client *source_p, const char *name,
-                       const char *reason, int temp_time);
+                      const char *reason, int temp_time);
 static void propagate_resv(struct Client *source_p, const char *target,
-                       int temp_time, const char *name, const char *reason);
-static void cluster_resv(struct Client *source_p, int temp_time, 
-                       const char *name, const char *reason);
+                          int temp_time, const char *name, const char *reason);
+static void cluster_resv(struct Client *source_p, int temp_time,
+                        const char *name, const char *reason);
 
 static void handle_remote_unresv(struct Client *source_p, const char *name);
 static void remove_resv(struct Client *source_p, const char *name);
-static int remove_resv_from_file(struct Client *source_p, const char *name);
+static void resv_chan_forcepart(const char *name, const char *reason, int temp_time);
 
 /*
  * mo_resv()
@@ -86,8 +88,7 @@ mo_resv(struct Client *client_p, struct Client *source_p, int parc, const char *
 
        if(!IsOperResv(source_p))
        {
-               sendto_one(source_p, form_str(ERR_NOPRIVS),
-                          me.name, source_p->name, "resv");
+               sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "resv");
                return 0;
        }
 
@@ -102,23 +103,22 @@ mo_resv(struct Client *client_p, struct Client *source_p, int parc, const char *
        name = parv[loc];
        loc++;
 
-       if((parc >= loc+2) && (irccmp(parv[loc], "ON") == 0))
+       if((parc >= loc + 2) && (irccmp(parv[loc], "ON") == 0))
        {
                if(!IsOperRemoteBan(source_p))
                {
                        sendto_one(source_p, form_str(ERR_NOPRIVS),
-                               me.name, source_p->name, "remoteban");
+                                  me.name, source_p->name, "remoteban");
                        return 0;
                }
 
-               target_server = parv[loc+1];
+               target_server = parv[loc + 1];
                loc += 2;
        }
 
        if(parc <= loc || EmptyString(parv[loc]))
        {
-               sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
-                          me.name, source_p->name, "RESV");
+               sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS), me.name, source_p->name, "RESV");
                return 0;
        }
 
@@ -147,8 +147,7 @@ mo_resv(struct Client *client_p, struct Client *source_p, int parc, const char *
  *     parv[3] = reason
  */
 static int
-ms_resv(struct Client *client_p, struct Client *source_p,
-       int parc, const char *parv[])
+ms_resv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
 {
        /* parv[0]  parv[1]        parv[2]  parv[3]
         * oper     target server  resv     reason
@@ -166,8 +165,7 @@ ms_resv(struct Client *client_p, struct Client *source_p,
 }
 
 static int
-me_resv(struct Client *client_p, struct Client *source_p,
-       int parc, const char *parv[])
+me_resv(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
 {
        /* time name 0 :reason */
        if(!IsPerson(source_p))
@@ -186,15 +184,14 @@ me_resv(struct Client *client_p, struct Client *source_p,
  * side effects - will parse the resv and create it if valid
  */
 static void
-parse_resv(struct Client *source_p, const char *name, 
-          const char *reason, int temp_time)
+parse_resv(struct Client *source_p, const char *name, const char *reason, int temp_time)
 {
        struct ConfItem *aconf;
 
-       if(!MyClient(source_p) && 
+       if(!MyClient(source_p) &&
           !find_shared_conf(source_p->username, source_p->host,
-                       source_p->servptr->name, 
-                       (temp_time > 0) ? SHARED_TRESV : SHARED_PRESV))
+                            source_p->servptr->name,
+                            (temp_time > 0) ? SHARED_TRESV : SHARED_PRESV))
                return;
 
        if(IsChannelName(name))
@@ -202,139 +199,141 @@ parse_resv(struct Client *source_p, const char *name,
                if(hash_find_resv(name))
                {
                        sendto_one_notice(source_p,
-                                       ":A RESV has already been placed on channel: %s",
-                                       name);
+                                         ":A RESV has already been placed on channel: %s", name);
                        return;
                }
 
                if(strlen(name) > CHANNELLEN)
                {
-                       sendto_one_notice(source_p, ":Invalid RESV length: %s",
-                                         name);
+                       sendto_one_notice(source_p, ":Invalid RESV length: %s", name);
                        return;
                }
 
                if(strchr(reason, '"'))
                {
-                       sendto_one_notice(source_p,
-                                       ":Invalid character '\"' in comment");
+                       sendto_one_notice(source_p, ":Invalid character '\"' in comment");
                        return;
                }
 
                aconf = make_conf();
                aconf->status = CONF_RESV_CHANNEL;
                aconf->port = 0;
-               DupString(aconf->name, name);
-               DupString(aconf->passwd, reason);
-               add_to_resv_hash(aconf->name, aconf);
+               aconf->host = rb_strdup(name);
+               aconf->passwd = rb_strdup(reason);
+               add_to_resv_hash(aconf->host, aconf);
+               resv_chan_forcepart(aconf->host, aconf->passwd, temp_time);
 
                if(temp_time > 0)
                {
-                       aconf->hold = CurrentTime + temp_time;
+                       aconf->hold = rb_current_time() + temp_time;
 
                        sendto_realops_snomask(SNO_GENERAL, L_ALL,
-                                    "%s added temporary %d min. RESV for [%s] [%s]",
-                                    get_oper_name(source_p), temp_time / 60,
-                                    name, reason);
+                                              "%s added temporary %d min. RESV for [%s] [%s]",
+                                              get_oper_name(source_p), temp_time / 60,
+                                              name, reason);
                        ilog(L_KLINE, "R %s %d %s %s",
-                               get_oper_name(source_p), temp_time / 60,
-                               name, reason);
+                            get_oper_name(source_p), temp_time / 60, name, reason);
                        sendto_one_notice(source_p, ":Added temporary %d min. RESV [%s]",
-                                       temp_time / 60, name);
+                                         temp_time / 60, name);
                }
                else
-                       write_confitem(RESV_TYPE, source_p, NULL, aconf->name, 
-                                       aconf->passwd, NULL, NULL, 0);
+               {
+                       sendto_realops_snomask(SNO_GENERAL, L_ALL,
+                                              "%s added RESV for [%s] [%s]",
+                                              get_oper_name(source_p), name, reason);
+                       ilog(L_KLINE, "R %s 0 %s %s",
+                            get_oper_name(source_p), name, reason);
+                       sendto_one_notice(source_p, ":Added RESV [%s]", name);
+
+                       bandb_add(BANDB_RESV, source_p, aconf->host, NULL, aconf->passwd, NULL, 0);
+               }
        }
        else if(clean_resv_nick(name))
        {
-               if(strlen(name) > NICKLEN*2)
+               if(strlen(name) > NICKLEN * 2)
                {
-                       sendto_one_notice(source_p, ":Invalid RESV length: %s",
-                                          name);
+                       sendto_one_notice(source_p, ":Invalid RESV length: %s", name);
                        return;
                }
 
                if(strchr(reason, '"'))
                {
-                       sendto_one_notice(source_p,
-                                       ":Invalid character '\"' in comment");
+                       sendto_one_notice(source_p, ":Invalid character '\"' in comment");
                        return;
                }
 
                if(!valid_wild_card_simple(name))
                {
                        sendto_one_notice(source_p,
-                                          ":Please include at least %d non-wildcard "
-                                          "characters with the resv",
-                                          ConfigFileEntry.min_nonwildcard_simple);
+                                         ":Please include at least %d non-wildcard "
+                                         "characters with the resv",
+                                         ConfigFileEntry.min_nonwildcard_simple);
                        return;
                }
 
                if(find_nick_resv_mask(name))
                {
                        sendto_one_notice(source_p,
-                                          ":A RESV has already been placed on nick: %s",
-                                          name);
+                                         ":A RESV has already been placed on nick: %s", name);
                        return;
                }
 
                aconf = make_conf();
                aconf->status = CONF_RESV_NICK;
                aconf->port = 0;
-               DupString(aconf->name, name);
-               DupString(aconf->passwd, reason);
-               dlinkAddAlloc(aconf, &resv_conf_list);
+               aconf->host = rb_strdup(name);
+               aconf->passwd = rb_strdup(reason);
+               rb_dlinkAddAlloc(aconf, &resv_conf_list);
 
                if(temp_time > 0)
                {
-                       aconf->hold = CurrentTime + temp_time;
+                       aconf->hold = rb_current_time() + temp_time;
 
                        sendto_realops_snomask(SNO_GENERAL, L_ALL,
-                                    "%s added temporary %d min. RESV for [%s] [%s]",
-                                    get_oper_name(source_p), temp_time / 60,
-                                    name, reason);
+                                              "%s added temporary %d min. RESV for [%s] [%s]",
+                                              get_oper_name(source_p), temp_time / 60,
+                                              name, reason);
                        ilog(L_KLINE, "R %s %d %s %s",
-                               get_oper_name(source_p), temp_time / 60,
-                               name, reason);
+                            get_oper_name(source_p), temp_time / 60, name, reason);
                        sendto_one_notice(source_p, ":Added temporary %d min. RESV [%s]",
-                                       temp_time / 60, name);
+                                         temp_time / 60, name);
                }
                else
-                       write_confitem(RESV_TYPE, source_p, NULL, aconf->name, 
-                                       aconf->passwd, NULL, NULL, 0);
+               {
+                       sendto_realops_snomask(SNO_GENERAL, L_ALL,
+                                              "%s added RESV for [%s] [%s]",
+                                              get_oper_name(source_p), name, reason);
+                       ilog(L_KLINE, "R %s 0 %s %s",
+                            get_oper_name(source_p), name, reason);
+                       sendto_one_notice(source_p, ":Added RESV [%s]", name);
+
+                       bandb_add(BANDB_RESV, source_p, aconf->host, NULL, aconf->passwd, NULL, 0);
+               }
        }
        else
-               sendto_one_notice(source_p,
-                                 ":You have specified an invalid resv: [%s]",
-                                 name);
+               sendto_one_notice(source_p, ":You have specified an invalid resv: [%s]", name);
 }
 
-static void 
+static void
 propagate_resv(struct Client *source_p, const char *target,
-               int temp_time, const char *name, const char *reason)
+              int temp_time, const char *name, const char *reason)
 {
        if(!temp_time)
        {
                sendto_match_servs(source_p, target,
-                               CAP_CLUSTER, NOCAPS,
-                               "RESV %s %s :%s",
-                               target, name, reason);
+                                  CAP_CLUSTER, NOCAPS, "RESV %s %s :%s", target, name, reason);
                sendto_match_servs(source_p, target,
-                               CAP_ENCAP, CAP_CLUSTER,
-                               "ENCAP %s RESV %d %s 0 :%s",
-                               target, temp_time, name, reason);
+                                  CAP_ENCAP, CAP_CLUSTER,
+                                  "ENCAP %s RESV %d %s 0 :%s", target, temp_time, name, reason);
        }
        else
                sendto_match_servs(source_p, target,
-                               CAP_ENCAP, NOCAPS,
-                               "ENCAP %s RESV %d %s 0 :%s",
-                               target, temp_time, name, reason);
+                                  CAP_ENCAP, NOCAPS,
+                                  "ENCAP %s RESV %d %s 0 :%s", target, temp_time, name, reason);
 }
 
 static void
-cluster_resv(struct Client *source_p, int temp_time, const char *name,
-               const char *reason)
+cluster_resv(struct Client *source_p, int temp_time, const char *name, const char *reason)
 {
        struct remote_conf *shared_p;
        rb_dlink_node *ptr;
@@ -352,19 +351,18 @@ cluster_resv(struct Client *source_p, int temp_time, const char *name,
                                continue;
 
                        sendto_match_servs(source_p, shared_p->server,
-                                       CAP_CLUSTER, NOCAPS,
-                                       "RESV %s %s :%s",
-                                       shared_p->server, name, reason);
+                                          CAP_CLUSTER, NOCAPS,
+                                          "RESV %s %s :%s", shared_p->server, name, reason);
                        sendto_match_servs(source_p, shared_p->server,
-                                       CAP_ENCAP, CAP_CLUSTER,
-                                       "ENCAP %s RESV 0 %s 0 :%s",
-                                       shared_p->server, name, reason);
+                                          CAP_ENCAP, CAP_CLUSTER,
+                                          "ENCAP %s RESV 0 %s 0 :%s",
+                                          shared_p->server, name, reason);
                }
                else if(shared_p->flags & SHARED_TRESV)
                        sendto_match_servs(source_p, shared_p->server,
-                                       CAP_ENCAP, NOCAPS,
-                                       "ENCAP %s RESV %d %s 0 :%s",
-                                       shared_p->server, temp_time, name, reason);
+                                          CAP_ENCAP, NOCAPS,
+                                          "ENCAP %s RESV %d %s 0 :%s",
+                                          shared_p->server, temp_time, name, reason);
        }
 }
 
@@ -379,8 +377,7 @@ mo_unresv(struct Client *client_p, struct Client *source_p, int parc, const char
 {
        if(!IsOperResv(source_p))
        {
-               sendto_one(source_p, form_str(ERR_NOPRIVS),
-                          me.name, source_p->name, "resv");
+               sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "resv");
                return 0;
        }
 
@@ -389,19 +386,17 @@ mo_unresv(struct Client *client_p, struct Client *source_p, int parc, const char
                if(!IsOperRemoteBan(source_p))
                {
                        sendto_one(source_p, form_str(ERR_NOPRIVS),
-                               me.name, source_p->name, "remoteban");
+                                  me.name, source_p->name, "remoteban");
                        return 0;
                }
 
-               propagate_generic(source_p, "UNRESV", parv[3], CAP_CLUSTER,
-                               "%s", parv[1]);
+               propagate_generic(source_p, "UNRESV", parv[3], CAP_CLUSTER, "%s", parv[1]);
 
                if(match(parv[3], me.name) == 0)
                        return 0;
        }
        else if(rb_dlink_list_length(&cluster_conf_list) > 0)
-               cluster_generic(source_p, "UNRESV", SHARED_UNRESV, CAP_CLUSTER,
-                               "%s", parv[1]);
+               cluster_generic(source_p, "UNRESV", SHARED_UNRESV, CAP_CLUSTER, "%s", parv[1]);
 
        remove_resv(source_p, parv[1]);
        return 0;
@@ -418,8 +413,7 @@ ms_unresv(struct Client *client_p, struct Client *source_p, int parc, const char
        /* parv[0]  parv[1]        parv[2]
         * oper     target server  resv to remove
         */
-       propagate_generic(source_p, "UNRESV", parv[1], CAP_CLUSTER,
-                       "%s", parv[2]);
+       propagate_generic(source_p, "UNRESV", parv[1], CAP_CLUSTER, "%s", parv[2]);
 
        if(!match(parv[1], me.name))
                return 0;
@@ -446,7 +440,7 @@ static void
 handle_remote_unresv(struct Client *source_p, const char *name)
 {
        if(!find_shared_conf(source_p->username, source_p->host,
-                               source_p->servptr->name, SHARED_UNRESV))
+                            source_p->servptr->name, SHARED_UNRESV))
                return;
 
        remove_resv(source_p, name);
@@ -468,16 +462,13 @@ remove_resv(struct Client *source_p, const char *name)
                }
 
                if(!aconf->hold)
-               {
-                       if (!remove_resv_from_file(source_p, name))
-                               return;
-               }
+                       bandb_del(BANDB_RESV, aconf->host, NULL);
                else
                {
                        sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
                        sendto_realops_snomask(SNO_GENERAL, L_ALL,
-                                       "%s has removed the RESV for: [%s]", 
-                                       get_oper_name(source_p), name);
+                                              "%s has removed the RESV for: [%s]",
+                                              get_oper_name(source_p), name);
                        ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);
                }
                del_from_resv_hash(name, aconf);
@@ -490,7 +481,7 @@ remove_resv(struct Client *source_p, const char *name)
                {
                        aconf = ptr->data;
 
-                       if(irccmp(aconf->name, name))
+                       if(irccmp(aconf->host, name))
                                aconf = NULL;
                        else
                                break;
@@ -503,136 +494,70 @@ remove_resv(struct Client *source_p, const char *name)
                }
 
                if(!aconf->hold)
-               {
-                       if (!remove_resv_from_file(source_p, name))
-                               return;
-               }
+                       bandb_del(BANDB_RESV, aconf->host, NULL);
                else
                {
                        sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
                        sendto_realops_snomask(SNO_GENERAL, L_ALL,
-                                       "%s has removed the RESV for: [%s]", 
-                                       get_oper_name(source_p), name);
+                                              "%s has removed the RESV for: [%s]",
+                                              get_oper_name(source_p), name);
                        ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);
                }
                /* already have ptr from the loop above.. */
-               dlinkDestroy(ptr, &resv_conf_list);
+               rb_dlinkDestroy(ptr, &resv_conf_list);
        }
        free_conf(aconf);
 
        return;
 }
 
-/* remove_resv_from_file()
- *
- * inputs      - client removing the resv
- *             - resv to remove
- * outputs     -
- * side effects - resv if found, is removed from conf
- *              - does not touch resv hash or resv_conf_list
- */
-static int
-remove_resv_from_file(struct Client *source_p, const char *name)
+static void 
+resv_chan_forcepart(const char *name, const char *reason, int temp_time)
 {
-       FILE *in, *out;
-       char buf[BUFSIZE];
-       char buff[BUFSIZE];
-       char temppath[BUFSIZE];
-       const char *filename;
-       mode_t oldumask;
-       char *p;
-       int error_on_write = 0;
-       int found_resv = 0;
-
-       ircsprintf(temppath, "%s.tmp", ConfigFileEntry.resvfile);
-       filename = get_conf_name(RESV_TYPE);
-
-       if((in = fopen(filename, "r")) == NULL)
-       {
-               sendto_one_notice(source_p, ":Cannot open %s", filename);
-               return 0;
-       }
-
-       oldumask = umask(0);
-
-       if((out = fopen(temppath, "w")) == NULL)
-       {
-               sendto_one_notice(source_p, ":Cannot open %s", temppath);
-               fclose(in);
-               umask(oldumask);
-               return 0;
-       }
+       rb_dlink_node *ptr;
+       rb_dlink_node *next_ptr;
+       struct Channel *chptr;
+       struct membership *msptr;
+       struct Client *target_p;
 
-       umask(oldumask);
+       if(!ConfigChannel.resv_forcepart)
+               return;
 
-       while (fgets(buf, sizeof(buf), in))
+       /* for each user on our server in the channel list
+        * send them a PART, and notify opers.
+        */
+       chptr = find_channel(name);
+       if(chptr != NULL)
        {
-               const char *resv_name;
-
-               if(error_on_write)
+               RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->locmembers.head)
                {
-                       if(temppath != NULL)
-                               (void) unlink(temppath);
+                       msptr = ptr->data;
+                       target_p = msptr->client_p;
 
-                       break;
-               }
+                       if(IsExemptResv(target_p))
+                               continue;
 
-               strlcpy(buff, buf, sizeof(buff));
+                       sendto_server(target_p, chptr, CAP_TS6, NOCAPS,
+                                     ":%s PART %s", target_p->id, chptr->chname);
 
-               if((p = strchr(buff, '\n')) != NULL)
-                       *p = '\0';
+                       sendto_channel_local(ALL_MEMBERS, chptr, ":%s!%s@%s PART %s :%s",
+                                            target_p->name, target_p->username,
+                                            target_p->host, chptr->chname, target_p->name);
 
-               if((*buff == '\0') || (*buff == '#'))
-               {
-                       error_on_write = (fputs(buf, out) < 0) ? YES : NO;
-                       continue;
-               }
+                       remove_user_from_channel(msptr);
 
-               if((resv_name = getfield(buff)) == NULL)
-               {
-                       error_on_write = (fputs(buf, out) < 0) ? YES : NO;
-                       continue;
-               }
+                       /* notify opers & user they were removed from the channel */
+                       sendto_realops_snomask(SNO_GENERAL, L_ALL,
+                                            "Forced PART for %s!%s@%s from %s (%s)",
+                                            target_p->name, target_p->username, 
+                                            target_p->host, name, reason);
 
-               if(irccmp(resv_name, name) == 0)
-               {
-                       found_resv++;
-               }
-               else
-               {
-                       error_on_write = (fputs(buf, out) < 0) ? YES : NO;
+                       if(temp_time > 0)
+                               sendto_one_notice(target_p, ":*** Channel %s is temporarily unavailable on this server.",
+                                          name);
+                       else
+                               sendto_one_notice(target_p, ":*** Channel %s is no longer available on this server.",
+                                          name);
                }
        }
-
-       fclose(in);
-       if (fclose(out))
-               error_on_write = YES;
-
-       if(error_on_write)
-       {
-               sendto_one_notice(source_p, ":Couldn't write temp resv file, aborted");
-               return 0;
-       }
-       else if(!found_resv)
-       {
-               sendto_one_notice(source_p, ":Cannot find RESV for %s in file", name);
-
-               if(temppath != NULL)
-                       (void) unlink(temppath);
-
-               return 0;
-       }
-
-       if (rename(temppath, filename))
-       {
-               sendto_one_notice(source_p, ":Couldn't rename temp file, aborted");
-               return 0;
-       }
-
-       sendto_one_notice(source_p, ":RESV for [%s] is removed", name);
-       sendto_realops_snomask(SNO_GENERAL, L_ALL,
-                            "%s has removed the RESV for: [%s]", get_oper_name(source_p), name);
-       ilog(L_KLINE, "UR %s %s", get_oper_name(source_p), name);
-
-       return 1;
 }