irc.cc 54 KB

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