chan.cc 102 KB

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