irc.c 49 KB

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