irc.cc 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780
  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. 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 std::deque<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);
  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 *) 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 *) 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(mem, 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 *) 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) {
  775. return;
  776. }
  777. if (channel_pending(chan) || !shouldjoin(chan) || !channel_active(chan) || me_op(chan)) {
  778. chan->channel.do_opreq = 0;
  779. return;
  780. }
  781. if (chan->channel.no_op) {
  782. if (chan->channel.no_op > now) /* dont op until this time has passed */
  783. return;
  784. else
  785. chan->channel.no_op = 0;
  786. }
  787. /* check server lag */
  788. if (server_lag > lag_threshold) {
  789. putlog(LOG_GETIN, "*", "Not asking for ops on %s - I'm too lagged", chan->dname);
  790. chan->channel.no_op = now + 10;
  791. return;
  792. }
  793. /* Check if my hostmask is recognized (every 10th time) */
  794. static int check_hostmask_cnt = 10;
  795. if (check_hostmask_cnt++ == 10) {
  796. check_hostmask();
  797. check_hostmask_cnt = 0;
  798. }
  799. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_BOT, 0, 0, 0 };
  800. get_user_flagrec(conf.bot->u, &fr, chan->dname, chan);
  801. if (!chk_op(fr, chan) || glob_kick(fr) || chan_kick(fr)) {
  802. putlog(LOG_GETIN, "*", "Not requesting op for %s - I do not have access to that channel.", chan->dname);
  803. chan->channel.no_op = now + 10;
  804. return;
  805. }
  806. int i = 0, my_exp = 0, first = 100;
  807. time_t n = now;
  808. /* max OPREQ_COUNT requests per OPREQ_SECONDS sec */
  809. while (i < 5) {
  810. if (n - chan->opreqtime[i] > op_requests.time) {
  811. if (first > i)
  812. first = i;
  813. my_exp++;
  814. chan->opreqtime[i] = 0;
  815. }
  816. i++;
  817. }
  818. if ((5 - my_exp) >= op_requests.count) {
  819. putlog(LOG_GETIN, "*", "Delaying opreq for %s - Maximum of %d:%d reached", chan->dname, op_requests.count,
  820. op_requests.time);
  821. chan->channel.no_op = now + op_requests.time + 1;
  822. return;
  823. }
  824. int cnt = op_bots, i2 = 0;
  825. memberlist *ml = NULL, *botops[MAX_BOTS];
  826. char s[UHOSTLEN] = "";
  827. for (i = 0, ml = chan->channel.member; (i < MAX_BOTS) && ml && ml->nick[0]; ml = ml->next) {
  828. if (!chan_hasop(ml) || chan_issplit(ml))
  829. continue;
  830. member_getuser(ml, 1);
  831. if (!ml->user || !ml->user->bot)
  832. continue;
  833. /* Is the bot linked? */
  834. if (findbot(ml->user->handle))
  835. botops[i++] = ml;
  836. }
  837. if (!i) {
  838. if (channel_active(chan) && !channel_pending(chan)) {
  839. chan->channel.no_op = now + op_requests.time;
  840. putlog(LOG_GETIN, "*", "No one to ask for ops on %s - Delaying requests for %d seconds.", chan->dname, op_requests.time);
  841. }
  842. return;
  843. }
  844. char l[1024] = "";
  845. size_t len = 0;
  846. // Pick a random member to use as verification
  847. memberlist* shared_member = NULL;
  848. for (int z = 0; z < 5; ++z) {
  849. int shared_member_cnt = z < 4 ? randint(chan->channel.members) : 0;
  850. int shared_idx = 0;
  851. for (shared_member = chan->channel.member; shared_member && shared_member->nick[0]; shared_member = shared_member->next) {
  852. if (shared_member->split) continue;
  853. if (shared_idx >= shared_member_cnt)
  854. break;
  855. ++shared_idx;
  856. }
  857. if (shared_member) break;
  858. }
  859. if (!shared_member) {
  860. chan->channel.no_op = now + op_requests.time;
  861. putlog(LOG_GETIN, "*", "Too many split clients on %s - Delaying requests for %d seconds.", chan->dname, op_requests.time);
  862. return;
  863. }
  864. /* first scan for bots on my server, ask first found for ops */
  865. simple_snprintf(s, sizeof(s), "gi o %s %s %s %s %s", chan->dname, botname, curnetwork, shared_member->nick, shared_member->userhost);
  866. /* look for bots 0-1 hops away */
  867. for (i2 = 0; i2 < i; i2++) {
  868. if (botops[i2]->hops < 2) {
  869. putbot(botops[i2]->user->handle, s);
  870. chan->opreqtime[first] = n;
  871. if (l[0])
  872. len += strlcpy(l + len, ", ", sizeof(l) - len);
  873. len += strlcpy(l + len, botops[i2]->user->handle, sizeof(l) - len);
  874. len += strlcpy(l + len, "/", sizeof(l) - len);
  875. len += strlcpy(l + len, botops[i2]->nick, sizeof(l) - len);
  876. botops[i2] = NULL;
  877. --cnt;
  878. break;
  879. }
  880. //FIXME: Also prefer our localhub or child bots
  881. }
  882. /* Pick random op and ask for ops */
  883. while (cnt) {
  884. i2 = randint(i);
  885. if (botops[i2]) {
  886. putbot(botops[i2]->user->handle, s);
  887. chan->opreqtime[first] = n;
  888. if (l[0])
  889. len += strlcpy(l + len, ", ", sizeof(l) - len);
  890. len += strlcpy(l + len, botops[i2]->user->handle, sizeof(l) - len);
  891. len += strlcpy(l + len, "/", sizeof(l) - len);
  892. len += strlcpy(l + len, botops[i2]->nick, sizeof(l) - len);
  893. --cnt;
  894. botops[i2] = NULL;
  895. } else {
  896. if (i < op_bots)
  897. cnt--;
  898. }
  899. }
  900. l[len] = 0;
  901. putlog(LOG_GETIN, "*", "Requested ops on %s from %s", chan->dname, l);
  902. }
  903. static void
  904. request_in(struct chanset_t *chan)
  905. {
  906. /* Lag situation */
  907. if (!shouldjoin(chan))
  908. return;
  909. /* Check if my hostmask is recognized (every 10th time) */
  910. static int check_hostmask_cnt = 10;
  911. if (check_hostmask_cnt++ == 10) {
  912. check_hostmask();
  913. check_hostmask_cnt = 0;
  914. }
  915. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_BOT, 0, 0, 0 };
  916. get_user_flagrec(conf.bot->u, &fr, chan->dname, chan);
  917. if (!chk_op(fr, chan) || glob_kick(fr) || chan_kick(fr)) {
  918. putlog(LOG_GETIN, "*", "Not requesting help to join %s - I do not have access to that channel.", chan->dname);
  919. return;
  920. }
  921. int foundBots = 0;
  922. // char* botops[MAX_BOTS];
  923. for (tand_t* bot = tandbot; bot && (foundBots < MAX_BOTS); bot = bot->next) {
  924. if (bot->hub || !bot->u)
  925. continue;
  926. get_user_flagrec(bot->u, &fr, chan->dname, chan);
  927. if (bot_shouldjoin(bot->u, &fr, chan) && chk_op(fr, chan)) {
  928. ++foundBots;
  929. // botops[foundBots++] = bot->bot;
  930. }
  931. }
  932. if (!foundBots) {
  933. putlog(LOG_GETIN, "*", "No bots available, can't request help to join %s", chan->dname);
  934. return;
  935. }
  936. bd::String request(bd::String::printf("gi i %s %s %s!%s %s", chan->dname, botname, botname, botuserhost, botuserip));
  937. putallbots(request.c_str());
  938. putlog(LOG_GETIN, "*", "Requested help to join %s", chan->dname);
  939. }
  940. /* Set the key.
  941. */
  942. void
  943. my_setkey(struct chanset_t *chan, char *k)
  944. {
  945. free(chan->channel.key);
  946. chan->channel.key = k ? strdup(k) : (char *) calloc(1, 1);
  947. }
  948. /* Adds a ban, exempt or invite mask to the list
  949. * m should be chan->channel.(exempt|invite|ban)
  950. */
  951. static bool
  952. new_mask(masklist *m, char *s, char *who)
  953. {
  954. for (; m && m->mask[0] && rfc_casecmp(m->mask, s); m = m->next) ;
  955. if (m->mask[0])
  956. return 1; /* Already existent mask */
  957. m->next = (masklist *) calloc(1, sizeof(masklist));
  958. m->next->next = NULL;
  959. m->next->mask = (char *) calloc(1, 1);
  960. free(m->mask);
  961. m->mask = strdup(s);
  962. m->who = strdup(who);
  963. m->timer = now;
  964. return 0;
  965. }
  966. /* Removes a nick from the channel member list (returns 1 if successful)
  967. */
  968. static bool
  969. killmember(struct chanset_t *chan, char *nick, bool cacheMember)
  970. {
  971. memberlist *x = NULL, *old = NULL;
  972. for (x = chan->channel.member; x && x->nick[0]; old = x, x = x->next)
  973. if (!rfc_casecmp(x->nick, nick))
  974. break;
  975. if (unlikely(!x || !x->nick[0])) {
  976. if (!channel_pending(chan))
  977. putlog(LOG_MISC, "*", "(!) killmember(%s, %s) -> nonexistent", chan->dname, nick);
  978. return 0;
  979. }
  980. if (old)
  981. old->next = x->next;
  982. else
  983. chan->channel.member = x->next;
  984. if (cacheMember) {
  985. x->last = now;
  986. x->user = NULL;
  987. x->next = NULL;
  988. x->tried_getuser = 0;
  989. // Don't delete here, will delete when it expires from the cache.
  990. (*chan->channel.cached_members)[x->userhost] = x;
  991. } else {
  992. chan->channel.cached_members->remove(x->userhost);
  993. delete_member(x);
  994. }
  995. --chan->channel.members;
  996. /* The following two errors should NEVER happen. We will try to correct
  997. * them though, to keep the bot from crashing.
  998. */
  999. if (unlikely(chan->channel.members < 0)) {
  1000. chan->channel.members = 0;
  1001. chan->channel.splitmembers = 0;
  1002. for (x = chan->channel.member; x && x->nick[0]; x = x->next) {
  1003. chan->channel.members++;
  1004. if (x->split)
  1005. ++(chan->channel.splitmembers);
  1006. }
  1007. putlog(LOG_MISC, "*", "(!) actually I know of %d members.", chan->channel.members);
  1008. }
  1009. if (unlikely(!chan->channel.member)) {
  1010. chan->channel.member = (memberlist *) calloc(1, sizeof(memberlist));
  1011. chan->channel.member->nick[0] = 0;
  1012. chan->channel.member->next = NULL;
  1013. }
  1014. return 1;
  1015. }
  1016. /**
  1017. * Update the member with cached information from a parted/quitted member
  1018. */
  1019. static void member_update_from_cache(struct chanset_t* chan, memberlist *m) {
  1020. // Are they in the cache?
  1021. const bd::String userhost(m->userhost);
  1022. if (chan->channel.cached_members->contains(userhost)) {
  1023. memberlist *cached_member = (*chan->channel.cached_members)[userhost];
  1024. // Update important flood tracking information
  1025. swap(m->floodtime, cached_member->floodtime);
  1026. swap(m->floodnum, cached_member->floodnum);
  1027. // Update EVOICE flag
  1028. if (cached_member->flags & EVOICE) {
  1029. m->flags |= EVOICE;
  1030. }
  1031. // No longer need the cached member
  1032. delete_member(cached_member);
  1033. chan->channel.cached_members->remove(userhost);
  1034. }
  1035. }
  1036. /* Check whether I'm voice. Returns boolean 1 or 0.
  1037. */
  1038. bool
  1039. me_voice(const struct chanset_t *chan)
  1040. {
  1041. memberlist *mx = ismember(chan, botname);
  1042. if (!mx)
  1043. return 0;
  1044. if (chan_hasvoice(mx))
  1045. return 1;
  1046. else
  1047. return 0;
  1048. }
  1049. /* Check if there are any ops on the channel. Returns boolean 1 or 0.
  1050. */
  1051. static bool
  1052. any_ops(struct chanset_t *chan)
  1053. {
  1054. memberlist *x = NULL;
  1055. for (x = chan->channel.member; x && x->nick[0]; x = x->next)
  1056. if (chan_hasop(x))
  1057. break;
  1058. if (!x || !x->nick[0])
  1059. return 0;
  1060. return 1;
  1061. }
  1062. static void get_channel_masks(struct chanset_t* chan) {
  1063. bd::String tocheck(size_t(4));
  1064. if (!(chan->ircnet_status & CHAN_ASKEDBANS)) {
  1065. chan->ircnet_status |= CHAN_ASKEDBANS;
  1066. tocheck += 'b';
  1067. }
  1068. if (do_eI) {
  1069. chan->channel.last_eI = now;
  1070. if (!(chan->ircnet_status & CHAN_ASKED_EXEMPTS) && use_exempts == 1) {
  1071. chan->ircnet_status |= CHAN_ASKED_EXEMPTS;
  1072. tocheck += 'e';
  1073. }
  1074. if (!(chan->ircnet_status & CHAN_ASKED_INVITES) && use_invites == 1) {
  1075. chan->ircnet_status |= CHAN_ASKED_INVITES;
  1076. tocheck += 'I';
  1077. }
  1078. }
  1079. if (tocheck.length())
  1080. dprintf(DP_MODE, "MODE %s %s\n", chan->name, tocheck.c_str());
  1081. }
  1082. /* Reset the channel information.
  1083. */
  1084. void
  1085. reset_chan_info(struct chanset_t *chan)
  1086. {
  1087. if (!chan) return;
  1088. if (!chan->name[0])
  1089. strlcpy(chan->name, chan->dname, sizeof(chan->name));
  1090. /* Don't reset the channel if we're already resetting it */
  1091. if (!shouldjoin(chan)) {
  1092. sdprintf("Resetting %s but I shouldn't be there, parting...", chan->dname);
  1093. dprintf(DP_MODE, "PART %s\n", chan->name);
  1094. return;
  1095. }
  1096. if (!channel_pending(chan)) {
  1097. bool opped = me_op(chan) ? 1 : 0;
  1098. my_setkey(chan, NULL);
  1099. clear_channel(chan, 1);
  1100. chan->ircnet_status |= CHAN_PEND;
  1101. chan->ircnet_status &= ~(CHAN_ACTIVE | CHAN_ASKEDMODES | CHAN_JOINING);
  1102. /* don't bother checking bans if it's +take */
  1103. if (!channel_take(chan)) {
  1104. if (opped) {
  1105. get_channel_masks(chan);
  1106. }
  1107. }
  1108. /* These 2 need to get out asap, so into the mode queue */
  1109. dprintf(DP_MODE, "MODE %s\n", chan->name);
  1110. send_chan_who(DP_MODE, chan, 1);
  1111. /* clear_channel nuked the data...so */
  1112. dprintf(DP_HELP, "TOPIC %s\n", chan->name);//Topic is very low priority
  1113. }
  1114. }
  1115. static void send_chan_who(int queue, struct chanset_t *chan, bool chain) {
  1116. if (chain) {
  1117. if (std::find(std::begin(chained_who), std::end(chained_who),
  1118. chan->name) != std::end(chained_who))
  1119. chained_who.push_back(chan->name);
  1120. chained_who_idx = queue;
  1121. if (chained_who.size() > 1)
  1122. return;
  1123. }
  1124. if (use_354) /* Added benefit of getting numeric IP! :) */
  1125. dprintf(queue, "WHO %s %%c%%h%%n%%u%%f%%r%%d%%i\n", chan->name);
  1126. else
  1127. dprintf(queue, "WHO %s\n", chan->name);
  1128. }
  1129. void force_join_chan(struct chanset_t* chan, int idx) {
  1130. chan->ircnet_status = 0;
  1131. join_chan(chan, idx);
  1132. }
  1133. void join_chan(struct chanset_t* chan, int idx) {
  1134. if (shouldjoin(chan) && !(chan->ircnet_status & (CHAN_ACTIVE | CHAN_PEND | CHAN_JOINING))) {
  1135. dprintf(idx, "JOIN %s %s\n",
  1136. (chan->name[0]) ? chan->name : chan->dname,
  1137. chan->channel.key[0] ? chan->channel.key : chan->key_prot);
  1138. clear_channel(chan, 1);
  1139. chan->ircnet_status |= CHAN_JOINING;
  1140. }
  1141. }
  1142. /* If i'm the only person on the channel, and i'm not op'd,
  1143. * might as well leave and rejoin. If i'm NOT the only person
  1144. * on the channel, but i'm still not op'd, demand ops.
  1145. */
  1146. static void
  1147. check_lonely_channel(struct chanset_t *chan)
  1148. {
  1149. if (channel_pending(chan) || !channel_active(chan) || me_op(chan) ||
  1150. !shouldjoin(chan) || (chan->channel.mode & CHANANON))
  1151. return;
  1152. static int whined = 0;
  1153. if ((chan->channel.members - chan->channel.splitmembers) == 1 && channel_cycle(chan) && !channel_stop_cycle(chan)) {
  1154. if (chan->name[0] != '+') { /* Its pointless to cycle + chans for ops */
  1155. putlog(LOG_MISC, "*", "Trying to cycle %s to regain ops.", chan->dname);
  1156. dprintf(DP_MODE, "PART %s\n", chan->name);
  1157. // Will auto rejoin once the bot PARTs
  1158. whined = 0;
  1159. }
  1160. } else if (any_ops(chan)) {
  1161. whined = 0;
  1162. } else {
  1163. /* Other people here, but none are ops. If there are other bots make
  1164. * them LEAVE!
  1165. */
  1166. if (!whined) {
  1167. /* + is opless. Complaining about no ops when without special
  1168. * help(services), we cant get them - Raist
  1169. */
  1170. if (chan->name[0] != '+')
  1171. putlog(LOG_MISC, "*", "%s is active but has no ops :(", chan->dname);
  1172. whined = 1;
  1173. }
  1174. #ifdef disabled
  1175. memberlist *m = NULL;
  1176. bool ok = 1;
  1177. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1178. member_getuser(m, 0);
  1179. if (!m->is_me && (!m->user || !m->user->bot)) {
  1180. ok = 0;
  1181. break;
  1182. }
  1183. }
  1184. if (ok && channel_cycle(chan)) {
  1185. /* ALL bots! make them LEAVE!!! */
  1186. /*
  1187. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  1188. if (!m->is_me)
  1189. dprintf(DP_SERVER, "PRIVMSG %s :go %s\n", m->nick, chan->dname);
  1190. */
  1191. }
  1192. #endif
  1193. }
  1194. }
  1195. static void
  1196. warn_pls_take(struct chanset_t *chan)
  1197. {
  1198. if (channel_take(chan) && me_op(chan))
  1199. putlog(LOG_WARN, "*", "%s is set +take, and I'm already opped! +take is insecure, try +bitch instead",
  1200. chan->dname);
  1201. }
  1202. /* FIXME: max sendq will occur. */
  1203. static void
  1204. check_servers(struct chanset_t *chan)
  1205. {
  1206. for (memberlist *m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1207. if (!m->is_me && chan_hasop(m) && (m->hops == -1)) {
  1208. putlog(LOG_DEBUG, "*", "Updating WHO for '%s' because '%s' is missing data.", chan->dname, m->nick);
  1209. send_chan_who(DP_HELP, chan);
  1210. break; /* lets just do one chan at a time to save from flooding */
  1211. }
  1212. }
  1213. }
  1214. static void do_protect(struct chanset_t* chan, const char* reason) {
  1215. // Don't bother with these if already botbitch, already processed it, or it's a hacked bot and +botbitch won't help.
  1216. if (!channel_botbitch(chan)) {
  1217. if (chan->protect_backup) {
  1218. putlog(LOG_MISC, "*", "%s detected in %s: Setting +botbitch/+backup to protect the channel.", reason, chan->dname);
  1219. do_chanset(NULL, chan, "+botbitch +bitch +backup", DO_LOCAL | DO_NET);
  1220. } else {
  1221. putlog(LOG_MISC, "*", "%s detected in %s: Setting +botbitch to protect the channel.", reason, chan->dname);
  1222. do_chanset(NULL, chan, "+botbitch +bitch", DO_LOCAL | DO_NET);
  1223. }
  1224. // enforce_closed(chan);
  1225. // dprintf(DP_MODE, "TOPIC %s :Auto-closed - channel fight\n", chan->name);
  1226. // enforce_bitch(chan);
  1227. reversing = 1; // Reverse any modes which triggered this.
  1228. }
  1229. }
  1230. static void
  1231. check_netfight(struct chanset_t *chan)
  1232. {
  1233. if (channel_protect(chan) && fight_threshold) {
  1234. if ((chan->channel.fighting) && (chan->channel.fighting > fight_threshold) && !chan_bitch(chan)) {
  1235. do_protect(chan, "Channel fight");
  1236. }
  1237. }
  1238. chan->channel.fighting = 0; /* we put this here because we need to clear it once per min */
  1239. }
  1240. bool
  1241. raise_limit(struct chanset_t *chan, int default_limitraise)
  1242. {
  1243. if (!chan || !me_op(chan))
  1244. return false;
  1245. /* Don't bother setting limit if the user has set a protect -l */
  1246. if (chan->mode_mns_prot & CHANLIMIT)
  1247. return false;
  1248. const int limitraise = (chan->limitraise ? ((chan->limitraise % 2 == 0) ? chan->limitraise : (chan->limitraise + 1)) : default_limitraise);
  1249. if (limitraise) {
  1250. const int nl = (chan->channel.members - chan->channel.splitmembers) + limitraise; /* new limit */
  1251. const int i = limitraise >> 2; /* DIV 4 */
  1252. /* if the newlimit will be in the range made by these vars, dont change. */
  1253. const int ul = nl + i; /* upper limit */
  1254. const int ll = nl - i; /* lower limit */
  1255. if ((chan->channel.maxmembers >= ll) && (chan->channel.maxmembers <= ul)) {
  1256. return false; /* the current limit is in the range, so leave it. */
  1257. }
  1258. if (nl != chan->channel.maxmembers) {
  1259. char s[6] = "";
  1260. simple_snprintf(s, sizeof(s), "%d", nl);
  1261. add_mode(chan, '+', 'l', s);
  1262. return true;
  1263. }
  1264. }
  1265. return false;
  1266. }
  1267. void check_shouldjoin(struct chanset_t* chan)
  1268. {
  1269. if ((channel_active(chan) || channel_pending(chan)) && !shouldjoin(chan)) {
  1270. sdprintf("Active/Pending in %s but I shouldn't be there, parting...", chan->dname);
  1271. dprintf(DP_SERVER, "PART %s\n", chan->name[0] ? chan->name : chan->dname);
  1272. } else if (shouldjoin(chan)) {
  1273. join_chan(chan);
  1274. }
  1275. }
  1276. static void
  1277. check_expired_chanstuff(struct chanset_t *chan)
  1278. {
  1279. memberlist *m = NULL;
  1280. check_shouldjoin(chan);
  1281. if (channel_active(chan) && shouldjoin(chan)) {
  1282. masklist *b = NULL, *e = NULL;
  1283. memberlist *n = NULL;
  1284. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  1285. if (me_op(chan)) {
  1286. if (channel_dynamicbans(chan) && chan->ban_time)
  1287. for (b = chan->channel.ban; b->mask[0]; b = b->next)
  1288. if (now - b->timer > 60 * chan->ban_time &&
  1289. !u_sticky_mask(chan->bans, b->mask) &&
  1290. !u_sticky_mask(global_bans, b->mask) && expired_mask(chan, b->who)) {
  1291. putlog(LOG_MODES, chan->dname, "(%s) Channel ban on %s expired.", chan->dname, b->mask);
  1292. add_mode(chan, '-', 'b', b->mask);
  1293. b->timer = now;
  1294. }
  1295. if (use_exempts && channel_dynamicexempts(chan) && chan->exempt_time)
  1296. for (e = chan->channel.exempt; e->mask[0]; e = e->next)
  1297. if (now - e->timer > 60 * chan->exempt_time &&
  1298. !u_sticky_mask(chan->exempts, e->mask) &&
  1299. !u_sticky_mask(global_exempts, e->mask) && expired_mask(chan, e->who)) {
  1300. /* Check to see if it matches a ban */
  1301. int match = 0;
  1302. for (b = chan->channel.ban; b->mask[0]; b = b->next)
  1303. if (wild_match(b->mask, e->mask) || wild_match(e->mask, b->mask)) {
  1304. match = 1;
  1305. break;
  1306. }
  1307. /* Leave this extra logging in for now. Can be removed later
  1308. * Jason
  1309. */
  1310. if (match) {
  1311. putlog(LOG_MODES, chan->dname,
  1312. "(%s) Channel exemption %s NOT expired. Exempt still set!", chan->dname, e->mask);
  1313. } else {
  1314. putlog(LOG_MODES, chan->dname, "(%s) Channel exemption on %s expired.", chan->dname, e->mask);
  1315. add_mode(chan, '-', 'e', e->mask);
  1316. }
  1317. e->timer = now;
  1318. }
  1319. if (use_invites && channel_dynamicinvites(chan) && chan->invite_time && !(chan->channel.mode & CHANINV)) {
  1320. for (b = chan->channel.invite; b->mask[0]; b = b->next) {
  1321. if (now - b->timer > 60 * chan->invite_time &&
  1322. !u_sticky_mask(chan->invites, b->mask) &&
  1323. !u_sticky_mask(global_invites, b->mask) && expired_mask(chan, b->who)) {
  1324. putlog(LOG_MODES, chan->dname, "(%s) Channel invitation on %s expired.", chan->dname, b->mask);
  1325. add_mode(chan, '-', 'I', b->mask);
  1326. b->timer = now;
  1327. }
  1328. }
  1329. }
  1330. } /* me_op */
  1331. size_t splitmembers = 0, bot_ops = 0;
  1332. const bool im_opped = me_op(chan);
  1333. for (m = chan->channel.member; m && m->nick[0]; m = n) {
  1334. n = m->next;
  1335. // Update split members
  1336. if (m->split) {
  1337. ++splitmembers;
  1338. if (now - m->split > wait_split) {
  1339. putlog(LOG_JOIN, chan->dname, "%s (%s) got lost in the net-split.", m->nick, m->userhost);
  1340. --(chan->channel.splitmembers);
  1341. killmember(chan, m->nick, false);
  1342. continue;
  1343. }
  1344. }
  1345. //This bot is set +r, so resolve.
  1346. if (!m->userip[0] && doresolv(chan)) {
  1347. char host[UHOSTLEN] = "", *p = NULL;
  1348. p = strchr(m->userhost, '@');
  1349. if (p) {
  1350. ++p;
  1351. strlcpy(host, p, strlen(m->userhost) - (p - host));
  1352. resolve_to_member(chan, m->nick, host);
  1353. }
  1354. }
  1355. if (im_opped) {
  1356. if (dovoice(chan) && !loading && !chan_hasop(m)) { /* autovoice of +v users if bot is +y */
  1357. get_user_flagrec(m->user, &fr, chan->dname, chan);
  1358. /* Autoop */
  1359. if (!chan_sentop(m) && chk_autoop(m, fr, chan)) {
  1360. do_op(m, chan, 0, 0);
  1361. }
  1362. /* +v or +voice */
  1363. if (!chan_hasvoice(m) && !chan_sentvoice(m)) {
  1364. member_getuser(m, 1);
  1365. if (m->user) {
  1366. if (!(m->flags & EVOICE) &&
  1367. (
  1368. /* +voice: Voice all clients who are not flag:+q. If the chan is +voicebitch, only op flag:+v clients */
  1369. (channel_voice(chan) && !chk_devoice(fr) && (!channel_voicebitch(chan) || (channel_voicebitch(chan) && chk_voice(fr, chan)))) ||
  1370. /* Or, if the channel is -voice but they still qualify to be voiced */
  1371. (!channel_voice(chan) && !privchan(fr, chan, PRIV_VOICE) && chk_voice(fr, chan))
  1372. )
  1373. ) {
  1374. add_mode(chan, '+', 'v', m);
  1375. }
  1376. } else if (!m->user && channel_voice(chan) && !channel_voicebitch(chan) && voice_ok(m, chan)) {
  1377. add_mode(chan, '+', 'v', m);
  1378. }
  1379. }
  1380. }
  1381. }
  1382. if (m->user && m->user->bot) {
  1383. ++bot_ops;
  1384. }
  1385. }
  1386. // Update minutely
  1387. chan->channel.splitmembers = splitmembers;
  1388. check_lonely_channel(chan);
  1389. if (bot_ops && !im_opped) {
  1390. request_op(chan);
  1391. }
  1392. if (role == 3) {
  1393. recheck_channel_modes(chan);
  1394. }
  1395. }
  1396. // Clear out expired cached members
  1397. if (chan->channel.cached_members && chan->channel.cached_members->size()) {
  1398. bd::Array<bd::String> member_uhosts(chan->channel.cached_members->keys());
  1399. for (size_t i = 0; i < member_uhosts.length(); ++i) {
  1400. const bd::String uhost(member_uhosts[i]);
  1401. m = (*chan->channel.cached_members)[uhost];
  1402. // Delete the expired member
  1403. if (now - m->last > wait_split) {
  1404. delete_member(m);
  1405. chan->channel.cached_members->remove(uhost);
  1406. }
  1407. }
  1408. }
  1409. }
  1410. void
  1411. irc_minutely()
  1412. {
  1413. for (struct chanset_t *chan = chanset; chan; chan = chan->next) {
  1414. warn_pls_take(chan);
  1415. if (server_online) {
  1416. if (!channel_pending(chan)) {
  1417. check_netfight(chan);
  1418. check_servers(chan);
  1419. }
  1420. check_expired_chanstuff(chan);
  1421. }
  1422. }
  1423. }
  1424. int check_bind_authc(char *cmd, Auth *a, char *chname, char *par)
  1425. {
  1426. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  1427. int x = 0;
  1428. get_user_flagrec(a->user, &fr, chname);
  1429. if (a->GetIdx(chname)) {
  1430. x = check_auth_dcc(a, cmd, par);
  1431. }
  1432. LOGC(cmd);
  1433. if (x & BIND_RET_BREAK)
  1434. return (1);
  1435. return (0);
  1436. }
  1437. /* Flush the modes for EVERY channel.
  1438. */
  1439. void
  1440. flush_modes()
  1441. {
  1442. if (!modesperline) /* Haven't received 005 yet :) */
  1443. return;
  1444. memberlist *m = NULL;
  1445. Context;
  1446. for (struct chanset_t *chan = chanset; chan; chan = chan->next) {
  1447. if (!me_op(chan)) continue;
  1448. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1449. if (m->delay && m->delay <= now) {
  1450. m->delay = 0L;
  1451. m->flags &= ~FULL_DELAY;
  1452. if (chan_sentop(m)) {
  1453. m->flags &= ~SENTOP;
  1454. do_op(m, chan, 0, 0);
  1455. }
  1456. if (chan_sentvoice(m)) {
  1457. m->flags &= ~SENTVOICE;
  1458. add_mode(chan, '+', 'v', m);
  1459. }
  1460. }
  1461. }
  1462. flush_mode(chan, NORMAL);
  1463. }
  1464. }
  1465. void
  1466. irc_report(int idx, int details)
  1467. {
  1468. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  1469. char ch[1024] = "", q[160] = "";
  1470. const char *p = NULL;
  1471. int k = 10;
  1472. size_t len;
  1473. strlcpy(q, "Channels: ", sizeof(q));
  1474. for (struct chanset_t *chan = chanset; chan; chan = chan->next) {
  1475. if (idx != DP_STDOUT)
  1476. get_user_flagrec(dcc[idx].user, &fr, chan->dname, chan);
  1477. if (!privchan(fr, chan, PRIV_OP) && (idx == DP_STDOUT || glob_master(fr) || chan_master(fr))) {
  1478. p = NULL;
  1479. if (shouldjoin(chan)) {
  1480. if (chan->ircnet_status & CHAN_JUPED)
  1481. p = "juped";
  1482. else if (channel_joining(chan))
  1483. p = "joining";
  1484. else if (!(chan->ircnet_status & CHAN_ACTIVE))
  1485. p = "trying";
  1486. else if (chan->ircnet_status & CHAN_PEND)
  1487. p = "pending";
  1488. else if ((chan->dname[0] != '+') && !me_op(chan))
  1489. p = "want ops!";
  1490. }
  1491. len = simple_snprintf(ch, sizeof(ch), "%s%s%s%s, ", chan->dname, p ? "(" : "", p ? p : "", p ? ")" : "");
  1492. if ((k + len) > 70) {
  1493. dprintf(idx, " %s\n", q);
  1494. strlcpy(q, " ", sizeof(q));
  1495. k = 10;
  1496. }
  1497. k += my_strcpy(q + k, ch);
  1498. }
  1499. }
  1500. if (k > 10) {
  1501. q[k - 2] = 0;
  1502. dprintf(idx, " %s\n", q);
  1503. }
  1504. }
  1505. static void bot_release_nick (char *botnick, char *code, char *par) {
  1506. release_nick(par);
  1507. }
  1508. static cmd_t irc_bot[] = {
  1509. {"gi", "", (Function) getin_request, NULL, LEAF},
  1510. {"mr", "", (Function) mass_request, NULL, LEAF},
  1511. {"rn", "", (Function) bot_release_nick, NULL, LEAF},
  1512. {NULL, NULL, NULL, NULL, 0}
  1513. };
  1514. void
  1515. irc_init()
  1516. {
  1517. timer_create_secs(60, "irc_minutely", (Function) irc_minutely);
  1518. /* Add our commands to the imported tables. */
  1519. add_builtins("dcc", irc_dcc);
  1520. add_builtins("bot", irc_bot);
  1521. add_builtins("raw", irc_raw);
  1522. add_builtins("msg", C_msg);
  1523. }
  1524. /* vim: set sts=2 sw=2 ts=8 et: */