chan.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192
  1. /*
  2. *
  3. * chan.c -- part of irc.mod
  4. * almost everything to do with channel manipulation
  5. * telling channel status
  6. * 'who' response
  7. * user kickban, kick, op, deop
  8. * idle kicking
  9. *
  10. */
  11. static time_t last_ctcp = (time_t) 0L;
  12. static int count_ctcp = 0;
  13. static time_t last_invtime = (time_t) 0L;
  14. static char last_invchan[300] = "";
  15. typedef struct resolvstruct {
  16. struct chanset_t *chan;
  17. char *nick;
  18. } resolv_member;
  19. static void resolv_member_callback(int id, void *client_data, const char *host, char **ips)
  20. {
  21. resolv_member *r = (resolv_member *) client_data;
  22. if (!r || !r->chan || !r->nick)
  23. return;
  24. memberlist *m = NULL;
  25. char *ps = NULL, *pe = NULL, s[UHOSTLEN + 1];
  26. if (ips && ips[0]) {
  27. for (m = r->chan->channel.member; m && m->nick[0]; m = m->next) {
  28. if (!rfc_casecmp(m->nick, r->nick)) {
  29. if (!m->userip[0] && m->userhost[0]) {
  30. ps = m->userhost;
  31. pe = strchr(ps, '@');
  32. if (pe) {
  33. char user[15] = "";
  34. simple_snprintf(user, pe - ps + 1, m->userhost);
  35. simple_snprintf(m->userip, sizeof(m->userip), "%s@%s", user, ips[0]);
  36. if (!m->user) {
  37. simple_snprintf(s, sizeof(s), "%s!%s", m->nick, m->userip);
  38. m->user = get_user_by_host(s);
  39. }
  40. return;
  41. }
  42. }
  43. }
  44. }
  45. }
  46. return;
  47. }
  48. void resolve_to_member(struct chanset_t *chan, char *nick, char *host)
  49. {
  50. resolv_member *r = (resolv_member *) my_calloc(1, sizeof(resolv_member));
  51. r->chan = chan;
  52. r->nick = strdup(nick);
  53. egg_dns_lookup(host, 20, resolv_member_callback, (void *) r);
  54. }
  55. /* ID length for !channels.
  56. */
  57. #define CHANNEL_ID_LEN 5
  58. static void print_memberlist(memberlist *toprint)
  59. {
  60. memberlist *m = NULL;
  61. for (m = toprint; m && m->nick[0]; m = m->next) {
  62. sdprintf("%s!%s user: %s tried: %d hops: %d", m->nick, m->userhost, m->user ? m->user->handle : "", m->tried_getuser, m->hops);
  63. }
  64. }
  65. /* Returns a pointer to a new channel member structure.
  66. */
  67. static memberlist *newmember(struct chanset_t *chan, char *nick)
  68. {
  69. memberlist *x = chan->channel.member,
  70. *lx = NULL,
  71. *n = (memberlist *) my_calloc(1, sizeof(memberlist));
  72. /* This sorts the list */
  73. while (x && x->nick[0] && (rfc_casecmp(x->nick, nick) < 0)) {
  74. lx = x;
  75. x = x->next;
  76. }
  77. n->next = NULL;
  78. strlcpy(n->nick, nick, sizeof(n->nick));
  79. n->split = 0L;
  80. n->last = 0L;
  81. n->delay = 0L;
  82. n->hops = -1;
  83. if (!lx) {
  84. n->next = chan->channel.member;
  85. chan->channel.member = n;
  86. } else {
  87. n->next = lx->next;
  88. lx->next = n;
  89. }
  90. chan->channel.members++;
  91. return n;
  92. }
  93. /* Always pass the channel dname (display name) to this function <cybah>
  94. */
  95. static void update_idle(char *chname, char *nick)
  96. {
  97. struct chanset_t *chan = findchan_by_dname(chname);
  98. if (chan) {
  99. memberlist *m = ismember(chan, nick);
  100. if (m)
  101. m->last = now;
  102. }
  103. }
  104. /* Returns the current channel mode.
  105. */
  106. static char *getchanmode(struct chanset_t *chan)
  107. {
  108. static char s[121] = "";
  109. int atr = chan->channel.mode;
  110. size_t i = 1;
  111. s[0] = '+';
  112. if (atr & CHANINV)
  113. s[i++] = 'i';
  114. if (atr & CHANPRIV)
  115. s[i++] = 'p';
  116. if (atr & CHANSEC)
  117. s[i++] = 's';
  118. if (atr & CHANMODER)
  119. s[i++] = 'm';
  120. if (atr & CHANNOCLR)
  121. s[i++] = 'c';
  122. if (atr & CHANNOCTCP)
  123. s[i++] = 'C';
  124. if (atr & CHANREGON)
  125. s[i++] = 'R';
  126. if (atr & CHANTOPIC)
  127. s[i++] = 't';
  128. if (atr & CHANMODR)
  129. s[i++] = 'M';
  130. if (atr & CHANLONLY)
  131. s[i++] = 'r';
  132. if (atr & CHANNOMSG)
  133. s[i++] = 'n';
  134. if (atr & CHANANON)
  135. s[i++] = 'a';
  136. if (atr & CHANKEY)
  137. s[i++] = 'k';
  138. if (chan->channel.maxmembers != 0)
  139. s[i++] = 'l';
  140. s[i] = 0;
  141. if (chan->channel.key[0])
  142. i += simple_sprintf(s + i, " %s", chan->channel.key);
  143. if (chan->channel.maxmembers != 0)
  144. simple_sprintf(s + i, " %d", chan->channel.maxmembers);
  145. return s;
  146. }
  147. static void check_exemptlist(struct chanset_t *chan, char *from)
  148. {
  149. if (!use_exempts)
  150. return;
  151. bool ok = 0;
  152. for (masklist *e = chan->channel.exempt; e->mask[0]; e = e->next)
  153. if (wild_match(e->mask, from)) {
  154. add_mode(chan, '-', 'e', e->mask);
  155. ok = 1;
  156. }
  157. if (prevent_mixing && ok)
  158. flush_mode(chan, QUICK);
  159. }
  160. void priority_do(struct chanset_t * chan, bool opsonly, int action)
  161. {
  162. if (!me_op(chan))
  163. return;
  164. if (channel_pending(chan) || !shouldjoin(chan) || !channel_active(chan))
  165. return;
  166. memberlist *m = NULL;
  167. int ops = 0, targets = 0, bpos = 0, tpos = 0, ft = 0, ct = 0, actions = 0, sent = 0;
  168. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  169. if (!m->user && !m->tried_getuser) {
  170. char s[256] = "";
  171. simple_sprintf(s, "%s!%s", m->nick, m->userhost);
  172. m->user = get_user_by_host(s);
  173. if (!m->user && doresolv(chan) && m->userip[0]) {
  174. simple_snprintf(s, sizeof(s), "%s!%s", m->nick, m->userip);
  175. m->user = get_user_by_host(s);
  176. }
  177. m->tried_getuser = 1;
  178. }
  179. if (m->user && m->user->bot && (m->user->flags & USER_OP)) {
  180. ops++;
  181. if (!strcmp(m->nick, botname))
  182. bpos = (ops - 1);
  183. } else if (!opsonly || chan_hasop(m)) {
  184. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  185. if (m->user)
  186. get_user_flagrec(m->user, &fr, chan->dname);
  187. if (((glob_deop(fr) && !chan_op(fr)) || chan_deop(fr)) || /* +d */
  188. ((!channel_privchan(chan) && !chan_op(fr) && !glob_op(fr)) || /* simply no +o flag. */
  189. (channel_privchan(chan) && !glob_bot(fr) && !glob_owner(fr) && !chan_op(fr)))) { /* private? */
  190. targets++;
  191. }
  192. }
  193. }
  194. if (!targets || !ops)
  195. return;
  196. ft = (bpos * targets) / ops;
  197. ct = ((bpos + 2) * targets + (ops - 1)) / ops;
  198. ct = (ct - ft + 1);
  199. if (ct > 20)
  200. ct = 20;
  201. while (ft >= targets)
  202. ft -= targets;
  203. actions = 0;
  204. sent = 0;
  205. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  206. if (!opsonly || chan_hasop(m)) {
  207. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  208. if (m->user)
  209. get_user_flagrec(m->user, &fr, chan->dname);
  210. if (((glob_deop(fr) && !chan_op(fr)) || chan_deop(fr)) ||
  211. ((!channel_privchan(chan) && !chan_op(fr) && !glob_op(fr)) ||
  212. (channel_privchan(chan) && !glob_bot(fr) && !glob_owner(fr) && !chan_op(fr)))) {
  213. if (tpos >= ft) {
  214. if ((action == PRIO_DEOP) && !chan_sentdeop(m)) {
  215. actions++;
  216. sent++;
  217. add_mode(chan, '-', 'o', m->nick);
  218. if (actions >= ct) {
  219. flush_mode(chan, QUICK);
  220. return;
  221. }
  222. } else if ((action == PRIO_KICK) && !chan_sentkick(m)) {
  223. actions++;
  224. sent++;
  225. if (chan->closed_ban)
  226. doban(chan, m);
  227. dprintf(DP_MODE, "KICK %s %s :%s%s\n", chan->name, m->nick, kickprefix, response(RES_CLOSED));
  228. m->flags |= SENTKICK;
  229. if (actions >= ct)
  230. return;
  231. }
  232. }
  233. tpos++;
  234. }
  235. }
  236. }
  237. ct = ct - actions;
  238. if (ct > ft)
  239. ct = ft;
  240. ft = 0;
  241. actions = 0;
  242. tpos = 0;
  243. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  244. if (!opsonly || chan_hasop(m)) {
  245. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  246. if (m->user)
  247. get_user_flagrec(m->user, &fr, chan->dname);
  248. if (((glob_deop(fr) && !chan_op(fr)) || chan_deop(fr)) ||
  249. ((!channel_privchan(chan) && !chan_op(fr) && !glob_op(fr)) ||
  250. (channel_privchan(chan) && !glob_bot(fr) && !glob_owner(fr) && !chan_op(fr)))) {
  251. if (tpos >= ft) {
  252. if ((action == PRIO_DEOP) && !chan_sentdeop(m)) {
  253. actions++;
  254. sent++;
  255. add_mode(chan, '-', 'o', m->nick);
  256. if ((actions >= ct) || (sent > 20)) {
  257. flush_mode(chan, QUICK);
  258. return;
  259. }
  260. } else if ((action == PRIO_KICK) && !chan_sentkick(m)) {
  261. actions++;
  262. if (chan->closed_ban)
  263. doban(chan, m);
  264. dprintf(DP_MODE, "KICK %s %s :%s%s\n", chan->name, m->nick, kickprefix, response(RES_CLOSED));
  265. m->flags |= SENTKICK;
  266. if ((actions >= ct) || (sent > 5))
  267. return;
  268. }
  269. }
  270. tpos++;
  271. }
  272. }
  273. }
  274. }
  275. /* lame code
  276. static int target_priority(struct chanset_t * chan, memberlist *target, int opsonly)
  277. {
  278. memberlist *m;
  279. int ops = 0, targets = 0, bpos = 0, ft = 0, ct = 0, tp = (-1), pos = 0;
  280. return 1;
  281. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  282. if (m->user && ((m->user->flags & (USER_BOT | USER_OP)) == (USER_BOT | USER_OP))) {
  283. ops++;
  284. if (match_my_nick(m->nick))
  285. bpos = ops;
  286. } else if (!opsonly || chan_hasop(m)) {
  287. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  288. if (m->user)
  289. get_user_flagrec(m->user, &fr, chan->dname);
  290. if (((glob_deop(fr) && !chan_op(fr)) || chan_deop(fr)) ||
  291. ((!channel_privchan(chan) && !chan_op(fr) && !glob_op(fr)) ||
  292. (channel_privchan(chan) && !glob_bot(fr) && !glob_owner(fr) && !chan_op(fr)))) {
  293. targets++;
  294. }
  295. }
  296. if (m == target)
  297. tp = pos;
  298. pos++;
  299. }
  300. if (!targets || !ops || (tp < 0)) {
  301. return 0;
  302. }
  303. ft = (bpos * targets) / ops;
  304. ct = ((bpos + 2) * targets + (ops - 1)) / ops;
  305. ct = (ct - ft + 1);
  306. if (ct > 20)
  307. ct = 20;
  308. while (ft >= targets) {
  309. ft -= targets;
  310. }
  311. if (ct >= targets) {
  312. putlog(LOG_MISC, "*", "%s 1 ct >= targets; ct %d targets %d", target, ct, targets);
  313. if ((tp >= ft) || (tp <= (ct % targets))) {
  314. putlog(LOG_MISC, "*", "%s (1) first if, tp %d ft %d ct/targets %d", target, tp, ft, (ct % targets));
  315. return 1;
  316. }
  317. } else {
  318. putlog(LOG_MISC, "*", "%s 2 else, ct %d targets %d", target, ct, targets);
  319. if ((tp >= ft) && (tp <= ct)) {
  320. putlog(LOG_MISC, "*", "%s (1) second if, tp %d ft %d", target, tp, ft);
  321. return 1;
  322. }
  323. }
  324. putlog(LOG_MISC, "*", "%s (0) returning 0", target);
  325. return 0;
  326. }
  327. */
  328. /* Check a channel and clean-out any more-specific matching masks.
  329. *
  330. * Moved all do_ban(), do_exempt() and do_invite() into this single function
  331. * as the code bloat is starting to get rediculous <cybah>
  332. */
  333. static void do_mask(struct chanset_t *chan, masklist *m, char *mask, char Mode)
  334. {
  335. for (; m && m->mask[0]; m = m->next)
  336. if (wild_match(mask, m->mask) && rfc_casecmp(mask, m->mask))
  337. add_mode(chan, '-', Mode, m->mask);
  338. add_mode(chan, '+', Mode, mask);
  339. flush_mode(chan, QUICK);
  340. }
  341. /* This is a clone of detect_flood, but works for channel specificity now
  342. * and handles kick & deop as well.
  343. */
  344. static bool detect_chan_flood(char *floodnick, char *floodhost, char *from,
  345. struct chanset_t *chan, int which, char *victim)
  346. {
  347. if (!chan || (which < 0) || (which >= FLOOD_CHAN_MAX))
  348. return 0;
  349. /* Okay, make sure i'm not flood-checking myself */
  350. if (match_my_nick(floodnick))
  351. return 0;
  352. if (!egg_strcasecmp(floodhost, botuserhost))
  353. return 0;
  354. /* My user@host (?) */
  355. /* Do not punish non-existant channel members and IRC services like
  356. * ChanServ
  357. */
  358. memberlist *m = ismember(chan, floodnick);
  359. if (!m && (which != FLOOD_JOIN))
  360. return 0;
  361. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  362. get_user_flagrec(get_user_by_host(from), &fr, chan->dname);
  363. if (glob_bot(fr) ||
  364. ((which == FLOOD_DEOP) &&
  365. (glob_master(fr) || chan_master(fr))) ||
  366. ((which == FLOOD_KICK) &&
  367. (glob_master(fr) || chan_master(fr))) ||
  368. ((which != FLOOD_DEOP) && (which != FLOOD_KICK) &&
  369. (chk_noflood(fr))))
  370. return 0;
  371. char h[UHOSTLEN] = "", ftype[12] = "", *p = NULL;
  372. struct userrec *u = NULL;
  373. int thr = 0;
  374. time_t lapse = 0;
  375. /* Determine how many are necessary to make a flood. */
  376. switch (which) {
  377. case FLOOD_PRIVMSG:
  378. case FLOOD_NOTICE:
  379. thr = chan->flood_pub_thr;
  380. lapse = chan->flood_pub_time;
  381. strcpy(ftype, "pub");
  382. break;
  383. case FLOOD_CTCP:
  384. thr = chan->flood_ctcp_thr;
  385. lapse = chan->flood_ctcp_time;
  386. strcpy(ftype, "pub");
  387. break;
  388. case FLOOD_NICK:
  389. thr = chan->flood_nick_thr;
  390. lapse = chan->flood_nick_time;
  391. strcpy(ftype, "nick");
  392. break;
  393. case FLOOD_JOIN:
  394. thr = chan->flood_join_thr;
  395. lapse = chan->flood_join_time;
  396. strcpy(ftype, "join");
  397. break;
  398. case FLOOD_DEOP:
  399. thr = chan->flood_deop_thr;
  400. lapse = chan->flood_deop_time;
  401. strcpy(ftype, "deop");
  402. break;
  403. case FLOOD_KICK:
  404. thr = chan->flood_kick_thr;
  405. lapse = chan->flood_kick_time;
  406. strcpy(ftype, "kick");
  407. break;
  408. }
  409. if ((thr == 0) || (lapse == 0))
  410. return 0; /* no flood protection */
  411. if ((which == FLOOD_KICK) || (which == FLOOD_DEOP))
  412. p = floodnick;
  413. else {
  414. p = strchr(floodhost, '@');
  415. if (p) {
  416. p++;
  417. }
  418. if (!p)
  419. return 0;
  420. }
  421. if (rfc_casecmp(chan->floodwho[which], p)) { /* new */
  422. strncpy(chan->floodwho[which], p, sizeof(chan->floodwho[which]));
  423. chan->floodwho[which][sizeof(chan->floodwho[which])] = 0;
  424. chan->floodtime[which] = now;
  425. chan->floodnum[which] = 1;
  426. return 0;
  427. }
  428. if (chan->floodtime[which] < now - lapse) {
  429. /* Flood timer expired, reset it */
  430. chan->floodtime[which] = now;
  431. chan->floodnum[which] = 1;
  432. return 0;
  433. }
  434. /* Deop'n the same person, sillyness ;) - so just ignore it */
  435. if (which == FLOOD_DEOP) {
  436. if (!rfc_casecmp(chan->deopd, victim))
  437. return 0;
  438. else
  439. strcpy(chan->deopd, victim);
  440. }
  441. chan->floodnum[which]++;
  442. if (chan->floodnum[which] >= thr) { /* FLOOD */
  443. /* Reset counters */
  444. chan->floodnum[which] = 0;
  445. chan->floodtime[which] = 0;
  446. chan->floodwho[which][0] = 0;
  447. if (which == FLOOD_DEOP)
  448. chan->deopd[0] = 0;
  449. u = get_user_by_host(from);
  450. switch (which) {
  451. case FLOOD_PRIVMSG:
  452. case FLOOD_NOTICE:
  453. case FLOOD_CTCP:
  454. /* Flooding chan! either by public or notice */
  455. if (!chan_sentkick(m) && me_op(chan)) {
  456. putlog(LOG_MODES, chan->dname, "Channel flood from %s -- kicking", floodnick);
  457. dprintf(DP_MODE, "KICK %s %s :%s%s\n", chan->name, floodnick, kickprefix, response(RES_FLOOD));
  458. m->flags |= SENTKICK;
  459. }
  460. return 1;
  461. case FLOOD_JOIN:
  462. case FLOOD_NICK:
  463. if (use_exempts &&
  464. (u_match_mask(global_exempts, from) ||
  465. u_match_mask(chan->exempts, from)))
  466. return 1;
  467. simple_sprintf(h, "*!*@%s", p);
  468. if (!isbanned(chan, h) && me_op(chan)) {
  469. check_exemptlist(chan, from);
  470. do_mask(chan, chan->channel.ban, h, 'b');
  471. }
  472. if ((u_match_mask(global_bans, from))
  473. || (u_match_mask(chan->bans, from)))
  474. return 1; /* Already banned */
  475. if (which == FLOOD_JOIN)
  476. putlog(LOG_MISC | LOG_JOIN, chan->dname, "JOIN flood from @%s! Banning.", p);
  477. else
  478. putlog(LOG_MISC | LOG_JOIN, chan->dname, "NICK flood from @%s! Banning.", p);
  479. strcpy(ftype + 4, " flood");
  480. u_addmask('b', chan, h, conf.bot->nick, ftype, now + (60 * chan->ban_time), 0);
  481. if (!channel_enforcebans(chan) && me_op(chan)) {
  482. char s[UHOSTLEN];
  483. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  484. simple_sprintf(s, "%s!%s", m->nick, m->userhost);
  485. if (wild_match(h, s) &&
  486. (m->joined >= chan->floodtime[which]) &&
  487. !chan_sentkick(m) && !match_my_nick(m->nick) && me_op(chan)) {
  488. m->flags |= SENTKICK;
  489. if (which == FLOOD_JOIN)
  490. dprintf(DP_SERVER, "KICK %s %s :%sjoin flood\n", chan->name, m->nick, kickprefix);
  491. else
  492. dprintf(DP_SERVER, "KICK %s %s :%s%s\n", chan->name, m->nick, kickprefix, response(RES_NICKFLOOD));
  493. }
  494. }
  495. }
  496. return 1;
  497. case FLOOD_KICK:
  498. if (me_op(chan) && !chan_sentkick(m)) {
  499. putlog(LOG_MODES, chan->dname, "Kicking %s, for mass kick.", floodnick);
  500. dprintf(DP_MODE, "KICK %s %s :%s%s\n", chan->name, floodnick, kickprefix, response(RES_KICKFLOOD));
  501. m->flags |= SENTKICK;
  502. }
  503. return 1;
  504. case FLOOD_DEOP:
  505. if (me_op(chan) && !chan_sentkick(m)) {
  506. putlog(LOG_MODES, chan->dname,
  507. "Mass deop on %s by %s", chan->dname, from);
  508. dprintf(DP_MODE, "KICK %s %s :%s%s\n", chan->name, floodnick, kickprefix, response(RES_MASSDEOP));
  509. m->flags |= SENTKICK;
  510. }
  511. if (u) {
  512. char s[256] = "";
  513. simple_sprintf(s, "Mass deop on %s by %s", chan->dname, from);
  514. deflag_user(u, DEFLAG_MDOP, s, chan);
  515. }
  516. return 1;
  517. }
  518. }
  519. return 0;
  520. }
  521. /* Given a chan/m do all necesary exempt checks and ban. */
  522. static void refresh_ban_kick(struct chanset_t *, char *, char *);
  523. static void doban(struct chanset_t *chan, memberlist *m)
  524. {
  525. if (!chan || !m) return;
  526. char s[UHOSTLEN] = "", *s1 = NULL;
  527. simple_sprintf(s, "%s!%s", m->nick, m->userhost);
  528. if (!(use_exempts &&
  529. (u_match_mask(global_exempts,s) ||
  530. u_match_mask(chan->exempts, s)))) {
  531. if (u_match_mask(global_bans, s) || u_match_mask(chan->bans, s))
  532. refresh_ban_kick(chan, s, m->nick);
  533. check_exemptlist(chan, s);
  534. s1 = quickban(chan, m->userhost);
  535. u_addmask('b', chan, s1, conf.bot->nick, "joined closed chan", now + (60 * chan->ban_time), 0);
  536. }
  537. return;
  538. }
  539. /* Given a [nick!]user@host, place a quick ban on them on a chan.
  540. */
  541. static char *quickban(struct chanset_t *chan, char *uhost)
  542. {
  543. static char s1[512] = "";
  544. maskhost(uhost, s1);
  545. /* this used to only happen with strict_host == 0 */
  546. if (strlen(s1) != 1)
  547. s1[2] = '*'; /* arthur2 */
  548. do_mask(chan, chan->channel.ban, s1, 'b');
  549. return s1;
  550. }
  551. /* Kick any user (except friends/masters) with certain mask from channel
  552. * with a specified comment. Ernst 18/3/1998
  553. */
  554. static void kick_all(struct chanset_t *chan, char *hostmask, const char *comment, int bantype)
  555. {
  556. if (!me_op(chan))
  557. return;
  558. int flushed = 0;
  559. char s[UHOSTLEN] = "";
  560. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  561. for (memberlist *m = chan->channel.member; m && m->nick[0]; m = m->next) {
  562. simple_sprintf(s, "%s!%s", m->nick, m->userhost);
  563. get_user_flagrec(m->user ? m->user : get_user_by_host(s), &fr, chan->dname);
  564. if (me_op(chan) &&
  565. (wild_match(hostmask, s) || match_cidr(hostmask, s)) &&
  566. !chan_sentkick(m) &&
  567. !match_my_nick(m->nick) && !chan_issplit(m) &&
  568. !(use_exempts &&
  569. ((bantype && (isexempted(chan, s) || (chan->ircnet_status & CHAN_ASKED_EXEMPTS))) ||
  570. (u_match_mask(global_exempts, s) ||
  571. u_match_mask(chan->exempts, s))))) {
  572. if (!flushed) {
  573. /* We need to kick someone, flush eventual bans first */
  574. flush_mode(chan, QUICK);
  575. flushed += 1;
  576. }
  577. if (!chan_sentkick(m)) {
  578. m->flags |= SENTKICK; /* Mark as pending kick */
  579. dprintf(DP_MODE, "KICK %s %s :%s%s\n", chan->name, m->nick, kickprefix, comment);
  580. }
  581. }
  582. }
  583. }
  584. /* If any bans match this wildcard expression, refresh them on the channel.
  585. */
  586. static void refresh_ban_kick(struct chanset_t *chan, char *user, char *nick)
  587. {
  588. memberlist *m = ismember(chan, nick);
  589. if (!m || chan_sentkick(m))
  590. return;
  591. register maskrec *b = NULL;
  592. /* Check global bans in first cycle and channel bans
  593. in second cycle. */
  594. for (int cycle = 0; cycle < 2; cycle++) {
  595. for (b = cycle ? chan->bans : global_bans; b; b = b->next) {
  596. if (wild_match(b->mask, user) || match_cidr(b->mask, user)) {
  597. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  598. char c[512] = ""; /* The ban comment. */
  599. char s[UHOSTLEN] = "";
  600. simple_sprintf(s, "%s!%s", m->nick, m->userhost);
  601. get_user_flagrec(m->user ? m->user : get_user_by_host(s), &fr,
  602. chan->dname);
  603. if (role == 1)
  604. add_mode(chan, '-', 'o', nick); /* Guess it can't hurt. */
  605. check_exemptlist(chan, user);
  606. do_mask(chan, chan->channel.ban, b->mask, 'b');
  607. b->lastactive = now;
  608. if (b->desc && b->desc[0] != '@')
  609. simple_snprintf(c, sizeof c, "banned: %s", b->desc);
  610. else
  611. c[0] = 0;
  612. if (role == 2)
  613. kick_all(chan, b->mask, c[0] ? (const char *) c : "You are banned", 0);
  614. return; /* Drop out on 1st ban. */
  615. }
  616. }
  617. }
  618. }
  619. /* This is a bit cumbersome at the moment, but it works... Any improvements
  620. * then feel free to have a go.. Jason
  621. */
  622. static void refresh_exempt(struct chanset_t *chan, char *user)
  623. {
  624. maskrec *e = NULL;
  625. masklist *b = NULL;
  626. /* Check global exempts in first cycle and channel exempts
  627. in second cycle. */
  628. for (int cycle = 0; cycle < 2; cycle++) {
  629. for (e = cycle ? chan->exempts : global_exempts; e; e = e->next) {
  630. if (wild_match(user, e->mask) || wild_match(e->mask, user) || match_cidr(e->mask, user)) {
  631. for (b = chan->channel.ban; b && b->mask[0]; b = b->next) {
  632. if (wild_match(b->mask, user) || wild_match(user, b->mask)) {
  633. if (e->lastactive < now - 60 && !isexempted(chan, e->mask)) {
  634. do_mask(chan, chan->channel.exempt, e->mask, 'e');
  635. e->lastactive = now;
  636. }
  637. }
  638. }
  639. }
  640. }
  641. }
  642. }
  643. static void refresh_invite(struct chanset_t *chan, char *user)
  644. {
  645. maskrec *i = NULL;
  646. /* Check global invites in first cycle and channel invites
  647. in second cycle. */
  648. for (int cycle = 0; cycle < 2; cycle++) {
  649. for (i = cycle ? chan->invites : global_invites; i; i = i->next) {
  650. if (wild_match(i->mask, user) &&
  651. ((i->flags & MASKREC_STICKY) || (chan->channel.mode & CHANINV))) {
  652. if (i->lastactive < now - 60 && !isinvited(chan, i->mask)) {
  653. do_mask(chan, chan->channel.invite, i->mask, 'I');
  654. i->lastactive = now;
  655. return;
  656. }
  657. }
  658. }
  659. }
  660. }
  661. /* Enforce all channel bans in a given channel. Ernst 18/3/1998
  662. */
  663. static void enforce_bans(struct chanset_t *chan)
  664. {
  665. if (!me_op(chan))
  666. return; /* Can't do it :( */
  667. if ((chan->ircnet_status & CHAN_ASKED_EXEMPTS))
  668. return;
  669. char me[UHOSTLEN] = "", meip[UHOSTLEN] = "";
  670. simple_sprintf(me, "%s!%s", botname, botuserhost);
  671. simple_sprintf(meip, "%s!%s", botname, botuserip);
  672. /* Go through all bans, kicking the users. */
  673. for (masklist *b = chan->channel.ban; b && b->mask[0]; b = b->next) {
  674. if (!(wild_match(b->mask, me) || match_cidr(b->mask, meip)) && !isexempted(chan, b->mask))
  675. kick_all(chan, b->mask, "You are banned", 1);
  676. }
  677. }
  678. /* Make sure that all who are 'banned' on the userlist are actually in fact
  679. * banned on the channel.
  680. *
  681. * Note: Since i was getting a ban list, i assume i'm chop.
  682. */
  683. static void recheck_bans(struct chanset_t *chan)
  684. {
  685. maskrec *u = NULL;
  686. /* Check global bans in first cycle and channel bans
  687. in second cycle. */
  688. for (int cycle = 0; cycle < 2; cycle++) {
  689. for (u = cycle ? chan->bans : global_bans; u; u = u->next)
  690. if (!isbanned(chan, u->mask) && (!channel_dynamicbans(chan) || (u->flags & MASKREC_STICKY)))
  691. add_mode(chan, '+', 'b', u->mask);
  692. }
  693. }
  694. /* Make sure that all who are exempted on the userlist are actually in fact
  695. * exempted on the channel.
  696. *
  697. * Note: Since i was getting an excempt list, i assume i'm chop.
  698. */
  699. static void recheck_exempts(struct chanset_t *chan)
  700. {
  701. maskrec *e = NULL;
  702. masklist *b = NULL;
  703. /* Check global exempts in first cycle and channel exempts
  704. in second cycle. */
  705. for (int cycle = 0; cycle < 2; cycle++) {
  706. for (e = cycle ? chan->exempts : global_exempts; e; e = e->next) {
  707. if (!isexempted(chan, e->mask) &&
  708. (!channel_dynamicexempts(chan) || (e->flags & MASKREC_STICKY)))
  709. add_mode(chan, '+', 'e', e->mask);
  710. for (b = chan->channel.ban; b && b->mask[0]; b = b->next) {
  711. if ((wild_match(b->mask, e->mask) || wild_match(e->mask, b->mask)) &&
  712. !isexempted(chan, e->mask))
  713. add_mode(chan,'+','e',e->mask);
  714. /* do_mask(chan, chan->channel.exempt, e->mask, 'e');*/
  715. }
  716. }
  717. }
  718. }
  719. /* Make sure that all who are invited on the userlist are actually in fact
  720. * invited on the channel.
  721. *
  722. * Note: Since i was getting an invite list, i assume i'm chop.
  723. */
  724. static void recheck_invites(struct chanset_t *chan)
  725. {
  726. maskrec *ir = NULL;
  727. /* Check global invites in first cycle and channel invites
  728. in second cycle. */
  729. for (int cycle = 0; cycle < 2; cycle++) {
  730. for (ir = cycle ? chan->invites : global_invites; ir; ir = ir->next) {
  731. /* If invite isn't set and (channel is not dynamic invites and not invite
  732. * only) or invite is sticky.
  733. */
  734. if (!isinvited(chan, ir->mask) && ((!channel_dynamicinvites(chan) &&
  735. !(chan->channel.mode & CHANINV)) || ir->flags & MASKREC_STICKY))
  736. add_mode(chan, '+', 'I', ir->mask);
  737. /* do_mask(chan, chan->channel.invite, ir->mask, 'I');*/
  738. }
  739. }
  740. }
  741. /* Resets the masks on the channel.
  742. */
  743. static void resetmasks(struct chanset_t *chan, masklist *m, maskrec *mrec, maskrec *global_masks, char mode)
  744. {
  745. if (!me_op(chan))
  746. return; /* Can't do it */
  747. /* Remove masks we didn't put there */
  748. for (; m && m->mask[0]; m = m->next) {
  749. if (!u_equals_mask(global_masks, m->mask) && !u_equals_mask(mrec, m->mask))
  750. add_mode(chan, '-', mode, m->mask);
  751. }
  752. /* Make sure the intended masks are still there */
  753. switch (mode) {
  754. case 'b':
  755. recheck_bans(chan);
  756. break;
  757. case 'e':
  758. recheck_exempts(chan);
  759. break;
  760. case 'I':
  761. recheck_invites(chan);
  762. break;
  763. default:
  764. putlog(LOG_MISC, "*", "(!) Invalid mode '%c' in resetmasks()", mode);
  765. break;
  766. }
  767. }
  768. void check_this_mask(const char type, struct chanset_t *chan, char *mask, bool sticky)
  769. {
  770. if (type == 'b')
  771. check_this_ban(chan, mask, sticky);
  772. else if (type == 'e')
  773. check_this_exempt(chan, mask, sticky);
  774. else if (type == 'I')
  775. check_this_invite(chan, mask, sticky);
  776. }
  777. void check_this_ban(struct chanset_t *chan, char *banmask, bool sticky)
  778. {
  779. if (!me_op(chan))
  780. return;
  781. char user[UHOSTLEN] = "";
  782. for (memberlist *m = chan->channel.member; m && m->nick[0]; m = m->next) {
  783. simple_sprintf(user, "%s!%s", m->nick, m->userhost);
  784. if (wild_match(banmask, user) &&
  785. !(use_exempts &&
  786. (u_match_mask(global_exempts, user) ||
  787. u_match_mask(chan->exempts, user))))
  788. refresh_ban_kick(chan, user, m->nick);
  789. }
  790. if (!isbanned(chan, banmask) &&
  791. (!channel_dynamicbans(chan) || sticky))
  792. add_mode(chan, '+', 'b', banmask);
  793. }
  794. void check_this_exempt(struct chanset_t *chan, char *mask, bool sticky)
  795. {
  796. if (!isexempted(chan, mask) && (!channel_dynamicexempts(chan) || sticky))
  797. add_mode(chan, '+', 'e', mask);
  798. }
  799. void check_this_invite(struct chanset_t *chan, char *mask, bool sticky)
  800. {
  801. if (!isinvited(chan, mask) && (!channel_dynamicinvites(chan) || sticky))
  802. add_mode(chan, '+', 'I', mask);
  803. }
  804. void recheck_channel_modes(struct chanset_t *chan)
  805. {
  806. int cur = chan->channel.mode,
  807. mns = chan->mode_mns_prot,
  808. pls = chan->mode_pls_prot;
  809. if (channel_closed(chan)) {
  810. if (chan->closed_invite) {
  811. pls |= CHANINV;
  812. mns &= ~CHANINV;
  813. }
  814. if (chan->closed_private) {
  815. pls |= CHANPRIV;
  816. mns &= ~CHANPRIV;
  817. }
  818. }
  819. if (!(chan->status & CHAN_ASKEDMODES)) {
  820. if (pls & CHANINV && !(cur & CHANINV))
  821. add_mode(chan, '+', 'i', "");
  822. else if (mns & CHANINV && cur & CHANINV)
  823. add_mode(chan, '-', 'i', "");
  824. if (pls & CHANPRIV && !(cur & CHANPRIV))
  825. add_mode(chan, '+', 'p', "");
  826. else if (mns & CHANPRIV && cur & CHANPRIV)
  827. add_mode(chan, '-', 'p', "");
  828. if (pls & CHANSEC && !(cur & CHANSEC))
  829. add_mode(chan, '+', 's', "");
  830. else if (mns & CHANSEC && cur & CHANSEC)
  831. add_mode(chan, '-', 's', "");
  832. if (pls & CHANMODER && !(cur & CHANMODER))
  833. add_mode(chan, '+', 'm', "");
  834. else if (mns & CHANMODER && cur & CHANMODER)
  835. add_mode(chan, '-', 'm', "");
  836. if (pls & CHANNOCLR && !(cur & CHANNOCLR))
  837. add_mode(chan, '+', 'c', "");
  838. else if (mns & CHANNOCLR && cur & CHANNOCLR)
  839. add_mode(chan, '-', 'c', "");
  840. if (pls & CHANNOCTCP && !(cur & CHANNOCTCP))
  841. add_mode(chan, '+', 'C', "");
  842. else if (mns & CHANNOCTCP && cur & CHANNOCTCP)
  843. add_mode(chan, '-', 'C', "");
  844. if (pls & CHANREGON && !(cur & CHANREGON))
  845. add_mode(chan, '+', 'R', "");
  846. else if (mns & CHANREGON && cur & CHANREGON)
  847. add_mode(chan, '-', 'R', "");
  848. if (pls & CHANMODR && !(cur & CHANMODR))
  849. add_mode(chan, '+', 'M', "");
  850. else if (mns & CHANMODR && cur & CHANMODR)
  851. add_mode(chan, '-', 'M', "");
  852. if (pls & CHANLONLY && !(cur & CHANLONLY))
  853. add_mode(chan, '+', 'r', "");
  854. else if (mns & CHANLONLY && cur & CHANLONLY)
  855. add_mode(chan, '-', 'r', "");
  856. if (pls & CHANTOPIC && !(cur & CHANTOPIC))
  857. add_mode(chan, '+', 't', "");
  858. else if (mns & CHANTOPIC && cur & CHANTOPIC)
  859. add_mode(chan, '-', 't', "");
  860. if (pls & CHANNOMSG && !(cur & CHANNOMSG))
  861. add_mode(chan, '+', 'n', "");
  862. else if ((mns & CHANNOMSG) && (cur & CHANNOMSG))
  863. add_mode(chan, '-', 'n', "");
  864. if ((pls & CHANANON) && !(cur & CHANANON))
  865. add_mode(chan, '+', 'a', "");
  866. else if ((mns & CHANANON) && (cur & CHANANON))
  867. add_mode(chan, '-', 'a', "");
  868. if ((pls & CHANQUIET) && !(cur & CHANQUIET))
  869. add_mode(chan, '+', 'q', "");
  870. else if ((mns & CHANQUIET) && (cur & CHANQUIET))
  871. add_mode(chan, '-', 'q', "");
  872. if ((chan->limit_prot != 0) && (chan->channel.maxmembers == 0)) {
  873. char s[50] = "";
  874. simple_sprintf(s, "%d", chan->limit_prot);
  875. add_mode(chan, '+', 'l', s);
  876. } else if ((mns & CHANLIMIT) && (chan->channel.maxmembers != 0))
  877. add_mode(chan, '-', 'l', "");
  878. if (chan->key_prot[0]) {
  879. if (rfc_casecmp(chan->channel.key, chan->key_prot) != 0) {
  880. if (chan->channel.key[0])
  881. add_mode(chan, '-', 'k', chan->channel.key);
  882. add_mode(chan, '+', 'k', chan->key_prot);
  883. }
  884. } else if ((mns & CHANKEY) && (chan->channel.key[0]))
  885. add_mode(chan, '-', 'k', chan->channel.key);
  886. }
  887. }
  888. static void check_this_member(struct chanset_t *chan, char *nick, struct flag_record *fr)
  889. {
  890. memberlist *m = ismember(chan, nick);
  891. if (!m || match_my_nick(nick) || !me_op(chan))
  892. return;
  893. char s[UHOSTLEN] = "";
  894. if (me_op(chan)) {
  895. /* +d or bitch and not an op
  896. * we dont check private because +private does not imply bitch. */
  897. if (chan_hasop(m) &&
  898. (chk_deop(*fr, chan) ||
  899. (!loading && userlist && chan_bitch(chan) && !chk_op(*fr, chan)) ) ) {
  900. /* if (target_priority(chan, m, 1)) */
  901. add_mode(chan, '-', 'o', m->nick);
  902. } else if (!chan_hasop(m) && dovoice(chan) && m->user && !u_pass_match(m->user, "-") && chk_autoop(*fr, chan)) {
  903. do_op(m->nick, chan, 1, 0);
  904. }
  905. if (dovoice(chan)) {
  906. if (chan_hasvoice(m) && !chan_hasop(m)) {
  907. /* devoice +q users .. */
  908. if (chk_devoice(*fr))
  909. add_mode(chan, '-', 'v', m->nick);
  910. } else if (!chan_hasvoice(m) && !chan_hasop(m)) {
  911. /* voice +v users */
  912. if (chk_voice(*fr, chan)) {
  913. add_mode(chan, '+', 'v', m->nick);
  914. if (m->flags & EVOICE)
  915. m->flags &= ~EVOICE;
  916. }
  917. }
  918. }
  919. }
  920. simple_sprintf(s, "%s!%s", m->nick, m->userhost);
  921. /* check vs invites */
  922. if (use_invites &&
  923. (u_match_mask(global_invites,s) ||
  924. u_match_mask(chan->invites, s)))
  925. refresh_invite(chan, s);
  926. /* don't kickban if permanent exempted */
  927. if (!(use_exempts &&
  928. (u_match_mask(global_exempts,s) ||
  929. u_match_mask(chan->exempts, s)))) {
  930. if (u_match_mask(global_bans, s) || u_match_mask(chan->bans, s))
  931. refresh_ban_kick(chan, s, m->nick);
  932. /* are they +k ? */
  933. if (!chan_sentkick(m) && (chan_kick(*fr) || glob_kick(*fr)) && me_op(chan)) {
  934. char *p = (char *) get_user(&USERENTRY_COMMENT, m->user);
  935. check_exemptlist(chan, s);
  936. quickban(chan, m->userhost);
  937. dprintf(DP_SERVER, "KICK %s %s :%s%s\n", chan->name, m->nick, bankickprefix, p ? p : response(RES_KICKBAN));
  938. m->flags |= SENTKICK;
  939. }
  940. }
  941. }
  942. void check_this_user(char *hand, int del, char *host)
  943. {
  944. char s[UHOSTLEN] = "";
  945. memberlist *m = NULL;
  946. struct userrec *u = NULL;
  947. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  948. for (struct chanset_t *chan = chanset; chan; chan = chan->next)
  949. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  950. simple_sprintf(s, "%s!%s", m->nick, m->userhost);
  951. u = m->user ? m->user : get_user_by_host(s);
  952. if ((u && !egg_strcasecmp(u->handle, hand) && del < 2) ||
  953. (!u && del == 2 && wild_match(host, s))) {
  954. u = del ? NULL : u;
  955. get_user_flagrec(u, &fr, chan->dname);
  956. check_this_member(chan, m->nick, &fr);
  957. }
  958. }
  959. }
  960. static void enforce_bitch(struct chanset_t *chan) {
  961. if (!chan || !me_op(chan))
  962. return;
  963. priority_do(chan, 1, PRIO_DEOP);
  964. }
  965. void enforce_closed(struct chanset_t *chan) {
  966. if (!chan || !me_op(chan))
  967. return;
  968. char buf[3] = "";
  969. if (chan->closed_invite && !(chan->channel.mode & CHANINV))
  970. strcat(buf, "i");
  971. if (chan->closed_private && !(chan->channel.mode & CHANPRIV))
  972. strcat(buf, "p");
  973. if (buf && buf[0])
  974. dprintf(DP_MODE, "MODE %s +%s\n", chan->name, buf);
  975. priority_do(chan, 0, PRIO_KICK);
  976. }
  977. static char *
  978. take_massopline(char *op, char **to_op)
  979. {
  980. char *nicks = (char *) my_calloc(1, 151),
  981. *modes = (char *) my_calloc(1, 31),
  982. *ret = (char *) my_calloc(1, 182),
  983. *nick = NULL;
  984. register bool useop = 0;
  985. for (unsigned int i = 0; i < modesperline; i++) {
  986. if (*to_op[0] || op) {
  987. /* if 'op' then use it, then move on to to_op */
  988. if (!useop && op) {
  989. nick = op;
  990. useop = 1;
  991. } else if (*to_op[0])
  992. nick = newsplit(to_op);
  993. if (nick) {
  994. strcat(modes, "+o");
  995. strcat(nicks, nick);
  996. strcat(nicks, " ");
  997. }
  998. }
  999. }
  1000. strcat(ret, modes);
  1001. strcat(ret, " ");
  1002. strcat(ret, nicks);
  1003. free(modes);
  1004. free(nicks);
  1005. return ret;
  1006. }
  1007. inline static char *
  1008. take_makeline(char *op, char *deops, unsigned int deopn)
  1009. {
  1010. bool opn = op ? 1 : 0;
  1011. unsigned int n = opn + deopn; /* op + deops */
  1012. unsigned int pos = randint(deopn), i;
  1013. char *ret = (char *) my_calloc(1, 151);
  1014. for (i = 0; i < n; i++) {
  1015. if (opn && i == pos)
  1016. strcat(ret, "+o");
  1017. else if (deopn)
  1018. strcat(ret, "-o");
  1019. }
  1020. strcat(ret, " ");
  1021. for (i = 0; i < n; i++) {
  1022. if (opn && i == pos)
  1023. strcat(ret, op);
  1024. else if (deopn)
  1025. strcat(ret, newsplit(&deops));
  1026. strcat(ret, " ");
  1027. }
  1028. return ret;
  1029. }
  1030. static void
  1031. do_take(struct chanset_t *chan)
  1032. {
  1033. char work[1024] = "", *op, *modeline, deops[512] = "";;
  1034. char *to_op = (char *) my_calloc(1, 2048), *to_op_ptr = to_op;
  1035. char *to_deop = (char *) my_calloc(1, 2048), *to_deop_ptr = to_deop;
  1036. register bool hasop, isbot;
  1037. register unsigned int lines_max = 5, lines = 0, deopn, i;
  1038. if (floodless)
  1039. lines_max = 15;
  1040. for (memberlist *m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1041. hasop = (m->flags & CHANOP);
  1042. isbot = 0;
  1043. if (m->user && m->user->bot)
  1044. isbot = 1;
  1045. if (rfc_casecmp(m->nick, botname)) {
  1046. if (isbot && !hasop) {
  1047. strcat(to_op, m->nick);
  1048. strcat(to_op, " ");
  1049. } else if (!isbot && hasop) {
  1050. strcat(to_deop, m->nick);
  1051. strcat(to_deop, " ");
  1052. }
  1053. }
  1054. }
  1055. shuffle(to_op, " ");
  1056. shuffle(to_deop, " ");
  1057. /*
  1058. putlog(LOG_MISC, "*", "op: %s", to_op);
  1059. putlog(LOG_MISC, "*", "deop: %s", to_deop);
  1060. */
  1061. while (to_op[0] || to_deop[0]) {
  1062. deopn = 0;
  1063. op = NULL;
  1064. modeline = NULL;
  1065. deops[0] = 0;
  1066. if (to_op[0])
  1067. op = newsplit(&to_op);
  1068. for (i = 0; i < modesperline; i++) {
  1069. if (to_deop[0] && ((i < (modesperline - 1)) || (!op))) {
  1070. deopn++;
  1071. strcat(deops, newsplit(&to_deop));
  1072. strcat(deops, " ");
  1073. }
  1074. }
  1075. strcat(work, "MODE ");
  1076. strcat(work, chan->name);
  1077. strcat(work, " ");
  1078. if (deops[0])
  1079. modeline = take_makeline(op, deops, deopn);
  1080. else
  1081. modeline = take_massopline(op, &to_op);
  1082. strcat(work, modeline);
  1083. strcat(work, "\r\n");
  1084. lines++;
  1085. free(modeline);
  1086. /* just dump when we have 5 lines because the server is going to throttle after that anyway */
  1087. if (lines == lines_max) {
  1088. tputs(serv, work, strlen(work));
  1089. work[0] = 0;
  1090. lines = 0;
  1091. }
  1092. }
  1093. if (work[0])
  1094. tputs(serv, work, strlen(work));
  1095. free(to_op_ptr);
  1096. free(to_deop_ptr);
  1097. if (channel_closed(chan))
  1098. enforce_closed(chan);
  1099. enforce_bitch(chan); /* hell why not! */
  1100. return;
  1101. }
  1102. /* Things to do when i just became a chanop:
  1103. */
  1104. void recheck_channel(struct chanset_t *chan, int dobans)
  1105. {
  1106. static int stacking = 0;
  1107. if (stacking)
  1108. return; /* wewps */
  1109. if (!userlist || loading) /* Bot doesnt know anybody */
  1110. return; /* ... it's better not to deop everybody */
  1111. memberlist *m = NULL;
  1112. char s[UHOSTLEN] = "";
  1113. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  1114. int stop_reset = 0, botops = 0, nonbotops = 0, botnonops = 0;
  1115. stacking++;
  1116. putlog(LOG_DEBUG, "*", "recheck_channel %s", chan->dname);
  1117. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1118. bool hasop = chan_hasop(m);
  1119. if (m) {
  1120. if (m->user && m->user->bot && (m->user->flags & USER_OP)) {
  1121. if (hasop)
  1122. botops++;
  1123. else
  1124. botnonops++;
  1125. } else if (hasop)
  1126. nonbotops++;
  1127. }
  1128. }
  1129. /* don't do much if i'm lonely bot. Maybe they won't notice me? :P */
  1130. if (botops == 1 && !botnonops) {
  1131. if (chan_bitch(chan) || channel_closed(chan))
  1132. putlog(LOG_MISC, "*", "Opped in %s, not checking +closed/+bitch until more bots arrive.", chan->dname);
  1133. } else {
  1134. /* if the chan is +closed, mass deop first, safer that way. */
  1135. if (chan_bitch(chan) || channel_closed(chan))
  1136. enforce_bitch(chan);
  1137. if (channel_closed(chan))
  1138. enforce_closed(chan);
  1139. }
  1140. /* this can all die, we want to enforce +bitch/+take first :) */
  1141. /* This is a bad hack for +e/+I */
  1142. if (dobans == 2 && !channel_take(chan) && me_op(chan) && do_eI) {
  1143. last_eI = now;
  1144. if (!(chan->ircnet_status & CHAN_ASKED_EXEMPTS) && use_exempts == 1) {
  1145. chan->ircnet_status |= CHAN_ASKED_EXEMPTS;
  1146. dprintf(DP_MODE, "MODE %s +e\n", chan->name);
  1147. }
  1148. if (!(chan->ircnet_status & CHAN_ASKED_INVITES) && use_invites == 1) {
  1149. chan->ircnet_status |= CHAN_ASKED_INVITES;
  1150. dprintf(DP_MODE, "MODE %s +I\n", chan->name);
  1151. }
  1152. }
  1153. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1154. simple_sprintf(s, "%s!%s", m->nick, m->userhost);
  1155. if (!m->user && !m->tried_getuser) {
  1156. m->tried_getuser = 1;
  1157. m->user = get_user_by_host(s);
  1158. if (!m->user && doresolv(chan) && m->userip[0]) {
  1159. simple_snprintf(s, sizeof(s), "%s!%s", m->nick, m->userip);
  1160. m->user = get_user_by_host(s);
  1161. }
  1162. }
  1163. get_user_flagrec(m->user, &fr, chan->dname);
  1164. if (glob_bot(fr) && chan_hasop(m) && !match_my_nick(m->nick))
  1165. stop_reset = 1;
  1166. check_this_member(chan, m->nick, &fr);
  1167. }
  1168. if (dobans) {
  1169. if (channel_nouserbans(chan) && !stop_reset)
  1170. resetbans(chan);
  1171. else
  1172. recheck_bans(chan);
  1173. if (use_invites && !(chan->ircnet_status & CHAN_ASKED_INVITES)) {
  1174. if (channel_nouserinvites(chan) && !stop_reset)
  1175. resetinvites(chan);
  1176. else
  1177. recheck_invites(chan);
  1178. }
  1179. if (use_exempts && !(chan->ircnet_status & CHAN_ASKED_EXEMPTS)) {
  1180. if (channel_nouserexempts(chan) && !stop_reset)
  1181. resetexempts(chan);
  1182. else
  1183. recheck_exempts(chan);
  1184. } else {
  1185. if (channel_enforcebans(chan))
  1186. enforce_bans(chan);
  1187. }
  1188. flush_mode(chan, QUICK);
  1189. if ((chan->status & CHAN_ASKEDMODES) && !channel_inactive(chan))
  1190. dprintf(DP_MODE, "MODE %s\n", chan->name);
  1191. recheck_channel_modes(chan);
  1192. }
  1193. stacking--;
  1194. }
  1195. /* got 302: userhost
  1196. * <server> 302 <to> :<nick??user@host>
  1197. */
  1198. static int got302(char *from, char *msg)
  1199. {
  1200. char *p = NULL, *nick = NULL, *uhost = NULL;
  1201. #ifdef CACHE
  1202. cache_t *cache = NULL;
  1203. cache_chan_t *cchan = NULL;
  1204. #endif
  1205. newsplit(&msg);
  1206. fixcolon(msg);
  1207. p = strchr(msg, '=');
  1208. if (!p)
  1209. p = strchr(msg, '*');
  1210. if (!p)
  1211. return 0;
  1212. *p = 0;
  1213. nick = msg;
  1214. p += 2; /* skip =|* plus the next char */
  1215. uhost = p;
  1216. if ((p = strchr(uhost, ' ')))
  1217. *p = 0;
  1218. if (match_my_nick(nick)) {
  1219. strlcpy(botuserip, uhost, UHOSTLEN);
  1220. sdprintf("botuserip: %s", botuserip);
  1221. return 0;
  1222. }
  1223. #ifdef CACHE
  1224. if ((cache = cache_find(nick))) {
  1225. if (!cache->uhost[0])
  1226. strcpy(cache->uhost, uhost);
  1227. if (!cache->handle[0]) {
  1228. char s[UHOSTLEN] = "";
  1229. struct userrec *u = NULL;
  1230. simple_sprintf(s, "%s!%s", nick, uhost);
  1231. if ((u = get_user_by_host(s)))
  1232. strcpy(cache->handle, u->handle);
  1233. }
  1234. cache->timeval = now;
  1235. /* check if we should invite this client to chans */
  1236. for (cchan = cache->cchan; cchan && cchan->dname[0]; cchan = cchan->next) {
  1237. if (cchan->invite) {
  1238. dprintf(DP_SERVER, "INVITE %s %s\n", nick, cchan->dname);
  1239. cchan->invite = 0;
  1240. cchan->invited = 1;
  1241. }
  1242. if (cchan->ban) {
  1243. cchan->ban = 0;
  1244. dprintf(DP_DUMP, "MODE %s +b *!%s\n", cchan->dname, uhost);
  1245. }
  1246. }
  1247. }
  1248. #endif
  1249. return 0;
  1250. }
  1251. #ifdef CACHE
  1252. /* got341 invited
  1253. * <server> 341 <to> <nick> <channel>
  1254. */
  1255. static int got341(char *from, char *msg)
  1256. {
  1257. char *nick = NULL, *chname = NULL;
  1258. cache_t *cache = NULL;
  1259. cache_chan_t *cchan = NULL;
  1260. newsplit(&msg);
  1261. nick = newsplit(&msg);
  1262. chname = newsplit(&msg);
  1263. struct chanset_t *chan = findchan(chname);
  1264. if (!chan) {
  1265. putlog(LOG_MISC, "*", "%s: %s", "Hmm, mode info from a channel I'm not on", chname);
  1266. dprintf(DP_SERVER, "PART %s\n", chname);
  1267. return 0;
  1268. }
  1269. cache = cache_find(nick);
  1270. if (cache) {
  1271. for (cchan = cache->cchan; cchan && cchan->dname; cchan = cchan->next) {
  1272. if (!rfc_casecmp(cchan->dname, chan->dname)) {
  1273. if (!cache->uhost[0] || !cchan->invited)
  1274. goto hijack;
  1275. cache->timeval = now;
  1276. notice_invite(chan, cache->handle[0] ? cache->handle : NULL, nick, cache->uhost, cchan->op);
  1277. // cache_del_chan_parm(cache, cache->cchan);
  1278. break;
  1279. }
  1280. }
  1281. }
  1282. if (!cache || !cchan)
  1283. goto hijack;
  1284. return 0;
  1285. hijack:
  1286. if (!cache)
  1287. cache = cache_new(nick);
  1288. if (!cchan)
  1289. cchan = cache_chan_add(cache, chan->dname);
  1290. if (!cache->uhost[0]) {
  1291. dprintf(DP_DUMP, "MODE %s +b %s!*@*\n", chan->name, nick);
  1292. cchan->ban = 1;
  1293. dprintf(DP_DUMP, "USERHOST %s\n", nick);
  1294. } else {
  1295. dprintf(DP_DUMP, "MODE %s +b *!*%s\n", chan->name, cache->uhost);
  1296. }
  1297. putlog(LOG_MISC, "*", "HIJACKED invite detected: %s to %s", nick, chan->dname);
  1298. dprintf(DP_DUMP, "PRIVMSG %s :ALERT! \002%s was invited via a hijacked connection/process.\002\n", chan->name, nick);
  1299. return 0;
  1300. }
  1301. #endif /* CACHE */
  1302. /* got 324: mode status
  1303. * <server> 324 <to> <channel> <mode>
  1304. */
  1305. static int got324(char *from, char *msg)
  1306. {
  1307. char *chname = NULL;
  1308. newsplit(&msg);
  1309. chname = newsplit(&msg);
  1310. if (match_my_nick(chname))
  1311. return 0;
  1312. struct chanset_t *chan = findchan(chname);
  1313. if (!chan) {
  1314. putlog(LOG_MISC, "*", "%s: %s", "Hmm, mode info from a channel I'm not on", chname);
  1315. dprintf(DP_SERVER, "PART %s\n", chname);
  1316. return 0;
  1317. }
  1318. int i = 1;
  1319. bool ok = 0;
  1320. char *p = NULL, *q = NULL;
  1321. if (chan->status & CHAN_ASKEDMODES)
  1322. ok = 1;
  1323. chan->status &= ~CHAN_ASKEDMODES;
  1324. chan->channel.mode = 0;
  1325. while (msg[i] != 0) {
  1326. if (msg[i] == 'i')
  1327. chan->channel.mode |= CHANINV;
  1328. if (msg[i] == 'p')
  1329. chan->channel.mode |= CHANPRIV;
  1330. if (msg[i] == 's')
  1331. chan->channel.mode |= CHANSEC;
  1332. if (msg[i] == 'm')
  1333. chan->channel.mode |= CHANMODER;
  1334. if (msg[i] == 'c')
  1335. chan->channel.mode |= CHANNOCLR;
  1336. if (msg[i] == 'C')
  1337. chan->channel.mode |= CHANNOCTCP;
  1338. if (msg[i] == 'R')
  1339. chan->channel.mode |= CHANREGON;
  1340. if (msg[i] == 'M')
  1341. chan->channel.mode |= CHANMODR;
  1342. if (msg[i] == 'r')
  1343. chan->channel.mode |= CHANLONLY;
  1344. if (msg[i] == 't')
  1345. chan->channel.mode |= CHANTOPIC;
  1346. if (msg[i] == 'n')
  1347. chan->channel.mode |= CHANNOMSG;
  1348. if (msg[i] == 'a')
  1349. chan->channel.mode |= CHANANON;
  1350. if (msg[i] == 'q')
  1351. chan->channel.mode |= CHANQUIET;
  1352. if (msg[i] == 'k') {
  1353. chan->channel.mode |= CHANKEY;
  1354. p = strchr(msg, ' ');
  1355. if (p != NULL) { /* Test for null key assignment */
  1356. p++;
  1357. q = strchr(p, ' ');
  1358. if (q != NULL) {
  1359. *q = 0;
  1360. my_setkey(chan, p);
  1361. strcpy(p, q + 1);
  1362. } else {
  1363. my_setkey(chan, p);
  1364. *p = 0;
  1365. }
  1366. }
  1367. if ((chan->channel.mode & CHANKEY) && (!chan->channel.key[0] ||
  1368. !strcmp("*", chan->channel.key)))
  1369. /* Undernet use to show a blank channel key if one was set when
  1370. * you first joined a channel; however, this has been replaced by
  1371. * an asterisk and this has been agreed upon by other major IRC
  1372. * networks so we'll check for an asterisk here as well
  1373. * (guppy 22Dec2001) */
  1374. chan->status |= CHAN_ASKEDMODES;
  1375. }
  1376. if (msg[i] == 'l') {
  1377. p = strchr(msg, ' ');
  1378. if (p != NULL) { /* test for null limit assignment */
  1379. p++;
  1380. q = strchr(p, ' ');
  1381. if (q != NULL) {
  1382. *q = 0;
  1383. chan->channel.maxmembers = atoi(p);
  1384. /* strcpy(p, q + 1); */
  1385. simple_sprintf(p, "%s", q + 1);
  1386. } else {
  1387. chan->channel.maxmembers = atoi(p);
  1388. *p = 0;
  1389. }
  1390. }
  1391. }
  1392. i++;
  1393. }
  1394. if (ok)
  1395. recheck_channel_modes(chan);
  1396. return 0;
  1397. }
  1398. static void memberlist_reposition(struct chanset_t *chan, memberlist *target) {
  1399. /* Move target from it's current position to it's correct sorted position */
  1400. memberlist *old = NULL, *m = NULL;
  1401. if (chan->channel.member == target) {
  1402. chan->channel.member = target->next;
  1403. } else {
  1404. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1405. if (m->next == target) {
  1406. m->next = target->next;
  1407. break;
  1408. }
  1409. }
  1410. }
  1411. target->next = NULL;
  1412. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1413. if (rfc_casecmp(m->nick, target->nick)>0) {
  1414. if (old) {
  1415. target->next = m;
  1416. old->next = target;
  1417. } else {
  1418. target->next = chan->channel.member;
  1419. chan->channel.member = target;
  1420. }
  1421. return;
  1422. }
  1423. old = m;
  1424. }
  1425. if (old) {
  1426. target->next = old->next;
  1427. old->next = target;
  1428. } else {
  1429. target->next = NULL;
  1430. chan->channel.member = target;
  1431. }
  1432. }
  1433. static int got352or4(struct chanset_t *chan, char *user, char *host, char *nick, char *flags, int hops)
  1434. {
  1435. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  1436. char userhost[UHOSTLEN] = "";
  1437. memberlist *m = ismember(chan, nick); /* in my channel list copy? */
  1438. bool waschanop = 0, me = 0;
  1439. // struct chanset_t *ch = NULL;
  1440. // memberlist *ml = NULL;
  1441. me = match_my_nick(nick);
  1442. if (!m) { /* Nope, so update */
  1443. m = newmember(chan, nick); /* Get a new channel entry */
  1444. m->joined = m->split = m->delay = 0L; /* Don't know when he joined */
  1445. m->flags = 0; /* No flags for now */
  1446. m->last = now; /* Last time I saw him */
  1447. m->user = NULL;
  1448. }
  1449. if (!m->nick[0])
  1450. strcpy(m->nick, nick); /* Store the nick in list */
  1451. m->hops = hops;
  1452. /* Propagate hops to other channel memlists... might save us a WHO #chan */
  1453. /* FIXME: great concept, HORRIBLE CPU USAGE
  1454. for (ch = chanset; ch; ch = ch->next) {
  1455. if (ch != chan) {
  1456. for (ml = ch->channel.member; ml && ml->nick[0]; ml = ml->next) {
  1457. if (!strcmp(ml->nick, m->nick)) {
  1458. ml->hops = m->hops;
  1459. break;
  1460. }
  1461. }
  1462. }
  1463. }
  1464. */
  1465. waschanop = me_op(chan); /* Am I opped here? */
  1466. if (strchr(flags, '@') != NULL) /* Flags say he's opped? */
  1467. m->flags |= (CHANOP | WASOP); /* Yes, so flag in my table */
  1468. else
  1469. m->flags &= ~(CHANOP | WASOP);
  1470. if (strchr(flags, '*'))
  1471. m->flags |= OPER;
  1472. else
  1473. m->flags &= ~OPER;
  1474. if (strchr(flags, '+') != NULL) /* Flags say he's voiced? */
  1475. m->flags |= CHANVOICE; /* Yes */
  1476. else
  1477. m->flags &= ~CHANVOICE;
  1478. if (!(m->flags & (CHANVOICE | CHANOP)))
  1479. m->flags |= STOPWHO;
  1480. /* Store the userhost */
  1481. if (!m->userhost[0])
  1482. simple_snprintf(m->userhost, sizeof(m->userhost), "%s@%s", user, host);
  1483. simple_snprintf(userhost, sizeof(userhost), "%s!%s", nick, m->userhost);
  1484. if (me) { /* Is it me? */
  1485. // strcpy(botuserhost, m->userhost); /* Yes, save my own userhost */
  1486. m->joined = now; /* set this to keep the whining masses happy */
  1487. if (!waschanop && me_op(chan))
  1488. recheck_channel(chan, 2);
  1489. if (!me_op(chan) && any_ops(chan))
  1490. chan->channel.do_opreq = 1;
  1491. }
  1492. if (!m->user && !m->tried_getuser) {
  1493. m->user = get_user_by_host(userhost);
  1494. m->tried_getuser = 1;
  1495. }
  1496. //userhost failed, let's try resolving them...
  1497. if (!m->user && doresolv(chan)) {
  1498. if (is_dotted_ip(host))
  1499. simple_snprintf(m->userip, sizeof(m->userip), "%s@%s", user, host);
  1500. else
  1501. resolve_to_member(chan, nick, host);
  1502. }
  1503. get_user_flagrec(m->user, &fr, chan->dname);
  1504. if (me_op(chan)) {
  1505. /* are they a chanop, and me too */
  1506. /* are they a channel or global de-op */
  1507. if (chan_hasop(m) && chk_deop(fr, chan) && !me)
  1508. /* && target_priority(chan, m, 1) */
  1509. add_mode(chan, '-', 'o', nick);
  1510. /* if channel is enforce bans */
  1511. if (channel_enforcebans(chan) &&
  1512. !chan_sentkick(m) &&
  1513. /* and user matches a ban */
  1514. (u_match_mask(global_bans, userhost) || u_match_mask(chan->bans, userhost)) &&
  1515. /* and it's not me, and i'm an op */
  1516. !me) {
  1517. /* && target_priority(chan, m, 0) */
  1518. dprintf(DP_SERVER, "KICK %s %s :%s%s\n", chan->name, nick, bankickprefix, r_banned());
  1519. m->flags |= SENTKICK;
  1520. }
  1521. /* if the user is a +k */
  1522. else if ((chan_kick(fr) || glob_kick(fr)) &&
  1523. !chan_sentkick(m) &&
  1524. /* and it's not me :) who'd set me +k anyway, a sicko? */
  1525. /* and if im an op */
  1526. !me) {
  1527. /* && target_priority(chan, m, 0) */
  1528. /* cya later! */
  1529. quickban(chan, userhost);
  1530. dprintf(DP_SERVER, "KICK %s %s :%s%s\n", chan->name, nick, bankickprefix, response(RES_KICKBAN));
  1531. m->flags |= SENTKICK;
  1532. }
  1533. }
  1534. return 0;
  1535. }
  1536. /* got a 352: who info!
  1537. */
  1538. static int got352(char *from, char *msg)
  1539. {
  1540. char *chname = NULL;
  1541. struct chanset_t *chan = NULL;
  1542. newsplit(&msg); /* Skip my nick - effeciently */
  1543. chname = newsplit(&msg); /* Grab the channel */
  1544. chan = findchan(chname); /* See if I'm on channel */
  1545. if (chan) { /* Am I? */
  1546. char *nick = NULL, *user = NULL, *host = NULL, *flags = NULL, *hops = NULL;
  1547. user = newsplit(&msg); /* Grab the user */
  1548. host = newsplit(&msg); /* Grab the host */
  1549. newsplit(&msg); /* skip the server */
  1550. nick = newsplit(&msg); /* Grab the nick */
  1551. flags = newsplit(&msg); /* Grab the flags */
  1552. hops = newsplit(&msg); /* grab server hops */
  1553. hops++;
  1554. got352or4(chan, user, host, nick, flags, atoi(hops));
  1555. }
  1556. return 0;
  1557. }
  1558. /* got a 354: who info! - iru style
  1559. */
  1560. static int got354(char *from, char *msg)
  1561. {
  1562. if (use_354) {
  1563. newsplit(&msg); /* Skip my nick - effeciently */
  1564. if (msg[0] && (strchr(CHANMETA, msg[0]) != NULL)) {
  1565. char *chname = newsplit(&msg); /* Grab the channel */
  1566. struct chanset_t *chan = findchan(chname); /* See if I'm on channel */
  1567. if (chan) { /* Am I? */
  1568. char *nick = NULL, *user = NULL, *host = NULL, *flags = NULL, *hops = NULL;
  1569. user = newsplit(&msg); /* Grab the user */
  1570. host = newsplit(&msg); /* Grab the host */
  1571. nick = newsplit(&msg); /* Grab the nick */
  1572. flags = newsplit(&msg); /* Grab the flags */
  1573. hops = newsplit(&msg); /* yay for hops, does iru even have hops?? */
  1574. hops++;
  1575. got352or4(chan, user, host, nick, flags, atoi(hops));
  1576. }
  1577. }
  1578. }
  1579. return 0;
  1580. }
  1581. /* got 315: end of who
  1582. * <server> 315 <to> <chan> :End of /who
  1583. */
  1584. static int got315(char *from, char *msg)
  1585. {
  1586. char *chname = NULL;
  1587. struct chanset_t *chan = NULL;
  1588. newsplit(&msg);
  1589. chname = newsplit(&msg);
  1590. chan = findchan(chname);
  1591. /* May have left the channel before the who info came in */
  1592. if (!chan || !channel_pending(chan))
  1593. return 0;
  1594. /* Finished getting who list, can now be considered officially ON CHANNEL */
  1595. chan->status |= CHAN_ACTIVE;
  1596. chan->status &= ~(CHAN_PEND | CHAN_JOINING);
  1597. /* Am *I* on the channel now? if not, well d0h. */
  1598. if (shouldjoin(chan) && !ismember(chan, botname)) {
  1599. putlog(LOG_MISC | LOG_JOIN, chan->dname, "Oops, I'm not really on %s.", chan->dname);
  1600. clear_channel(chan, 1);
  1601. chan->status &= ~CHAN_ACTIVE;
  1602. chan->status |= CHAN_JOINING;
  1603. dprintf(DP_MODE, "JOIN %s %s\n",
  1604. (chan->name[0]) ? chan->name : chan->dname,
  1605. chan->channel.key[0] ? chan->channel.key : chan->key_prot);
  1606. } else {
  1607. if (me_op(chan))
  1608. recheck_channel(chan, 2);
  1609. else if (chan->channel.members == 1)
  1610. chan->status |= CHAN_STOP_CYCLE;
  1611. else
  1612. request_op(chan);
  1613. }
  1614. /* do not check for i-lines here. */
  1615. return 0;
  1616. }
  1617. /* got 367: ban info
  1618. * <server> 367 <to> <chan> <ban> [placed-by] [timestamp]
  1619. */
  1620. static int got367(char *from, char *origmsg)
  1621. {
  1622. char *chname = NULL, buf[511] = "", *msg = NULL;
  1623. struct chanset_t *chan = NULL;
  1624. strlcpy(buf, origmsg, sizeof(buf));
  1625. msg = buf;
  1626. newsplit(&msg);
  1627. chname = newsplit(&msg);
  1628. chan = findchan(chname);
  1629. if (!chan || !(channel_pending(chan) || channel_active(chan)))
  1630. return 0;
  1631. char *ban = newsplit(&msg), *who = newsplit(&msg);
  1632. /* Extended timestamp format? */
  1633. if (who[0])
  1634. newban(chan, ban, who);
  1635. else
  1636. newban(chan, ban, "existent");
  1637. return 0;
  1638. }
  1639. /* got 368: end of ban list
  1640. * <server> 368 <to> <chan> :etc
  1641. */
  1642. static int got368(char *from, char *msg)
  1643. {
  1644. struct chanset_t *chan = NULL;
  1645. char *chname = NULL;
  1646. /* Okay, now add bans that i want, which aren't set yet */
  1647. newsplit(&msg);
  1648. chname = newsplit(&msg);
  1649. chan = findchan(chname);
  1650. if (chan)
  1651. chan->status &= ~CHAN_ASKEDBANS;
  1652. /* If i sent a mode -b on myself (deban) in got367, either
  1653. * resetbans() or recheck_bans() will flush that.
  1654. */
  1655. return 0;
  1656. }
  1657. /* got 348: ban exemption info
  1658. * <server> 348 <to> <chan> <exemption>
  1659. */
  1660. static int got348(char *from, char *origmsg)
  1661. {
  1662. if (use_exempts == 0)
  1663. return 0;
  1664. char *chname = NULL, buf[511] = "", *msg = NULL;
  1665. struct chanset_t *chan = NULL;
  1666. strlcpy(buf, origmsg, sizeof(buf));
  1667. msg = buf;
  1668. newsplit(&msg);
  1669. chname = newsplit(&msg);
  1670. chan = findchan(chname);
  1671. if (!chan || !(channel_pending(chan) || channel_active(chan)))
  1672. return 0;
  1673. char *exempt = newsplit(&msg), *who = newsplit(&msg);
  1674. /* Extended timestamp format? */
  1675. if (who[0])
  1676. newexempt(chan, exempt, who);
  1677. else
  1678. newexempt(chan, exempt, "existent");
  1679. return 0;
  1680. }
  1681. /* got 349: end of ban exemption list
  1682. * <server> 349 <to> <chan> :etc
  1683. */
  1684. static int got349(char *from, char *msg)
  1685. {
  1686. if (use_exempts == 1) {
  1687. struct chanset_t *chan = NULL;
  1688. char *chname = NULL;
  1689. newsplit(&msg);
  1690. chname = newsplit(&msg);
  1691. chan = findchan(chname);
  1692. if (chan) {
  1693. putlog(LOG_DEBUG, "*", "END +e %s", chan->dname);
  1694. chan->ircnet_status &= ~CHAN_ASKED_EXEMPTS;
  1695. if (channel_nouserexempts(chan))
  1696. resetexempts(chan);
  1697. else
  1698. recheck_exempts(chan);
  1699. if (channel_enforcebans(chan))
  1700. enforce_bans(chan);
  1701. }
  1702. }
  1703. return 0;
  1704. }
  1705. static void got353(char *from, char *msg)
  1706. {
  1707. char *chname = NULL;
  1708. struct chanset_t *chan = NULL;
  1709. newsplit(&msg); /* my nick */
  1710. newsplit(&msg); /* *|@|= */
  1711. chname = newsplit(&msg);
  1712. chan = findchan(chname);
  1713. fixcolon(msg);
  1714. irc_log(chan, "%s", msg);
  1715. }
  1716. /* got 346: invite exemption info
  1717. * <server> 346 <to> <chan> <exemption>
  1718. */
  1719. static int got346(char *from, char *origmsg)
  1720. {
  1721. if (use_invites == 0)
  1722. return 0;
  1723. char *chname = NULL, buf[511] = "", *msg = NULL;
  1724. struct chanset_t *chan = NULL;
  1725. strlcpy(buf, origmsg, sizeof(buf));
  1726. msg = buf;
  1727. newsplit(&msg);
  1728. chname = newsplit(&msg);
  1729. chan = findchan(chname);
  1730. if (!chan || !(channel_pending(chan) || channel_active(chan)))
  1731. return 0;
  1732. char *invite = newsplit(&msg), *who = newsplit(&msg);
  1733. /* Extended timestamp format? */
  1734. if (who[0])
  1735. newinvite(chan, invite, who);
  1736. else
  1737. newinvite(chan, invite, "existent");
  1738. return 0;
  1739. }
  1740. /* got 347: end of invite exemption list
  1741. * <server> 347 <to> <chan> :etc
  1742. */
  1743. static int got347(char *from, char *msg)
  1744. {
  1745. if (use_invites == 1) {
  1746. struct chanset_t *chan = NULL;
  1747. char *chname = NULL;
  1748. newsplit(&msg);
  1749. chname = newsplit(&msg);
  1750. chan = findchan(chname);
  1751. if (chan) {
  1752. chan->ircnet_status &= ~CHAN_ASKED_INVITES;
  1753. if (channel_nouserinvites(chan))
  1754. resetinvites(chan);
  1755. else
  1756. recheck_invites(chan);
  1757. }
  1758. }
  1759. return 0;
  1760. }
  1761. /* Too many channels.
  1762. */
  1763. static int got405(char *from, char *msg)
  1764. {
  1765. char *chname = NULL;
  1766. newsplit(&msg);
  1767. chname = newsplit(&msg);
  1768. putlog(LOG_MISC, "*", "I'm on too many channels--can't join: %s", chname);
  1769. return 0;
  1770. }
  1771. /* This is only of use to us with !channels. We get this message when
  1772. * attempting to join a non-existant !channel... The channel must be
  1773. * created by sending 'JOIN !!<channel>'. <cybah>
  1774. *
  1775. * 403 - ERR_NOSUCHCHANNEL
  1776. */
  1777. static int got403(char *from, char *msg)
  1778. {
  1779. char *chname = NULL;
  1780. struct chanset_t *chan = NULL;
  1781. newsplit(&msg);
  1782. chname = newsplit(&msg);
  1783. if (chname && chname[0]=='!') {
  1784. chan = findchan_by_dname(chname);
  1785. if (!chan) {
  1786. chan = findchan(chname);
  1787. if (!chan)
  1788. return 0; /* Ignore it */
  1789. /* We have the channel unique name, so we have attempted to join
  1790. * a specific !channel that doesnt exist. Now attempt to join the
  1791. * channel using it's short name.
  1792. */
  1793. putlog(LOG_MISC, "*",
  1794. "Unique channel %s does not exist... Attempting to join with "
  1795. "short name.", chname);
  1796. dprintf(DP_SERVER, "JOIN %s\n", chan->dname);
  1797. chan->status |= CHAN_JOINING;
  1798. } else {
  1799. /* We have found the channel, so the server has given us the short
  1800. * name. Prefix another '!' to it, and attempt the join again...
  1801. */
  1802. putlog(LOG_MISC, "*",
  1803. "Channel %s does not exist... Attempting to create it.", chname);
  1804. dprintf(DP_SERVER, "JOIN !%s\n", chan->dname);
  1805. }
  1806. }
  1807. return 0;
  1808. }
  1809. /* got 471: can't join channel, full
  1810. */
  1811. static int got471(char *from, char *msg)
  1812. {
  1813. char *chname = NULL;
  1814. struct chanset_t *chan = NULL;
  1815. newsplit(&msg);
  1816. chname = newsplit(&msg);
  1817. /* !channel short names (also referred to as 'description names'
  1818. * can be received by skipping over the unique ID.
  1819. */
  1820. if ((chname[0] == '!') && (strlen(chname) > CHANNEL_ID_LEN)) {
  1821. chname += CHANNEL_ID_LEN;
  1822. chname[0] = '!';
  1823. }
  1824. /* We use dname because name is first set on JOIN and we might not
  1825. * have joined the channel yet.
  1826. */
  1827. chan = findchan_by_dname(chname);
  1828. if (chan) {
  1829. chan->status &= ~CHAN_JOINING;
  1830. putlog(LOG_JOIN, chan->dname, "Channel full--can't join: %s", chan->dname);
  1831. request_in(chan);
  1832. /* need: limit */
  1833. chan = findchan_by_dname(chname);
  1834. if (!chan)
  1835. return 0;
  1836. } else
  1837. putlog(LOG_JOIN, chname, "Channel full--can't join: %s", chname);
  1838. return 0;
  1839. }
  1840. /* got 473: can't join channel, invite only
  1841. */
  1842. static int got473(char *from, char *msg)
  1843. {
  1844. char *chname = NULL;
  1845. struct chanset_t *chan = NULL;
  1846. newsplit(&msg);
  1847. chname = newsplit(&msg);
  1848. /* !channel short names (also referred to as 'description names'
  1849. * can be received by skipping over the unique ID.
  1850. */
  1851. if ((chname[0] == '!') && (strlen(chname) > CHANNEL_ID_LEN)) {
  1852. chname += CHANNEL_ID_LEN;
  1853. chname[0] = '!';
  1854. }
  1855. /* We use dname because name is first set on JOIN and we might not
  1856. * have joined the channel yet.
  1857. */
  1858. chan = findchan_by_dname(chname);
  1859. if (chan) {
  1860. chan->status &= ~CHAN_JOINING;
  1861. putlog(LOG_JOIN, chan->dname, "Channel invite only--can't join: %s", chan->dname);
  1862. request_in(chan);
  1863. /* need: invite */
  1864. chan = findchan_by_dname(chname);
  1865. if (!chan)
  1866. return 0;
  1867. } else
  1868. putlog(LOG_JOIN, chname, "Channel invite only--can't join: %s", chname);
  1869. return 0;
  1870. }
  1871. /* got 474: can't join channel, banned
  1872. */
  1873. static int got474(char *from, char *msg)
  1874. {
  1875. char *chname = NULL;
  1876. struct chanset_t *chan = NULL;
  1877. newsplit(&msg);
  1878. chname = newsplit(&msg);
  1879. /* !channel short names (also referred to as 'description names'
  1880. * can be received by skipping over the unique ID.
  1881. */
  1882. if ((chname[0] == '!') && (strlen(chname) > CHANNEL_ID_LEN)) {
  1883. chname += CHANNEL_ID_LEN;
  1884. chname[0] = '!';
  1885. }
  1886. /* We use dname because name is first set on JOIN and we might not
  1887. * have joined the channel yet.
  1888. */
  1889. chan = findchan_by_dname(chname);
  1890. if (chan) {
  1891. chan->status &= ~CHAN_JOINING;
  1892. putlog(LOG_JOIN, chan->dname, "Banned from channel--can't join: %s", chan->dname);
  1893. request_in(chan);
  1894. /* need: unban */
  1895. chan = findchan_by_dname(chname);
  1896. if (!chan)
  1897. return 0;
  1898. } else
  1899. putlog(LOG_JOIN, chname, "Banned from channel--can't join: %s", chname);
  1900. return 0;
  1901. }
  1902. /* got 475: can't join channel, bad key
  1903. */
  1904. static int got475(char *from, char *msg)
  1905. {
  1906. char *chname = NULL;
  1907. struct chanset_t *chan = NULL;
  1908. newsplit(&msg);
  1909. chname = newsplit(&msg);
  1910. /* !channel short names (also referred to as 'description names'
  1911. * can be received by skipping over the unique ID.
  1912. */
  1913. if ((chname[0] == '!') && (strlen(chname) > CHANNEL_ID_LEN)) {
  1914. chname += CHANNEL_ID_LEN;
  1915. chname[0] = '!';
  1916. }
  1917. /* We use dname because name is first set on JOIN and we might not
  1918. * have joined the channel yet.
  1919. */
  1920. chan = findchan_by_dname(chname);
  1921. if (chan && shouldjoin(chan)) {
  1922. chan->status &= ~CHAN_JOINING;
  1923. putlog(LOG_JOIN, chan->dname, "Bad key--can't join: %s", chan->dname);
  1924. if (chan->channel.key[0]) {
  1925. free(chan->channel.key);
  1926. chan->channel.key = (char *) my_calloc(1, 1);
  1927. dprintf(DP_MODE, "JOIN %s %s\n", chan->dname, chan->key_prot);
  1928. } else {
  1929. request_in(chan);
  1930. /* need: key */
  1931. }
  1932. } else
  1933. putlog(LOG_JOIN, chname, "Bad key--can't join: %s", chname);
  1934. return 0;
  1935. }
  1936. /* got invitation
  1937. */
  1938. static int gotinvite(char *from, char *msg)
  1939. {
  1940. char *nick = NULL;
  1941. struct chanset_t *chan = NULL;
  1942. newsplit(&msg);
  1943. fixcolon(msg);
  1944. nick = splitnick(&from);
  1945. if (!rfc_casecmp(last_invchan, msg))
  1946. if (now - last_invtime < 30)
  1947. return 0; /* Two invites to the same channel in 30 seconds? */
  1948. putlog(LOG_MISC, "*", "%s!%s invited me to %s", nick, from, msg);
  1949. strncpy(last_invchan, msg, 299);
  1950. last_invchan[299] = 0;
  1951. last_invtime = now;
  1952. chan = findchan(msg);
  1953. if (!chan)
  1954. /* Might be a short-name */
  1955. chan = findchan_by_dname(msg);
  1956. else {
  1957. if (channel_pending(chan) || channel_active(chan))
  1958. dprintf(DP_HELP, "NOTICE %s :I'm already here.\n", nick);
  1959. else if (shouldjoin(chan))
  1960. dprintf(DP_MODE, "JOIN %s %s\n", (chan->name[0]) ? chan->name : chan->dname,
  1961. chan->channel.key[0] ? chan->channel.key : chan->key_prot);
  1962. }
  1963. return 0;
  1964. }
  1965. /* Set the topic.
  1966. */
  1967. static void set_topic(struct chanset_t *chan, char *k)
  1968. {
  1969. if (chan->channel.topic)
  1970. free(chan->channel.topic);
  1971. if (k && k[0]) {
  1972. chan->channel.topic = (char *) my_calloc(1, strlen(k) + 1);
  1973. strcpy(chan->channel.topic, k);
  1974. } else
  1975. chan->channel.topic = NULL;
  1976. }
  1977. /* Topic change.
  1978. */
  1979. static int gottopic(char *from, char *msg)
  1980. {
  1981. char *chname = newsplit(&msg), *nick = NULL;
  1982. struct chanset_t *chan = NULL;
  1983. struct userrec *u = NULL;
  1984. fixcolon(msg);
  1985. u = get_user_by_host(from);
  1986. nick = splitnick(&from);
  1987. chan = findchan(chname);
  1988. if (chan) {
  1989. memberlist *m = ismember(chan, nick);
  1990. irc_log(chan, "%s!%s changed topic to: %s", nick, from, msg);
  1991. if (m != NULL)
  1992. m->last = now;
  1993. set_topic(chan, msg);
  1994. }
  1995. return 0;
  1996. }
  1997. /* 331: no current topic for this channel
  1998. * <server> 331 <to> <chname> :etc
  1999. */
  2000. static int got331(char *from, char *msg)
  2001. {
  2002. char *chname = NULL;
  2003. struct chanset_t *chan = NULL;
  2004. newsplit(&msg);
  2005. chname = newsplit(&msg);
  2006. chan = findchan(chname);
  2007. if (chan) {
  2008. set_topic(chan, NULL);
  2009. }
  2010. return 0;
  2011. }
  2012. /* 332: topic on a channel i've just joined
  2013. * <server> 332 <to> <chname> :topic goes here
  2014. */
  2015. static int got332(char *from, char *msg)
  2016. {
  2017. struct chanset_t *chan = NULL;
  2018. char *chname = NULL;
  2019. newsplit(&msg);
  2020. chname = newsplit(&msg);
  2021. chan = findchan(chname);
  2022. if (chan) {
  2023. fixcolon(msg);
  2024. set_topic(chan, msg);
  2025. }
  2026. return 0;
  2027. }
  2028. /* Got a join
  2029. */
  2030. static int gotjoin(char *from, char *chname)
  2031. {
  2032. char *nick = NULL, *p = NULL, buf[UHOSTLEN] = "", *uhost = buf;
  2033. char *ch_dname = NULL;
  2034. struct chanset_t *chan = NULL;
  2035. memberlist *m = NULL;
  2036. masklist *b = NULL;
  2037. struct userrec *u = NULL;
  2038. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  2039. fixcolon(chname);
  2040. chan = findchan(chname);
  2041. if (!chan && chname[0] == '!') {
  2042. /* As this is a !channel, we need to search for it by display (short)
  2043. * name now. This will happen when we initially join the channel, as we
  2044. * dont know the unique channel name that the server has made up. <cybah>
  2045. */
  2046. int l_chname = strlen(chname);
  2047. if (l_chname > (CHANNEL_ID_LEN + 1)) {
  2048. ch_dname = (char *) my_calloc(1, l_chname + 1);
  2049. if (ch_dname) {
  2050. simple_snprintf(ch_dname, l_chname + 2, "!%s",
  2051. chname + (CHANNEL_ID_LEN + 1));
  2052. chan = findchan_by_dname(ch_dname);
  2053. if (!chan) {
  2054. /* Hmm.. okay. Maybe the admin's a genius and doesn't know the
  2055. * difference between id and descriptive channel names. Search
  2056. * the channel name in the dname list using the id-name.
  2057. */
  2058. chan = findchan_by_dname(chname);
  2059. if (chan) {
  2060. /* Duh, I was right. Mark this channel as inactive and log
  2061. * the incident.
  2062. */
  2063. chan->status |= CHAN_INACTIVE;
  2064. putlog(LOG_MISC, "*", "Deactivated channel %s, because it uses "
  2065. "an ID channel-name. Use the descriptive name instead.",
  2066. chname);
  2067. dprintf(DP_SERVER, "PART %s\n", chname);
  2068. goto exit;
  2069. }
  2070. }
  2071. }
  2072. }
  2073. } else if (!chan) {
  2074. /* As this is not a !chan, we need to search for it by display name now.
  2075. * Unlike !chan's, we dont need to remove the unique part.
  2076. */
  2077. chan = findchan_by_dname(chname);
  2078. }
  2079. if (!chan || (chan && !shouldjoin(chan))) {
  2080. strcpy(uhost, from);
  2081. nick = splitnick(&uhost);
  2082. if (match_my_nick(nick)) {
  2083. putlog(LOG_WARN, "*", "joined %s but didn't want to!", chname);
  2084. dprintf(DP_MODE, "PART %s\n", chname);
  2085. }
  2086. } else if (!channel_pending(chan)) {
  2087. char *host = NULL;
  2088. chan->status &= ~CHAN_STOP_CYCLE;
  2089. strcpy(uhost, from);
  2090. nick = splitnick(&uhost);
  2091. detect_chan_flood(nick, uhost, from, chan, FLOOD_JOIN, NULL);
  2092. if ((host = strchr(uhost, '@')))
  2093. host++;
  2094. chan = findchan(chname);
  2095. if (!chan) {
  2096. if (ch_dname)
  2097. chan = findchan_by_dname(ch_dname);
  2098. else
  2099. chan = findchan_by_dname(chname);
  2100. }
  2101. if (!chan)
  2102. /* The channel doesn't exist anymore, so get out of here. */
  2103. goto exit;
  2104. /* Grab last time joined before we update it */
  2105. u = get_user_by_host(from);
  2106. get_user_flagrec(u, &fr, chan->dname); /* Lam: fix to work with !channels */
  2107. if (!channel_active(chan) && !match_my_nick(nick)) {
  2108. /* uh, what?! i'm on the channel?! */
  2109. putlog(LOG_ERROR, "*", "confused bot: guess I'm on %s and didn't realize it", chan->dname);
  2110. chan->status |= CHAN_ACTIVE;
  2111. chan->status &= ~(CHAN_PEND | CHAN_JOINING);
  2112. reset_chan_info(chan);
  2113. } else {
  2114. int splitjoin = 0;
  2115. m = ismember(chan, nick);
  2116. if (m && m->split && !egg_strcasecmp(m->userhost, uhost)) {
  2117. chan = findchan(chname);
  2118. if (!chan) {
  2119. if (ch_dname)
  2120. chan = findchan_by_dname(ch_dname);
  2121. else
  2122. chan = findchan_by_dname(chname);
  2123. }
  2124. if (!chan)
  2125. /* The channel doesn't exist anymore, so get out of here. */
  2126. goto exit;
  2127. /* The tcl binding might have deleted the current user. Recheck. */
  2128. if (!m->user) {
  2129. m->user = get_user_by_host(from);
  2130. m->tried_getuser = 1;
  2131. if (!m->user && doresolv(chan)) {
  2132. if (is_dotted_ip(host))
  2133. strlcpy(m->userip, uhost, sizeof(m->userip));
  2134. else
  2135. resolve_to_member(chan, nick, host);
  2136. }
  2137. }
  2138. splitjoin++;
  2139. m->split = 0;
  2140. m->last = now;
  2141. m->delay = 0L;
  2142. m->flags = (chan_hasop(m) ? WASOP : 0);
  2143. set_handle_laston(chan->dname, m->user, now);
  2144. m->flags |= STOPWHO;
  2145. irc_log(chan, "%s returned from netsplit", m->nick);
  2146. } else {
  2147. if (m)
  2148. killmember(chan, nick);
  2149. m = newmember(chan, nick);
  2150. m->joined = now;
  2151. m->split = 0L;
  2152. m->flags = 0;
  2153. m->last = now;
  2154. m->delay = 0L;
  2155. strlcpy(m->nick, nick, sizeof(m->nick));
  2156. strlcpy(m->userhost, uhost, sizeof(m->userhost));
  2157. m->user = u;
  2158. m->tried_getuser = 1;
  2159. if (!m->user && doresolv(chan)) {
  2160. if (is_dotted_ip(host))
  2161. strlcpy(m->userip, uhost, sizeof(m->userip));
  2162. else
  2163. resolve_to_member(chan, nick, host);
  2164. }
  2165. m->flags |= STOPWHO;
  2166. /* The tcl binding might have deleted the current user and the
  2167. * current channel, so we'll now have to re-check whether they
  2168. * both still exist.
  2169. */
  2170. chan = findchan(chname);
  2171. if (!chan) {
  2172. if (ch_dname)
  2173. chan = findchan_by_dname(ch_dname);
  2174. else
  2175. chan = findchan_by_dname(chname);
  2176. }
  2177. if (!chan)
  2178. /* The channel doesn't exist anymore, so get out of here. */
  2179. goto exit;
  2180. /* The record saved in the channel record always gets updated,
  2181. so we can use that. */
  2182. u = m->user;
  2183. if (match_my_nick(nick)) {
  2184. /* It was me joining! Need to update the channel record with the
  2185. * unique name for the channel (as the server see's it). <cybah>
  2186. */
  2187. strlcpy(chan->name, chname, 81);
  2188. chan->status &= ~CHAN_JUPED;
  2189. /* ... and log us joining. Using chan->dname for the channel is
  2190. * important in this case. As the config file will never contain
  2191. * logs with the unique name.
  2192. */
  2193. if (chname[0] == '!')
  2194. irc_log(chan, "Joined. (%s)", chname);
  2195. else
  2196. irc_log(chan, "Joined.");
  2197. if (!match_my_nick(chname))
  2198. reset_chan_info(chan);
  2199. } else {
  2200. irc_log(chan, "Join: %s (%s)", nick, uhost);
  2201. set_handle_laston(chan->dname, u, now);
  2202. }
  2203. }
  2204. /* ok, the op-on-join,etc, tests...first only both if Im opped */
  2205. if (me_op(chan)) {
  2206. /* Check for and reset exempts and invites.
  2207. *
  2208. * This will require further checking to account for when to use the
  2209. * various modes.
  2210. */
  2211. if (u_match_mask(global_invites,from) ||
  2212. u_match_mask(chan->invites, from))
  2213. refresh_invite(chan, from);
  2214. if (!(use_exempts && (u_match_mask(global_exempts,from) || u_match_mask(chan->exempts, from)))) {
  2215. if (channel_enforcebans(chan) && !chan_op(fr) && !glob_op(fr) && !chan_sentkick(m) &&
  2216. !(use_exempts && (isexempted(chan, from) || (chan->ircnet_status & CHAN_ASKED_EXEMPTS))) &&
  2217. me_op(chan)) {
  2218. for (b = chan->channel.ban; b->mask[0]; b = b->next) {
  2219. if (wild_match(b->mask, from) || match_cidr(b->mask, from)) {
  2220. dprintf(DP_SERVER, "KICK %s %s :%s%s\n", chname, m->nick, bankickprefix, r_banned());
  2221. m->flags |= SENTKICK;
  2222. goto exit;
  2223. }
  2224. }
  2225. }
  2226. /* If it matches a ban, dispose of them. */
  2227. if (u_match_mask(global_bans, from) || u_match_mask(chan->bans, from)) {
  2228. refresh_ban_kick(chan, from, nick);
  2229. /* Likewise for kick'ees */
  2230. } else if (!chan_sentkick(m) && (glob_kick(fr) || chan_kick(fr)) &&
  2231. me_op(chan)) {
  2232. check_exemptlist(chan, from);
  2233. quickban(chan, from);
  2234. p = (char *) get_user(&USERENTRY_COMMENT, m->user);
  2235. dprintf(DP_MODE, "KICK %s %s :%s%s\n", chname, nick, bankickprefix, response(RES_KICKBAN));
  2236. m->flags |= SENTKICK;
  2237. }
  2238. }
  2239. bool op = 0;
  2240. #ifdef CACHE
  2241. cache_t *cache = cache_find(nick);
  2242. if (cache) {
  2243. cache_chan_t *cchan = NULL;
  2244. if (egg_strcasecmp(cache->uhost, m->userhost)) {
  2245. }
  2246. for (cchan = cache->cchan; cchan && cchan->dname[0]; cchan = cchan->next) {
  2247. if (!rfc_casecmp(cchan->dname, chan->dname)) {
  2248. if (cchan->op) {
  2249. op = 1;
  2250. cchan->op = 0;
  2251. }
  2252. break;
  2253. }
  2254. }
  2255. }
  2256. #endif /* CACHE */
  2257. if (!splitjoin && !chan_hasop(m) && (op || (dovoice(chan) &&
  2258. !u_pass_match(m->user, "-") && chk_autoop(fr, chan)))) {
  2259. do_op(m->nick, chan, 1, 0);
  2260. }
  2261. }
  2262. }
  2263. }
  2264. exit:
  2265. if (ch_dname)
  2266. free(ch_dname);
  2267. return 0;
  2268. }
  2269. /* Got a part
  2270. */
  2271. static int gotpart(char *from, char *msg)
  2272. {
  2273. char *nick = NULL, *chname = NULL;
  2274. struct chanset_t *chan = NULL;
  2275. struct userrec *u = NULL;
  2276. chname = newsplit(&msg);
  2277. fixcolon(chname);
  2278. fixcolon(msg);
  2279. chan = findchan(chname);
  2280. u = get_user_by_host(from);
  2281. nick = splitnick(&from);
  2282. if (chan && !shouldjoin(chan) && match_my_nick(nick)) {
  2283. irc_log(chan, "Parting");
  2284. clear_channel(chan, 1);
  2285. chan->status &= ~(CHAN_ACTIVE | CHAN_PEND | CHAN_JOINING);
  2286. return 0;
  2287. }
  2288. if (chan && !channel_pending(chan)) {
  2289. if (!channel_active(chan)) {
  2290. /* whoa! */
  2291. putlog(LOG_ERRORS, "*", "confused bot: guess I'm on %s and didn't realize it", chan->dname);
  2292. chan->status |= CHAN_ACTIVE;
  2293. chan->status &= ~(CHAN_PEND | CHAN_JOINING);
  2294. reset_chan_info(chan);
  2295. }
  2296. set_handle_laston(chan->dname, u, now);
  2297. chan = findchan(chname);
  2298. if (!chan)
  2299. return 0;
  2300. killmember(chan, nick);
  2301. if (msg[0])
  2302. irc_log(chan, "Part: %s (%s) [%s]", nick, from, msg);
  2303. else
  2304. irc_log(chan, "Part: %s (%s)", nick, from);
  2305. /* If it was me, all hell breaks loose... */
  2306. if (match_my_nick(nick)) {
  2307. clear_channel(chan, 1);
  2308. chan->status &= ~(CHAN_ACTIVE | CHAN_PEND | CHAN_JOINING);
  2309. if (shouldjoin(chan)) {
  2310. dprintf(DP_MODE, "JOIN %s %s\n",
  2311. (chan->name[0]) ? chan->name : chan->dname,
  2312. chan->channel.key[0] ? chan->channel.key : chan->key_prot);
  2313. chan->status |= CHAN_JOINING;
  2314. }
  2315. } else
  2316. check_lonely_channel(chan);
  2317. }
  2318. return 0;
  2319. }
  2320. /* Got a kick
  2321. */
  2322. static int gotkick(char *from, char *origmsg)
  2323. {
  2324. char buf2[511], *msg = buf2, *chname = NULL;
  2325. struct chanset_t *chan = NULL;
  2326. strlcpy(buf2, origmsg, sizeof(buf2));
  2327. msg = buf2;
  2328. chname = newsplit(&msg);
  2329. chan = findchan(chname);
  2330. if (!chan)
  2331. return 0;
  2332. char *nick = newsplit(&msg);
  2333. if (match_my_nick(nick) && channel_pending(chan) && shouldjoin(chan) && !channel_joining(chan)) {
  2334. chan->status &= ~(CHAN_ACTIVE | CHAN_PEND);
  2335. chan->status |= CHAN_JOINING;
  2336. dprintf(DP_MODE, "JOIN %s %s\n",
  2337. (chan->name[0]) ? chan->name : chan->dname,
  2338. chan->channel.key[0] ? chan->channel.key : chan->key_prot);
  2339. clear_channel(chan, 1);
  2340. return 0; /* rejoin if kicked before getting needed info <Wcc[08/08/02]> */
  2341. }
  2342. if (channel_active(chan)) {
  2343. char *whodid = NULL, s1[UHOSTLEN] = "", buf[UHOSTLEN] = "", *uhost = buf;
  2344. memberlist *m = NULL;
  2345. struct userrec *u = NULL;
  2346. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  2347. chan->channel.fighting++;
  2348. fixcolon(msg);
  2349. u = get_user_by_host(from);
  2350. strcpy(uhost, from);
  2351. whodid = splitnick(&uhost);
  2352. detect_chan_flood(whodid, uhost, from, chan, FLOOD_KICK, nick);
  2353. chan = findchan(chname);
  2354. if (!chan)
  2355. return 0;
  2356. m = ismember(chan, whodid);
  2357. if (m)
  2358. m->last = now;
  2359. /* This _needs_ to use chan->dname <cybah> */
  2360. get_user_flagrec(u, &fr, chan->dname);
  2361. set_handle_laston(chan->dname, u, now);
  2362. chan = findchan(chname);
  2363. if (!chan)
  2364. return 0;
  2365. if ((m = ismember(chan, nick))) {
  2366. struct userrec *u2 = NULL;
  2367. simple_sprintf(s1, "%s!%s", m->nick, m->userhost);
  2368. u2 = get_user_by_host(s1);
  2369. set_handle_laston(chan->dname, u2, now);
  2370. // maybe_revenge(chan, from, s1, REVENGE_KICK);
  2371. } else {
  2372. simple_sprintf(s1, "%s!*@could.not.loookup.hostname", nick);
  2373. }
  2374. irc_log(chan, "%s was kicked by %s (%s)", s1, from, msg);
  2375. /* Kicked ME?!? the sods! */
  2376. if (match_my_nick(nick) && shouldjoin(chan) && !channel_joining(chan)) {
  2377. chan->status &= ~(CHAN_ACTIVE | CHAN_PEND);
  2378. dprintf(DP_MODE, "JOIN %s %s\n",
  2379. (chan->name[0]) ? chan->name : chan->dname,
  2380. chan->channel.key[0] ? chan->channel.key : chan->key_prot);
  2381. chan->status |= CHAN_JOINING;
  2382. clear_channel(chan, 1);
  2383. } else {
  2384. killmember(chan, nick);
  2385. check_lonely_channel(chan);
  2386. }
  2387. }
  2388. return 0;
  2389. }
  2390. /* Got a nick change
  2391. */
  2392. static int gotnick(char *from, char *msg)
  2393. {
  2394. char *nick = NULL, *chname = NULL, s1[UHOSTLEN] = "", buf[UHOSTLEN] = "", *uhost = buf;
  2395. memberlist *m = NULL, *mm = NULL;
  2396. struct chanset_t *oldchan = NULL;
  2397. struct userrec *u = NULL;
  2398. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  2399. strcpy(uhost, from);
  2400. nick = splitnick(&uhost);
  2401. fixcolon(msg);
  2402. irc_log(NULL, "[%s] Nick change: %s -> %s", samechans(nick, ","), nick, msg);
  2403. clear_chanlist_member(nick); /* Cache for nick 'nick' is meaningless now. */
  2404. Auth *auth = Auth::Find(uhost);
  2405. if (auth)
  2406. auth->NewNick(msg);
  2407. for (struct chanset_t *chan = chanset; chan; chan = chan->next) {
  2408. oldchan = chan;
  2409. chname = chan->dname;
  2410. m = ismember(chan, nick);
  2411. if (m) {
  2412. m->last = now;
  2413. /* Not just a capitalization change */
  2414. if (rfc_casecmp(nick, msg)) {
  2415. /* Someone on channel with old nick?! */
  2416. if ((mm = ismember(chan, msg)))
  2417. killmember(chan, mm->nick);
  2418. }
  2419. /*
  2420. * Banned?
  2421. */
  2422. /* Compose a nick!user@host for the new nick */
  2423. simple_sprintf(s1, "%s!%s", msg, uhost);
  2424. strcpy(m->nick, msg);
  2425. memberlist_reposition(chan, m);
  2426. detect_chan_flood(msg, uhost, from, chan, FLOOD_NICK, NULL);
  2427. if (!findchan_by_dname(chname)) {
  2428. chan = oldchan;
  2429. continue;
  2430. }
  2431. /* don't fill the serverqueue with modes or kicks in a nickflood */
  2432. if (chan_sentkick(m) || chan_sentdeop(m) || chan_sentop(m) ||
  2433. chan_sentdevoice(m) || chan_sentvoice(m))
  2434. m->flags |= STOPCHECK;
  2435. /* Any pending kick or mode to the old nick is lost. */
  2436. m->flags &= ~(SENTKICK | SENTDEOP | SENTOP |
  2437. SENTVOICE | SENTDEVOICE);
  2438. /* make sure they stay devoiced if EVOICE! */
  2439. /* nick-ban or nick is +k or something? */
  2440. if (!chan_stopcheck(m)) {
  2441. get_user_flagrec(m->user ? m->user : get_user_by_host(s1), &fr, chan->dname);
  2442. check_this_member(chan, m->nick, &fr);
  2443. }
  2444. u = get_user_by_host(from); /* make sure this is in the loop, someone could have changed the record
  2445. in an earlier iteration of the loop */
  2446. if (!findchan_by_dname(chname)) {
  2447. chan = oldchan;
  2448. continue;
  2449. }
  2450. }
  2451. }
  2452. return 0;
  2453. }
  2454. void check_should_cycle(struct chanset_t *chan)
  2455. {
  2456. if (!me_op(chan))
  2457. return;
  2458. //If there are other ops split off and i'm the only op on this side of split, cycle
  2459. int localops = 0, localbotops = 0, splitops = 0, splitbotops = 0, localnonops = 0;
  2460. for (memberlist *ml = chan->channel.member; ml && ml->nick[0]; ml = ml->next) {
  2461. if (chan_hasop(ml)) {
  2462. if (chan_issplit(ml)) {
  2463. splitops++;
  2464. if ((ml->user) && ml->user->bot)
  2465. splitbotops++;
  2466. } else {
  2467. localops++;
  2468. if ((ml->user) && ml->user->bot)
  2469. localbotops++;
  2470. if (localbotops >= 2)
  2471. return;
  2472. if (localops > localbotops)
  2473. return;
  2474. }
  2475. } else {
  2476. if (!chan_issplit(ml))
  2477. localnonops++;
  2478. }
  2479. }
  2480. if (splitbotops > 5) {
  2481. /* I'm only one opped here... and other side has some ops... so i'm cycling */
  2482. if (localnonops) {
  2483. /* need to unset any +kil first */
  2484. dprintf(DP_MODE, "MODE %s -ilk %s\nPART %s\nJOIN %s\n", chan->name,
  2485. (chan->channel.key && chan->channel.key[0]) ? chan->channel.key : "",
  2486. chan->name, chan->name);
  2487. } else
  2488. dprintf(DP_MODE, "PART %s\nJOIN %s\n", chan->name, chan->name);
  2489. }
  2490. }
  2491. /* Signoff, similar to part.
  2492. */
  2493. static int gotquit(char *from, char *msg)
  2494. {
  2495. char *nick = NULL, *chname = NULL, *p = NULL;
  2496. int split = 0;
  2497. char from2[NICKMAX + UHOSTMAX + 1] = "";
  2498. memberlist *m = NULL;
  2499. struct chanset_t *oldchan = NULL;
  2500. struct userrec *u = NULL;
  2501. strcpy(from2,from);
  2502. u = get_user_by_host(from2);
  2503. nick = splitnick(&from);
  2504. fixcolon(msg);
  2505. /* Fred1: Instead of expensive wild_match on signoff, quicker method.
  2506. * Determine if signoff string matches "%.% %.%", and only one
  2507. * space.
  2508. */
  2509. p = strchr(msg, ' ');
  2510. if (p && (p == strrchr(msg, ' '))) {
  2511. char *z1 = NULL, *z2 = NULL;
  2512. *p = 0;
  2513. z1 = strchr(p + 1, '.');
  2514. z2 = strchr(msg, '.');
  2515. if (z1 && z2 && (*(z1 + 1) != 0) && (z1 - 1 != p) &&
  2516. (z2 + 1 != p) && (z2 != msg)) {
  2517. /* Server split, or else it looked like it anyway (no harm in
  2518. * assuming)
  2519. */
  2520. split = 1;
  2521. } else
  2522. *p = ' ';
  2523. }
  2524. if (msg[0])
  2525. irc_log(NULL, "[%s] Quits %s (%s) (%s)", samechans(nick, ","), nick, from, msg);
  2526. else
  2527. irc_log(NULL, "[%s] Quits %s (%s)", samechans(nick, ","), nick, from);
  2528. struct chanset_t *chan = NULL;
  2529. for (chan = chanset; chan; chan = chan->next) {
  2530. oldchan = chan;
  2531. chname = chan->dname;
  2532. m = ismember(chan, nick);
  2533. if (m) {
  2534. u = get_user_by_host(from2);
  2535. if (u) {
  2536. set_handle_laston(chan->dname, u, now); /* If you remove this, the bot will crash when the user record in question
  2537. is removed/modified during the tcl binds below, and the users was on more
  2538. than one monitored channel */
  2539. }
  2540. if (split) {
  2541. m->split = now;
  2542. if (!findchan_by_dname(chname)) {
  2543. chan = oldchan;
  2544. continue;
  2545. }
  2546. irc_log(chan, "%s (%s) got netsplit.", nick, from);
  2547. } else {
  2548. if (!findchan_by_dname(chname)) {
  2549. chan = oldchan;
  2550. continue;
  2551. }
  2552. killmember(chan, nick);
  2553. check_lonely_channel(chan);
  2554. }
  2555. if (channel_cycle(chan))
  2556. check_should_cycle(chan);
  2557. }
  2558. }
  2559. /* Our nick quit? if so, grab it.
  2560. */
  2561. if (keepnick) {
  2562. if (!rfc_casecmp(nick, origbotname)) {
  2563. putlog(LOG_MISC, "*", "Switching back to nick %s", origbotname);
  2564. dprintf(DP_SERVER, "NICK %s\n", origbotname);
  2565. }
  2566. }
  2567. #ifdef CACHE
  2568. /* see if they were in our cache at all */
  2569. cache_t *cache = cache_find(nick);
  2570. if (cache)
  2571. cache_del(nick, cache);
  2572. #endif /* CACHE */
  2573. return 0;
  2574. }
  2575. /* Got a private message.
  2576. */
  2577. static int gotmsg(char *from, char *msg)
  2578. {
  2579. if (!strchr("&#!+@$", msg[0]))
  2580. return 0;
  2581. bool ignoring = match_ignore(from);
  2582. char *to = newsplit(&msg), *realto = (to[0] == '@') ? to + 1 : to;
  2583. struct chanset_t *chan = findchan(realto);
  2584. if (!chan)
  2585. return 0; /* Private msg to an unknown channel?? */
  2586. char buf[UHOSTLEN] = "", *nick = NULL, buf2[512] = "", *uhost = buf, *p = NULL, *p1 = NULL, *code = NULL, *ctcp = NULL;
  2587. int ctcp_count = 0;
  2588. struct userrec *u = NULL;
  2589. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  2590. fixcolon(msg);
  2591. strcpy(uhost, from);
  2592. nick = splitnick(&uhost);
  2593. /* Only check if flood-ctcp is active */
  2594. detect_autokick(nick, uhost, chan, msg);
  2595. if (flood_ctcp.count && detect_avalanche(msg)) {
  2596. memberlist *m = ismember(chan, nick);
  2597. u = get_user_by_host(from);
  2598. get_user_flagrec(u, &fr, chan->dname);
  2599. /* Discard -- kick user if it was to the channel */
  2600. if (m && me_op(chan) &&
  2601. !chan_sentkick(m) &&
  2602. !(use_exempts && ban_fun &&
  2603. /* don't kickban if permanent exempted -- Eule */
  2604. (u_match_mask(global_exempts, from) ||
  2605. u_match_mask(chan->exempts, from)))) {
  2606. if (ban_fun) {
  2607. check_exemptlist(chan, from);
  2608. u_addmask('b', chan, quickban(chan, uhost), conf.bot->nick,
  2609. "that was fun, let's do it again!", now + (60 * chan->ban_time), 0);
  2610. }
  2611. if (kick_fun) {
  2612. /* This can induce kickflood - arthur2 */
  2613. dprintf(DP_SERVER, "KICK %s %s :%sthat was fun, let's do it again!\n", chan->name, nick, kickprefix);
  2614. m->flags |= SENTKICK;
  2615. }
  2616. }
  2617. if (!ignoring) {
  2618. putlog(LOG_MODES, "*", "Avalanche from %s!%s in %s - ignoring",
  2619. nick, uhost, chan->dname);
  2620. p = strchr(uhost, '@');
  2621. if (p)
  2622. p++;
  2623. else
  2624. p = uhost;
  2625. simple_sprintf(buf2, "*!*@%s", p);
  2626. addignore(buf2, conf.bot->nick, "ctcp avalanche", now + (60 * ignore_time));
  2627. }
  2628. return 0;
  2629. }
  2630. /* Check for CTCP: */
  2631. ctcp_reply[0] = 0;
  2632. p = strchr(msg, 1);
  2633. while (p && *p) {
  2634. p++;
  2635. p1 = p;
  2636. while ((*p != 1) && *p)
  2637. p++;
  2638. if (*p == 1) {
  2639. *p = 0;
  2640. ctcp = buf2;
  2641. strcpy(ctcp, p1);
  2642. strcpy(p1 - 1, p + 1);
  2643. detect_chan_flood(nick, uhost, from, chan,
  2644. strncmp(ctcp, "ACTION ", 7) ?
  2645. FLOOD_CTCP : FLOOD_PRIVMSG, NULL);
  2646. chan = findchan(realto);
  2647. if (!chan)
  2648. return 0;
  2649. /* Respond to the first answer_ctcp */
  2650. p = strchr(msg, 1);
  2651. if (ctcp_count < answer_ctcp) {
  2652. ctcp_count++;
  2653. if (ctcp[0] != ' ') {
  2654. code = newsplit(&ctcp);
  2655. u = get_user_by_host(from);
  2656. if (!ignoring || trigger_on_ignore) {
  2657. if (check_bind_ctcp(nick, uhost, u, to, code, ctcp) == BIND_RET_LOG) {
  2658. chan = findchan(realto);
  2659. if (!chan)
  2660. return 0;
  2661. update_idle(chan->dname, nick);
  2662. }
  2663. /* Log DCC, it's to a channel damnit! */
  2664. if (!strcmp(code, "ACTION")) {
  2665. irc_log(chan, "* %s %s", nick, ctcp);
  2666. } else {
  2667. irc_log(chan, "CTCP %s: %s from %s (%s) to %s", code, ctcp, nick, from, to);
  2668. }
  2669. }
  2670. }
  2671. }
  2672. }
  2673. }
  2674. /* Send out possible ctcp responses */
  2675. if (ctcp_reply[0]) {
  2676. if (ctcp_mode != 2) {
  2677. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply);
  2678. } else {
  2679. if (now - last_ctcp > flood_ctcp.time) {
  2680. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply);
  2681. count_ctcp = 1;
  2682. } else if (count_ctcp < flood_ctcp.count) {
  2683. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply);
  2684. count_ctcp++;
  2685. }
  2686. last_ctcp = now;
  2687. }
  2688. }
  2689. if (msg[0]) {
  2690. int botmatch = 0;
  2691. char *my_msg = NULL, *my_ptr = NULL, *fword = NULL;
  2692. /* Check even if we're ignoring the host. (modified by Eule 17.7.99) */
  2693. detect_chan_flood(nick, uhost, from, chan, FLOOD_PRIVMSG, NULL);
  2694. if (!(chan = findchan(realto)))
  2695. return 0;
  2696. if (auth_chan) {
  2697. my_msg = my_ptr = strdup(msg);
  2698. fword = newsplit(&my_msg); /* take out first word */
  2699. /* the first word is a wildcard match to our nick. */
  2700. botmatch = wild_match(fword, botname);
  2701. if (botmatch && strcmp(fword, "*"))
  2702. fword = newsplit(&my_msg); /* now this will be the command */
  2703. /* is it a cmd? */
  2704. if (auth_prefix[0] && fword && fword[0] && fword[1] && ((botmatch && fword[0] != auth_prefix[0]) || (fword[0] == auth_prefix[0]))) {
  2705. Auth *auth = Auth::Find(uhost);
  2706. if (auth && auth->Authed()) {
  2707. if (fword[0] == auth_prefix[0])
  2708. fword++;
  2709. auth->atime = now;
  2710. check_bind_authc(fword, auth, chan->dname, my_msg);
  2711. }
  2712. }
  2713. free(my_ptr);
  2714. }
  2715. irc_log(chan, "%s<%s> %s", to[0] == '@' ? "@" : "", nick, msg);
  2716. update_idle(chan->dname, nick);
  2717. }
  2718. return 0;
  2719. }
  2720. /* Got a private notice.
  2721. */
  2722. static int gotnotice(char *from, char *msg)
  2723. {
  2724. if (!strchr(CHANMETA "@", *msg))
  2725. return 0;
  2726. bool ignoring = match_ignore(from);
  2727. char *to = newsplit(&msg), *realto = (*to == '@') ? to + 1 : to;
  2728. struct chanset_t *chan = findchan(realto);
  2729. chan = findchan(realto);
  2730. if (!chan)
  2731. return 0; /* Notice to an unknown channel?? */
  2732. char *nick = NULL, buf2[512] = "", *p = NULL, *p1 = NULL, buf[512] = "", *uhost = buf;
  2733. char *ctcp = NULL, *code = NULL;
  2734. struct userrec *u = NULL;
  2735. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  2736. fixcolon(msg);
  2737. strcpy(uhost, from);
  2738. nick = splitnick(&uhost);
  2739. u = get_user_by_host(from);
  2740. if (flood_ctcp.count && detect_avalanche(msg)) {
  2741. memberlist *m = ismember(chan, nick);
  2742. get_user_flagrec(u, &fr, chan->dname);
  2743. /* Discard -- kick user if it was to the channel */
  2744. if (me_op(chan) && m && !chan_sentkick(m) &&
  2745. !(use_exempts && ban_fun &&
  2746. /* don't kickban if permanent exempted -- Eule */
  2747. (u_match_mask(global_exempts,from) ||
  2748. u_match_mask(chan->exempts, from)))) {
  2749. if (ban_fun) {
  2750. check_exemptlist(chan, from);
  2751. u_addmask('b', chan, quickban(chan, uhost), conf.bot->nick,
  2752. "that was fun, let's do it again!", now + (60 * chan->ban_time), 0);
  2753. }
  2754. if (kick_fun) {
  2755. /* This can induce kickflood - arthur2 */
  2756. dprintf(DP_SERVER, "KICK %s %s :%sthat was fun, let's do it again!\n", chan->name, nick, kickprefix);
  2757. m->flags |= SENTKICK;
  2758. }
  2759. }
  2760. if (!ignoring)
  2761. putlog(LOG_MODES, "*", "Avalanche from %s", from);
  2762. return 0;
  2763. }
  2764. /* Check for CTCP: */
  2765. p = strchr(msg, 1);
  2766. while (p && *p) {
  2767. p++;
  2768. p1 = p;
  2769. while ((*p != 1) && *p)
  2770. p++;
  2771. if (*p == 1) {
  2772. *p = 0;
  2773. ctcp = buf2;
  2774. strcpy(ctcp, p1);
  2775. strcpy(p1 - 1, p + 1);
  2776. p = strchr(msg, 1);
  2777. detect_chan_flood(nick, uhost, from, chan,
  2778. strncmp(ctcp, "ACTION ", 7) ?
  2779. FLOOD_CTCP : FLOOD_PRIVMSG, NULL);
  2780. chan = findchan(realto);
  2781. if (!chan)
  2782. return 0;
  2783. if (ctcp[0] != ' ') {
  2784. code = newsplit(&ctcp);
  2785. if (!ignoring || trigger_on_ignore) {
  2786. check_bind_ctcr(nick, uhost, u, chan->dname, code, msg);
  2787. chan = findchan(realto);
  2788. if (!chan)
  2789. return 0;
  2790. irc_log(chan, "CTCP reply %s: %s from %s (%s) to %s", code, msg, nick, from, chan->dname);
  2791. update_idle(chan->dname, nick);
  2792. }
  2793. }
  2794. }
  2795. }
  2796. if (msg[0]) {
  2797. /* Check even if we're ignoring the host. (modified by Eule 17.7.99) */
  2798. detect_chan_flood(nick, uhost, from, chan, FLOOD_NOTICE, NULL);
  2799. chan = findchan(realto);
  2800. if (!chan)
  2801. return 0;
  2802. update_idle(chan->dname, nick);
  2803. if (!ignoring)
  2804. irc_log(chan, "-%s:%s- %s", nick, to, msg);
  2805. }
  2806. return 0;
  2807. }
  2808. static cmd_t irc_raw[] =
  2809. {
  2810. {"302", "", (Function) got302, "irc:302", LEAF},
  2811. #ifdef CACHE
  2812. {"341", "", (Function) got341, "irc:341", LEAF},
  2813. #endif /* CACHE */
  2814. {"324", "", (Function) got324, "irc:324", LEAF},
  2815. {"352", "", (Function) got352, "irc:352", LEAF},
  2816. {"354", "", (Function) got354, "irc:354", LEAF},
  2817. {"315", "", (Function) got315, "irc:315", LEAF},
  2818. {"367", "", (Function) got367, "irc:367", LEAF},
  2819. {"368", "", (Function) got368, "irc:368", LEAF},
  2820. {"403", "", (Function) got403, "irc:403", LEAF},
  2821. {"405", "", (Function) got405, "irc:405", LEAF},
  2822. {"471", "", (Function) got471, "irc:471", LEAF},
  2823. {"473", "", (Function) got473, "irc:473", LEAF},
  2824. {"474", "", (Function) got474, "irc:474", LEAF},
  2825. {"475", "", (Function) got475, "irc:475", LEAF},
  2826. {"INVITE", "", (Function) gotinvite, "irc:invite", LEAF},
  2827. {"TOPIC", "", (Function) gottopic, "irc:topic", LEAF},
  2828. {"331", "", (Function) got331, "irc:331", LEAF},
  2829. {"332", "", (Function) got332, "irc:332", LEAF},
  2830. {"JOIN", "", (Function) gotjoin, "irc:join", LEAF},
  2831. {"PART", "", (Function) gotpart, "irc:part", LEAF},
  2832. {"KICK", "", (Function) gotkick, "irc:kick", LEAF},
  2833. {"NICK", "", (Function) gotnick, "irc:nick", LEAF},
  2834. {"QUIT", "", (Function) gotquit, "irc:quit", LEAF},
  2835. {"PRIVMSG", "", (Function) gotmsg, "irc:msg", LEAF},
  2836. {"NOTICE", "", (Function) gotnotice, "irc:notice", LEAF},
  2837. {"MODE", "", (Function) gotmode, "irc:mode", LEAF},
  2838. {"346", "", (Function) got346, "irc:346", LEAF},
  2839. {"347", "", (Function) got347, "irc:347", LEAF},
  2840. {"348", "", (Function) got348, "irc:348", LEAF},
  2841. {"349", "", (Function) got349, "irc:349", LEAF},
  2842. {"353", "", (Function) got353, "irc:353", LEAF},
  2843. {NULL, NULL, NULL, NULL, 0}
  2844. };