irc.c 47 KB

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