irc.c 45 KB

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