irc.c 44 KB

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