irc.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  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. u = get_user_by_handle(userlist, s1);
  695. if ((mx = ismember(chan, badnick)))
  696. mx->user = u;
  697. set_user_flagrec(u, &fr, chan->dname);
  698. simple_sprintf(s, "(%s) %s (%s)", ct, reason, whobad);
  699. set_user(&USERENTRY_COMMENT, u, (void *) s);
  700. putlog(LOG_MISC, "*", "Now deopping %s (%s)", whobad, reason);
  701. }
  702. }
  703. /* Always try to deop the offender */
  704. if (!mevictim)
  705. add_mode(chan, '-', 'o', badnick);
  706. /* Ban. Should be done before kicking. */
  707. if (chan->revenge_mode > 2) {
  708. char s[UHOSTLEN] = "", s1[UHOSTLEN] = "";
  709. splitnick(&whobad);
  710. maskhost(whobad, s1);
  711. simple_sprintf(s, "(%s) %s", ct, reason);
  712. u_addmask('b', chan, s1, conf.bot->nick, s, now + (60 * chan->ban_time), 0);
  713. if (!mevictim && me_op(chan)) {
  714. add_mode(chan, '+', 'b', s1);
  715. flush_mode(chan, QUICK);
  716. }
  717. }
  718. /* Kick the offender */
  719. if ((chan->revenge_mode > 1) &&
  720. !chan_sentkick(m) &&
  721. /* ... and can I actually do anything about it? */
  722. me_op(chan) && !mevictim) {
  723. dprintf(DP_MODE, "KICK %s %s :%s%s\n", chan->name, m->nick, bankickprefix, kickreason(KICK_MEAN));
  724. m->flags |= SENTKICK;
  725. }
  726. }
  727. /* Punishes bad guys under certain circumstances using methods as defined
  728. * by the revenge_mode flag.
  729. */
  730. static void maybe_revenge(struct chanset_t *chan, char *whobad,
  731. char *whovictim, int type)
  732. {
  733. char *badnick = NULL, *victimstr = NULL;
  734. int mevictim;
  735. struct userrec *u = NULL, *u2 = NULL;
  736. if (!chan || (type < 0))
  737. return;
  738. /* Get info about offender */
  739. u = get_user_by_host(whobad);
  740. badnick = splitnick(&whobad);
  741. /* Get info about victim */
  742. u2 = get_user_by_host(whovictim);
  743. victimstr = splitnick(&whovictim);
  744. mevictim = match_my_nick(victimstr);
  745. /* Do we want to revenge? */
  746. if (!want_to_revenge(chan, u, u2, badnick, victimstr, mevictim))
  747. return; /* No, leave them alone ... */
  748. /* Haha! Do the vengeful thing ... */
  749. punish_badguy(chan, whobad, u, badnick, victimstr, mevictim, type);
  750. }
  751. /* Set the key.
  752. */
  753. static void my_setkey(struct chanset_t *chan, char *k)
  754. {
  755. free(chan->channel.key);
  756. if (k == NULL) {
  757. chan->channel.key = (char *) calloc(1, 1);
  758. return;
  759. }
  760. chan->channel.key = (char *) calloc(1, strlen(k) + 1);
  761. strcpy(chan->channel.key, k);
  762. }
  763. /* Adds a ban, exempt or invite mask to the list
  764. * m should be chan->channel.(exempt|invite|ban)
  765. */
  766. static void newmask(masklist *m, char *s, char *who)
  767. {
  768. for (; m && m->mask[0] && rfc_casecmp(m->mask, s); m = m->next);
  769. if (m->mask[0])
  770. return; /* Already existent mask */
  771. m->next = (masklist *) calloc(1, sizeof(masklist));
  772. m->next->next = NULL;
  773. m->next->mask = (char *) calloc(1, 1);
  774. free(m->mask);
  775. m->mask = (char *) calloc(1, strlen(s) + 1);
  776. strcpy(m->mask, s);
  777. m->who = (char *) calloc(1, strlen(who) + 1);
  778. strcpy(m->who, who);
  779. m->timer = now;
  780. }
  781. /* Removes a nick from the channel member list (returns 1 if successful)
  782. */
  783. static int real_killmember(struct chanset_t *chan, char *nick, const char *file, int line)
  784. {
  785. memberlist *x = NULL, *old = NULL;
  786. for (x = chan->channel.member; x && x->nick[0]; old = x, x = x->next)
  787. if (!rfc_casecmp(x->nick, nick))
  788. break;
  789. if (!x || !x->nick[0]) {
  790. if (!channel_pending(chan))
  791. putlog(LOG_MISC, "*", "(!) killmember(%s, %s) -> nonexistent (%s: %d)", chan->dname, nick, file, line);
  792. return 0;
  793. }
  794. if (old)
  795. old->next = x->next;
  796. else
  797. chan->channel.member = x->next;
  798. free(x);
  799. chan->channel.members--;
  800. /* The following two errors should NEVER happen. We will try to correct
  801. * them though, to keep the bot from crashing.
  802. */
  803. if (chan->channel.members < 0) {
  804. chan->channel.members = 0;
  805. for (x = chan->channel.member; x && x->nick[0]; x = x->next)
  806. chan->channel.members++;
  807. putlog(LOG_MISC, "*", "(!) actually I know of %d members.", chan->channel.members);
  808. }
  809. if (!chan->channel.member) {
  810. chan->channel.member = (memberlist *) calloc(1, sizeof(memberlist));
  811. chan->channel.member->nick[0] = 0;
  812. chan->channel.member->next = NULL;
  813. }
  814. return 1;
  815. }
  816. /* Check if I am a chanop. Returns boolean 1 or 0.
  817. */
  818. int me_op(struct chanset_t *chan)
  819. {
  820. memberlist *mx = NULL;
  821. mx = ismember(chan, botname);
  822. if (!mx)
  823. return 0;
  824. if (chan_hasop(mx))
  825. return 1;
  826. else
  827. return 0;
  828. }
  829. /* Check whether I'm voice. Returns boolean 1 or 0.
  830. */
  831. static int me_voice(struct chanset_t *chan)
  832. {
  833. memberlist *mx = NULL;
  834. mx = ismember(chan, botname);
  835. if (!mx)
  836. return 0;
  837. if (chan_hasvoice(mx))
  838. return 1;
  839. else
  840. return 0;
  841. }
  842. /* Check if there are any ops on the channel. Returns boolean 1 or 0.
  843. */
  844. static int any_ops(struct chanset_t *chan)
  845. {
  846. memberlist *x = NULL;
  847. for (x = chan->channel.member; x && x->nick[0]; x = x->next)
  848. if (chan_hasop(x))
  849. break;
  850. if (!x || !x->nick[0])
  851. return 0;
  852. return 1;
  853. }
  854. /* Reset the channel information.
  855. */
  856. static void reset_chan_info(struct chanset_t *chan)
  857. {
  858. int opped = 0;
  859. /* Don't reset the channel if we're already resetting it */
  860. if (!shouldjoin(chan)) {
  861. dprintf(DP_MODE, "PART %s\n", chan->name);
  862. return;
  863. }
  864. if (!channel_pending(chan)) {
  865. if (me_op(chan))
  866. opped += 1;
  867. free(chan->channel.key);
  868. chan->channel.key = (char *) calloc(1, 1);
  869. clear_channel(chan, 1);
  870. chan->status |= CHAN_PEND;
  871. chan->status &= ~(CHAN_ACTIVE | CHAN_ASKEDMODES);
  872. if (!(chan->status & CHAN_ASKEDBANS)) {
  873. chan->status |= CHAN_ASKEDBANS;
  874. dprintf(DP_MODE, "MODE %s +b\n", chan->name);
  875. }
  876. if (opped && !channel_take(chan)) {
  877. if (!(chan->ircnet_status & CHAN_ASKED_EXEMPTS) &&
  878. use_exempts == 1) {
  879. chan->ircnet_status |= CHAN_ASKED_EXEMPTS;
  880. dprintf(DP_MODE, "MODE %s +e\n", chan->name);
  881. }
  882. if (!(chan->ircnet_status & CHAN_ASKED_INVITED) &&
  883. use_invites == 1) {
  884. chan->ircnet_status |= CHAN_ASKED_INVITED;
  885. dprintf(DP_MODE, "MODE %s +I\n", chan->name);
  886. }
  887. }
  888. /* These 2 need to get out asap, so into the mode queue */
  889. dprintf(DP_MODE, "MODE %s\n", chan->name);
  890. if (use_354)
  891. dprintf(DP_MODE, "WHO %s %%c%%h%%n%%u%%f\n", chan->name);
  892. else
  893. dprintf(DP_MODE, "WHO %s\n", chan->name);
  894. /* clear_channel nuked the data...so */
  895. }
  896. }
  897. /* Leave the specified channel and notify registered Tcl procs. This
  898. * should not be called by itsself.
  899. */
  900. void do_channel_part(struct chanset_t *chan)
  901. {
  902. if (shouldjoin(chan) && chan->name[0]) {
  903. /* Using chan->name is important here, especially for !chans <cybah> */
  904. dprintf(DP_SERVER, "PART %s\n", chan->name);
  905. /* As we don't know of this channel anymore when we receive the server's
  906. ack for the above PART, we have to notify about it _now_. */
  907. }
  908. }
  909. /* If i'm the only person on the channel, and i'm not op'd,
  910. * might as well leave and rejoin. If i'm NOT the only person
  911. * on the channel, but i'm still not op'd, demand ops.
  912. */
  913. static void check_lonely_channel(struct chanset_t *chan)
  914. {
  915. memberlist *m = NULL;
  916. char s[UHOSTLEN] = "";
  917. int i = 0;
  918. static int whined = 0;
  919. if (channel_pending(chan) || !channel_active(chan) || me_op(chan) ||
  920. !shouldjoin(chan) || (chan->channel.mode & CHANANON))
  921. return;
  922. /* Count non-split channel members */
  923. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  924. if (!chan_issplit(m))
  925. i++;
  926. if (i == 1 && channel_cycle(chan) && !channel_stop_cycle(chan)) {
  927. if (chan->name[0] != '+') { /* Its pointless to cycle + chans for ops */
  928. putlog(LOG_MISC, "*", "Trying to cycle %s to regain ops.", chan->dname);
  929. dprintf(DP_MODE, "PART %s\n", chan->name);
  930. /* If it's a !chan, we need to recreate the channel with !!chan <cybah> */
  931. dprintf(DP_MODE, "JOIN %s%s %s\n", (chan->dname[0] == '!') ? "!" : "",
  932. chan->dname, chan->key_prot);
  933. whined = 0;
  934. }
  935. } else if (any_ops(chan)) {
  936. whined = 0;
  937. request_op(chan);
  938. /* need: op */
  939. } else {
  940. /* Other people here, but none are ops. If there are other bots make
  941. * them LEAVE!
  942. */
  943. int ok = 1;
  944. struct userrec *u = NULL;
  945. if (!whined) {
  946. /* + is opless. Complaining about no ops when without special
  947. * help(services), we cant get them - Raist
  948. */
  949. if (chan->name[0] != '+')
  950. putlog(LOG_MISC, "*", "%s is active but has no ops :(", chan->dname);
  951. whined = 1;
  952. }
  953. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  954. sprintf(s, "%s!%s", m->nick, m->userhost);
  955. u = get_user_by_host(s);
  956. if (!match_my_nick(m->nick) && (!u || !(u->flags & USER_BOT))) {
  957. ok = 0;
  958. break;
  959. }
  960. }
  961. if (ok && channel_cycle(chan)) {
  962. /* ALL bots! make them LEAVE!!! */
  963. /*
  964. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  965. if (!match_my_nick(m->nick))
  966. dprintf(DP_SERVER, "PRIVMSG %s :go %s\n", m->nick, chan->dname);
  967. */
  968. } else {
  969. /* Some humans on channel, but still op-less */
  970. request_op(chan);
  971. /* need: op */
  972. }
  973. }
  974. }
  975. static void warn_pls_take()
  976. {
  977. struct chanset_t *chan = NULL;
  978. for (chan = chanset; chan; chan = chan->next)
  979. if (channel_take(chan) && me_op(chan))
  980. putlog(LOG_WARN, "*", "%s is set +take, and I'm already opped! +take is insecure, try +bitch instead", chan->dname);
  981. }
  982. /* FIXME: max sendq will occur. */
  983. void check_servers() {
  984. struct chanset_t *chan = NULL;
  985. for (chan = chanset; chan; chan = chan->next) {
  986. memberlist *m = NULL;
  987. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  988. if (!match_my_nick(m->nick) && chan_hasop(m) && (!m->server || (m->server && !m->server[0]) || (m->hops == -1))) {
  989. putlog(LOG_DEBUG, "*", "Updating WHO for '%s' because '%s' is missing data.", chan->dname, m->nick);
  990. dprintf(DP_HELP, "WHO %s\n", chan->name);
  991. break; /* lets just do one chan at a time to save from flooding */
  992. }
  993. }
  994. }
  995. }
  996. #ifdef S_AUTOLOCK
  997. void check_netfight()
  998. {
  999. int limit = atoi(CFG_FIGHTTHRESHOLD.gdata ? CFG_FIGHTTHRESHOLD.gdata : "0");
  1000. if (limit) {
  1001. struct chanset_t *chan = NULL;
  1002. for (chan = chanset; chan; chan = chan->next) {
  1003. if ((chan->channel.fighting) && (chan->channel.fighting > limit)) {
  1004. if (!channel_bitch(chan) || !channel_closed(chan)) {
  1005. putlog(LOG_WARN, "*", "Auto-closed %s - channel fight", chan->dname);
  1006. do_chanset(NULL, chan, "+bitch +closed", DO_LOCAL | DO_NET);
  1007. enforce_closed(chan);
  1008. dprintf(DP_MODE, "TOPIC %s :Auto-closed - channel fight\n", chan->name);
  1009. }
  1010. }
  1011. chan->channel.fighting = 0; /* we put this here because we need to clear it once per min */
  1012. }
  1013. }
  1014. }
  1015. #endif /* S_AUTOLOCK */
  1016. void raise_limit(struct chanset_t * chan) {
  1017. int nl, cl, i, mem, ul, ll;
  1018. char s[50] = "";
  1019. if (!chan || !me_op(chan)) {
  1020. return;
  1021. }
  1022. mem = chan->channel.members; //members
  1023. nl = mem + chan->limitraise; //new limit
  1024. cl = chan->channel.maxmembers; //current limit
  1025. i = chan->limitraise / 4;
  1026. /* if the newlimit will be in the range made by these vars, dont change. */
  1027. ul = nl + i; //upper limit range
  1028. ll = nl - i; //lower limit range
  1029. if (cl > ll && cl < ul) {
  1030. return; /* the current limit is in the range, so leave it. */
  1031. }
  1032. if (nl != chan->channel.maxmembers) {
  1033. sprintf(s, "%d", nl);
  1034. add_mode(chan, '+', 'l', s);
  1035. }
  1036. }
  1037. static void check_expired_chanstuff()
  1038. {
  1039. masklist *b = NULL, *e = NULL;
  1040. memberlist *m = NULL, *n = NULL;
  1041. char s[UHOSTLEN] = "";
  1042. struct chanset_t *chan;
  1043. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0};
  1044. if (!server_online)
  1045. return;
  1046. for (chan = chanset; chan; chan = chan->next) {
  1047. if (channel_active(chan)) {
  1048. if (me_op(chan)) {
  1049. if (channel_dynamicbans(chan) && chan->ban_time)
  1050. for (b = chan->channel.ban; b->mask[0]; b = b->next)
  1051. if (now - b->timer > 60 * chan->ban_time &&
  1052. !u_sticky_mask(chan->bans, b->mask) &&
  1053. !u_sticky_mask(global_bans, b->mask) &&
  1054. expired_mask(chan, b->who)) {
  1055. putlog(LOG_MODES, chan->dname,
  1056. "(%s) Channel ban on %s expired.",
  1057. chan->dname, b->mask);
  1058. add_mode(chan, '-', 'b', b->mask);
  1059. b->timer = now;
  1060. }
  1061. if (use_exempts && channel_dynamicexempts(chan) && chan->exempt_time)
  1062. for (e = chan->channel.exempt; e->mask[0]; e = e->next)
  1063. if (now - e->timer > 60 * chan->exempt_time &&
  1064. !u_sticky_mask(chan->exempts, e->mask) &&
  1065. !u_sticky_mask(global_exempts, e->mask) &&
  1066. expired_mask(chan, e->who)) {
  1067. /* Check to see if it matches a ban */
  1068. int match = 0;
  1069. for (b = chan->channel.ban; b->mask[0]; b = b->next)
  1070. if (wild_match(b->mask, e->mask) ||
  1071. wild_match(e->mask, b->mask)) {
  1072. match = 1;
  1073. break;
  1074. }
  1075. /* Leave this extra logging in for now. Can be removed later
  1076. * Jason
  1077. */
  1078. if (match) {
  1079. putlog(LOG_MODES, chan->dname,
  1080. "(%s) Channel exemption %s NOT expired. Exempt still set!",
  1081. chan->dname, e->mask);
  1082. } else {
  1083. putlog(LOG_MODES, chan->dname,
  1084. "(%s) Channel exemption on %s expired.",
  1085. chan->dname, e->mask);
  1086. add_mode(chan, '-', 'e', e->mask);
  1087. }
  1088. e->timer = now;
  1089. }
  1090. if (use_invites && channel_dynamicinvites(chan) &&
  1091. chan->invite_time && !(chan->channel.mode & CHANINV))
  1092. for (b = chan->channel.invite; b->mask[0]; b = b->next)
  1093. if (now - b->timer > 60 * chan->invite_time &&
  1094. !u_sticky_mask(chan->invites, b->mask) &&
  1095. !u_sticky_mask(global_invites, b->mask) &&
  1096. expired_mask(chan, b->who)) {
  1097. putlog(LOG_MODES, chan->dname,
  1098. "(%s) Channel invitation on %s expired.",
  1099. chan->dname, b->mask);
  1100. add_mode(chan, '-', 'I', b->mask);
  1101. b->timer = now;
  1102. }
  1103. if (chan->idle_kick)
  1104. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  1105. if (now - m->last >= chan->idle_kick * 60 &&
  1106. !match_my_nick(m->nick) && !chan_issplit(m)) {
  1107. sprintf(s, "%s!%s", m->nick, m->userhost);
  1108. get_user_flagrec(m->user ? m->user : get_user_by_host(s),
  1109. &fr, chan->dname);
  1110. if (!(glob_bot(fr) || (glob_op(fr) && !glob_deop(fr)) || chan_op(fr))) {
  1111. dprintf(DP_SERVER, "KICK %s %s :%sidle %d min\n", chan->name,
  1112. m->nick, kickprefix, chan->idle_kick);
  1113. m->flags |= SENTKICK;
  1114. }
  1115. }
  1116. }
  1117. for (m = chan->channel.member; m && m->nick[0]; m = n) {
  1118. n = m->next;
  1119. if (m->split && now - m->split > wait_split) {
  1120. sprintf(s, "%s!%s", m->nick, m->userhost);
  1121. putlog(LOG_JOIN, chan->dname,
  1122. "%s (%s) got lost in the net-split.",
  1123. m->nick, m->userhost);
  1124. killmember(chan, m->nick);
  1125. }
  1126. m = n;
  1127. }
  1128. /* autovoice of +v users if bot is +y */
  1129. if (!loading && channel_active(chan) && me_op(chan) && dovoice(chan)) {
  1130. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1131. sprintf(s, "%s!%s", m->nick, m->userhost);
  1132. if (!m->user)
  1133. m->user = get_user_by_host(s);
  1134. if (m->user) {
  1135. struct flag_record fr2 = { FR_GLOBAL | FR_CHAN, 0, 0 };
  1136. get_user_flagrec(m->user, &fr2, chan->dname);
  1137. if (!private(fr2, chan, PRIV_VOICE) &&
  1138. ((channel_voice(chan) && !chk_devoice(fr2, chan)) ||
  1139. (!channel_voice(chan) && chk_voice(fr2, chan))) &&
  1140. !glob_bot(fr2) &&
  1141. !chan_hasop(m) && !chan_hasvoice(m) && !(m->flags & EVOICE)) {
  1142. putlog(LOG_DEBUG, "@", "VOICING %s in %s as '%s'", m->nick, chan->dname, m->user->handle);
  1143. add_mode(chan, '+', 'v', m->nick);
  1144. } else if (!glob_bot(fr2) &&
  1145. (chk_devoice(fr2, chan) || (m->flags & EVOICE))) {
  1146. if (!chan_hasop(m) && chan_hasvoice(m))
  1147. add_mode(chan, '-', 'v', m->nick);
  1148. }
  1149. } else if (m->user == NULL && !(m->flags & EVOICE)) {
  1150. if (channel_voice(chan) && !chan_hasop(m) && !chan_hasvoice(m)) {
  1151. add_mode(chan, '+', 'v', m->nick);
  1152. }
  1153. }
  1154. }
  1155. }
  1156. check_lonely_channel(chan);
  1157. }
  1158. else if (shouldjoin(chan) && !channel_pending(chan))
  1159. dprintf(DP_MODE, "JOIN %s %s\n",
  1160. (chan->name[0]) ? chan->name : chan->dname,
  1161. chan->channel.key[0] ? chan->channel.key : chan->key_prot);
  1162. }
  1163. }
  1164. #ifdef S_AUTHCMDS
  1165. static int check_bind_pubc(char *cmd, char *nick, char *from, struct userrec *u, char *args, char *chan)
  1166. {
  1167. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0};
  1168. int x = check_bind(BT_msgc, cmd, &fr, nick, from, u, args, chan);
  1169. if (x & BIND_RET_LOG)
  1170. putlog(LOG_CMDS, "*", "(%s!%s) !%s! %s %s%s %s", nick, from, u ? u->handle : "*", chan, cmdprefix, cmd, args);
  1171. if (x & BIND_RET_BREAK) return(1);
  1172. return(0);
  1173. }
  1174. #endif /* S_AUTHCMDS */
  1175. /* Flush the modes for EVERY channel.
  1176. */
  1177. void flush_modes()
  1178. {
  1179. struct chanset_t *chan = NULL;
  1180. memberlist *m = NULL;
  1181. if (modesperline > MODES_PER_LINE_MAX)
  1182. modesperline = MODES_PER_LINE_MAX;
  1183. for (chan = chanset; chan; chan = chan->next) {
  1184. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1185. if (m->delay && m->delay <= now) {
  1186. m->delay = 0L;
  1187. m->flags &= ~FULL_DELAY;
  1188. if (chan_sentop(m)) {
  1189. m->flags &= ~SENTOP;
  1190. do_op(m->nick, chan, 0);
  1191. }
  1192. if (chan_sentvoice(m)) {
  1193. m->flags &= ~SENTVOICE;
  1194. add_mode(chan, '+', 'v', m->nick);
  1195. }
  1196. }
  1197. }
  1198. flush_mode(chan, NORMAL);
  1199. }
  1200. }
  1201. void irc_report(int idx, int details)
  1202. {
  1203. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0};
  1204. char ch[1024] = "", q[160] = "", *p = NULL;
  1205. int k, l;
  1206. struct chanset_t *chan = NULL;
  1207. strcpy(q, "Channels: ");
  1208. k = 10;
  1209. for (chan = chanset; chan; chan = chan->next) {
  1210. if (idx != DP_STDOUT)
  1211. get_user_flagrec(dcc[idx].user, &fr, chan->dname);
  1212. if (!private(fr, chan, PRIV_OP) &&
  1213. (idx == DP_STDOUT || glob_master(fr) || chan_master(fr))) {
  1214. p = NULL;
  1215. if (shouldjoin(chan)) {
  1216. if (chan->status & CHAN_JUPED)
  1217. p = MISC_JUPED;
  1218. else if (!(chan->status & CHAN_ACTIVE))
  1219. p = MISC_TRYING;
  1220. else if (chan->status & CHAN_PEND)
  1221. p = MISC_PENDING;
  1222. else if ((chan->dname[0] != '+') && !me_op(chan))
  1223. p = MISC_WANTOPS;
  1224. }
  1225. l = simple_sprintf(ch, "%s%s%s%s, ", chan->dname, p ? "(" : "",
  1226. p ? p : "", p ? ")" : "");
  1227. if ((k + l) > 70) {
  1228. dprintf(idx, " %s\n", q);
  1229. strcpy(q, " ");
  1230. k = 10;
  1231. }
  1232. k += my_strcpy(q + k, ch);
  1233. }
  1234. }
  1235. if (k > 10) {
  1236. q[k - 2] = 0;
  1237. dprintf(idx, " %s\n", q);
  1238. }
  1239. }
  1240. static void do_nettype()
  1241. {
  1242. switch (net_type) {
  1243. case 0: /* Efnet */
  1244. kick_method = 1;
  1245. modesperline = 4;
  1246. use_354 = 0;
  1247. use_exempts = 1;
  1248. use_invites = 1;
  1249. max_bans = 100;
  1250. max_modes = 100;
  1251. rfc_compliant = 1;
  1252. include_lk = 0;
  1253. break;
  1254. case 1: /* Ircnet */
  1255. kick_method = 4;
  1256. modesperline = 3;
  1257. use_354 = 0;
  1258. use_exempts = 1;
  1259. use_invites = 1;
  1260. max_bans = 30;
  1261. max_modes = 30;
  1262. rfc_compliant = 1;
  1263. include_lk = 1;
  1264. break;
  1265. case 2: /* Undernet */
  1266. kick_method = 1;
  1267. modesperline = 6;
  1268. use_354 = 1;
  1269. use_exempts = 0;
  1270. use_invites = 0;
  1271. max_bans = 30;
  1272. max_modes = 30;
  1273. rfc_compliant = 1;
  1274. include_lk = 1;
  1275. break;
  1276. case 3: /* Dalnet */
  1277. kick_method = 1;
  1278. modesperline = 6;
  1279. use_354 = 0;
  1280. use_exempts = 0;
  1281. use_invites = 0;
  1282. max_bans = 100;
  1283. max_modes = 100;
  1284. rfc_compliant = 0;
  1285. include_lk = 1;
  1286. break;
  1287. case 4: /* hybrid-6+ */
  1288. kick_method = 1;
  1289. modesperline = 4;
  1290. use_354 = 0;
  1291. use_exempts = 1;
  1292. use_invites = 0;
  1293. max_bans = 20;
  1294. max_modes = 20;
  1295. rfc_compliant = 1;
  1296. include_lk = 0;
  1297. break;
  1298. default:
  1299. break;
  1300. }
  1301. /* Update all rfc_ function pointers */
  1302. /* add_hook(HOOK_RFC_CASECMP, (Function) rfc_compliant); */
  1303. }
  1304. static cmd_t irc_bot[] = {
  1305. {"dp", "", (Function) mdop_request, NULL},
  1306. {"gi", "", (Function) getin_request, NULL},
  1307. {NULL, NULL, NULL, NULL}
  1308. };
  1309. static void getin_5secondly()
  1310. {
  1311. if (server_online) {
  1312. struct chanset_t *chan = NULL;
  1313. for (chan = chanset; chan; chan = chan->next) {
  1314. if ((!channel_pending(chan) && channel_active(chan)) && !me_op(chan))
  1315. request_op(chan);
  1316. }
  1317. }
  1318. }
  1319. void irc_changed(struct cfg_entry *cfgent, char *oldval, int *valid)
  1320. {
  1321. int i;
  1322. if (!cfgent->gdata)
  1323. return;
  1324. *valid = 0;
  1325. if (!strcmp(cfgent->name, "op-requests")) {
  1326. int L, R;
  1327. char *value = cfgent->gdata;
  1328. L = atoi(value);
  1329. value = strchr(value, ':');
  1330. if (!value)
  1331. return;
  1332. value++;
  1333. R = atoi(value);
  1334. if ((R >= 60) || (R <= 3) || (L < 1) || (L > R))
  1335. return;
  1336. *valid = 1;
  1337. return;
  1338. }
  1339. i = atoi(cfgent->gdata);
  1340. if (!strcmp(cfgent->name, "op-bots")) {
  1341. if ((i < 1) || (i > 10))
  1342. return;
  1343. } else if (!strcmp(cfgent->name, "invite-bots")) {
  1344. if ((i < 1) || (i > 10))
  1345. return;
  1346. } else if (!strcmp(cfgent->name, "key-bots")) {
  1347. if ((i < 1) || (i > 10))
  1348. return;
  1349. } else if (!strcmp(cfgent->name, "limit-bots")) {
  1350. if ((i < 1) || (i > 10))
  1351. return;
  1352. } else if (!strcmp(cfgent->name, "unban-bots")) {
  1353. if ((i < 1) || (i > 10))
  1354. return;
  1355. } else if (!strcmp(cfgent->name, "lag-threshold")) {
  1356. if ((i < 3) || (i > 60))
  1357. return;
  1358. } else if (!strcmp(cfgent->name, "fight-threshold")) {
  1359. if (i && ((i < 50) || (i > 1000)))
  1360. return;
  1361. } else if (!strcmp(cfgent->name, "op-time-slack")) {
  1362. if ((i < 30) || (i > 1200))
  1363. return;
  1364. }
  1365. *valid = 1;
  1366. return;
  1367. }
  1368. struct cfg_entry CFG_OPBOTS = {
  1369. "op-bots", CFGF_GLOBAL, NULL, NULL,
  1370. irc_changed, NULL, NULL
  1371. };
  1372. struct cfg_entry CFG_INBOTS = {
  1373. "in-bots", CFGF_GLOBAL, NULL, NULL,
  1374. irc_changed, NULL, NULL
  1375. };
  1376. struct cfg_entry CFG_LAGTHRESHOLD = {
  1377. "lag-threshold", CFGF_GLOBAL, NULL, NULL,
  1378. irc_changed, NULL, NULL
  1379. };
  1380. struct cfg_entry CFG_OPREQUESTS = {
  1381. "op-requests", CFGF_GLOBAL, NULL, NULL,
  1382. irc_changed, NULL, NULL
  1383. };
  1384. struct cfg_entry CFG_OPTIMESLACK = {
  1385. "op-time-slack", CFGF_GLOBAL, NULL, NULL,
  1386. irc_changed, NULL, NULL
  1387. };
  1388. #ifdef S_AUTOLOCK
  1389. struct cfg_entry CFG_FIGHTTHRESHOLD = {
  1390. "fight-threshold", CFGF_GLOBAL, NULL, NULL,
  1391. irc_changed, NULL, NULL
  1392. };
  1393. #endif /* S_AUTOLOCK */
  1394. void irc_init()
  1395. {
  1396. struct chanset_t *chan = NULL;
  1397. add_cfg(&CFG_OPBOTS);
  1398. add_cfg(&CFG_INBOTS);
  1399. add_cfg(&CFG_LAGTHRESHOLD);
  1400. add_cfg(&CFG_OPREQUESTS);
  1401. add_cfg(&CFG_OPTIMESLACK);
  1402. #ifdef S_AUTOLOCK
  1403. add_cfg(&CFG_FIGHTTHRESHOLD);
  1404. #endif /* S_AUTOLOCK */
  1405. for (chan = chanset; chan; chan = chan->next) {
  1406. if (shouldjoin(chan))
  1407. dprintf(DP_MODE, "JOIN %s %s\n",
  1408. (chan->name[0]) ? chan->name : chan->dname, chan->key_prot);
  1409. chan->status &= ~(CHAN_ACTIVE | CHAN_PEND | CHAN_ASKEDBANS);
  1410. chan->ircnet_status &= ~(CHAN_ASKED_INVITED | CHAN_ASKED_EXEMPTS);
  1411. }
  1412. timer_create_secs(60, "check_expired_chanstuff", (Function) check_expired_chanstuff);
  1413. timer_create_secs(60, "warn_pls_take", (Function) warn_pls_take);
  1414. timer_create_secs(60, "check_servers", (Function) check_servers);
  1415. timer_create_secs(5, "getin_5secondly", (Function) getin_5secondly);
  1416. #ifdef S_AUTOLOCK
  1417. timer_create_secs(60, "check_netfight", (Function) check_netfight);
  1418. #endif /* S_AUTOLOCK */
  1419. /* Add our commands to the imported tables. */
  1420. add_builtins("dcc", irc_dcc);
  1421. add_builtins("bot", irc_bot);
  1422. add_builtins("raw", irc_raw);
  1423. add_builtins("msg", C_msg);
  1424. #ifdef S_AUTHCMDS
  1425. add_builtins("msgc", C_msgc);
  1426. #endif /* S_AUTHCMDS */
  1427. do_nettype();
  1428. }
  1429. #endif /* LEAF */