irc.c 46 KB

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