irc.c 45 KB

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