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"
46 #include "inspstring.h"
47 #include "helperfuncs.h"
50 extern std::vector<Module*> modules;
51 extern std::vector<ircd_module*> factory;
52 extern std::vector<std::string> module_names;
56 extern char ServerName[MAXBUF];
57 extern char Network[MAXBUF];
58 extern char ServerDesc[MAXBUF];
59 extern char AdminName[MAXBUF];
60 extern char AdminEmail[MAXBUF];
61 extern char AdminNick[MAXBUF];
62 extern char diepass[MAXBUF];
63 extern char restartpass[MAXBUF];
64 extern char motd[MAXBUF];
65 extern char rules[MAXBUF];
66 extern char list[MAXBUF];
67 extern char PrefixQuit[MAXBUF];
68 extern char DieValue[MAXBUF];
70 extern bool AllowHalfop;
71 extern bool AllowProtect;
72 extern bool AllowFounder;
76 char* give_ops(userrec *user,char *dest,chanrec *chan,int status)
80 if ((!user) || (!dest) || (!chan))
82 log(DEFAULT,"*** BUG *** give_ops was given an invalid parameter");
88 log(DEFAULT,"the target nickname given to give_ops was invalid");
89 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
95 log(DEFAULT,"the target nickname given to give_ops couldnt be found");
96 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
103 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_OP));
105 if (MOD_RESULT == ACR_DENY)
107 if (MOD_RESULT == ACR_DEFAULT)
109 if ((status < STATUS_OP) && (!is_uline(user->server)))
111 log(DEBUG,"%s cant give ops to %s because they nave status %d and needs %d",user->nick,dest,status,STATUS_OP);
112 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
118 for (unsigned int i = 0; i != MAXCHANS; i++)
120 if ((d->chans[i].channel != NULL) && (chan != NULL))
121 if (!strcasecmp(d->chans[i].channel->name,chan->name))
123 if (d->chans[i].uc_modes & UCMODE_OP)
125 /* mode already set on user, dont allow multiple */
126 log(DEFAULT,"The target user given to give_ops was already opped on the channel");
129 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
130 log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
134 log(DEFAULT,"The target channel given to give_ops was not in the users mode list");
139 char* give_hops(userrec *user,char *dest,chanrec *chan,int status)
143 if ((!user) || (!dest) || (!chan))
145 log(DEFAULT,"*** BUG *** give_hops was given an invalid parameter");
152 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
157 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
163 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_HALFOP));
165 if (MOD_RESULT == ACR_DENY)
167 if (MOD_RESULT == ACR_DEFAULT)
169 if ((status < STATUS_OP) && (!is_uline(user->server)))
171 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
176 for (unsigned int i = 0; i != MAXCHANS; i++)
178 if ((d->chans[i].channel != NULL) && (chan != NULL))
179 if (!strcasecmp(d->chans[i].channel->name,chan->name))
181 if (d->chans[i].uc_modes & UCMODE_HOP)
183 /* mode already set on user, dont allow multiple */
186 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
187 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
195 char* give_voice(userrec *user,char *dest,chanrec *chan,int status)
199 if ((!user) || (!dest) || (!chan))
201 log(DEFAULT,"*** BUG *** give_voice was given an invalid parameter");
208 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
213 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
219 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_VOICE));
221 if (MOD_RESULT == ACR_DENY)
223 if (MOD_RESULT == ACR_DEFAULT)
225 if ((status < STATUS_HOP) && (!is_uline(user->server)))
227 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
232 for (unsigned int i = 0; i != MAXCHANS; i++)
234 if ((d->chans[i].channel != NULL) && (chan != NULL))
235 if (!strcasecmp(d->chans[i].channel->name,chan->name))
237 if (d->chans[i].uc_modes & UCMODE_VOICE)
239 /* mode already set on user, dont allow multiple */
242 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
243 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
251 char* take_ops(userrec *user,char *dest,chanrec *chan,int status)
255 if ((!user) || (!dest) || (!chan))
257 log(DEFAULT,"*** BUG *** take_ops was given an invalid parameter");
264 log(DEBUG,"take_ops was given an invalid target nickname of %s",dest);
265 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
270 log(DEBUG,"take_ops couldnt resolve the target nickname: %s",dest);
271 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
277 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEOP));
279 if (MOD_RESULT == ACR_DENY)
281 if (MOD_RESULT == ACR_DEFAULT)
283 if ((status < STATUS_OP) && (!is_uline(user->server)))
285 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
290 for (unsigned int i = 0; i != MAXCHANS; i++)
292 if ((d->chans[i].channel != NULL) && (chan != NULL))
293 if (!strcasecmp(d->chans[i].channel->name,chan->name))
295 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
297 /* mode already set on user, dont allow multiple */
300 d->chans[i].uc_modes ^= UCMODE_OP;
301 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
305 log(DEBUG,"take_ops couldnt locate the target channel in the target users list");
310 char* take_hops(userrec *user,char *dest,chanrec *chan,int status)
314 if ((!user) || (!dest) || (!chan))
316 log(DEFAULT,"*** BUG *** take_hops was given an invalid parameter");
323 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
328 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
334 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEHALFOP));
336 if (MOD_RESULT == ACR_DENY)
338 if (MOD_RESULT == ACR_DEFAULT)
340 if ((status < STATUS_OP) && (!is_uline(user->server)))
342 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
347 for (unsigned int i = 0; i != MAXCHANS; i++)
349 if ((d->chans[i].channel != NULL) && (chan != NULL))
350 if (!strcasecmp(d->chans[i].channel->name,chan->name))
352 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
354 /* mode already set on user, dont allow multiple */
357 d->chans[i].uc_modes ^= UCMODE_HOP;
358 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
366 char* take_voice(userrec *user,char *dest,chanrec *chan,int status)
370 if ((!user) || (!dest) || (!chan))
372 log(DEFAULT,"*** BUG *** take_voice was given an invalid parameter");
379 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
384 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
390 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEVOICE));
392 if (MOD_RESULT == ACR_DENY)
394 if (MOD_RESULT == ACR_DEFAULT)
396 if ((status < STATUS_HOP) && (!is_uline(user->server)))
398 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
403 for (unsigned int i = 0; i != MAXCHANS; i++)
405 if ((d->chans[i].channel != NULL) && (chan != NULL))
406 if (!strcasecmp(d->chans[i].channel->name,chan->name))
408 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
410 /* mode already set on user, dont allow multiple */
413 d->chans[i].uc_modes ^= UCMODE_VOICE;
414 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
422 char* add_ban(userrec *user,char *dest,chanrec *chan,int status)
424 if ((!user) || (!dest) || (!chan)) {
425 log(DEFAULT,"*** BUG *** add_ban was given an invalid parameter");
430 if ((!user) || (!dest) || (!chan))
432 unsigned int l = strlen(dest);
433 if (strchr(dest,'!')==0)
435 if (strchr(dest,'@')==0)
437 for (unsigned int i = 0; i < l; i++)
440 for (unsigned int i = 0; i < l; i++)
444 for (unsigned int i = 0; i < l; i++)
450 for (unsigned int i = 0; i < l; i++)
456 long maxbans = GetMaxBans(chan->name);
457 if ((unsigned)chan->bans.size() > (unsigned)maxbans)
459 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);
463 log(DEBUG,"add_ban: %s %s",chan->name,user->nick);
466 FOREACH_RESULT(OnAddBan(user,chan,dest));
471 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
473 if (!strcasecmp(i->data,dest))
475 // dont allow a user to set the same ban twice
481 strncpy(b.data,dest,MAXBUF);
482 strncpy(b.set_by,user->nick,NICKMAX);
483 chan->bans.push_back(b);
487 char* take_ban(userrec *user,char *dest,chanrec *chan,int status)
489 if ((!user) || (!dest) || (!chan)) {
490 log(DEFAULT,"*** BUG *** take_ban was given an invalid parameter");
494 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
495 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
497 if (!strcasecmp(i->data,dest))
500 FOREACH_RESULT(OnDelBan(user,chan,dest));
510 // tidies up redundant modes, e.g. +nt-nt+i becomes +-+i,
511 // a section further down the chain tidies up the +-+- crap.
512 std::string compress_modes(std::string modes,bool channelmodes)
516 memset(counts,0,sizeof(counts));
517 memset(active,0,sizeof(active));
518 log(DEBUG,"compress_modes: %s",modes.c_str());
519 for (unsigned int i = 0; i < modes.length(); i++)
521 if ((modes[i] == '+') || (modes[i] == '-'))
525 if ((strchr("itnmsp",modes[i])) || ((ModeDefined(modes[i],MT_CHANNEL)) && (ModeDefinedOn(modes[i],MT_CHANNEL)==0) && (ModeDefinedOff(modes[i],MT_CHANNEL)==0)))
527 log(DEBUG,"Tidy mode %c",modes[i]);
528 counts[(unsigned int)modes[i]]++;
529 active[(unsigned int)modes[i]] = true;
534 log(DEBUG,"Tidy mode %c",modes[i]);
535 counts[(unsigned int)modes[i]]++;
536 active[(unsigned int)modes[i]] = true;
539 for (int j = 65; j < 127; j++)
541 if ((counts[j] > 1) && (active[j] == true))
544 v[0] = (unsigned char)j;
546 std::string mode_str = v;
547 std::string::size_type pos = modes.find(mode_str);
548 if (pos != std::string::npos)
550 log(DEBUG,"all occurances of mode %c to be deleted...",(unsigned char)j);
551 while (modes.find(mode_str) != std::string::npos)
552 modes.erase(modes.find(mode_str),1);
553 log(DEBUG,"New mode line: %s",modes.c_str());
560 void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
563 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
567 char modelist[MAXBUF];
568 char outlist[MAXBUF];
570 char outpars[32][MAXBUF];
576 bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
584 FOREACH_RESULT(OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
586 if (MOD_RESULT == ACR_DENY)
589 log(DEBUG,"process_modes: start: parameters=%d",pcnt);
591 strlcpy(modelist,parameters[1],MAXBUF); /* mode list, e.g. +oo-o *
592 * parameters[2] onwards are parameters for
593 * modes that require them :) */
594 strlcpy(outlist,"+",MAXBUF);
597 log(DEBUG,"process_modes: modelist: %s",modelist);
599 std::string tidied = compress_modes(modelist,true);
600 strlcpy(modelist,tidied.c_str(),MAXBUF);
602 int len = strlen(modelist);
603 while (modelist[len-1] == ' ')
604 modelist[--len] = '\0';
605 for (ptr = 0; ptr < len; ptr++)
610 log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
612 char modechar = modelist[ptr];
613 switch (modelist[ptr])
618 int t = strlen(outlist)-1;
619 if ((outlist[t] == '+') || (outlist[t] == '-'))
635 int t = strlen(outlist)-1;
636 if ((outlist[t] == '+') || (outlist[t] == '-'))
650 if ((param >= pcnt)) break;
651 log(DEBUG,"Enough parameters left");
655 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], true, 1));
658 log(DEBUG,"calling give_ops");
659 r = give_ops(user,parameters[param++],chan,status);
666 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], false, 1));
669 log(DEBUG,"calling take_ops");
670 r = take_ops(user,parameters[param++],chan,status);
676 strlcat(outlist,"o",MAXBUF);
677 strlcpy(outpars[pc++],r,MAXBUF);
682 if (((param >= pcnt)) || (!AllowHalfop)) break;
686 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], true, 1));
689 r = give_hops(user,parameters[param++],chan,status);
696 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], false, 1));
699 r = take_hops(user,parameters[param++],chan,status);
705 strlcat(outlist,"h",MAXBUF);
706 strlcpy(outpars[pc++],r,MAXBUF);
712 if ((param >= pcnt)) break;
716 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], true, 1));
719 r = give_voice(user,parameters[param++],chan,status);
726 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], false, 1));
729 r = take_voice(user,parameters[param++],chan,status);
735 strlcat(outlist,"v",MAXBUF);
736 strlcpy(outpars[pc++],r,MAXBUF);
741 if ((param >= pcnt)) break;
745 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], true, 1));
748 r = add_ban(user,parameters[param++],chan,status);
755 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], false, 1));
758 r = take_ban(user,parameters[param++],chan,status);
764 strlcat(outlist,"b",MAXBUF);
765 strlcpy(outpars[pc++],parameters[param-1],MAXBUF);
779 if (previously_unset_k)
781 previously_set_k = true;
783 if (!strcmp(chan->key,""))
786 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], true, 1));
791 strlcpy(key,parameters[param++],32);
792 strlcpy(outpars[pc++],key,MAXBUF);
793 strlcpy(chan->key,key,MAXBUF);
801 /* checks on -k are case sensitive and only accurate to the
802 first 32 characters */
803 if (previously_set_k)
805 previously_unset_k = true;
809 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], false, 1));
812 strlcpy(key,parameters[param++],32);
813 /* only allow -k if correct key given */
814 if (!strcmp(chan->key,key))
816 strlcat(outlist,"k",MAXBUF);
817 strlcpy(chan->key,"",MAXBUF);
818 strlcpy(outpars[pc++],key,MAXBUF);
828 if (previously_set_l)
830 previously_unset_l = true;
832 FOREACH_RESULT(OnRawMode(user, chan, 'l', "", false, 0));
843 if ((param >= pcnt)) break;
848 if (previously_unset_l)
850 previously_set_l = true;
851 bool invalid = false;
852 for (unsigned int i = 0; i < strlen(parameters[param]); i++)
854 if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
859 if (atoi(parameters[param]) < 1)
868 FOREACH_RESULT(OnRawMode(user, chan, 'l', parameters[param], true, 1));
872 chan->limit = atoi(parameters[param]);
874 // reported by mech: large values cause underflow
876 chan->limit = 0x7FFF;
881 strlcat(outlist,"l",MAXBUF);
882 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
890 FOREACH_RESULT(OnRawMode(user, chan, 'i', "", mdir, 0));
895 if (!(chan->binarymodes & CM_INVITEONLY)) strlcat(outlist,"i",MAXBUF);
896 chan->binarymodes |= CM_INVITEONLY;
900 if (chan->binarymodes & CM_INVITEONLY) strlcat(outlist,"i",MAXBUF);
901 chan->binarymodes &= ~CM_INVITEONLY;
908 FOREACH_RESULT(OnRawMode(user, chan, 't', "", mdir, 0));
913 if (!(chan->binarymodes & CM_TOPICLOCK)) strlcat(outlist,"t",MAXBUF);
914 chan->binarymodes |= CM_TOPICLOCK;
918 if (chan->binarymodes & CM_NOEXTERNAL) strlcat(outlist,"t",MAXBUF);
919 chan->binarymodes &= ~CM_TOPICLOCK;
926 FOREACH_RESULT(OnRawMode(user, chan, 'n', "", mdir, 0));
931 if (!(chan->binarymodes & CM_NOEXTERNAL)) strlcat(outlist,"n",MAXBUF);
932 chan->binarymodes |= CM_NOEXTERNAL;
936 if (chan->binarymodes & CM_NOEXTERNAL) strlcat(outlist,"n",MAXBUF);
937 chan->binarymodes &= ~CM_NOEXTERNAL;
944 FOREACH_RESULT(OnRawMode(user, chan, 'm', "", mdir, 0));
949 if (!(chan->binarymodes & CM_MODERATED)) strlcat(outlist,"m",MAXBUF);
950 chan->binarymodes |= CM_MODERATED;
954 if (chan->binarymodes & CM_MODERATED) strlcat(outlist,"m",MAXBUF);
955 chan->binarymodes &= ~CM_MODERATED;
962 FOREACH_RESULT(OnRawMode(user, chan, 's', "", mdir, 0));
967 if (!(chan->binarymodes & CM_SECRET)) strlcat(outlist,"s",MAXBUF);
968 chan->binarymodes |= CM_SECRET;
969 if (chan->binarymodes & CM_PRIVATE)
971 chan->binarymodes &= ~CM_PRIVATE;
974 strlcat(outlist,"-p+",MAXBUF);
980 if (chan->binarymodes & CM_SECRET) strlcat(outlist,"s",MAXBUF);
981 chan->binarymodes &= ~CM_SECRET;
988 FOREACH_RESULT(OnRawMode(user, chan, 'p', "", mdir, 0));
993 if (!(chan->binarymodes & CM_PRIVATE)) strlcat(outlist,"p",MAXBUF);
994 chan->binarymodes |= CM_PRIVATE;
995 if (chan->binarymodes & CM_SECRET)
997 chan->binarymodes &= ~CM_SECRET;
1000 strlcat(outlist,"-s+",MAXBUF);
1006 if (chan->binarymodes & CM_PRIVATE) strlcat(outlist,"p",MAXBUF);
1007 chan->binarymodes &= ~CM_PRIVATE;
1013 log(DEBUG,"Preprocessing custom mode %c: modelist: %s",modechar,chan->custom_modes);
1016 if (((!strchr(chan->custom_modes,modechar)) && (!mdir)) || ((strchr(chan->custom_modes,modechar)) && (mdir)))
1018 if (!ModeIsListMode(modechar,MT_CHANNEL))
1020 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",modechar,chan->name);
1024 if (ModeDefined(modechar,MT_CHANNEL))
1026 log(DEBUG,"A module has claimed this mode");
1029 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1031 p.push_back(parameters[param]);
1033 if ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))
1035 p.push_back(parameters[param]);
1038 bool handled = false;
1041 // we're supposed to have a parameter, but none was given... so dont handle the mode.
1042 if (((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir)))
1044 log(DEBUG,"Not enough parameters for module-mode %c",modechar);
1050 // BIG ASS IDIOTIC CODER WARNING!
1051 // Using OnRawMode on another modules mode's behavour
1052 // will confuse the crap out of admins! just because you CAN
1053 // do it, doesnt mean you SHOULD!
1055 std::string para = "";
1058 FOREACH_RESULT(OnRawMode(user, chan, modechar, para, mdir, pcnt));
1061 for (int i = 0; i <= MODCOUNT; i++)
1065 int t = modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p);
1068 log(DEBUG,"OnExtendedMode returned nonzero for a module");
1069 char app[] = {modechar, 0};
1070 if (ModeIsListMode(modechar,MT_CHANNEL))
1081 strlcat(outlist, app,MAXBUF);
1083 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1090 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
1092 strlcat(outlist, app,MAXBUF);
1094 else if (!strchr(outlist,modechar))
1096 strlcat(outlist, app,MAXBUF);
1099 chan->SetCustomMode(modechar,mdir);
1100 // include parameters in output if mode has them
1101 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1103 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
1104 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1107 // break, because only one module can handle the mode.
1116 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,modechar);
1124 /* this ensures only the *valid* modes are sent out onto the network */
1125 int xt = strlen(outlist)-1;
1126 while ((outlist[xt] == '-') || (outlist[xt] == '+'))
1129 xt = strlen(outlist)-1;
1133 strlcpy(outstr,outlist,MAXBUF);
1134 for (ptr = 0; ptr < pc; ptr++)
1136 strlcat(outstr," ",MAXBUF);
1137 strlcat(outstr,outpars[ptr],MAXBUF);
1141 log(DEBUG,"Local mode change");
1142 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outstr);
1143 FOREACH_MOD OnMode(user, chan, TYPE_CHANNEL, outstr);
1151 WriteChannelWithServ(ServerName,chan,"MODE %s %s",chan->name,outstr);
1159 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1166 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1168 bool allowed_umode(char umode, char* sourcemodes,bool adding,bool serveroverride)
1170 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1171 // Servers can +o and -o arbitrarily
1172 if ((serveroverride == true) && (umode == 'o'))
1176 // RFC1459 specified modes
1177 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1179 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1183 // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1184 if ((strchr(sourcemodes,'o')) && (!adding))
1186 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1189 else if (umode == 'o')
1191 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1195 // process any module-defined modes that need oper
1196 if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1198 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1202 if (ModeDefined(umode,MT_CLIENT))
1204 // process any module-defined modes that don't need oper
1205 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1206 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1208 // no, this mode needs oper, and this user 'aint got what it takes!
1214 // anything else - return false.
1215 log(DEBUG,"umode %c not known by any ruleset",umode);
1219 bool process_module_umode(char umode, userrec* source, void* dest, bool adding)
1226 strlcpy(s2->nick,ServerName,NICKMAX);
1227 strlcpy(s2->modes,"o",52);
1234 if (ModeDefined(umode,MT_CLIENT))
1236 for (int i = 0; i <= MODCOUNT; i++)
1238 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1240 log(DEBUG,"Module %s claims umode %c",module_names[i].c_str(),umode);
1244 log(DEBUG,"No module claims umode %c",umode);
1263 void handle_mode(char **parameters, int pcnt, userrec *user)
1269 char outpars[MAXBUF];
1271 dest = Find(parameters[0]);
1278 if ((dest) && (pcnt == 1))
1280 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1284 if ((dest) && (pcnt > 1))
1286 std::string tidied = compress_modes(parameters[1],false);
1287 parameters[1] = (char*)tidied.c_str();
1289 char dmodes[MAXBUF];
1290 strlcpy(dmodes,dest->modes,52);
1291 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1296 if (strchr(user->modes,'o'))
1307 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1311 strcpy(outpars,"+");
1314 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1317 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1319 if (parameters[1][i] == ' ')
1321 if (parameters[1][i] == '+')
1325 int t = strlen(outpars)-1;
1326 if ((outpars[t] == '+') || (outpars[t] == '-'))
1332 strcat(outpars,"+");
1338 if (parameters[1][i] == '-')
1342 int t = strlen(outpars)-1;
1343 if ((outpars[t] == '+') || (outpars[t] == '-'))
1349 strcat(outpars,"-");
1357 if (strchr(user->modes,'o'))
1363 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (allowed_umode(parameters[1][i],user->modes,direction,false)))
1372 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true,false)))
1374 char umode = parameters[1][i];
1375 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1377 int q = strlen(dmodes);
1378 int r = strlen(outpars);
1380 dmodes[q] = parameters[1][i];
1382 outpars[r] = parameters[1][i];
1383 if (parameters[1][i] == 'o')
1385 FOREACH_MOD OnGlobalOper(dest);
1392 if ((allowed_umode(parameters[1][i],user->modes,false,false)) && (strchr(dmodes,parameters[1][i])))
1394 char umode = parameters[1][i];
1395 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1401 unsigned int r = strlen(outpars);
1403 outpars[r] = parameters[1][i];
1406 for (q = 0; q < strlen(dmodes); q++)
1408 if (dmodes[q] != parameters[1][i])
1412 strlcat(temp,moo,MAXBUF);
1415 strlcpy(dmodes,temp,52);
1428 strlcpy(b,"",MAXBUF);
1431 while (i < strlen (outpars))
1433 b[z++] = outpars[i++];
1435 if (i<strlen(outpars)-1)
1437 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1439 // someones playing silly buggers and trying
1440 // to put a +- or -+ into the line...
1444 if (i == strlen(outpars)-1)
1446 if ((outpars[i] == '-') || (outpars[i] == '+'))
1454 if ((b[z] == '-') || (b[z] == '+'))
1457 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1462 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1463 FOREACH_MOD OnMode(user, dest, TYPE_USER, b);
1466 if (strlen(dmodes)>MAXMODES)
1468 dmodes[MAXMODES-1] = '\0';
1470 log(DEBUG,"Stripped mode line");
1471 log(DEBUG,"Line dest is now %s",dmodes);
1472 strlcpy(dest->modes,dmodes,52);
1479 Ptr = FindChan(parameters[0]);
1484 /* just /modes #channel */
1485 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
1486 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1492 char* mode = parameters[1];
1496 FOREACH_RESULT(OnRawMode(user, Ptr, *mode, "", false, 0));
1502 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1504 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1506 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1509 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1511 // list of items for an extmode
1512 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1513 FOREACH_MOD OnSendList(user,Ptr,*mode);
1519 if ((Ptr) && (!has_channel(user,Ptr)))
1521 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1528 FOREACH_RESULT(OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1530 if (MOD_RESULT == ACR_DENY)
1532 if (MOD_RESULT == ACR_DEFAULT)
1534 if (cstatus(user,Ptr) < STATUS_HOP)
1536 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1541 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1546 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1553 void server_mode(char **parameters, int pcnt, userrec *user)
1559 char outpars[MAXBUF];
1561 dest = Find(parameters[0]);
1563 // fix: ChroNiCk found this - we cant use this as debug if its null!
1566 log(DEBUG,"server_mode on %s",dest->nick);
1569 if ((dest) && (pcnt > 1))
1571 std::string tidied = compress_modes(parameters[1],false);
1572 parameters[1] = (char*)tidied.c_str();
1574 char dmodes[MAXBUF];
1575 strlcpy(dmodes,dest->modes,52);
1577 strcpy(outpars,"+");
1580 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1583 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1585 if (parameters[1][i] == ' ')
1587 if (parameters[1][i] == '+')
1591 int t = strlen(outpars)-1;
1592 if ((outpars[t] == '+') || (outpars[t] == '-'))
1598 strcat(outpars,"+");
1604 if (parameters[1][i] == '-')
1608 int t = strlen(outpars)-1;
1609 if ((outpars[t] == '+') || (outpars[t] == '-'))
1615 strcat(outpars,"-");
1622 log(DEBUG,"begin mode processing entry");
1628 log(DEBUG,"umode %c being added",parameters[1][i]);
1629 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true,true)))
1631 char umode = parameters[1][i];
1632 log(DEBUG,"umode %c is an allowed umode",umode);
1633 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1635 int v1 = strlen(dmodes);
1636 int v2 = strlen(outpars);
1638 dmodes[v1] = parameters[1][i];
1640 outpars[v2] = parameters[1][i];
1646 // can only remove a mode they already have
1647 log(DEBUG,"umode %c being removed",parameters[1][i]);
1648 if ((allowed_umode(parameters[1][i],user->modes,false,true)) && (strchr(dmodes,parameters[1][i])))
1650 char umode = parameters[1][i];
1651 log(DEBUG,"umode %c is an allowed umode",umode);
1652 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1658 unsigned int v1 = strlen(outpars);
1660 outpars[v1] = parameters[1][i];
1663 for (q = 0; q < strlen(dmodes); q++)
1665 if (dmodes[q] != parameters[1][i])
1669 strlcat(temp,moo,MAXBUF);
1672 strlcpy(dmodes,temp,52);
1682 strlcpy(b,"",MAXBUF);
1685 while (i < strlen (outpars))
1687 b[z++] = outpars[i++];
1689 if (i<strlen(outpars)-1)
1691 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1693 // someones playing silly buggers and trying
1694 // to put a +- or -+ into the line...
1698 if (i == strlen(outpars)-1)
1700 if ((outpars[i] == '-') || (outpars[i] == '+'))
1708 if ((b[z] == '-') || (b[z] == '+'))
1711 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1716 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1717 FOREACH_MOD OnMode(user, dest, TYPE_USER, b);
1720 if (strlen(dmodes)>MAXMODES)
1722 dmodes[MAXMODES-1] = '\0';
1724 log(DEBUG,"Stripped mode line");
1725 log(DEBUG,"Line dest is now %s",dmodes);
1726 strlcpy(dest->modes,dmodes,MAXMODES);
1733 Ptr = FindChan(parameters[0]);
1736 process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1740 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1746 void merge_mode(char **parameters, int pcnt)
1752 char outpars[MAXBUF];
1754 dest = Find(parameters[0]);
1756 // fix: ChroNiCk found this - we cant use this as debug if its null!
1759 log(DEBUG,"merge_mode on %s",dest->nick);
1762 if ((dest) && (pcnt > 1))
1764 std::string tidied = compress_modes(parameters[1],false);
1765 parameters[1] = (char*)tidied.c_str();
1767 char dmodes[MAXBUF];
1768 strlcpy(dmodes,dest->modes,52);
1770 strcpy(outpars,"+");
1773 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1776 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1778 if (parameters[1][i] == ' ')
1780 if (parameters[1][i] == '+')
1784 int t = strlen(outpars)-1;
1785 if ((outpars[t] == '+') || (outpars[t] == '-'))
1791 strcat(outpars,"+");
1797 if (parameters[1][i] == '-')
1801 int t = strlen(outpars)-1;
1802 if ((outpars[t] == '+') || (outpars[t] == '-'))
1808 strcat(outpars,"-");
1815 log(DEBUG,"begin mode processing entry");
1821 log(DEBUG,"umode %c being added",parameters[1][i]);
1822 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],"o",true,true)))
1824 char umode = parameters[1][i];
1825 log(DEBUG,"umode %c is an allowed umode",umode);
1826 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1828 int v1 = strlen(dmodes);
1829 int v2 = strlen(outpars);
1831 dmodes[v1] = parameters[1][i];
1833 outpars[v2] = parameters[1][i];
1839 // can only remove a mode they already have
1840 log(DEBUG,"umode %c being removed",parameters[1][i]);
1841 if ((allowed_umode(parameters[1][i],"o",false,true)) && (strchr(dmodes,parameters[1][i])))
1843 char umode = parameters[1][i];
1844 log(DEBUG,"umode %c is an allowed umode",umode);
1845 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1851 unsigned int v1 = strlen(outpars);
1853 outpars[v1] = parameters[1][i];
1856 for (q = 0; q < strlen(dmodes); q++)
1858 if (dmodes[q] != parameters[1][i])
1862 strlcat(temp,moo,MAXBUF);
1865 strlcpy(dmodes,temp,52);
1878 while (i < strlen (outpars))
1880 b[z++] = outpars[i++];
1882 if (i<strlen(outpars)-1)
1884 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1886 // someones playing silly buggers and trying
1887 // to put a +- or -+ into the line...
1891 if (i == strlen(outpars)-1)
1893 if ((outpars[i] == '-') || (outpars[i] == '+'))
1901 if ((b[z] == '-') || (b[z] == '+'))
1904 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1907 if (strlen(dmodes)>MAXMODES)
1909 dmodes[MAXMODES-1] = '\0';
1911 log(DEBUG,"Stripped mode line");
1912 log(DEBUG,"Line dest is now %s",dmodes);
1913 strlcpy(dest->modes,dmodes,MAXMODES);
1920 Ptr = FindChan(parameters[0]);
1924 strlcpy(s2.nick,ServerName,NICKMAX);
1925 strcpy(s2.modes,"o");
1927 process_modes(parameters,&s2,Ptr,STATUS_OP,pcnt,true,true,false);
1932 void merge_mode2(char **parameters, int pcnt, userrec* user)
1938 char outpars[MAXBUF];
1940 dest = Find(parameters[0]);
1942 // fix: ChroNiCk found this - we cant use this as debug if its null!
1945 log(DEBUG,"merge_mode2 on %s",dest->nick);
1948 if ((dest) && (pcnt > 1))
1950 std::string tidied = compress_modes(parameters[1],false);
1951 parameters[1] = (char*)tidied.c_str();
1953 char dmodes[MAXBUF];
1954 strlcpy(dmodes,dest->modes,52);
1956 strcpy(outpars,"+");
1959 if ((parameters[1][0] == ':') && (strlen(parameters[1])>1))
1961 // some stupid 3rd party things (such as services packages) put a colon on the mode list...
1962 log(DEBUG,"Some muppet put a colon on the modelist! changed to '%s'",++parameters[1]);
1964 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1967 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1969 if (parameters[1][i] == ' ')
1971 if (parameters[1][i] == '+')
1975 int t = strlen(outpars)-1;
1976 if ((outpars[t] == '+') || (outpars[t] == '-'))
1982 strcat(outpars,"+");
1988 if (parameters[1][i] == '-')
1992 int t = strlen(outpars)-1;
1993 if ((outpars[t] == '+') || (outpars[t] == '-'))
1999 strcat(outpars,"-");
2006 log(DEBUG,"begin mode processing entry");
2012 log(DEBUG,"umode %c being added",parameters[1][i]);
2013 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true,false)))
2015 char umode = parameters[1][i];
2016 log(DEBUG,"umode %c is an allowed umode",umode);
2017 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2019 int v1 = strlen(dmodes);
2020 int v2 = strlen(outpars);
2022 dmodes[v1] = parameters[1][i];
2024 outpars[v2] = parameters[1][i];
2025 log(DEBUG,"OUTPARS='%s', DMODES='%s'",outpars,dmodes);
2031 // can only remove a mode they already have
2032 log(DEBUG,"umode %c being removed",parameters[1][i]);
2033 if ((allowed_umode(parameters[1][i],user->modes,false,false)) && (strchr(dmodes,parameters[1][i])))
2035 char umode = parameters[1][i];
2036 log(DEBUG,"umode %c is an allowed umode",umode);
2037 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2043 unsigned int v1 = strlen(outpars);
2045 outpars[v1] = parameters[1][i];
2048 for (q = 0; q < strlen(dmodes); q++)
2050 if (dmodes[q] != parameters[1][i])
2054 strlcat(temp,moo,MAXBUF);
2057 strlcpy(dmodes,temp,52);
2064 log(DEBUG,"DONE! OUTPARS='%s', DMODES='%s'",outpars,dmodes);
2071 while (i < strlen (outpars))
2073 b[z++] = outpars[i++];
2075 if (i<strlen(outpars)-1)
2077 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
2079 // someones playing silly buggers and trying
2080 // to put a +- or -+ into the line...
2084 if (i == strlen(outpars)-1)
2086 if ((outpars[i] == '-') || (outpars[i] == '+'))
2094 if ((b[z] == '-') || (b[z] == '+'))
2098 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
2103 WriteTo(user,dest,"MODE %s :%s",dest->nick,b);
2104 FOREACH_MOD OnMode(user, dest, TYPE_USER, b);
2107 if (strlen(dmodes)>MAXMODES)
2109 dmodes[MAXMODES-1] = '\0';
2111 log(DEBUG,"Stripped mode line");
2112 log(DEBUG,"Line dest is now %s",dmodes);
2113 strlcpy(dest->modes,dmodes,MAXMODES);
2119 Ptr = FindChan(parameters[0]);
2122 log(DEBUG,"merge_mode2: found channel %s",Ptr->name);
2125 //if ((cstatus(user,Ptr) < STATUS_HOP) && (!is_uline(user->server)))
2129 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,true);