chan.c 85 KB

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