irc.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522
  1. /*
  2. * irc.c -- part of irc.mod
  3. * support for channels within the bot
  4. *
  5. */
  6. #include "src/common.h"
  7. #define MAKING_IRC
  8. #include "irc.h"
  9. #include "src/match.h"
  10. #include "src/settings.h"
  11. #include "src/tandem.h"
  12. #include "src/net.h"
  13. #include "src/botnet.h"
  14. #include "src/botmsg.h"
  15. #include "src/main.h"
  16. #include "src/response.h"
  17. #include "src/set.h"
  18. #include "src/userrec.h"
  19. #include "src/misc.h"
  20. #include "src/rfc1459.h"
  21. #include "src/chanprog.h"
  22. #include "src/auth.h"
  23. #include "src/userrec.h"
  24. #include "src/tclhash.h"
  25. #include "src/userent.h"
  26. #include "src/egg_timer.h"
  27. #include "src/mod/share.mod/share.h"
  28. #include "src/mod/server.mod/server.h"
  29. #include "src/mod/channels.mod/channels.h"
  30. #include "src/mod/ctcp.mod/ctcp.h"
  31. #include <stdarg.h>
  32. #define PRIO_DEOP 1
  33. #define PRIO_KICK 2
  34. #ifdef CACHE
  35. static cache_t *irccache = NULL;
  36. #endif /* CACHE */
  37. static time_t last_eI; /* this will stop +e and +I from being checked over and over if the bot is stuck in a
  38. * -o+o loop for some reason, hence possibly causing a SENDQ kill
  39. */
  40. #define do_eI (((now - last_eI) > 30) ? 1 : 0)
  41. static int net_type = 0;
  42. static time_t wait_split = 300; /* Time to wait for user to return from
  43. * net-split. */
  44. int max_bans; /* Modified by net-type 1-4 */
  45. int max_exempts;
  46. int max_invites;
  47. int max_modes; /* Modified by net-type 1-4 */
  48. static bool bounce_bans = 0;
  49. static bool bounce_exempts = 0;
  50. static bool bounce_invites = 0;
  51. static bool bounce_modes = 0;
  52. unsigned int modesperline; /* Number of modes per line to send. */
  53. static size_t mode_buf_len = 200; /* Maximum bytes to send in 1 mode. */
  54. bool use_354 = 0; /* Use ircu's short 354 /who
  55. * responses. */
  56. static bool kick_fun = 0;
  57. static bool ban_fun = 1;
  58. static bool keepnick = 1; /* Keep nick */
  59. static bool prevent_mixing = 1; /* To prevent mixing old/new modes */
  60. static bool include_lk = 1; /* For correct calculation
  61. * in real_add_mode. */
  62. #include "chan.c"
  63. #include "mode.c"
  64. #include "cmdsirc.c"
  65. #include "msgcmds.c"
  66. static int
  67. voice_ok(memberlist *m, struct chanset_t *chan)
  68. {
  69. if (m->flags & EVOICE)
  70. return 0;
  71. if (m->userhost[0] && !chan->voice_non_ident && m->userhost[0] == '~')
  72. return 0;
  73. return 1;
  74. }
  75. static void
  76. detect_autokick(char *nick, char *uhost, struct chanset_t *chan, char *msg)
  77. {
  78. if (!nick || !nick[0] || !uhost || !uhost[0] || !chan || !msg || !msg[0])
  79. return;
  80. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  81. struct userrec *u = get_user_by_host(uhost);
  82. int i = 0;
  83. //size_t tot = strlen(msg);
  84. get_user_flagrec(u, &fr, chan->dname);
  85. for (; *msg; ++msg) {
  86. if (egg_isupper(*msg))
  87. i++;
  88. }
  89. /* if ((chan->capslimit)) { */
  90. while (((msg) && *msg)) {
  91. if (egg_isupper(*msg))
  92. i++;
  93. msg++;
  94. }
  95. /*
  96. if (chan->capslimit && ((i / tot) >= chan->capslimit)) {
  97. dprintf(DP_MODE, "PRIVMSG %s :flood stats for %s: %d/%d are CAP, percentage: %d\n", chan->name, nick, i, tot, (i/tot)*100);
  98. if ((((i / tot) * 100) >= 50)) {
  99. dprintf(DP_HELP, "PRIVMSG %s :cap flood.\n", chan->dname);
  100. }
  101. */
  102. }
  103. void notice_invite(struct chanset_t *chan, char *handle, char *nick, char *uhost, bool op) {
  104. char fhandle[21] = "";
  105. const char *ops = " (auto-op)";
  106. if (handle)
  107. simple_sprintf(fhandle, "\002%s\002 ", handle);
  108. putlog(LOG_MISC, "*", "Invited %s%s(%s%s%s) to %s.", handle ? handle : "", handle ? " " : "", nick, uhost ? "!" : "", uhost ? uhost : "", chan->dname);
  109. dprintf(DP_MODE, "PRIVMSG %s :\001ACTION has invited %s(%s%s%s) to %s.%s\001\n",
  110. chan->name, fhandle, nick, uhost ? "!" : "", uhost ? uhost : "", chan->dname, op ? ops : "");
  111. }
  112. #ifdef CACHE
  113. static cache_t *cache_new(char *nick)
  114. {
  115. cache_t *cache = (cache_t *) my_calloc(1, sizeof(cache_t));
  116. cache->next = NULL;
  117. strcpy(cache->nick, nick);
  118. cache->uhost[0] = 0;
  119. cache->handle[0] = 0;
  120. // cache->user = NULL;
  121. cache->timeval = now;
  122. cache->cchan = NULL;
  123. list_append((struct list_type **) &irccache, (struct list_type *) cache);
  124. return cache;
  125. }
  126. static cache_chan_t *cache_chan_add(cache_t *cache, char *chname)
  127. {
  128. cache_chan_t *cchan = (cache_chan_t *) my_calloc(1, sizeof(cache_chan_t));
  129. cchan->next = NULL;
  130. strcpy(cchan->dname, chname);
  131. cchan->ban = 0;
  132. cchan->invite = 0;
  133. cchan->invited = 0;
  134. list_append((struct list_type **) &(cache->cchan), (struct list_type *) cchan);
  135. return cchan;
  136. }
  137. static void cache_chan_find(cache_t *cache, cache_chan_t *cchan, char *nick, char *chname)
  138. {
  139. if (!cache)
  140. cache = cache_find(nick);
  141. if (cache) {
  142. for (cchan = cache->cchan; cchan && cchan->dname[0]; cchan = cchan->next) {
  143. if (!rfc_casecmp(cchan->dname, chname)) {
  144. return;
  145. }
  146. }
  147. }
  148. return;
  149. }
  150. static void cache_chan_del(char *nick, char *chname) {
  151. cache_t *cache = NULL;
  152. cache_chan_t *cchan = NULL;
  153. cache_chan_find(cache, cchan, nick, chname);
  154. if (cchan) {
  155. list_delete((struct list_type **) &cache->cchan, (struct list_type *) cchan);
  156. free(cache);
  157. }
  158. }
  159. static cache_t *cache_find(char *nick)
  160. {
  161. cache_t *cache = NULL;
  162. for (cache = irccache; cache && cache->nick[0]; cache = cache->next)
  163. if (!rfc_casecmp(cache->nick, nick))
  164. break;
  165. return cache;
  166. }
  167. static void cache_del(char *nick, cache_t *cache)
  168. {
  169. if (!cache)
  170. cache = cache_find(nick);
  171. if (cache) {
  172. list_delete((struct list_type **) &irccache, (struct list_type *) cache);
  173. free(cache);
  174. }
  175. }
  176. static void cache_debug(void)
  177. {
  178. cache_t *cache = NULL;
  179. cache_chan_t *cchan = NULL;
  180. for (cache = irccache; cache && cache->nick[0]; cache = cache->next) {
  181. dprintf(DP_MODE, "PRIVMSG #wraith :%s!%s (%s)\n", cache->nick, cache->uhost, cache->handle);
  182. for (cchan = cache->cchan; cchan && cchan->dname[0]; cchan = cchan->next)
  183. dprintf(DP_MODE, "PRIVMSG #wraith :%s %d %d %d\n", cchan->dname, cchan->ban, cchan->invite, cchan->invited);
  184. }
  185. }
  186. #endif /* CACHE */
  187. static void cache_invite(struct chanset_t *chan, char *nick, char *host, char *handle, bool op, bool bot)
  188. {
  189. #ifdef CACHE
  190. cache_t *cache = NULL;
  191. if ((cache = cache_find(nick)) == NULL)
  192. cache = cache_new(nick);
  193. cache->bot = bot;
  194. /* if we find they have a host but it doesnt match the new host, wipe it */
  195. if (host && cache->uhost[0] && egg_strcasecmp(cache->uhost, host))
  196. cache->uhost[0] = 0;
  197. if (host && !cache->uhost[0])
  198. strcpy(cache->uhost, host);
  199. /* if we find they have a handle but it doesnt match the new handle, wipe it */
  200. if (handle && cache->handle[0] && egg_strcasecmp(cache->handle, handle))
  201. cache->handle[0] = 0;
  202. if (handle && !cache->handle[0])
  203. strcpy(cache->handle, handle);
  204. cache_chan_t *cchan = cache_chan_add(cache, chan->dname);
  205. cchan->op = op;
  206. if (!host) {
  207. cchan->invite = 1;
  208. dprintf(DP_MODE, "USERHOST %s\n", nick);
  209. return;
  210. }
  211. /* if we have a uhost already, it's safe to invite them */
  212. cchan->invited = 1;
  213. cchan->invite = 0;
  214. #endif /* CACHE */
  215. dprintf(DP_SERVER, "INVITE %s %s\n", nick, chan->name);
  216. }
  217. static char *
  218. makecookie(char *chn, char *bnick)
  219. {
  220. char *buf = NULL, randstring[5] = "", ts[11] = "", *chname = NULL, *hash = NULL, tohash[50] = "";
  221. chname = strdup(chn);
  222. make_rand_str(randstring, 4);
  223. /* &ts[4] is now last 6 digits of time */
  224. sprintf(ts, "%010li", now + timesync);
  225. /* Only use first 3 chars of chan */
  226. if (strlen(chname) > 2)
  227. chname[3] = 0;
  228. strtoupper(chname);
  229. strtolower(bnick);
  230. simple_sprintf(tohash, "%c%s%s%s%s%c", settings.salt2[0], bnick, chname, &ts[4], randstring, settings.salt2[15]);
  231. hash = MD5(tohash);
  232. buf = (char *) my_calloc(1, 20);
  233. simple_sprintf(buf, "%c%c%c!%s@%s", hash[8], hash[16], hash[18], randstring, ts);
  234. /* sprintf(buf, "%c/%s!%s@%X", hash[16], randstring, ts, BIT31); */
  235. free(chname);
  236. return buf;
  237. }
  238. static int
  239. checkcookie(char *chn, char *bnick, char *cookie)
  240. {
  241. char randstring[5] = "", ts[11] = "", *chname = NULL, *hash = NULL, tohash[50] = "", *p = NULL;
  242. time_t optime = 0;
  243. chname = strdup(chn);
  244. p = cookie;
  245. p += 4; /* has! */
  246. strlcpy(randstring, p, sizeof(randstring));
  247. p += 5; /* rand@ */
  248. /* &ts[4] is now last 6 digits of time */
  249. strlcpy(ts, p, sizeof(ts));
  250. optime = atol(ts);
  251. /* Only use first 3 chars of chan */
  252. if (strlen(chname) > 2)
  253. chname[3] = 0;
  254. strtoupper(chname);
  255. /* hash!rand@ts */
  256. strtolower(bnick);
  257. simple_sprintf(tohash, "%c%s%s%s%s%c", settings.salt2[0], bnick, chname, &ts[4], randstring, settings.salt2[15]);
  258. hash = MD5(tohash);
  259. if (!(hash[8] == cookie[0] && hash[16] == cookie[1] && hash[18] == cookie[2]))
  260. return BC_HASH;
  261. if (((now + timesync) - optime) > 1800)
  262. return BC_SLACK;
  263. return 0;
  264. }
  265. /*
  266. opreq = o #chan nick
  267. inreq = i #chan nick uhost
  268. inreq keyreply = K #chan key
  269. */
  270. void
  271. getin_request(char *botnick, char *code, char *par)
  272. {
  273. if (!server_online)
  274. return;
  275. if (!par[0] || !par)
  276. return;
  277. struct chanset_t *chan = NULL;
  278. char *what = newsplit(&par), *chname = newsplit(&par);
  279. if (!chname[0] || !chname)
  280. return;
  281. if (!(chan = findchan_by_dname(chname))) {
  282. putlog(LOG_GETIN, "*", "getin req from %s for %s which is not a valid channel!", botnick, chname);
  283. return;
  284. }
  285. char *tmp = newsplit(&par); /* nick */
  286. if (!tmp[0])
  287. return;
  288. struct userrec *u = NULL;
  289. memberlist *mem = NULL;
  290. char nick[NICKLEN] = "", uhost[UHOSTLEN] = "", uip[UHOSTLEN] = "";
  291. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  292. strlcpy(nick, tmp, sizeof(nick));
  293. tmp = newsplit(&par); /* userhost */
  294. if (tmp[0])
  295. strlcpy(uhost, tmp, sizeof(uhost));
  296. tmp = newsplit(&par); /* userip */
  297. if (tmp[0])
  298. egg_snprintf(uip, sizeof uip, "%s!%s", nick, tmp);
  299. /*
  300. * if (!ismember(chan, botname)) {
  301. * putlog(LOG_GETIN, "*", "getin req from %s for %s - I'm not on %s!", botnick, chname, chname);
  302. * return;
  303. * }
  304. */
  305. const char *type = what[0] == 'o' ? "op" : "in", *desc = what[0] == 'o' ? "on" : "for";
  306. u = get_user_by_handle(userlist, botnick);
  307. if (!u) {
  308. putlog(LOG_GETIN, "*", "%sreq from %s/%s %s %s - No user called %s in userlist", type, botnick, nick, desc,
  309. chan->dname, botnick);
  310. return;
  311. }
  312. if (server_lag > lag_threshold) {
  313. putlog(LOG_GETIN, "*", "%sreq from %s/%s %s %s - I'm too lagged", type, botnick, nick, desc, chan->dname);
  314. return;
  315. }
  316. if (what[0] != 'K') {
  317. if (!(channel_pending(chan) || channel_active(chan))) {
  318. putlog(LOG_GETIN, "*", "%sreq from %s/%s %s %s - I'm not on %s now", type, botnick, nick, desc,
  319. chan->dname, chan->dname);
  320. return;
  321. }
  322. }
  323. if (!chan) {
  324. putlog(LOG_GETIN, "*", "%sreq from %s/%s %s %s - Channel %s doesn't exist", type, botnick, nick,
  325. desc, chname, chname);
  326. return;
  327. }
  328. mem = ismember(chan, nick);
  329. if (mem && chan_issplit(mem)) {
  330. putlog(LOG_GETIN, "*", "%sreq from %s/%s %s %s - %s is split", type, botnick, nick, desc, chan->dname, nick);
  331. return;
  332. }
  333. if (what[0] == 'o') {
  334. if (!mem) {
  335. putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - %s isn't on %s", botnick, nick, chan->dname, nick, chan->dname);
  336. return;
  337. }
  338. if (!mem->user && !mem->tried_getuser) {
  339. mem->user = get_user_by_host(uhost);
  340. mem->tried_getuser = 1;
  341. }
  342. if (mem->user != u) {
  343. putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - %s doesn't match %s", botnick, nick, chan->dname, nick, botnick);
  344. return;
  345. }
  346. get_user_flagrec(u, &fr, chan->dname);
  347. if (!chk_op(fr, chan)) {
  348. putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - %s doesnt have +o for chan.", botnick, nick, chan->dname, botnick);
  349. return;
  350. }
  351. if (chan_hasop(mem)) {
  352. putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - %s already has ops", botnick, nick, chan->dname, nick);
  353. return;
  354. }
  355. if (!me_op(chan)) {
  356. putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - I haven't got ops", botnick, nick, chan->dname);
  357. return;
  358. }
  359. if (chan_sentop(mem)) {
  360. putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - Already sent a +o", botnick, nick, chan->dname);
  361. return;
  362. }
  363. if (chan->channel.no_op) {
  364. if (chan->channel.no_op > now) /* dont op until this time has passed */
  365. return;
  366. else
  367. chan->channel.no_op = 0;
  368. }
  369. do_op(nick, chan, 0, 1);
  370. putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - Opped", botnick, nick, chan->dname);
  371. } else if (what[0] == 'i') {
  372. if (mem) {
  373. putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - %s is already on %s", botnick, nick, chan->dname, nick, chan->dname);
  374. return;
  375. }
  376. #ifdef no
  377. if (mem->user != u) {
  378. putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - %s doesn't match %s", botnick, nick, chan->dname, nick, botnick);
  379. return;
  380. }
  381. #endif
  382. get_user_flagrec(u, &fr, chan->dname);
  383. if (!chk_op(fr, chan) || chan_kick(fr) || glob_kick(fr)) {
  384. putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - %s doesn't have acces for chan.", botnick, nick, chan->dname, botnick);
  385. return;
  386. }
  387. char s[256] = "", *p = NULL, *p2 = NULL, *p3 = NULL;
  388. bool sendi = 0;
  389. strcpy(s, getchanmode(chan));
  390. p = (char *) &s;
  391. p2 = newsplit(&p);
  392. p3 = newsplit(&p);
  393. if (strchr(p2, 'l')) {
  394. if (!me_op(chan)) {
  395. putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - I haven't got ops", botnick, nick, chan->dname);
  396. return;
  397. } else {
  398. int lim = chan->channel.members + 5, curlim;
  399. if (!*p)
  400. curlim = atoi(p3);
  401. else
  402. curlim = atoi(p);
  403. if (curlim > 0 && curlim < lim) {
  404. char s2[16] = "";
  405. sendi = 1;
  406. simple_sprintf(s2, "%d", lim);
  407. add_mode(chan, '+', 'l', s2);
  408. putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - Raised limit to %d", botnick, nick, chan->dname, lim);
  409. }
  410. }
  411. }
  412. struct maskrec **mr = NULL, *tmr = NULL;
  413. mr = &global_bans;
  414. while (*mr) {
  415. if (wild_match((*mr)->mask, uhost) ||
  416. wild_match((*mr)->mask, uip) ||
  417. match_cidr((*mr)->mask, uip)) {
  418. if (!noshare) {
  419. shareout("-m b %s\n", (*mr)->mask);
  420. }
  421. putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - Removed permanent global ban %s", botnick, nick,
  422. chan->dname, (*mr)->mask);
  423. /*gban_total--; */
  424. free((*mr)->mask);
  425. if ((*mr)->desc)
  426. free((*mr)->desc);
  427. if ((*mr)->user)
  428. free((*mr)->user);
  429. tmr = *mr;
  430. *mr = (*mr)->next;
  431. free(tmr);
  432. } else {
  433. mr = &((*mr)->next);
  434. }
  435. }
  436. mr = &chan->bans;
  437. while (*mr) {
  438. if (wild_match((*mr)->mask, uhost) ||
  439. wild_match((*mr)->mask, uip) ||
  440. match_cidr((*mr)->mask, uip)) {
  441. if (!noshare) {
  442. shareout("-mc b %s %s\n", chan->dname, (*mr)->mask);
  443. }
  444. putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - Removed permanent channel ban %s", botnick, nick,
  445. chan->dname, (*mr)->mask);
  446. free((*mr)->mask);
  447. if ((*mr)->desc)
  448. free((*mr)->desc);
  449. if ((*mr)->user)
  450. free((*mr)->user);
  451. tmr = *mr;
  452. *mr = (*mr)->next;
  453. free(tmr);
  454. } else {
  455. mr = &((*mr)->next);
  456. }
  457. }
  458. for (struct maskstruct *b = chan->channel.ban; b->mask[0]; b = b->next) {
  459. if (wild_match(b->mask, uhost) ||
  460. wild_match(b->mask, uip) ||
  461. match_cidr(b->mask, uip)) {
  462. add_mode(chan, '-', 'b', b->mask);
  463. putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - Removed active ban %s", botnick, nick, chan->dname,
  464. b->mask);
  465. sendi = 1;
  466. }
  467. }
  468. if (strchr(p2, 'k')) {
  469. sendi = 0;
  470. tmp = (char *) my_calloc(1, strlen(chan->dname) + strlen(p3) + 7);
  471. simple_sprintf(tmp, "gi K %s %s", chan->dname, p3);
  472. putbot(botnick, tmp);
  473. putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - Sent key (%s)", botnick, nick, chan->dname, p3);
  474. free(tmp);
  475. }
  476. if (strchr(p2, 'i')) {
  477. if (!me_op(chan)) {
  478. putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - I haven't got ops", botnick, nick, chan->dname);
  479. return;
  480. } else {
  481. sendi = 1;
  482. putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - Invited", botnick, nick, chan->dname);
  483. }
  484. }
  485. if (sendi && me_op(chan))
  486. dprintf(DP_MODE, "INVITE %s %s\n", nick, chan->name);
  487. } else if (what[0] == 'K') {
  488. if (!chan) {
  489. putlog(LOG_GETIN, "*", "Got key for nonexistant channel %s from %s", chname, botnick);
  490. return;
  491. }
  492. if (!shouldjoin(chan)) {
  493. putlog(LOG_GETIN, "*", "Got key for %s from %s - I shouldn't be on that chan?!?", chan->dname, botnick);
  494. } else {
  495. if (!(channel_pending(chan) || channel_active(chan))) {
  496. putlog(LOG_GETIN, "*", "Got key for %s from %s (%s) - Joining", chan->dname, botnick, nick);
  497. dprintf(DP_MODE, "JOIN %s %s\n", chan->dname, nick);
  498. chan->status |= CHAN_JOINING;
  499. } else {
  500. putlog(LOG_GETIN, "*", "Got key for %s from %s - I'm already in the channel", chan->dname, botnick);
  501. }
  502. }
  503. }
  504. }
  505. static void
  506. request_op(struct chanset_t *chan)
  507. {
  508. if (!chan || (chan && (channel_pending(chan) || !shouldjoin(chan) || !channel_active(chan) || me_op(chan))))
  509. return;
  510. if (chan->channel.no_op) {
  511. if (chan->channel.no_op > now) /* dont op until this time has passed */
  512. return;
  513. else
  514. chan->channel.no_op = 0;
  515. }
  516. chan->channel.do_opreq = 0;
  517. /* check server lag */
  518. if (server_lag > lag_threshold) {
  519. putlog(LOG_GETIN, "*", "Not asking for ops on %s - I'm too lagged", chan->dname);
  520. return;
  521. }
  522. int i = 0, my_exp = 0, first = 100;
  523. time_t n = now;
  524. /* max OPREQ_COUNT requests per OPREQ_SECONDS sec */
  525. while (i < 5) {
  526. if (n - chan->opreqtime[i] > op_requests.time) {
  527. if (first > i)
  528. first = i;
  529. my_exp++;
  530. chan->opreqtime[i] = 0;
  531. }
  532. i++;
  533. }
  534. if ((5 - my_exp) >= op_requests.count) {
  535. putlog(LOG_GETIN, "*", "Delaying opreq for %s - Maximum of %d:%li reached", chan->dname, op_requests.count,
  536. op_requests.time);
  537. return;
  538. }
  539. int cnt = op_bots, i2;
  540. memberlist *ml = NULL;
  541. memberlist *botops[MAX_BOTS];
  542. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  543. char s[UHOSTLEN] = "";
  544. for (i = 0, ml = chan->channel.member; (i < MAX_BOTS) && ml && ml->nick[0]; ml = ml->next) {
  545. /* If bot, linked, global op & !split & chanop & (chan is reserver | bot isn't +a) ->
  546. * add to temp list */
  547. if (!ml->user && !ml->tried_getuser) {
  548. simple_sprintf(s, "%s!%s", ml->nick, ml->userhost);
  549. ml->user = get_user_by_host(s);
  550. ml->tried_getuser = 1;
  551. }
  552. if ((i < MAX_BOTS) && ml->user && ml->user->bot &&
  553. findbot(ml->user->handle) && chan_hasop(ml) && !chan_issplit(ml)) {
  554. get_user_flagrec(ml->user, &fr, NULL);
  555. if (chk_op(fr, chan))
  556. botops[i++] = ml;
  557. }
  558. }
  559. if (!i) {
  560. if (channel_active(chan) && !channel_pending(chan))
  561. putlog(LOG_GETIN, "*", "No one to ask for ops on %s", chan->dname);
  562. return;
  563. }
  564. char *l = (char *) my_calloc(1, cnt * 50);
  565. /* first scan for bots on my server, ask first found for ops */
  566. simple_sprintf(s, "gi o %s %s", chan->dname, botname);
  567. /* look for bots 0-1 hops away */
  568. for (i2 = 0; i2 < i; i2++) {
  569. if (botops[i2]->hops < 2) {
  570. putbot(botops[i2]->user->handle, s);
  571. chan->opreqtime[first] = n;
  572. if (l[0]) {
  573. strcat(l, ", ");
  574. strcat(l, botops[i2]->user->handle);
  575. } else {
  576. strcpy(l, botops[i2]->user->handle);
  577. }
  578. strcat(l, "/");
  579. strcat(l, botops[i2]->nick);
  580. botops[i2] = NULL;
  581. cnt--;
  582. break;
  583. }
  584. }
  585. /* Pick random op and ask for ops */
  586. while (cnt) {
  587. i2 = randint(i);
  588. if (botops[i2]) {
  589. putbot(botops[i2]->user->handle, s);
  590. chan->opreqtime[first] = n;
  591. if (l[0]) {
  592. strcat(l, ", ");
  593. strcat(l, botops[i2]->user->handle);
  594. } else {
  595. strcpy(l, botops[i2]->user->handle);
  596. }
  597. strcat(l, "/");
  598. strcat(l, botops[i2]->nick);
  599. cnt--;
  600. botops[i2] = NULL;
  601. } else {
  602. if (i < op_bots)
  603. cnt--;
  604. }
  605. }
  606. putlog(LOG_GETIN, "*", "Requested ops on %s from %s", chan->dname, l);
  607. free(l);
  608. }
  609. static void
  610. request_in(struct chanset_t *chan)
  611. {
  612. int i = 0;
  613. struct userrec *botops[MAX_BOTS];
  614. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  615. for (struct userrec *u = userlist; u && (i < MAX_BOTS); u = u->next) {
  616. get_user_flagrec(u, &fr, NULL);
  617. if (bot_hublevel(u) == 999 && glob_bot(fr) && chk_op(fr, chan)
  618. #ifdef G_BACKUP
  619. && (!glob_backupbot(fr) || channel_backup(chan))
  620. #endif
  621. /* G_BACKUP */
  622. && nextbot(u->handle) >= 0)
  623. botops[i++] = u;
  624. }
  625. if (!i) {
  626. putlog(LOG_GETIN, "*", "No bots linked, can't request help to join %s", chan->dname);
  627. return;
  628. }
  629. int cnt = in_bots, n;
  630. char s[255] = "", *l = (char *) my_calloc(1, cnt * 30);
  631. simple_sprintf(s, "gi i %s %s %s!%s %s", chan->dname, botname, botname, botuserhost, botuserip);
  632. while (cnt) {
  633. n = randint(i);
  634. if (botops[n]) {
  635. putbot(botops[n]->handle, s);
  636. if (l[0]) {
  637. strcat(l, ", ");
  638. strcat(l, botops[n]->handle);
  639. } else {
  640. strcpy(l, botops[n]->handle);
  641. }
  642. botops[n] = NULL;
  643. cnt--;
  644. } else {
  645. if (i < in_bots)
  646. cnt--;
  647. }
  648. }
  649. putlog(LOG_GETIN, "*", "Requesting help to join %s from %s", chan->dname, l);
  650. free(l);
  651. }
  652. /* Contains the logic to decide wether we want to punish someone. Returns
  653. * true (1) if we want to, false (0) if not.
  654. */
  655. static bool
  656. want_to_revenge(struct chanset_t *chan, struct userrec *u,
  657. struct userrec *u2, char *badnick, char *victimstr, bool mevictim)
  658. {
  659. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  660. /* Do not take revenge upon ourselves. */
  661. if (match_my_nick(badnick))
  662. return 0;
  663. get_user_flagrec(u, &fr, chan->dname);
  664. /* Kickee didn't kick themself? */
  665. if (rfc_casecmp(badnick, victimstr)) {
  666. /* They kicked me? */
  667. if (mevictim) {
  668. /* ... and I'm allowed to take revenge? <snicker> */
  669. if (channel_revengebot(chan))
  670. return 1;
  671. /* Do we revenge for our users ... and do we actually know the victim? */
  672. } else if (channel_revenge(chan) && u2) {
  673. struct flag_record fr2 = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  674. get_user_flagrec(u2, &fr2, chan->dname);
  675. /* Protecting friends? */
  676. /* Protecting ops? */
  677. if ((channel_protectops(chan) &&
  678. /* ... and kicked is valid op? */
  679. (chan_op(fr2) || (glob_op(fr2) && !chan_deop(fr2)))))
  680. return 1;
  681. }
  682. }
  683. return 0;
  684. }
  685. /* Dependant on revenge_mode, punish the offender.
  686. */
  687. static void
  688. punish_badguy(struct chanset_t *chan, char *whobad,
  689. struct userrec *u, char *badnick, char *victimstr, bool mevictim, int type)
  690. {
  691. memberlist *m = ismember(chan, badnick);
  692. if (!m)
  693. return;
  694. char reason[1024] = "", ct[81] = "", *kick_msg = NULL;
  695. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  696. get_user_flagrec(u, &fr, chan->dname);
  697. /* Get current time into a string */
  698. egg_strftime(ct, sizeof ct, "%d %b %Z", gmtime(&now));
  699. /* Put together log and kick messages */
  700. reason[0] = 0;
  701. switch (type) {
  702. case REVENGE_KICK:
  703. kick_msg = "don't kick my friends, bud";
  704. simple_sprintf(reason, "kicked %s off %s", victimstr, chan->dname);
  705. break;
  706. case REVENGE_DEOP:
  707. simple_sprintf(reason, "deopped %s on %s", victimstr, chan->dname);
  708. kick_msg = "don't deop my friends, bud";
  709. break;
  710. default:
  711. kick_msg = "revenge!";
  712. }
  713. putlog(LOG_MISC, chan->dname, "Punishing %s (%s)", badnick, reason);
  714. /* Set the offender +d */
  715. if ((chan->revenge_mode > 0) &&
  716. /* ... unless there's no more to do */
  717. !(chan_deop(fr) || glob_deop(fr))) {
  718. char s[UHOSTLEN], s1[UHOSTLEN];
  719. memberlist *mx = NULL;
  720. /* Removing op */
  721. if (chan_op(fr) || (glob_op(fr) && !chan_deop(fr))) {
  722. fr.match = FR_CHAN;
  723. if (chan_op(fr)) {
  724. fr.chan &= ~USER_OP;
  725. } else {
  726. fr.chan |= USER_DEOP;
  727. }
  728. set_user_flagrec(u, &fr, chan->dname);
  729. putlog(LOG_MISC, "*", "No longer opping %s[%s] (%s)", u->handle, whobad, reason);
  730. }
  731. /* ... or just setting to deop */
  732. else if (u) {
  733. /* In the user list already, cool :) */
  734. fr.match = FR_CHAN;
  735. fr.chan |= USER_DEOP;
  736. set_user_flagrec(u, &fr, chan->dname);
  737. simple_sprintf(s, "(%s) %s", ct, reason);
  738. putlog(LOG_MISC, "*", "Now deopping %s[%s] (%s)", u->handle, whobad, s);
  739. }
  740. /* ... or creating new user and setting that to deop */
  741. else {
  742. strcpy(s1, whobad);
  743. maskhost(s1, s);
  744. strcpy(s1, badnick);
  745. /* If that handle exists use "badX" (where X is an increasing number)
  746. * instead.
  747. */
  748. while (get_user_by_handle(userlist, s1)) {
  749. if (!strncmp(s1, "bad", 3)) {
  750. int i;
  751. i = atoi(s1 + 3);
  752. simple_sprintf(s1 + 3, "%d", i + 1);
  753. } else
  754. strcpy(s1, "bad1"); /* Start with '1' */
  755. }
  756. userlist = adduser(userlist, s1, s, "-", 0, 0);
  757. fr.match = FR_CHAN;
  758. fr.chan = USER_DEOP;
  759. u = get_user_by_handle(userlist, s1);
  760. if ((mx = ismember(chan, badnick)))
  761. mx->user = u;
  762. set_user_flagrec(u, &fr, chan->dname);
  763. simple_sprintf(s, "(%s) %s (%s)", ct, reason, whobad);
  764. set_user(&USERENTRY_COMMENT, u, (void *) s);
  765. putlog(LOG_MISC, "*", "Now deopping %s (%s)", whobad, reason);
  766. }
  767. }
  768. /* Always try to deop the offender */
  769. if (!mevictim)
  770. add_mode(chan, '-', 'o', badnick);
  771. /* Ban. Should be done before kicking. */
  772. if (chan->revenge_mode > 2) {
  773. char s[UHOSTLEN] = "", s1[UHOSTLEN] = "";
  774. splitnick(&whobad);
  775. maskhost(whobad, s1);
  776. simple_sprintf(s, "(%s) %s", ct, reason);
  777. u_addmask('b', chan, s1, conf.bot->nick, s, now + (60 * chan->ban_time), 0);
  778. if (!mevictim && me_op(chan)) {
  779. add_mode(chan, '+', 'b', s1);
  780. flush_mode(chan, QUICK);
  781. }
  782. }
  783. /* Kick the offender */
  784. if ((chan->revenge_mode > 1) && !chan_sentkick(m) &&
  785. /* ... and can I actually do anything about it? */
  786. me_op(chan) && !mevictim) {
  787. dprintf(DP_MODE, "KICK %s %s :%s%s\n", chan->name, m->nick, bankickprefix, response(RES_MEAN));
  788. m->flags |= SENTKICK;
  789. }
  790. }
  791. /* Punishes bad guys under certain circumstances using methods as defined
  792. * by the revenge_mode flag.
  793. */
  794. static void
  795. maybe_revenge(struct chanset_t *chan, char *whobad, char *whovictim, int type)
  796. {
  797. if (!chan || (type < 0))
  798. return;
  799. /* Get info about offender */
  800. struct userrec *u = get_user_by_host(whobad), *u2 = NULL;
  801. char *badnick = splitnick(&whobad), *victimstr = NULL;
  802. bool mevictim;
  803. /* Get info about victim */
  804. u2 = get_user_by_host(whovictim);
  805. victimstr = splitnick(&whovictim);
  806. mevictim = match_my_nick(victimstr);
  807. /* Do we want to revenge? */
  808. if (!want_to_revenge(chan, u, u2, badnick, victimstr, mevictim))
  809. return; /* No, leave them alone ... */
  810. /* Haha! Do the vengeful thing ... */
  811. punish_badguy(chan, whobad, u, badnick, victimstr, mevictim, type);
  812. }
  813. /* Set the key.
  814. */
  815. static void
  816. my_setkey(struct chanset_t *chan, char *k)
  817. {
  818. free(chan->channel.key);
  819. if (k == NULL) {
  820. chan->channel.key = (char *) my_calloc(1, 1);
  821. return;
  822. }
  823. chan->channel.key = strdup(k);
  824. }
  825. /* Adds a ban, exempt or invite mask to the list
  826. * m should be chan->channel.(exempt|invite|ban)
  827. */
  828. static void
  829. new_mask(masklist *m, char *s, char *who)
  830. {
  831. for (; m && m->mask[0] && rfc_casecmp(m->mask, s); m = m->next) ;
  832. if (m->mask[0])
  833. return; /* Already existent mask */
  834. m->next = (masklist *) my_calloc(1, sizeof(masklist));
  835. m->next->next = NULL;
  836. m->next->mask = (char *) my_calloc(1, 1);
  837. free(m->mask);
  838. m->mask = strdup(s);
  839. m->who = strdup(who);
  840. m->timer = now;
  841. }
  842. /* Removes a nick from the channel member list (returns 1 if successful)
  843. */
  844. static bool
  845. killmember(struct chanset_t *chan, char *nick)
  846. {
  847. memberlist *x = NULL, *old = NULL;
  848. for (x = chan->channel.member; x && x->nick[0]; old = x, x = x->next)
  849. if (!rfc_casecmp(x->nick, nick))
  850. break;
  851. if (!x || !x->nick[0]) {
  852. if (!channel_pending(chan))
  853. putlog(LOG_MISC, "*", "(!) killmember(%s, %s) -> nonexistent", chan->dname, nick);
  854. return 0;
  855. }
  856. if (old)
  857. old->next = x->next;
  858. else
  859. chan->channel.member = x->next;
  860. free(x);
  861. chan->channel.members--;
  862. /* The following two errors should NEVER happen. We will try to correct
  863. * them though, to keep the bot from crashing.
  864. */
  865. if (chan->channel.members < 0) {
  866. chan->channel.members = 0;
  867. for (x = chan->channel.member; x && x->nick[0]; x = x->next)
  868. chan->channel.members++;
  869. putlog(LOG_MISC, "*", "(!) actually I know of %d members.", chan->channel.members);
  870. }
  871. if (!chan->channel.member) {
  872. chan->channel.member = (memberlist *) my_calloc(1, sizeof(memberlist));
  873. chan->channel.member->nick[0] = 0;
  874. chan->channel.member->next = NULL;
  875. }
  876. return 1;
  877. }
  878. /* Check if I am a chanop. Returns boolean 1 or 0.
  879. */
  880. bool
  881. me_op(struct chanset_t *chan)
  882. {
  883. memberlist *mx = ismember(chan, botname);
  884. if (!mx)
  885. return 0;
  886. if (chan_hasop(mx))
  887. return 1;
  888. else
  889. return 0;
  890. }
  891. /* Check whether I'm voice. Returns boolean 1 or 0.
  892. */
  893. static bool
  894. me_voice(struct chanset_t *chan)
  895. {
  896. memberlist *mx = ismember(chan, botname);
  897. if (!mx)
  898. return 0;
  899. if (chan_hasvoice(mx))
  900. return 1;
  901. else
  902. return 0;
  903. }
  904. /* Check if there are any ops on the channel. Returns boolean 1 or 0.
  905. */
  906. static bool
  907. any_ops(struct chanset_t *chan)
  908. {
  909. memberlist *x = NULL;
  910. for (x = chan->channel.member; x && x->nick[0]; x = x->next)
  911. if (chan_hasop(x))
  912. break;
  913. if (!x || !x->nick[0])
  914. return 0;
  915. return 1;
  916. }
  917. /* Reset the channel information.
  918. */
  919. void
  920. reset_chan_info(struct chanset_t *chan)
  921. {
  922. if (!chan) return;
  923. if (!chan->name[0])
  924. strlcpy(chan->name, chan->dname, 81);
  925. /* Don't reset the channel if we're already resetting it */
  926. if (!shouldjoin(chan)) {
  927. dprintf(DP_MODE, "PART %s\n", chan->name);
  928. return;
  929. }
  930. if (!channel_pending(chan)) {
  931. bool opped = 0;
  932. if (me_op(chan))
  933. opped = 1;
  934. free(chan->channel.key);
  935. chan->channel.key = (char *) my_calloc(1, 1);
  936. clear_channel(chan, 1);
  937. chan->status |= CHAN_PEND;
  938. chan->status &= ~(CHAN_ACTIVE | CHAN_ASKEDMODES | CHAN_JOINING);
  939. /* don't bother checking bans if it's +take */
  940. if (!channel_take(chan)) {
  941. if (!(chan->status & CHAN_ASKEDBANS)) {
  942. chan->status |= CHAN_ASKEDBANS;
  943. dprintf(DP_MODE, "MODE %s +b\n", chan->name);
  944. }
  945. if (opped && do_eI) {
  946. last_eI = now;
  947. if (!(chan->ircnet_status & CHAN_ASKED_EXEMPTS) && use_exempts == 1) {
  948. chan->ircnet_status |= CHAN_ASKED_EXEMPTS;
  949. dprintf(DP_MODE, "MODE %s +e\n", chan->name);
  950. }
  951. if (!(chan->ircnet_status & CHAN_ASKED_INVITES) && use_invites == 1) {
  952. chan->ircnet_status |= CHAN_ASKED_INVITES;
  953. dprintf(DP_MODE, "MODE %s +I\n", chan->name);
  954. }
  955. }
  956. }
  957. /* These 2 need to get out asap, so into the mode queue */
  958. dprintf(DP_MODE, "MODE %s\n", chan->name);
  959. if (use_354)
  960. dprintf(DP_MODE, "WHO %s %%c%%h%%n%%u%%f\n", chan->name);
  961. else
  962. dprintf(DP_MODE, "WHO %s\n", chan->name);
  963. /* clear_channel nuked the data...so */
  964. }
  965. }
  966. /* If i'm the only person on the channel, and i'm not op'd,
  967. * might as well leave and rejoin. If i'm NOT the only person
  968. * on the channel, but i'm still not op'd, demand ops.
  969. */
  970. static void
  971. check_lonely_channel(struct chanset_t *chan)
  972. {
  973. if (channel_pending(chan) || !channel_active(chan) || me_op(chan) ||
  974. !shouldjoin(chan) || (chan->channel.mode & CHANANON))
  975. return;
  976. memberlist *m = NULL;
  977. char s[UHOSTLEN] = "";
  978. int i = 0;
  979. static int whined = 0;
  980. /* Count non-split channel members */
  981. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  982. if (!chan_issplit(m))
  983. i++;
  984. if (i == 1 && channel_cycle(chan) && !channel_stop_cycle(chan)) {
  985. if (chan->name[0] != '+') { /* Its pointless to cycle + chans for ops */
  986. putlog(LOG_MISC, "*", "Trying to cycle %s to regain ops.", chan->dname);
  987. dprintf(DP_MODE, "PART %s\n", chan->name);
  988. /* If it's a !chan, we need to recreate the channel with !!chan <cybah> */
  989. dprintf(DP_MODE, "JOIN %s%s %s\n", (chan->dname[0] == '!') ? "!" : "", chan->dname, chan->key_prot);
  990. chan->status |= CHAN_JOINING;
  991. whined = 0;
  992. }
  993. } else if (any_ops(chan)) {
  994. whined = 0;
  995. request_op(chan);
  996. /* need: op */
  997. } else {
  998. /* Other people here, but none are ops. If there are other bots make
  999. * them LEAVE!
  1000. */
  1001. bool ok = 1;
  1002. struct userrec *u = NULL;
  1003. if (!whined) {
  1004. /* + is opless. Complaining about no ops when without special
  1005. * help(services), we cant get them - Raist
  1006. */
  1007. if (chan->name[0] != '+')
  1008. putlog(LOG_MISC, "*", "%s is active but has no ops :(", chan->dname);
  1009. whined = 1;
  1010. }
  1011. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1012. simple_sprintf(s, "%s!%s", m->nick, m->userhost);
  1013. u = get_user_by_host(s);
  1014. if (!match_my_nick(m->nick) && (!u || !u->bot)) {
  1015. ok = 0;
  1016. break;
  1017. }
  1018. }
  1019. if (ok && channel_cycle(chan)) {
  1020. /* ALL bots! make them LEAVE!!! */
  1021. /*
  1022. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  1023. if (!match_my_nick(m->nick))
  1024. dprintf(DP_SERVER, "PRIVMSG %s :go %s\n", m->nick, chan->dname);
  1025. */
  1026. } else {
  1027. /* Some humans on channel, but still op-less */
  1028. request_op(chan);
  1029. /* need: op */
  1030. }
  1031. }
  1032. }
  1033. static void
  1034. warn_pls_take(struct chanset_t *chan)
  1035. {
  1036. if (channel_take(chan) && me_op(chan))
  1037. putlog(LOG_WARN, "*", "%s is set +take, and I'm already opped! +take is insecure, try +bitch instead",
  1038. chan->dname);
  1039. }
  1040. /* FIXME: max sendq will occur. */
  1041. static void
  1042. check_servers(struct chanset_t *chan)
  1043. {
  1044. for (memberlist *m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1045. if (!match_my_nick(m->nick) && chan_hasop(m) && (m->hops == -1)) {
  1046. putlog(LOG_DEBUG, "*", "Updating WHO for '%s' because '%s' is missing data.", chan->dname, m->nick);
  1047. dprintf(DP_HELP, "WHO %s\n", chan->name);
  1048. break; /* lets just do one chan at a time to save from flooding */
  1049. }
  1050. }
  1051. }
  1052. static void
  1053. check_netfight(struct chanset_t *chan)
  1054. {
  1055. if (fight_threshold) {
  1056. if ((chan->channel.fighting) && (chan->channel.fighting > fight_threshold)) {
  1057. if (!chan_bitch(chan) || !channel_closed(chan)) {
  1058. putlog(LOG_WARN, "*", "Auto-closed %s - channel fight", chan->dname);
  1059. do_chanset(NULL, chan, "+bitch +closed +backup", DO_LOCAL | DO_NET);
  1060. enforce_closed(chan);
  1061. dprintf(DP_MODE, "TOPIC %s :Auto-closed - channel fight\n", chan->name);
  1062. }
  1063. }
  1064. }
  1065. chan->channel.fighting = 0; /* we put this here because we need to clear it once per min */
  1066. }
  1067. void
  1068. raise_limit(struct chanset_t *chan)
  1069. {
  1070. if (!chan || !me_op(chan))
  1071. return;
  1072. int nl = chan->channel.members + chan->limitraise; /* new limit */
  1073. int i = chan->limitraise >> 2; /* DIV 4 */
  1074. /* if the newlimit will be in the range made by these vars, dont change. */
  1075. int ul = nl + i; /* upper limit */
  1076. int ll = nl - i; /* lower limit */
  1077. if ((chan->channel.maxmembers > ll) && (chan->channel.maxmembers < ul))
  1078. return; /* the current limit is in the range, so leave it. */
  1079. if (nl != chan->channel.maxmembers) {
  1080. char s[5] = "";
  1081. simple_sprintf(s, "%d", nl);
  1082. add_mode(chan, '+', 'l', s);
  1083. }
  1084. }
  1085. static void
  1086. check_expired_chanstuff(struct chanset_t *chan)
  1087. {
  1088. if (channel_active(chan)) {
  1089. masklist *b = NULL, *e = NULL;
  1090. memberlist *m = NULL, *n = NULL;
  1091. char s[UHOSTLEN] = "";
  1092. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  1093. if (me_op(chan)) {
  1094. if (channel_dynamicbans(chan) && chan->ban_time)
  1095. for (b = chan->channel.ban; b->mask[0]; b = b->next)
  1096. if (now - b->timer > 60 * chan->ban_time &&
  1097. !u_sticky_mask(chan->bans, b->mask) &&
  1098. !u_sticky_mask(global_bans, b->mask) && expired_mask(chan, b->who)) {
  1099. putlog(LOG_MODES, chan->dname, "(%s) Channel ban on %s expired.", chan->dname, b->mask);
  1100. add_mode(chan, '-', 'b', b->mask);
  1101. b->timer = now;
  1102. }
  1103. if (use_exempts && channel_dynamicexempts(chan) && chan->exempt_time)
  1104. for (e = chan->channel.exempt; e->mask[0]; e = e->next)
  1105. if (now - e->timer > 60 * chan->exempt_time &&
  1106. !u_sticky_mask(chan->exempts, e->mask) &&
  1107. !u_sticky_mask(global_exempts, e->mask) && expired_mask(chan, e->who)) {
  1108. /* Check to see if it matches a ban */
  1109. int match = 0;
  1110. for (b = chan->channel.ban; b->mask[0]; b = b->next)
  1111. if (wild_match(b->mask, e->mask) || wild_match(e->mask, b->mask)) {
  1112. match = 1;
  1113. break;
  1114. }
  1115. /* Leave this extra logging in for now. Can be removed later
  1116. * Jason
  1117. */
  1118. if (match) {
  1119. putlog(LOG_MODES, chan->dname,
  1120. "(%s) Channel exemption %s NOT expired. Exempt still set!", chan->dname, e->mask);
  1121. } else {
  1122. putlog(LOG_MODES, chan->dname, "(%s) Channel exemption on %s expired.", chan->dname, e->mask);
  1123. add_mode(chan, '-', 'e', e->mask);
  1124. }
  1125. e->timer = now;
  1126. }
  1127. if (use_invites && channel_dynamicinvites(chan) && chan->invite_time && !(chan->channel.mode & CHANINV)) {
  1128. for (b = chan->channel.invite; b->mask[0]; b = b->next) {
  1129. if (now - b->timer > 60 * chan->invite_time &&
  1130. !u_sticky_mask(chan->invites, b->mask) &&
  1131. !u_sticky_mask(global_invites, b->mask) && expired_mask(chan, b->who)) {
  1132. putlog(LOG_MODES, chan->dname, "(%s) Channel invitation on %s expired.", chan->dname, b->mask);
  1133. add_mode(chan, '-', 'I', b->mask);
  1134. b->timer = now;
  1135. }
  1136. }
  1137. }
  1138. } /* me_op */
  1139. // for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1140. for (m = chan->channel.member; m && m->nick[0]; m = n) {
  1141. n = m->next;
  1142. if (m->split && now - m->split > wait_split) {
  1143. simple_sprintf(s, "%s!%s", m->nick, m->userhost);
  1144. putlog(LOG_JOIN, chan->dname, "%s (%s) got lost in the net-split.", m->nick, m->userhost);
  1145. killmember(chan, m->nick);
  1146. continue;
  1147. }
  1148. if (me_op(chan)) {
  1149. if (chan->idle_kick) {
  1150. if (now - m->last >= chan->idle_kick * 60 && !match_my_nick(m->nick) && !chan_issplit(m)) {
  1151. simple_sprintf(s, "%s!%s", m->nick, m->userhost);
  1152. get_user_flagrec(m->user ? m->user : get_user_by_host(s), &fr, chan->dname);
  1153. if (!(glob_bot(fr) || (glob_op(fr) && !glob_deop(fr)) || chan_op(fr))) {
  1154. dprintf(DP_SERVER, "KICK %s %s :%sidle %d min\n", chan->name, m->nick, kickprefix, chan->idle_kick);
  1155. m->flags |= SENTKICK;
  1156. }
  1157. }
  1158. } else if (dovoice(chan) && !loading) { /* autovoice of +v users if bot is +y */
  1159. if (!chan_hasop(m) && !chan_hasvoice(m)) {
  1160. if (!m->user && !m->tried_getuser) {
  1161. simple_sprintf(s, "%s!%s", m->nick, m->userhost);
  1162. m->user = get_user_by_host(s);
  1163. m->tried_getuser = 1;
  1164. }
  1165. if (m->user) {
  1166. get_user_flagrec(m->user, &fr, chan->dname);
  1167. if (!glob_bot(fr)) {
  1168. if (!privchan(fr, chan, PRIV_VOICE) &&
  1169. ((channel_voice(chan) && !chk_devoice(fr)) ||
  1170. (!channel_voice(chan) && chk_voice(fr, chan)))) {
  1171. add_mode(chan, '+', 'v', m->nick);
  1172. } else if ((chk_devoice(fr) || (m->flags & EVOICE))) {
  1173. add_mode(chan, '-', 'v', m->nick);
  1174. }
  1175. }
  1176. } else if (!m->user && channel_voice(chan) && voice_ok(m, chan)) {
  1177. add_mode(chan, '+', 'v', m->nick);
  1178. }
  1179. }
  1180. }
  1181. }
  1182. m = n;
  1183. }
  1184. check_lonely_channel(chan);
  1185. } else if (shouldjoin(chan) && !channel_pending(chan) && !channel_joining(chan)) {
  1186. dprintf(DP_MODE, "JOIN %s %s\n",
  1187. (chan->name[0]) ? chan->name : chan->dname,
  1188. chan->channel.key[0] ? chan->channel.key : chan->key_prot);
  1189. chan->status |= CHAN_JOINING;
  1190. }
  1191. }
  1192. void
  1193. irc_minutely()
  1194. {
  1195. for (register struct chanset_t *chan = chanset; chan; chan = chan->next) {
  1196. warn_pls_take(chan);
  1197. if (server_online) {
  1198. check_netfight(chan);
  1199. check_servers(chan);
  1200. check_expired_chanstuff(chan);
  1201. }
  1202. }
  1203. }
  1204. int check_bind_authc(char *cmd, Auth *auth, char *chname, char *args)
  1205. {
  1206. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  1207. int x = 0;
  1208. get_user_flagrec(auth->user, &fr, chname);
  1209. x = check_bind(BT_msgc, cmd, &fr, auth, chname, args);
  1210. if (x & BIND_RET_LOG) {
  1211. if (chname)
  1212. putlog(LOG_CMDS, "*", "(%s!%s) !%s! %s %c%s %s", auth->nick, auth->host,
  1213. auth->handle, chname, auth_prefix[0], cmd, args);
  1214. else
  1215. putlog(LOG_CMDS, "*", "(%s!%s) !%s! %c%s %s", auth->nick, auth->host, auth->handle, auth_prefix[0], cmd, args);
  1216. }
  1217. if (x & BIND_RET_BREAK)
  1218. return (1);
  1219. return (0);
  1220. }
  1221. /* Flush the modes for EVERY channel.
  1222. */
  1223. void
  1224. flush_modes()
  1225. {
  1226. if (!modesperline) /* Haven't received 005 yet :) */
  1227. return;
  1228. memberlist *m = NULL;
  1229. for (register struct chanset_t *chan = chanset; chan; chan = chan->next) {
  1230. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1231. if (m->delay && m->delay <= now) {
  1232. m->delay = 0L;
  1233. m->flags &= ~FULL_DELAY;
  1234. if (chan_sentop(m)) {
  1235. m->flags &= ~SENTOP;
  1236. do_op(m->nick, chan, 0, 0);
  1237. }
  1238. if (chan_sentvoice(m)) {
  1239. m->flags &= ~SENTVOICE;
  1240. add_mode(chan, '+', 'v', m->nick);
  1241. }
  1242. }
  1243. }
  1244. flush_mode(chan, NORMAL);
  1245. }
  1246. }
  1247. void
  1248. irc_report(int idx, int details)
  1249. {
  1250. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  1251. char ch[1024] = "", q[160] = "", *p = NULL;
  1252. int k = 10;
  1253. size_t len;
  1254. strcpy(q, "Channels: ");
  1255. for (struct chanset_t *chan = chanset; chan; chan = chan->next) {
  1256. if (idx != DP_STDOUT)
  1257. get_user_flagrec(dcc[idx].user, &fr, chan->dname);
  1258. if (!privchan(fr, chan, PRIV_OP) && (idx == DP_STDOUT || glob_master(fr) || chan_master(fr))) {
  1259. p = NULL;
  1260. if (shouldjoin(chan)) {
  1261. if (chan->status & CHAN_JUPED)
  1262. p = "juped";
  1263. else if (channel_joining(chan))
  1264. p = "joining";
  1265. else if (!(chan->status & CHAN_ACTIVE))
  1266. p = "trying";
  1267. else if (chan->status & CHAN_PEND)
  1268. p = "pending";
  1269. else if ((chan->dname[0] != '+') && !me_op(chan))
  1270. p = "want ops!";
  1271. }
  1272. len = simple_sprintf(ch, "%s%s%s%s, ", chan->dname, p ? "(" : "", p ? p : "", p ? ")" : "");
  1273. if ((k + len) > 70) {
  1274. dprintf(idx, " %s\n", q);
  1275. strcpy(q, " ");
  1276. k = 10;
  1277. }
  1278. k += my_strcpy(q + k, ch);
  1279. }
  1280. }
  1281. if (k > 10) {
  1282. q[k - 2] = 0;
  1283. dprintf(idx, " %s\n", q);
  1284. }
  1285. }
  1286. static void
  1287. do_nettype()
  1288. {
  1289. switch (net_type) {
  1290. case 0: /* Efnet */
  1291. include_lk = 0;
  1292. break;
  1293. case 1: /* Ircnet */
  1294. include_lk = 1;
  1295. break;
  1296. case 2: /* Undernet */
  1297. include_lk = 1;
  1298. break;
  1299. case 3: /* Dalnet */
  1300. include_lk = 1;
  1301. break;
  1302. case 4: /* hybrid-6+ */
  1303. include_lk = 0;
  1304. break;
  1305. default:
  1306. break;
  1307. }
  1308. }
  1309. static cmd_t irc_bot[] = {
  1310. {"dp", "", (Function) mdop_request, NULL, LEAF},
  1311. {"gi", "", (Function) getin_request, NULL, LEAF},
  1312. {NULL, NULL, NULL, NULL, 0}
  1313. };
  1314. static void
  1315. getin_5secondly()
  1316. {
  1317. if (!server_online)
  1318. return;
  1319. for (register struct chanset_t *chan = chanset; chan; chan = chan->next) {
  1320. if ((!channel_pending(chan) && channel_active(chan)) && !me_op(chan))
  1321. request_op(chan);
  1322. }
  1323. }
  1324. void
  1325. irc_init()
  1326. {
  1327. timer_create_secs(60, "irc_minutely", (Function) irc_minutely);
  1328. timer_create_secs(5, "getin_5secondly", (Function) getin_5secondly);
  1329. /* Add our commands to the imported tables. */
  1330. add_builtins("dcc", irc_dcc);
  1331. add_builtins("bot", irc_bot);
  1332. add_builtins("raw", irc_raw);
  1333. add_builtins("msg", C_msg);
  1334. add_builtins("msgc", C_msgc);
  1335. do_nettype();
  1336. }