chan.cc 102 KB

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