irc.c 45 KB

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