1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
19 #include "inspircd_config.h"
21 #include "inspircd_io.h"
22 #include "inspircd_util.h"
24 #include <sys/errno.h>
28 #include <ext/hash_map>
36 #include "connection.h"
47 #include "inspstring.h"
48 #include "helperfuncs.h"
51 extern std::vector<Module*> modules;
52 extern std::vector<ircd_module*> factory;
53 extern std::vector<std::string> module_names;
57 extern char ServerName[MAXBUF];
58 extern char Network[MAXBUF];
59 extern char ServerDesc[MAXBUF];
60 extern char AdminName[MAXBUF];
61 extern char AdminEmail[MAXBUF];
62 extern char AdminNick[MAXBUF];
63 extern char diepass[MAXBUF];
64 extern char restartpass[MAXBUF];
65 extern char motd[MAXBUF];
66 extern char rules[MAXBUF];
67 extern char list[MAXBUF];
68 extern char PrefixQuit[MAXBUF];
69 extern char DieValue[MAXBUF];
71 extern bool AllowHalfop;
72 extern bool AllowProtect;
73 extern bool AllowFounder;
77 char* give_ops(userrec *user,char *dest,chanrec *chan,int status)
81 if ((!user) || (!dest) || (!chan))
83 log(DEFAULT,"*** BUG *** give_ops was given an invalid parameter");
89 log(DEFAULT,"the target nickname given to give_ops was invalid");
90 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
96 log(DEFAULT,"the target nickname given to give_ops couldnt be found");
97 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
104 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_OP));
106 if (MOD_RESULT == ACR_DENY)
108 if (MOD_RESULT == ACR_DEFAULT)
110 if ((status < STATUS_OP) && (!is_uline(user->server)))
112 log(DEBUG,"%s cant give ops to %s because they nave status %d and needs %d",user->nick,dest,status,STATUS_OP);
113 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
119 for (int i = 0; i != MAXCHANS; i++)
121 if ((d->chans[i].channel != NULL) && (chan != NULL))
122 if (!strcasecmp(d->chans[i].channel->name,chan->name))
124 if (d->chans[i].uc_modes & UCMODE_OP)
126 /* mode already set on user, dont allow multiple */
127 log(DEFAULT,"The target user given to give_ops was already opped on the channel");
130 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
131 log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
135 log(DEFAULT,"The target channel given to give_ops was not in the users mode list");
140 char* give_hops(userrec *user,char *dest,chanrec *chan,int status)
144 if ((!user) || (!dest) || (!chan))
146 log(DEFAULT,"*** BUG *** give_hops was given an invalid parameter");
153 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
158 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
164 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_HALFOP));
166 if (MOD_RESULT == ACR_DENY)
168 if (MOD_RESULT == ACR_DEFAULT)
170 if ((status < STATUS_OP) && (!is_uline(user->server)))
172 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
177 for (int i = 0; i != MAXCHANS; i++)
179 if ((d->chans[i].channel != NULL) && (chan != NULL))
180 if (!strcasecmp(d->chans[i].channel->name,chan->name))
182 if (d->chans[i].uc_modes & UCMODE_HOP)
184 /* mode already set on user, dont allow multiple */
187 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
188 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
196 char* give_voice(userrec *user,char *dest,chanrec *chan,int status)
200 if ((!user) || (!dest) || (!chan))
202 log(DEFAULT,"*** BUG *** give_voice was given an invalid parameter");
209 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
214 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
220 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_VOICE));
222 if (MOD_RESULT == ACR_DENY)
224 if (MOD_RESULT == ACR_DEFAULT)
226 if ((status < STATUS_HOP) && (!is_uline(user->server)))
228 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
233 for (int i = 0; i != MAXCHANS; i++)
235 if ((d->chans[i].channel != NULL) && (chan != NULL))
236 if (!strcasecmp(d->chans[i].channel->name,chan->name))
238 if (d->chans[i].uc_modes & UCMODE_VOICE)
240 /* mode already set on user, dont allow multiple */
243 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
244 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
252 char* take_ops(userrec *user,char *dest,chanrec *chan,int status)
256 if ((!user) || (!dest) || (!chan))
258 log(DEFAULT,"*** BUG *** take_ops was given an invalid parameter");
265 log(DEBUG,"take_ops was given an invalid target nickname of %s",dest);
266 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
271 log(DEBUG,"take_ops couldnt resolve the target nickname: %s",dest);
272 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
278 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEOP));
280 if (MOD_RESULT == ACR_DENY)
282 if (MOD_RESULT == ACR_DEFAULT)
284 if ((status < STATUS_OP) && (!is_uline(user->server)))
286 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
291 for (int i = 0; i != MAXCHANS; i++)
293 if ((d->chans[i].channel != NULL) && (chan != NULL))
294 if (!strcasecmp(d->chans[i].channel->name,chan->name))
296 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
298 /* mode already set on user, dont allow multiple */
301 d->chans[i].uc_modes ^= UCMODE_OP;
302 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
306 log(DEBUG,"take_ops couldnt locate the target channel in the target users list");
311 char* take_hops(userrec *user,char *dest,chanrec *chan,int status)
315 if ((!user) || (!dest) || (!chan))
317 log(DEFAULT,"*** BUG *** take_hops was given an invalid parameter");
324 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
329 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
335 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEHALFOP));
337 if (MOD_RESULT == ACR_DENY)
339 if (MOD_RESULT == ACR_DEFAULT)
341 if ((status < STATUS_OP) && (!is_uline(user->server)))
343 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
348 for (int i = 0; i != MAXCHANS; i++)
350 if ((d->chans[i].channel != NULL) && (chan != NULL))
351 if (!strcasecmp(d->chans[i].channel->name,chan->name))
353 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
355 /* mode already set on user, dont allow multiple */
358 d->chans[i].uc_modes ^= UCMODE_HOP;
359 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
367 char* take_voice(userrec *user,char *dest,chanrec *chan,int status)
371 if ((!user) || (!dest) || (!chan))
373 log(DEFAULT,"*** BUG *** take_voice was given an invalid parameter");
380 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
385 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
391 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEVOICE));
393 if (MOD_RESULT == ACR_DENY)
395 if (MOD_RESULT == ACR_DEFAULT)
397 if ((status < STATUS_HOP) && (!is_uline(user->server)))
399 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
404 for (int i = 0; i != MAXCHANS; i++)
406 if ((d->chans[i].channel != NULL) && (chan != NULL))
407 if (!strcasecmp(d->chans[i].channel->name,chan->name))
409 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
411 /* mode already set on user, dont allow multiple */
414 d->chans[i].uc_modes ^= UCMODE_VOICE;
415 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
423 char* add_ban(userrec *user,char *dest,chanrec *chan,int status)
425 if ((!user) || (!dest) || (!chan)) {
426 log(DEFAULT,"*** BUG *** add_ban was given an invalid parameter");
431 if ((!user) || (!dest) || (!chan))
433 int l = strlen(dest);
434 if (strchr(dest,'!')==0)
436 if (strchr(dest,'@')==0)
438 for (int i = 0; i < l; i++)
441 for (int i = 0; i < l; i++)
445 for (int i = 0; i < l; i++)
451 for (int i = 0; i < l; i++)
457 long maxbans = GetMaxBans(chan->name);
458 if (chan->bans.size() > maxbans)
460 WriteServ(user->fd,"478 %s %s :Channel ban list for %s is full (maximum entries for this channel is %d)",user->nick, chan->name,chan->name,maxbans);
464 log(DEBUG,"add_ban: %s %s",chan->name,user->nick);
467 FOREACH_RESULT(OnAddBan(user,chan,dest));
472 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
474 if (!strcasecmp(i->data,dest))
476 // dont allow a user to set the same ban twice
482 strncpy(b.data,dest,MAXBUF);
483 strncpy(b.set_by,user->nick,NICKMAX);
484 chan->bans.push_back(b);
488 char* take_ban(userrec *user,char *dest,chanrec *chan,int status)
490 if ((!user) || (!dest) || (!chan)) {
491 log(DEFAULT,"*** BUG *** take_ban was given an invalid parameter");
495 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
496 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
498 if (!strcasecmp(i->data,dest))
501 FOREACH_RESULT(OnDelBan(user,chan,dest));
511 // tidies up redundant modes, e.g. +nt-nt+i becomes +-+i,
512 // a section further down the chain tidies up the +-+- crap.
513 std::string compress_modes(std::string modes,bool channelmodes)
518 memset(counts,0,sizeof(counts));
519 memset(active,0,sizeof(active));
520 log(DEBUG,"compress_modes: %s",modes.c_str());
521 for (int i = 0; i < modes.length(); i++)
523 if ((modes[i] == '+') || (modes[i] == '-'))
527 if ((strchr("itnmsp",modes[i])) || ((ModeDefined(modes[i],MT_CHANNEL)) && (ModeDefinedOn(modes[i],MT_CHANNEL)==0) && (ModeDefinedOff(modes[i],MT_CHANNEL)==0)))
529 log(DEBUG,"Tidy mode %c",modes[i]);
530 counts[(unsigned int)modes[i]]++;
531 active[(unsigned int)modes[i]] = true;
536 log(DEBUG,"Tidy mode %c",modes[i]);
537 counts[(unsigned int)modes[i]]++;
538 active[(unsigned int)modes[i]] = true;
541 for (int j = 65; j < 127; j++)
543 if ((counts[j] > 1) && (active[j] == true))
546 v[0] = (unsigned char)j;
548 std::string mode_str = v;
549 std::string::size_type pos = modes.find(mode_str);
550 if (pos != std::string::npos)
552 log(DEBUG,"all occurances of mode %c to be deleted...",(unsigned char)j);
553 while (modes.find(mode_str) != std::string::npos)
554 modes.erase(modes.find(mode_str),1);
555 log(DEBUG,"New mode line: %s",modes.c_str());
562 void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
565 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
569 char modelist[MAXBUF];
570 char outlist[MAXBUF];
572 char outpars[32][MAXBUF];
578 bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
586 FOREACH_RESULT(OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
588 if (MOD_RESULT == ACR_DENY)
591 log(DEBUG,"process_modes: start: parameters=%d",pcnt);
593 strlcpy(modelist,parameters[1],MAXBUF); /* mode list, e.g. +oo-o *
594 * parameters[2] onwards are parameters for
595 * modes that require them :) */
596 strlcpy(outlist,"+",MAXBUF);
599 log(DEBUG,"process_modes: modelist: %s",modelist);
601 std::string tidied = compress_modes(modelist,true);
602 strlcpy(modelist,tidied.c_str(),MAXBUF);
604 int len = strlen(modelist);
605 while (modelist[len-1] == ' ')
606 modelist[--len] = '\0';
607 for (ptr = 0; ptr < len; ptr++)
612 log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
614 char modechar = modelist[ptr];
615 switch (modelist[ptr])
620 int t = strlen(outlist)-1;
621 if ((outlist[t] == '+') || (outlist[t] == '-'))
637 int t = strlen(outlist)-1;
638 if ((outlist[t] == '+') || (outlist[t] == '-'))
652 if ((param >= pcnt)) break;
653 log(DEBUG,"Enough parameters left");
657 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], true, 1));
660 log(DEBUG,"calling give_ops");
661 r = give_ops(user,parameters[param++],chan,status);
668 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], false, 1));
671 log(DEBUG,"calling take_ops");
672 r = take_ops(user,parameters[param++],chan,status);
678 strlcat(outlist,"o",MAXBUF);
679 strlcpy(outpars[pc++],r,MAXBUF);
684 if (((param >= pcnt)) || (!AllowHalfop)) break;
688 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], true, 1));
691 r = give_hops(user,parameters[param++],chan,status);
698 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], false, 1));
701 r = take_hops(user,parameters[param++],chan,status);
707 strlcat(outlist,"h",MAXBUF);
708 strlcpy(outpars[pc++],r,MAXBUF);
714 if ((param >= pcnt)) break;
718 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], true, 1));
721 r = give_voice(user,parameters[param++],chan,status);
728 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], false, 1));
731 r = take_voice(user,parameters[param++],chan,status);
737 strlcat(outlist,"v",MAXBUF);
738 strlcpy(outpars[pc++],r,MAXBUF);
743 if ((param >= pcnt)) break;
747 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], true, 1));
750 r = add_ban(user,parameters[param++],chan,status);
757 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], false, 1));
760 r = take_ban(user,parameters[param++],chan,status);
766 strlcat(outlist,"b",MAXBUF);
767 strlcpy(outpars[pc++],parameters[param-1],MAXBUF);
781 if (previously_unset_k)
783 previously_set_k = true;
785 if (!strcmp(chan->key,""))
788 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], true, 1));
793 strlcpy(key,parameters[param++],32);
794 strlcpy(outpars[pc++],key,MAXBUF);
795 strlcpy(chan->key,key,MAXBUF);
803 /* checks on -k are case sensitive and only accurate to the
804 first 32 characters */
805 if (previously_set_k)
807 previously_unset_k = true;
811 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], false, 1));
814 strlcpy(key,parameters[param++],32);
815 /* only allow -k if correct key given */
816 if (!strcmp(chan->key,key))
818 strlcat(outlist,"k",MAXBUF);
819 strlcpy(chan->key,"",MAXBUF);
820 strlcpy(outpars[pc++],key,MAXBUF);
830 if (previously_set_l)
832 previously_unset_l = true;
834 FOREACH_RESULT(OnRawMode(user, chan, 'l', "", false, 0));
845 if ((param >= pcnt)) break;
850 if (previously_unset_l)
852 previously_set_l = true;
853 bool invalid = false;
854 for (int i = 0; i < strlen(parameters[param]); i++)
856 if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
861 if (atoi(parameters[param]) < 1)
870 FOREACH_RESULT(OnRawMode(user, chan, 'l', parameters[param], true, 1));
874 chan->limit = atoi(parameters[param]);
876 // reported by mech: large values cause underflow
878 chan->limit = 0x7FFF;
883 strlcat(outlist,"l",MAXBUF);
884 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
892 FOREACH_RESULT(OnRawMode(user, chan, 'i', "", mdir, 0));
897 if (!(chan->binarymodes & CM_INVITEONLY)) strlcat(outlist,"i",MAXBUF);
898 chan->binarymodes |= CM_INVITEONLY;
902 if (chan->binarymodes & CM_INVITEONLY) strlcat(outlist,"i",MAXBUF);
903 chan->binarymodes &= ~CM_INVITEONLY;
910 FOREACH_RESULT(OnRawMode(user, chan, 't', "", mdir, 0));
915 if (!(chan->binarymodes & CM_TOPICLOCK)) strlcat(outlist,"t",MAXBUF);
916 chan->binarymodes |= CM_TOPICLOCK;
920 if (chan->binarymodes & CM_NOEXTERNAL) strlcat(outlist,"t",MAXBUF);
921 chan->binarymodes &= ~CM_TOPICLOCK;
928 FOREACH_RESULT(OnRawMode(user, chan, 'n', "", mdir, 0));
933 if (!(chan->binarymodes & CM_NOEXTERNAL)) strlcat(outlist,"n",MAXBUF);
934 chan->binarymodes |= CM_NOEXTERNAL;
938 if (chan->binarymodes & CM_NOEXTERNAL) strlcat(outlist,"n",MAXBUF);
939 chan->binarymodes &= ~CM_NOEXTERNAL;
946 FOREACH_RESULT(OnRawMode(user, chan, 'm', "", mdir, 0));
951 if (!(chan->binarymodes & CM_MODERATED)) strlcat(outlist,"m",MAXBUF);
952 chan->binarymodes |= CM_MODERATED;
956 if (chan->binarymodes & CM_MODERATED) strlcat(outlist,"m",MAXBUF);
957 chan->binarymodes &= ~CM_MODERATED;
964 FOREACH_RESULT(OnRawMode(user, chan, 's', "", mdir, 0));
969 if (!(chan->binarymodes & CM_SECRET)) strlcat(outlist,"s",MAXBUF);
970 chan->binarymodes |= CM_SECRET;
971 if (chan->binarymodes & CM_PRIVATE)
973 chan->binarymodes &= ~CM_PRIVATE;
976 strlcat(outlist,"-p+",MAXBUF);
982 if (chan->binarymodes & CM_SECRET) strlcat(outlist,"s",MAXBUF);
983 chan->binarymodes &= ~CM_SECRET;
990 FOREACH_RESULT(OnRawMode(user, chan, 'p', "", mdir, 0));
995 if (!(chan->binarymodes & CM_PRIVATE)) strlcat(outlist,"p",MAXBUF);
996 chan->binarymodes |= CM_PRIVATE;
997 if (chan->binarymodes & CM_SECRET)
999 chan->binarymodes &= ~CM_SECRET;
1002 strlcat(outlist,"-s+",MAXBUF);
1008 if (chan->binarymodes & CM_PRIVATE) strlcat(outlist,"p",MAXBUF);
1009 chan->binarymodes &= ~CM_PRIVATE;
1015 log(DEBUG,"Preprocessing custom mode %c: modelist: %s",modechar,chan->custom_modes);
1018 if (((!strchr(chan->custom_modes,modechar)) && (!mdir)) || ((strchr(chan->custom_modes,modechar)) && (mdir)))
1020 if (!ModeIsListMode(modechar,MT_CHANNEL))
1022 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",modechar,chan->name);
1026 if (ModeDefined(modechar,MT_CHANNEL))
1028 log(DEBUG,"A module has claimed this mode");
1031 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1033 p.push_back(parameters[param]);
1035 if ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))
1037 p.push_back(parameters[param]);
1040 bool handled = false;
1043 // we're supposed to have a parameter, but none was given... so dont handle the mode.
1044 if (((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir)))
1046 log(DEBUG,"Not enough parameters for module-mode %c",modechar);
1052 // BIG ASS IDIOTIC CODER WARNING!
1053 // Using OnRawMode on another modules mode's behavour
1054 // will confuse the crap out of admins! just because you CAN
1055 // do it, doesnt mean you SHOULD!
1057 std::string para = "";
1060 FOREACH_RESULT(OnRawMode(user, chan, modechar, para, mdir, pcnt));
1063 for (int i = 0; i <= MODCOUNT; i++)
1067 int t = modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p);
1070 log(DEBUG,"OnExtendedMode returned nonzero for a module");
1071 char app[] = {modechar, 0};
1072 if (ModeIsListMode(modechar,MT_CHANNEL))
1083 strlcat(outlist, app,MAXBUF);
1085 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1092 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
1094 strlcat(outlist, app,MAXBUF);
1096 else if (!strchr(outlist,modechar))
1098 strlcat(outlist, app,MAXBUF);
1101 chan->SetCustomMode(modechar,mdir);
1102 // include parameters in output if mode has them
1103 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1105 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
1106 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1109 // break, because only one module can handle the mode.
1118 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,modechar);
1126 /* this ensures only the *valid* modes are sent out onto the network */
1127 int xt = strlen(outlist)-1;
1128 while ((outlist[xt] == '-') || (outlist[xt] == '+'))
1131 xt = strlen(outlist)-1;
1135 strlcpy(outstr,outlist,MAXBUF);
1136 for (ptr = 0; ptr < pc; ptr++)
1138 strlcat(outstr," ",MAXBUF);
1139 strlcat(outstr,outpars[ptr],MAXBUF);
1143 log(DEBUG,"Local mode change");
1144 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outstr);
1152 WriteChannelWithServ(ServerName,chan,"MODE %s %s",chan->name,outstr);
1153 // M token for a usermode must go to all servers
1154 char buffer[MAXBUF];
1155 snprintf(buffer,MAXBUF,"M %s %s",chan->name, outstr);
1156 NetSendToAll(buffer);
1164 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1165 // M token for a usermode must go to all servers
1166 char buffer[MAXBUF];
1167 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick,chan->name, outstr);
1168 NetSendToAll(buffer);
1175 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1177 bool allowed_umode(char umode, char* sourcemodes,bool adding)
1179 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1180 // RFC1459 specified modes
1181 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1183 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1187 // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1188 if ((strchr(sourcemodes,'o')) && (!adding))
1190 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1193 else if (umode == 'o')
1195 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1199 // process any module-defined modes that need oper
1200 if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1202 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1206 if (ModeDefined(umode,MT_CLIENT))
1208 // process any module-defined modes that don't need oper
1209 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1210 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1212 // no, this mode needs oper, and this user 'aint got what it takes!
1218 // anything else - return false.
1219 log(DEBUG,"umode %c not known by any ruleset",umode);
1223 bool process_module_umode(char umode, userrec* source, void* dest, bool adding)
1230 strlcpy(s2->nick,ServerName,NICKMAX);
1231 strlcpy(s2->modes,"o",52);
1238 if (ModeDefined(umode,MT_CLIENT))
1240 for (int i = 0; i <= MODCOUNT; i++)
1242 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1244 log(DEBUG,"Module %s claims umode %c",module_names[i].c_str(),umode);
1248 log(DEBUG,"No module claims umode %c",umode);
1267 void handle_mode(char **parameters, int pcnt, userrec *user)
1273 char outpars[MAXBUF];
1275 dest = Find(parameters[0]);
1282 if ((dest) && (pcnt == 1))
1284 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1288 if ((dest) && (pcnt > 1))
1290 std::string tidied = compress_modes(parameters[1],false);
1291 parameters[1] = (char*)tidied.c_str();
1293 char dmodes[MAXBUF];
1294 strlcpy(dmodes,dest->modes,52);
1295 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1300 if (strchr(user->modes,'o'))
1311 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1315 strcpy(outpars,"+");
1318 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1321 for (int i = 0; i < strlen(parameters[1]); i++)
1323 if (parameters[1][i] == ' ')
1325 if (parameters[1][i] == '+')
1329 int t = strlen(outpars)-1;
1330 if ((outpars[t] == '+') || (outpars[t] == '-'))
1336 strcat(outpars,"+");
1342 if (parameters[1][i] == '-')
1346 int t = strlen(outpars)-1;
1347 if ((outpars[t] == '+') || (outpars[t] == '-'))
1353 strcat(outpars,"-");
1361 if (strchr(user->modes,'o'))
1367 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (allowed_umode(parameters[1][i],user->modes,direction)))
1376 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1378 char umode = parameters[1][i];
1379 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1381 int q = strlen(dmodes);
1382 int r = strlen(outpars);
1384 dmodes[q] = parameters[1][i];
1386 outpars[r] = parameters[1][i];
1387 if (parameters[1][i] == 'o')
1389 FOREACH_MOD OnGlobalOper(dest);
1396 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1398 char umode = parameters[1][i];
1399 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1405 int r = strlen(outpars);
1407 outpars[r] = parameters[1][i];
1410 for (q = 0; q < strlen(dmodes); q++)
1412 if (dmodes[q] != parameters[1][i])
1416 strlcat(temp,moo,MAXBUF);
1419 strlcpy(dmodes,temp,52);
1432 strlcpy(b,"",MAXBUF);
1435 while (i < strlen (outpars))
1437 b[z++] = outpars[i++];
1439 if (i<strlen(outpars)-1)
1441 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1443 // someones playing silly buggers and trying
1444 // to put a +- or -+ into the line...
1448 if (i == strlen(outpars)-1)
1450 if ((outpars[i] == '-') || (outpars[i] == '+'))
1458 if ((b[z] == '-') || (b[z] == '+'))
1461 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1466 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1467 // M token for a usermode must go to all servers
1468 char buffer[MAXBUF];
1469 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1470 NetSendToAll(buffer);
1473 if (strlen(dmodes)>MAXMODES)
1475 dmodes[MAXMODES-1] = '\0';
1477 log(DEBUG,"Stripped mode line");
1478 log(DEBUG,"Line dest is now %s",dmodes);
1479 strlcpy(dest->modes,dmodes,52);
1486 Ptr = FindChan(parameters[0]);
1491 /* just /modes #channel */
1492 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
1493 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1499 char* mode = parameters[1];
1503 FOREACH_RESULT(OnRawMode(user, Ptr, *mode, "", false, 0));
1509 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1511 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1513 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1516 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1518 // list of items for an extmode
1519 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1520 FOREACH_MOD OnSendList(user,Ptr,*mode);
1526 if ((Ptr) && (!has_channel(user,Ptr)))
1528 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1535 FOREACH_RESULT(OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1537 if (MOD_RESULT == ACR_DENY)
1539 if (MOD_RESULT == ACR_DEFAULT)
1541 if (cstatus(user,Ptr) < STATUS_HOP)
1543 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1548 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1553 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1560 void server_mode(char **parameters, int pcnt, userrec *user)
1566 char outpars[MAXBUF];
1568 dest = Find(parameters[0]);
1570 // fix: ChroNiCk found this - we cant use this as debug if its null!
1573 log(DEBUG,"server_mode on %s",dest->nick);
1576 if ((dest) && (pcnt > 1))
1578 std::string tidied = compress_modes(parameters[1],false);
1579 parameters[1] = (char*)tidied.c_str();
1581 char dmodes[MAXBUF];
1582 strlcpy(dmodes,dest->modes,52);
1584 strcpy(outpars,"+");
1587 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1590 for (int i = 0; i < strlen(parameters[1]); i++)
1592 if (parameters[1][i] == ' ')
1594 if (parameters[1][i] == '+')
1598 int t = strlen(outpars)-1;
1599 if ((outpars[t] == '+') || (outpars[t] == '-'))
1605 strcat(outpars,"+");
1611 if (parameters[1][i] == '-')
1615 int t = strlen(outpars)-1;
1616 if ((outpars[t] == '+') || (outpars[t] == '-'))
1622 strcat(outpars,"-");
1629 log(DEBUG,"begin mode processing entry");
1635 log(DEBUG,"umode %c being added",parameters[1][i]);
1636 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1638 char umode = parameters[1][i];
1639 log(DEBUG,"umode %c is an allowed umode",umode);
1640 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1642 int v1 = strlen(dmodes);
1643 int v2 = strlen(outpars);
1645 dmodes[v1] = parameters[1][i];
1647 outpars[v2] = parameters[1][i];
1653 // can only remove a mode they already have
1654 log(DEBUG,"umode %c being removed",parameters[1][i]);
1655 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1657 char umode = parameters[1][i];
1658 log(DEBUG,"umode %c is an allowed umode",umode);
1659 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1665 int v1 = strlen(outpars);
1667 outpars[v1] = parameters[1][i];
1670 for (q = 0; q < strlen(dmodes); q++)
1672 if (dmodes[q] != parameters[1][i])
1676 strlcat(temp,moo,MAXBUF);
1679 strlcpy(dmodes,temp,52);
1689 strlcpy(b,"",MAXBUF);
1692 while (i < strlen (outpars))
1694 b[z++] = outpars[i++];
1696 if (i<strlen(outpars)-1)
1698 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1700 // someones playing silly buggers and trying
1701 // to put a +- or -+ into the line...
1705 if (i == strlen(outpars)-1)
1707 if ((outpars[i] == '-') || (outpars[i] == '+'))
1715 if ((b[z] == '-') || (b[z] == '+'))
1718 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1723 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1724 // M token for a usermode must go to all servers
1725 char buffer[MAXBUF];
1726 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1727 NetSendToAll(buffer);
1730 if (strlen(dmodes)>MAXMODES)
1732 dmodes[MAXMODES-1] = '\0';
1734 log(DEBUG,"Stripped mode line");
1735 log(DEBUG,"Line dest is now %s",dmodes);
1736 strlcpy(dest->modes,dmodes,MAXMODES);
1743 Ptr = FindChan(parameters[0]);
1746 process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1750 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1756 void merge_mode(char **parameters, int pcnt)
1762 char outpars[MAXBUF];
1764 dest = Find(parameters[0]);
1766 // fix: ChroNiCk found this - we cant use this as debug if its null!
1769 log(DEBUG,"merge_mode on %s",dest->nick);
1772 if ((dest) && (pcnt > 1))
1774 std::string tidied = compress_modes(parameters[1],false);
1775 parameters[1] = (char*)tidied.c_str();
1777 char dmodes[MAXBUF];
1778 strlcpy(dmodes,dest->modes,52);
1780 strcpy(outpars,"+");
1783 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1786 for (int i = 0; i < strlen(parameters[1]); i++)
1788 if (parameters[1][i] == ' ')
1790 if (parameters[1][i] == '+')
1794 int t = strlen(outpars)-1;
1795 if ((outpars[t] == '+') || (outpars[t] == '-'))
1801 strcat(outpars,"+");
1807 if (parameters[1][i] == '-')
1811 int t = strlen(outpars)-1;
1812 if ((outpars[t] == '+') || (outpars[t] == '-'))
1818 strcat(outpars,"-");
1825 log(DEBUG,"begin mode processing entry");
1831 log(DEBUG,"umode %c being added",parameters[1][i]);
1832 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],"o",true)))
1834 char umode = parameters[1][i];
1835 log(DEBUG,"umode %c is an allowed umode",umode);
1836 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1838 int v1 = strlen(dmodes);
1839 int v2 = strlen(outpars);
1841 dmodes[v1] = parameters[1][i];
1843 outpars[v2] = parameters[1][i];
1849 // can only remove a mode they already have
1850 log(DEBUG,"umode %c being removed",parameters[1][i]);
1851 if ((allowed_umode(parameters[1][i],"o",false)) && (strchr(dmodes,parameters[1][i])))
1853 char umode = parameters[1][i];
1854 log(DEBUG,"umode %c is an allowed umode",umode);
1855 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1861 int v1 = strlen(outpars);
1863 outpars[v1] = parameters[1][i];
1866 for (q = 0; q < strlen(dmodes); q++)
1868 if (dmodes[q] != parameters[1][i])
1872 strlcat(temp,moo,MAXBUF);
1875 strlcpy(dmodes,temp,52);
1888 while (i < strlen (outpars))
1890 b[z++] = outpars[i++];
1892 if (i<strlen(outpars)-1)
1894 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1896 // someones playing silly buggers and trying
1897 // to put a +- or -+ into the line...
1901 if (i == strlen(outpars)-1)
1903 if ((outpars[i] == '-') || (outpars[i] == '+'))
1911 if ((b[z] == '-') || (b[z] == '+'))
1914 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1917 if (strlen(dmodes)>MAXMODES)
1919 dmodes[MAXMODES-1] = '\0';
1921 log(DEBUG,"Stripped mode line");
1922 log(DEBUG,"Line dest is now %s",dmodes);
1923 strlcpy(dest->modes,dmodes,MAXMODES);
1930 Ptr = FindChan(parameters[0]);
1934 strlcpy(s2.nick,ServerName,NICKMAX);
1935 strcpy(s2.modes,"o");
1937 process_modes(parameters,&s2,Ptr,STATUS_OP,pcnt,true,true,false);
1942 void merge_mode2(char **parameters, int pcnt, userrec* user)
1948 char outpars[MAXBUF];
1950 dest = Find(parameters[0]);
1952 // fix: ChroNiCk found this - we cant use this as debug if its null!
1955 log(DEBUG,"merge_mode2 on %s",dest->nick);
1958 if ((dest) && (pcnt > 1))
1960 std::string tidied = compress_modes(parameters[1],false);
1961 parameters[1] = (char*)tidied.c_str();
1963 char dmodes[MAXBUF];
1964 strlcpy(dmodes,dest->modes,52);
1966 strcpy(outpars,"+");
1969 if ((parameters[1][0] == ':') && (strlen(parameters[1])>1))
1971 // some stupid 3rd party things (such as services packages) put a colon on the mode list...
1972 log(DEBUG,"Some muppet put a colon on the modelist! changed to '%s'",++parameters[1]);
1974 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1977 for (int i = 0; i < strlen(parameters[1]); i++)
1979 if (parameters[1][i] == ' ')
1981 if (parameters[1][i] == '+')
1985 int t = strlen(outpars)-1;
1986 if ((outpars[t] == '+') || (outpars[t] == '-'))
1992 strcat(outpars,"+");
1998 if (parameters[1][i] == '-')
2002 int t = strlen(outpars)-1;
2003 if ((outpars[t] == '+') || (outpars[t] == '-'))
2009 strcat(outpars,"-");
2016 log(DEBUG,"begin mode processing entry");
2022 log(DEBUG,"umode %c being added",parameters[1][i]);
2023 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
2025 char umode = parameters[1][i];
2026 log(DEBUG,"umode %c is an allowed umode",umode);
2027 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2029 int v1 = strlen(dmodes);
2030 int v2 = strlen(outpars);
2032 dmodes[v1] = parameters[1][i];
2034 outpars[v2] = parameters[1][i];
2035 log(DEBUG,"OUTPARS='%s', DMODES='%s'",outpars,dmodes);
2041 // can only remove a mode they already have
2042 log(DEBUG,"umode %c being removed",parameters[1][i]);
2043 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
2045 char umode = parameters[1][i];
2046 log(DEBUG,"umode %c is an allowed umode",umode);
2047 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2053 int v1 = strlen(outpars);
2055 outpars[v1] = parameters[1][i];
2058 for (q = 0; q < strlen(dmodes); q++)
2060 if (dmodes[q] != parameters[1][i])
2064 strlcat(temp,moo,MAXBUF);
2067 strlcpy(dmodes,temp,52);
2074 log(DEBUG,"DONE! OUTPARS='%s', DMODES='%s'",outpars,dmodes);
2081 while (i < strlen (outpars))
2083 b[z++] = outpars[i++];
2085 if (i<strlen(outpars)-1)
2087 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
2089 // someones playing silly buggers and trying
2090 // to put a +- or -+ into the line...
2094 if (i == strlen(outpars)-1)
2096 if ((outpars[i] == '-') || (outpars[i] == '+'))
2104 if ((b[z] == '-') || (b[z] == '+'))
2108 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
2113 WriteTo(user,dest,"MODE %s :%s",dest->nick,b);
2114 log(DEBUG,"Sent: :%s MODE %s",user->nick,b);
2117 if (strlen(dmodes)>MAXMODES)
2119 dmodes[MAXMODES-1] = '\0';
2121 log(DEBUG,"Stripped mode line");
2122 log(DEBUG,"Line dest is now %s",dmodes);
2123 strlcpy(dest->modes,dmodes,MAXMODES);
2129 Ptr = FindChan(parameters[0]);
2132 log(DEBUG,"merge_mode2: found channel %s",Ptr->name);
2135 if ((cstatus(user,Ptr) < STATUS_HOP) && (!is_uline(user->server)))
2139 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,true);