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"
58 extern std::vector<Module*> modules;
59 extern std::vector<ircd_module*> factory;
60 extern std::vector<std::string> module_names;
64 extern char ServerName[MAXBUF];
65 extern char Network[MAXBUF];
66 extern char ServerDesc[MAXBUF];
67 extern char AdminName[MAXBUF];
68 extern char AdminEmail[MAXBUF];
69 extern char AdminNick[MAXBUF];
70 extern char diepass[MAXBUF];
71 extern char restartpass[MAXBUF];
72 extern char motd[MAXBUF];
73 extern char rules[MAXBUF];
74 extern char list[MAXBUF];
75 extern char PrefixQuit[MAXBUF];
76 extern char DieValue[MAXBUF];
78 extern bool AllowHalfop;
79 extern bool AllowProtect;
80 extern bool AllowFounder;
84 char* give_ops(userrec *user,char *dest,chanrec *chan,int status)
88 if ((!user) || (!dest) || (!chan))
90 log(DEFAULT,"*** BUG *** give_ops was given an invalid parameter");
96 log(DEFAULT,"the target nickname given to give_ops was invalid");
97 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
103 log(DEFAULT,"the target nickname given to give_ops couldnt be found");
104 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
111 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_OP));
113 if (MOD_RESULT == ACR_DENY)
115 if (MOD_RESULT == ACR_DEFAULT)
117 if ((status < STATUS_OP) && (!is_uline(user->server)))
119 log(DEBUG,"%s cant give ops to %s because they nave status %d and needs %d",user->nick,dest,status,STATUS_OP);
120 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
126 for (int i = 0; i != MAXCHANS; i++)
128 if ((d->chans[i].channel != NULL) && (chan != NULL))
129 if (!strcasecmp(d->chans[i].channel->name,chan->name))
131 if (d->chans[i].uc_modes & UCMODE_OP)
133 /* mode already set on user, dont allow multiple */
134 log(DEFAULT,"The target user given to give_ops was already opped on the channel");
137 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
138 log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
142 log(DEFAULT,"The target channel given to give_ops was not in the users mode list");
147 char* give_hops(userrec *user,char *dest,chanrec *chan,int status)
151 if ((!user) || (!dest) || (!chan))
153 log(DEFAULT,"*** BUG *** give_hops was given an invalid parameter");
160 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
165 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
171 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_HALFOP));
173 if (MOD_RESULT == ACR_DENY)
175 if (MOD_RESULT == ACR_DEFAULT)
177 if ((status < STATUS_OP) && (!is_uline(user->server)))
179 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
184 for (int i = 0; i != MAXCHANS; i++)
186 if ((d->chans[i].channel != NULL) && (chan != NULL))
187 if (!strcasecmp(d->chans[i].channel->name,chan->name))
189 if (d->chans[i].uc_modes & UCMODE_HOP)
191 /* mode already set on user, dont allow multiple */
194 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
195 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
203 char* give_voice(userrec *user,char *dest,chanrec *chan,int status)
207 if ((!user) || (!dest) || (!chan))
209 log(DEFAULT,"*** BUG *** give_voice was given an invalid parameter");
216 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
221 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
227 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_VOICE));
229 if (MOD_RESULT == ACR_DENY)
231 if (MOD_RESULT == ACR_DEFAULT)
233 if ((status < STATUS_HOP) && (!is_uline(user->server)))
235 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
240 for (int i = 0; i != MAXCHANS; i++)
242 if ((d->chans[i].channel != NULL) && (chan != NULL))
243 if (!strcasecmp(d->chans[i].channel->name,chan->name))
245 if (d->chans[i].uc_modes & UCMODE_VOICE)
247 /* mode already set on user, dont allow multiple */
250 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
251 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
259 char* take_ops(userrec *user,char *dest,chanrec *chan,int status)
263 if ((!user) || (!dest) || (!chan))
265 log(DEFAULT,"*** BUG *** take_ops was given an invalid parameter");
272 log(DEBUG,"take_ops was given an invalid target nickname of %s",dest);
273 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
278 log(DEBUG,"take_ops couldnt resolve the target nickname: %s",dest);
279 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
285 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEOP));
287 if (MOD_RESULT == ACR_DENY)
289 if (MOD_RESULT == ACR_DEFAULT)
291 if ((status < STATUS_OP) && (!is_uline(user->server)))
293 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
298 for (int i = 0; i != MAXCHANS; i++)
300 if ((d->chans[i].channel != NULL) && (chan != NULL))
301 if (!strcasecmp(d->chans[i].channel->name,chan->name))
303 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
305 /* mode already set on user, dont allow multiple */
308 d->chans[i].uc_modes ^= UCMODE_OP;
309 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
313 log(DEBUG,"take_ops couldnt locate the target channel in the target users list");
318 char* take_hops(userrec *user,char *dest,chanrec *chan,int status)
322 if ((!user) || (!dest) || (!chan))
324 log(DEFAULT,"*** BUG *** take_hops was given an invalid parameter");
331 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
336 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
342 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEHALFOP));
344 if (MOD_RESULT == ACR_DENY)
346 if (MOD_RESULT == ACR_DEFAULT)
348 if ((status < STATUS_OP) && (!is_uline(user->server)))
350 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
355 for (int i = 0; i != MAXCHANS; i++)
357 if ((d->chans[i].channel != NULL) && (chan != NULL))
358 if (!strcasecmp(d->chans[i].channel->name,chan->name))
360 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
362 /* mode already set on user, dont allow multiple */
365 d->chans[i].uc_modes ^= UCMODE_HOP;
366 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
374 char* take_voice(userrec *user,char *dest,chanrec *chan,int status)
378 if ((!user) || (!dest) || (!chan))
380 log(DEFAULT,"*** BUG *** take_voice was given an invalid parameter");
387 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
392 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
398 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEVOICE));
400 if (MOD_RESULT == ACR_DENY)
402 if (MOD_RESULT == ACR_DEFAULT)
404 if ((status < STATUS_HOP) && (!is_uline(user->server)))
406 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
411 for (int i = 0; i != MAXCHANS; i++)
413 if ((d->chans[i].channel != NULL) && (chan != NULL))
414 if (!strcasecmp(d->chans[i].channel->name,chan->name))
416 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
418 /* mode already set on user, dont allow multiple */
421 d->chans[i].uc_modes ^= UCMODE_VOICE;
422 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
430 char* add_ban(userrec *user,char *dest,chanrec *chan,int status)
432 if ((!user) || (!dest) || (!chan)) {
433 log(DEFAULT,"*** BUG *** add_ban was given an invalid parameter");
438 if ((!user) || (!dest) || (!chan))
440 int l = strlen(dest);
441 if (strchr(dest,'!')==0)
443 if (strchr(dest,'@')==0)
445 for (int i = 0; i < l; i++)
448 for (int i = 0; i < l; i++)
452 for (int i = 0; i < l; i++)
458 for (int i = 0; i < l; i++)
464 long maxbans = GetMaxBans(chan->name);
465 if (chan->bans.size() > maxbans)
467 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);
471 log(DEBUG,"add_ban: %s %s",chan->name,user->nick);
474 FOREACH_RESULT(OnAddBan(user,chan,dest));
479 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
481 if (!strcasecmp(i->data,dest))
483 // dont allow a user to set the same ban twice
489 strncpy(b.data,dest,MAXBUF);
490 strncpy(b.set_by,user->nick,NICKMAX);
491 chan->bans.push_back(b);
495 char* take_ban(userrec *user,char *dest,chanrec *chan,int status)
497 if ((!user) || (!dest) || (!chan)) {
498 log(DEFAULT,"*** BUG *** take_ban was given an invalid parameter");
502 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
503 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
505 if (!strcasecmp(i->data,dest))
508 FOREACH_RESULT(OnDelBan(user,chan,dest));
518 // tidies up redundant modes, e.g. +nt-nt+i becomes +-+i,
519 // a section further down the chain tidies up the +-+- crap.
520 std::string compress_modes(std::string modes,bool channelmodes)
525 memset(counts,0,sizeof(counts));
526 memset(active,0,sizeof(active));
527 log(DEBUG,"compress_modes: %s",modes.c_str());
528 for (int i = 0; i < modes.length(); i++)
530 if ((modes[i] == '+') || (modes[i] == '-'))
534 if ((strchr("itnmsp",modes[i])) || ((ModeDefined(modes[i],MT_CHANNEL)) && (ModeDefinedOn(modes[i],MT_CHANNEL)==0) && (ModeDefinedOff(modes[i],MT_CHANNEL)==0)))
536 log(DEBUG,"Tidy mode %c",modes[i]);
537 counts[(unsigned int)modes[i]]++;
538 active[(unsigned int)modes[i]] = true;
543 log(DEBUG,"Tidy mode %c",modes[i]);
544 counts[(unsigned int)modes[i]]++;
545 active[(unsigned int)modes[i]] = true;
548 for (int j = 65; j < 127; j++)
550 if ((counts[j] > 1) && (active[j] == true))
553 v[0] = (unsigned char)j;
555 std::string::size_type pos = modes.find(std::string(v));
556 if (pos != std::string::npos)
558 log(DEBUG,"all occurances of mode %c to be deleted...",(unsigned char)j);
559 while (modes.find(std::string(v)) != std::string::npos)
560 modes.erase(modes.find(std::string(v)),1);
561 log(DEBUG,"New mode line: %s",modes.c_str());
568 void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
571 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
575 char modelist[MAXBUF];
576 char outlist[MAXBUF];
578 char outpars[32][MAXBUF];
584 bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
592 FOREACH_RESULT(OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
594 if (MOD_RESULT == ACR_DENY)
597 log(DEBUG,"process_modes: start: parameters=%d",pcnt);
599 strlcpy(modelist,parameters[1],MAXBUF); /* mode list, e.g. +oo-o *
600 * parameters[2] onwards are parameters for
601 * modes that require them :) */
602 strlcpy(outlist,"+",MAXBUF);
605 log(DEBUG,"process_modes: modelist: %s",modelist);
607 std::string tidied = compress_modes(modelist,true);
608 strlcpy(modelist,tidied.c_str(),MAXBUF);
610 int len = strlen(modelist);
611 while (modelist[len-1] == ' ')
612 modelist[--len] = '\0';
613 for (ptr = 0; ptr < len; ptr++)
618 log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
620 char modechar = modelist[ptr];
621 switch (modelist[ptr])
626 int t = strlen(outlist)-1;
627 if ((outlist[t] == '+') || (outlist[t] == '-'))
643 int t = strlen(outlist)-1;
644 if ((outlist[t] == '+') || (outlist[t] == '-'))
658 if ((param >= pcnt)) break;
659 log(DEBUG,"Enough parameters left");
663 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], true, 1));
666 log(DEBUG,"calling give_ops");
667 r = give_ops(user,parameters[param++],chan,status);
674 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], false, 1));
677 log(DEBUG,"calling take_ops");
678 r = take_ops(user,parameters[param++],chan,status);
684 strlcat(outlist,"o",MAXBUF);
685 strlcpy(outpars[pc++],r,MAXBUF);
690 if (((param >= pcnt)) || (!AllowHalfop)) break;
694 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], true, 1));
697 r = give_hops(user,parameters[param++],chan,status);
704 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], false, 1));
707 r = take_hops(user,parameters[param++],chan,status);
713 strlcat(outlist,"h",MAXBUF);
714 strlcpy(outpars[pc++],r,MAXBUF);
720 if ((param >= pcnt)) break;
724 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], true, 1));
727 r = give_voice(user,parameters[param++],chan,status);
734 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], false, 1));
737 r = take_voice(user,parameters[param++],chan,status);
743 strlcat(outlist,"v",MAXBUF);
744 strlcpy(outpars[pc++],r,MAXBUF);
749 if ((param >= pcnt)) break;
753 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], true, 1));
756 r = add_ban(user,parameters[param++],chan,status);
763 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], false, 1));
766 r = take_ban(user,parameters[param++],chan,status);
772 strlcat(outlist,"b",MAXBUF);
773 strlcpy(outpars[pc++],parameters[param-1],MAXBUF);
787 if (previously_unset_k)
789 previously_set_k = true;
791 if (!strcmp(chan->key,""))
794 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], true, 1));
799 strlcpy(key,parameters[param++],32);
800 strlcpy(outpars[pc++],key,MAXBUF);
801 strlcpy(chan->key,key,MAXBUF);
809 /* checks on -k are case sensitive and only accurate to the
810 first 32 characters */
811 if (previously_set_k)
813 previously_unset_k = true;
817 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], false, 1));
820 strlcpy(key,parameters[param++],32);
821 /* only allow -k if correct key given */
822 if (!strcmp(chan->key,key))
824 strlcat(outlist,"k",MAXBUF);
825 strlcpy(chan->key,"",MAXBUF);
826 strlcpy(outpars[pc++],key,MAXBUF);
836 if (previously_set_l)
838 previously_unset_l = true;
840 FOREACH_RESULT(OnRawMode(user, chan, 'l', "", false, 0));
851 if ((param >= pcnt)) break;
856 if (previously_unset_l)
858 previously_set_l = true;
859 bool invalid = false;
860 for (int i = 0; i < strlen(parameters[param]); i++)
862 if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
867 if (atoi(parameters[param]) < 1)
876 FOREACH_RESULT(OnRawMode(user, chan, 'l', parameters[param], true, 1));
880 chan->limit = atoi(parameters[param]);
882 // reported by mech: large values cause underflow
884 chan->limit = 0x7FFFFF;
889 strlcat(outlist,"l",MAXBUF);
890 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
898 FOREACH_RESULT(OnRawMode(user, chan, 'i', "", mdir, 0));
901 if (chan->inviteonly != mdir)
903 strlcat(outlist,"i",MAXBUF);
905 chan->inviteonly = mdir;
911 FOREACH_RESULT(OnRawMode(user, chan, 't', "", mdir, 0));
914 if (chan->topiclock != mdir)
916 strlcat(outlist,"t",MAXBUF);
918 chan->topiclock = mdir;
924 FOREACH_RESULT(OnRawMode(user, chan, 'n', "", mdir, 0));
927 if (chan->noexternal != mdir)
929 strlcat(outlist,"n",MAXBUF);
931 chan->noexternal = mdir;
937 FOREACH_RESULT(OnRawMode(user, chan, 'm', "", mdir, 0));
940 if (chan->moderated != mdir)
942 strlcat(outlist,"m",MAXBUF);
944 chan->moderated = mdir;
950 FOREACH_RESULT(OnRawMode(user, chan, 's', "", mdir, 0));
953 if (chan->secret != mdir)
961 strlcat(outlist,"-p+",MAXBUF);
965 strlcat(outlist,"+p-",MAXBUF);
975 FOREACH_RESULT(OnRawMode(user, chan, 'p', "", mdir, 0));
978 if (chan->c_private != mdir)
980 strlcat(outlist,"p",MAXBUF);
986 strlcat(outlist,"-s+",MAXBUF);
990 strlcat(outlist,"+s-",MAXBUF);
994 chan->c_private = mdir;
999 log(DEBUG,"Preprocessing custom mode %c: modelist: %s",modechar,chan->custom_modes);
1002 if (((!strchr(chan->custom_modes,modechar)) && (!mdir)) || ((strchr(chan->custom_modes,modechar)) && (mdir)))
1004 if (!ModeIsListMode(modechar,MT_CHANNEL))
1006 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",modechar,chan->name);
1010 if (ModeDefined(modechar,MT_CHANNEL))
1012 log(DEBUG,"A module has claimed this mode");
1015 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1017 p.push_back(parameters[param]);
1019 if ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))
1021 p.push_back(parameters[param]);
1024 bool handled = false;
1027 // we're supposed to have a parameter, but none was given... so dont handle the mode.
1028 if (((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir)))
1030 log(DEBUG,"Not enough parameters for module-mode %c",modechar);
1036 // BIG ASS IDIOTIC CODER WARNING!
1037 // Using OnRawMode on another modules mode's behavour
1038 // will confuse the crap out of admins! just because you CAN
1039 // do it, doesnt mean you SHOULD!
1041 std::string para = "";
1044 FOREACH_RESULT(OnRawMode(user, chan, modechar, para, mdir, pcnt));
1047 for (int i = 0; i <= MODCOUNT; i++)
1051 int t = modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p);
1054 log(DEBUG,"OnExtendedMode returned nonzero for a module");
1055 char app[] = {modechar, 0};
1056 if (ModeIsListMode(modechar,MT_CHANNEL))
1066 strlcat(outlist, app,MAXBUF);
1068 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1075 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
1077 strlcat(outlist, app,MAXBUF);
1079 else if (!strchr(outlist,modechar))
1081 strlcat(outlist, app,MAXBUF);
1084 chan->SetCustomMode(modechar,mdir);
1085 // include parameters in output if mode has them
1086 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1088 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
1089 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1092 // break, because only one module can handle the mode.
1101 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,modechar);
1109 /* this ensures only the *valid* modes are sent out onto the network */
1110 int xt = strlen(outlist)-1;
1111 while ((outlist[xt] == '-') || (outlist[xt] == '+'))
1114 xt = strlen(outlist)-1;
1118 strlcpy(outstr,outlist,MAXBUF);
1119 for (ptr = 0; ptr < pc; ptr++)
1121 strlcat(outstr," ",MAXBUF);
1122 strlcat(outstr,outpars[ptr],MAXBUF);
1126 log(DEBUG,"Local mode change");
1127 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outstr);
1135 WriteChannelWithServ(ServerName,chan,"MODE %s %s",chan->name,outstr);
1136 // M token for a usermode must go to all servers
1137 char buffer[MAXBUF];
1138 snprintf(buffer,MAXBUF,"M %s %s",chan->name, outstr);
1139 NetSendToAll(buffer);
1147 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1148 // M token for a usermode must go to all servers
1149 char buffer[MAXBUF];
1150 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick,chan->name, outstr);
1151 NetSendToAll(buffer);
1158 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1160 bool allowed_umode(char umode, char* sourcemodes,bool adding)
1162 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1163 // RFC1459 specified modes
1164 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1166 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1170 // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1171 if ((strchr(sourcemodes,'o')) && (!adding))
1173 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1176 else if (umode == 'o')
1178 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1182 // process any module-defined modes that need oper
1183 if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1185 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1189 if (ModeDefined(umode,MT_CLIENT))
1191 // process any module-defined modes that don't need oper
1192 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1193 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1195 // no, this mode needs oper, and this user 'aint got what it takes!
1201 // anything else - return false.
1202 log(DEBUG,"umode %c not known by any ruleset",umode);
1206 bool process_module_umode(char umode, userrec* source, void* dest, bool adding)
1213 strlcpy(s2->nick,ServerName,NICKMAX);
1214 strcpy(s2->modes,"o");
1221 if (ModeDefined(umode,MT_CLIENT))
1223 for (int i = 0; i <= MODCOUNT; i++)
1225 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1227 log(DEBUG,"Module %s claims umode %c",module_names[i].c_str(),umode);
1231 log(DEBUG,"No module claims umode %c",umode);
1250 void handle_mode(char **parameters, int pcnt, userrec *user)
1256 char outpars[MAXBUF];
1258 dest = Find(parameters[0]);
1265 if ((dest) && (pcnt == 1))
1267 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1271 if ((dest) && (pcnt > 1))
1273 std::string tidied = compress_modes(parameters[1],false);
1274 parameters[1] = (char*)tidied.c_str();
1276 char dmodes[MAXBUF];
1277 strlcpy(dmodes,dest->modes,MAXBUF);
1278 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1283 if (strchr(user->modes,'o'))
1294 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1298 strcpy(outpars,"+");
1301 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1304 for (int i = 0; i < strlen(parameters[1]); i++)
1306 if (parameters[1][i] == ' ')
1308 if (parameters[1][i] == '+')
1312 int t = strlen(outpars)-1;
1313 if ((outpars[t] == '+') || (outpars[t] == '-'))
1319 strcat(outpars,"+");
1325 if (parameters[1][i] == '-')
1329 int t = strlen(outpars)-1;
1330 if ((outpars[t] == '+') || (outpars[t] == '-'))
1336 strcat(outpars,"-");
1344 if (strchr(user->modes,'o'))
1350 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (allowed_umode(parameters[1][i],user->modes,direction)))
1359 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1361 char umode = parameters[1][i];
1362 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1364 int q = strlen(dmodes);
1365 int r = strlen(outpars);
1367 dmodes[q] = parameters[1][i];
1369 outpars[r] = parameters[1][i];
1370 if (parameters[1][i] == 'o')
1372 FOREACH_MOD OnGlobalOper(dest);
1379 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1381 char umode = parameters[1][i];
1382 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1388 int r = strlen(outpars);
1390 outpars[r] = parameters[1][i];
1393 for (q = 0; q < strlen(dmodes); q++)
1395 if (dmodes[q] != parameters[1][i])
1399 strlcat(temp,moo,MAXBUF);
1402 strlcpy(dmodes,temp,MAXBUF);
1415 strlcpy(b,"",MAXBUF);
1418 while (i < strlen (outpars))
1420 b[z++] = outpars[i++];
1422 if (i<strlen(outpars)-1)
1424 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1426 // someones playing silly buggers and trying
1427 // to put a +- or -+ into the line...
1431 if (i == strlen(outpars)-1)
1433 if ((outpars[i] == '-') || (outpars[i] == '+'))
1441 if ((b[z] == '-') || (b[z] == '+'))
1444 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1449 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1450 // M token for a usermode must go to all servers
1451 char buffer[MAXBUF];
1452 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1453 NetSendToAll(buffer);
1456 if (strlen(dmodes)>MAXMODES)
1458 dmodes[MAXMODES-1] = '\0';
1460 log(DEBUG,"Stripped mode line");
1461 log(DEBUG,"Line dest is now %s",dmodes);
1462 strlcpy(dest->modes,dmodes,MAXMODES);
1469 Ptr = FindChan(parameters[0]);
1474 /* just /modes #channel */
1475 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
1476 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1482 char* mode = parameters[1];
1486 FOREACH_RESULT(OnRawMode(user, Ptr, *mode, "", false, 0));
1492 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1494 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1496 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1499 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1501 // list of items for an extmode
1502 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1503 FOREACH_MOD OnSendList(user,Ptr,*mode);
1509 if ((Ptr) && (!has_channel(user,Ptr)))
1511 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1518 FOREACH_RESULT(OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1520 if (MOD_RESULT == ACR_DENY)
1522 if (MOD_RESULT == ACR_DEFAULT)
1524 if (cstatus(user,Ptr) < STATUS_HOP)
1526 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1531 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1536 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1543 void server_mode(char **parameters, int pcnt, userrec *user)
1549 char outpars[MAXBUF];
1551 dest = Find(parameters[0]);
1553 // fix: ChroNiCk found this - we cant use this as debug if its null!
1556 log(DEBUG,"server_mode on %s",dest->nick);
1559 if ((dest) && (pcnt > 1))
1561 std::string tidied = compress_modes(parameters[1],false);
1562 parameters[1] = (char*)tidied.c_str();
1564 char dmodes[MAXBUF];
1565 strlcpy(dmodes,dest->modes,MAXBUF);
1567 strcpy(outpars,"+");
1570 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1573 for (int i = 0; i < strlen(parameters[1]); i++)
1575 if (parameters[1][i] == ' ')
1577 if (parameters[1][i] == '+')
1581 int t = strlen(outpars)-1;
1582 if ((outpars[t] == '+') || (outpars[t] == '-'))
1588 strcat(outpars,"+");
1594 if (parameters[1][i] == '-')
1598 int t = strlen(outpars)-1;
1599 if ((outpars[t] == '+') || (outpars[t] == '-'))
1605 strcat(outpars,"-");
1612 log(DEBUG,"begin mode processing entry");
1618 log(DEBUG,"umode %c being added",parameters[1][i]);
1619 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1621 char umode = parameters[1][i];
1622 log(DEBUG,"umode %c is an allowed umode",umode);
1623 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1625 int v1 = strlen(dmodes);
1626 int v2 = strlen(outpars);
1628 dmodes[v1] = parameters[1][i];
1630 outpars[v2] = parameters[1][i];
1636 // can only remove a mode they already have
1637 log(DEBUG,"umode %c being removed",parameters[1][i]);
1638 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1640 char umode = parameters[1][i];
1641 log(DEBUG,"umode %c is an allowed umode",umode);
1642 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1648 int v1 = strlen(outpars);
1650 outpars[v1] = parameters[1][i];
1653 for (q = 0; q < strlen(dmodes); q++)
1655 if (dmodes[q] != parameters[1][i])
1659 strlcat(temp,moo,MAXBUF);
1662 strlcpy(dmodes,temp,MAXBUF);
1672 strlcpy(b,"",MAXBUF);
1675 while (i < strlen (outpars))
1677 b[z++] = outpars[i++];
1679 if (i<strlen(outpars)-1)
1681 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1683 // someones playing silly buggers and trying
1684 // to put a +- or -+ into the line...
1688 if (i == strlen(outpars)-1)
1690 if ((outpars[i] == '-') || (outpars[i] == '+'))
1698 if ((b[z] == '-') || (b[z] == '+'))
1701 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1706 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1707 // M token for a usermode must go to all servers
1708 char buffer[MAXBUF];
1709 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1710 NetSendToAll(buffer);
1713 if (strlen(dmodes)>MAXMODES)
1715 dmodes[MAXMODES-1] = '\0';
1717 log(DEBUG,"Stripped mode line");
1718 log(DEBUG,"Line dest is now %s",dmodes);
1719 strlcpy(dest->modes,dmodes,MAXMODES);
1726 Ptr = FindChan(parameters[0]);
1729 process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1733 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1739 void merge_mode(char **parameters, int pcnt)
1745 char outpars[MAXBUF];
1747 dest = Find(parameters[0]);
1749 // fix: ChroNiCk found this - we cant use this as debug if its null!
1752 log(DEBUG,"merge_mode on %s",dest->nick);
1755 if ((dest) && (pcnt > 1))
1757 std::string tidied = compress_modes(parameters[1],false);
1758 parameters[1] = (char*)tidied.c_str();
1760 char dmodes[MAXBUF];
1761 strlcpy(dmodes,dest->modes,MAXBUF);
1763 strcpy(outpars,"+");
1766 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1769 for (int i = 0; i < strlen(parameters[1]); i++)
1771 if (parameters[1][i] == ' ')
1773 if (parameters[1][i] == '+')
1777 int t = strlen(outpars)-1;
1778 if ((outpars[t] == '+') || (outpars[t] == '-'))
1784 strcat(outpars,"+");
1790 if (parameters[1][i] == '-')
1794 int t = strlen(outpars)-1;
1795 if ((outpars[t] == '+') || (outpars[t] == '-'))
1801 strcat(outpars,"-");
1808 log(DEBUG,"begin mode processing entry");
1814 log(DEBUG,"umode %c being added",parameters[1][i]);
1815 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],"o",true)))
1817 char umode = parameters[1][i];
1818 log(DEBUG,"umode %c is an allowed umode",umode);
1819 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1821 int v1 = strlen(dmodes);
1822 int v2 = strlen(outpars);
1824 dmodes[v1] = parameters[1][i];
1826 outpars[v2] = parameters[1][i];
1832 // can only remove a mode they already have
1833 log(DEBUG,"umode %c being removed",parameters[1][i]);
1834 if ((allowed_umode(parameters[1][i],"o",false)) && (strchr(dmodes,parameters[1][i])))
1836 char umode = parameters[1][i];
1837 log(DEBUG,"umode %c is an allowed umode",umode);
1838 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1844 int v1 = strlen(outpars);
1846 outpars[v1] = parameters[1][i];
1849 for (q = 0; q < strlen(dmodes); q++)
1851 if (dmodes[q] != parameters[1][i])
1855 strlcat(temp,moo,MAXBUF);
1858 strlcpy(dmodes,temp,MAXBUF);
1871 while (i < strlen (outpars))
1873 b[z++] = outpars[i++];
1875 if (i<strlen(outpars)-1)
1877 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1879 // someones playing silly buggers and trying
1880 // to put a +- or -+ into the line...
1884 if (i == strlen(outpars)-1)
1886 if ((outpars[i] == '-') || (outpars[i] == '+'))
1894 if ((b[z] == '-') || (b[z] == '+'))
1897 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1900 if (strlen(dmodes)>MAXMODES)
1902 dmodes[MAXMODES-1] = '\0';
1904 log(DEBUG,"Stripped mode line");
1905 log(DEBUG,"Line dest is now %s",dmodes);
1906 strlcpy(dest->modes,dmodes,MAXMODES);
1913 Ptr = FindChan(parameters[0]);
1917 strlcpy(s2.nick,ServerName,NICKMAX);
1918 strcpy(s2.modes,"o");
1920 process_modes(parameters,&s2,Ptr,STATUS_OP,pcnt,true,true,false);
1925 void merge_mode2(char **parameters, int pcnt, userrec* user)
1931 char outpars[MAXBUF];
1933 dest = Find(parameters[0]);
1935 // fix: ChroNiCk found this - we cant use this as debug if its null!
1938 log(DEBUG,"merge_mode2 on %s",dest->nick);
1941 if ((dest) && (pcnt > 1))
1943 std::string tidied = compress_modes(parameters[1],false);
1944 parameters[1] = (char*)tidied.c_str();
1946 char dmodes[MAXBUF];
1947 strlcpy(dmodes,dest->modes,MAXBUF);
1949 strcpy(outpars,"+");
1952 if ((parameters[1][0] == ':') && (strlen(parameters[1])>1))
1954 // some stupid 3rd party things (such as services packages) put a colon on the mode list...
1955 log(DEBUG,"Some muppet put a colon on the modelist! changed to '%s'",++parameters[1]);
1957 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1960 for (int i = 0; i < strlen(parameters[1]); i++)
1962 if (parameters[1][i] == ' ')
1964 if (parameters[1][i] == '+')
1968 int t = strlen(outpars)-1;
1969 if ((outpars[t] == '+') || (outpars[t] == '-'))
1975 strcat(outpars,"+");
1981 if (parameters[1][i] == '-')
1985 int t = strlen(outpars)-1;
1986 if ((outpars[t] == '+') || (outpars[t] == '-'))
1992 strcat(outpars,"-");
1999 log(DEBUG,"begin mode processing entry");
2005 log(DEBUG,"umode %c being added",parameters[1][i]);
2006 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
2008 char umode = parameters[1][i];
2009 log(DEBUG,"umode %c is an allowed umode",umode);
2010 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2012 int v1 = strlen(dmodes);
2013 int v2 = strlen(outpars);
2015 dmodes[v1] = parameters[1][i];
2017 outpars[v2] = parameters[1][i];
2018 log(DEBUG,"OUTPARS='%s', DMODES='%s'",outpars,dmodes);
2024 // can only remove a mode they already have
2025 log(DEBUG,"umode %c being removed",parameters[1][i]);
2026 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
2028 char umode = parameters[1][i];
2029 log(DEBUG,"umode %c is an allowed umode",umode);
2030 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2036 int v1 = strlen(outpars);
2038 outpars[v1] = parameters[1][i];
2041 for (q = 0; q < strlen(dmodes); q++)
2043 if (dmodes[q] != parameters[1][i])
2047 strlcat(temp,moo,MAXBUF);
2050 strlcpy(dmodes,temp,MAXBUF);
2057 log(DEBUG,"DONE! OUTPARS='%s', DMODES='%s'",outpars,dmodes);
2064 while (i < strlen (outpars))
2066 b[z++] = outpars[i++];
2068 if (i<strlen(outpars)-1)
2070 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
2072 // someones playing silly buggers and trying
2073 // to put a +- or -+ into the line...
2077 if (i == strlen(outpars)-1)
2079 if ((outpars[i] == '-') || (outpars[i] == '+'))
2087 if ((b[z] == '-') || (b[z] == '+'))
2090 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
2094 WriteTo(user,dest,"MODE %s :%s",dest->nick,b);
2095 log(DEBUG,"Sent: :%s MODE %s",user->nick,b);
2097 if (strlen(dmodes)>MAXMODES)
2099 dmodes[MAXMODES-1] = '\0';
2101 log(DEBUG,"Stripped mode line");
2102 log(DEBUG,"Line dest is now %s",dmodes);
2103 strlcpy(dest->modes,dmodes,MAXMODES);
2110 Ptr = FindChan(parameters[0]);
2113 log(DEBUG,"merge_mode2: found channel %s",Ptr->name);
2116 if ((cstatus(user,Ptr) < STATUS_HOP) && (!is_uline(user->server)))
2120 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,true);