chan.c 94 KB

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