irc.c 52 KB

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