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 * ---------------------------------------------------
18 #include "inspircd_io.h"
19 #include "inspircd_util.h"
20 #include "inspircd_config.h"
23 #include <sys/errno.h>
24 #include <sys/ioctl.h>
25 #include <sys/utsname.h>
30 #include <ext/hash_map>
42 #include "connection.h"
53 #include "inspstring.h"
54 #include "helperfuncs.h"
59 extern std::vector<Module*> modules;
60 extern std::vector<ircd_module*> factory;
61 extern std::vector<std::string> module_names;
65 extern char ServerName[MAXBUF];
66 extern char Network[MAXBUF];
67 extern char ServerDesc[MAXBUF];
68 extern char AdminName[MAXBUF];
69 extern char AdminEmail[MAXBUF];
70 extern char AdminNick[MAXBUF];
71 extern char diepass[MAXBUF];
72 extern char restartpass[MAXBUF];
73 extern char motd[MAXBUF];
74 extern char rules[MAXBUF];
75 extern char list[MAXBUF];
76 extern char PrefixQuit[MAXBUF];
77 extern char DieValue[MAXBUF];
79 extern bool AllowHalfop;
80 extern bool AllowProtect;
81 extern bool AllowFounder;
85 char* give_ops(userrec *user,char *dest,chanrec *chan,int status)
89 if ((!user) || (!dest) || (!chan))
91 log(DEFAULT,"*** BUG *** give_ops was given an invalid parameter");
97 log(DEFAULT,"the target nickname given to give_ops was invalid");
98 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
104 log(DEFAULT,"the target nickname given to give_ops couldnt be found");
105 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
112 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_OP));
114 if (MOD_RESULT == ACR_DENY)
116 if (MOD_RESULT == ACR_DEFAULT)
118 if ((status < STATUS_OP) && (!is_uline(user->server)))
120 log(DEBUG,"%s cant give ops to %s because they nave status %d and needs %d",user->nick,dest,status,STATUS_OP);
121 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
127 for (int i = 0; i != MAXCHANS; i++)
129 if ((d->chans[i].channel != NULL) && (chan != NULL))
130 if (!strcasecmp(d->chans[i].channel->name,chan->name))
132 if (d->chans[i].uc_modes & UCMODE_OP)
134 /* mode already set on user, dont allow multiple */
135 log(DEFAULT,"The target user given to give_ops was already opped on the channel");
138 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
139 log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
143 log(DEFAULT,"The target channel given to give_ops was not in the users mode list");
148 char* give_hops(userrec *user,char *dest,chanrec *chan,int status)
152 if ((!user) || (!dest) || (!chan))
154 log(DEFAULT,"*** BUG *** give_hops was given an invalid parameter");
161 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
166 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
172 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_HALFOP));
174 if (MOD_RESULT == ACR_DENY)
176 if (MOD_RESULT == ACR_DEFAULT)
178 if ((status < STATUS_OP) && (!is_uline(user->server)))
180 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
185 for (int i = 0; i != MAXCHANS; i++)
187 if ((d->chans[i].channel != NULL) && (chan != NULL))
188 if (!strcasecmp(d->chans[i].channel->name,chan->name))
190 if (d->chans[i].uc_modes & UCMODE_HOP)
192 /* mode already set on user, dont allow multiple */
195 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
196 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
204 char* give_voice(userrec *user,char *dest,chanrec *chan,int status)
208 if ((!user) || (!dest) || (!chan))
210 log(DEFAULT,"*** BUG *** give_voice was given an invalid parameter");
217 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
222 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
228 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_VOICE));
230 if (MOD_RESULT == ACR_DENY)
232 if (MOD_RESULT == ACR_DEFAULT)
234 if ((status < STATUS_HOP) && (!is_uline(user->server)))
236 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
241 for (int i = 0; i != MAXCHANS; i++)
243 if ((d->chans[i].channel != NULL) && (chan != NULL))
244 if (!strcasecmp(d->chans[i].channel->name,chan->name))
246 if (d->chans[i].uc_modes & UCMODE_VOICE)
248 /* mode already set on user, dont allow multiple */
251 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
252 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
260 char* take_ops(userrec *user,char *dest,chanrec *chan,int status)
264 if ((!user) || (!dest) || (!chan))
266 log(DEFAULT,"*** BUG *** take_ops was given an invalid parameter");
273 log(DEBUG,"take_ops was given an invalid target nickname of %s",dest);
274 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
279 log(DEBUG,"take_ops couldnt resolve the target nickname: %s",dest);
280 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
286 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEOP));
288 if (MOD_RESULT == ACR_DENY)
290 if (MOD_RESULT == ACR_DEFAULT)
292 if ((status < STATUS_OP) && (!is_uline(user->server)))
294 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
299 for (int i = 0; i != MAXCHANS; i++)
301 if ((d->chans[i].channel != NULL) && (chan != NULL))
302 if (!strcasecmp(d->chans[i].channel->name,chan->name))
304 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
306 /* mode already set on user, dont allow multiple */
309 d->chans[i].uc_modes ^= UCMODE_OP;
310 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
314 log(DEBUG,"take_ops couldnt locate the target channel in the target users list");
319 char* take_hops(userrec *user,char *dest,chanrec *chan,int status)
323 if ((!user) || (!dest) || (!chan))
325 log(DEFAULT,"*** BUG *** take_hops was given an invalid parameter");
332 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
337 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
343 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEHALFOP));
345 if (MOD_RESULT == ACR_DENY)
347 if (MOD_RESULT == ACR_DEFAULT)
349 if ((status < STATUS_OP) && (!is_uline(user->server)))
351 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
356 for (int i = 0; i != MAXCHANS; i++)
358 if ((d->chans[i].channel != NULL) && (chan != NULL))
359 if (!strcasecmp(d->chans[i].channel->name,chan->name))
361 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
363 /* mode already set on user, dont allow multiple */
366 d->chans[i].uc_modes ^= UCMODE_HOP;
367 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
375 char* take_voice(userrec *user,char *dest,chanrec *chan,int status)
379 if ((!user) || (!dest) || (!chan))
381 log(DEFAULT,"*** BUG *** take_voice was given an invalid parameter");
388 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
393 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
399 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEVOICE));
401 if (MOD_RESULT == ACR_DENY)
403 if (MOD_RESULT == ACR_DEFAULT)
405 if ((status < STATUS_HOP) && (!is_uline(user->server)))
407 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
412 for (int i = 0; i != MAXCHANS; i++)
414 if ((d->chans[i].channel != NULL) && (chan != NULL))
415 if (!strcasecmp(d->chans[i].channel->name,chan->name))
417 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
419 /* mode already set on user, dont allow multiple */
422 d->chans[i].uc_modes ^= UCMODE_VOICE;
423 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
431 char* add_ban(userrec *user,char *dest,chanrec *chan,int status)
433 if ((!user) || (!dest) || (!chan)) {
434 log(DEFAULT,"*** BUG *** add_ban was given an invalid parameter");
439 if ((!user) || (!dest) || (!chan))
441 int l = strlen(dest);
442 if (strchr(dest,'!')==0)
444 if (strchr(dest,'@')==0)
446 for (int i = 0; i < l; i++)
449 for (int i = 0; i < l; i++)
453 for (int i = 0; i < l; i++)
459 for (int i = 0; i < l; i++)
465 long maxbans = GetMaxBans(chan->name);
466 if (chan->bans.size() > maxbans)
468 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);
472 log(DEBUG,"add_ban: %s %s",chan->name,user->nick);
475 FOREACH_RESULT(OnAddBan(user,chan,dest));
480 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
482 if (!strcasecmp(i->data,dest))
484 // dont allow a user to set the same ban twice
490 strncpy(b.data,dest,MAXBUF);
491 strncpy(b.set_by,user->nick,NICKMAX);
492 chan->bans.push_back(b);
496 char* take_ban(userrec *user,char *dest,chanrec *chan,int status)
498 if ((!user) || (!dest) || (!chan)) {
499 log(DEFAULT,"*** BUG *** take_ban was given an invalid parameter");
503 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
504 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
506 if (!strcasecmp(i->data,dest))
509 FOREACH_RESULT(OnDelBan(user,chan,dest));
519 // tidies up redundant modes, e.g. +nt-nt+i becomes +-+i,
520 // a section further down the chain tidies up the +-+- crap.
521 std::string compress_modes(std::string modes,bool channelmodes)
526 memset(counts,0,sizeof(counts));
527 memset(active,0,sizeof(active));
528 log(DEBUG,"compress_modes: %s",modes.c_str());
529 for (int i = 0; i < modes.length(); i++)
531 if ((modes[i] == '+') || (modes[i] == '-'))
535 if ((strchr("itnmsp",modes[i])) || ((ModeDefined(modes[i],MT_CHANNEL)) && (ModeDefinedOn(modes[i],MT_CHANNEL)==0) && (ModeDefinedOff(modes[i],MT_CHANNEL)==0)))
537 log(DEBUG,"Tidy mode %c",modes[i]);
538 counts[(unsigned int)modes[i]]++;
539 active[(unsigned int)modes[i]] = true;
544 log(DEBUG,"Tidy mode %c",modes[i]);
545 counts[(unsigned int)modes[i]]++;
546 active[(unsigned int)modes[i]] = true;
549 for (int j = 65; j < 127; j++)
551 if ((counts[j] > 1) && (active[j] == true))
554 v[0] = (unsigned char)j;
556 std::string mode_str = v;
557 std::string::size_type pos = modes.find(mode_str);
558 if (pos != std::string::npos)
560 log(DEBUG,"all occurances of mode %c to be deleted...",(unsigned char)j);
561 while (modes.find(mode_str) != std::string::npos)
562 modes.erase(modes.find(mode_str),1);
563 log(DEBUG,"New mode line: %s",modes.c_str());
570 void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
573 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
577 char modelist[MAXBUF];
578 char outlist[MAXBUF];
580 char outpars[32][MAXBUF];
586 bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
594 FOREACH_RESULT(OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
596 if (MOD_RESULT == ACR_DENY)
599 log(DEBUG,"process_modes: start: parameters=%d",pcnt);
601 strlcpy(modelist,parameters[1],MAXBUF); /* mode list, e.g. +oo-o *
602 * parameters[2] onwards are parameters for
603 * modes that require them :) */
604 strlcpy(outlist,"+",MAXBUF);
607 log(DEBUG,"process_modes: modelist: %s",modelist);
609 std::string tidied = compress_modes(modelist,true);
610 strlcpy(modelist,tidied.c_str(),MAXBUF);
612 int len = strlen(modelist);
613 while (modelist[len-1] == ' ')
614 modelist[--len] = '\0';
615 for (ptr = 0; ptr < len; ptr++)
620 log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
622 char modechar = modelist[ptr];
623 switch (modelist[ptr])
628 int t = strlen(outlist)-1;
629 if ((outlist[t] == '+') || (outlist[t] == '-'))
645 int t = strlen(outlist)-1;
646 if ((outlist[t] == '+') || (outlist[t] == '-'))
660 if ((param >= pcnt)) break;
661 log(DEBUG,"Enough parameters left");
665 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], true, 1));
668 log(DEBUG,"calling give_ops");
669 r = give_ops(user,parameters[param++],chan,status);
676 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], false, 1));
679 log(DEBUG,"calling take_ops");
680 r = take_ops(user,parameters[param++],chan,status);
686 strlcat(outlist,"o",MAXBUF);
687 strlcpy(outpars[pc++],r,MAXBUF);
692 if (((param >= pcnt)) || (!AllowHalfop)) break;
696 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], true, 1));
699 r = give_hops(user,parameters[param++],chan,status);
706 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], false, 1));
709 r = take_hops(user,parameters[param++],chan,status);
715 strlcat(outlist,"h",MAXBUF);
716 strlcpy(outpars[pc++],r,MAXBUF);
722 if ((param >= pcnt)) break;
726 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], true, 1));
729 r = give_voice(user,parameters[param++],chan,status);
736 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], false, 1));
739 r = take_voice(user,parameters[param++],chan,status);
745 strlcat(outlist,"v",MAXBUF);
746 strlcpy(outpars[pc++],r,MAXBUF);
751 if ((param >= pcnt)) break;
755 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], true, 1));
758 r = add_ban(user,parameters[param++],chan,status);
765 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], false, 1));
768 r = take_ban(user,parameters[param++],chan,status);
774 strlcat(outlist,"b",MAXBUF);
775 strlcpy(outpars[pc++],parameters[param-1],MAXBUF);
789 if (previously_unset_k)
791 previously_set_k = true;
793 if (!strcmp(chan->key,""))
796 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], true, 1));
801 strlcpy(key,parameters[param++],32);
802 strlcpy(outpars[pc++],key,MAXBUF);
803 strlcpy(chan->key,key,MAXBUF);
811 /* checks on -k are case sensitive and only accurate to the
812 first 32 characters */
813 if (previously_set_k)
815 previously_unset_k = true;
819 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], false, 1));
822 strlcpy(key,parameters[param++],32);
823 /* only allow -k if correct key given */
824 if (!strcmp(chan->key,key))
826 strlcat(outlist,"k",MAXBUF);
827 strlcpy(chan->key,"",MAXBUF);
828 strlcpy(outpars[pc++],key,MAXBUF);
838 if (previously_set_l)
840 previously_unset_l = true;
842 FOREACH_RESULT(OnRawMode(user, chan, 'l', "", false, 0));
853 if ((param >= pcnt)) break;
858 if (previously_unset_l)
860 previously_set_l = true;
861 bool invalid = false;
862 for (int i = 0; i < strlen(parameters[param]); i++)
864 if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
869 if (atoi(parameters[param]) < 1)
878 FOREACH_RESULT(OnRawMode(user, chan, 'l', parameters[param], true, 1));
882 chan->limit = atoi(parameters[param]);
884 // reported by mech: large values cause underflow
886 chan->limit = 0x7FFF;
891 strlcat(outlist,"l",MAXBUF);
892 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
900 FOREACH_RESULT(OnRawMode(user, chan, 'i', "", mdir, 0));
905 if (!(chan->binarymodes & CM_INVITEONLY)) strlcat(outlist,"i",MAXBUF);
906 chan->binarymodes |= CM_INVITEONLY;
910 if (chan->binarymodes & CM_INVITEONLY) strlcat(outlist,"i",MAXBUF);
911 chan->binarymodes &= ~CM_INVITEONLY;
918 FOREACH_RESULT(OnRawMode(user, chan, 't', "", mdir, 0));
923 if (!(chan->binarymodes & CM_TOPICLOCK)) strlcat(outlist,"t",MAXBUF);
924 chan->binarymodes |= CM_TOPICLOCK;
928 if (chan->binarymodes & CM_NOEXTERNAL) strlcat(outlist,"t",MAXBUF);
929 chan->binarymodes &= ~CM_TOPICLOCK;
936 FOREACH_RESULT(OnRawMode(user, chan, 'n', "", mdir, 0));
941 if (!(chan->binarymodes & CM_NOEXTERNAL)) strlcat(outlist,"n",MAXBUF);
942 chan->binarymodes |= CM_NOEXTERNAL;
946 if (chan->binarymodes & CM_NOEXTERNAL) strlcat(outlist,"n",MAXBUF);
947 chan->binarymodes &= ~CM_NOEXTERNAL;
954 FOREACH_RESULT(OnRawMode(user, chan, 'm', "", mdir, 0));
959 if (!(chan->binarymodes & CM_MODERATED)) strlcat(outlist,"m",MAXBUF);
960 chan->binarymodes |= CM_MODERATED;
964 if (chan->binarymodes & CM_MODERATED) strlcat(outlist,"m",MAXBUF);
965 chan->binarymodes &= ~CM_MODERATED;
972 FOREACH_RESULT(OnRawMode(user, chan, 's', "", mdir, 0));
977 if (!(chan->binarymodes & CM_SECRET)) strlcat(outlist,"s",MAXBUF);
978 chan->binarymodes |= CM_SECRET;
979 if (chan->binarymodes & CM_PRIVATE)
981 chan->binarymodes &= ~CM_PRIVATE;
984 strlcat(outlist,"-p+",MAXBUF);
990 if (chan->binarymodes & CM_SECRET) strlcat(outlist,"s",MAXBUF);
991 chan->binarymodes &= ~CM_SECRET;
998 FOREACH_RESULT(OnRawMode(user, chan, 'p', "", mdir, 0));
1003 if (!(chan->binarymodes & CM_PRIVATE)) strlcat(outlist,"p",MAXBUF);
1004 chan->binarymodes |= CM_PRIVATE;
1005 if (chan->binarymodes & CM_SECRET)
1007 chan->binarymodes &= ~CM_SECRET;
1010 strlcat(outlist,"-s+",MAXBUF);
1016 if (chan->binarymodes & CM_PRIVATE) strlcat(outlist,"p",MAXBUF);
1017 chan->binarymodes &= ~CM_PRIVATE;
1023 log(DEBUG,"Preprocessing custom mode %c: modelist: %s",modechar,chan->custom_modes);
1026 if (((!strchr(chan->custom_modes,modechar)) && (!mdir)) || ((strchr(chan->custom_modes,modechar)) && (mdir)))
1028 if (!ModeIsListMode(modechar,MT_CHANNEL))
1030 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",modechar,chan->name);
1034 if (ModeDefined(modechar,MT_CHANNEL))
1036 log(DEBUG,"A module has claimed this mode");
1039 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1041 p.push_back(parameters[param]);
1043 if ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))
1045 p.push_back(parameters[param]);
1048 bool handled = false;
1051 // we're supposed to have a parameter, but none was given... so dont handle the mode.
1052 if (((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir)))
1054 log(DEBUG,"Not enough parameters for module-mode %c",modechar);
1060 // BIG ASS IDIOTIC CODER WARNING!
1061 // Using OnRawMode on another modules mode's behavour
1062 // will confuse the crap out of admins! just because you CAN
1063 // do it, doesnt mean you SHOULD!
1065 std::string para = "";
1068 FOREACH_RESULT(OnRawMode(user, chan, modechar, para, mdir, pcnt));
1071 for (int i = 0; i <= MODCOUNT; i++)
1075 int t = modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p);
1078 log(DEBUG,"OnExtendedMode returned nonzero for a module");
1079 char app[] = {modechar, 0};
1080 if (ModeIsListMode(modechar,MT_CHANNEL))
1091 strlcat(outlist, app,MAXBUF);
1093 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1100 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
1102 strlcat(outlist, app,MAXBUF);
1104 else if (!strchr(outlist,modechar))
1106 strlcat(outlist, app,MAXBUF);
1109 chan->SetCustomMode(modechar,mdir);
1110 // include parameters in output if mode has them
1111 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1113 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
1114 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1117 // break, because only one module can handle the mode.
1126 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,modechar);
1134 /* this ensures only the *valid* modes are sent out onto the network */
1135 int xt = strlen(outlist)-1;
1136 while ((outlist[xt] == '-') || (outlist[xt] == '+'))
1139 xt = strlen(outlist)-1;
1143 strlcpy(outstr,outlist,MAXBUF);
1144 for (ptr = 0; ptr < pc; ptr++)
1146 strlcat(outstr," ",MAXBUF);
1147 strlcat(outstr,outpars[ptr],MAXBUF);
1151 log(DEBUG,"Local mode change");
1152 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outstr);
1160 WriteChannelWithServ(ServerName,chan,"MODE %s %s",chan->name,outstr);
1161 // M token for a usermode must go to all servers
1162 char buffer[MAXBUF];
1163 snprintf(buffer,MAXBUF,"M %s %s",chan->name, outstr);
1164 NetSendToAll(buffer);
1172 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1173 // M token for a usermode must go to all servers
1174 char buffer[MAXBUF];
1175 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick,chan->name, outstr);
1176 NetSendToAll(buffer);
1183 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1185 bool allowed_umode(char umode, char* sourcemodes,bool adding)
1187 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1188 // RFC1459 specified modes
1189 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1191 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1195 // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1196 if ((strchr(sourcemodes,'o')) && (!adding))
1198 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1201 else if (umode == 'o')
1203 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1207 // process any module-defined modes that need oper
1208 if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1210 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1214 if (ModeDefined(umode,MT_CLIENT))
1216 // process any module-defined modes that don't need oper
1217 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1218 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1220 // no, this mode needs oper, and this user 'aint got what it takes!
1226 // anything else - return false.
1227 log(DEBUG,"umode %c not known by any ruleset",umode);
1231 bool process_module_umode(char umode, userrec* source, void* dest, bool adding)
1238 strlcpy(s2->nick,ServerName,NICKMAX);
1239 strcpy(s2->modes,"o");
1246 if (ModeDefined(umode,MT_CLIENT))
1248 for (int i = 0; i <= MODCOUNT; i++)
1250 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1252 log(DEBUG,"Module %s claims umode %c",module_names[i].c_str(),umode);
1256 log(DEBUG,"No module claims umode %c",umode);
1275 void handle_mode(char **parameters, int pcnt, userrec *user)
1281 char outpars[MAXBUF];
1283 dest = Find(parameters[0]);
1290 if ((dest) && (pcnt == 1))
1292 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1296 if ((dest) && (pcnt > 1))
1298 std::string tidied = compress_modes(parameters[1],false);
1299 parameters[1] = (char*)tidied.c_str();
1301 char dmodes[MAXBUF];
1302 strlcpy(dmodes,dest->modes,MAXBUF);
1303 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1308 if (strchr(user->modes,'o'))
1319 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1323 strcpy(outpars,"+");
1326 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1329 for (int i = 0; i < strlen(parameters[1]); i++)
1331 if (parameters[1][i] == ' ')
1333 if (parameters[1][i] == '+')
1337 int t = strlen(outpars)-1;
1338 if ((outpars[t] == '+') || (outpars[t] == '-'))
1344 strcat(outpars,"+");
1350 if (parameters[1][i] == '-')
1354 int t = strlen(outpars)-1;
1355 if ((outpars[t] == '+') || (outpars[t] == '-'))
1361 strcat(outpars,"-");
1369 if (strchr(user->modes,'o'))
1375 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (allowed_umode(parameters[1][i],user->modes,direction)))
1384 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1386 char umode = parameters[1][i];
1387 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1389 int q = strlen(dmodes);
1390 int r = strlen(outpars);
1392 dmodes[q] = parameters[1][i];
1394 outpars[r] = parameters[1][i];
1395 if (parameters[1][i] == 'o')
1397 FOREACH_MOD OnGlobalOper(dest);
1404 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1406 char umode = parameters[1][i];
1407 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1413 int r = strlen(outpars);
1415 outpars[r] = parameters[1][i];
1418 for (q = 0; q < strlen(dmodes); q++)
1420 if (dmodes[q] != parameters[1][i])
1424 strlcat(temp,moo,MAXBUF);
1427 strlcpy(dmodes,temp,MAXBUF);
1440 strlcpy(b,"",MAXBUF);
1443 while (i < strlen (outpars))
1445 b[z++] = outpars[i++];
1447 if (i<strlen(outpars)-1)
1449 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1451 // someones playing silly buggers and trying
1452 // to put a +- or -+ into the line...
1456 if (i == strlen(outpars)-1)
1458 if ((outpars[i] == '-') || (outpars[i] == '+'))
1466 if ((b[z] == '-') || (b[z] == '+'))
1469 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1474 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1475 // M token for a usermode must go to all servers
1476 char buffer[MAXBUF];
1477 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1478 NetSendToAll(buffer);
1481 if (strlen(dmodes)>MAXMODES)
1483 dmodes[MAXMODES-1] = '\0';
1485 log(DEBUG,"Stripped mode line");
1486 log(DEBUG,"Line dest is now %s",dmodes);
1487 strlcpy(dest->modes,dmodes,MAXMODES);
1494 Ptr = FindChan(parameters[0]);
1499 /* just /modes #channel */
1500 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
1501 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1507 char* mode = parameters[1];
1511 FOREACH_RESULT(OnRawMode(user, Ptr, *mode, "", false, 0));
1517 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1519 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1521 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1524 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1526 // list of items for an extmode
1527 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1528 FOREACH_MOD OnSendList(user,Ptr,*mode);
1534 if ((Ptr) && (!has_channel(user,Ptr)))
1536 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1543 FOREACH_RESULT(OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1545 if (MOD_RESULT == ACR_DENY)
1547 if (MOD_RESULT == ACR_DEFAULT)
1549 if (cstatus(user,Ptr) < STATUS_HOP)
1551 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1556 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1561 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1568 void server_mode(char **parameters, int pcnt, userrec *user)
1574 char outpars[MAXBUF];
1576 dest = Find(parameters[0]);
1578 // fix: ChroNiCk found this - we cant use this as debug if its null!
1581 log(DEBUG,"server_mode on %s",dest->nick);
1584 if ((dest) && (pcnt > 1))
1586 std::string tidied = compress_modes(parameters[1],false);
1587 parameters[1] = (char*)tidied.c_str();
1589 char dmodes[MAXBUF];
1590 strlcpy(dmodes,dest->modes,MAXBUF);
1592 strcpy(outpars,"+");
1595 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1598 for (int i = 0; i < strlen(parameters[1]); i++)
1600 if (parameters[1][i] == ' ')
1602 if (parameters[1][i] == '+')
1606 int t = strlen(outpars)-1;
1607 if ((outpars[t] == '+') || (outpars[t] == '-'))
1613 strcat(outpars,"+");
1619 if (parameters[1][i] == '-')
1623 int t = strlen(outpars)-1;
1624 if ((outpars[t] == '+') || (outpars[t] == '-'))
1630 strcat(outpars,"-");
1637 log(DEBUG,"begin mode processing entry");
1643 log(DEBUG,"umode %c being added",parameters[1][i]);
1644 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1646 char umode = parameters[1][i];
1647 log(DEBUG,"umode %c is an allowed umode",umode);
1648 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1650 int v1 = strlen(dmodes);
1651 int v2 = strlen(outpars);
1653 dmodes[v1] = parameters[1][i];
1655 outpars[v2] = parameters[1][i];
1661 // can only remove a mode they already have
1662 log(DEBUG,"umode %c being removed",parameters[1][i]);
1663 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1665 char umode = parameters[1][i];
1666 log(DEBUG,"umode %c is an allowed umode",umode);
1667 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1673 int v1 = strlen(outpars);
1675 outpars[v1] = parameters[1][i];
1678 for (q = 0; q < strlen(dmodes); q++)
1680 if (dmodes[q] != parameters[1][i])
1684 strlcat(temp,moo,MAXBUF);
1687 strlcpy(dmodes,temp,MAXBUF);
1697 strlcpy(b,"",MAXBUF);
1700 while (i < strlen (outpars))
1702 b[z++] = outpars[i++];
1704 if (i<strlen(outpars)-1)
1706 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1708 // someones playing silly buggers and trying
1709 // to put a +- or -+ into the line...
1713 if (i == strlen(outpars)-1)
1715 if ((outpars[i] == '-') || (outpars[i] == '+'))
1723 if ((b[z] == '-') || (b[z] == '+'))
1726 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1731 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1732 // M token for a usermode must go to all servers
1733 char buffer[MAXBUF];
1734 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1735 NetSendToAll(buffer);
1738 if (strlen(dmodes)>MAXMODES)
1740 dmodes[MAXMODES-1] = '\0';
1742 log(DEBUG,"Stripped mode line");
1743 log(DEBUG,"Line dest is now %s",dmodes);
1744 strlcpy(dest->modes,dmodes,MAXMODES);
1751 Ptr = FindChan(parameters[0]);
1754 process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1758 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1764 void merge_mode(char **parameters, int pcnt)
1770 char outpars[MAXBUF];
1772 dest = Find(parameters[0]);
1774 // fix: ChroNiCk found this - we cant use this as debug if its null!
1777 log(DEBUG,"merge_mode on %s",dest->nick);
1780 if ((dest) && (pcnt > 1))
1782 std::string tidied = compress_modes(parameters[1],false);
1783 parameters[1] = (char*)tidied.c_str();
1785 char dmodes[MAXBUF];
1786 strlcpy(dmodes,dest->modes,MAXBUF);
1788 strcpy(outpars,"+");
1791 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1794 for (int i = 0; i < strlen(parameters[1]); i++)
1796 if (parameters[1][i] == ' ')
1798 if (parameters[1][i] == '+')
1802 int t = strlen(outpars)-1;
1803 if ((outpars[t] == '+') || (outpars[t] == '-'))
1809 strcat(outpars,"+");
1815 if (parameters[1][i] == '-')
1819 int t = strlen(outpars)-1;
1820 if ((outpars[t] == '+') || (outpars[t] == '-'))
1826 strcat(outpars,"-");
1833 log(DEBUG,"begin mode processing entry");
1839 log(DEBUG,"umode %c being added",parameters[1][i]);
1840 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],"o",true)))
1842 char umode = parameters[1][i];
1843 log(DEBUG,"umode %c is an allowed umode",umode);
1844 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1846 int v1 = strlen(dmodes);
1847 int v2 = strlen(outpars);
1849 dmodes[v1] = parameters[1][i];
1851 outpars[v2] = parameters[1][i];
1857 // can only remove a mode they already have
1858 log(DEBUG,"umode %c being removed",parameters[1][i]);
1859 if ((allowed_umode(parameters[1][i],"o",false)) && (strchr(dmodes,parameters[1][i])))
1861 char umode = parameters[1][i];
1862 log(DEBUG,"umode %c is an allowed umode",umode);
1863 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1869 int v1 = strlen(outpars);
1871 outpars[v1] = parameters[1][i];
1874 for (q = 0; q < strlen(dmodes); q++)
1876 if (dmodes[q] != parameters[1][i])
1880 strlcat(temp,moo,MAXBUF);
1883 strlcpy(dmodes,temp,MAXBUF);
1896 while (i < strlen (outpars))
1898 b[z++] = outpars[i++];
1900 if (i<strlen(outpars)-1)
1902 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1904 // someones playing silly buggers and trying
1905 // to put a +- or -+ into the line...
1909 if (i == strlen(outpars)-1)
1911 if ((outpars[i] == '-') || (outpars[i] == '+'))
1919 if ((b[z] == '-') || (b[z] == '+'))
1922 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1925 if (strlen(dmodes)>MAXMODES)
1927 dmodes[MAXMODES-1] = '\0';
1929 log(DEBUG,"Stripped mode line");
1930 log(DEBUG,"Line dest is now %s",dmodes);
1931 strlcpy(dest->modes,dmodes,MAXMODES);
1938 Ptr = FindChan(parameters[0]);
1942 strlcpy(s2.nick,ServerName,NICKMAX);
1943 strcpy(s2.modes,"o");
1945 process_modes(parameters,&s2,Ptr,STATUS_OP,pcnt,true,true,false);
1950 void merge_mode2(char **parameters, int pcnt, userrec* user)
1956 char outpars[MAXBUF];
1958 dest = Find(parameters[0]);
1960 // fix: ChroNiCk found this - we cant use this as debug if its null!
1963 log(DEBUG,"merge_mode2 on %s",dest->nick);
1966 if ((dest) && (pcnt > 1))
1968 std::string tidied = compress_modes(parameters[1],false);
1969 parameters[1] = (char*)tidied.c_str();
1971 char dmodes[MAXBUF];
1972 strlcpy(dmodes,dest->modes,MAXBUF);
1974 strcpy(outpars,"+");
1977 if ((parameters[1][0] == ':') && (strlen(parameters[1])>1))
1979 // some stupid 3rd party things (such as services packages) put a colon on the mode list...
1980 log(DEBUG,"Some muppet put a colon on the modelist! changed to '%s'",++parameters[1]);
1982 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1985 for (int i = 0; i < strlen(parameters[1]); i++)
1987 if (parameters[1][i] == ' ')
1989 if (parameters[1][i] == '+')
1993 int t = strlen(outpars)-1;
1994 if ((outpars[t] == '+') || (outpars[t] == '-'))
2000 strcat(outpars,"+");
2006 if (parameters[1][i] == '-')
2010 int t = strlen(outpars)-1;
2011 if ((outpars[t] == '+') || (outpars[t] == '-'))
2017 strcat(outpars,"-");
2024 log(DEBUG,"begin mode processing entry");
2030 log(DEBUG,"umode %c being added",parameters[1][i]);
2031 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
2033 char umode = parameters[1][i];
2034 log(DEBUG,"umode %c is an allowed umode",umode);
2035 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2037 int v1 = strlen(dmodes);
2038 int v2 = strlen(outpars);
2040 dmodes[v1] = parameters[1][i];
2042 outpars[v2] = parameters[1][i];
2043 log(DEBUG,"OUTPARS='%s', DMODES='%s'",outpars,dmodes);
2049 // can only remove a mode they already have
2050 log(DEBUG,"umode %c being removed",parameters[1][i]);
2051 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
2053 char umode = parameters[1][i];
2054 log(DEBUG,"umode %c is an allowed umode",umode);
2055 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2061 int v1 = strlen(outpars);
2063 outpars[v1] = parameters[1][i];
2066 for (q = 0; q < strlen(dmodes); q++)
2068 if (dmodes[q] != parameters[1][i])
2072 strlcat(temp,moo,MAXBUF);
2075 strlcpy(dmodes,temp,MAXBUF);
2082 log(DEBUG,"DONE! OUTPARS='%s', DMODES='%s'",outpars,dmodes);
2089 while (i < strlen (outpars))
2091 b[z++] = outpars[i++];
2093 if (i<strlen(outpars)-1)
2095 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
2097 // someones playing silly buggers and trying
2098 // to put a +- or -+ into the line...
2102 if (i == strlen(outpars)-1)
2104 if ((outpars[i] == '-') || (outpars[i] == '+'))
2112 if ((b[z] == '-') || (b[z] == '+'))
2116 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
2121 WriteTo(user,dest,"MODE %s :%s",dest->nick,b);
2122 log(DEBUG,"Sent: :%s MODE %s",user->nick,b);
2125 if (strlen(dmodes)>MAXMODES)
2127 dmodes[MAXMODES-1] = '\0';
2129 log(DEBUG,"Stripped mode line");
2130 log(DEBUG,"Line dest is now %s",dmodes);
2131 strlcpy(dest->modes,dmodes,MAXMODES);
2137 Ptr = FindChan(parameters[0]);
2140 log(DEBUG,"merge_mode2: found channel %s",Ptr->name);
2143 if ((cstatus(user,Ptr) < STATUS_HOP) && (!is_uline(user->server)))
2147 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,true);