irc.c 53 KB

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