1
0

irc.c 56 KB

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