irc.c 54 KB

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