irc.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2010 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * irc.c -- part of irc.mod
  22. * support for channels within the bot
  23. *
  24. */
  25. #include "src/common.h"
  26. #define MAKING_IRC
  27. #include "irc.h"
  28. #include "src/adns.h"
  29. #include "src/match.h"
  30. #include "src/settings.h"
  31. #include "src/base64.h"
  32. #include "src/tandem.h"
  33. #include "src/net.h"
  34. #include "src/botnet.h"
  35. #include "src/botmsg.h"
  36. #include "src/main.h"
  37. #include "src/response.h"
  38. #include "src/set.h"
  39. #include "src/userrec.h"
  40. #include "src/misc.h"
  41. #include "src/rfc1459.h"
  42. #include "src/socket.h"
  43. #include "src/adns.h"
  44. #include "src/chanprog.h"
  45. #include "src/auth.h"
  46. #include "src/userrec.h"
  47. #include "src/binds.h"
  48. #include "src/userent.h"
  49. #include "src/egg_timer.h"
  50. #include "src/mod/share.mod/share.h"
  51. #include "src/mod/server.mod/server.h"
  52. #include "src/mod/channels.mod/channels.h"
  53. #include "src/mod/ctcp.mod/ctcp.h"
  54. #include <bdlib/src/String.h>
  55. #include <bdlib/src/HashTable.h>
  56. #include <bdlib/src/Queue.h>
  57. #include <bdlib/src/base64.h>
  58. #include <stdarg.h>
  59. #define PRIO_DEOP 1
  60. #define PRIO_KICK 2
  61. #ifdef CACHE
  62. static cache_t *irccache = NULL;
  63. #endif /* CACHE */
  64. #define do_eI (((now - chan->channel.last_eI) > 30) ? 1 : 0)
  65. static time_t wait_split = 900; /* Time to wait for user to return from
  66. * net-split. */
  67. int max_bans; /* Modified by net-type 1-4 */
  68. int max_exempts;
  69. int max_invites;
  70. int max_modes; /* Modified by net-type 1-4 */
  71. static bool bounce_bans = 0;
  72. static bool bounce_exempts = 0;
  73. static bool bounce_invites = 0;
  74. static bool bounce_modes = 0;
  75. unsigned int modesperline; /* Number of modes per line to send. */
  76. static size_t mode_buf_len = 200; /* Maximum bytes to send in 1 mode. */
  77. bool use_354 = 0; /* Use ircu's short 354 /who
  78. * responses. */
  79. static bool kick_fun = 0;
  80. static bool ban_fun = 1;
  81. static bool prevent_mixing = 1; /* To prevent mixing old/new modes */
  82. bool include_lk = 1; /* For correct calculation
  83. * in real_add_mode. */
  84. bd::HashTable<bd::String, unsigned long> bot_counters;
  85. unsigned long my_cookie_counter = 0;
  86. static bd::Queue<bd::String> chained_who;
  87. static int chained_who_idx;
  88. static int
  89. voice_ok(memberlist *m, struct chanset_t *chan)
  90. {
  91. if (m->flags & EVOICE)
  92. return 0;
  93. if (m->userhost[0] && !chan->voice_non_ident && m->userhost[0] == '~')
  94. return 0;
  95. return 1;
  96. }
  97. #include "chan.c"
  98. #include "mode.c"
  99. #include "cmdsirc.c"
  100. #include "msgcmds.c"
  101. #ifdef unfinished
  102. static void
  103. detect_offense(memberlist* m, struct chanset_t *chan, char *msg)
  104. {
  105. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  106. struct userrec *u = m->user ? m->user : get_user_by_host(m->userhost);
  107. int i = 0;
  108. //size_t tot = strlen(msg);
  109. get_user_flagrec(u, &fr, chan->dname, chan);
  110. for (; *msg; ++msg) {
  111. if (egg_isupper(*msg))
  112. i++;
  113. }
  114. /* if ((chan->capslimit)) { */
  115. while (((msg) && *msg)) {
  116. if (egg_isupper(*msg))
  117. i++;
  118. msg++;
  119. }
  120. /*
  121. if (chan->capslimit && ((i / tot) >= chan->capslimit)) {
  122. dprintf(DP_MODE, "PRIVMSG %s :flood stats for %s: %d/%d are CAP, percentage: %d\n", chan->name, nick, i, tot, (i/tot)*100);
  123. if ((((i / tot) * 100) >= 50)) {
  124. dprintf(DP_HELP, "PRIVMSG %s :cap flood.\n", chan->dname);
  125. }
  126. */
  127. }
  128. #endif
  129. void unlock_chan(struct chanset_t *chan)
  130. {
  131. if (chan->channel.drone_set_mode) {
  132. char buf[3] = "", *p = buf;
  133. if ((chan->channel.drone_set_mode & CHANINV) && !(chan->mode_pls_prot & CHANINV))
  134. *p++ = 'i';
  135. if ((chan->channel.drone_set_mode & CHANMODER) && !(chan->mode_pls_prot & CHANMODER))
  136. *p++ = 'm';
  137. *p = 0;
  138. dprintf(DP_MODE, "MODE %s :-%s\n", chan->name[0] ? chan->name : chan->dname, buf);
  139. }
  140. chan->channel.drone_set_mode = 0;
  141. }
  142. void detected_drone_flood(struct chanset_t* chan, memberlist* m) {
  143. egg_timeval_t howlong;
  144. chan->channel.drone_set_mode = 0;
  145. char buf[3] = "", *p = buf;
  146. if (!(chan->channel.mode & CHANINV) && !(chan->mode_mns_prot & CHANINV)) {
  147. chan->channel.drone_set_mode |= CHANINV;
  148. *p++ = 'i';
  149. }
  150. if (!(chan->channel.mode & CHANMODER) && !(chan->mode_mns_prot & CHANMODER)) {
  151. chan->channel.drone_set_mode |= CHANMODER;
  152. *p++ = 'm';
  153. }
  154. *p = 0;
  155. if (chan->channel.drone_set_mode && buf[0]) {
  156. chan->channel.drone_joins = 0;
  157. chan->channel.drone_jointime = 0;
  158. dprintf(DP_MODE_NEXT, "MODE %s +%s\n", chan->name[0] ? chan->name : chan->dname, buf);
  159. howlong.sec = chan->flood_lock_time;
  160. howlong.usec = 0;
  161. timer_create_complex(&howlong, "unlock", (Function) unlock_chan, (void *) chan, 0);
  162. putlog(LOG_MISC, "*", "Flood detected in %s! Locking for %d seconds.", chan->dname, chan->flood_lock_time);
  163. }
  164. }
  165. void notice_invite(struct chanset_t *chan, char *handle, char *nick, char *uhost, bool op) {
  166. char fhandle[21] = "";
  167. const char *ops = " (auto-op)";
  168. if (handle)
  169. simple_snprintf(fhandle, sizeof(fhandle), "\002%s\002 ", handle);
  170. putlog(LOG_MISC, "*", "Invited %s%s(%s%s%s) to %s.", handle ? handle : "", handle ? " " : "", nick, uhost ? "!" : "", uhost ? uhost : "", chan->dname);
  171. bd::String msg;
  172. msg = bd::String::printf("\001ACTION has invited %s(%s%s%s) to %s.%s\001",
  173. fhandle, nick, uhost ? "!" : "", uhost ? uhost : "", chan->dname, op ? ops : "");
  174. privmsg(chan->name, msg, DP_MODE);
  175. }
  176. #ifdef CACHE
  177. static cache_t *cache_new(char *nick)
  178. {
  179. cache_t *cache = (cache_t *) my_calloc(1, sizeof(cache_t));
  180. cache->next = NULL;
  181. strlcpy(cache->nick, nick, sizeof(cache->nick));
  182. cache->uhost[0] = 0;
  183. cache->handle[0] = 0;
  184. // cache->user = NULL;
  185. cache->timeval = now;
  186. cache->cchan = NULL;
  187. list_append((struct list_type **) &irccache, (struct list_type *) cache);
  188. return cache;
  189. }
  190. static cache_chan_t *cache_chan_add(cache_t *cache, char *chname)
  191. {
  192. cache_chan_t *cchan = (cache_chan_t *) my_calloc(1, sizeof(cache_chan_t));
  193. cchan->next = NULL;
  194. strlcpy(cchan->dname, chname, sizeof(cchan->dname));
  195. cchan->ban = 0;
  196. cchan->invite = 0;
  197. cchan->invited = 0;
  198. list_append((struct list_type **) &(cache->cchan), (struct list_type *) cchan);
  199. return cchan;
  200. }
  201. static void cache_chan_find(cache_t *cache, cache_chan_t *cchan, char *nick, char *chname)
  202. {
  203. if (!cache)
  204. cache = cache_find(nick);
  205. if (cache) {
  206. for (cchan = cache->cchan; cchan && cchan->dname[0]; cchan = cchan->next) {
  207. if (!rfc_casecmp(cchan->dname, chname)) {
  208. return;
  209. }
  210. }
  211. }
  212. return;
  213. }
  214. static void cache_chan_del(char *nick, char *chname) {
  215. cache_t *cache = NULL;
  216. cache_chan_t *cchan = NULL;
  217. cache_chan_find(cache, cchan, nick, chname);
  218. if (cchan) {
  219. list_delete((struct list_type **) &cache->cchan, (struct list_type *) cchan);
  220. free(cache);
  221. }
  222. }
  223. static cache_t *cache_find(char *nick)
  224. {
  225. cache_t *cache = NULL;
  226. for (cache = irccache; cache && cache->nick[0]; cache = cache->next)
  227. if (!rfc_casecmp(cache->nick, nick))
  228. break;
  229. return cache;
  230. }
  231. static void cache_del(char *nick, cache_t *cache)
  232. {
  233. if (!cache)
  234. cache = cache_find(nick);
  235. if (cache) {
  236. list_delete((struct list_type **) &irccache, (struct list_type *) cache);
  237. free(cache);
  238. }
  239. }
  240. static void cache_debug(void)
  241. {
  242. cache_t *cache = NULL;
  243. cache_chan_t *cchan = NULL;
  244. for (cache = irccache; cache && cache->nick[0]; cache = cache->next) {
  245. dprintf(DP_MODE, "PRIVMSG #wraith :%s!%s (%s)\n", cache->nick, cache->uhost, cache->handle);
  246. for (cchan = cache->cchan; cchan && cchan->dname[0]; cchan = cchan->next)
  247. dprintf(DP_MODE, "PRIVMSG #wraith :%s %d %d %d\n", cchan->dname, cchan->ban, cchan->invite, cchan->invited);
  248. }
  249. }
  250. #endif /* CACHE */
  251. static void cache_invite(struct chanset_t *chan, char *nick, char *host, char *handle, bool op, bool bot)
  252. {
  253. #ifdef CACHE
  254. cache_t *cache = NULL;
  255. if ((cache = cache_find(nick)) == NULL)
  256. cache = cache_new(nick);
  257. cache->bot = bot;
  258. /* if we find they have a host but it doesnt match the new host, wipe it */
  259. if (host && cache->uhost[0] && strcasecmp(cache->uhost, host))
  260. cache->uhost[0] = 0;
  261. if (host && !cache->uhost[0])
  262. strlcpy(cache->uhost, host, sizeof(cache->uhost));
  263. /* if we find they have a handle but it doesnt match the new handle, wipe it */
  264. if (handle && cache->handle[0] && strcasecmp(cache->handle, handle))
  265. cache->handle[0] = 0;
  266. if (handle && !cache->handle[0])
  267. strlcpy(cache->handle, handle, sizeof(cache->handle));
  268. cache_chan_t *cchan = cache_chan_add(cache, chan->dname);
  269. cchan->op = op;
  270. if (!host) {
  271. cchan->invite = 1;
  272. dprintf(DP_MODE, "USERHOST %s\n", nick);
  273. return;
  274. }
  275. /* if we have a uhost already, it's safe to invite them */
  276. cchan->invited = 1;
  277. cchan->invite = 0;
  278. #endif /* CACHE */
  279. dprintf(DP_SERVER, "INVITE %s %s\n", nick, chan->name);
  280. }
  281. static inline const char * cookie_hash(const char* chname, const memberlist* opper, const memberlist* opped, const char* ts, const char* randstring, const char* key) {
  282. char tohash[201] = "";
  283. const char salt2[] = SALT2;
  284. simple_snprintf(tohash, sizeof(tohash), STR("%c%s%c%c%c\n%c%c%s%s%s"),
  285. salt2[0],
  286. ts,
  287. randstring[0], randstring[1], randstring[2], randstring[3],
  288. salt2[15],
  289. opped->nick,
  290. opped->userhost,
  291. key);
  292. #ifdef DEBUG
  293. sdprintf("chname: %s ts: %s randstring: %c%c%c%c", chname, ts, randstring[0], randstring[1], randstring[2], randstring[3]);
  294. sdprintf("tohash: %s", tohash);
  295. #endif
  296. const char* md5 = MD5(tohash);
  297. OPENSSL_cleanse(tohash, sizeof(tohash));
  298. return md5;
  299. }
  300. static inline void cookie_key(char *key, size_t key_len, const char* randstring, const memberlist *opper, const char *chname) {
  301. const char salt1[] = SALT1;
  302. const char salt2[] = SALT2;
  303. simple_snprintf2(key, key_len, STR("%c%c%c%^s%c%c%c%c%c%c%^s%c%c%c%c%c%c%c%s"),
  304. randstring[0],
  305. salt1[5],
  306. randstring[3],
  307. opper->user->handle,
  308. randstring[2],
  309. salt1[4],
  310. salt1[0],
  311. salt1[1],
  312. salt1[3],
  313. salt1[6],
  314. chname,
  315. salt1[10],
  316. randstring[1],
  317. salt2[15],
  318. salt2[13],
  319. salt1[10],
  320. salt2[3],
  321. salt2[1],
  322. opper->userhost
  323. );
  324. }
  325. #define HASH_INDEX1(_x) (8 + (_x))
  326. #define HASH_INDEX2(_x) (16 + (_x))
  327. #define HASH_INDEX3(_x) (18 + (_x))
  328. void makecookie(char *out, size_t len, const char *chname, const memberlist* opper, const memberlist* m1, const memberlist* m2, const memberlist* m3) {
  329. char randstring[5] = "", ts[11] = "";
  330. make_rand_str(randstring, 4);
  331. /* &ts[4] is now last 6 digits of time */
  332. simple_snprintf(ts, sizeof(ts), "%010li", (long) (now + timesync));
  333. char cookie_clear[101] = "";
  334. //Increase my counter
  335. ++my_cookie_counter;
  336. if (my_cookie_counter > (unsigned long)(-500))
  337. my_cookie_counter = 0;
  338. simple_snprintf2(cookie_clear, sizeof(cookie_clear), STR("%s%s%D"), randstring, &ts[3], my_cookie_counter);
  339. char key[150] = "";
  340. cookie_key(key, sizeof(key), randstring, opper, chname);
  341. const char* hash1 = cookie_hash(chname, opper, m1, &ts[4], randstring, key);
  342. const char* hash2 = m2 ? cookie_hash(chname, opper, m2, &ts[4], randstring, key) : NULL;
  343. const char* hash3 = m3 ? cookie_hash(chname, opper, m3, &ts[4], randstring, key) : NULL;
  344. bd::String cookie = encrypt_string(MD5(key), bd::String(cookie_clear));
  345. cookie = bd::base64Encode(cookie);
  346. #ifdef DEBUG
  347. sdprintf("key: %s", key);
  348. sdprintf("cookie_clear: %s", cookie_clear);
  349. sdprintf("hash1: %s", hash1);
  350. if (hash2) sdprintf("hash2: %s", hash2);
  351. if (hash3) sdprintf("hash3: %s", hash3);
  352. #endif
  353. if (m3)
  354. simple_snprintf(out, len + 1, STR("%c%c%c%c%c%c%c%c%c!%s@%s"),
  355. hash1[HASH_INDEX1(0)],
  356. hash1[HASH_INDEX2(0)],
  357. hash1[HASH_INDEX3(0)],
  358. hash2[HASH_INDEX1(1)],
  359. hash2[HASH_INDEX2(1)],
  360. hash2[HASH_INDEX3(1)],
  361. hash3[HASH_INDEX1(2)],
  362. hash3[HASH_INDEX2(2)],
  363. hash3[HASH_INDEX3(2)],
  364. randstring,
  365. cookie.c_str());
  366. else if (m2)
  367. simple_snprintf(out, len + 1, STR("%c%c%c%c%c%c!%s@%s"),
  368. hash1[HASH_INDEX1(0)],
  369. hash1[HASH_INDEX2(0)],
  370. hash1[HASH_INDEX3(0)],
  371. hash2[HASH_INDEX1(1)],
  372. hash2[HASH_INDEX2(1)],
  373. hash2[HASH_INDEX3(1)],
  374. randstring,
  375. cookie.c_str());
  376. else
  377. simple_snprintf(out, len + 1, STR("%c%c%c!%s@%s"),
  378. hash1[HASH_INDEX1(0)],
  379. hash1[HASH_INDEX2(0)],
  380. hash1[HASH_INDEX3(0)],
  381. randstring,
  382. cookie.c_str());
  383. #ifdef DEBUG
  384. sdprintf("cookie: %s", out);
  385. #endif
  386. }
  387. // Clear counter for bot
  388. void counter_clear(const char* botnick) {
  389. bot_counters[botnick] = 0;
  390. }
  391. static inline int checkcookie(const char *chname, const memberlist* opper, const memberlist* opped, const char *cookie, int indexHint) {
  392. #define HOST(_x) (6 + (_x) + ((hashes << 1) + hashes)) /* x + (hashes * 3) */
  393. #define SALT(_x) (1 + (_x) + ((hashes << 1) + hashes)) /* x + (hashes * 3) */
  394. /* How many hashes are in the cookie? */
  395. const size_t hashes = cookie[3] == '!' ? 1 : (cookie[6] == '!' ? 2 : 3);
  396. char key[150] = "";
  397. const char *randstring = &cookie[SALT(0)];
  398. cookie_key(key, sizeof(key), randstring, opper, chname);
  399. bd::String ciphertext = bd::base64Decode((char*) &cookie[HOST(0)]);
  400. bd::String cleartext = decrypt_string(MD5(key), ciphertext);
  401. char ts[8] = "";
  402. strlcpy(ts, cleartext.c_str() + 4, sizeof(ts));
  403. unsigned long counter = base64_to_int(cleartext.c_str() + 4 + 7);
  404. //Lookup counter for the opper
  405. unsigned long last_counter = 0;
  406. // Don't check counter for my own ops as it may have incremented in the queue before seeing the last last counter
  407. bd::String handle;
  408. if (conf.bot->u != opper->user) {
  409. handle = opper->user->handle;
  410. if (bot_counters.contains(handle))
  411. last_counter = bot_counters[handle];
  412. else
  413. last_counter = 0;
  414. }
  415. #ifdef DEBUG
  416. sdprintf("key: %s", key);
  417. sdprintf("plaintext from cookie: %s", cleartext.c_str());
  418. sdprintf("ts from cookie: %s", ts);
  419. if (indexHint == 0) {
  420. sdprintf("last counter from %s: %lu", opper->user->handle, last_counter);
  421. sdprintf("counter from cookie: %lu", counter);
  422. }
  423. #endif
  424. const time_t optime = atol(ts);
  425. if ((((now + timesync) % 10000000) - optime) > 3900)
  426. return BC_SLACK;
  427. //Only check on the first cookie
  428. if (indexHint == 0 && conf.bot->u != opper->user) {
  429. if (counter <= last_counter)
  430. return BC_COUNTER;
  431. // graceful overflow
  432. if (counter > (unsigned long)(-1000))
  433. counter = 0;
  434. //Update counter for the opper
  435. bot_counters[handle] = counter;
  436. }
  437. const char *hash = cookie_hash(chname, opper, opped, &ts[1], randstring, key);
  438. #ifdef DEBUG
  439. sdprintf("hash: %s", hash);
  440. #endif
  441. /* Compare the expected hash to each of the given hashes */
  442. /* indexHint, Which position of the +ooo are we? (1) could be either index (1) or (2).. but not (0). */
  443. /* See if any of the cookies match the hash we want */
  444. for (size_t i = indexHint; i < hashes; ++i) {
  445. const char *cookie_index = cookie + ((i << 1) + i); /* i * 3 */
  446. if ((hash[HASH_INDEX1(i)] == cookie_index[0] &&
  447. hash[HASH_INDEX2(i)] == cookie_index[1] &&
  448. hash[HASH_INDEX3(i)] == cookie_index[2])) {
  449. return 0;
  450. }
  451. }
  452. /* None matched -> failure */
  453. return BC_HASH;
  454. #undef HOST
  455. #undef SALT
  456. }
  457. /*
  458. opreq = o #chan nick
  459. inreq = i #chan nick uhost
  460. inreq keyreply = K #chan key
  461. */
  462. void
  463. getin_request(char *botnick, char *code, char *par)
  464. {
  465. if (unlikely(!server_online))
  466. return;
  467. if (unlikely(!par[0] || !par))
  468. return;
  469. char *what = newsplit(&par), *chname = newsplit(&par);
  470. if (unlikely(!chname[0] || !chname))
  471. return;
  472. char *tmp = newsplit(&par); /* nick */
  473. if (unlikely(!tmp[0]))
  474. return;
  475. char nick[NICKLEN] = "";
  476. strlcpy(nick, tmp, sizeof(nick));
  477. const char *type = what[0] == 'o' ? "op" : "in", *desc = what[0] == 'o' ? "on" : "for";
  478. struct chanset_t *chan = findchan_by_dname(chname);
  479. if (unlikely(!chan)) {
  480. putlog(LOG_GETIN, "*", "%sreq from %s/%s %s %s which is not a valid channel!", type, botnick, nick, desc, chname);
  481. return;
  482. }
  483. struct userrec *u = get_user_by_handle(userlist, botnick);
  484. if (unlikely(!u)) {
  485. putlog(LOG_GETIN, "*", "%sreq from %s/%s %s %s - No user called %s in userlist", type, botnick, nick, desc,
  486. chan->dname, botnick);
  487. return;
  488. }
  489. if (connect_bursting) {
  490. putlog(LOG_GETIN, "*", "%sreq from %s/%s %s %s - I'm in connect burst mode.", type, botnick, nick, desc, chan->dname);
  491. return;
  492. }
  493. if (server_lag > lag_threshold) {
  494. putlog(LOG_GETIN, "*", "%sreq from %s/%s %s %s - I'm too lagged", type, botnick, nick, desc, chan->dname);
  495. return;
  496. }
  497. if (what[0] != 'K') {
  498. if (!(channel_pending(chan) || channel_active(chan))) {
  499. putlog(LOG_GETIN, "*", "%sreq from %s/%s %s %s - I'm not on %s right now.", type, botnick, nick, desc,
  500. chan->dname, chan->dname);
  501. return;
  502. }
  503. }
  504. memberlist* mem = ismember(chan, nick);
  505. if (mem && chan_issplit(mem)) {
  506. putlog(LOG_GETIN, "*", "%sreq from %s/%s %s %s - %s is split", type, botnick, nick, desc, chan->dname, nick);
  507. return;
  508. }
  509. if (what[0] == 'K') {
  510. if (unlikely(!shouldjoin(chan))) {
  511. putlog(LOG_GETIN, "*", "Got key for %s from %s - I shouldn't be on that chan?!?", chan->dname, botnick);
  512. } else {
  513. if (!(channel_pending(chan) || channel_active(chan))) {
  514. my_setkey(chan, nick);
  515. putlog(LOG_GETIN, "*", "Got key for %s from %s (%s) - Joining", chan->dname, botnick, chan->channel.key);
  516. join_chan(chan);
  517. } else {
  518. putlog(LOG_GETIN, "*", "Got key for %s from %s - I'm already in the channel", chan->dname, botnick);
  519. }
  520. }
  521. return;
  522. }
  523. char uhost[UHOSTLEN] = "";
  524. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  525. tmp = newsplit(&par); /* userhost */
  526. if (tmp[0])
  527. strlcpy(uhost, tmp, sizeof(uhost));
  528. if (what[0] == 'o') {
  529. if (!me_op(chan)) {
  530. putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - I haven't got ops", botnick, nick, chan->dname);
  531. return;
  532. }
  533. if (!mem) {
  534. putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - %s isn't on %s", botnick, nick, chan->dname, nick, chan->dname);
  535. return;
  536. }
  537. if (chan_hasop(mem)) {
  538. putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - %s already has ops", botnick, nick, chan->dname, nick);
  539. return;
  540. }
  541. if (chan_sentop(mem)) {
  542. putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - Already sent a +o", botnick, nick, chan->dname);
  543. return;
  544. }
  545. member_getuser(mem);
  546. if (mem->user != u) {
  547. putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - user mismatch: (%s)/(%s)", botnick, nick, chan->dname, u ? u->handle : "*", mem->user ? mem->user->handle : "*");
  548. return;
  549. }
  550. get_user_flagrec(u, &fr, chan->dname, chan);
  551. if (!chk_op(fr, chan)) {
  552. putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - %s doesnt have +o for chan.", botnick, nick, chan->dname, botnick);
  553. return;
  554. }
  555. if (unlikely(glob_kick(fr) || chan_kick(fr))) {
  556. putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - %s is currently being autokicked.", botnick, nick, chan->dname, botnick);
  557. return;
  558. }
  559. // Does the remote bot have the same number of clients in its channel as me? And a shared member?
  560. int members = atoi(tmp);
  561. char *shared_nick = par[0] ? newsplit(&par) : NULL;
  562. memberlist* shared_member = shared_nick ? ismember(chan, shared_nick) : NULL;
  563. char *shared_host = par[0] ? newsplit(&par) : NULL;
  564. if (!shared_nick || !shared_member || !shared_host || !((chan->channel.members - chan->channel.splitmembers) == members) || strcmp(shared_host, shared_member->userhost)) {
  565. putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - Bot seems to be on a different network???", botnick, nick, chan->dname);
  566. return;
  567. }
  568. if (chan->channel.no_op) {
  569. if (chan->channel.no_op > now) /* dont op until this time has passed */
  570. return;
  571. else
  572. chan->channel.no_op = 0;
  573. }
  574. do_op(nick, chan, 0, 1);
  575. putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - Opped", botnick, nick, chan->dname);
  576. } else if (what[0] == 'i') {
  577. if (mem) {
  578. putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - %s is already on %s", botnick, nick, chan->dname, nick, chan->dname);
  579. return;
  580. }
  581. get_user_flagrec(u, &fr, chan->dname, chan);
  582. if (unlikely(!chk_op(fr, chan) || chan_kick(fr) || glob_kick(fr))) {
  583. putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - %s doesn't have acces for chan.", botnick, nick, chan->dname, botnick);
  584. return;
  585. }
  586. if (chan->channel.mode & CHANKEY) {
  587. char *key = chan->channel.key[0] ? chan->channel.key : chan->key_prot;
  588. size_t siz = strlen(chan->dname) + strlen(key ? key : 0) + 6 + 1;
  589. tmp = (char *) my_calloc(1, siz);
  590. simple_snprintf(tmp, siz, "gi K %s %s", chan->dname, key ? key : "");
  591. putbot(botnick, tmp);
  592. putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - Sent key (%s)", botnick, nick, chan->dname, key ? key : "");
  593. free(tmp);
  594. }
  595. if (!me_op(chan)) {
  596. putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - I haven't got ops", botnick, nick, chan->dname);
  597. return;
  598. }
  599. bool sendi = 0;
  600. if (chan->channel.maxmembers) {
  601. int lim = (chan->channel.members - chan->channel.splitmembers) + 5, curlim = chan->channel.maxmembers;
  602. if (curlim < lim) {
  603. char s2[6] = "";
  604. sendi = 1;
  605. simple_snprintf(s2, sizeof(s2), "%d", lim);
  606. add_mode(chan, '+', 'l', s2);
  607. putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - Raised limit to %d", botnick, nick, chan->dname, lim);
  608. }
  609. }
  610. char uip[UHOSTLEN] = "";
  611. tmp = newsplit(&par); /* userip */
  612. if (tmp[0])
  613. simple_snprintf(uip, sizeof uip, "%s!%s", nick, tmp);
  614. struct maskrec **mr = NULL, *tmr = NULL;
  615. /* Check internal global bans */
  616. mr = &global_bans;
  617. while (*mr) {
  618. if (unlikely(wild_match((*mr)->mask, uhost) ||
  619. wild_match((*mr)->mask, uip) ||
  620. match_cidr((*mr)->mask, uip))) {
  621. if (!noshare)
  622. shareout("-m b %s\n", (*mr)->mask);
  623. putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - Removed permanent global ban %s", botnick, nick,
  624. chan->dname, (*mr)->mask);
  625. free((*mr)->mask);
  626. if ((*mr)->desc)
  627. free((*mr)->desc);
  628. if ((*mr)->user)
  629. free((*mr)->user);
  630. tmr = *mr;
  631. *mr = (*mr)->next;
  632. free(tmr);
  633. } else {
  634. mr = &((*mr)->next);
  635. }
  636. }
  637. /* Check internal channel bans */
  638. mr = &chan->bans;
  639. while (*mr) {
  640. if (unlikely(wild_match((*mr)->mask, uhost) ||
  641. wild_match((*mr)->mask, uip) ||
  642. match_cidr((*mr)->mask, uip))) {
  643. if (!noshare)
  644. shareout("-mc b %s %s\n", chan->dname, (*mr)->mask);
  645. putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - Removed permanent channel ban %s", botnick, nick,
  646. chan->dname, (*mr)->mask);
  647. free((*mr)->mask);
  648. if ((*mr)->desc)
  649. free((*mr)->desc);
  650. if ((*mr)->user)
  651. free((*mr)->user);
  652. tmr = *mr;
  653. *mr = (*mr)->next;
  654. free(tmr);
  655. } else {
  656. mr = &((*mr)->next);
  657. }
  658. }
  659. /* Check bans active in channel */
  660. for (struct maskstruct *b = chan->channel.ban; b->mask[0]; b = b->next) {
  661. if (unlikely(wild_match(b->mask, uhost) ||
  662. wild_match(b->mask, uip) ||
  663. match_cidr(b->mask, uip))) {
  664. add_mode(chan, '-', 'b', b->mask);
  665. putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - Removed active ban %s", botnick, nick, chan->dname,
  666. b->mask);
  667. sendi = 1;
  668. }
  669. }
  670. if (chan->channel.mode & CHANINV) {
  671. sendi = 1;
  672. putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - Invited", botnick, nick, chan->dname);
  673. }
  674. if (sendi)
  675. dprintf(DP_MODE, "INVITE %s %s\n", nick, chan->name[0] ? chan->name : chan->dname);
  676. }
  677. }
  678. static void
  679. request_op(struct chanset_t *chan)
  680. {
  681. if (!chan || (chan && (channel_pending(chan) || !shouldjoin(chan) || !channel_active(chan) || me_op(chan))))
  682. return;
  683. if (chan->channel.no_op) {
  684. if (chan->channel.no_op > now) /* dont op until this time has passed */
  685. return;
  686. else
  687. chan->channel.no_op = 0;
  688. }
  689. chan->channel.do_opreq = 0;
  690. /* check server lag */
  691. if (server_lag > lag_threshold) {
  692. putlog(LOG_GETIN, "*", "Not asking for ops on %s - I'm too lagged", chan->dname);
  693. chan->channel.no_op = now + 10;
  694. return;
  695. }
  696. /* Check if my hostmask is recognized (every 10th time) */
  697. static int check_hostmask_cnt = 10;
  698. if (check_hostmask_cnt++ == 10) {
  699. check_hostmask();
  700. check_hostmask_cnt = 0;
  701. }
  702. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_BOT, 0, 0, 0 };
  703. get_user_flagrec(conf.bot->u, &fr, chan->dname, chan);
  704. if (!chk_op(fr, chan) || glob_kick(fr) || chan_kick(fr)) {
  705. putlog(LOG_GETIN, "*", "Not requesting op for %s - I do not have access to that channel.", chan->dname);
  706. chan->channel.no_op = now + 10;
  707. return;
  708. }
  709. int i = 0, my_exp = 0, first = 100;
  710. time_t n = now;
  711. /* max OPREQ_COUNT requests per OPREQ_SECONDS sec */
  712. while (i < 5) {
  713. if (n - chan->opreqtime[i] > op_requests.time) {
  714. if (first > i)
  715. first = i;
  716. my_exp++;
  717. chan->opreqtime[i] = 0;
  718. }
  719. i++;
  720. }
  721. if ((5 - my_exp) >= op_requests.count) {
  722. putlog(LOG_GETIN, "*", "Delaying opreq for %s - Maximum of %d:%d reached", chan->dname, op_requests.count,
  723. op_requests.time);
  724. chan->channel.no_op = now + op_requests.time + 1;
  725. return;
  726. }
  727. int cnt = op_bots, i2 = 0;
  728. memberlist *ml = NULL, *botops[MAX_BOTS];
  729. char s[UHOSTLEN] = "";
  730. for (i = 0, ml = chan->channel.member; (i < MAX_BOTS) && ml && ml->nick[0]; ml = ml->next) {
  731. if (!chan_hasop(ml) || chan_issplit(ml))
  732. continue;
  733. member_getuser(ml, 1);
  734. if (!ml->user || !ml->user->bot)
  735. continue;
  736. /* Is the bot linked? */
  737. if (findbot(ml->user->handle))
  738. botops[i++] = ml;
  739. }
  740. if (!i) {
  741. if (channel_active(chan) && !channel_pending(chan)) {
  742. chan->channel.no_op = now + op_requests.time;
  743. putlog(LOG_GETIN, "*", "No one to ask for ops on %s - Delaying requests for %d seconds.", chan->dname, op_requests.time);
  744. }
  745. return;
  746. }
  747. char l[1024] = "";
  748. size_t len = 0;
  749. // Pick a random member to use as verification
  750. memberlist* shared_member = NULL;
  751. for (int z = 0; z < 5; ++z) {
  752. int shared_member_cnt = z < 4 ? randint(chan->channel.members) : 0;
  753. int shared_idx = 0;
  754. for (shared_member = chan->channel.member; shared_member && shared_member->nick[0]; shared_member = shared_member->next) {
  755. if (shared_member->split) continue;
  756. if (shared_idx >= shared_member_cnt)
  757. break;
  758. ++shared_idx;
  759. }
  760. if (shared_member) break;
  761. }
  762. if (!shared_member) {
  763. chan->channel.no_op = now + op_requests.time;
  764. putlog(LOG_GETIN, "*", "Too many split clients on %s - Delaying requests for %d seconds.", chan->dname, op_requests.time);
  765. return;
  766. }
  767. /* first scan for bots on my server, ask first found for ops */
  768. simple_snprintf(s, sizeof(s), "gi o %s %s %d %s %s", chan->dname, botname, (chan->channel.members - chan->channel.splitmembers), shared_member->nick, shared_member->userhost);
  769. /* look for bots 0-1 hops away */
  770. for (i2 = 0; i2 < i; i2++) {
  771. if (botops[i2]->hops < 2) {
  772. putbot(botops[i2]->user->handle, s);
  773. chan->opreqtime[first] = n;
  774. if (l[0])
  775. len += strlcpy(l + len, ", ", sizeof(l) - len);
  776. len += strlcpy(l + len, botops[i2]->user->handle, sizeof(l) - len);
  777. len += strlcpy(l + len, "/", sizeof(l) - len);
  778. len += strlcpy(l + len, botops[i2]->nick, sizeof(l) - len);
  779. botops[i2] = NULL;
  780. --cnt;
  781. break;
  782. }
  783. //FIXME: Also prefer our localhub or child bots
  784. }
  785. /* Pick random op and ask for ops */
  786. while (cnt) {
  787. i2 = randint(i);
  788. if (botops[i2]) {
  789. putbot(botops[i2]->user->handle, s);
  790. chan->opreqtime[first] = n;
  791. if (l[0])
  792. len += strlcpy(l + len, ", ", sizeof(l) - len);
  793. len += strlcpy(l + len, botops[i2]->user->handle, sizeof(l) - len);
  794. len += strlcpy(l + len, "/", sizeof(l) - len);
  795. len += strlcpy(l + len, botops[i2]->nick, sizeof(l) - len);
  796. --cnt;
  797. botops[i2] = NULL;
  798. } else {
  799. if (i < op_bots)
  800. cnt--;
  801. }
  802. }
  803. l[len] = 0;
  804. putlog(LOG_GETIN, "*", "Requested ops on %s from %s", chan->dname, l);
  805. }
  806. static void
  807. request_in(struct chanset_t *chan)
  808. {
  809. /* Lag situation */
  810. if (!shouldjoin(chan))
  811. return;
  812. /* Check if my hostmask is recognized (every 10th time) */
  813. static int check_hostmask_cnt = 10;
  814. if (check_hostmask_cnt++ == 10) {
  815. check_hostmask();
  816. check_hostmask_cnt = 0;
  817. }
  818. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_BOT, 0, 0, 0 };
  819. get_user_flagrec(conf.bot->u, &fr, chan->dname, chan);
  820. if (!chk_op(fr, chan) || glob_kick(fr) || chan_kick(fr)) {
  821. putlog(LOG_GETIN, "*", "Not requesting help to join %s - I do not have access to that channel.", chan->dname);
  822. return;
  823. }
  824. int foundBots = 0;
  825. char* botops[MAX_BOTS];
  826. for (tand_t* bot = tandbot; bot && (foundBots < MAX_BOTS); bot = bot->next) {
  827. if (bot->hub || !bot->u)
  828. continue;
  829. get_user_flagrec(bot->u, &fr, chan->dname, chan);
  830. if (bot_shouldjoin(bot->u, &fr, chan) && chk_op(fr, chan))
  831. botops[foundBots++] = bot->bot;
  832. }
  833. if (!foundBots) {
  834. putlog(LOG_GETIN, "*", "No bots available, can't request help to join %s", chan->dname);
  835. return;
  836. }
  837. int cnt = foundBots < in_bots ? foundBots : in_bots;
  838. char s[255] = "";
  839. char l[1024] = "";
  840. size_t len = 0;
  841. simple_snprintf(s, sizeof(s), "gi i %s %s %s!%s %s", chan->dname, botname, botname, botuserhost, botuserip);
  842. shuffleArray(botops, foundBots);
  843. for (int n = 0; n < cnt; ++n) {
  844. putbot(botops[n], s);
  845. if (l[0])
  846. len += strlcpy(l + len, ", ", sizeof(l) - len);
  847. len += strlcpy(l + len, botops[n], sizeof(l) - len);
  848. }
  849. l[len] = 0;
  850. putlog(LOG_GETIN, "*", "Requested help to join %s from %s", chan->dname, l);
  851. }
  852. #ifdef REVENGE
  853. /* Contains the logic to decide wether we want to punish someone. Returns
  854. * true (1) if we want to, false (0) if not.
  855. */
  856. static bool
  857. want_to_revenge(struct chanset_t *chan, struct userrec *u,
  858. struct userrec *u2, char *badnick, char *victimstr, bool mevictim)
  859. {
  860. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  861. /* Do not take revenge upon ourselves. */
  862. if (match_my_nick(badnick))
  863. return 0;
  864. get_user_flagrec(u, &fr, chan->dname, chan);
  865. /* Kickee didn't kick themself? */
  866. if (rfc_casecmp(badnick, victimstr)) {
  867. /* They kicked me? */
  868. if (mevictim) {
  869. /* ... and I'm allowed to take revenge? <snicker> */
  870. if (channel_revengebot(chan))
  871. return 1;
  872. /* Do we revenge for our users ... and do we actually know the victim? */
  873. } else if (channel_revenge(chan) && u2) {
  874. struct flag_record fr2 = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  875. get_user_flagrec(u2, &fr2, chan->dname, chan);
  876. /* Protecting friends? */
  877. /* Protecting ops? */
  878. if ((channel_protectops(chan) && chk_op(fr2, chan)))
  879. return 1;
  880. }
  881. }
  882. return 0;
  883. }
  884. /* Dependant on revenge_mode, punish the offender.
  885. */
  886. static void
  887. punish_badguy(struct chanset_t *chan, char *whobad,
  888. struct userrec *u, char *badnick, char *victimstr, bool mevictim, int type)
  889. {
  890. memberlist *m = ismember(chan, badnick);
  891. if (!m)
  892. return;
  893. char reason[1024] = "", ct[81] = "", *kick_msg = NULL;
  894. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  895. get_user_flagrec(u, &fr, chan->dname, chan);
  896. /* Get current time into a string */
  897. strftime(ct, sizeof ct, "%d %b %Z", gmtime(&now));
  898. /* Put together log and kick messages */
  899. reason[0] = 0;
  900. switch (type) {
  901. case REVENGE_KICK:
  902. kick_msg = "don't kick my friends, bud";
  903. simple_snprintf(reason, sizeof(reason), "kicked %s off %s", victimstr, chan->dname);
  904. break;
  905. case REVENGE_DEOP:
  906. simple_snprintf(reason, sizeof(reason), "deopped %s on %s", victimstr, chan->dname);
  907. kick_msg = "don't deop my friends, bud";
  908. break;
  909. default:
  910. kick_msg = "revenge!";
  911. }
  912. putlog(LOG_MISC, chan->dname, "Punishing %s (%s)", badnick, reason);
  913. /* Set the offender +d */
  914. if ((chan->revenge_mode > 0) &&
  915. /* ... unless there's no more to do */
  916. !(chan_deop(fr) || glob_deop(fr))) {
  917. char s[UHOSTLEN], s1[UHOSTLEN];
  918. memberlist *mx = NULL;
  919. /* Removing op */
  920. if (chk_op(fr, chan)) {
  921. fr.match = FR_CHAN;
  922. if (chan_op(fr)) {
  923. fr.chan &= ~USER_OP;
  924. } else {
  925. fr.chan |= USER_DEOP;
  926. }
  927. set_user_flagrec(u, &fr, chan->dname);
  928. putlog(LOG_MISC, "*", "No longer opping %s[%s] (%s)", u->handle, whobad, reason);
  929. }
  930. /* ... or just setting to deop */
  931. else if (u) {
  932. /* In the user list already, cool :) */
  933. fr.match = FR_CHAN;
  934. fr.chan |= USER_DEOP;
  935. set_user_flagrec(u, &fr, chan->dname);
  936. simple_snprintf(s, sizeof(s), "(%s) %s", ct, reason);
  937. putlog(LOG_MISC, "*", "Now deopping %s[%s] (%s)", u->handle, whobad, s);
  938. }
  939. /* ... or creating new user and setting that to deop */
  940. else {
  941. strlcpy(s1, whobad, sizeof(s1));
  942. maskhost(s1, s);
  943. strlcpy(s1, badnick, sizeof(s1));
  944. /* If that handle exists use "badX" (where X is an increasing number)
  945. * instead.
  946. */
  947. while (get_user_by_handle(userlist, s1)) {
  948. if (!strncmp(s1, "bad", 3)) {
  949. int i;
  950. i = atoi(s1 + 3);
  951. simple_snprintf(s1 + 3, sizeof(s1) - 3, "%d", i + 1);
  952. } else
  953. strlcpy(s1, "bad1", sizeof(s1)); /* Start with '1' */
  954. }
  955. userlist = adduser(userlist, s1, s, "-", 0, 0);
  956. fr.match = FR_CHAN;
  957. fr.chan = USER_DEOP;
  958. u = get_user_by_handle(userlist, s1);
  959. if ((mx = ismember(chan, badnick)))
  960. mx->user = u;
  961. set_user_flagrec(u, &fr, chan->dname);
  962. simple_snprintf(s, sizeof(s), "(%s) %s (%s)", ct, reason, whobad);
  963. set_user(&USERENTRY_COMMENT, u, (void *) s);
  964. putlog(LOG_MISC, "*", "Now deopping %s (%s)", whobad, reason);
  965. }
  966. }
  967. /* Always try to deop the offender */
  968. if (!mevictim)
  969. add_mode(chan, '-', 'o', badnick);
  970. /* Ban. Should be done before kicking. */
  971. if (chan->revenge_mode > 2) {
  972. char s[UHOSTLEN] = "", s1[UHOSTLEN] = "";
  973. splitnick(&whobad);
  974. maskhost(whobad, s1);
  975. simple_snprintf(s, sizeof(s), "(%s) %s", ct, reason);
  976. u_addmask('b', chan, s1, conf.bot->nick, s, now + (60 * chan->ban_time), 0);
  977. if (!mevictim && me_op(chan)) {
  978. add_mode(chan, '+', 'b', s1);
  979. flush_mode(chan, QUICK);
  980. }
  981. }
  982. /* Kick the offender */
  983. if ((chan->revenge_mode > 1) && !chan_sentkick(m) &&
  984. /* ... and can I actually do anything about it? */
  985. me_op(chan) && !mevictim) {
  986. dprintf(DP_MODE, "KICK %s %s :%s%s\n", chan->name, m->nick, bankickprefix, response(RES_MEAN));
  987. m->flags |= SENTKICK;
  988. }
  989. }
  990. /* Punishes bad guys under certain circumstances using methods as defined
  991. * by the revenge_mode flag.
  992. */
  993. static void
  994. maybe_revenge(struct chanset_t *chan, char *whobad, char *whovictim, int type)
  995. {
  996. if (!chan || (type < 0))
  997. return;
  998. /* Get info about offender */
  999. struct userrec *u = get_user_by_host(whobad), *u2 = NULL;
  1000. char *badnick = splitnick(&whobad), *victimstr = NULL;
  1001. bool mevictim;
  1002. /* Get info about victim */
  1003. u2 = get_user_by_host(whovictim);
  1004. victimstr = splitnick(&whovictim);
  1005. mevictim = match_my_nick(victimstr);
  1006. /* Do we want to revenge? */
  1007. if (!want_to_revenge(chan, u, u2, badnick, victimstr, mevictim))
  1008. return; /* No, leave them alone ... */
  1009. /* Haha! Do the vengeful thing ... */
  1010. punish_badguy(chan, whobad, u, badnick, victimstr, mevictim, type);
  1011. }
  1012. #endif
  1013. /* Set the key.
  1014. */
  1015. void
  1016. my_setkey(struct chanset_t *chan, char *k)
  1017. {
  1018. free(chan->channel.key);
  1019. chan->channel.key = k ? strdup(k) : (char *) my_calloc(1, 1);
  1020. }
  1021. /* Adds a ban, exempt or invite mask to the list
  1022. * m should be chan->channel.(exempt|invite|ban)
  1023. */
  1024. static bool
  1025. new_mask(masklist *m, char *s, char *who)
  1026. {
  1027. for (; m && m->mask[0] && rfc_casecmp(m->mask, s); m = m->next) ;
  1028. if (m->mask[0])
  1029. return 1; /* Already existent mask */
  1030. m->next = (masklist *) my_calloc(1, sizeof(masklist));
  1031. m->next->next = NULL;
  1032. m->next->mask = (char *) my_calloc(1, 1);
  1033. free(m->mask);
  1034. m->mask = strdup(s);
  1035. m->who = strdup(who);
  1036. m->timer = now;
  1037. return 0;
  1038. }
  1039. /* Removes a nick from the channel member list (returns 1 if successful)
  1040. */
  1041. static bool
  1042. killmember(struct chanset_t *chan, char *nick)
  1043. {
  1044. memberlist *x = NULL, *old = NULL;
  1045. for (x = chan->channel.member; x && x->nick[0]; old = x, x = x->next)
  1046. if (!rfc_casecmp(x->nick, nick))
  1047. break;
  1048. if (unlikely(!x || !x->nick[0])) {
  1049. if (!channel_pending(chan))
  1050. putlog(LOG_MISC, "*", "(!) killmember(%s, %s) -> nonexistent", chan->dname, nick);
  1051. return 0;
  1052. }
  1053. if (old)
  1054. old->next = x->next;
  1055. else
  1056. chan->channel.member = x->next;
  1057. free(x);
  1058. chan->channel.members--;
  1059. /* The following two errors should NEVER happen. We will try to correct
  1060. * them though, to keep the bot from crashing.
  1061. */
  1062. if (unlikely(chan->channel.members < 0)) {
  1063. chan->channel.members = 0;
  1064. chan->channel.splitmembers = 0;
  1065. for (x = chan->channel.member; x && x->nick[0]; x = x->next) {
  1066. chan->channel.members++;
  1067. if (x->split)
  1068. ++(chan->channel.splitmembers);
  1069. }
  1070. putlog(LOG_MISC, "*", "(!) actually I know of %d members.", chan->channel.members);
  1071. }
  1072. if (unlikely(!chan->channel.member)) {
  1073. chan->channel.member = (memberlist *) my_calloc(1, sizeof(memberlist));
  1074. chan->channel.member->nick[0] = 0;
  1075. chan->channel.member->next = NULL;
  1076. }
  1077. return 1;
  1078. }
  1079. /* Check whether I'm voice. Returns boolean 1 or 0.
  1080. */
  1081. bool
  1082. me_voice(const struct chanset_t *chan)
  1083. {
  1084. memberlist *mx = ismember(chan, botname);
  1085. if (!mx)
  1086. return 0;
  1087. if (chan_hasvoice(mx))
  1088. return 1;
  1089. else
  1090. return 0;
  1091. }
  1092. /* Check if there are any ops on the channel. Returns boolean 1 or 0.
  1093. */
  1094. static bool
  1095. any_ops(struct chanset_t *chan)
  1096. {
  1097. memberlist *x = NULL;
  1098. for (x = chan->channel.member; x && x->nick[0]; x = x->next)
  1099. if (chan_hasop(x))
  1100. break;
  1101. if (!x || !x->nick[0])
  1102. return 0;
  1103. return 1;
  1104. }
  1105. static void get_channel_masks(struct chanset_t* chan) {
  1106. bd::String tocheck(size_t(4));
  1107. if (!(chan->ircnet_status & CHAN_ASKEDBANS)) {
  1108. chan->ircnet_status |= CHAN_ASKEDBANS;
  1109. tocheck += 'b';
  1110. }
  1111. if (do_eI) {
  1112. chan->channel.last_eI = now;
  1113. if (!(chan->ircnet_status & CHAN_ASKED_EXEMPTS) && use_exempts == 1) {
  1114. chan->ircnet_status |= CHAN_ASKED_EXEMPTS;
  1115. tocheck += 'e';
  1116. }
  1117. if (!(chan->ircnet_status & CHAN_ASKED_INVITES) && use_invites == 1) {
  1118. chan->ircnet_status |= CHAN_ASKED_INVITES;
  1119. tocheck += 'I';
  1120. }
  1121. }
  1122. if (tocheck.length())
  1123. dprintf(DP_MODE, "MODE %s %s\n", chan->name, tocheck.c_str());
  1124. }
  1125. /* Reset the channel information.
  1126. */
  1127. void
  1128. reset_chan_info(struct chanset_t *chan)
  1129. {
  1130. if (!chan) return;
  1131. if (!chan->name[0])
  1132. strlcpy(chan->name, chan->dname, sizeof(chan->name));
  1133. /* Don't reset the channel if we're already resetting it */
  1134. if (!shouldjoin(chan)) {
  1135. sdprintf("Resetting %s but I shouldn't be there, parting...", chan->dname);
  1136. dprintf(DP_MODE, "PART %s\n", chan->name);
  1137. return;
  1138. }
  1139. if (!channel_pending(chan)) {
  1140. bool opped = me_op(chan) ? 1 : 0;
  1141. my_setkey(chan, NULL);
  1142. clear_channel(chan, 1);
  1143. chan->ircnet_status |= CHAN_PEND;
  1144. chan->ircnet_status &= ~(CHAN_ACTIVE | CHAN_ASKEDMODES | CHAN_JOINING);
  1145. /* don't bother checking bans if it's +take */
  1146. if (!channel_take(chan)) {
  1147. if (opped) {
  1148. get_channel_masks(chan);
  1149. }
  1150. }
  1151. /* These 2 need to get out asap, so into the mode queue */
  1152. dprintf(DP_MODE, "MODE %s\n", chan->name);
  1153. send_chan_who(DP_MODE, chan, 1);
  1154. /* clear_channel nuked the data...so */
  1155. dprintf(DP_HELP, "TOPIC %s\n", chan->name);//Topic is very low priority
  1156. }
  1157. }
  1158. static void send_chan_who(int queue, struct chanset_t *chan, bool chain) {
  1159. if (chain) {
  1160. if (!chained_who.contains(chan->name))
  1161. chained_who.enqueue(chan->name);
  1162. chained_who_idx = queue;
  1163. if (chained_who.size() > 1)
  1164. return;
  1165. }
  1166. if (use_354) /* Added benefit of getting numeric IP! :) */
  1167. dprintf(queue, "WHO %s %%c%%h%%n%%u%%f%%r%%d%%i\n", chan->name);
  1168. else
  1169. dprintf(queue, "WHO %s\n", chan->name);
  1170. }
  1171. void force_join_chan(struct chanset_t* chan, int idx) {
  1172. chan->ircnet_status = 0;
  1173. join_chan(chan, idx);
  1174. }
  1175. void join_chan(struct chanset_t* chan, int idx) {
  1176. if (shouldjoin(chan) && !(chan->ircnet_status & (CHAN_ACTIVE | CHAN_PEND | CHAN_JOINING))) {
  1177. dprintf(idx, "JOIN %s %s\n",
  1178. (chan->name[0]) ? chan->name : chan->dname,
  1179. chan->channel.key[0] ? chan->channel.key : chan->key_prot);
  1180. clear_channel(chan, 1);
  1181. chan->ircnet_status |= CHAN_JOINING;
  1182. }
  1183. }
  1184. /* If i'm the only person on the channel, and i'm not op'd,
  1185. * might as well leave and rejoin. If i'm NOT the only person
  1186. * on the channel, but i'm still not op'd, demand ops.
  1187. */
  1188. static void
  1189. check_lonely_channel(struct chanset_t *chan)
  1190. {
  1191. if (channel_pending(chan) || !channel_active(chan) || me_op(chan) ||
  1192. !shouldjoin(chan) || (chan->channel.mode & CHANANON))
  1193. return;
  1194. memberlist *m = NULL;
  1195. char s[UHOSTLEN] = "";
  1196. static int whined = 0;
  1197. if ((chan->channel.members - chan->channel.splitmembers) == 1 && channel_cycle(chan) && !channel_stop_cycle(chan)) {
  1198. if (chan->name[0] != '+') { /* Its pointless to cycle + chans for ops */
  1199. putlog(LOG_MISC, "*", "Trying to cycle %s to regain ops.", chan->dname);
  1200. dprintf(DP_MODE, "PART %s\n", chan->name);
  1201. // Will auto rejoin once the bot PARTs
  1202. whined = 0;
  1203. }
  1204. } else if (any_ops(chan)) {
  1205. whined = 0;
  1206. request_op(chan);
  1207. /* need: op */
  1208. } else {
  1209. /* Other people here, but none are ops. If there are other bots make
  1210. * them LEAVE!
  1211. */
  1212. bool ok = 1;
  1213. struct userrec *u = NULL;
  1214. if (!whined) {
  1215. /* + is opless. Complaining about no ops when without special
  1216. * help(services), we cant get them - Raist
  1217. */
  1218. if (chan->name[0] != '+')
  1219. putlog(LOG_MISC, "*", "%s is active but has no ops :(", chan->dname);
  1220. whined = 1;
  1221. }
  1222. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1223. simple_snprintf(s, sizeof(s), "%s!%s", m->nick, m->userhost);
  1224. u = get_user_by_host(s);
  1225. if (!m->is_me && (!u || !u->bot)) {
  1226. ok = 0;
  1227. break;
  1228. }
  1229. }
  1230. if (ok && channel_cycle(chan)) {
  1231. /* ALL bots! make them LEAVE!!! */
  1232. /*
  1233. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  1234. if (!m->is_me)
  1235. dprintf(DP_SERVER, "PRIVMSG %s :go %s\n", m->nick, chan->dname);
  1236. */
  1237. }
  1238. }
  1239. }
  1240. static void
  1241. warn_pls_take(struct chanset_t *chan)
  1242. {
  1243. if (channel_take(chan) && me_op(chan))
  1244. putlog(LOG_WARN, "*", "%s is set +take, and I'm already opped! +take is insecure, try +bitch instead",
  1245. chan->dname);
  1246. }
  1247. /* FIXME: max sendq will occur. */
  1248. static void
  1249. check_servers(struct chanset_t *chan)
  1250. {
  1251. for (memberlist *m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1252. if (!m->is_me && chan_hasop(m) && (m->hops == -1)) {
  1253. putlog(LOG_DEBUG, "*", "Updating WHO for '%s' because '%s' is missing data.", chan->dname, m->nick);
  1254. send_chan_who(DP_HELP, chan);
  1255. break; /* lets just do one chan at a time to save from flooding */
  1256. }
  1257. }
  1258. }
  1259. static void do_protect(struct chanset_t* chan, const char* reason) {
  1260. // Don't bother with these if already botbitch, already processed it, or it's a hacked bot and +botbitch won't help.
  1261. if (!channel_botbitch(chan)) {
  1262. if (chan->protect_backup) {
  1263. putlog(LOG_MISC, "*", "%s detected in %s: Setting +botbitch/+backup to protect the channel.", reason, chan->dname);
  1264. do_chanset(NULL, chan, "+botbitch +bitch +backup", DO_LOCAL | DO_NET);
  1265. } else {
  1266. putlog(LOG_MISC, "*", "%s detected in %s: Setting +botbitch to protect the channel.", reason, chan->dname);
  1267. do_chanset(NULL, chan, "+botbitch +bitch", DO_LOCAL | DO_NET);
  1268. }
  1269. // enforce_closed(chan);
  1270. // dprintf(DP_MODE, "TOPIC %s :Auto-closed - channel fight\n", chan->name);
  1271. // enforce_bitch(chan);
  1272. reversing = 1; // Reverse any modes which triggered this.
  1273. }
  1274. }
  1275. static void
  1276. check_netfight(struct chanset_t *chan)
  1277. {
  1278. if (channel_protect(chan) && fight_threshold) {
  1279. if ((chan->channel.fighting) && (chan->channel.fighting > fight_threshold) && !chan_bitch(chan)) {
  1280. do_protect(chan, "Channel fight");
  1281. }
  1282. }
  1283. chan->channel.fighting = 0; /* we put this here because we need to clear it once per min */
  1284. }
  1285. void
  1286. raise_limit(struct chanset_t *chan)
  1287. {
  1288. if (!chan || !me_op(chan))
  1289. return;
  1290. /* Don't bother setting limit if the user has set a protect -l */
  1291. if (chan->mode_mns_prot & CHANLIMIT)
  1292. return;
  1293. int nl = (chan->channel.members - chan->channel.splitmembers) + chan->limitraise; /* new limit */
  1294. int i = chan->limitraise >> 2; /* DIV 4 */
  1295. /* if the newlimit will be in the range made by these vars, dont change. */
  1296. int ul = nl + i; /* upper limit */
  1297. int ll = nl - i; /* lower limit */
  1298. if ((chan->channel.maxmembers > ll) && (chan->channel.maxmembers < ul))
  1299. return; /* the current limit is in the range, so leave it. */
  1300. if (nl != chan->channel.maxmembers) {
  1301. char s[6] = "";
  1302. simple_snprintf(s, sizeof(s), "%d", nl);
  1303. add_mode(chan, '+', 'l', s);
  1304. }
  1305. }
  1306. void check_shouldjoin(struct chanset_t* chan)
  1307. {
  1308. if ((channel_active(chan) || channel_pending(chan)) && !shouldjoin(chan)) {
  1309. sdprintf("Active/Pending in %s but I shouldn't be there, parting...", chan->dname);
  1310. dprintf(DP_SERVER, "PART %s\n", chan->name[0] ? chan->name : chan->dname);
  1311. } else if (shouldjoin(chan)) {
  1312. join_chan(chan);
  1313. }
  1314. }
  1315. static void
  1316. check_expired_chanstuff(struct chanset_t *chan)
  1317. {
  1318. check_shouldjoin(chan);
  1319. if (channel_active(chan) && shouldjoin(chan)) {
  1320. masklist *b = NULL, *e = NULL;
  1321. memberlist *m = NULL, *n = NULL;
  1322. char s[UHOSTLEN] = "";
  1323. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  1324. if (me_op(chan)) {
  1325. if (channel_dynamicbans(chan) && chan->ban_time)
  1326. for (b = chan->channel.ban; b->mask[0]; b = b->next)
  1327. if (now - b->timer > 60 * chan->ban_time &&
  1328. !u_sticky_mask(chan->bans, b->mask) &&
  1329. !u_sticky_mask(global_bans, b->mask) && expired_mask(chan, b->who)) {
  1330. putlog(LOG_MODES, chan->dname, "(%s) Channel ban on %s expired.", chan->dname, b->mask);
  1331. add_mode(chan, '-', 'b', b->mask);
  1332. b->timer = now;
  1333. }
  1334. if (use_exempts && channel_dynamicexempts(chan) && chan->exempt_time)
  1335. for (e = chan->channel.exempt; e->mask[0]; e = e->next)
  1336. if (now - e->timer > 60 * chan->exempt_time &&
  1337. !u_sticky_mask(chan->exempts, e->mask) &&
  1338. !u_sticky_mask(global_exempts, e->mask) && expired_mask(chan, e->who)) {
  1339. /* Check to see if it matches a ban */
  1340. int match = 0;
  1341. for (b = chan->channel.ban; b->mask[0]; b = b->next)
  1342. if (wild_match(b->mask, e->mask) || wild_match(e->mask, b->mask)) {
  1343. match = 1;
  1344. break;
  1345. }
  1346. /* Leave this extra logging in for now. Can be removed later
  1347. * Jason
  1348. */
  1349. if (match) {
  1350. putlog(LOG_MODES, chan->dname,
  1351. "(%s) Channel exemption %s NOT expired. Exempt still set!", chan->dname, e->mask);
  1352. } else {
  1353. putlog(LOG_MODES, chan->dname, "(%s) Channel exemption on %s expired.", chan->dname, e->mask);
  1354. add_mode(chan, '-', 'e', e->mask);
  1355. }
  1356. e->timer = now;
  1357. }
  1358. if (use_invites && channel_dynamicinvites(chan) && chan->invite_time && !(chan->channel.mode & CHANINV)) {
  1359. for (b = chan->channel.invite; b->mask[0]; b = b->next) {
  1360. if (now - b->timer > 60 * chan->invite_time &&
  1361. !u_sticky_mask(chan->invites, b->mask) &&
  1362. !u_sticky_mask(global_invites, b->mask) && expired_mask(chan, b->who)) {
  1363. putlog(LOG_MODES, chan->dname, "(%s) Channel invitation on %s expired.", chan->dname, b->mask);
  1364. add_mode(chan, '-', 'I', b->mask);
  1365. b->timer = now;
  1366. }
  1367. }
  1368. }
  1369. } /* me_op */
  1370. // for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1371. int splitmembers = 0;
  1372. for (m = chan->channel.member; m && m->nick[0]; m = n) {
  1373. n = m->next;
  1374. if (m->split) {
  1375. ++splitmembers;
  1376. if (now - m->split > wait_split) {
  1377. simple_snprintf(s, sizeof(s), "%s!%s", m->nick, m->userhost);
  1378. putlog(LOG_JOIN, chan->dname, "%s (%s) got lost in the net-split.", m->nick, m->userhost);
  1379. --(chan->channel.splitmembers);
  1380. killmember(chan, m->nick);
  1381. continue;
  1382. }
  1383. }
  1384. //This bot is set +r, so resolve.
  1385. if (!m->userip[0] && doresolv(chan)) {
  1386. char host[UHOSTLEN] = "", *p = NULL;
  1387. p = strchr(m->userhost, '@');
  1388. if (p) {
  1389. ++p;
  1390. strlcpy(host, p, strlen(m->userhost) - (p - host));
  1391. resolve_to_member(chan, m->nick, host);
  1392. }
  1393. }
  1394. if (me_op(chan)) {
  1395. if (dovoice(chan) && !loading) { /* autovoice of +v users if bot is +y */
  1396. if (!chan_hasop(m) && !chan_hasvoice(m) && !chan_sentvoice(m)) {
  1397. member_getuser(m, 1);
  1398. if (m->user) {
  1399. get_user_flagrec(m->user, &fr, chan->dname, chan);
  1400. if (!glob_bot(fr)) {
  1401. if (!(m->flags & EVOICE) &&
  1402. (
  1403. /* +voice: Voice all clients who are not flag:+q. If the chan is +voicebitch, only op flag:+v clients */
  1404. (channel_voice(chan) && !chk_devoice(fr) && (!channel_voicebitch(chan) || (channel_voicebitch(chan) && chk_voice(fr, chan)))) ||
  1405. /* Or, if the channel is -voice but they still qualify to be voiced */
  1406. (!channel_voice(chan) && !privchan(fr, chan, PRIV_VOICE) && chk_voice(fr, chan))
  1407. )
  1408. ) {
  1409. add_mode(chan, '+', 'v', m->nick);
  1410. }
  1411. }
  1412. } else if (!m->user && channel_voice(chan) && !channel_voicebitch(chan) && voice_ok(m, chan)) {
  1413. add_mode(chan, '+', 'v', m->nick);
  1414. }
  1415. }
  1416. }
  1417. }
  1418. m = n;
  1419. }
  1420. // Update minutely
  1421. chan->channel.splitmembers = splitmembers;
  1422. check_lonely_channel(chan);
  1423. }
  1424. }
  1425. void
  1426. irc_minutely()
  1427. {
  1428. for (register struct chanset_t *chan = chanset; chan; chan = chan->next) {
  1429. warn_pls_take(chan);
  1430. if (server_online) {
  1431. if (!channel_pending(chan)) {
  1432. check_netfight(chan);
  1433. check_servers(chan);
  1434. }
  1435. check_expired_chanstuff(chan);
  1436. }
  1437. }
  1438. }
  1439. int check_bind_authc(char *cmd, Auth *a, char *chname, char *par)
  1440. {
  1441. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  1442. int x = 0;
  1443. get_user_flagrec(a->user, &fr, chname);
  1444. if (a->GetIdx(chname)) {
  1445. x = check_auth_dcc(a, cmd, par);
  1446. }
  1447. LOGC(cmd);
  1448. if (x & BIND_RET_BREAK)
  1449. return (1);
  1450. return (0);
  1451. }
  1452. /* Flush the modes for EVERY channel.
  1453. */
  1454. void
  1455. flush_modes()
  1456. {
  1457. if (!modesperline) /* Haven't received 005 yet :) */
  1458. return;
  1459. memberlist *m = NULL;
  1460. Context;
  1461. for (register struct chanset_t *chan = chanset; chan; chan = chan->next) {
  1462. if (!me_op(chan)) continue;
  1463. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1464. if (m->delay && m->delay <= now) {
  1465. m->delay = 0L;
  1466. m->flags &= ~FULL_DELAY;
  1467. if (chan_sentop(m)) {
  1468. m->flags &= ~SENTOP;
  1469. do_op(m->nick, chan, 0, 0);
  1470. }
  1471. if (chan_sentvoice(m)) {
  1472. m->flags &= ~SENTVOICE;
  1473. add_mode(chan, '+', 'v', m->nick);
  1474. }
  1475. }
  1476. }
  1477. flush_mode(chan, NORMAL);
  1478. }
  1479. }
  1480. void
  1481. irc_report(int idx, int details)
  1482. {
  1483. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  1484. char ch[1024] = "", q[160] = "";
  1485. const char *p = NULL;
  1486. int k = 10;
  1487. size_t len;
  1488. strlcpy(q, "Channels: ", sizeof(q));
  1489. for (struct chanset_t *chan = chanset; chan; chan = chan->next) {
  1490. if (idx != DP_STDOUT)
  1491. get_user_flagrec(dcc[idx].user, &fr, chan->dname, chan);
  1492. if (!privchan(fr, chan, PRIV_OP) && (idx == DP_STDOUT || glob_master(fr) || chan_master(fr))) {
  1493. p = NULL;
  1494. if (shouldjoin(chan)) {
  1495. if (chan->ircnet_status & CHAN_JUPED)
  1496. p = "juped";
  1497. else if (channel_joining(chan))
  1498. p = "joining";
  1499. else if (!(chan->ircnet_status & CHAN_ACTIVE))
  1500. p = "trying";
  1501. else if (chan->ircnet_status & CHAN_PEND)
  1502. p = "pending";
  1503. else if ((chan->dname[0] != '+') && !me_op(chan))
  1504. p = "want ops!";
  1505. }
  1506. len = simple_snprintf(ch, sizeof(ch), "%s%s%s%s, ", chan->dname, p ? "(" : "", p ? p : "", p ? ")" : "");
  1507. if ((k + len) > 70) {
  1508. dprintf(idx, " %s\n", q);
  1509. strlcpy(q, " ", sizeof(q));
  1510. k = 10;
  1511. }
  1512. k += my_strcpy(q + k, ch);
  1513. }
  1514. }
  1515. if (k > 10) {
  1516. q[k - 2] = 0;
  1517. dprintf(idx, " %s\n", q);
  1518. }
  1519. }
  1520. static void bot_release_nick (char *botnick, char *code, char *par) {
  1521. release_nick(par);
  1522. }
  1523. static cmd_t irc_bot[] = {
  1524. {"gi", "", (Function) getin_request, NULL, LEAF},
  1525. {"mr", "", (Function) mass_request, NULL, LEAF},
  1526. {"rn", "", (Function) bot_release_nick, NULL, LEAF},
  1527. {NULL, NULL, NULL, NULL, 0}
  1528. };
  1529. void
  1530. irc_init()
  1531. {
  1532. timer_create_secs(60, "irc_minutely", (Function) irc_minutely);
  1533. /* Add our commands to the imported tables. */
  1534. add_builtins("dcc", irc_dcc);
  1535. add_builtins("bot", irc_bot);
  1536. add_builtins("raw", irc_raw);
  1537. add_builtins("msg", C_msg);
  1538. }