irc.cc 54 KB

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