irc.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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) > 600)
  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] = "";
  414. checked_hostmask = 1;
  415. sprintf(s, "*!%s", botuserhost); /* just add actual user@ident, regardless of ~ */
  416. /* dont add the host if it conflicts with another in the userlist */
  417. struct userrec *u = NULL;
  418. struct list_type *q = NULL;
  419. for (u = userlist; u; u = u->next) {
  420. q = (struct list_type *) get_user(&USERENTRY_HOSTS, u);
  421. for (; q; q = q->next) {
  422. if (wild_match(s, q->extra) || wild_match(q->extra, s)) {
  423. if (u != conf.bot->u)
  424. putlog(LOG_WARN, "*", "My automatic hostmask '%s' would conflict with user: '%s'. (Not adding)", s, u->handle);
  425. return;
  426. }
  427. }
  428. }
  429. addhost_by_handle(conf.bot->nick, s);
  430. putlog(LOG_GETIN, "*", "Updated my hostmask: %s", s);
  431. }
  432. static void
  433. request_op(struct chanset_t *chan)
  434. {
  435. if (!chan || (chan && (channel_pending(chan) || !shouldjoin(chan) || !channel_active(chan) || me_op(chan))))
  436. return;
  437. if (chan->channel.no_op) {
  438. if (chan->channel.no_op > now) /* dont op until this time has passed */
  439. return;
  440. else
  441. chan->channel.no_op = 0;
  442. }
  443. chan->channel.do_opreq = 0;
  444. /* check server lag */
  445. if (server_lag > LAG_THRESHOLD) {
  446. putlog(LOG_GETIN, "*", "Not asking for ops on %s - I'm too lagged", chan->dname);
  447. return;
  448. }
  449. int i = 0, my_exp = 0, first = 100;
  450. time_t n = now;
  451. /* max OPREQ_COUNT requests per OPREQ_SECONDS sec */
  452. while (i < 5) {
  453. if (n - chan->opreqtime[i] > OPREQ_SECONDS) {
  454. if (first > i)
  455. first = i;
  456. my_exp++;
  457. chan->opreqtime[i] = 0;
  458. }
  459. i++;
  460. }
  461. if ((5 - my_exp) >= OPREQ_COUNT) {
  462. putlog(LOG_GETIN, "*", "Delaying opreq for %s - Maximum of %d:%d reached", chan->dname, OPREQ_COUNT,
  463. OPREQ_SECONDS);
  464. return;
  465. }
  466. if (!checked_hostmask)
  467. check_hostmask(); /* check, and update hostmask */
  468. int cnt = OP_BOTS, i2;
  469. memberlist *ml = NULL;
  470. memberlist *botops[MAX_BOTS];
  471. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  472. for (i = 0, ml = chan->channel.member; (i < MAX_BOTS) && ml && ml->nick[0]; ml = ml->next) {
  473. /* If bot, linked, global op & !split & chanop & (chan is reserver | bot isn't +a) ->
  474. * add to temp list */
  475. if ((i < MAX_BOTS) && ml->user && ml->user->bot &&
  476. (nextbot(ml->user->handle) >= 0) && chan_hasop(ml) && !chan_issplit(ml)) {
  477. get_user_flagrec(ml->user, &fr, NULL);
  478. if (chk_op(fr, chan))
  479. botops[i++] = ml;
  480. }
  481. }
  482. if (!i) {
  483. if (channel_active(chan) && !channel_pending(chan))
  484. putlog(LOG_GETIN, "*", "No one to ask for ops on %s", chan->dname);
  485. return;
  486. }
  487. char *l = (char *) calloc(1, cnt * 50), s[100] = "";
  488. /* first scan for bots on my server, ask first found for ops */
  489. sprintf(s, "gi o %s %s", chan->dname, botname);
  490. /* look for bots 0-1 hops away */
  491. for (i2 = 0; i2 < i; i2++) {
  492. if (botops[i2]->hops < 2) {
  493. putbot(botops[i2]->user->handle, s);
  494. chan->opreqtime[first] = n;
  495. if (l[0]) {
  496. strcat(l, ", ");
  497. strcat(l, botops[i2]->user->handle);
  498. } else {
  499. strcpy(l, botops[i2]->user->handle);
  500. }
  501. strcat(l, "/");
  502. strcat(l, botops[i2]->nick);
  503. botops[i2] = NULL;
  504. cnt--;
  505. break;
  506. }
  507. }
  508. /* Pick random op and ask for ops */
  509. while (cnt) {
  510. i2 = randint(i);
  511. if (botops[i2]) {
  512. putbot(botops[i2]->user->handle, s);
  513. chan->opreqtime[first] = n;
  514. if (l[0]) {
  515. strcat(l, ", ");
  516. strcat(l, botops[i2]->user->handle);
  517. } else {
  518. strcpy(l, botops[i2]->user->handle);
  519. }
  520. strcat(l, "/");
  521. strcat(l, botops[i2]->nick);
  522. cnt--;
  523. botops[i2] = NULL;
  524. } else {
  525. if (i < OP_BOTS)
  526. cnt--;
  527. }
  528. }
  529. putlog(LOG_GETIN, "*", "Requested ops on %s from %s", chan->dname, l);
  530. free(l);
  531. }
  532. static void
  533. request_in(struct chanset_t *chan)
  534. {
  535. int i = 0;
  536. struct userrec *botops[MAX_BOTS];
  537. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  538. for (struct userrec *u = userlist; u && (i < MAX_BOTS); u = u->next) {
  539. get_user_flagrec(u, &fr, NULL);
  540. if (bot_hublevel(u) == 999 && glob_bot(fr) && chk_op(fr, chan)
  541. #ifdef G_BACKUP
  542. && (!glob_backupbot(fr) || channel_backup(chan))
  543. #endif
  544. /* G_BACKUP */
  545. && nextbot(u->handle) >= 0)
  546. botops[i++] = u;
  547. }
  548. if (!i) {
  549. putlog(LOG_GETIN, "*", "No bots linked, can't request help to join %s", chan->dname);
  550. return;
  551. }
  552. char s[255] = "", *l = (char *) calloc(1, IN_BOTS * 30);
  553. int cnt = IN_BOTS, n;
  554. if (!checked_hostmask)
  555. check_hostmask(); /* check, and update hostmask */
  556. sprintf(s, "gi i %s %s %s!%s %s %s", chan->dname, botname, botname, botuserhost,
  557. myipstr(4) ? myipstr(4) : ".", myipstr(6) ? myipstr(6) : ".");
  558. while (cnt) {
  559. n = randint(i);
  560. if (botops[n]) {
  561. putbot(botops[n]->handle, s);
  562. if (l[0]) {
  563. strcat(l, ", ");
  564. strcat(l, botops[n]->handle);
  565. } else {
  566. strcpy(l, botops[n]->handle);
  567. }
  568. botops[n] = NULL;
  569. cnt--;
  570. } else {
  571. if (i < IN_BOTS)
  572. cnt--;
  573. }
  574. }
  575. putlog(LOG_GETIN, "*", "Requesting help to join %s from %s", chan->dname, l);
  576. free(l);
  577. }
  578. /* Contains the logic to decide wether we want to punish someone. Returns
  579. * true (1) if we want to, false (0) if not.
  580. */
  581. static bool
  582. want_to_revenge(struct chanset_t *chan, struct userrec *u,
  583. struct userrec *u2, char *badnick, char *victimstr, bool mevictim)
  584. {
  585. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  586. /* Do not take revenge upon ourselves. */
  587. if (match_my_nick(badnick))
  588. return 0;
  589. get_user_flagrec(u, &fr, chan->dname);
  590. /* Kickee didn't kick themself? */
  591. if (rfc_casecmp(badnick, victimstr)) {
  592. /* They kicked me? */
  593. if (mevictim) {
  594. /* ... and I'm allowed to take revenge? <snicker> */
  595. if (channel_revengebot(chan))
  596. return 1;
  597. /* Do we revenge for our users ... and do we actually know the victim? */
  598. } else if (channel_revenge(chan) && u2) {
  599. struct flag_record fr2 = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  600. get_user_flagrec(u2, &fr2, chan->dname);
  601. /* Protecting friends? */
  602. /* Protecting ops? */
  603. if ((channel_protectops(chan) &&
  604. /* ... and kicked is valid op? */
  605. (chan_op(fr2) || (glob_op(fr2) && !chan_deop(fr2)))))
  606. return 1;
  607. }
  608. }
  609. return 0;
  610. }
  611. /* Dependant on revenge_mode, punish the offender.
  612. */
  613. static void
  614. punish_badguy(struct chanset_t *chan, char *whobad,
  615. struct userrec *u, char *badnick, char *victimstr, bool mevictim, int type)
  616. {
  617. memberlist *m = ismember(chan, badnick);
  618. if (!m)
  619. return;
  620. char reason[1024] = "", ct[81] = "", *kick_msg = NULL;
  621. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  622. get_user_flagrec(u, &fr, chan->dname);
  623. /* Get current time into a string */
  624. egg_strftime(ct, sizeof ct, "%d %b %Z", gmtime(&now));
  625. /* Put together log and kick messages */
  626. reason[0] = 0;
  627. switch (type) {
  628. case REVENGE_KICK:
  629. kick_msg = IRC_KICK_PROTECT;
  630. simple_sprintf(reason, "kicked %s off %s", victimstr, chan->dname);
  631. break;
  632. case REVENGE_DEOP:
  633. simple_sprintf(reason, "deopped %s on %s", victimstr, chan->dname);
  634. kick_msg = IRC_DEOP_PROTECT;
  635. break;
  636. default:
  637. kick_msg = "revenge!";
  638. }
  639. putlog(LOG_MISC, chan->dname, "Punishing %s (%s)", badnick, reason);
  640. /* Set the offender +d */
  641. if ((chan->revenge_mode > 0) &&
  642. /* ... unless there's no more to do */
  643. !(chan_deop(fr) || glob_deop(fr))) {
  644. char s[UHOSTLEN], s1[UHOSTLEN];
  645. memberlist *mx = NULL;
  646. /* Removing op */
  647. if (chan_op(fr) || (glob_op(fr) && !chan_deop(fr))) {
  648. fr.match = FR_CHAN;
  649. if (chan_op(fr)) {
  650. fr.chan &= ~USER_OP;
  651. } else {
  652. fr.chan |= USER_DEOP;
  653. }
  654. set_user_flagrec(u, &fr, chan->dname);
  655. putlog(LOG_MISC, "*", "No longer opping %s[%s] (%s)", u->handle, whobad, reason);
  656. }
  657. /* ... or just setting to deop */
  658. else if (u) {
  659. /* In the user list already, cool :) */
  660. fr.match = FR_CHAN;
  661. fr.chan |= USER_DEOP;
  662. set_user_flagrec(u, &fr, chan->dname);
  663. simple_sprintf(s, "(%s) %s", ct, reason);
  664. putlog(LOG_MISC, "*", "Now deopping %s[%s] (%s)", u->handle, whobad, s);
  665. }
  666. /* ... or creating new user and setting that to deop */
  667. else {
  668. strcpy(s1, whobad);
  669. maskhost(s1, s);
  670. strcpy(s1, badnick);
  671. /* If that handle exists use "badX" (where X is an increasing number)
  672. * instead.
  673. */
  674. while (get_user_by_handle(userlist, s1)) {
  675. if (!strncmp(s1, "bad", 3)) {
  676. int i;
  677. i = atoi(s1 + 3);
  678. simple_sprintf(s1 + 3, "%d", i + 1);
  679. } else
  680. strcpy(s1, "bad1"); /* Start with '1' */
  681. }
  682. userlist = adduser(userlist, s1, s, "-", 0, 0);
  683. fr.match = FR_CHAN;
  684. fr.chan = USER_DEOP;
  685. u = get_user_by_handle(userlist, s1);
  686. if ((mx = ismember(chan, badnick)))
  687. mx->user = u;
  688. set_user_flagrec(u, &fr, chan->dname);
  689. simple_sprintf(s, "(%s) %s (%s)", ct, reason, whobad);
  690. set_user(&USERENTRY_COMMENT, u, (void *) s);
  691. putlog(LOG_MISC, "*", "Now deopping %s (%s)", whobad, reason);
  692. }
  693. }
  694. /* Always try to deop the offender */
  695. if (!mevictim)
  696. add_mode(chan, '-', 'o', badnick);
  697. /* Ban. Should be done before kicking. */
  698. if (chan->revenge_mode > 2) {
  699. char s[UHOSTLEN] = "", s1[UHOSTLEN] = "";
  700. splitnick(&whobad);
  701. maskhost(whobad, s1);
  702. simple_sprintf(s, "(%s) %s", ct, reason);
  703. u_addmask('b', chan, s1, conf.bot->nick, s, now + (60 * chan->ban_time), 0);
  704. if (!mevictim && me_op(chan)) {
  705. add_mode(chan, '+', 'b', s1);
  706. flush_mode(chan, QUICK);
  707. }
  708. }
  709. /* Kick the offender */
  710. if ((chan->revenge_mode > 1) && !chan_sentkick(m) &&
  711. /* ... and can I actually do anything about it? */
  712. me_op(chan) && !mevictim) {
  713. dprintf(DP_MODE, "KICK %s %s :%s%s\n", chan->name, m->nick, bankickprefix, response(RES_MEAN));
  714. m->flags |= SENTKICK;
  715. }
  716. }
  717. /* Punishes bad guys under certain circumstances using methods as defined
  718. * by the revenge_mode flag.
  719. */
  720. static void
  721. maybe_revenge(struct chanset_t *chan, char *whobad, char *whovictim, int type)
  722. {
  723. if (!chan || (type < 0))
  724. return;
  725. /* Get info about offender */
  726. struct userrec *u = get_user_by_host(whobad), *u2 = NULL;
  727. char *badnick = splitnick(&whobad), *victimstr = NULL;
  728. bool mevictim;
  729. /* Get info about victim */
  730. u2 = get_user_by_host(whovictim);
  731. victimstr = splitnick(&whovictim);
  732. mevictim = match_my_nick(victimstr);
  733. /* Do we want to revenge? */
  734. if (!want_to_revenge(chan, u, u2, badnick, victimstr, mevictim))
  735. return; /* No, leave them alone ... */
  736. /* Haha! Do the vengeful thing ... */
  737. punish_badguy(chan, whobad, u, badnick, victimstr, mevictim, type);
  738. }
  739. /* Set the key.
  740. */
  741. static void
  742. my_setkey(struct chanset_t *chan, char *k)
  743. {
  744. free(chan->channel.key);
  745. if (k == NULL) {
  746. chan->channel.key = (char *) calloc(1, 1);
  747. return;
  748. }
  749. chan->channel.key = (char *) calloc(1, strlen(k) + 1);
  750. strcpy(chan->channel.key, k);
  751. }
  752. /* Adds a ban, exempt or invite mask to the list
  753. * m should be chan->channel.(exempt|invite|ban)
  754. */
  755. static void
  756. new_mask(masklist *m, char *s, char *who)
  757. {
  758. for (; m && m->mask[0] && rfc_casecmp(m->mask, s); m = m->next) ;
  759. if (m->mask[0])
  760. return; /* Already existent mask */
  761. m->next = (masklist *) calloc(1, sizeof(masklist));
  762. m->next->next = NULL;
  763. m->next->mask = (char *) calloc(1, 1);
  764. free(m->mask);
  765. m->mask = (char *) calloc(1, strlen(s) + 1);
  766. strcpy(m->mask, s);
  767. m->who = (char *) calloc(1, strlen(who) + 1);
  768. strcpy(m->who, who);
  769. m->timer = now;
  770. }
  771. /* Removes a nick from the channel member list (returns 1 if successful)
  772. */
  773. /* FIXME: This passing of FILE:LINE is unnecesary, an OLD bug may still exist from the malloc->calloc patch */
  774. static bool
  775. real_killmember(struct chanset_t *chan, char *nick, const char *file, int line)
  776. {
  777. memberlist *x = NULL, *old = NULL;
  778. for (x = chan->channel.member; x && x->nick[0]; old = x, x = x->next)
  779. if (!rfc_casecmp(x->nick, nick))
  780. break;
  781. if (!x || !x->nick[0]) {
  782. if (!channel_pending(chan))
  783. putlog(LOG_MISC, "*", "(!) killmember(%s, %s) -> nonexistent (%s: %d)", chan->dname, nick, file, line);
  784. return 0;
  785. }
  786. if (old)
  787. old->next = x->next;
  788. else
  789. chan->channel.member = x->next;
  790. free(x);
  791. chan->channel.members--;
  792. /* The following two errors should NEVER happen. We will try to correct
  793. * them though, to keep the bot from crashing.
  794. */
  795. if (chan->channel.members < 0) {
  796. chan->channel.members = 0;
  797. for (x = chan->channel.member; x && x->nick[0]; x = x->next)
  798. chan->channel.members++;
  799. putlog(LOG_MISC, "*", "(!) actually I know of %d members.", chan->channel.members);
  800. }
  801. if (!chan->channel.member) {
  802. chan->channel.member = (memberlist *) calloc(1, sizeof(memberlist));
  803. chan->channel.member->nick[0] = 0;
  804. chan->channel.member->next = NULL;
  805. }
  806. return 1;
  807. }
  808. /* Check if I am a chanop. Returns boolean 1 or 0.
  809. */
  810. bool
  811. me_op(struct chanset_t *chan)
  812. {
  813. memberlist *mx = ismember(chan, botname);
  814. if (!mx)
  815. return 0;
  816. if (chan_hasop(mx))
  817. return 1;
  818. else
  819. return 0;
  820. }
  821. /* Check whether I'm voice. Returns boolean 1 or 0.
  822. */
  823. static bool
  824. me_voice(struct chanset_t *chan)
  825. {
  826. memberlist *mx = ismember(chan, botname);
  827. if (!mx)
  828. return 0;
  829. if (chan_hasvoice(mx))
  830. return 1;
  831. else
  832. return 0;
  833. }
  834. /* Check if there are any ops on the channel. Returns boolean 1 or 0.
  835. */
  836. static bool
  837. any_ops(struct chanset_t *chan)
  838. {
  839. memberlist *x = NULL;
  840. for (x = chan->channel.member; x && x->nick[0]; x = x->next)
  841. if (chan_hasop(x))
  842. break;
  843. if (!x || !x->nick[0])
  844. return 0;
  845. return 1;
  846. }
  847. /* Reset the channel information.
  848. */
  849. static void
  850. reset_chan_info(struct chanset_t *chan)
  851. {
  852. /* Don't reset the channel if we're already resetting it */
  853. if (!shouldjoin(chan)) {
  854. dprintf(DP_MODE, "PART %s\n", chan->name);
  855. return;
  856. }
  857. if (!channel_pending(chan)) {
  858. bool opped = 0;
  859. if (me_op(chan))
  860. opped = 1;
  861. free(chan->channel.key);
  862. chan->channel.key = (char *) calloc(1, 1);
  863. clear_channel(chan, 1);
  864. chan->status |= CHAN_PEND;
  865. chan->status &= ~(CHAN_ACTIVE | CHAN_ASKEDMODES);
  866. if (!(chan->status & CHAN_ASKEDBANS)) {
  867. chan->status |= CHAN_ASKEDBANS;
  868. dprintf(DP_MODE, "MODE %s +b\n", chan->name);
  869. }
  870. if (opped && !channel_take(chan)) {
  871. if (!(chan->ircnet_status & CHAN_ASKED_EXEMPTS) && use_exempts == 1) {
  872. chan->ircnet_status |= CHAN_ASKED_EXEMPTS;
  873. dprintf(DP_MODE, "MODE %s +e\n", chan->name);
  874. }
  875. if (!(chan->ircnet_status & CHAN_ASKED_INVITED) && use_invites == 1) {
  876. chan->ircnet_status |= CHAN_ASKED_INVITED;
  877. dprintf(DP_MODE, "MODE %s +I\n", chan->name);
  878. }
  879. }
  880. /* These 2 need to get out asap, so into the mode queue */
  881. dprintf(DP_MODE, "MODE %s\n", chan->name);
  882. if (use_354)
  883. dprintf(DP_MODE, "WHO %s %%c%%h%%n%%u%%f\n", chan->name);
  884. else
  885. dprintf(DP_MODE, "WHO %s\n", chan->name);
  886. /* clear_channel nuked the data...so */
  887. }
  888. }
  889. /* If i'm the only person on the channel, and i'm not op'd,
  890. * might as well leave and rejoin. If i'm NOT the only person
  891. * on the channel, but i'm still not op'd, demand ops.
  892. */
  893. static void
  894. check_lonely_channel(struct chanset_t *chan)
  895. {
  896. if (channel_pending(chan) || !channel_active(chan) || me_op(chan) ||
  897. !shouldjoin(chan) || (chan->channel.mode & CHANANON))
  898. return;
  899. memberlist *m = NULL;
  900. char s[UHOSTLEN] = "";
  901. int i = 0;
  902. static int whined = 0;
  903. /* Count non-split channel members */
  904. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  905. if (!chan_issplit(m))
  906. i++;
  907. if (i == 1 && channel_cycle(chan) && !channel_stop_cycle(chan)) {
  908. if (chan->name[0] != '+') { /* Its pointless to cycle + chans for ops */
  909. putlog(LOG_MISC, "*", "Trying to cycle %s to regain ops.", chan->dname);
  910. dprintf(DP_MODE, "PART %s\n", chan->name);
  911. /* If it's a !chan, we need to recreate the channel with !!chan <cybah> */
  912. dprintf(DP_MODE, "JOIN %s%s %s\n", (chan->dname[0] == '!') ? "!" : "", chan->dname, chan->key_prot);
  913. whined = 0;
  914. }
  915. } else if (any_ops(chan)) {
  916. whined = 0;
  917. request_op(chan);
  918. /* need: op */
  919. } else {
  920. /* Other people here, but none are ops. If there are other bots make
  921. * them LEAVE!
  922. */
  923. bool ok = 1;
  924. struct userrec *u = NULL;
  925. if (!whined) {
  926. /* + is opless. Complaining about no ops when without special
  927. * help(services), we cant get them - Raist
  928. */
  929. if (chan->name[0] != '+')
  930. putlog(LOG_MISC, "*", "%s is active but has no ops :(", chan->dname);
  931. whined = 1;
  932. }
  933. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  934. sprintf(s, "%s!%s", m->nick, m->userhost);
  935. u = get_user_by_host(s);
  936. if (!match_my_nick(m->nick) && (!u || !u->bot)) {
  937. ok = 0;
  938. break;
  939. }
  940. }
  941. if (ok && channel_cycle(chan)) {
  942. /* ALL bots! make them LEAVE!!! */
  943. /*
  944. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  945. if (!match_my_nick(m->nick))
  946. dprintf(DP_SERVER, "PRIVMSG %s :go %s\n", m->nick, chan->dname);
  947. */
  948. } else {
  949. /* Some humans on channel, but still op-less */
  950. request_op(chan);
  951. /* need: op */
  952. }
  953. }
  954. }
  955. static void
  956. warn_pls_take(struct chanset_t *chan)
  957. {
  958. if (channel_take(chan) && me_op(chan))
  959. putlog(LOG_WARN, "*", "%s is set +take, and I'm already opped! +take is insecure, try +bitch instead",
  960. chan->dname);
  961. }
  962. /* FIXME: max sendq will occur. */
  963. static void
  964. check_servers(struct chanset_t *chan)
  965. {
  966. for (memberlist *m = chan->channel.member; m && m->nick[0]; m = m->next) {
  967. if (!match_my_nick(m->nick) && chan_hasop(m) && (m->hops == -1)) {
  968. putlog(LOG_DEBUG, "*", "Updating WHO for '%s' because '%s' is missing data.", chan->dname, m->nick);
  969. dprintf(DP_HELP, "WHO %s\n", chan->name);
  970. break; /* lets just do one chan at a time to save from flooding */
  971. }
  972. }
  973. }
  974. static void
  975. check_netfight(struct chanset_t *chan)
  976. {
  977. int limit = atoi(CFG_FIGHTTHRESHOLD.gdata ? CFG_FIGHTTHRESHOLD.gdata : "0");
  978. if (limit) {
  979. if ((chan->channel.fighting) && (chan->channel.fighting > limit)) {
  980. if (!channel_bitch(chan) || !channel_closed(chan)) {
  981. putlog(LOG_WARN, "*", "Auto-closed %s - channel fight", chan->dname);
  982. do_chanset(NULL, chan, "+bitch +closed +backup", DO_LOCAL | DO_NET);
  983. enforce_closed(chan);
  984. dprintf(DP_MODE, "TOPIC %s :Auto-closed - channel fight\n", chan->name);
  985. }
  986. }
  987. }
  988. chan->channel.fighting = 0; /* we put this here because we need to clear it once per min */
  989. }
  990. void
  991. raise_limit(struct chanset_t *chan)
  992. {
  993. if (!chan || !me_op(chan))
  994. return;
  995. int nl = chan->channel.members + chan->limitraise; /* new limit */
  996. int i = chan->limitraise >> 2; /* DIV 4 */
  997. /* if the newlimit will be in the range made by these vars, dont change. */
  998. int ul = nl + i; /* upper limit */
  999. int ll = nl - i; /* lower limit */
  1000. if ((chan->channel.maxmembers > ll) && (chan->channel.maxmembers < ul))
  1001. return; /* the current limit is in the range, so leave it. */
  1002. if (nl != chan->channel.maxmembers) {
  1003. char s[5] = "";
  1004. sprintf(s, "%d", nl);
  1005. add_mode(chan, '+', 'l', s);
  1006. }
  1007. }
  1008. static void
  1009. check_expired_chanstuff(struct chanset_t *chan)
  1010. {
  1011. if (channel_active(chan)) {
  1012. masklist *b = NULL, *e = NULL;
  1013. memberlist *m = NULL, *n = NULL;
  1014. char s[UHOSTLEN] = "";
  1015. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  1016. if (me_op(chan)) {
  1017. if (channel_dynamicbans(chan) && chan->ban_time)
  1018. for (b = chan->channel.ban; b->mask[0]; b = b->next)
  1019. if (now - b->timer > 60 * chan->ban_time &&
  1020. !u_sticky_mask(chan->bans, b->mask) &&
  1021. !u_sticky_mask(global_bans, b->mask) && expired_mask(chan, b->who)) {
  1022. putlog(LOG_MODES, chan->dname, "(%s) Channel ban on %s expired.", chan->dname, b->mask);
  1023. add_mode(chan, '-', 'b', b->mask);
  1024. b->timer = now;
  1025. }
  1026. if (use_exempts && channel_dynamicexempts(chan) && chan->exempt_time)
  1027. for (e = chan->channel.exempt; e->mask[0]; e = e->next)
  1028. if (now - e->timer > 60 * chan->exempt_time &&
  1029. !u_sticky_mask(chan->exempts, e->mask) &&
  1030. !u_sticky_mask(global_exempts, e->mask) && expired_mask(chan, e->who)) {
  1031. /* Check to see if it matches a ban */
  1032. int match = 0;
  1033. for (b = chan->channel.ban; b->mask[0]; b = b->next)
  1034. if (wild_match(b->mask, e->mask) || wild_match(e->mask, b->mask)) {
  1035. match = 1;
  1036. break;
  1037. }
  1038. /* Leave this extra logging in for now. Can be removed later
  1039. * Jason
  1040. */
  1041. if (match) {
  1042. putlog(LOG_MODES, chan->dname,
  1043. "(%s) Channel exemption %s NOT expired. Exempt still set!", chan->dname, e->mask);
  1044. } else {
  1045. putlog(LOG_MODES, chan->dname, "(%s) Channel exemption on %s expired.", chan->dname, e->mask);
  1046. add_mode(chan, '-', 'e', e->mask);
  1047. }
  1048. e->timer = now;
  1049. }
  1050. if (use_invites && channel_dynamicinvites(chan) && chan->invite_time && !(chan->channel.mode & CHANINV)) {
  1051. for (b = chan->channel.invite; b->mask[0]; b = b->next) {
  1052. if (now - b->timer > 60 * chan->invite_time &&
  1053. !u_sticky_mask(chan->invites, b->mask) &&
  1054. !u_sticky_mask(global_invites, b->mask) && expired_mask(chan, b->who)) {
  1055. putlog(LOG_MODES, chan->dname, "(%s) Channel invitation on %s expired.", chan->dname, b->mask);
  1056. add_mode(chan, '-', 'I', b->mask);
  1057. b->timer = now;
  1058. }
  1059. }
  1060. }
  1061. } /* me_op */
  1062. // for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1063. for (m = chan->channel.member; m && m->nick[0]; m = n) {
  1064. n = m->next;
  1065. if (m->split && now - m->split > wait_split) {
  1066. sprintf(s, "%s!%s", m->nick, m->userhost);
  1067. putlog(LOG_JOIN, chan->dname, "%s (%s) got lost in the net-split.", m->nick, m->userhost);
  1068. killmember(chan, m->nick);
  1069. continue;
  1070. }
  1071. if (me_op(chan)) {
  1072. if (chan->idle_kick) {
  1073. if (now - m->last >= chan->idle_kick * 60 && !match_my_nick(m->nick) && !chan_issplit(m)) {
  1074. sprintf(s, "%s!%s", m->nick, m->userhost);
  1075. get_user_flagrec(m->user ? m->user : get_user_by_host(s), &fr, chan->dname);
  1076. if (!(glob_bot(fr) || (glob_op(fr) && !glob_deop(fr)) || chan_op(fr))) {
  1077. dprintf(DP_SERVER, "KICK %s %s :%sidle %d min\n", chan->name, m->nick, kickprefix, chan->idle_kick);
  1078. m->flags |= SENTKICK;
  1079. }
  1080. }
  1081. } else if (dovoice(chan) && !loading) { /* autovoice of +v users if bot is +y */
  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. m = n;
  1105. }
  1106. check_lonely_channel(chan);
  1107. } else if (shouldjoin(chan) && !channel_pending(chan)) {
  1108. dprintf(DP_MODE, "JOIN %s %s\n",
  1109. (chan->name[0]) ? chan->name : chan->dname,
  1110. chan->channel.key[0] ? chan->channel.key : chan->key_prot);
  1111. }
  1112. }
  1113. void
  1114. irc_minutely()
  1115. {
  1116. for (register struct chanset_t *chan = chanset; chan; chan = chan->next) {
  1117. warn_pls_take(chan);
  1118. if (server_online) {
  1119. check_netfight(chan);
  1120. check_servers(chan);
  1121. check_expired_chanstuff(chan);
  1122. }
  1123. }
  1124. }
  1125. static int
  1126. check_bind_pubc(char *cmd, char *nick, char *from, struct userrec *u, char *args, char *chname)
  1127. {
  1128. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  1129. int x = 0;
  1130. get_user_flagrec(u, &fr, chname);
  1131. x = check_bind(BT_msgc, cmd, &fr, nick, from, u, chname, args);
  1132. if (x & BIND_RET_LOG)
  1133. putlog(LOG_CMDS, "*", "(%s!%s) !%s! %s %c%s %s", nick, from, u ? u->handle : "*", chname, cmdprefix, cmd,
  1134. args);
  1135. if (x & BIND_RET_BREAK)
  1136. return (1);
  1137. return (0);
  1138. }
  1139. /* Flush the modes for EVERY channel.
  1140. */
  1141. void
  1142. flush_modes()
  1143. {
  1144. if (!modesperline) /* Haven't received 005 yet :) */
  1145. return;
  1146. memberlist *m = NULL;
  1147. for (register struct chanset_t *chan = chanset; chan; chan = chan->next) {
  1148. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1149. if (m->delay && m->delay <= now) {
  1150. m->delay = 0L;
  1151. m->flags &= ~FULL_DELAY;
  1152. if (chan_sentop(m)) {
  1153. m->flags &= ~SENTOP;
  1154. do_op(m->nick, chan, 0, 0);
  1155. }
  1156. if (chan_sentvoice(m)) {
  1157. m->flags &= ~SENTVOICE;
  1158. add_mode(chan, '+', 'v', m->nick);
  1159. }
  1160. }
  1161. }
  1162. flush_mode(chan, NORMAL);
  1163. }
  1164. }
  1165. void
  1166. irc_report(int idx, int details)
  1167. {
  1168. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  1169. char ch[1024] = "", q[160] = "", *p = NULL;
  1170. int k = 10;
  1171. size_t len;
  1172. strcpy(q, "Channels: ");
  1173. for (struct chanset_t *chan = chanset; chan; chan = chan->next) {
  1174. if (idx != DP_STDOUT)
  1175. get_user_flagrec(dcc[idx].user, &fr, chan->dname);
  1176. if (!privchan(fr, chan, PRIV_OP) && (idx == DP_STDOUT || glob_master(fr) || chan_master(fr))) {
  1177. p = NULL;
  1178. if (shouldjoin(chan)) {
  1179. if (chan->status & CHAN_JUPED)
  1180. p = MISC_JUPED;
  1181. else if (!(chan->status & CHAN_ACTIVE))
  1182. p = MISC_TRYING;
  1183. else if (chan->status & CHAN_PEND)
  1184. p = MISC_PENDING;
  1185. else if ((chan->dname[0] != '+') && !me_op(chan))
  1186. p = MISC_WANTOPS;
  1187. }
  1188. len = simple_sprintf(ch, "%s%s%s%s, ", chan->dname, p ? "(" : "", p ? p : "", p ? ")" : "");
  1189. if ((k + len) > 70) {
  1190. dprintf(idx, " %s\n", q);
  1191. strcpy(q, " ");
  1192. k = 10;
  1193. }
  1194. k += my_strcpy(q + k, ch);
  1195. }
  1196. }
  1197. if (k > 10) {
  1198. q[k - 2] = 0;
  1199. dprintf(idx, " %s\n", q);
  1200. }
  1201. }
  1202. static void
  1203. do_nettype()
  1204. {
  1205. switch (net_type) {
  1206. case 0: /* Efnet */
  1207. include_lk = 0;
  1208. break;
  1209. case 1: /* Ircnet */
  1210. include_lk = 1;
  1211. break;
  1212. case 2: /* Undernet */
  1213. include_lk = 1;
  1214. break;
  1215. case 3: /* Dalnet */
  1216. include_lk = 1;
  1217. break;
  1218. case 4: /* hybrid-6+ */
  1219. include_lk = 0;
  1220. break;
  1221. default:
  1222. break;
  1223. }
  1224. }
  1225. static cmd_t irc_bot[] = {
  1226. {"dp", "", (Function) mdop_request, NULL},
  1227. {"gi", "", (Function) getin_request, NULL},
  1228. {NULL, NULL, NULL, NULL}
  1229. };
  1230. static void
  1231. getin_5secondly()
  1232. {
  1233. if (!server_online)
  1234. return;
  1235. for (register struct chanset_t *chan = chanset; chan; chan = chan->next) {
  1236. if ((!channel_pending(chan) && channel_active(chan)) && !me_op(chan))
  1237. request_op(chan);
  1238. }
  1239. }
  1240. void
  1241. irc_init()
  1242. {
  1243. for (struct chanset_t *chan = chanset; chan; chan = chan->next) {
  1244. if (shouldjoin(chan))
  1245. dprintf(DP_MODE, "JOIN %s %s\n", (chan->name[0]) ? chan->name : chan->dname, chan->key_prot);
  1246. chan->status &= ~(CHAN_ACTIVE | CHAN_PEND | CHAN_ASKEDBANS);
  1247. chan->ircnet_status &= ~(CHAN_ASKED_INVITED | CHAN_ASKED_EXEMPTS);
  1248. }
  1249. timer_create_secs(60, "irc_minutely", (Function) irc_minutely);
  1250. timer_create_secs(5, "getin_5secondly", (Function) getin_5secondly);
  1251. /* Add our commands to the imported tables. */
  1252. add_builtins("dcc", irc_dcc);
  1253. add_builtins("bot", irc_bot);
  1254. add_builtins("raw", irc_raw);
  1255. add_builtins("msg", C_msg);
  1256. add_builtins("msgc", C_msgc);
  1257. do_nettype();
  1258. }
  1259. #endif /* LEAF */