irc.c 50 KB

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