irc.c 54 KB

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