chan.c 88 KB

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