chan.c 94 KB

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