irc.c 47 KB

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