chan.cc 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560
  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 = new 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. strlcpy(n->nick, nick, sizeof(n->nick));
  225. n->rfc_nick = std::make_shared<RfcString>(n->nick);
  226. n->hops = -1;
  227. if (!lx) {
  228. // Free the pseudo-member created in init_channel()
  229. if (unlikely(!chan->channel.member->nick[0])) {
  230. free(chan->channel.member);
  231. chan->channel.member = NULL;
  232. }
  233. n->next = chan->channel.member;
  234. chan->channel.member = n;
  235. } else {
  236. n->next = lx->next;
  237. lx->next = n;
  238. }
  239. ++(chan->channel.members);
  240. n->floodtime = new bd::HashTable<flood_t, time_t>;
  241. n->floodnum = new bd::HashTable<flood_t, int>;
  242. (*chan->channel.hashed_members)[*n->rfc_nick] = n;
  243. return n;
  244. }
  245. void delete_member(memberlist* m) {
  246. delete m->floodtime;
  247. delete m->floodnum;
  248. delete m;
  249. }
  250. static bool member_getuser(memberlist* m, bool act_on_lookup) {
  251. if (!m) return 0;
  252. if (!m->user && !m->tried_getuser && !m->is_me) {
  253. m->user = get_user_by_host(m->from);
  254. if (!m->user && m->userip[0]) {
  255. m->user = get_user_by_host(m->fromip);
  256. }
  257. m->tried_getuser = 1;
  258. /* Managed to get the user for a previously unknown user. Act on it! */
  259. if (act_on_lookup && m->user)
  260. check_this_user(m->user->handle, 0, NULL);
  261. } else if (!m->user && m->is_me) {
  262. m->user = conf.bot->u;
  263. m->tried_getuser = 1;
  264. }
  265. return !(m->user == NULL);
  266. }
  267. /* Always pass the channel dname (display name) to this function <cybah>
  268. */
  269. static void update_idle(char *chname, char *nick)
  270. {
  271. struct chanset_t *chan = findchan_by_dname(chname);
  272. if (chan) {
  273. memberlist *m = ismember(chan, nick);
  274. if (m)
  275. m->last = now;
  276. }
  277. }
  278. /* Returns the current channel mode.
  279. */
  280. static char *getchanmode(struct chanset_t *chan)
  281. {
  282. static char s[121] = "";
  283. int atr = chan->channel.mode;
  284. size_t i = 1;
  285. s[0] = '+';
  286. if (atr & CHANINV)
  287. s[i++] = 'i';
  288. if (atr & CHANPRIV)
  289. s[i++] = 'p';
  290. if (atr & CHANSEC)
  291. s[i++] = 's';
  292. if (atr & CHANMODER)
  293. s[i++] = 'm';
  294. if (atr & CHANNOCLR)
  295. s[i++] = 'c';
  296. if (atr & CHANNOCTCP)
  297. s[i++] = 'C';
  298. if (atr & CHANREGON)
  299. s[i++] = 'R';
  300. if (atr & CHANTOPIC)
  301. s[i++] = 't';
  302. if (atr & CHANMODR)
  303. s[i++] = 'M';
  304. if (atr & CHANLONLY)
  305. s[i++] = 'r';
  306. if (atr & CHANNOMSG)
  307. s[i++] = 'n';
  308. if (atr & CHANANON)
  309. s[i++] = 'a';
  310. if (atr & CHANKEY)
  311. s[i++] = 'k';
  312. if (chan->channel.maxmembers != 0)
  313. s[i++] = 'l';
  314. s[i] = 0;
  315. if (chan->channel.key[0])
  316. i += simple_snprintf(s + i, sizeof(s) - i, " %s", chan->channel.key);
  317. if (chan->channel.maxmembers != 0)
  318. simple_snprintf(s + i, sizeof(s) - i, " %d", chan->channel.maxmembers);
  319. return s;
  320. }
  321. static void check_exemptlist(struct chanset_t *chan, const char *from)
  322. {
  323. if (!use_exempts)
  324. return;
  325. bool ok = 0;
  326. for (masklist *e = chan->channel.exempt; e->mask[0]; e = e->next)
  327. if (wild_match(e->mask, from)) {
  328. add_mode(chan, '-', 'e', e->mask);
  329. ok = 1;
  330. }
  331. if (prevent_mixing && ok)
  332. flush_mode(chan, QUICK);
  333. }
  334. static void priority_do(struct chanset_t * chan, bool opsonly, int action, bool flush = 1)
  335. {
  336. if (!me_op(chan))
  337. return;
  338. if (channel_pending(chan) || !shouldjoin(chan) || !channel_active(chan))
  339. return;
  340. memberlist *m = NULL;
  341. int ops = 0, targets = 0, bpos = 0, tpos = 0, ft = 0, ct = 0, actions = 0, sent = 0;
  342. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  343. member_getuser(m);
  344. if (m->user && m->user->bot && (m->user->flags & USER_OP)) {
  345. ++ops;
  346. if (m->is_me)
  347. bpos = (ops - 1);
  348. } else if (!opsonly || chan_hasop(m)) {
  349. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  350. if (m->user)
  351. get_user_flagrec(m->user, &fr, chan->dname, chan);
  352. if (!chk_op(fr, chan))
  353. ++targets;
  354. }
  355. }
  356. if (!targets || !ops)
  357. return;
  358. for (int n = 0; n < 2; ++n) {
  359. // First pass - Handle my priority targets
  360. if (n == 0) {
  361. ft = (bpos * targets) / ops;
  362. ct = ((bpos + 2) * targets + (ops - 1)) / ops;
  363. ct = (ct - ft + 1);
  364. if (ct > 20)
  365. ct = 20;
  366. while (ft >= targets)
  367. ft -= targets;
  368. actions = 0;
  369. sent = 0;
  370. } else {
  371. // Second pass - Handle all remaining if I am able to
  372. ct = ct - actions;
  373. if (ct > ft)
  374. ct = ft;
  375. ft = 0;
  376. actions = 0;
  377. tpos = 0;
  378. }
  379. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  380. if (!opsonly || chan_hasop(m)) {
  381. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  382. if (m->user)
  383. get_user_flagrec(m->user, &fr, chan->dname, chan);
  384. if (!chk_op(fr, chan)) {
  385. if (tpos >= ft) {
  386. if ((action == PRIO_DEOP) && !chan_sentdeop(m)) {
  387. ++actions;
  388. ++sent;
  389. add_mode(chan, '-', 'o', m);
  390. if (!floodless && (actions >= ct || (n == 1 && sent > 20))) {
  391. if (flush)
  392. flush_mode(chan, QUICK);
  393. return;
  394. }
  395. } else if ((action == PRIO_KICK) && !chan_sentkick(m) &&
  396. // Check closed-exempt
  397. !((chan_hasop(m) && chan->closed_exempt_mode == CHAN_FLAG_OP) ||
  398. ((chan_hasvoice(m) || chan_hasop(m)) && chan->closed_exempt_mode == CHAN_FLAG_VOICE))) {
  399. ++actions;
  400. ++sent;
  401. if (chan->closed_ban)
  402. do_closed_kick(chan, m);
  403. dprintf(DP_MODE, "KICK %s %s :%s%s\n", chan->name, m->nick, kickprefix, response(RES_CLOSED));
  404. m->flags |= SENTKICK;
  405. if (!floodless && (actions >= ct || (n == 1 && sent > 5)))
  406. return;
  407. }
  408. }
  409. ++tpos;
  410. }
  411. }
  412. }
  413. }
  414. }
  415. /* lame code
  416. static int target_priority(struct chanset_t * chan, memberlist *target, int opsonly)
  417. {
  418. memberlist *m;
  419. int ops = 0, targets = 0, bpos = 0, ft = 0, ct = 0, tp = (-1), pos = 0;
  420. return 1;
  421. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  422. if (m->user && ((m->user->flags & (USER_BOT | USER_OP)) == (USER_BOT | USER_OP))) {
  423. ops++;
  424. if (m->is_me)
  425. bpos = ops;
  426. } else if (!opsonly || chan_hasop(m)) {
  427. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  428. if (m->user)
  429. get_user_flagrec(m->user, &fr, chan->dname);
  430. if (((glob_deop(fr) && !chan_op(fr)) || chan_deop(fr)) ||
  431. ((!channel_privchan(chan) && !chan_op(fr) && !glob_op(fr)) ||
  432. (channel_privchan(chan) && !glob_bot(fr) && !glob_owner(fr) && !chan_op(fr)))) {
  433. targets++;
  434. }
  435. }
  436. if (m == target)
  437. tp = pos;
  438. pos++;
  439. }
  440. if (!targets || !ops || (tp < 0)) {
  441. return 0;
  442. }
  443. ft = (bpos * targets) / ops;
  444. ct = ((bpos + 2) * targets + (ops - 1)) / ops;
  445. ct = (ct - ft + 1);
  446. if (ct > 20)
  447. ct = 20;
  448. while (ft >= targets) {
  449. ft -= targets;
  450. }
  451. if (ct >= targets) {
  452. putlog(LOG_MISC, "*", "%s 1 ct >= targets; ct %d targets %d", target, ct, targets);
  453. if ((tp >= ft) || (tp <= (ct % targets))) {
  454. putlog(LOG_MISC, "*", "%s (1) first if, tp %d ft %d ct/targets %d", target, tp, ft, (ct % targets));
  455. return 1;
  456. }
  457. } else {
  458. putlog(LOG_MISC, "*", "%s 2 else, ct %d targets %d", target, ct, targets);
  459. if ((tp >= ft) && (tp <= ct)) {
  460. putlog(LOG_MISC, "*", "%s (1) second if, tp %d ft %d", target, tp, ft);
  461. return 1;
  462. }
  463. }
  464. putlog(LOG_MISC, "*", "%s (0) returning 0", target);
  465. return 0;
  466. }
  467. */
  468. /* Check a channel and clean-out any more-specific matching masks.
  469. *
  470. * Moved all do_ban(), do_exempt() and do_invite() into this single function
  471. * as the code bloat is starting to get rediculous <cybah>
  472. */
  473. static void do_mask(struct chanset_t *chan, masklist *m, char *mask, char Mode)
  474. {
  475. for (; m && m->mask[0]; m = m->next)
  476. if (wild_match(mask, m->mask) && rfc_casecmp(mask, m->mask))
  477. add_mode(chan, '-', Mode, m->mask);
  478. add_mode(chan, '+', Mode, mask);
  479. flush_mode(chan, QUICK);
  480. }
  481. /* This is a clone of detect_flood, but works for channel specificity now
  482. * and handles kick & deop as well.
  483. */
  484. static bool detect_chan_flood(memberlist* m, const char *from, struct chanset_t *chan, flood_t which, const char *msg)
  485. {
  486. /* Do not punish non-existant channel members and IRC services like
  487. * ChanServ
  488. */
  489. if (!chan || (which < 0) || (which >= FLOOD_CHAN_MAX) || !m ||
  490. !(chan->role & ROLE_FLOOD))
  491. return 0;
  492. /* Okay, make sure i'm not flood-checking myself */
  493. if (m->is_me) {
  494. return 0;
  495. }
  496. if (!strcasecmp(m->userhost, botuserhost))
  497. return 0;
  498. /* My user@host (?) */
  499. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  500. struct userrec *u = NULL;
  501. member_getuser(m);
  502. u = m->user;
  503. get_user_flagrec(u, &fr, chan->dname, chan);
  504. if (glob_bot(fr) ||
  505. ((which == FLOOD_DEOP) &&
  506. (glob_master(fr) || chan_master(fr))) ||
  507. ((which == FLOOD_KICK) &&
  508. (glob_master(fr) || chan_master(fr))) ||
  509. ((which != FLOOD_DEOP) && (which != FLOOD_KICK) &&
  510. ( (chk_noflood(fr) ||
  511. (m && chan->flood_exempt_mode == CHAN_FLAG_OP && chan_hasop(m)) ||
  512. (m && chan->flood_exempt_mode == CHAN_FLAG_VOICE && (chan_hasvoice(m) || chan_hasop(m))) )
  513. )))
  514. return 0;
  515. char h[UHOSTLEN] = "", ftype[14] = "", *p = NULL;
  516. int thr = 0, mthr = 0;
  517. int increment = 1;
  518. time_t lapse = 0, mlapse = 0;
  519. /* Determine how many are necessary to make a flood. */
  520. switch (which) {
  521. case FLOOD_PRIVMSG:
  522. case FLOOD_NOTICE:
  523. thr = chan->flood_pub_thr;
  524. lapse = chan->flood_pub_time;
  525. mthr = chan->flood_mpub_thr;
  526. mlapse = chan->flood_mpub_time;
  527. strlcpy(ftype, "pub", sizeof(ftype));
  528. break;
  529. case FLOOD_BYTES:
  530. thr = chan->flood_bytes_thr;
  531. lapse = chan->flood_bytes_time;
  532. mthr = chan->flood_mbytes_thr;
  533. mlapse = chan->flood_mbytes_time;
  534. strlcpy(ftype, "bytes", sizeof(ftype));
  535. increment = static_cast<int>(strlen(msg));
  536. break;
  537. case FLOOD_CTCP:
  538. thr = chan->flood_ctcp_thr;
  539. lapse = chan->flood_ctcp_time;
  540. mthr = chan->flood_mctcp_thr;
  541. mlapse = chan->flood_mctcp_time;
  542. strlcpy(ftype, "ctcp", sizeof(ftype));
  543. break;
  544. case FLOOD_NICK:
  545. thr = chan->flood_nick_thr;
  546. lapse = chan->flood_nick_time;
  547. strlcpy(ftype, "nick", sizeof(ftype));
  548. break;
  549. case FLOOD_JOIN:
  550. case FLOOD_PART:
  551. thr = chan->flood_join_thr;
  552. lapse = chan->flood_join_time;
  553. strlcpy(ftype, "join", sizeof(ftype));
  554. break;
  555. case FLOOD_DEOP:
  556. thr = chan->flood_deop_thr;
  557. lapse = chan->flood_deop_time;
  558. strlcpy(ftype, "deop", sizeof(ftype));
  559. break;
  560. case FLOOD_KICK:
  561. thr = chan->flood_kick_thr;
  562. lapse = chan->flood_kick_time;
  563. strlcpy(ftype, "kick", sizeof(ftype));
  564. break;
  565. }
  566. if ((which == FLOOD_KICK) || (which == FLOOD_DEOP))
  567. p = m->nick;
  568. else {
  569. p = strchr(m->userhost, '@');
  570. if (p) {
  571. p++;
  572. }
  573. if (!p)
  574. return 0;
  575. }
  576. // Track across all clients in the channel
  577. if (mlapse && mthr) {
  578. if (!chan->channel.floodtime->contains("all")) {
  579. (*chan->channel.floodtime)["all"][which] = now;
  580. (*chan->channel.floodnum)["all"][which] = increment;
  581. return 0;
  582. }
  583. // No point locking down due to OPs flooding.
  584. if (!chan_hasop(m)) {
  585. bd::HashTable<flood_t, time_t> *global_floodtime = &(*chan->channel.floodtime)["all"];
  586. bd::HashTable<flood_t, int> *global_floodnum = &(*chan->channel.floodnum)["all"];
  587. if ((*global_floodtime)[which] < now - mlapse) {
  588. /* Flood timer expired, reset it */
  589. (*global_floodtime)[which] = now;
  590. (*global_floodnum)[which] = increment;
  591. } else {
  592. (*global_floodnum)[which] += increment;
  593. if ((*global_floodnum)[which] >= mthr) { /* FLOOD */
  594. /* Reset counters */
  595. (*global_floodnum).remove(which);
  596. (*global_floodtime).remove(which);
  597. if (!chan->channel.drone_set_mode) {
  598. lockdown_chan(chan, FLOOD_MASS_FLOOD, ftype);
  599. }
  600. }
  601. }
  602. }
  603. }
  604. if ((thr == 0) || (lapse == 0)) {
  605. return 0; /* no flood protection */
  606. }
  607. // Track individual hosts/clients
  608. bd::HashTable<flood_t, time_t> *floodtime; // floodtime[FLOOD_PRIVMSG] = now;
  609. bd::HashTable<flood_t, int> *floodnum; // floodnum[FLOOD_PRIVMSG] = 1;
  610. switch (which) {
  611. // These 2 don't have a persistent member, use chan list
  612. case FLOOD_JOIN:
  613. case FLOOD_PART:
  614. // If not found, add them and start the count for next iteration
  615. if (!chan->channel.floodtime->contains(m->userhost)) {
  616. (*chan->channel.floodtime)[m->userhost][which] = now;
  617. (*chan->channel.floodnum)[m->userhost][which] = increment;
  618. return 0;
  619. } else {
  620. floodtime = &(*chan->channel.floodtime)[m->userhost];
  621. floodnum = &(*chan->channel.floodnum)[m->userhost];
  622. }
  623. break;
  624. default:
  625. // Everything else, log to the member
  626. // If not found, add them and start the count for next iteration
  627. if (!m->floodtime->contains(which)) {
  628. (*m->floodtime)[which] = now;
  629. (*m->floodnum)[which] = increment;
  630. return 0;
  631. } else {
  632. floodtime = m->floodtime;
  633. floodnum = m->floodnum;
  634. }
  635. break;
  636. }
  637. if ((*floodtime)[which] < now - lapse) {
  638. /* Flood timer expired, reset it */
  639. (*floodtime)[which] = now;
  640. (*floodnum)[which] = increment;
  641. return 0;
  642. }
  643. (*floodnum)[which] += increment;
  644. if ((*floodnum)[which] >= thr) { /* FLOOD */
  645. /* Reset counters */
  646. (*floodnum).remove(which);
  647. (*floodtime).remove(which);
  648. switch (which) {
  649. case FLOOD_PRIVMSG:
  650. case FLOOD_NOTICE:
  651. case FLOOD_CTCP:
  652. case FLOOD_BYTES:
  653. /* Flooding chan! either by public or notice */
  654. if (!chan_sentkick(m) && me_op(chan)) {
  655. if (channel_floodban(chan)) {
  656. putlog(LOG_MODES, chan->dname, "Channel flood from %s -- banning", m->nick);
  657. char *s1 = quickban(chan, from);
  658. u_addmask('b', chan, s1, conf.bot->nick, "channel flood", now + (60 * chan->ban_time), 0);
  659. } else {
  660. const char *response = punish_flooder(chan, m);
  661. putlog(LOG_MODES, chan->dname, "Channel flood from %s -- %s", m->nick, response);
  662. }
  663. }
  664. return 1;
  665. case FLOOD_JOIN:
  666. case FLOOD_PART:
  667. case FLOOD_NICK:
  668. if (use_exempts &&
  669. (u_match_mask(global_exempts, from) ||
  670. u_match_mask(chan->exempts, from)))
  671. return 1;
  672. simple_snprintf(h, sizeof(h), "*!*@%s", p);
  673. if (!isbanned(chan, h) && me_op(chan)) {
  674. check_exemptlist(chan, from);
  675. do_mask(chan, chan->channel.ban, h, 'b');
  676. }
  677. if ((u_match_mask(global_bans, from))
  678. || (u_match_mask(chan->bans, from)))
  679. return 1; /* Already banned */
  680. if (which == FLOOD_JOIN || which == FLOOD_PART)
  681. putlog(LOG_MISC | LOG_JOIN, chan->dname, "JOIN flood from @%s! Banning.", p);
  682. else
  683. putlog(LOG_MISC | LOG_JOIN, chan->dname, "NICK flood from @%s! Banning.", p);
  684. strlcat(ftype, " flood", sizeof(ftype));
  685. u_addmask('b', chan, h, conf.bot->nick, ftype, now + (60 * chan->ban_time), 0);
  686. if (which == FLOOD_PART)
  687. add_mode(chan, '+', 'b', h);
  688. if (!channel_enforcebans(chan) && me_op(chan)) {
  689. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  690. if (!chan_sentkick(m) && wild_match(h, m->from) &&
  691. (m->joined >= (*floodtime)[which]) &&
  692. !m->is_me && me_op(chan)) {
  693. m->flags |= SENTKICK;
  694. if (which == FLOOD_JOIN)
  695. dprintf(DP_SERVER, "KICK %s %s :%sjoin flood\n", chan->name, m->nick, kickprefix);
  696. else
  697. dprintf(DP_SERVER, "KICK %s %s :%s%s\n", chan->name, m->nick, kickprefix, response(RES_NICKFLOOD));
  698. }
  699. }
  700. }
  701. return 1;
  702. case FLOOD_KICK:
  703. if (me_op(chan) && !chan_sentkick(m)) {
  704. putlog(LOG_MODES, chan->dname, "Kicking %s, for mass kick.", m->nick);
  705. dprintf(DP_MODE, "KICK %s %s :%s%s\n", chan->name, m->nick, kickprefix, response(RES_KICKFLOOD));
  706. m->flags |= SENTKICK;
  707. }
  708. if (channel_protect(chan))
  709. do_protect(chan, "Mass Kick");
  710. return 1;
  711. case FLOOD_DEOP:
  712. if (me_op(chan) && !chan_sentkick(m)) {
  713. putlog(LOG_MODES, chan->dname,
  714. "Mass deop on %s by %s", chan->dname, from);
  715. dprintf(DP_MODE, "KICK %s %s :%s%s\n", chan->name, m->nick, kickprefix, response(RES_MASSDEOP));
  716. m->flags |= SENTKICK;
  717. }
  718. if (u) {
  719. char s[256] = "";
  720. simple_snprintf(s, sizeof(s), "Mass deop on %s by %s", chan->dname, from);
  721. deflag_user(u, DEFLAG_EVENT_MDOP, s, chan);
  722. }
  723. if (channel_protect(chan))
  724. do_protect(chan, "Mass Deop");
  725. return 1;
  726. }
  727. }
  728. return 0;
  729. }
  730. /* Given a chan/m do all necesary exempt checks and ban. */
  731. static void refresh_ban_kick(struct chanset_t*, memberlist *, const char *);
  732. static void do_closed_kick(struct chanset_t *chan, memberlist *m)
  733. {
  734. if (!chan || !m) return;
  735. char *s1 = NULL;
  736. if (!(use_exempts &&
  737. (u_match_mask(global_exempts, m->from) ||
  738. u_match_mask(chan->exempts, m->from)))) {
  739. if (u_match_mask(global_bans, m->from) || u_match_mask(chan->bans, m->from))
  740. refresh_ban_kick(chan, m, m->from);
  741. check_exemptlist(chan, m->from);
  742. s1 = quickban(chan, m->from);
  743. u_addmask('b', chan, s1, conf.bot->nick, "joined closed chan", now + (60 * chan->ban_time), 0);
  744. }
  745. return;
  746. }
  747. /* Given a nick!user@host, place a quick ban on them on a chan.
  748. */
  749. static char *quickban(struct chanset_t *chan, const char *from)
  750. {
  751. static char s1[512] = "";
  752. maskaddr(from, s1, chan->ban_type);
  753. do_mask(chan, chan->channel.ban, s1, 'b');
  754. return s1;
  755. }
  756. /* Kick any user (except friends/masters) with certain mask from channel
  757. * with a specified comment. Ernst 18/3/1998
  758. */
  759. static void kick_all(struct chanset_t *chan, char *hostmask, const char *comment, int bantype)
  760. {
  761. int flushed = 0;
  762. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  763. for (memberlist *m = chan->channel.member; m && m->nick[0]; m = m->next) {
  764. get_user_flagrec(m->user, &fr, chan->dname, chan);
  765. if ((wild_match(hostmask, m->from) || match_cidr(hostmask, m->from) ||
  766. (m->userip[0] && (wild_match(hostmask, m->fromip) || match_cidr(hostmask, m->fromip)))) &&
  767. !chan_sentkick(m) &&
  768. !m->is_me && !chan_issplit(m) &&
  769. !(use_exempts &&
  770. ((bantype && (isexempted(chan, m->from) || (chan->ircnet_status & CHAN_ASKED_EXEMPTS))) ||
  771. (u_match_mask(global_exempts, m->from) ||
  772. u_match_mask(chan->exempts, m->from) ||
  773. (m->userip[0] &&
  774. (u_match_mask(global_exempts, m->fromip) ||
  775. u_match_mask(chan->exempts, m->fromip))))))) {
  776. if (!flushed) {
  777. /* We need to kick someone, flush eventual bans first */
  778. flush_mode(chan, QUICK);
  779. flushed += 1;
  780. }
  781. if (!chan_sentkick(m)) {
  782. m->flags |= SENTKICK; /* Mark as pending kick */
  783. dprintf(DP_MODE, "KICK %s %s :%s%s\n", chan->name, m->nick, kickprefix, comment);
  784. }
  785. }
  786. }
  787. }
  788. /* If any bans match this wildcard expression, refresh them on the channel.
  789. */
  790. static void refresh_ban_kick(struct chanset_t* chan, memberlist *m, const char *user)
  791. {
  792. if (!m || chan_sentkick(m))
  793. return;
  794. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  795. get_user_flagrec(m->user, &fr, chan->dname, chan);
  796. /* Check global bans in first cycle and channel bans
  797. in second cycle. */
  798. for (int cycle = 0; cycle < 2; cycle++) {
  799. for (maskrec* b = cycle ? chan->bans : global_bans; b; b = b->next) {
  800. if (wild_match(b->mask, user) || match_cidr(b->mask, user)) {
  801. if ((chan->role & ROLE_DEOP) && chan_hasop(m))
  802. add_mode(chan, '-', 'o', m); /* Guess it can't hurt. */
  803. check_exemptlist(chan, user);
  804. do_mask(chan, chan->channel.ban, b->mask, 'b');
  805. b->lastactive = now;
  806. if (chan->role & ROLE_KICK) {
  807. char c[512] = ""; /* The ban comment. */
  808. if (b->desc && b->desc[0] != '@')
  809. simple_snprintf(c, sizeof(c), "banned: %s", b->desc);
  810. kick_all(chan, b->mask, c[0] ? (const char *) c : "You are banned", 0);
  811. }
  812. return; /* Drop out on 1st ban. */
  813. }
  814. }
  815. }
  816. }
  817. /* This is a bit cumbersome at the moment, but it works... Any improvements
  818. * then feel free to have a go.. Jason
  819. */
  820. static void refresh_exempt(struct chanset_t *chan, char *user)
  821. {
  822. maskrec *e = NULL;
  823. masklist *b = NULL;
  824. /* Check global exempts in first cycle and channel exempts
  825. in second cycle. */
  826. for (int cycle = 0; cycle < 2; cycle++) {
  827. for (e = cycle ? chan->exempts : global_exempts; e; e = e->next) {
  828. if (wild_match(user, e->mask) || wild_match(e->mask, user) || match_cidr(e->mask, user)) {
  829. for (b = chan->channel.ban; b && b->mask[0]; b = b->next) {
  830. if (wild_match(b->mask, user) || wild_match(user, b->mask)) {
  831. if (e->lastactive < now - 60 && !isexempted(chan, e->mask)) {
  832. do_mask(chan, chan->channel.exempt, e->mask, 'e');
  833. e->lastactive = now;
  834. }
  835. }
  836. }
  837. }
  838. }
  839. }
  840. }
  841. static void refresh_invite(struct chanset_t *chan, const char *user)
  842. {
  843. maskrec *i = NULL;
  844. /* Check global invites in first cycle and channel invites
  845. in second cycle. */
  846. for (int cycle = 0; cycle < 2; cycle++) {
  847. for (i = cycle ? chan->invites : global_invites; i; i = i->next) {
  848. if (wild_match(i->mask, user) &&
  849. ((i->flags & MASKREC_STICKY) || (chan->channel.mode & CHANINV))) {
  850. if (i->lastactive < now - 60 && !isinvited(chan, i->mask)) {
  851. do_mask(chan, chan->channel.invite, i->mask, 'I');
  852. i->lastactive = now;
  853. return;
  854. }
  855. }
  856. }
  857. }
  858. }
  859. /* Enforce all channel bans in a given channel. Ernst 18/3/1998
  860. */
  861. static void enforce_bans(struct chanset_t *chan)
  862. {
  863. if (!me_op(chan))
  864. return; /* Can't do it :( */
  865. if ((chan->ircnet_status & CHAN_ASKED_EXEMPTS))
  866. return;
  867. const memberlist *me = ismember(chan, botname);
  868. /* Go through all bans, kicking the users. */
  869. for (masklist *b = chan->channel.ban; b && b->mask[0]; b = b->next) {
  870. if (!(wild_match(b->mask, me->from) || match_cidr(b->mask, me->fromip)) && !isexempted(chan, b->mask))
  871. kick_all(chan, b->mask, "You are banned", 1);
  872. }
  873. }
  874. /* Make sure that all who are 'banned' on the userlist are actually in fact
  875. * banned on the channel.
  876. *
  877. * Note: Since i was getting a ban list, i assume i'm chop.
  878. */
  879. static void recheck_bans(struct chanset_t *chan)
  880. {
  881. maskrec *u = NULL;
  882. /* Check global bans in first cycle and channel bans
  883. in second cycle. */
  884. for (int cycle = 0; cycle < 2; cycle++) {
  885. for (u = cycle ? chan->bans : global_bans; u; u = u->next)
  886. if (!isbanned(chan, u->mask) && (!channel_dynamicbans(chan) || (u->flags & MASKREC_STICKY)))
  887. add_mode(chan, '+', 'b', u->mask);
  888. }
  889. }
  890. /* Make sure that all who are exempted on the userlist are actually in fact
  891. * exempted on the channel.
  892. *
  893. * Note: Since i was getting an excempt list, i assume i'm chop.
  894. */
  895. static void recheck_exempts(struct chanset_t *chan)
  896. {
  897. maskrec *e = NULL;
  898. masklist *b = NULL;
  899. /* Check global exempts in first cycle and channel exempts
  900. in second cycle. */
  901. for (int cycle = 0; cycle < 2; cycle++) {
  902. for (e = cycle ? chan->exempts : global_exempts; e; e = e->next) {
  903. if (!isexempted(chan, e->mask) &&
  904. (!channel_dynamicexempts(chan) || (e->flags & MASKREC_STICKY)))
  905. add_mode(chan, '+', 'e', e->mask);
  906. for (b = chan->channel.ban; b && b->mask[0]; b = b->next) {
  907. if ((wild_match(b->mask, e->mask) || wild_match(e->mask, b->mask)) &&
  908. !isexempted(chan, e->mask))
  909. add_mode(chan,'+','e',e->mask);
  910. /* do_mask(chan, chan->channel.exempt, e->mask, 'e');*/
  911. }
  912. }
  913. }
  914. }
  915. /* Make sure that all who are invited on the userlist are actually in fact
  916. * invited on the channel.
  917. *
  918. * Note: Since i was getting an invite list, i assume i'm chop.
  919. */
  920. static void recheck_invites(struct chanset_t *chan)
  921. {
  922. maskrec *ir = NULL;
  923. /* Check global invites in first cycle and channel invites
  924. in second cycle. */
  925. for (int cycle = 0; cycle < 2; cycle++) {
  926. for (ir = cycle ? chan->invites : global_invites; ir; ir = ir->next) {
  927. /* If invite isn't set and (channel is not dynamic invites and not invite
  928. * only) or invite is sticky.
  929. */
  930. if (!isinvited(chan, ir->mask) && ((!channel_dynamicinvites(chan) &&
  931. (chan->channel.mode & CHANINV)) || ir->flags & MASKREC_STICKY))
  932. add_mode(chan, '+', 'I', ir->mask);
  933. /* do_mask(chan, chan->channel.invite, ir->mask, 'I');*/
  934. }
  935. }
  936. }
  937. /* Resets the masks on the channel.
  938. */
  939. static void resetmasks(struct chanset_t *chan, masklist *m, maskrec *mrec, maskrec *global_masks, char mode)
  940. {
  941. if (!me_op(chan))
  942. return; /* Can't do it */
  943. /* Remove masks we didn't put there */
  944. for (; m && m->mask[0]; m = m->next) {
  945. if (!u_equals_mask(global_masks, m->mask) && !u_equals_mask(mrec, m->mask))
  946. add_mode(chan, '-', mode, m->mask);
  947. }
  948. /* Make sure the intended masks are still there */
  949. switch (mode) {
  950. case 'b':
  951. recheck_bans(chan);
  952. break;
  953. case 'e':
  954. recheck_exempts(chan);
  955. break;
  956. case 'I':
  957. recheck_invites(chan);
  958. break;
  959. default:
  960. putlog(LOG_MISC, "*", "(!) Invalid mode '%c' in resetmasks()", mode);
  961. break;
  962. }
  963. }
  964. void check_this_ban(struct chanset_t *chan, char *banmask, bool sticky)
  965. {
  966. if (!me_op(chan))
  967. return;
  968. const char *user = NULL;
  969. for (memberlist *m = chan->channel.member; m && m->nick[0]; m = m->next) {
  970. for (int i = 0; i < (m->userip[0] ? 2 : 1); ++i) {
  971. if (i == 0 && m->userip[0]) // Check userip first in case userhost is already an ip
  972. user = m->fromip;
  973. else
  974. user = m->from;
  975. if ((wild_match(banmask, user) || match_cidr(banmask, user)) &&
  976. !(use_exempts &&
  977. (u_match_mask(global_exempts, user) ||
  978. u_match_mask(chan->exempts, user)))) {
  979. refresh_ban_kick(chan, m, user);
  980. break;
  981. }
  982. }
  983. }
  984. if (!isbanned(chan, banmask) &&
  985. (!channel_dynamicbans(chan) || sticky))
  986. add_mode(chan, '+', 'b', banmask);
  987. }
  988. void check_this_exempt(struct chanset_t *chan, char *mask, bool sticky)
  989. {
  990. if (!isexempted(chan, mask) && (!channel_dynamicexempts(chan) || sticky))
  991. add_mode(chan, '+', 'e', mask);
  992. }
  993. void check_this_invite(struct chanset_t *chan, char *mask, bool sticky)
  994. {
  995. if (!isinvited(chan, mask) && (!channel_dynamicinvites(chan) || sticky))
  996. add_mode(chan, '+', 'I', mask);
  997. }
  998. void recheck_channel_modes(struct chanset_t *chan)
  999. {
  1000. int cur = chan->channel.mode,
  1001. mns = chan->mode_mns_prot,
  1002. pls = chan->mode_pls_prot;
  1003. if (channel_closed(chan)) {
  1004. if (chan->closed_invite) {
  1005. pls |= CHANINV;
  1006. mns &= ~CHANINV;
  1007. }
  1008. if (chan->closed_private) {
  1009. pls |= CHANPRIV;
  1010. mns &= ~CHANPRIV;
  1011. }
  1012. }
  1013. if (channel_voice(chan) && chan->voice_moderate) {
  1014. pls |= CHANMODER;
  1015. mns &= ~CHANMODER;
  1016. }
  1017. if (!(chan->ircnet_status & CHAN_ASKEDMODES)) {
  1018. if (pls & CHANINV && !(cur & CHANINV))
  1019. add_mode(chan, '+', 'i', "");
  1020. else if (mns & CHANINV && cur & CHANINV)
  1021. add_mode(chan, '-', 'i', "");
  1022. if (pls & CHANPRIV && !(cur & CHANPRIV))
  1023. add_mode(chan, '+', 'p', "");
  1024. else if (mns & CHANPRIV && cur & CHANPRIV)
  1025. add_mode(chan, '-', 'p', "");
  1026. if (pls & CHANSEC && !(cur & CHANSEC))
  1027. add_mode(chan, '+', 's', "");
  1028. else if (mns & CHANSEC && cur & CHANSEC)
  1029. add_mode(chan, '-', 's', "");
  1030. if (pls & CHANMODER && !(cur & CHANMODER))
  1031. add_mode(chan, '+', 'm', "");
  1032. else if (mns & CHANMODER && cur & CHANMODER)
  1033. add_mode(chan, '-', 'm', "");
  1034. if (pls & CHANNOCLR && !(cur & CHANNOCLR))
  1035. add_mode(chan, '+', 'c', "");
  1036. else if (mns & CHANNOCLR && cur & CHANNOCLR)
  1037. add_mode(chan, '-', 'c', "");
  1038. if (pls & CHANNOCTCP && !(cur & CHANNOCTCP))
  1039. add_mode(chan, '+', 'C', "");
  1040. else if (mns & CHANNOCTCP && cur & CHANNOCTCP)
  1041. add_mode(chan, '-', 'C', "");
  1042. if (pls & CHANREGON && !(cur & CHANREGON))
  1043. add_mode(chan, '+', 'R', "");
  1044. else if (mns & CHANREGON && cur & CHANREGON)
  1045. add_mode(chan, '-', 'R', "");
  1046. if (pls & CHANMODR && !(cur & CHANMODR))
  1047. add_mode(chan, '+', 'M', "");
  1048. else if (mns & CHANMODR && cur & CHANMODR)
  1049. add_mode(chan, '-', 'M', "");
  1050. if (pls & CHANLONLY && !(cur & CHANLONLY))
  1051. add_mode(chan, '+', 'r', "");
  1052. else if (mns & CHANLONLY && cur & CHANLONLY)
  1053. add_mode(chan, '-', 'r', "");
  1054. if (pls & CHANTOPIC && !(cur & CHANTOPIC))
  1055. add_mode(chan, '+', 't', "");
  1056. else if (mns & CHANTOPIC && cur & CHANTOPIC)
  1057. add_mode(chan, '-', 't', "");
  1058. if (pls & CHANNOMSG && !(cur & CHANNOMSG))
  1059. add_mode(chan, '+', 'n', "");
  1060. else if ((mns & CHANNOMSG) && (cur & CHANNOMSG))
  1061. add_mode(chan, '-', 'n', "");
  1062. if ((pls & CHANANON) && !(cur & CHANANON))
  1063. add_mode(chan, '+', 'a', "");
  1064. else if ((mns & CHANANON) && (cur & CHANANON))
  1065. add_mode(chan, '-', 'a', "");
  1066. if ((pls & CHANQUIET) && !(cur & CHANQUIET))
  1067. add_mode(chan, '+', 'q', "");
  1068. else if ((mns & CHANQUIET) && (cur & CHANQUIET))
  1069. add_mode(chan, '-', 'q', "");
  1070. if ((chan->limit_prot != 0) && (chan->channel.maxmembers == 0)) {
  1071. char s[11] = "";
  1072. simple_snprintf(s, sizeof(s), "%d", chan->limit_prot);
  1073. add_mode(chan, '+', 'l', s);
  1074. } else if ((mns & CHANLIMIT) && (chan->channel.maxmembers != 0))
  1075. add_mode(chan, '-', 'l', "");
  1076. if (chan->key_prot[0]) {
  1077. if (rfc_casecmp(chan->channel.key, chan->key_prot) != 0) {
  1078. if (chan->channel.key[0])
  1079. add_mode(chan, '-', 'k', chan->channel.key);
  1080. add_mode(chan, '+', 'k', chan->key_prot);
  1081. }
  1082. } else if ((mns & CHANKEY) && (chan->channel.key[0]))
  1083. add_mode(chan, '-', 'k', chan->channel.key);
  1084. }
  1085. }
  1086. static void check_this_member(struct chanset_t *chan, memberlist *m,
  1087. struct flag_record *fr)
  1088. {
  1089. if (!m || m->is_me || !me_op(chan))
  1090. return;
  1091. /* +d or bitch and not an op
  1092. * we dont check private because +private does not imply bitch. */
  1093. if (chan_hasop(m) &&
  1094. (chk_deop(*fr, chan) ||
  1095. (!loading && userlist && chan_bitch(chan) && !chk_op(*fr, chan)) ) ) {
  1096. /* if (target_priority(chan, m, 1)) */
  1097. add_mode(chan, '-', 'o', m);
  1098. } else if (!chan_hasop(m) && (chan->role & ROLE_OP) && chk_autoop(m, *fr, chan)) {
  1099. do_op(m, chan, 1, 0);
  1100. }
  1101. if (dovoice(chan)) {
  1102. if (chan_hasvoice(m) && !chan_hasop(m)) {
  1103. /* devoice +q users .. */
  1104. if (chk_devoice(*fr) || (channel_voicebitch(chan) && !chk_voice(m, *fr, chan)))
  1105. add_mode(chan, '-', 'v', m);
  1106. } else if (!chan_hasvoice(m) && !chan_hasop(m)) {
  1107. /* voice +v users */
  1108. if (chk_voice(m, *fr, chan)) {
  1109. add_mode(chan, '+', 'v', m);
  1110. if (m->flags & EVOICE)
  1111. m->flags &= ~EVOICE;
  1112. }
  1113. }
  1114. }
  1115. const char *s = NULL;
  1116. for (int i = 0; i < (m->userip[0] ? 2 : 1); ++i) {
  1117. if (i == 0 && m->userip[0]) // Check userip first in case userhost is already an ip
  1118. s = m->fromip;
  1119. else
  1120. s = m->from;
  1121. /* check vs invites */
  1122. if (use_invites &&
  1123. (u_match_mask(global_invites,s) ||
  1124. u_match_mask(chan->invites, s)))
  1125. refresh_invite(chan, s);
  1126. /* don't kickban if permanent exempted */
  1127. if (!(use_exempts &&
  1128. (u_match_mask(global_exempts, s) ||
  1129. u_match_mask(chan->exempts, s)))) {
  1130. /* Are they banned in the internal list? */
  1131. if (u_match_mask(global_bans, s) || u_match_mask(chan->bans, s))
  1132. refresh_ban_kick(chan, m, s);
  1133. /* are they +k ? */
  1134. if (!chan_sentkick(m) && (chan_kick(*fr) || glob_kick(*fr)) && me_op(chan)) {
  1135. char *p = (char *) get_user(&USERENTRY_COMMENT, m->user);
  1136. check_exemptlist(chan, s);
  1137. quickban(chan, s);
  1138. dprintf(DP_SERVER, "KICK %s %s :%s%s\n", chan->name, m->nick, bankickprefix, p ? p : response(RES_KICKBAN));
  1139. m->flags |= SENTKICK;
  1140. }
  1141. }
  1142. }
  1143. }
  1144. //1 -user
  1145. //2 -host
  1146. void check_this_user(char *hand, int del, char *host)
  1147. {
  1148. memberlist *m = NULL;
  1149. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  1150. for (struct chanset_t *chan = chanset; chan; chan = chan->next) {
  1151. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1152. bool check_member = 0;
  1153. bool had_user = m->user ? 1 : 0;
  1154. struct userrec* u;
  1155. bool matches_hand = false;
  1156. m->tried_getuser = 0;
  1157. member_getuser(m);
  1158. u = m->user;
  1159. if (u) {
  1160. matches_hand = (strcasecmp(u->handle, hand) == 0);
  1161. }
  1162. if (u && u->bot && matches_hand) {
  1163. /* Newly discovered bot, or deleted bot which fullfilled a role,
  1164. * need to rebalance. */
  1165. if (!del || (del && (*chan->bot_roles)[u->handle] != 0)) {
  1166. chan->needs_role_rebalance = 1;
  1167. }
  1168. }
  1169. if (m->user && !had_user) // If a member is newly recognized, act on it
  1170. check_member = 1;
  1171. else if (del != 2 && m->user && matches_hand) { //general check / -user, match specified user
  1172. check_member = 1;
  1173. if (del == 1)
  1174. u = NULL; // Pretend user doesn't exist when checking
  1175. } else if (0 && del == 2) { //-host, may now match on a diff user and need to act on them
  1176. /*
  1177. const char *s = NULL;
  1178. for (int i = 0; i < (m->userip[0] ? 2 : 1); ++i) {
  1179. if (i == 0 && m->userip[0]) // Check userip first in case userhost is already an ip
  1180. s = m->fromip;
  1181. else
  1182. s = m->from;
  1183. if (wild_match(host, s) ||
  1184. (i == 0 && m->userip[0] && match_cidr(host, s))) {
  1185. check_member = 1;
  1186. break;
  1187. }
  1188. }
  1189. */
  1190. if (m->user && !had_user)
  1191. check_member = 1;
  1192. }
  1193. if (check_member) {
  1194. get_user_flagrec(u, &fr, chan->dname, chan);
  1195. check_this_member(chan, m, &fr);
  1196. }
  1197. }
  1198. }
  1199. }
  1200. static void enforce_bitch(struct chanset_t *chan, bool flush = 1) {
  1201. if (!chan || !me_op(chan))
  1202. return;
  1203. priority_do(chan, 1, PRIO_DEOP, flush);
  1204. }
  1205. void enforce_closed(struct chanset_t *chan) {
  1206. if (!chan || !me_op(chan))
  1207. return;
  1208. char buf[3] = "", *p = buf;
  1209. if (chan->closed_invite && !(chan->channel.mode & CHANINV) && !(chan->mode_mns_prot & CHANINV))
  1210. *p++ = 'i';
  1211. if (chan->closed_private && !(chan->channel.mode & CHANPRIV) && !(chan->mode_mns_prot & CHANPRIV))
  1212. *p++ = 'p';
  1213. *p = 0;
  1214. if (buf[0])
  1215. dprintf(DP_MODE, "MODE %s +%s\n", chan->name, buf);
  1216. priority_do(chan, 0, PRIO_KICK);
  1217. }
  1218. inline static char *
  1219. take_massopline(char *op, char **to_op)
  1220. {
  1221. const size_t modes_len = 31, nicks_len = 151;
  1222. char *nicks = (char *) calloc(1, nicks_len),
  1223. *modes = (char *) calloc(1, modes_len),
  1224. *nick = NULL;
  1225. bool useop = 0;
  1226. static char ret[182] = "";
  1227. memset(ret, 0, sizeof(ret));
  1228. for (unsigned int i = 0; i < modesperline; i++) {
  1229. if (*to_op[0] || op) {
  1230. /* if 'op' then use it, then move on to to_op */
  1231. if (!useop && op) {
  1232. nick = op;
  1233. useop = 1;
  1234. } else if (*to_op[0])
  1235. nick = newsplit(to_op);
  1236. if (nick) {
  1237. strlcat(modes, "+o", modes_len);
  1238. strlcat(nicks, nick, nicks_len);
  1239. if (i != modesperline - 1)
  1240. strlcat(nicks, " ", nicks_len);
  1241. }
  1242. }
  1243. }
  1244. strlcat(ret, modes, sizeof(ret));
  1245. strlcat(ret, " ", sizeof(ret));
  1246. strlcat(ret, nicks, sizeof(ret));
  1247. free(modes);
  1248. free(nicks);
  1249. return ret;
  1250. }
  1251. inline static char *
  1252. take_makeline(char *op, char *deops, unsigned int deopn, size_t deops_len)
  1253. {
  1254. bool opn = op ? 1 : 0;
  1255. unsigned int n = opn + deopn; /* op + deops */
  1256. unsigned int pos = randint(deopn), i;
  1257. static char ret[151] = "";
  1258. memset(ret, 0, sizeof(ret));
  1259. for (i = 0; i < n; i++) {
  1260. if (opn && i == pos)
  1261. strlcat(ret, "+o", sizeof(ret));
  1262. else if (deopn)
  1263. strlcat(ret, "-o", sizeof(ret));
  1264. }
  1265. strlcat(ret, " ", sizeof(ret));
  1266. for (i = 0; i < n; i++) {
  1267. if (opn && i == pos)
  1268. strlcat(ret, op, sizeof(ret));
  1269. else if (deopn)
  1270. strlcat(ret, newsplit(&deops), sizeof(ret));
  1271. if (i != n - 1)
  1272. strlcat(ret, " ", sizeof(ret));
  1273. }
  1274. return ret;
  1275. }
  1276. static void
  1277. do_take(struct chanset_t *chan)
  1278. {
  1279. char to_deop[2048] = "", *to_deop_ptr = to_deop;
  1280. char to_op[2048] = "", *to_op_ptr = to_op;
  1281. size_t to_op_len = 0, to_deop_len = 0;
  1282. /* Make lists of who needs to be opped, and who needs to be deopped */
  1283. for (memberlist *m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1284. if (!m->is_me) {
  1285. const bool isbot = m->user && m->user->bot ? 1 : 0;
  1286. /* Avoid countless unneeded operations from strcat/strlen */
  1287. if (isbot && !(m->flags & CHANOP)) {
  1288. to_op_len += strlcpy(to_op + to_op_len, m->nick, sizeof(to_op) - to_op_len);
  1289. *(to_op + to_op_len++) = ' ';
  1290. } else if (!isbot && (m->flags & CHANOP)) {
  1291. to_deop_len += strlcpy(to_deop + to_deop_len, m->nick, sizeof(to_deop) - to_deop_len);
  1292. *(to_deop + to_deop_len++) = ' ';
  1293. }
  1294. }
  1295. }
  1296. shuffle(to_op, " ", sizeof(to_op));
  1297. shuffle(to_deop, " ", sizeof(to_deop));
  1298. size_t deops_len = 0;
  1299. size_t work_len = 0;
  1300. short lines = 0;
  1301. const unsigned short max_lines = floodless ? 15 : default_alines;
  1302. char work[2048] = "", *op = NULL, *modeline = NULL, deops[2048] = "";
  1303. unsigned int deopn;
  1304. while (to_op_ptr[0] || to_deop_ptr[0]) {
  1305. deops_len = 0;
  1306. deopn = 0;
  1307. op = NULL;
  1308. modeline = NULL;
  1309. deops[0] = 0;
  1310. if (to_op_ptr[0])
  1311. op = newsplit(&to_op_ptr);
  1312. /* Prepare a list of modesperline-1 nicks for deop */
  1313. for (unsigned int i = 0; i < modesperline; i++) {
  1314. if (to_deop_ptr[0] && ((i < (modesperline - 1)) || (!op))) {
  1315. ++deopn;
  1316. const char *deop_nick = newsplit(&to_deop_ptr);
  1317. deops_len += strlcpy(deops + deops_len, deop_nick, sizeof(deops) - deops_len);
  1318. *(deops + deops_len++) = ' ';
  1319. }
  1320. }
  1321. deops[deops_len] = 0;
  1322. *(work + work_len++) = 'M';
  1323. *(work + work_len++) = 'O';
  1324. *(work + work_len++) = 'D';
  1325. *(work + work_len++) = 'E';
  1326. *(work + work_len++) = ' ';
  1327. work_len += strlcpy(work + work_len, chan->name, sizeof(work) - work_len);
  1328. *(work + work_len++) = ' ';
  1329. if (deops[0])
  1330. modeline = take_makeline(op, deops, deopn, deops_len);
  1331. else
  1332. modeline = take_massopline(op, &to_op_ptr);
  1333. work_len += strlcpy(work + work_len, modeline, sizeof(work) - work_len);
  1334. *(work + work_len++) = '\r';
  1335. *(work + work_len++) = '\n';
  1336. work[work_len] = 0;
  1337. // Prevent excess flood
  1338. if (++lines >= max_lines) {
  1339. tputs(serv, work, work_len);
  1340. work[0] = 0;
  1341. work_len = 0;
  1342. lines = 0;
  1343. }
  1344. }
  1345. if (work[0])
  1346. tputs(serv, work, work_len);
  1347. if (channel_closed(chan))
  1348. enforce_closed(chan);
  1349. enforce_bitch(chan); /* hell why not! */
  1350. return;
  1351. }
  1352. /* Things to do when i just became a chanop:
  1353. */
  1354. void recheck_channel(struct chanset_t *chan, int dobans)
  1355. {
  1356. static int stacking = 0;
  1357. if (stacking)
  1358. return; /* wewps */
  1359. if (!userlist || loading) /* Bot doesnt know anybody */
  1360. return; /* ... it's better not to deop everybody */
  1361. memberlist *m = NULL;
  1362. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  1363. int stop_reset = 0, botops = 0, nonbotops = 0, botnonops = 0;
  1364. bool flush = 0;
  1365. ++stacking;
  1366. putlog(LOG_DEBUG, "*", "recheck_channel(%s, %d)", chan->dname, dobans);
  1367. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1368. bool hasop = chan_hasop(m);
  1369. if (m) {
  1370. if (m->user && m->user->bot && (m->user->flags & USER_OP)) {
  1371. if (hasop)
  1372. ++botops;
  1373. else
  1374. ++botnonops;
  1375. } else if (hasop)
  1376. ++nonbotops;
  1377. }
  1378. }
  1379. /* don't do much if i'm lonely bot. Maybe they won't notice me? :P */
  1380. if (unlikely(botops == 1 && !botnonops)) {
  1381. if (chan_bitch(chan) || channel_closed(chan))
  1382. putlog(LOG_MISC, "*", "Opped in %s, not checking +closed/+bitch until more bots arrive.", chan->dname);
  1383. } else {
  1384. /* if the chan is +closed, mass deop first, safer that way. */
  1385. if (chan_bitch(chan) || channel_closed(chan)) {
  1386. flush = 1;
  1387. enforce_bitch(chan, 0);
  1388. }
  1389. if (channel_closed(chan))
  1390. enforce_closed(chan);
  1391. }
  1392. /* this can all die, we want to enforce +bitch/+take first :) */
  1393. if (!channel_take(chan)) {
  1394. /* This is a bad hack for +e/+I */
  1395. if (dobans == 2 && chan->channel.members > 1) {
  1396. get_channel_masks(chan);
  1397. }
  1398. //Check +d/+O/+k
  1399. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1400. member_getuser(m);
  1401. get_user_flagrec(m->user, &fr, chan->dname, chan);
  1402. //Already a bot opped, dont bother resetting masks
  1403. if (glob_bot(fr) && chan_hasop(m) && !m->is_me)
  1404. stop_reset = 1;
  1405. check_this_member(chan, m, &fr);
  1406. }
  1407. //Only reset masks if the bot has already received the ban list before (meaning it has already been opped once)
  1408. //Ie, don't set bans without knowing what they are! (asked for above on first op)
  1409. if (dobans && (chan->ircnet_status & CHAN_HAVEBANS)) {
  1410. if (channel_nouserbans(chan) && !stop_reset)
  1411. resetbans(chan);
  1412. else
  1413. recheck_bans(chan);
  1414. if (use_invites && !(chan->ircnet_status & CHAN_ASKED_INVITES)) {
  1415. if (channel_nouserinvites(chan) && !stop_reset)
  1416. resetinvites(chan);
  1417. else
  1418. recheck_invites(chan);
  1419. }
  1420. if (use_exempts && !(chan->ircnet_status & CHAN_ASKED_EXEMPTS)) {
  1421. if (channel_nouserexempts(chan) && !stop_reset)
  1422. resetexempts(chan);
  1423. else
  1424. recheck_exempts(chan);
  1425. } else {
  1426. if (channel_enforcebans(chan))
  1427. enforce_bans(chan);
  1428. }
  1429. flush = 1;
  1430. }
  1431. // Do this here as the above only runs after already having been opped and having gotten bans.
  1432. if (dobans) {
  1433. if ((chan->ircnet_status & CHAN_ASKEDMODES) && !channel_inactive(chan))
  1434. dprintf(DP_MODE, "MODE %s\n", chan->name);
  1435. recheck_channel_modes(chan);
  1436. }
  1437. if (flush)
  1438. flush_mode(chan, QUICK);
  1439. }
  1440. --stacking;
  1441. }
  1442. static int got001(char *from, char *msg)
  1443. {
  1444. //Just connected, cleanup some vars
  1445. chained_who.clear();
  1446. return 0;
  1447. }
  1448. #ifdef CACHE
  1449. /* got 302: userhost
  1450. * <server> 302 <to> :<nick??user@host>
  1451. */
  1452. static int got302(char *from, char *msg)
  1453. {
  1454. char *p = NULL, *nick = NULL, *uhost = NULL;
  1455. cache_t *cache = NULL;
  1456. cache_chan_t *cchan = NULL;
  1457. newsplit(&msg);
  1458. fixcolon(msg);
  1459. p = strchr(msg, '=');
  1460. if (!p)
  1461. p = strchr(msg, '*');
  1462. if (!p)
  1463. return 0;
  1464. *p = 0;
  1465. nick = msg;
  1466. p += 2; /* skip =|* plus the next char */
  1467. uhost = p;
  1468. if ((p = strchr(uhost, ' ')))
  1469. *p = 0;
  1470. if ((cache = cache_find(nick))) {
  1471. if (!cache->uhost[0])
  1472. strlcpy(cache->uhost, uhost, sizeof(cache->uhost));
  1473. if (!cache->handle[0]) {
  1474. char s[UHOSTLEN] = "";
  1475. struct userrec *u = NULL;
  1476. simple_snprintf(s, sizeof(s), "%s!%s", nick, uhost);
  1477. if ((u = get_user_by_host(s)))
  1478. strlcpy(cache->handle, u->handle, sizeof(cache->handle));
  1479. }
  1480. cache->timeval = now;
  1481. /* check if we should invite this client to chans */
  1482. for (cchan = cache->cchan; cchan && cchan->dname[0]; cchan = cchan->next) {
  1483. if (cchan->invite) {
  1484. dprintf(DP_SERVER, "INVITE %s %s\n", nick, cchan->dname);
  1485. cchan->invite = 0;
  1486. cchan->invited = 1;
  1487. }
  1488. if (cchan->ban) {
  1489. cchan->ban = 0;
  1490. dprintf(DP_MODE_NEXT, "MODE %s +b *!%s\n", cchan->dname, uhost);
  1491. }
  1492. }
  1493. }
  1494. return 0;
  1495. }
  1496. #endif
  1497. #ifdef CACHE
  1498. /* got341 invited
  1499. * <server> 341 <to> <nick> <channel>
  1500. */
  1501. static int got341(char *from, char *msg)
  1502. {
  1503. char *nick = NULL, *chname = NULL;
  1504. cache_t *cache = NULL;
  1505. cache_chan_t *cchan = NULL;
  1506. newsplit(&msg);
  1507. nick = newsplit(&msg);
  1508. chname = newsplit(&msg);
  1509. struct chanset_t *chan = findchan(chname);
  1510. if (!chan) {
  1511. putlog(LOG_MISC, "*", "%s: %s", "Hmm, mode info from a channel I'm not on", chname);
  1512. dprintf(DP_SERVER, "PART %s\n", chname);
  1513. return 0;
  1514. }
  1515. cache = cache_find(nick);
  1516. if (cache) {
  1517. for (cchan = cache->cchan; cchan && cchan->dname; cchan = cchan->next) {
  1518. if (!rfc_casecmp(cchan->dname, chan->dname)) {
  1519. if (!cache->uhost[0] || !cchan->invited)
  1520. goto hijack;
  1521. cache->timeval = now;
  1522. notice_invite(chan, cache->handle[0] ? cache->handle : NULL, nick, cache->uhost, cchan->op);
  1523. // cache_del_chan_parm(cache, cache->cchan);
  1524. break;
  1525. }
  1526. }
  1527. }
  1528. if (!cache || !cchan)
  1529. goto hijack;
  1530. return 0;
  1531. hijack:
  1532. if (!cache)
  1533. cache = cache_new(nick);
  1534. if (!cchan)
  1535. cchan = cache_chan_add(cache, chan->dname);
  1536. if (!cache->uhost[0]) {
  1537. dprintf(DP_MODE_NEXT, "MODE %s +b %s!*@*\n", chan->name, nick);
  1538. cchan->ban = 1;
  1539. dprintf(DP_MODE_NEXT, "USERHOST %s\n", nick);
  1540. } else {
  1541. dprintf(DP_MODE_NEXT, "MODE %s +b *!*%s\n", chan->name, cache->uhost);
  1542. }
  1543. putlog(LOG_MISC, "*", "HIJACKED invite detected: %s to %s", nick, chan->dname);
  1544. bd::String msg;
  1545. msg = bd::String::printf("ALERT! \002%s was invited via a hijacked connection/process.\002", nick);
  1546. privmsg(chan->name, msg, DP_MODE_NEXT);
  1547. return 0;
  1548. }
  1549. #endif /* CACHE */
  1550. /* got 710: knock
  1551. * <server> 710 <to> <channel> <from> :<reason?>
  1552. * ratbox :irc.umich.edu 710 #wraith #wraith bryand!bryan@oper.blessed.net :has asked for an invite.
  1553. * csircd :irc.nac.net 710 * bryan_!bryan@pluto.xzibition.com has knocked on channel #wraith for an invite
  1554. *
  1555. */
  1556. static int got710(char *from, char *msg)
  1557. {
  1558. char *chname = NULL;
  1559. struct chanset_t *chan = NULL;
  1560. char buf[UHOSTLEN] = "", *uhost = buf, *nick;
  1561. chname = newsplit(&msg);
  1562. if (!strcmp(chname, "*")) {
  1563. //csircd
  1564. uhost = newsplit(&msg);
  1565. //has knocked on channel #CHAN for an invite
  1566. newsplit(&msg); //has
  1567. newsplit(&msg); //knocked
  1568. newsplit(&msg); //on
  1569. newsplit(&msg); //channel
  1570. chname = newsplit(&msg);
  1571. } else {
  1572. //Hybrid/ratbox
  1573. newsplit(&msg); //not used
  1574. uhost = newsplit(&msg);
  1575. }
  1576. if (!strchr(CHANMETA, chname[0]) || !strchr(uhost, '!'))
  1577. return 0;
  1578. chan = findchan(chname);
  1579. if (!chan->knock_flags || !(chan->role & ROLE_INVITE))
  1580. return 0;
  1581. struct userrec *u = get_user_by_host(uhost);
  1582. if (!u)
  1583. return 0;
  1584. nick = splitnick(&uhost);
  1585. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  1586. get_user_flagrec(u, &fr, chan->dname, chan);
  1587. // PASSING: +o and op || +v and op/voice || user
  1588. if (!((chan->knock_flags == CHAN_FLAG_OP && chk_op(fr, chan)) ||
  1589. (chan->knock_flags == CHAN_FLAG_VOICE && (chk_op(fr, chan) || chk_voice(NULL, fr, chan))) ||
  1590. (chan->knock_flags == CHAN_FLAG_USER)) ||
  1591. chan_kick(fr) || glob_kick(fr)) {
  1592. return 0;
  1593. }
  1594. // dprintf(DP_HELP, "PRIVMSG %s :%s knocked, inviting.. (%s)\n", chname, nick, u ? u->handle : "");
  1595. cache_invite(chan, nick, uhost, u ? u->handle : NULL, 0, 0);
  1596. return 0;
  1597. }
  1598. /* got 324: mode status
  1599. * <server> 324 <to> <channel> <mode>
  1600. */
  1601. static int got324(char *from, char *msg)
  1602. {
  1603. char *chname = NULL;
  1604. newsplit(&msg);
  1605. chname = newsplit(&msg);
  1606. if (match_my_nick(chname))
  1607. return 0;
  1608. struct chanset_t *chan = findchan(chname);
  1609. if (!chan) {
  1610. putlog(LOG_MISC, "*", "%s: %s", "Hmm, mode info from a channel I'm not on", chname);
  1611. dprintf(DP_SERVER, "PART %s\n", chname);
  1612. return 0;
  1613. }
  1614. // Sanity check, there may not be a mode returned if the server is sending bad data.
  1615. if (!msg[0]) return 0;
  1616. int i = 1;
  1617. bool ok = 0;
  1618. char *p = NULL, *q = NULL;
  1619. if (chan->ircnet_status & CHAN_ASKEDMODES)
  1620. ok = 1;
  1621. chan->ircnet_status &= ~CHAN_ASKEDMODES;
  1622. chan->channel.mode = 0;
  1623. while (msg[i] != 0) {
  1624. if (msg[i] == 'i')
  1625. chan->channel.mode |= CHANINV;
  1626. if (msg[i] == 'p')
  1627. chan->channel.mode |= CHANPRIV;
  1628. if (msg[i] == 's')
  1629. chan->channel.mode |= CHANSEC;
  1630. if (msg[i] == 'm')
  1631. chan->channel.mode |= CHANMODER;
  1632. if (msg[i] == 'c')
  1633. chan->channel.mode |= CHANNOCLR;
  1634. if (msg[i] == 'C')
  1635. chan->channel.mode |= CHANNOCTCP;
  1636. if (msg[i] == 'R')
  1637. chan->channel.mode |= CHANREGON;
  1638. if (msg[i] == 'M')
  1639. chan->channel.mode |= CHANMODR;
  1640. if (msg[i] == 'r')
  1641. chan->channel.mode |= CHANLONLY;
  1642. if (msg[i] == 't')
  1643. chan->channel.mode |= CHANTOPIC;
  1644. if (msg[i] == 'n')
  1645. chan->channel.mode |= CHANNOMSG;
  1646. if (msg[i] == 'a')
  1647. chan->channel.mode |= CHANANON;
  1648. if (msg[i] == 'q')
  1649. chan->channel.mode |= CHANQUIET;
  1650. if (msg[i] == 'k') {
  1651. chan->channel.mode |= CHANKEY;
  1652. p = strchr(msg, ' ');
  1653. if (p != NULL) { /* Test for null key assignment */
  1654. p++;
  1655. q = strchr(p, ' ');
  1656. if (q != NULL) {
  1657. *q = 0;
  1658. my_setkey(chan, p);
  1659. strcpy(p, q + 1);
  1660. } else {
  1661. my_setkey(chan, p);
  1662. *p = 0;
  1663. }
  1664. }
  1665. if ((chan->channel.mode & CHANKEY) && (!chan->channel.key[0] ||
  1666. !strcmp("*", chan->channel.key)))
  1667. /* Undernet use to show a blank channel key if one was set when
  1668. * you first joined a channel; however, this has been replaced by
  1669. * an asterisk and this has been agreed upon by other major IRC
  1670. * networks so we'll check for an asterisk here as well
  1671. * (guppy 22Dec2001) */
  1672. chan->ircnet_status |= CHAN_ASKEDMODES;
  1673. }
  1674. if (msg[i] == 'l') {
  1675. p = strchr(msg, ' ');
  1676. if (p != NULL) { /* test for null limit assignment */
  1677. p++;
  1678. q = strchr(p, ' ');
  1679. if (q != NULL) {
  1680. *q = 0;
  1681. chan->channel.maxmembers = atoi(p);
  1682. /* strcpy(p, q + 1); */
  1683. simple_sprintf(p, "%s", q + 1);
  1684. } else {
  1685. chan->channel.maxmembers = atoi(p);
  1686. *p = 0;
  1687. }
  1688. }
  1689. }
  1690. i++;
  1691. }
  1692. if (ok)
  1693. recheck_channel_modes(chan);
  1694. return 0;
  1695. }
  1696. static void memberlist_reposition(struct chanset_t *chan, memberlist *target) {
  1697. /* Move target from it's current position to it's correct sorted position */
  1698. memberlist *old = NULL, *m = NULL;
  1699. if (chan->channel.member == target) {
  1700. chan->channel.member = target->next;
  1701. } else {
  1702. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1703. if (m->next == target) {
  1704. m->next = target->next;
  1705. break;
  1706. }
  1707. }
  1708. }
  1709. target->next = NULL;
  1710. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1711. if (rfc_casecmp(m->nick, target->nick)>0) {
  1712. if (old) {
  1713. target->next = m;
  1714. old->next = target;
  1715. } else {
  1716. target->next = chan->channel.member;
  1717. chan->channel.member = target;
  1718. }
  1719. return;
  1720. }
  1721. old = m;
  1722. }
  1723. if (old) {
  1724. target->next = old->next;
  1725. old->next = target;
  1726. } else {
  1727. target->next = NULL;
  1728. chan->channel.member = target;
  1729. }
  1730. }
  1731. static int got352or4(struct chanset_t *chan, char *user, char *host, char *nick, char *flags, int hops, char* realname, char* ip)
  1732. {
  1733. memberlist *m = ismember(chan, nick); /* in my channel list copy? */
  1734. // bool waschanop = 0;
  1735. // struct chanset_t *ch = NULL;
  1736. // memberlist *ml = NULL;
  1737. if (!m) { /* Nope, so update */
  1738. m = newmember(chan, nick); /* Get a new channel entry */
  1739. m->last = now; /* Last time I saw him */
  1740. /* Store the userhost */
  1741. simple_snprintf(m->userhost, sizeof(m->userhost), "%s@%s", user, host);
  1742. simple_snprintf(m->from, sizeof(m->from), "%s!%s", m->nick, m->userhost);
  1743. member_update_from_cache(chan, m);
  1744. if (!m->userip[0]) {
  1745. if (ip)
  1746. simple_snprintf(m->userip, sizeof(m->userip), "%s@%s", user, ip);
  1747. else if (is_dotted_ip(host))
  1748. simple_snprintf(m->userip, sizeof(m->userip), "%s@%s", user, host);
  1749. simple_snprintf(m->fromip, sizeof(m->fromip), "%s!%s", m->nick, m->userip);
  1750. }
  1751. }
  1752. m->hops = hops;
  1753. /* Propagate hops to other channel memlists... might save us a WHO #chan */
  1754. /* FIXME: great concept, HORRIBLE CPU USAGE
  1755. for (ch = chanset; ch; ch = ch->next) {
  1756. if (ch != chan) {
  1757. for (ml = ch->channel.member; ml && ml->nick[0]; ml = ml->next) {
  1758. if (!strcmp(ml->nick, m->nick)) {
  1759. ml->hops = m->hops;
  1760. break;
  1761. }
  1762. }
  1763. }
  1764. }
  1765. */
  1766. // waschanop = me_op(chan); /* Am I opped here? */
  1767. if (strchr(flags, '@') != NULL) /* Flags say he's opped? */
  1768. m->flags |= (CHANOP | WASOP); /* Yes, so flag in my table */
  1769. else
  1770. m->flags &= ~(CHANOP | WASOP);
  1771. if (strchr(flags, '*'))
  1772. m->flags |= OPER;
  1773. else
  1774. m->flags &= ~OPER;
  1775. if (strchr(flags, '+') != NULL) /* Flags say he's voiced? */
  1776. m->flags |= CHANVOICE; /* Yes */
  1777. else
  1778. m->flags &= ~CHANVOICE;
  1779. // if (!(m->flags & (CHANVOICE | CHANOP)))
  1780. // m->flags |= STOPWHO;
  1781. member_getuser(m);
  1782. //This bot is set +r, so resolve.
  1783. if (unlikely(doresolv(chan))) {
  1784. if (!m->userip[0])
  1785. resolve_to_member(chan, nick, host);
  1786. else if (!m->user && m->userip[0] && channel_rbl(chan))
  1787. resolve_to_rbl(chan, m->userip);
  1788. }
  1789. // If there's an opped bot, queue op to request_op
  1790. if (m->user && m->user->bot && chan_hasop(m)) {
  1791. chan->channel.do_opreq = 1;
  1792. }
  1793. return 0;
  1794. }
  1795. /* got a 352: who info!
  1796. */
  1797. static int got352(char *from, char *msg)
  1798. {
  1799. char *chname = NULL;
  1800. struct chanset_t *chan = NULL;
  1801. newsplit(&msg); /* Skip my nick - effeciently */
  1802. chname = newsplit(&msg); /* Grab the channel */
  1803. chan = findchan(chname); /* See if I'm on channel */
  1804. if (chan) { /* Am I? */
  1805. char *nick = NULL, *user = NULL, *host = NULL, *flags = NULL, *hops = NULL, *realname = NULL;
  1806. int real_hops = 1;
  1807. user = newsplit(&msg); /* Grab the user */
  1808. host = newsplit(&msg); /* Grab the host */
  1809. newsplit(&msg); /* skip the server */
  1810. nick = newsplit(&msg); /* Grab the nick */
  1811. flags = newsplit(&msg); /* Grab the flags */
  1812. hops = newsplit(&msg); /* grab server hops */
  1813. if (hops[0] == ':')
  1814. ++hops; /* Skip the : */
  1815. real_hops = atoi(hops);
  1816. realname = newsplit(&msg); /* realname/gecos */
  1817. got352or4(chan, user, host, nick, flags, real_hops, realname, NULL);
  1818. }
  1819. return 0;
  1820. }
  1821. /* got a 354: who info! - iru style
  1822. */
  1823. static int got354(char *from, char *msg)
  1824. {
  1825. if (use_354) {
  1826. newsplit(&msg); /* Skip my nick - effeciently */
  1827. if (msg[0] && (strchr(CHANMETA, msg[0]) != NULL)) {
  1828. char *chname = newsplit(&msg); /* Grab the channel */
  1829. struct chanset_t *chan = findchan(chname); /* See if I'm on channel */
  1830. if (chan) { /* Am I? */
  1831. char *nick = NULL, *user = NULL, *host = NULL, *flags = NULL, *hops = NULL, *realname = NULL, *ip = NULL;
  1832. int real_hops = 1;
  1833. user = newsplit(&msg); /* Grab the user */
  1834. ip = newsplit(&msg); /** Get the numeric IP :) */
  1835. host = newsplit(&msg); /* Grab the host */
  1836. nick = newsplit(&msg); /* Grab the nick */
  1837. flags = newsplit(&msg); /* Grab the flags */
  1838. hops = newsplit(&msg); /* server hops */
  1839. if (hops[0] == ':')
  1840. ++hops; /* Skip the : */
  1841. real_hops = atoi(hops);
  1842. realname = newsplit(&msg); /* realname/gecos */
  1843. got352or4(chan, user, host, nick, flags, real_hops, realname, ip);
  1844. }
  1845. }
  1846. }
  1847. return 0;
  1848. }
  1849. /* got 315: end of who
  1850. * <server> 315 <to> <chan> :End of /who
  1851. */
  1852. static int got315(char *from, char *msg)
  1853. {
  1854. char *chname = NULL;
  1855. struct chanset_t *chan = NULL;
  1856. newsplit(&msg);
  1857. chname = newsplit(&msg);
  1858. putlog(LOG_DEBUG, "*", "END who %s", chname);
  1859. if (!chained_who.empty()) {
  1860. // Send off next WHO request
  1861. while (1) {
  1862. if (chained_who.empty()) break;
  1863. // Dequeue the next chan in the chain
  1864. chan = findchan(chained_who.front().c_str());
  1865. chained_who.pop_front();
  1866. if (chan) {
  1867. if (!strcmp(chan->name, chname)) continue; // First reply got queued too
  1868. if (!shouldjoin(chan)) continue; // No longer care about this channel
  1869. // Somehow got the WHO already
  1870. if (channel_active(chan) && !channel_pending(chan)) continue;
  1871. send_chan_who(chained_who_idx, chan);
  1872. break;
  1873. }
  1874. }
  1875. }
  1876. chan = findchan(chname);
  1877. /* May have left the channel before the who info came in */
  1878. if (!chan || !channel_pending(chan) || !shouldjoin(chan))
  1879. return 0;
  1880. /* Finished getting who list, can now be considered officially ON CHANNEL */
  1881. chan->ircnet_status |= CHAN_ACTIVE;
  1882. chan->ircnet_status &= ~(CHAN_PEND | CHAN_JOINING);
  1883. memberlist* me = ismember(chan, botname);
  1884. /* Am *I* on the channel now? if not, well d0h. */
  1885. if (!me) {
  1886. putlog(LOG_MISC | LOG_JOIN, chan->dname, "Oops, I'm not really on %s.", chan->dname);
  1887. force_join_chan(chan);
  1888. } else {
  1889. me->is_me = 1;
  1890. me->user = conf.bot->u;
  1891. me->tried_getuser = 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. m->user = conf.bot->u;
  2463. m->tried_getuser = 1;
  2464. /* It was me joining! Need to update the channel record with the
  2465. * unique name for the channel (as the server see's it). <cybah>
  2466. */
  2467. strlcpy(chan->name, chname, sizeof(chan->name));
  2468. chan->ircnet_status &= ~CHAN_JUPED;
  2469. /* ... and log us joining. Using chan->dname for the channel is
  2470. * important in this case. As the config file will never contain
  2471. * logs with the unique name.
  2472. */
  2473. if (chname[0] == '!')
  2474. irc_log(chan, "Joined. (%s)", chname);
  2475. else
  2476. irc_log(chan, "Joined.");
  2477. if (!match_my_nick(chname))
  2478. reset_chan_info(chan);
  2479. } else {
  2480. irc_log(chan, "Join: %s (%s)", nick, uhost);
  2481. detect_chan_flood(m, from, chan, FLOOD_JOIN);
  2482. /* New bot available for roles, rebalance. */
  2483. if (is_bot(m->user)) {
  2484. chan->needs_role_rebalance = 1;
  2485. }
  2486. set_handle_laston(chan->dname, m->user, now);
  2487. }
  2488. }
  2489. /* ok, the op-on-join,etc, tests...first only both if Im opped */
  2490. if (me_op(chan)) {
  2491. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  2492. get_user_flagrec(m->user, &fr, chan->dname, chan);
  2493. bool is_op = chk_op(fr, chan);
  2494. /* Check for a mass join */
  2495. if (chan->role & ROLE_FLOOD &&
  2496. !splitjoin && chan->flood_mjoin_time && chan->flood_mjoin_thr && !is_op) {
  2497. if (chan->channel.drone_jointime < now - chan->flood_mjoin_time) { //expired, reset counter
  2498. chan->channel.drone_joins = 0;
  2499. }
  2500. ++chan->channel.drone_joins;
  2501. chan->channel.drone_jointime = now;
  2502. if (!chan->channel.drone_set_mode && chan->channel.drone_joins >= chan->flood_mjoin_thr) { //flood from dronenet, let's attempt to set +im
  2503. lockdown_chan(chan, FLOOD_MASSJOIN);
  2504. }
  2505. }
  2506. /* Check for and reset exempts and invites.
  2507. *
  2508. * This will require further checking to account for when to use the
  2509. * various modes.
  2510. */
  2511. if (u_match_mask(global_invites,from) ||
  2512. u_match_mask(chan->invites, from))
  2513. refresh_invite(chan, from);
  2514. if (chan->role & ROLE_BAN &&
  2515. !(use_exempts && (u_match_mask(global_exempts,from) || u_match_mask(chan->exempts, from)))) {
  2516. if (channel_enforcebans(chan) && !chan_sentkick(m) && !is_op &&
  2517. !(use_exempts && (isexempted(chan, from) || (chan->ircnet_status & CHAN_ASKED_EXEMPTS)))) {
  2518. for (masklist* b = chan->channel.ban; b->mask[0]; b = b->next) {
  2519. if (wild_match(b->mask, from) || match_cidr(b->mask, from)) {
  2520. dprintf(DP_SERVER, "KICK %s %s :%s%s\n", chname, m->nick, bankickprefix, r_banned(chan));
  2521. m->flags |= SENTKICK;
  2522. return 0;
  2523. }
  2524. }
  2525. }
  2526. /* If it matches a ban, dispose of them. */
  2527. if (u_match_mask(global_bans, from) || u_match_mask(chan->bans, from)) {
  2528. refresh_ban_kick(chan, m, from);
  2529. /* Likewise for kick'ees */
  2530. } else if (!chan_sentkick(m) && (glob_kick(fr) || chan_kick(fr))) {
  2531. check_exemptlist(chan, from);
  2532. quickban(chan, from);
  2533. dprintf(DP_MODE, "KICK %s %s :%s%s\n", chname, m->nick, bankickprefix, response(RES_KICKBAN));
  2534. m->flags |= SENTKICK;
  2535. }
  2536. }
  2537. bool op = 0;
  2538. #ifdef CACHE
  2539. cache_t *cache = cache_find(nick);
  2540. if (cache) {
  2541. cache_chan_t *cchan = NULL;
  2542. if (strcasecmp(cache->uhost, m->userhost)) {
  2543. }
  2544. for (cchan = cache->cchan; cchan && cchan->dname[0]; cchan = cchan->next) {
  2545. if (!rfc_casecmp(cchan->dname, chan->dname)) {
  2546. if (cchan->op) {
  2547. op = 1;
  2548. cchan->op = 0;
  2549. }
  2550. break;
  2551. }
  2552. }
  2553. }
  2554. #endif /* CACHE */
  2555. if (!splitjoin) {
  2556. /* Autoop */
  2557. if (!chan_hasop(m) &&
  2558. (op ||
  2559. ((chan->role & ROLE_OP) && chk_autoop(m, fr, chan)))) {
  2560. do_op(m, chan, 1, 0);
  2561. }
  2562. /* +v or +voice */
  2563. if (!chan_hasvoice(m) && !glob_bot(fr) && dovoice(chan)) {
  2564. if (m->user) {
  2565. if (!(m->flags & EVOICE) &&
  2566. (
  2567. /* +voice: Voice all clients who are not flag:+q. If the chan is +voicebitch, only op flag:+v clients */
  2568. (channel_voice(chan) && !chk_devoice(fr) && (!channel_voicebitch(chan) || (channel_voicebitch(chan) && chk_voice(m, fr, chan)))) ||
  2569. /* Or, if the channel is -voice but they still qualify to be voiced */
  2570. (!channel_voice(chan) && !privchan(fr, chan, PRIV_VOICE) && chk_voice(m, fr, chan))
  2571. )
  2572. ) {
  2573. m->delay = now + chan->auto_delay;
  2574. m->flags |= SENTVOICE;
  2575. }
  2576. } else if (!m->user && channel_voice(chan) && !channel_voicebitch(chan) && voice_ok(m, chan)) {
  2577. m->delay = now + chan->auto_delay;
  2578. m->flags |= SENTVOICE;
  2579. }
  2580. }
  2581. }
  2582. }
  2583. }
  2584. }
  2585. return 0;
  2586. }
  2587. /* Got a part
  2588. */
  2589. static int gotpart(char *from, char *msg)
  2590. {
  2591. char *nick = NULL, *chname = NULL;
  2592. struct chanset_t *chan = NULL;
  2593. char buf[UHOSTLEN] = "", *uhost = buf;
  2594. chname = newsplit(&msg);
  2595. fixcolon(chname);
  2596. fixcolon(msg);
  2597. chan = findchan(chname);
  2598. strlcpy(uhost, from, sizeof(buf));
  2599. nick = splitnick(&uhost);
  2600. if (chan && !shouldjoin(chan) && match_my_nick(nick)) {
  2601. irc_log(chan, "Parting");
  2602. clear_channel(chan, 1);
  2603. return 0;
  2604. }
  2605. if (chan && !channel_pending(chan)) {
  2606. memberlist* m = ismember(chan, nick);
  2607. struct userrec *u = (m && m->user) ? m->user : get_user_by_host(from);
  2608. if (!channel_active(chan)) {
  2609. /* whoa! */
  2610. putlog(LOG_ERRORS, "*", "confused bot: guess I'm on %s and didn't realize it", chan->dname);
  2611. chan->ircnet_status |= CHAN_ACTIVE;
  2612. chan->ircnet_status &= ~(CHAN_PEND | CHAN_JOINING);
  2613. reset_chan_info(chan);
  2614. }
  2615. /* This bot fullfilled a role, need to rebalance. */
  2616. if (u && u->bot && (*chan->bot_roles)[u->handle] != 0) {
  2617. chan->needs_role_rebalance = 1;
  2618. }
  2619. set_handle_laston(chan->dname, u, now);
  2620. if (m) {
  2621. detect_chan_flood(m, from, chan, FLOOD_PART);
  2622. killmember(chan, nick);
  2623. }
  2624. if (msg[0])
  2625. irc_log(chan, "Part: %s (%s) [%s]", nick, uhost, msg);
  2626. else
  2627. irc_log(chan, "Part: %s (%s)", nick, uhost);
  2628. /* If it was me, all hell breaks loose... */
  2629. if (match_my_nick(nick)) {
  2630. check_rejoin(chan);
  2631. } else
  2632. check_lonely_channel(chan);
  2633. }
  2634. return 0;
  2635. }
  2636. /* Got a kick
  2637. */
  2638. static int gotkick(char *from, char *origmsg)
  2639. {
  2640. char buf2[511], *msg = buf2, *chname = NULL;
  2641. struct chanset_t *chan = NULL;
  2642. strlcpy(buf2, origmsg, sizeof(buf2));
  2643. msg = buf2;
  2644. chname = newsplit(&msg);
  2645. chan = findchan(chname);
  2646. if (!chan)
  2647. return 0;
  2648. char *nick = newsplit(&msg);
  2649. if (match_my_nick(nick) && channel_pending(chan)) {
  2650. check_rejoin(chan);
  2651. return 0; /* rejoin if kicked before getting needed info <Wcc[08/08/02]> */
  2652. }
  2653. if (channel_active(chan)) {
  2654. char *whodid = NULL, buf[UHOSTLEN] = "", *uhost = buf;
  2655. memberlist *m = NULL, *mv = NULL;
  2656. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  2657. fixcolon(msg);
  2658. strlcpy(uhost, from, sizeof(buf));
  2659. whodid = splitnick(&uhost);
  2660. chan = findchan(chname);
  2661. if (!chan)
  2662. return 0;
  2663. m = ismember(chan, whodid);
  2664. if (m) {
  2665. detect_chan_flood(m, from, chan, FLOOD_KICK);
  2666. m->last = now;
  2667. member_getuser(m);
  2668. if (m->user) {
  2669. /* This _needs_ to use chan->dname <cybah> */
  2670. get_user_flagrec(m->user, &fr, chan->dname, chan);
  2671. set_handle_laston(chan->dname, m->user, now);
  2672. }
  2673. }
  2674. if ((!m || !m->user) || (m && m->user && !m->user->bot)) {
  2675. chan->channel.fighting++;
  2676. }
  2677. mv = ismember(chan, nick);
  2678. member_getuser(mv);
  2679. if (mv->user) {
  2680. // Revenge kick clients that kick our bots
  2681. if (chan->revenge && !mv->is_me && m && m != mv && mv->user->bot && !(m->user && m->user->bot)) {
  2682. if ((chan->role & ROLE_REVENGE) && !chan_sentkick(m) && me_op(chan)) {
  2683. m->flags |= SENTKICK;
  2684. dprintf(DP_MODE_NEXT, "KICK %s %s :%s%s\r\n", chan->name, m->nick, kickprefix, response(RES_REVENGE));
  2685. } else {
  2686. if (m->user) {
  2687. char tmp[128] = "";
  2688. simple_snprintf(tmp, sizeof(tmp), "Kicked bot %s on %s", mv->nick, chan->dname);
  2689. deflag_user(m->user, DEFLAG_EVENT_REVENGE_KICK, tmp, chan);
  2690. }
  2691. }
  2692. }
  2693. set_handle_laston(chan->dname, mv->user, now);
  2694. /* This bot fullfilled a role, need to rebalance. */
  2695. if (mv->user->bot && (*chan->bot_roles)[mv->user->handle] != 0) {
  2696. chan->needs_role_rebalance = 1;
  2697. }
  2698. }
  2699. irc_log(chan, "%s!%s was kicked by %s (%s)", mv->nick, mv->userhost, from, msg);
  2700. /* Kicked ME?!? the sods! */
  2701. if (mv->is_me) {
  2702. check_rejoin(chan);
  2703. } else {
  2704. killmember(chan, nick);
  2705. check_lonely_channel(chan);
  2706. }
  2707. }
  2708. return 0;
  2709. }
  2710. /* Got a nick change
  2711. */
  2712. static int gotnick(char *from, char *msg)
  2713. {
  2714. char *nick = NULL, s1[UHOSTLEN] = "", buf[UHOSTLEN] = "", *uhost = buf;
  2715. memberlist *m = NULL, *mm = NULL;
  2716. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  2717. strlcpy(uhost, from, sizeof(buf));
  2718. nick = splitnick(&uhost);
  2719. fixcolon(msg);
  2720. irc_log(NULL, "[%s] Nick change: %s -> %s", samechans(nick, ","), nick, msg);
  2721. const RfcString rfc_nick(nick);
  2722. clear_chanlist_member(rfc_nick); /* Cache for nick 'nick' is meaningless now. */
  2723. auto new_rfc_nick = std::make_shared<RfcString>(msg);
  2724. Auth *auth = Auth::Find(uhost);
  2725. if (auth)
  2726. auth->NewNick(*new_rfc_nick);
  2727. /* Compose a nick!user@host for the new nick */
  2728. simple_snprintf(s1, sizeof(s1), "%s!%s", msg, uhost);
  2729. /* Users can match by nick, so a recheck is needed */
  2730. struct userrec *u = get_user_by_host(s1);
  2731. for (struct chanset_t *chan = chanset; chan; chan = chan->next) {
  2732. m = ismember(chan, rfc_nick);
  2733. if (m) {
  2734. m->user = u;
  2735. m->last = now;
  2736. /* Not just a capitalization change */
  2737. if (rfc_casecmp(nick, msg)) {
  2738. /* Someone on channel with old nick?! */
  2739. if ((mm = ismember(chan, *new_rfc_nick)))
  2740. killmember(chan, mm->nick, false);
  2741. }
  2742. chan->channel.hashed_members->remove(*m->rfc_nick);
  2743. strlcpy(m->nick, msg, sizeof(m->nick));
  2744. m->rfc_nick = new_rfc_nick;
  2745. (*chan->channel.hashed_members)[*m->rfc_nick] = m;
  2746. strlcpy(m->from, s1, sizeof(m->from));
  2747. /*
  2748. * Banned?
  2749. */
  2750. memberlist_reposition(chan, m);
  2751. detect_chan_flood(m, from, chan, FLOOD_NICK);
  2752. /* Any pending kick or mode to the old nick is lost. */
  2753. m->flags &= ~(SENTKICK | SENTDEOP | SENTOP |
  2754. SENTVOICE | SENTDEVOICE);
  2755. /* nick-ban or nick is +k or something? */
  2756. get_user_flagrec(m->user, &fr, chan->dname, chan);
  2757. check_this_member(chan, m, &fr);
  2758. }
  2759. }
  2760. return 0;
  2761. }
  2762. void check_should_cycle(struct chanset_t *chan)
  2763. {
  2764. if (!me_op(chan))
  2765. return;
  2766. //If there are other ops split off and i'm the only op on this side of split, cycle
  2767. int localops = 0, localbotops = 0, splitops = 0, splitbotops = 0, localnonops = 0;
  2768. for (memberlist *ml = chan->channel.member; ml && ml->nick[0]; ml = ml->next) {
  2769. if (chan_hasop(ml)) {
  2770. if (chan_issplit(ml)) {
  2771. splitops++;
  2772. if ((ml->user) && ml->user->bot)
  2773. splitbotops++;
  2774. } else {
  2775. localops++;
  2776. if ((ml->user) && ml->user->bot)
  2777. localbotops++;
  2778. if (localbotops >= 2)
  2779. return;
  2780. if (localops > localbotops)
  2781. return;
  2782. }
  2783. } else {
  2784. if (!chan_issplit(ml))
  2785. localnonops++;
  2786. }
  2787. }
  2788. if (splitbotops > 5) {
  2789. sdprintf("Cycling %s", chan->dname);
  2790. /* I'm only one opped here... and other side has some ops... so i'm cycling */
  2791. if (localnonops) {
  2792. /* need to unset any +kil first */
  2793. 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 : "");
  2794. dprintf(DP_MODE, "PART %s\n", chan->name[0] ? chan->name : chan->dname);
  2795. } else
  2796. dprintf(DP_MODE, "PART %s\n", chan->name[0] ? chan->name : chan->dname);
  2797. }
  2798. }
  2799. /* Signoff, similar to part.
  2800. */
  2801. static int gotquit(char *from, char *msg)
  2802. {
  2803. char *nick = NULL, *p = NULL;
  2804. bool split = 0;
  2805. memberlist *m = NULL;
  2806. char from2[NICKMAX + UHOSTMAX + 1] = "";
  2807. struct userrec *u = NULL;
  2808. strlcpy(from2, from, sizeof(from2));
  2809. #ifdef TCL
  2810. u = get_user_by_host(from2);
  2811. #endif
  2812. nick = splitnick(&from);
  2813. fixcolon(msg);
  2814. /* Fred1: Instead of expensive wild_match on signoff, quicker method.
  2815. * Determine if signoff string matches "%.% %.%", and only one
  2816. * space.
  2817. */
  2818. p = strchr(msg, ' ');
  2819. if (p && (p == strrchr(msg, ' '))) {
  2820. char *z1 = NULL, *z2 = NULL;
  2821. *p = 0;
  2822. z1 = strchr(p + 1, '.');
  2823. z2 = strchr(msg, '.');
  2824. if (z1 && z2 && (*(z1 + 1) != 0) && (z1 - 1 != p) &&
  2825. (z2 + 1 != p) && (z2 != msg)) {
  2826. /* Server split, or else it looked like it anyway (no harm in
  2827. * assuming)
  2828. */
  2829. split = 1;
  2830. } else
  2831. *p = ' ';
  2832. }
  2833. if (msg[0])
  2834. irc_log(NULL, "[%s] Quits %s (%s) (%s)", samechans(nick, ","), nick, from, msg);
  2835. else
  2836. irc_log(NULL, "[%s] Quits %s (%s)", samechans(nick, ","), nick, from);
  2837. for (struct chanset_t* chan = chanset; chan; chan = chan->next) {
  2838. if (!channel_active(chan))
  2839. continue;
  2840. m = ismember(chan, nick);
  2841. if (m) {
  2842. member_getuser(m);
  2843. u = m->user;
  2844. if (u) {
  2845. if (u->bot) {
  2846. counter_clear(u->handle);
  2847. /* This bot fullfilled a role, need to rebalance. */
  2848. if ((*chan->bot_roles)[u->handle] != 0) {
  2849. chan->needs_role_rebalance = 1;
  2850. }
  2851. }
  2852. set_handle_laston(chan->dname, u, now); /* If you remove this, the bot will crash when the user record in question
  2853. is removed/modified during the tcl binds below, and the users was on more
  2854. than one monitored channel */
  2855. }
  2856. if (split) {
  2857. m->split = now;
  2858. ++(chan->channel.splitmembers);
  2859. irc_log(chan, "%s (%s) got netsplit.", nick, from);
  2860. } else {
  2861. killmember(chan, nick);
  2862. check_lonely_channel(chan);
  2863. }
  2864. if (channel_cycle(chan))
  2865. check_should_cycle(chan);
  2866. }
  2867. }
  2868. /* Our nick quit? if so, grab it.
  2869. */
  2870. if (keepnick && !match_my_nick(nick))
  2871. nicks_available(nick);
  2872. set_fish_key(nick, "");
  2873. #ifdef CACHE
  2874. /* see if they were in our cache at all */
  2875. cache_t *cache = cache_find(nick);
  2876. if (cache)
  2877. cache_del(nick, cache);
  2878. #endif /* CACHE */
  2879. return 0;
  2880. }
  2881. /* Got a channel message.
  2882. */
  2883. static int gotmsg(char *from, char *msg)
  2884. {
  2885. if (!strchr("&#!+@$", msg[0]))
  2886. return 0;
  2887. bool ignoring = match_ignore(from);
  2888. char *to = newsplit(&msg), *realto = (to[0] == '@') ? to + 1 : to;
  2889. struct chanset_t *chan = findchan(realto);
  2890. if (!chan)
  2891. return 0; /* Private msg to an unknown channel?? */
  2892. char buf[UHOSTLEN] = "", *nick = NULL, buf2[512] = "", *uhost = buf, *p = NULL, *p1 = NULL, *code = NULL, *ctcp = NULL;
  2893. int ctcp_count = 0;
  2894. struct userrec *u = NULL;
  2895. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  2896. fixcolon(msg);
  2897. strlcpy(uhost, from, sizeof(buf));
  2898. nick = splitnick(&uhost);
  2899. memberlist *m = ismember(chan, nick);
  2900. /* Only check if flood-ctcp is active */
  2901. if (m && flood_ctcp.count && detect_avalanche(msg)) {
  2902. member_getuser(m);
  2903. u = m->user;
  2904. get_user_flagrec(u, &fr, chan->dname, chan);
  2905. /* Discard -- kick user if it was to the channel */
  2906. if (m && me_op(chan) &&
  2907. !chan_sentkick(m) &&
  2908. !(use_exempts && ban_fun &&
  2909. /* don't kickban if permanent exempted -- Eule */
  2910. (u_match_mask(global_exempts, from) ||
  2911. u_match_mask(chan->exempts, from)))) {
  2912. if (ban_fun) {
  2913. check_exemptlist(chan, from);
  2914. u_addmask('b', chan, quickban(chan, from), conf.bot->nick,
  2915. "that was fun, let's do it again!", now + (60 * chan->ban_time), 0);
  2916. }
  2917. if (kick_fun) {
  2918. /* This can induce kickflood - arthur2 */
  2919. dprintf(DP_SERVER, "KICK %s %s :%sthat was fun, let's do it again!\n", chan->name, nick, kickprefix);
  2920. m->flags |= SENTKICK;
  2921. }
  2922. }
  2923. if (!ignoring) {
  2924. putlog(LOG_MODES, "*", "Avalanche from %s!%s in %s - ignoring",
  2925. nick, uhost, chan->dname);
  2926. p = strchr(uhost, '@');
  2927. if (p)
  2928. p++;
  2929. else
  2930. p = uhost;
  2931. simple_snprintf(buf2, sizeof(buf2), "*!*@%s", p);
  2932. addignore(buf2, conf.bot->nick, "ctcp avalanche", now + (60 * ignore_time));
  2933. }
  2934. return 0;
  2935. }
  2936. /* Check for CTCP: */
  2937. ctcp_reply[0] = 0;
  2938. p = strchr(msg, 1);
  2939. while (p && *p) {
  2940. p++;
  2941. p1 = p;
  2942. while ((*p != 1) && *p)
  2943. p++;
  2944. if (*p == 1) {
  2945. *p = 0;
  2946. ctcp = buf2;
  2947. strlcpy(ctcp, p1, sizeof(buf2));
  2948. strcpy(p1 - 1, p + 1);
  2949. if (m) {
  2950. detect_chan_flood(m, from, chan, strncmp(ctcp, "ACTION ", 7) ? FLOOD_CTCP : FLOOD_PRIVMSG);
  2951. detect_chan_flood(m, from, chan, FLOOD_BYTES, msg);
  2952. }
  2953. /* Respond to the first answer_ctcp */
  2954. p = strchr(msg, 1);
  2955. if (ctcp_count < answer_ctcp) {
  2956. ctcp_count++;
  2957. if (ctcp[0] != ' ') {
  2958. code = newsplit(&ctcp);
  2959. u = get_user_by_host(from);
  2960. if (!ignoring || trigger_on_ignore) {
  2961. if (check_bind_ctcp(nick, uhost, u, to, code, ctcp) == BIND_RET_LOG) {
  2962. update_idle(chan->dname, nick);
  2963. }
  2964. /* Log DCC, it's to a channel damnit! */
  2965. if (!strcmp(code, "ACTION")) {
  2966. irc_log(chan, "* %s %s", nick, ctcp);
  2967. } else {
  2968. irc_log(chan, "CTCP %s: from %s (%s) to %s: %s", code, nick, from, to, ctcp);
  2969. }
  2970. }
  2971. }
  2972. }
  2973. }
  2974. }
  2975. /* Send out possible ctcp responses */
  2976. if (ctcp_reply[0]) {
  2977. if (ctcp_mode != 2) {
  2978. notice(nick, ctcp_reply, DP_HELP);
  2979. } else {
  2980. if (now - last_ctcp > flood_ctcp.time) {
  2981. notice(nick, ctcp_reply, DP_HELP);
  2982. count_ctcp = 1;
  2983. } else if (count_ctcp < flood_ctcp.count) {
  2984. notice(nick, ctcp_reply, DP_HELP);
  2985. count_ctcp++;
  2986. }
  2987. last_ctcp = now;
  2988. }
  2989. }
  2990. if (msg[0]) {
  2991. int botmatch = 0;
  2992. char *my_msg = NULL, *my_ptr = NULL, *fword = NULL;
  2993. if (me_op(chan) && doflood(chan))
  2994. detect_offense(m, chan, msg);
  2995. if (m) {
  2996. detect_chan_flood(m, from, chan, FLOOD_PRIVMSG);
  2997. detect_chan_flood(m, from, chan, FLOOD_BYTES, msg);
  2998. }
  2999. if (auth_chan) {
  3000. my_msg = my_ptr = strdup(msg);
  3001. fword = newsplit(&my_msg); /* take out first word */
  3002. /* the first word is a wildcard match to our nick. */
  3003. botmatch = wild_match(fword, botname);
  3004. if (botmatch && strcmp(fword, "*"))
  3005. fword = newsplit(&my_msg); /* now this will be the command */
  3006. /* is it a cmd? */
  3007. if (auth_prefix[0] && fword && fword[0] && fword[1] && ((botmatch && fword[0] != auth_prefix[0]) || (fword[0] == auth_prefix[0]))) {
  3008. Auth *auth = Auth::Find(uhost);
  3009. if (auth && auth->Authed()) {
  3010. if (fword[0] == auth_prefix[0])
  3011. fword++;
  3012. auth->atime = now;
  3013. check_bind_authc(fword, auth, chan->dname, my_msg);
  3014. }
  3015. }
  3016. free(my_ptr);
  3017. }
  3018. irc_log(chan, "%s<%s> %s", to[0] == '@' ? "@" : "", nick, msg);
  3019. update_idle(chan->dname, nick);
  3020. }
  3021. return 0;
  3022. }
  3023. /* Got a private notice.
  3024. */
  3025. static int gotnotice(char *from, char *msg)
  3026. {
  3027. if (!strchr(CHANMETA "@", *msg))
  3028. return 0;
  3029. bool ignoring = match_ignore(from);
  3030. char *to = newsplit(&msg), *realto = (*to == '@') ? to + 1 : to;
  3031. struct chanset_t *chan = NULL;
  3032. chan = findchan(realto);
  3033. if (!chan)
  3034. return 0; /* Notice to an unknown channel?? */
  3035. char *nick = NULL, buf2[512] = "", *p = NULL, *p1 = NULL, buf[512] = "", *uhost = buf;
  3036. char *ctcp = NULL, *code = NULL;
  3037. struct userrec *u = NULL;
  3038. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  3039. fixcolon(msg);
  3040. strlcpy(uhost, from, sizeof(buf));
  3041. nick = splitnick(&uhost);
  3042. memberlist *m = ismember(chan, nick);
  3043. if (m) {
  3044. member_getuser(m);
  3045. u = m->user;
  3046. }
  3047. if (flood_ctcp.count && detect_avalanche(msg)) {
  3048. get_user_flagrec(u, &fr, chan->dname, chan);
  3049. /* Discard -- kick user if it was to the channel */
  3050. if (me_op(chan) && m && !chan_sentkick(m) &&
  3051. !(use_exempts && ban_fun &&
  3052. /* don't kickban if permanent exempted -- Eule */
  3053. (u_match_mask(global_exempts,from) ||
  3054. u_match_mask(chan->exempts, from)))) {
  3055. if (ban_fun) {
  3056. check_exemptlist(chan, from);
  3057. u_addmask('b', chan, quickban(chan, from), conf.bot->nick,
  3058. "that was fun, let's do it again!", now + (60 * chan->ban_time), 0);
  3059. }
  3060. if (kick_fun) {
  3061. /* This can induce kickflood - arthur2 */
  3062. dprintf(DP_SERVER, "KICK %s %s :%sthat was fun, let's do it again!\n", chan->name, nick, kickprefix);
  3063. m->flags |= SENTKICK;
  3064. }
  3065. }
  3066. if (!ignoring)
  3067. putlog(LOG_MODES, "*", "Avalanche from %s", from);
  3068. return 0;
  3069. }
  3070. /* Check for CTCP: */
  3071. p = strchr(msg, 1);
  3072. while (p && *p) {
  3073. p++;
  3074. p1 = p;
  3075. while ((*p != 1) && *p)
  3076. p++;
  3077. if (*p == 1) {
  3078. *p = 0;
  3079. ctcp = buf2;
  3080. strlcpy(ctcp, p1, sizeof(buf2));
  3081. strcpy(p1 - 1, p + 1);
  3082. p = strchr(msg, 1);
  3083. if (m) {
  3084. detect_chan_flood(m, from, chan, strncmp(ctcp, "ACTION ", 7) ? FLOOD_CTCP : FLOOD_PRIVMSG);
  3085. detect_chan_flood(m, from, chan, FLOOD_BYTES, msg);
  3086. }
  3087. if (ctcp[0] != ' ') {
  3088. code = newsplit(&ctcp);
  3089. if (!ignoring || trigger_on_ignore) {
  3090. check_bind_ctcr(nick, uhost, u, chan->dname, code, msg);
  3091. chan = findchan(realto);
  3092. if (!chan)
  3093. return 0;
  3094. irc_log(chan, "CTCP reply %s: %s from %s (%s) to %s", code, msg, nick, from, chan->dname);
  3095. update_idle(chan->dname, nick);
  3096. }
  3097. }
  3098. }
  3099. }
  3100. if (msg[0]) {
  3101. if (m) {
  3102. detect_chan_flood(m, from, chan, FLOOD_NOTICE);
  3103. detect_chan_flood(m, from, chan, FLOOD_BYTES, msg);
  3104. }
  3105. update_idle(chan->dname, nick);
  3106. if (!ignoring)
  3107. irc_log(chan, "-%s:%s- %s", nick, to, msg);
  3108. }
  3109. return 0;
  3110. }
  3111. static cmd_t irc_raw[] =
  3112. {
  3113. {"001", "", (Function) got001, "irc:001", LEAF},
  3114. #ifdef CACHE
  3115. {"302", "", (Function) got302, "irc:302", LEAF},
  3116. {"341", "", (Function) got341, "irc:341", LEAF},
  3117. #endif /* CACHE */
  3118. {"324", "", (Function) got324, "irc:324", LEAF},
  3119. {"352", "", (Function) got352, "irc:352", LEAF},
  3120. {"354", "", (Function) got354, "irc:354", LEAF},
  3121. {"315", "", (Function) got315, "irc:315", LEAF},
  3122. {"367", "", (Function) got367, "irc:367", LEAF},
  3123. {"368", "", (Function) got368, "irc:368", LEAF},
  3124. {"403", "", (Function) got403, "irc:403", LEAF},
  3125. {"405", "", (Function) got405, "irc:405", LEAF},
  3126. {"471", "", (Function) got471, "irc:471", LEAF},
  3127. {"473", "", (Function) got473, "irc:473", LEAF},
  3128. {"474", "", (Function) got474, "irc:474", LEAF},
  3129. {"475", "", (Function) got475, "irc:475", LEAF},
  3130. {"478", "", (Function) got478, "irc:478", LEAF},
  3131. {"INVITE", "", (Function) gotinvite, "irc:invite", LEAF},
  3132. {"TOPIC", "", (Function) gottopic, "irc:topic", LEAF},
  3133. {"331", "", (Function) got331, "irc:331", LEAF},
  3134. {"332", "", (Function) got332, "irc:332", LEAF},
  3135. {"JOIN", "", (Function) gotjoin, "irc:join", LEAF},
  3136. {"PART", "", (Function) gotpart, "irc:part", LEAF},
  3137. {"KICK", "", (Function) gotkick, "irc:kick", LEAF},
  3138. {"NICK", "", (Function) gotnick, "irc:nick", LEAF},
  3139. {"QUIT", "", (Function) gotquit, "irc:quit", LEAF},
  3140. {"PRIVMSG", "", (Function) gotmsg, "irc:msg", LEAF},
  3141. {"NOTICE", "", (Function) gotnotice, "irc:notice", LEAF},
  3142. {"MODE", "", (Function) gotmode, "irc:mode", LEAF},
  3143. {"346", "", (Function) got346, "irc:346", LEAF},
  3144. {"347", "", (Function) got347, "irc:347", LEAF},
  3145. {"348", "", (Function) got348, "irc:348", LEAF},
  3146. {"349", "", (Function) got349, "irc:349", LEAF},
  3147. {"353", "", (Function) got353, "irc:353", LEAF},
  3148. {"710", "", (Function) got710, "irc:710", LEAF},
  3149. {NULL, NULL, NULL, NULL, 0}
  3150. };
  3151. /* vim: set sts=2 sw=2 ts=8 et: */