irc.c 40 KB

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