irc.c 41 KB

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