irc.c 50 KB

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