userchan.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658
  1. /*
  2. * userchan.c -- part of channels.mod
  3. *
  4. */
  5. #ifdef S_DCCPASS
  6. extern struct cmd_pass *cmdpass;
  7. #endif
  8. struct chanuserrec *get_chanrec(struct userrec *u, char *chname)
  9. {
  10. struct chanuserrec *ch;
  11. for (ch = u->chanrec; ch; ch = ch->next)
  12. if (!rfc_casecmp(ch->channel, chname))
  13. return ch;
  14. return NULL;
  15. }
  16. static struct chanuserrec *add_chanrec(struct userrec *u, char *chname)
  17. {
  18. struct chanuserrec *ch = NULL;
  19. if (findchan_by_dname(chname)) {
  20. ch = user_malloc(sizeof(struct chanuserrec));
  21. ch->next = u->chanrec;
  22. u->chanrec = ch;
  23. ch->info = NULL;
  24. ch->flags = 0;
  25. ch->flags_udef = 0;
  26. ch->laston = 0;
  27. strncpy(ch->channel, chname, 81);
  28. ch->channel[80] = 0;
  29. if (!noshare && !(u->flags & USER_UNSHARED))
  30. shareout(findchan_by_dname(chname), "+cr %s %s\n", u->handle, chname);
  31. }
  32. return ch;
  33. }
  34. static void add_chanrec_by_handle(struct userrec *bu, char *hand, char *chname)
  35. {
  36. struct userrec *u;
  37. u = get_user_by_handle(bu, hand);
  38. if (!u)
  39. return;
  40. if (!get_chanrec(u, chname))
  41. add_chanrec(u, chname);
  42. }
  43. static void get_handle_chaninfo(char *handle, char *chname, char *s)
  44. {
  45. struct userrec *u;
  46. struct chanuserrec *ch;
  47. u = get_user_by_handle(userlist, handle);
  48. if (u == NULL) {
  49. s[0] = 0;
  50. return;
  51. }
  52. ch = get_chanrec(u, chname);
  53. if (ch == NULL) {
  54. s[0] = 0;
  55. return;
  56. }
  57. if (ch->info == NULL) {
  58. s[0] = 0;
  59. return;
  60. }
  61. strcpy(s, ch->info);
  62. return;
  63. }
  64. static void set_handle_chaninfo(struct userrec *bu, char *handle,
  65. char *chname, char *info)
  66. {
  67. struct userrec *u;
  68. struct chanuserrec *ch;
  69. struct chanset_t *cst;
  70. u = get_user_by_handle(bu, handle);
  71. if (!u)
  72. return;
  73. ch = get_chanrec(u, chname);
  74. if (!ch) {
  75. add_chanrec_by_handle(bu, handle, chname);
  76. ch = get_chanrec(u, chname);
  77. }
  78. if (info) {
  79. if (strlen(info) > 80)
  80. info[80] = 0;
  81. }
  82. if (ch->info != NULL)
  83. nfree(ch->info);
  84. if (info && info[0]) {
  85. ch->info = (char *) user_malloc(strlen(info) + 1);
  86. strcpy(ch->info, info);
  87. } else
  88. ch->info = NULL;
  89. cst = findchan_by_dname(chname);
  90. if ((!noshare) && (bu == userlist) &&
  91. !(u->flags & (USER_UNSHARED | USER_BOT)) && share_greet) {
  92. shareout(cst, "chchinfo %s %s %s\n", handle, chname, info ? info : "");
  93. }
  94. }
  95. static void del_chanrec(struct userrec *u, char *chname)
  96. {
  97. struct chanuserrec *ch = u->chanrec, *lst = NULL;
  98. while (ch) {
  99. if (!rfc_casecmp(chname, ch->channel)) {
  100. if (lst == NULL)
  101. u->chanrec = ch->next;
  102. else
  103. lst->next = ch->next;
  104. if (ch->info != NULL)
  105. nfree(ch->info);
  106. nfree(ch);
  107. if (!noshare && !(u->flags & USER_UNSHARED))
  108. shareout(findchan_by_dname(chname), "-cr %s %s\n", u->handle, chname);
  109. return;
  110. }
  111. lst = ch;
  112. ch = ch->next;
  113. }
  114. }
  115. static void set_handle_laston(char *chan, struct userrec *u, time_t n)
  116. {
  117. struct chanuserrec *ch;
  118. if (!u)
  119. return;
  120. touch_laston(u, chan, n);
  121. ch = get_chanrec(u, chan);
  122. if (!ch)
  123. return;
  124. ch->laston = n;
  125. }
  126. /* Is this mask sticky?
  127. */
  128. static int u_sticky_mask(maskrec *u, char *uhost)
  129. {
  130. for (; u; u = u->next)
  131. if (!rfc_casecmp(u->mask, uhost))
  132. return (u->flags & MASKREC_STICKY);
  133. return 0;
  134. }
  135. /* Set sticky attribute for a mask.
  136. */
  137. static int u_setsticky_mask(struct chanset_t *chan, maskrec *u, char *uhost,
  138. int sticky, char *botcmd)
  139. {
  140. int j;
  141. j = atoi(uhost);
  142. if (!j)
  143. j = (-1);
  144. while(u) {
  145. if (j >= 0)
  146. j--;
  147. if (!j || ((j < 0) && !rfc_casecmp(u->mask, uhost))) {
  148. if (sticky > 0)
  149. u->flags |= MASKREC_STICKY;
  150. else if (!sticky)
  151. u->flags &= ~MASKREC_STICKY;
  152. else /* We don't actually want to change, just skip over */
  153. return 0;
  154. if (!j)
  155. strcpy(uhost, u->mask);
  156. if (!noshare)
  157. shareout(chan, "%s %s %d %s\n", botcmd, uhost, sticky,
  158. (chan) ? chan->dname : "");
  159. return 1;
  160. }
  161. u = u->next;
  162. }
  163. if (j >= 0)
  164. return -j;
  165. return 0;
  166. }
  167. /* Merge of u_equals_ban(), u_equals_exempt() and u_equals_invite().
  168. *
  169. * Returns:
  170. * 0 not a ban
  171. * 1 temporary ban
  172. * 2 perm ban
  173. */
  174. static int u_equals_mask(maskrec *u, char *mask)
  175. {
  176. for (; u; u = u->next)
  177. if (!rfc_casecmp(u->mask, mask)) {
  178. if (u->flags & MASKREC_PERM)
  179. return 2;
  180. else
  181. return 1;
  182. }
  183. return 0;
  184. }
  185. static int u_match_mask(maskrec *rec, char *mask)
  186. {
  187. for (; rec; rec = rec->next)
  188. if (wild_match(rec->mask, mask))
  189. return 1;
  190. return 0;
  191. }
  192. static int u_delban(struct chanset_t *c, char *who, int doit)
  193. {
  194. int j, i = 0;
  195. maskrec *t;
  196. maskrec **u = (c) ? &c->bans : &global_bans;
  197. char temp[256];
  198. if (!strchr(who, '!') && (j = atoi(who))) {
  199. j--;
  200. for (; (*u) && j; u = &((*u)->next), j--);
  201. if (*u) {
  202. strncpyz(temp, (*u)->mask, sizeof temp);
  203. i = 1;
  204. } else
  205. return -j - 1;
  206. } else {
  207. /* Find matching host, if there is one */
  208. for (; *u && !i; u = &((*u)->next))
  209. if (!rfc_casecmp((*u)->mask, who)) {
  210. strncpyz(temp, who, sizeof temp);
  211. i = 1;
  212. break;
  213. }
  214. if (!*u)
  215. return 0;
  216. }
  217. if (i && doit) {
  218. if (!noshare) {
  219. char *mask = str_escape(temp, ':', '\\');
  220. if (mask) {
  221. /* Distribute chan bans differently */
  222. if (c)
  223. shareout(c, "-bc %s %s\n", c->dname, mask);
  224. else
  225. shareout(NULL, "-b %s\n", mask);
  226. nfree(mask);
  227. }
  228. }
  229. if (lastdeletedmask)
  230. nfree(lastdeletedmask);
  231. lastdeletedmask = nmalloc(strlen((*u)->mask) + 1);
  232. strcpy(lastdeletedmask, (*u)->mask);
  233. nfree((*u)->mask);
  234. if ((*u)->desc)
  235. nfree((*u)->desc);
  236. if ((*u)->user)
  237. nfree((*u)->user);
  238. t = *u;
  239. *u = (*u)->next;
  240. nfree(t);
  241. }
  242. return i;
  243. }
  244. static int u_delexempt (struct chanset_t * c, char * who, int doit)
  245. {
  246. int j, i = 0;
  247. maskrec *t;
  248. maskrec **u = c ? &(c->exempts) : &global_exempts;
  249. if (!strchr(who,'!') && (j = atoi(who))) {
  250. j--;
  251. for (;(*u) && j;u=&((*u)->next),j--);
  252. if (*u) {
  253. strcpy(who, (*u)->mask);
  254. i = 1;
  255. } else
  256. return -j-1;
  257. } else {
  258. /* Find matching host, if there is one */
  259. for (;*u && !i;u=&((*u)->next))
  260. if (!rfc_casecmp((*u)->mask,who)) {
  261. i = 1;
  262. break;
  263. }
  264. if (!*u)
  265. return 0;
  266. }
  267. if (i && doit) {
  268. if (!noshare) {
  269. char *mask = str_escape(who, ':', '\\');
  270. if (mask) {
  271. /* Distribute chan exempts differently */
  272. if (c)
  273. shareout(c, "-ec %s %s\n", c->dname, mask);
  274. else
  275. shareout(NULL, "-e %s\n", mask);
  276. nfree(mask);
  277. }
  278. }
  279. nfree((*u)->mask);
  280. if ((*u)->desc)
  281. nfree((*u)->desc);
  282. if ((*u)->user)
  283. nfree((*u)->user);
  284. t = *u;
  285. *u = (*u)->next;
  286. nfree(t);
  287. }
  288. return i;
  289. }
  290. static int u_delinvite(struct chanset_t *c, char *who, int doit)
  291. {
  292. int j, i = 0;
  293. maskrec *t;
  294. maskrec **u = c ? &(c->invites) : &global_invites;
  295. if (!strchr(who,'!') && (j = atoi(who))) {
  296. j--;
  297. for (;(*u) && j;u=&((*u)->next),j--);
  298. if (*u) {
  299. strcpy(who, (*u)->mask);
  300. i = 1;
  301. } else
  302. return -j-1;
  303. } else {
  304. /* Find matching host, if there is one */
  305. for (;*u && !i; u = &((*u)->next))
  306. if (!rfc_casecmp((*u)->mask,who)) {
  307. i = 1;
  308. break;
  309. }
  310. if (!*u)
  311. return 0;
  312. }
  313. if (i && doit) {
  314. if (!noshare) {
  315. char *mask = str_escape(who, ':', '\\');
  316. if (mask) {
  317. /* Distribute chan invites differently */
  318. if (c)
  319. shareout(c, "-invc %s %s\n", c->dname, mask);
  320. else
  321. shareout(NULL, "-inv %s\n", mask);
  322. nfree(mask);
  323. }
  324. }
  325. nfree((*u)->mask);
  326. if ((*u)->desc)
  327. nfree((*u)->desc);
  328. if ((*u)->user)
  329. nfree((*u)->user);
  330. t = *u;
  331. *u = (*u)->next;
  332. nfree(t);
  333. }
  334. return i;
  335. }
  336. /* Note: If first char of note is '*' it's a sticky ban.
  337. */
  338. static int u_addban(struct chanset_t *chan, char *ban, char *from, char *note,
  339. time_t expire_time, int flags)
  340. {
  341. char host[1024], s[1024];
  342. maskrec *p = NULL, *l, **u = chan ? &chan->bans : &global_bans;
  343. module_entry *me;
  344. strcpy(host, ban);
  345. /* Choke check: fix broken bans (must have '!' and '@') */
  346. if ((strchr(host, '!') == NULL) && (strchr(host, '@') == NULL))
  347. strcat(host, "!*@*");
  348. else if (strchr(host, '@') == NULL)
  349. strcat(host, "@*");
  350. else if (strchr(host, '!') == NULL) {
  351. char *i = strchr(host, '@');
  352. strcpy(s, i);
  353. *i = 0;
  354. strcat(host, "!*");
  355. strcat(host, s);
  356. }
  357. if ((me = module_find("server", 0, 0)) && me->funcs)
  358. simple_sprintf(s, "%s!%s", me->funcs[SERVER_BOTNAME],
  359. me->funcs[SERVER_BOTUSERHOST]);
  360. else
  361. simple_sprintf(s, "%s!%s@%s", origbotname, botuser, hostname);
  362. if (wild_match(host, s)) {
  363. putlog(LOG_MISC, "*", IRC_IBANNEDME);
  364. return 0;
  365. }
  366. if (expire_time == now)
  367. return 1;
  368. for (l = *u; l; l = l->next)
  369. if (!rfc_casecmp(l->mask, host)) {
  370. p = l;
  371. break;
  372. }
  373. /* It shouldn't expire and be sticky also */
  374. if (note[0] == '*') {
  375. flags |= MASKREC_STICKY;
  376. note++;
  377. }
  378. if ((expire_time == 0L) || (flags & MASKREC_PERM)) {
  379. flags |= MASKREC_PERM;
  380. expire_time = 0L;
  381. }
  382. if (p == NULL) {
  383. p = user_malloc(sizeof(maskrec));
  384. p->next = *u;
  385. *u = p;
  386. }
  387. else {
  388. nfree( p->mask );
  389. nfree( p->user );
  390. nfree( p->desc );
  391. }
  392. p->expire = expire_time;
  393. p->added = now;
  394. p->lastactive = 0;
  395. p->flags = flags;
  396. p->mask = user_malloc(strlen(host) + 1);
  397. strcpy(p->mask, host);
  398. p->user = user_malloc(strlen(from) + 1);
  399. strcpy(p->user, from);
  400. p->desc = user_malloc(strlen(note) + 1);
  401. strcpy(p->desc, note);
  402. if (!noshare) {
  403. char *mask = str_escape(host, ':', '\\');
  404. if (mask) {
  405. if (!chan)
  406. shareout(NULL, "+b %s %lu %s%s %s %s\n", mask, expire_time - now,
  407. (flags & MASKREC_STICKY) ? "s" : "",
  408. (flags & MASKREC_PERM) ? "p" : "-", from, note);
  409. else
  410. shareout(chan, "+bc %s %lu %s %s%s %s %s\n", mask, expire_time - now,
  411. chan->dname, (flags & MASKREC_STICKY) ? "s" : "",
  412. (flags & MASKREC_PERM) ? "p" : "-", from, note);
  413. nfree(mask);
  414. }
  415. }
  416. return 1;
  417. }
  418. /* Note: If first char of note is '*' it's a sticky invite.
  419. */
  420. static int u_addinvite(struct chanset_t *chan, char *invite, char *from,
  421. char *note, time_t expire_time, int flags)
  422. {
  423. char host[1024], s[1024];
  424. maskrec *p = NULL, *l, **u = chan ? &chan->invites : &global_invites;
  425. module_entry *me;
  426. strcpy(host, invite);
  427. /* Choke check: fix broken invites (must have '!' and '@') */
  428. if ((strchr(host, '!') == NULL) && (strchr(host, '@') == NULL))
  429. strcat(host, "!*@*");
  430. else if (strchr(host, '@') == NULL)
  431. strcat(host, "@*");
  432. else if (strchr(host, '!') == NULL) {
  433. char * i = strchr(host, '@');
  434. strcpy(s, i);
  435. *i = 0;
  436. strcat(host, "!*");
  437. strcat(host, s);
  438. }
  439. if ((me = module_find("server",0,0)) && me->funcs)
  440. simple_sprintf(s, "%s!%s", me->funcs[SERVER_BOTNAME],
  441. me->funcs[SERVER_BOTUSERHOST]);
  442. else
  443. simple_sprintf(s, "%s!%s@%s", origbotname, botuser, hostname);
  444. for (l = *u; l; l = l->next)
  445. if (!rfc_casecmp(l->mask, host)) {
  446. p = l;
  447. break;
  448. }
  449. /* It shouldn't expire and be sticky also */
  450. if (note[0] == '*') {
  451. flags |= MASKREC_STICKY;
  452. note++;
  453. }
  454. if ((expire_time == 0L) || (flags & MASKREC_PERM)) {
  455. flags |= MASKREC_PERM;
  456. expire_time = 0L;
  457. }
  458. if (p == NULL) {
  459. p = user_malloc(sizeof(maskrec));
  460. p->next = *u;
  461. *u = p;
  462. }
  463. else {
  464. nfree( p->mask );
  465. nfree( p->user );
  466. nfree( p->desc );
  467. }
  468. p->expire = expire_time;
  469. p->added = now;
  470. p->lastactive = 0;
  471. p->flags = flags;
  472. p->mask = user_malloc(strlen(host)+1);
  473. strcpy(p->mask,host);
  474. p->user = user_malloc(strlen(from)+1);
  475. strcpy(p->user,from);
  476. p->desc = user_malloc(strlen(note)+1);
  477. strcpy(p->desc,note);
  478. if (!noshare) {
  479. char *mask = str_escape(host, ':', '\\');
  480. if (mask) {
  481. if (!chan)
  482. shareout(NULL, "+inv %s %lu %s%s %s %s\n", mask, expire_time - now,
  483. (flags & MASKREC_STICKY) ? "s" : "",
  484. (flags & MASKREC_PERM) ? "p": "-", from, note);
  485. else
  486. shareout(chan, "+invc %s %lu %s %s%s %s %s\n", mask, expire_time - now,
  487. chan->dname, (flags & MASKREC_STICKY) ? "s" : "",
  488. (flags & MASKREC_PERM) ? "p": "-", from, note);
  489. nfree(mask);
  490. }
  491. }
  492. return 1;
  493. }
  494. /* Note: If first char of note is '*' it's a sticky exempt.
  495. */
  496. static int u_addexempt(struct chanset_t *chan, char *exempt, char *from,
  497. char *note, time_t expire_time, int flags)
  498. {
  499. char host[1024], s[1024];
  500. maskrec *p = NULL, *l, **u = chan ? &chan->exempts : &global_exempts;
  501. module_entry *me;
  502. strcpy(host, exempt);
  503. /* Choke check: fix broken exempts (must have '!' and '@') */
  504. if ((strchr(host, '!') == NULL) && (strchr(host, '@') == NULL))
  505. strcat(host, "!*@*");
  506. else if (strchr(host, '@') == NULL)
  507. strcat(host, "@*");
  508. else if (strchr(host, '!') == NULL) {
  509. char * i = strchr(host, '@');
  510. strcpy(s, i);
  511. *i = 0;
  512. strcat(host, "!*");
  513. strcat(host, s);
  514. }
  515. if ((me = module_find("server",0,0)) && me->funcs)
  516. simple_sprintf(s, "%s!%s", me->funcs[SERVER_BOTNAME],
  517. me->funcs[SERVER_BOTUSERHOST]);
  518. else
  519. simple_sprintf(s, "%s!%s@%s", origbotname, botuser, hostname);
  520. for (l = *u; l; l = l->next)
  521. if (!rfc_casecmp(l->mask, host)) {
  522. p = l;
  523. break;
  524. }
  525. /* It shouldn't expire and be sticky also */
  526. if (note[0] == '*') {
  527. flags |= MASKREC_STICKY;
  528. note++;
  529. }
  530. if ((expire_time == 0L) || (flags & MASKREC_PERM)) {
  531. flags |= MASKREC_PERM;
  532. expire_time = 0L;
  533. }
  534. if (p == NULL) {
  535. p = user_malloc(sizeof(maskrec));
  536. p->next = *u;
  537. *u = p;
  538. }
  539. else {
  540. nfree( p->mask );
  541. nfree( p->user );
  542. nfree( p->desc );
  543. }
  544. p->expire = expire_time;
  545. p->added = now;
  546. p->lastactive = 0;
  547. p->flags = flags;
  548. p->mask = user_malloc(strlen(host)+1);
  549. strcpy(p->mask,host);
  550. p->user = user_malloc(strlen(from)+1);
  551. strcpy(p->user,from);
  552. p->desc = user_malloc(strlen(note)+1);
  553. strcpy(p->desc,note);
  554. if (!noshare) {
  555. char *mask = str_escape(host, ':', '\\');
  556. if (mask) {
  557. if (!chan)
  558. shareout(NULL, "+e %s %lu %s%s %s %s\n", mask, expire_time - now,
  559. (flags & MASKREC_STICKY) ? "s" : "",
  560. (flags & MASKREC_PERM) ? "p": "-", from, note);
  561. else
  562. shareout(chan, "+ec %s %lu %s %s%s %s %s\n", mask, expire_time - now,
  563. chan->dname, (flags & MASKREC_STICKY) ? "s" : "",
  564. (flags & MASKREC_PERM) ? "p": "-", from, note);
  565. nfree(mask);
  566. }
  567. }
  568. return 1;
  569. }
  570. /* Take host entry from ban list and display it ban-style.
  571. */
  572. static void display_ban(int idx, int number, maskrec *ban,
  573. struct chanset_t *chan, int show_inact)
  574. {
  575. char dates[81], s[41];
  576. if (ban->added) {
  577. daysago(now, ban->added, s);
  578. sprintf(dates, "%s %s", MODES_CREATED, s);
  579. if (ban->added < ban->lastactive) {
  580. strcat(dates, ", ");
  581. strcat(dates, MODES_LASTUSED);
  582. strcat(dates, " ");
  583. daysago(now, ban->lastactive, s);
  584. strcat(dates, s);
  585. }
  586. } else
  587. dates[0] = 0;
  588. if (ban->flags & MASKREC_PERM)
  589. strcpy(s, "(perm)");
  590. else {
  591. char s1[41];
  592. days(ban->expire, now, s1);
  593. sprintf(s, "(expires %s)", s1);
  594. }
  595. if (ban->flags & MASKREC_STICKY)
  596. strcat(s, " (sticky)");
  597. if (!chan || ischanban(chan, ban->mask)) {
  598. if (number >= 0) {
  599. dprintf(idx, " [%3d] %s %s\n", number, ban->mask, s);
  600. } else {
  601. dprintf(idx, "BAN: %s %s\n", ban->mask, s);
  602. }
  603. } else if (show_inact) {
  604. if (number >= 0) {
  605. dprintf(idx, "! [%3d] %s %s\n", number, ban->mask, s);
  606. } else {
  607. dprintf(idx, "BAN (%s): %s %s\n", MODES_INACTIVE, ban->mask, s);
  608. }
  609. } else
  610. return;
  611. dprintf(idx, " %s: %s\n", ban->user, ban->desc);
  612. if (dates[0])
  613. dprintf(idx, " %s\n", dates);
  614. }
  615. /* Take host entry from exempt list and display it ban-style.
  616. */
  617. static void display_exempt(int idx, int number, maskrec *exempt,
  618. struct chanset_t *chan, int show_inact)
  619. {
  620. char dates[81], s[41];
  621. if (exempt->added) {
  622. daysago(now, exempt->added, s);
  623. sprintf(dates, "%s %s", MODES_CREATED, s);
  624. if (exempt->added < exempt->lastactive) {
  625. strcat(dates, ", ");
  626. strcat(dates, MODES_LASTUSED);
  627. strcat(dates, " ");
  628. daysago(now, exempt->lastactive, s);
  629. strcat(dates, s);
  630. }
  631. } else
  632. dates[0] = 0;
  633. if (exempt->flags & MASKREC_PERM)
  634. strcpy(s, "(perm)");
  635. else {
  636. char s1[41];
  637. days(exempt->expire, now, s1);
  638. sprintf(s, "(expires %s)", s1);
  639. }
  640. if (exempt->flags & MASKREC_STICKY)
  641. strcat(s, " (sticky)");
  642. if (!chan || ischanexempt(chan, exempt->mask)) {
  643. if (number >= 0) {
  644. dprintf(idx, " [%3d] %s %s\n", number, exempt->mask, s);
  645. } else {
  646. dprintf(idx, "EXEMPT: %s %s\n", exempt->mask, s);
  647. }
  648. } else if (show_inact) {
  649. if (number >= 0) {
  650. dprintf(idx, "! [%3d] %s %s\n", number, exempt->mask, s);
  651. } else {
  652. dprintf(idx, "EXEMPT (%s): %s %s\n", MODES_INACTIVE, exempt->mask, s);
  653. }
  654. } else
  655. return;
  656. dprintf(idx, " %s: %s\n", exempt->user, exempt->desc);
  657. if (dates[0])
  658. dprintf(idx, " %s\n", dates);
  659. }
  660. /* Take host entry from invite list and display it ban-style.
  661. */
  662. static void display_invite (int idx, int number, maskrec *invite,
  663. struct chanset_t *chan, int show_inact)
  664. {
  665. char dates[81], s[41];
  666. if (invite->added) {
  667. daysago(now, invite->added, s);
  668. sprintf(dates, "%s %s", MODES_CREATED, s);
  669. if (invite->added < invite->lastactive) {
  670. strcat(dates, ", ");
  671. strcat(dates, MODES_LASTUSED);
  672. strcat(dates, " ");
  673. daysago(now, invite->lastactive, s);
  674. strcat(dates, s);
  675. }
  676. } else
  677. dates[0] = 0;
  678. if (invite->flags & MASKREC_PERM)
  679. strcpy(s, "(perm)");
  680. else {
  681. char s1[41];
  682. days(invite->expire, now, s1);
  683. sprintf(s, "(expires %s)", s1);
  684. }
  685. if (invite->flags & MASKREC_STICKY)
  686. strcat(s, " (sticky)");
  687. if (!chan || ischaninvite(chan, invite->mask)) {
  688. if (number >= 0) {
  689. dprintf(idx, " [%3d] %s %s\n", number, invite->mask, s);
  690. } else {
  691. dprintf(idx, "INVITE: %s %s\n", invite->mask, s);
  692. }
  693. } else if (show_inact) {
  694. if (number >= 0) {
  695. dprintf(idx, "! [%3d] %s %s\n", number, invite->mask, s);
  696. } else {
  697. dprintf(idx, "INVITE (%s): %s %s\n", MODES_INACTIVE, invite->mask, s);
  698. }
  699. } else
  700. return;
  701. dprintf(idx, " %s: %s\n", invite->user, invite->desc);
  702. if (dates[0])
  703. dprintf(idx, " %s\n", dates);
  704. }
  705. static void tell_bans(int idx, int show_inact, char *match)
  706. {
  707. int k = 1;
  708. char *chname;
  709. struct chanset_t *chan = NULL;
  710. maskrec *u;
  711. /* Was a channel given? */
  712. if (match[0]) {
  713. chname = newsplit(&match);
  714. if (chname[0] && (strchr(CHANMETA, chname[0]))) {
  715. chan = findchan_by_dname(chname);
  716. if (!chan) {
  717. dprintf(idx, "%s.\n", CHAN_NOSUCH);
  718. return;
  719. }
  720. } else
  721. match = chname;
  722. }
  723. /* don't return here, we want to show global bans even if no chan */
  724. if (!chan && !(chan = findchan_by_dname(dcc[idx].u.chat->con_chan)) &&
  725. !(chan = chanset))
  726. chan = NULL;
  727. if (chan && show_inact)
  728. dprintf(idx, "%s: (! = %s %s)\n", BANS_GLOBAL,
  729. MODES_NOTACTIVE, chan->dname);
  730. else
  731. dprintf(idx, "%s:\n", BANS_GLOBAL);
  732. for (u = global_bans; u; u = u->next) {
  733. if (match[0]) {
  734. if ((wild_match(match, u->mask)) ||
  735. (wild_match(match, u->desc)) ||
  736. (wild_match(match, u->user)))
  737. display_ban(idx, k, u, chan, 1);
  738. k++;
  739. } else
  740. display_ban(idx, k++, u, chan, show_inact);
  741. }
  742. if (chan) {
  743. if (show_inact)
  744. dprintf(idx, "%s %s: (! = %s, * = %s)\n",
  745. BANS_BYCHANNEL, chan->dname,
  746. MODES_NOTACTIVE2, MODES_NOTBYBOT);
  747. else
  748. dprintf(idx, "%s %s: (* = %s)\n",
  749. BANS_BYCHANNEL, chan->dname,
  750. MODES_NOTBYBOT);
  751. for (u = chan->bans; u; u = u->next) {
  752. if (match[0]) {
  753. if ((wild_match(match, u->mask)) ||
  754. (wild_match(match, u->desc)) ||
  755. (wild_match(match, u->user)))
  756. display_ban(idx, k, u, chan, 1);
  757. k++;
  758. } else
  759. display_ban(idx, k++, u, chan, show_inact);
  760. }
  761. if (chan->status & CHAN_ACTIVE) {
  762. masklist *b;
  763. char s[UHOSTLEN], *s1, *s2, fill[256];
  764. int min, sec;
  765. for (b = chan->channel.ban; b && b->mask[0]; b = b->next) {
  766. if ((!u_equals_mask(global_bans, b->mask)) &&
  767. (!u_equals_mask(chan->bans, b->mask))) {
  768. strcpy(s, b->who);
  769. s2 = s;
  770. s1 = splitnick(&s2);
  771. if (s1[0])
  772. sprintf(fill, "%s (%s!%s)", b->mask, s1, s2);
  773. else
  774. sprintf(fill, "%s (server %s)", b->mask, s2);
  775. if (b->timer != 0) {
  776. min = (now - b->timer) / 60;
  777. sec = (now - b->timer) - (min * 60);
  778. sprintf(s, " (active %02d:%02d)", min, sec);
  779. strcat(fill, s);
  780. }
  781. if ((!match[0]) || (wild_match(match, b->mask)))
  782. dprintf(idx, "* [%3d] %s\n", k, fill);
  783. k++;
  784. }
  785. }
  786. }
  787. }
  788. if (k == 1)
  789. dprintf(idx, "(There are no bans, permanent or otherwise.)\n");
  790. if ((!show_inact) && (!match[0]))
  791. dprintf(idx, "%s.\n", BANS_USEBANSALL);
  792. }
  793. static void tell_exempts(int idx, int show_inact, char *match)
  794. {
  795. int k = 1;
  796. char *chname;
  797. struct chanset_t *chan = NULL;
  798. maskrec *u;
  799. /* Was a channel given? */
  800. if (match[0]) {
  801. chname = newsplit(&match);
  802. if (chname[0] && strchr(CHANMETA, chname[0])) {
  803. chan = findchan_by_dname(chname);
  804. if (!chan) {
  805. dprintf(idx, "%s.\n", CHAN_NOSUCH);
  806. return;
  807. }
  808. } else
  809. match = chname;
  810. }
  811. /* don't return here, we want to show global exempts even if no chan */
  812. if (!chan && !(chan = findchan_by_dname(dcc[idx].u.chat->con_chan))
  813. && !(chan = chanset))
  814. chan = NULL;
  815. if (chan && show_inact)
  816. dprintf(idx, "%s: (! = %s %s)\n", EXEMPTS_GLOBAL,
  817. MODES_NOTACTIVE, chan->dname);
  818. else
  819. dprintf(idx, "%s:\n", EXEMPTS_GLOBAL);
  820. for (u = global_exempts; u; u = u->next) {
  821. if (match[0]) {
  822. if ((wild_match(match, u->mask)) ||
  823. (wild_match(match, u->desc)) ||
  824. (wild_match(match, u->user)))
  825. display_exempt(idx, k, u, chan, 1);
  826. k++;
  827. } else
  828. display_exempt(idx, k++, u, chan, show_inact);
  829. }
  830. if (chan) {
  831. if (show_inact)
  832. dprintf(idx, "%s %s: (! = %s, * = %s)\n",
  833. EXEMPTS_BYCHANNEL, chan->dname,
  834. MODES_NOTACTIVE2,
  835. MODES_NOTBYBOT);
  836. else
  837. dprintf(idx, "%s %s: (* = %s)\n",
  838. EXEMPTS_BYCHANNEL, chan->dname,
  839. MODES_NOTBYBOT);
  840. for (u = chan->exempts; u; u = u->next) {
  841. if (match[0]) {
  842. if ((wild_match(match, u->mask)) ||
  843. (wild_match(match, u->desc)) ||
  844. (wild_match(match, u->user)))
  845. display_exempt(idx, k, u, chan, 1);
  846. k++;
  847. } else
  848. display_exempt(idx, k++, u, chan, show_inact);
  849. }
  850. if (chan->status & CHAN_ACTIVE) {
  851. masklist *e;
  852. char s[UHOSTLEN], *s1, *s2,fill[256];
  853. int min, sec;
  854. for (e = chan->channel.exempt; e && e->mask[0]; e = e->next) {
  855. if ((!u_equals_mask(global_exempts,e->mask)) &&
  856. (!u_equals_mask(chan->exempts, e->mask))) {
  857. strcpy(s, e->who);
  858. s2 = s;
  859. s1 = splitnick(&s2);
  860. if (s1[0])
  861. sprintf(fill, "%s (%s!%s)", e->mask, s1, s2);
  862. else
  863. sprintf(fill, "%s (server %s)", e->mask, s2);
  864. if (e->timer != 0) {
  865. min = (now - e->timer) / 60;
  866. sec = (now - e->timer) - (min * 60);
  867. sprintf(s, " (active %02d:%02d)", min, sec);
  868. strcat(fill, s);
  869. }
  870. if ((!match[0]) || (wild_match(match, e->mask)))
  871. dprintf(idx, "* [%3d] %s\n", k, fill);
  872. k++;
  873. }
  874. }
  875. }
  876. }
  877. if (k == 1)
  878. dprintf(idx, "(There are no ban exempts, permanent or otherwise.)\n");
  879. if ((!show_inact) && (!match[0]))
  880. dprintf(idx, "%s.\n", EXEMPTS_USEEXEMPTSALL);
  881. }
  882. static void tell_invites(int idx, int show_inact, char *match)
  883. {
  884. int k = 1;
  885. char *chname;
  886. struct chanset_t *chan = NULL;
  887. maskrec *u;
  888. /* Was a channel given? */
  889. if (match[0]) {
  890. chname = newsplit(&match);
  891. if (chname[0] && strchr(CHANMETA, chname[0])) {
  892. chan = findchan_by_dname(chname);
  893. if (!chan) {
  894. dprintf(idx, "%s.\n", CHAN_NOSUCH);
  895. return;
  896. }
  897. } else
  898. match = chname;
  899. }
  900. /* don't return here, we want to show global invites even if no chan */
  901. if (!chan && !(chan = findchan_by_dname(dcc[idx].u.chat->con_chan))
  902. && !(chan = chanset))
  903. chan = NULL;
  904. if (chan && show_inact)
  905. dprintf(idx, "%s: (! = %s %s)\n", INVITES_GLOBAL,
  906. MODES_NOTACTIVE, chan->dname);
  907. else
  908. dprintf(idx, "%s:\n", INVITES_GLOBAL);
  909. for (u = global_invites; u; u = u->next) {
  910. if (match[0]) {
  911. if ((wild_match(match, u->mask)) ||
  912. (wild_match(match, u->desc)) ||
  913. (wild_match(match, u->user)))
  914. display_invite(idx, k, u, chan, 1);
  915. k++;
  916. } else
  917. display_invite(idx, k++, u, chan, show_inact);
  918. }
  919. if (chan) {
  920. if (show_inact)
  921. dprintf(idx, "%s %s: (! = %s, * = %s)\n",
  922. INVITES_BYCHANNEL, chan->dname,
  923. MODES_NOTACTIVE2,
  924. MODES_NOTBYBOT);
  925. else
  926. dprintf(idx, "%s %s: (* = %s)\n",
  927. INVITES_BYCHANNEL, chan->dname,
  928. MODES_NOTBYBOT);
  929. for (u = chan->invites; u; u = u->next) {
  930. if (match[0]) {
  931. if ((wild_match(match, u->mask)) ||
  932. (wild_match(match, u->desc)) ||
  933. (wild_match(match, u->user)))
  934. display_invite(idx, k, u, chan, 1);
  935. k++;
  936. } else
  937. display_invite(idx, k++, u, chan, show_inact);
  938. }
  939. if (chan->status & CHAN_ACTIVE) {
  940. masklist *i;
  941. char s[UHOSTLEN], *s1, *s2,fill[256];
  942. int min, sec;
  943. for (i = chan->channel.invite; i && i->mask[0]; i = i->next) {
  944. if ((!u_equals_mask(global_invites,i->mask)) &&
  945. (!u_equals_mask(chan->invites, i->mask))) {
  946. strcpy(s, i->who);
  947. s2 = s;
  948. s1 = splitnick(&s2);
  949. if (s1[0])
  950. sprintf(fill, "%s (%s!%s)", i->mask, s1, s2);
  951. else
  952. sprintf(fill, "%s (server %s)", i->mask, s2);
  953. if (i->timer != 0) {
  954. min = (now - i->timer) / 60;
  955. sec = (now - i->timer) - (min * 60);
  956. sprintf(s, " (active %02d:%02d)", min, sec);
  957. strcat(fill, s);
  958. }
  959. if ((!match[0]) || (wild_match(match, i->mask)))
  960. dprintf(idx, "* [%3d] %s\n", k, fill);
  961. k++;
  962. }
  963. }
  964. }
  965. }
  966. if (k == 1)
  967. dprintf(idx, "(There are no invites, permanent or otherwise.)\n");
  968. if ((!show_inact) && (!match[0]))
  969. dprintf(idx, "%s.\n", INVITES_USEINVITESALL);
  970. }
  971. static int write_config(FILE *f, int idx)
  972. {
  973. int i = 0;
  974. #ifdef S_DCCPASS
  975. struct cmd_pass *cp;
  976. #endif
  977. putlog(LOG_DEBUG, "@", "Writing config entries...");
  978. if (lfprintf(f, CONFIG_NAME " - -\n") == EOF) /* Daemus */
  979. return 0;
  980. for (i = 0; i < cfg_count; i++)
  981. if ((cfg[i]->flags & CFGF_GLOBAL) && (cfg[i]->gdata)) {
  982. if (lfprintf(f, "@ %s %s\n", cfg[i]->name, cfg[i]->gdata ? cfg[i]->gdata : "") == EOF)
  983. return 0;
  984. }
  985. #ifdef S_DCCPASS
  986. for (cp = cmdpass; cp; cp = cp->next)
  987. if (lfprintf(f, "- %s %s\n", cp->name, cp->pass) == EOF)
  988. return 0;
  989. #endif
  990. return 1;
  991. }
  992. /* Write the ban lists and the ignore list to a file.
  993. */
  994. static int write_bans(FILE *f, int idx)
  995. {
  996. struct chanset_t *chan;
  997. maskrec *b;
  998. struct igrec *i;
  999. char *mask;
  1000. if (global_ign)
  1001. if (lfprintf(f, IGNORE_NAME " - -\n") == EOF) /* Daemus */
  1002. return 0;
  1003. for (i = global_ign; i; i = i->next) {
  1004. mask = str_escape(i->igmask, ':', '\\');
  1005. if (!mask ||
  1006. lfprintf(f, "- %s:%s%lu:%s:%lu:%s\n", mask,
  1007. (i->flags & IGREC_PERM) ? "+" : "", i->expire,
  1008. i->user ? i->user : botnetnick, i->added,
  1009. i->msg ? i->msg : "") == EOF) {
  1010. if (mask)
  1011. nfree(mask);
  1012. return 0;
  1013. }
  1014. nfree(mask);
  1015. }
  1016. if (global_bans)
  1017. if (lfprintf(f, BAN_NAME " - -\n") == EOF) /* Daemus */
  1018. return 0;
  1019. for (b = global_bans; b; b = b->next) {
  1020. mask = str_escape(b->mask, ':', '\\');
  1021. if (!mask ||
  1022. lfprintf(f, "- %s:%s%lu%s:+%lu:%lu:%s:%s\n", mask,
  1023. (b->flags & MASKREC_PERM) ? "+" : "", b->expire,
  1024. (b->flags & MASKREC_STICKY) ? "*" : "", b->added,
  1025. b->lastactive, b->user ? b->user : botnetnick,
  1026. b->desc ? b->desc : "requested") == EOF) {
  1027. if (mask)
  1028. nfree(mask);
  1029. return 0;
  1030. }
  1031. nfree(mask);
  1032. }
  1033. for (chan = chanset; chan; chan = chan->next)
  1034. if ((idx < 0) || 1) {
  1035. struct flag_record fr = {FR_CHAN | FR_GLOBAL | FR_BOT, 0, 0, 0, 0, 0};
  1036. if (idx >= 0)
  1037. get_user_flagrec(dcc[idx].user, &fr, chan->dname);
  1038. else
  1039. fr.chan = BOT_SHARE;
  1040. //if ((fr.chan & BOT_SHARE) || (fr.bot & BOT_GLOBAL)) {
  1041. if (lfprintf(f, "::%s bans\n", chan->dname) == EOF)
  1042. return 0;
  1043. for (b = chan->bans; b; b = b->next) {
  1044. mask = str_escape(b->mask, ':', '\\');
  1045. if (!mask ||
  1046. lfprintf(f, "- %s:%s%lu%s:+%lu:%lu:%s:%s\n", mask,
  1047. (b->flags & MASKREC_PERM) ? "+" : "", b->expire,
  1048. (b->flags & MASKREC_STICKY) ? "*" : "", b->added,
  1049. b->lastactive, b->user ? b->user : botnetnick,
  1050. b->desc ? b->desc : "requested") == EOF) {
  1051. if (mask)
  1052. nfree(mask);
  1053. return 0;
  1054. }
  1055. nfree(mask);
  1056. }
  1057. //}
  1058. }
  1059. return 1;
  1060. }
  1061. #ifdef S_IRCNET
  1062. /* Write the exemptlists to a file.
  1063. */
  1064. static int write_exempts(FILE *f, int idx)
  1065. {
  1066. struct chanset_t *chan;
  1067. maskrec *e;
  1068. char *mask;
  1069. if (global_exempts)
  1070. if (lfprintf(f, EXEMPT_NAME " - -\n") == EOF) /* Daemus */
  1071. return 0;
  1072. for (e = global_exempts; e; e = e->next) {
  1073. mask = str_escape(e->mask, ':', '\\');
  1074. if (!mask ||
  1075. lfprintf(f, "%s %s:%s%lu%s:+%lu:%lu:%s:%s\n", "%", e->mask,
  1076. (e->flags & MASKREC_PERM) ? "+" : "", e->expire,
  1077. (e->flags & MASKREC_STICKY) ? "*" : "", e->added,
  1078. e->lastactive, e->user ? e->user : botnetnick,
  1079. e->desc ? e->desc : "requested") == EOF) {
  1080. if (mask)
  1081. nfree(mask);
  1082. return 0;
  1083. }
  1084. nfree(mask);
  1085. }
  1086. for (chan = chanset;chan;chan=chan->next)
  1087. if ((idx < 0) || 1) {
  1088. struct flag_record fr = {FR_CHAN | FR_GLOBAL | FR_BOT, 0, 0, 0, 0, 0};
  1089. if (idx >= 0)
  1090. get_user_flagrec(dcc[idx].user,&fr,chan->dname);
  1091. else
  1092. fr.chan = BOT_SHARE;
  1093. // if ((fr.chan & BOT_SHARE) || (fr.bot & BOT_GLOBAL)) {
  1094. if (lfprintf(f, "&&%s exempts\n", chan->dname) == EOF)
  1095. return 0;
  1096. for (e = chan->exempts; e; e = e->next) {
  1097. mask = str_escape(e->mask, ':', '\\');
  1098. if (!mask ||
  1099. lfprintf(f,"%s %s:%s%lu%s:+%lu:%lu:%s:%s\n","%",e->mask,
  1100. (e->flags & MASKREC_PERM) ? "+" : "", e->expire,
  1101. (e->flags & MASKREC_STICKY) ? "*" : "", e->added,
  1102. e->lastactive, e->user ? e->user : botnetnick,
  1103. e->desc ? e->desc : "requested") == EOF) {
  1104. if (mask)
  1105. nfree(mask);
  1106. return 0;
  1107. }
  1108. nfree(mask);
  1109. }
  1110. //}
  1111. }
  1112. return 1;
  1113. }
  1114. /* Write the invitelists to a file.
  1115. */
  1116. static int write_invites(FILE *f, int idx)
  1117. {
  1118. struct chanset_t *chan;
  1119. maskrec *ir;
  1120. char *mask;
  1121. if (global_invites)
  1122. if (lfprintf(f, INVITE_NAME " - -\n") == EOF) /* Daemus */
  1123. return 0;
  1124. for (ir = global_invites; ir; ir = ir->next) {
  1125. mask = str_escape(ir->mask, ':', '\\');
  1126. if (!mask ||
  1127. lfprintf(f,"@ %s:%s%lu%s:+%lu:%lu:%s:%s\n",ir->mask,
  1128. (ir->flags & MASKREC_PERM) ? "+" : "", ir->expire,
  1129. (ir->flags & MASKREC_STICKY) ? "*" : "", ir->added,
  1130. ir->lastactive, ir->user ? ir->user : botnetnick,
  1131. ir->desc ? ir->desc : "requested") == EOF) {
  1132. if (mask)
  1133. nfree(mask);
  1134. return 0;
  1135. }
  1136. nfree(mask);
  1137. }
  1138. for (chan = chanset; chan; chan = chan->next)
  1139. if ((idx < 0) || (1)) {
  1140. struct flag_record fr = {FR_CHAN | FR_GLOBAL | FR_BOT, 0, 0, 0, 0, 0};
  1141. if (idx >= 0)
  1142. get_user_flagrec(dcc[idx].user,&fr,chan->dname);
  1143. else
  1144. fr.chan = BOT_SHARE;
  1145. // if ((fr.chan & BOT_SHARE) || (fr.bot & BOT_GLOBAL)) {
  1146. if (lfprintf(f, "$$%s invites\n", chan->dname) == EOF)
  1147. return 0;
  1148. for (ir = chan->invites; ir; ir = ir->next) {
  1149. mask = str_escape(ir->mask, ':', '\\');
  1150. if (!mask ||
  1151. lfprintf(f,"@ %s:%s%lu%s:+%lu:%lu:%s:%s\n",ir->mask,
  1152. (ir->flags & MASKREC_PERM) ? "+" : "", ir->expire,
  1153. (ir->flags & MASKREC_STICKY) ? "*" : "", ir->added,
  1154. ir->lastactive, ir->user ? ir->user : botnetnick,
  1155. ir->desc ? ir->desc : "requested") == EOF) {
  1156. if (mask)
  1157. nfree(mask);
  1158. return 0;
  1159. }
  1160. nfree(mask);
  1161. }
  1162. //}
  1163. }
  1164. return 1;
  1165. }
  1166. #endif
  1167. /* Write the channels to the userfile
  1168. */
  1169. static int write_chans(FILE *f, int idx)
  1170. {
  1171. char w[1024], w2[1024], name[163];
  1172. char udefs[2048] = "", sadd[5], buf[2048];
  1173. /* Chanchar template
  1174. *char temp[121];
  1175. */
  1176. struct chanset_t *chan;
  1177. struct udef_struct *ul;
  1178. putlog(LOG_DEBUG, "*", "Writing channels..");
  1179. if (lfprintf(f, CHANS_NAME " - -\n") == EOF) /* Daemus */
  1180. return 0;
  1181. for (chan = chanset; chan; chan = chan->next) {
  1182. if ((idx < 0) || (1)) {
  1183. struct flag_record fr = {FR_CHAN | FR_GLOBAL | FR_BOT, 0, 0, 0, 0, 0};
  1184. if (idx >= 0)
  1185. get_user_flagrec(dcc[idx].user,&fr,chan->dname);
  1186. else
  1187. fr.chan = BOT_SHARE;
  1188. // if ((fr.chan & BOT_SHARE) || (fr.bot & BOT_GLOBAL)) {
  1189. putlog(LOG_DEBUG, "*", "writing channel %s to userfile..", chan->dname);
  1190. memset(udefs,'\0',2048);
  1191. convert_element(chan->dname, name);
  1192. get_mode_protect(chan, w);
  1193. convert_element(w, w2);
  1194. /* Chanchar template
  1195. * convert_element(chan->temp, temp);
  1196. */
  1197. for (ul = udef; ul; ul = ul->next) { //put the udefs into one string
  1198. memset(buf,'\0',2048);
  1199. if (ul->defined && ul->name) {
  1200. if (ul->type == UDEF_FLAG)
  1201. sprintf(buf, "%c%s%s ", getudef(ul->values, chan->dname) ? '+' : '-', "udef-flag-", ul->name);
  1202. else if (ul->type == UDEF_INT)
  1203. sprintf(buf, "%s%s %d ", "udef-int-", ul->name, getudef(ul->values, chan->dname));
  1204. else
  1205. debug1("UDEF-ERROR: unknown type %d", ul->type);
  1206. strcat(udefs,buf);
  1207. }
  1208. }
  1209. //insert the ending character into a string
  1210. sprintf(sadd, "}\n");
  1211. if (lfprintf(f, "+ channel %s %s%schanmode %s idle-kick %d limit %d stopnethack-mode %d \
  1212. revenge-mode %d \
  1213. flood-chan %d:%d flood-ctcp %d:%d flood-join %d:%d \
  1214. flood-kick %d:%d flood-deop %d:%d flood-nick %d:%d \
  1215. ban-time %d \
  1216. exempt-time %d invite-time %d \
  1217. %cenforcebans %cdynamicbans %cuserbans %cbitch \
  1218. %cprotectops %cdontkickops \
  1219. %crevenge %crevengebot %cprivate \
  1220. %ccycle %cinactive \
  1221. %cdynamicexempts %cuserexempts %cdynamicinvites %cuserinvites \
  1222. %cnodesynch \
  1223. %cclosed %ctake %cmanop %cvoice %cfastop %s %s",
  1224. "add",
  1225. name,
  1226. " { ",
  1227. w2,
  1228. /* Chanchar template
  1229. * temp,
  1230. * also include temp %s in dprintf.
  1231. */
  1232. chan->idle_kick, /* idle-kick 0 is same as dont-idle-kick (lcode)*/
  1233. chan->limitraise,
  1234. chan->stopnethack_mode,
  1235. chan->revenge_mode,
  1236. chan->flood_pub_thr, chan->flood_pub_time,
  1237. chan->flood_ctcp_thr, chan->flood_ctcp_time,
  1238. chan->flood_join_thr, chan->flood_join_time,
  1239. chan->flood_kick_thr, chan->flood_kick_time,
  1240. chan->flood_deop_thr, chan->flood_deop_time,
  1241. chan->flood_nick_thr, chan->flood_nick_time,
  1242. /* Chanint template
  1243. * chan->temp,
  1244. * also include temp %d in dprintf
  1245. */
  1246. chan->ban_time,
  1247. #ifdef S_IRCNET
  1248. chan->exempt_time,
  1249. chan->invite_time,
  1250. #endif
  1251. PLSMNS(channel_enforcebans(chan)),
  1252. PLSMNS(channel_dynamicbans(chan)),
  1253. PLSMNS(!channel_nouserbans(chan)),
  1254. PLSMNS(channel_bitch(chan)),
  1255. PLSMNS(channel_protectops(chan)),
  1256. PLSMNS(channel_dontkickops(chan)),
  1257. PLSMNS(channel_revenge(chan)),
  1258. PLSMNS(channel_revengebot(chan)),
  1259. PLSMNS(channel_private(chan)),
  1260. PLSMNS(channel_cycle(chan)),
  1261. PLSMNS(channel_inactive(chan)),
  1262. #ifdef S_IRCNET
  1263. PLSMNS(channel_dynamicexempts(chan)),
  1264. PLSMNS(!channel_nouserexempts(chan)),
  1265. PLSMNS(channel_dynamicinvites(chan)),
  1266. PLSMNS(!channel_nouserinvites(chan)),
  1267. #endif
  1268. PLSMNS(channel_nodesynch(chan)),
  1269. PLSMNS(channel_closed(chan)),
  1270. PLSMNS(channel_take(chan)),
  1271. PLSMNS(channel_manop(chan)),
  1272. PLSMNS(channel_voice(chan)),
  1273. PLSMNS(channel_fastop(chan)),
  1274. /* Chanflag template
  1275. * also include a %ctemp above.
  1276. * PLSMNS(channel_temp(chan)),
  1277. */
  1278. udefs,
  1279. sadd) == EOF)
  1280. return 0;
  1281. // }
  1282. }
  1283. }
  1284. return 1;
  1285. }
  1286. static void channels_writeuserfile(void)
  1287. {
  1288. #ifdef HUB
  1289. char s[1024];
  1290. FILE *f;
  1291. int ret = 0;
  1292. putlog(LOG_DEBUG, "@", "Writing channel/ban/exempt/invite entries.");
  1293. simple_sprintf(s, "%s~new", userfile);
  1294. f = fopen(s, "a");
  1295. if (f) {
  1296. //write channels to the userfile
  1297. ret = write_chans(f, -1);
  1298. ret += write_config(f, -1);
  1299. ret += write_bans(f, -1);
  1300. #ifdef S_IRCNET
  1301. ret += write_exempts(f, -1);
  1302. ret += write_invites(f, -1);
  1303. #endif
  1304. fclose(f);
  1305. }
  1306. #ifdef S_IRCNET
  1307. if (ret < 5)
  1308. #else
  1309. if (ret < 3)
  1310. #endif
  1311. putlog(LOG_MISC, "*", USERF_ERRWRITE);
  1312. //old .chan write_channels();
  1313. #endif
  1314. }
  1315. /* Expire mask originally set by `who' on `chan'?
  1316. *
  1317. * We might not want to expire masks in all cases, as other bots
  1318. * often tend to immediately reset masks they've listed in their
  1319. * internal ban list, making it quite senseless for us to remove
  1320. * them in the first place.
  1321. *
  1322. * Returns 1 if a mask on `chan' by `who' may be expired and 0 if
  1323. * not.
  1324. */
  1325. static int expired_mask(struct chanset_t *chan, char *who)
  1326. {
  1327. memberlist *m, *m2;
  1328. char buf[UHOSTLEN], *snick, *sfrom;
  1329. struct userrec *u;
  1330. /* Always expire masks, regardless of who set it? */
  1331. if (force_expire)
  1332. return 1;
  1333. strcpy(buf, who);
  1334. sfrom = buf;
  1335. snick = splitnick(&sfrom);
  1336. if (!snick[0])
  1337. return 1;
  1338. m = ismember(chan, snick);
  1339. if (!m)
  1340. for (m2 = chan->channel.member; m2 && m2->nick[0]; m2 = m2->next)
  1341. if (!egg_strcasecmp(sfrom, m2->userhost)) {
  1342. m = m2;
  1343. break;
  1344. }
  1345. if (!m || !chan_hasop(m) || !rfc_casecmp(m->nick, botname))
  1346. return 1;
  1347. /* At this point we know the person/bot who set the mask is currently
  1348. * present in the channel and has op.
  1349. */
  1350. if (m->user)
  1351. u = m->user;
  1352. else {
  1353. simple_sprintf(buf, "%s!%s", m->nick, m->userhost);
  1354. u = get_user_by_host(buf);
  1355. }
  1356. /* Do not expire masks set by bots. */
  1357. if (u && u->flags & USER_BOT)
  1358. return 0;
  1359. else
  1360. return 1;
  1361. }
  1362. #ifdef LEAF
  1363. int checklimit = 1;
  1364. void check_limitraise() {
  1365. int i=0;
  1366. struct chanset_t * chan;
  1367. struct userrec *u;
  1368. u=get_user_by_handle(userlist, botnetnick);
  1369. for (chan=chanset;chan;chan=chan->next,i++) {
  1370. if (i % 2 == checklimit) {
  1371. if (chan->limitraise) {
  1372. get_user_flagrec(u, &user, chan->dname);
  1373. if (glob_dolimit(user) || (chan_dolimit(user)))
  1374. raise_limit(chan);
  1375. }
  1376. }
  1377. }
  1378. if (checklimit)
  1379. checklimit=0;
  1380. else
  1381. checklimit=1;
  1382. }
  1383. #endif
  1384. /* Check for expired timed-bans.
  1385. */
  1386. static void check_expired_bans(void)
  1387. {
  1388. maskrec *u, *u2;
  1389. struct chanset_t *chan;
  1390. masklist *b;
  1391. for (u = global_bans; u; u = u2) {
  1392. u2 = u->next;
  1393. if (!(u->flags & MASKREC_PERM) && (now >= u->expire)) {
  1394. putlog(LOG_MISC, "*", "%s %s (%s)", BANS_NOLONGER,
  1395. u->mask, MISC_EXPIRED);
  1396. for (chan = chanset; chan; chan = chan->next)
  1397. for (b = chan->channel.ban; b->mask[0]; b = b->next)
  1398. if (!rfc_casecmp(b->mask, u->mask) &&
  1399. expired_mask(chan, b->who) && b->timer != now) {
  1400. add_mode(chan, '-', 'b', u->mask);
  1401. b->timer = now;
  1402. }
  1403. u_delban(NULL, u->mask, 1);
  1404. }
  1405. }
  1406. /* Check for specific channel-domain bans expiring */
  1407. for (chan = chanset; chan; chan = chan->next) {
  1408. for (u = chan->bans; u; u = u2) {
  1409. u2 = u->next;
  1410. if (!(u->flags & MASKREC_PERM) && (now >= u->expire)) {
  1411. putlog(LOG_MISC, "*", "%s %s %s %s (%s)", BANS_NOLONGER,
  1412. u->mask, MISC_ONLOCALE, chan->dname, MISC_EXPIRED);
  1413. for (b = chan->channel.ban; b->mask[0]; b = b->next)
  1414. if (!rfc_casecmp(b->mask, u->mask) &&
  1415. expired_mask(chan, b->who) && b->timer != now) {
  1416. add_mode(chan, '-', 'b', u->mask);
  1417. b->timer = now;
  1418. }
  1419. u_delban(chan, u->mask, 1);
  1420. }
  1421. }
  1422. }
  1423. }
  1424. #ifdef S_IRCNET
  1425. /* Check for expired timed-exemptions
  1426. */
  1427. static void check_expired_exempts(void)
  1428. {
  1429. maskrec *u, *u2;
  1430. struct chanset_t *chan;
  1431. masklist *b, *e;
  1432. int match;
  1433. if (!use_exempts)
  1434. return;
  1435. for (u = global_exempts; u; u = u2) {
  1436. u2 = u->next;
  1437. if (!(u->flags & MASKREC_PERM) && (now >= u->expire)) {
  1438. putlog(LOG_MISC, "*", "%s %s (%s)", EXEMPTS_NOLONGER,
  1439. u->mask, MISC_EXPIRED);
  1440. for (chan = chanset; chan; chan = chan->next) {
  1441. match = 0;
  1442. b = chan->channel.ban;
  1443. while (b->mask[0] && !match) {
  1444. if (wild_match(b->mask, u->mask) ||
  1445. wild_match(u->mask, b->mask))
  1446. match = 1;
  1447. else
  1448. b = b->next;
  1449. }
  1450. if (match)
  1451. putlog(LOG_MISC, chan->dname,
  1452. "Exempt not expired on channel %s. Ban still set!",
  1453. chan->dname);
  1454. else
  1455. for (e = chan->channel.exempt; e->mask[0]; e = e->next)
  1456. if (!rfc_casecmp(e->mask, u->mask) &&
  1457. expired_mask(chan, e->who) && e->timer != now) {
  1458. add_mode(chan, '-', 'e', u->mask);
  1459. e->timer = now;
  1460. }
  1461. }
  1462. u_delexempt(NULL, u->mask,1);
  1463. }
  1464. }
  1465. /* Check for specific channel-domain exempts expiring */
  1466. for (chan = chanset; chan; chan = chan->next) {
  1467. for (u = chan->exempts; u; u = u2) {
  1468. u2 = u->next;
  1469. if (!(u->flags & MASKREC_PERM) && (now >= u->expire)) {
  1470. match=0;
  1471. b = chan->channel.ban;
  1472. while (b->mask[0] && !match) {
  1473. if (wild_match(b->mask, u->mask) ||
  1474. wild_match(u->mask, b->mask))
  1475. match=1;
  1476. else
  1477. b = b->next;
  1478. }
  1479. if (match)
  1480. putlog(LOG_MISC, chan->dname,
  1481. "Exempt not expired on channel %s. Ban still set!",
  1482. chan->dname);
  1483. else {
  1484. putlog(LOG_MISC, "*", "%s %s %s %s (%s)", EXEMPTS_NOLONGER,
  1485. u->mask, MISC_ONLOCALE, chan->dname, MISC_EXPIRED);
  1486. for (e = chan->channel.exempt; e->mask[0]; e = e->next)
  1487. if (!rfc_casecmp(e->mask, u->mask) &&
  1488. expired_mask(chan, e->who) && e->timer != now) {
  1489. add_mode(chan, '-', 'e', u->mask);
  1490. e->timer = now;
  1491. }
  1492. u_delexempt(chan, u->mask, 1);
  1493. }
  1494. }
  1495. }
  1496. }
  1497. }
  1498. /* Check for expired timed-invites.
  1499. */
  1500. static void check_expired_invites(void)
  1501. {
  1502. maskrec *u, *u2;
  1503. struct chanset_t *chan;
  1504. masklist *b;
  1505. if (!use_invites)
  1506. return;
  1507. for (u = global_invites; u; u = u2) {
  1508. u2 = u->next;
  1509. if (!(u->flags & MASKREC_PERM) && (now >= u->expire)) {
  1510. putlog(LOG_MISC, "*", "%s %s (%s)", INVITES_NOLONGER,
  1511. u->mask, MISC_EXPIRED);
  1512. for (chan = chanset; chan; chan = chan->next)
  1513. if (!(chan->channel.mode & CHANINV))
  1514. for (b = chan->channel.invite; b->mask[0]; b = b->next)
  1515. if (!rfc_casecmp(b->mask, u->mask) &&
  1516. expired_mask(chan, b->who) && b->timer != now) {
  1517. add_mode(chan, '-', 'I', u->mask);
  1518. b->timer = now;
  1519. }
  1520. u_delinvite(NULL, u->mask,1);
  1521. }
  1522. }
  1523. /* Check for specific channel-domain invites expiring */
  1524. for (chan = chanset; chan; chan = chan->next) {
  1525. for (u = chan->invites; u; u = u2) {
  1526. u2 = u->next;
  1527. if (!(u->flags & MASKREC_PERM) && (now >= u->expire)) {
  1528. putlog(LOG_MISC, "*", "%s %s %s %s (%s)", INVITES_NOLONGER,
  1529. u->mask, MISC_ONLOCALE, chan->dname, MISC_EXPIRED);
  1530. if (!(chan->channel.mode & CHANINV))
  1531. for (b = chan->channel.invite; b->mask[0]; b = b->next)
  1532. if (!rfc_casecmp(b->mask, u->mask) &&
  1533. expired_mask(chan, b->who) && b->timer != now) {
  1534. add_mode(chan, '-', 'I', u->mask);
  1535. b->timer = now;
  1536. }
  1537. u_delinvite(chan, u->mask, 1);
  1538. }
  1539. }
  1540. }
  1541. }
  1542. #endif