irc.c 54 KB

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