chan.c 87 KB

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