chan.c 87 KB

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