irc.c 51 KB

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