chanprog.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /*
  2. * chanprog.c -- handles:
  3. * rmspace()
  4. * maintaining the server list
  5. * revenge punishment
  6. * timers, utimers
  7. * telling the current programmed settings
  8. * initializing a lot of stuff and loading the tcl scripts
  9. *
  10. */
  11. #include "common.h"
  12. #include "chanprog.h"
  13. #include "settings.h"
  14. #include "src/mod/channels.mod/channels.h"
  15. #include "src/mod/server.mod/server.h"
  16. #include "src/mod/share.mod/share.h"
  17. #include "rfc1459.h"
  18. #include "net.h"
  19. #include "misc.h"
  20. #include "users.h"
  21. #include "userrec.h"
  22. #include "main.h"
  23. #include "debug.h"
  24. #include "dccutil.h"
  25. #include "botmsg.h"
  26. #if HAVE_GETRUSAGE
  27. #include <sys/resource.h>
  28. #if HAVE_SYS_RUSAGE_H
  29. #include <sys/rusage.h>
  30. #endif
  31. #endif
  32. #include <sys/utsname.h>
  33. struct chanset_t *chanset = NULL; /* Channel list */
  34. char admin[121] = ""; /* Admin info */
  35. char origbotname[NICKLEN + 1] = "";
  36. char botname[NICKLEN + 1] = ""; /* Primary botname */
  37. port_t my_port = 0;
  38. /* Remove leading and trailing whitespaces.
  39. */
  40. void rmspace(char *s)
  41. {
  42. if (!s || !*s)
  43. return;
  44. register char *p = NULL, *q = NULL;
  45. /* Remove trailing whitespaces. */
  46. for (q = s + strlen(s) - 1; q >= s && egg_isspace(*q); q--);
  47. *(q + 1) = 0;
  48. /* Remove leading whitespaces. */
  49. for (p = s; egg_isspace(*p); p++);
  50. if (p != s)
  51. memmove(s, p, q - p + 2);
  52. }
  53. /* Returns memberfields if the nick is in the member list.
  54. */
  55. memberlist *ismember(struct chanset_t *chan, const char *nick)
  56. {
  57. register memberlist *x = NULL;
  58. if (chan && nick && nick[0])
  59. for (x = chan->channel.member; x && x->nick[0]; x = x->next)
  60. if (!rfc_casecmp(x->nick, nick))
  61. return x;
  62. return NULL;
  63. }
  64. /* Find a chanset by channel name as the server knows it (ie !ABCDEchannel)
  65. */
  66. struct chanset_t *findchan(const char *name)
  67. {
  68. register struct chanset_t *chan = NULL;
  69. for (chan = chanset; chan; chan = chan->next)
  70. if (!rfc_casecmp(chan->name, name))
  71. return chan;
  72. return NULL;
  73. }
  74. /* Find a chanset by display name (ie !channel)
  75. */
  76. struct chanset_t *findchan_by_dname(const char *name)
  77. {
  78. register struct chanset_t *chan = NULL;
  79. for (chan = chanset; chan; chan = chan->next)
  80. if (!rfc_casecmp(chan->dname, name))
  81. return chan;
  82. return NULL;
  83. }
  84. /*
  85. * "caching" functions
  86. */
  87. /* Shortcut for get_user_by_host -- might have user record in one
  88. * of the channel caches.
  89. */
  90. struct userrec *check_chanlist(const char *host)
  91. {
  92. char *nick = NULL, *uhost = NULL, buf[UHOSTLEN] = "";
  93. register memberlist *m = NULL;
  94. register struct chanset_t *chan = NULL;
  95. strlcpy(buf, host, sizeof buf);
  96. uhost = buf;
  97. nick = splitnick(&uhost);
  98. for (chan = chanset; chan; chan = chan->next)
  99. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  100. if (!rfc_casecmp(nick, m->nick) && !egg_strcasecmp(uhost, m->userhost))
  101. return m->user;
  102. return NULL;
  103. }
  104. /* Shortcut for get_user_by_handle -- might have user record in channels
  105. */
  106. struct userrec *check_chanlist_hand(const char *hand)
  107. {
  108. register struct chanset_t *chan = NULL;
  109. register memberlist *m = NULL;
  110. for (chan = chanset; chan; chan = chan->next)
  111. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  112. if (m->user && !egg_strcasecmp(m->user->handle, hand))
  113. return m->user;
  114. return NULL;
  115. }
  116. /* Clear the user pointers in the chanlists.
  117. *
  118. * Necessary when a hostmask is added/removed, a user is added or a new
  119. * userfile is loaded.
  120. */
  121. void clear_chanlist(void)
  122. {
  123. register memberlist *m = NULL;
  124. register struct chanset_t *chan = NULL;
  125. for (chan = chanset; chan; chan = chan->next)
  126. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  127. m->user = NULL;
  128. m->tried_getuser = 0;
  129. }
  130. }
  131. /* Clear the user pointer of a specific nick in the chanlists.
  132. *
  133. * Necessary when a hostmask is added/removed, a nick changes, etc.
  134. * Does not completely invalidate the channel cache like clear_chanlist().
  135. */
  136. void clear_chanlist_member(const char *nick)
  137. {
  138. register memberlist *m = NULL;
  139. register struct chanset_t *chan = NULL;
  140. for (chan = chanset; chan; chan = chan->next)
  141. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  142. if (!rfc_casecmp(m->nick, nick)) {
  143. m->user = NULL;
  144. m->tried_getuser = 0;
  145. break;
  146. }
  147. }
  148. /* If this user@host is in a channel, set it (it was null)
  149. */
  150. void set_chanlist(const char *host, struct userrec *rec)
  151. {
  152. char *nick = NULL, *uhost = NULL, buf[UHOSTLEN] = "";
  153. register memberlist *m = NULL;
  154. register struct chanset_t *chan = NULL;
  155. strlcpy(buf, host, sizeof buf);
  156. uhost = buf;
  157. nick = splitnick(&uhost);
  158. for (chan = chanset; chan; chan = chan->next)
  159. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  160. if (!rfc_casecmp(nick, m->nick) && !egg_strcasecmp(uhost, m->userhost))
  161. m->user = rec;
  162. }
  163. /* 0 marks all channels
  164. * 1 removes marked channels
  165. * 2 unmarks all channels
  166. */
  167. void checkchans(int which)
  168. {
  169. struct chanset_t *chan = NULL, *chan_next = NULL;
  170. if (which == 0 || which == 2) {
  171. for (chan = chanset; chan; chan = chan->next) {
  172. if (which == 0) {
  173. chan->status |= CHAN_FLAGGED;
  174. } else if (which == 2) {
  175. chan->status &= ~CHAN_FLAGGED;
  176. }
  177. }
  178. } else if (which == 1) {
  179. for (chan = chanset; chan; chan = chan_next) {
  180. chan_next = chan->next;
  181. if (chan->status & CHAN_FLAGGED) {
  182. putlog(LOG_MISC, "*", "No longer supporting channel %s", chan->dname);
  183. remove_channel(chan);
  184. }
  185. }
  186. }
  187. }
  188. /* Dump uptime info out to dcc (guppy 9Jan99)
  189. */
  190. void tell_verbose_uptime(int idx)
  191. {
  192. char s[256] = "", s1[121] = "";
  193. time_t now2, hr, min;
  194. now2 = now - online_since;
  195. if (now2 > 86400) {
  196. /* days */
  197. sprintf(s, "%d day", (int) (now2 / 86400));
  198. if ((int) (now2 / 86400) >= 2)
  199. strcat(s, "s");
  200. strcat(s, ", ");
  201. now2 -= (((int) (now2 / 86400)) * 86400);
  202. }
  203. hr = (time_t) ((int) now2 / 3600);
  204. now2 -= (hr * 3600);
  205. min = (time_t) ((int) now2 / 60);
  206. sprintf(&s[strlen(s)], "%02d:%02d", (int) hr, (int) min);
  207. s1[0] = 0;
  208. if (backgrd)
  209. strcpy(s1, MISC_BACKGROUND);
  210. else {
  211. if (term_z)
  212. strcpy(s1, MISC_TERMMODE);
  213. else
  214. strcpy(s1, MISC_LOGMODE);
  215. }
  216. dprintf(idx, "%s %s (%s)\n", MISC_ONLINEFOR, s, s1);
  217. }
  218. /* Dump status info out to dcc
  219. */
  220. void tell_verbose_status(int idx)
  221. {
  222. char s[256] = "", s1[121] = "", s2[81] = "", *vers_t = NULL, *uni_t = NULL;
  223. int i;
  224. time_t now2, hr, min;
  225. #if HAVE_GETRUSAGE
  226. struct rusage ru;
  227. #else
  228. # if HAVE_CLOCK
  229. clock_t cl;
  230. # endif
  231. #endif /* HAVE_GETRUSAGE */
  232. struct utsname un;
  233. if (!uname(&un) < 0) {
  234. vers_t = " ";
  235. uni_t = "*unknown*";
  236. } else {
  237. vers_t = un.release;
  238. uni_t = un.sysname;
  239. }
  240. i = count_users(userlist);
  241. dprintf(idx, "I am %s, running %s: %d user%s\n", conf.bot->nick, ver, i, i == 1 ? "" : "s");
  242. if (conf.bot->localhub)
  243. dprintf(idx, "I am a localhub.\n");
  244. if (conf.bot->hub && isupdatehub())
  245. dprintf(idx, "I am an update hub.\n");
  246. now2 = now - online_since;
  247. s[0] = 0;
  248. if (now2 > 86400) {
  249. /* days */
  250. sprintf(s, "%d day", (int) (now2 / 86400));
  251. if ((int) (now2 / 86400) >= 2)
  252. strcat(s, "s");
  253. strcat(s, ", ");
  254. now2 -= (((int) (now2 / 86400)) * 86400);
  255. }
  256. hr = (time_t) ((int) now2 / 3600);
  257. now2 -= (hr * 3600);
  258. min = (time_t) ((int) now2 / 60);
  259. sprintf(&s[strlen(s)], "%02d:%02d", (int) hr, (int) min);
  260. s1[0] = 0;
  261. if (backgrd)
  262. strcpy(s1, MISC_BACKGROUND);
  263. else {
  264. if (term_z)
  265. strcpy(s1, MISC_TERMMODE);
  266. else
  267. strcpy(s1, MISC_LOGMODE);
  268. }
  269. #if HAVE_GETRUSAGE
  270. getrusage(RUSAGE_SELF, &ru);
  271. hr = (int) ((ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) / 60);
  272. min = (int) ((ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) - (hr * 60));
  273. sprintf(s2, "CPU %02d:%02d", (int) hr, (int) min); /* Actally min/sec */
  274. #else
  275. # if HAVE_CLOCK
  276. cl = (clock() / CLOCKS_PER_SEC);
  277. hr = (int) (cl / 60);
  278. min = (int) (cl - (hr * 60));
  279. sprintf(s2, "CPU %02d:%02d", (int) hr, (int) min); /* Actually min/sec */
  280. # else
  281. sprintf(s2, "CPU ???");
  282. # endif
  283. #endif /* HAVE_GETRUSAGE */
  284. dprintf(idx, "%s %s (%s) %s %s %4.1f%%\n", MISC_ONLINEFOR,
  285. s, s1, s2, MISC_CACHEHIT,
  286. 100.0 * ((float) cache_hit) / ((float) (cache_hit + cache_miss)));
  287. if (admin[0])
  288. dprintf(idx, "Admin: %s\n", admin);
  289. dprintf(idx, "OS: %s %s\n", uni_t, vers_t);
  290. dprintf(idx, "Running from: %s\n", binname);
  291. }
  292. /* Show all internal state variables
  293. */
  294. void tell_settings(int idx)
  295. {
  296. char s[1024] = "";
  297. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  298. dprintf(idx, "Botnet Nickname: %s\n", conf.bot->nick);
  299. if (firewall[0])
  300. dprintf(idx, "Firewall: %s, port %d\n", firewall, firewallport);
  301. if (conf.bot->hub)
  302. dprintf(idx, "Userfile: %s \n", userfile);
  303. dprintf(idx, "Directories:\n");
  304. dprintf(idx, " Temp : %s\n", tempdir);
  305. fr.global = default_flags;
  306. build_flags(s, &fr, NULL);
  307. dprintf(idx, "%s [%s]\n", MISC_NEWUSERFLAGS, s);
  308. if (conf.bot->hub && owner[0])
  309. dprintf(idx, "%s: %s\n", MISC_PERMOWNER, owner);
  310. dprintf(idx, "Ignores last %li mins\n", ignore_time);
  311. }
  312. void reaffirm_owners()
  313. {
  314. /* Make sure perm owners are +a */
  315. if (owner[0]) {
  316. char *q = owner, *p = strchr(q, ','), s[121] = "";
  317. struct userrec *u = NULL;
  318. while (p) {
  319. strlcpy(s, q, (p - q) + 1);
  320. rmspace(s);
  321. u = get_user_by_handle(userlist, s);
  322. if (u)
  323. u->flags = sanity_check(u->flags | USER_ADMIN, 0);
  324. q = p + 1;
  325. p = strchr(q, ',');
  326. }
  327. strcpy(s, q);
  328. rmspace(s);
  329. u = get_user_by_handle(userlist, s);
  330. if (u)
  331. u->flags = sanity_check(u->flags | USER_ADMIN, 0);
  332. }
  333. }
  334. void load_internal_users()
  335. {
  336. char *p = NULL, *ln = NULL, *hand = NULL, *ip = NULL, *port = NULL, *pass = NULL, *q = NULL;
  337. char *hosts = NULL, host[UHOSTMAX] = "", buf[2048] = "", *attr = NULL, tmp[51] = "";
  338. int i, hublevel = 0;
  339. struct bot_addr *bi = NULL;
  340. struct userrec *u = NULL;
  341. /* hubs */
  342. egg_snprintf(buf, sizeof buf, "%s", settings.hubs);
  343. p = buf;
  344. while (p) {
  345. ln = p;
  346. p = strchr(p, ',');
  347. if (p)
  348. *p++ = 0;
  349. hand = ln;
  350. ip = NULL;
  351. port = NULL;
  352. hosts = NULL;
  353. u = NULL;
  354. for (i = 0; ln; i++) {
  355. switch (i) {
  356. case 0:
  357. hand = ln;
  358. break;
  359. case 1:
  360. ip = ln;
  361. break;
  362. case 2:
  363. port = ln;
  364. break;
  365. case 3:
  366. hublevel++; /* We must increment this even if it is already added */
  367. if (!get_user_by_handle(userlist, hand)) {
  368. userlist = adduser(userlist, hand, "none", "-", USER_OP, 1);
  369. u = get_user_by_handle(userlist, hand);
  370. egg_snprintf(tmp, sizeof(tmp), "%li [internal]", now);
  371. set_user(&USERENTRY_ADDED, u, tmp);
  372. bi = (struct bot_addr *) my_calloc(1, sizeof(struct bot_addr));
  373. bi->address = strdup(ip);
  374. bi->telnet_port = atoi(port) ? atoi(port) : 0;
  375. bi->relay_port = bi->telnet_port;
  376. bi->hublevel = hublevel;
  377. if (conf.bot->hub && (!bi->hublevel) && (!strcmp(hand, conf.bot->nick)))
  378. bi->hublevel = 99;
  379. bi->uplink = (char *) my_calloc(1, 1);
  380. set_user(&USERENTRY_BOTADDR, u, bi);
  381. /* set_user(&USERENTRY_PASS, get_user_by_handle(userlist, hand), SALT2); */
  382. }
  383. default:
  384. /* ln = userids for hostlist, add them all */
  385. hosts = ln;
  386. ln = strchr(ln, ' ');
  387. if (ln && (ln = strchr(ln, ' ')))
  388. *ln++ = 0;
  389. if (!u)
  390. u = get_user_by_handle(userlist, hand);
  391. while (hosts) {
  392. egg_snprintf(host, sizeof host, "-telnet!%s@%s", hosts, ip);
  393. set_user(&USERENTRY_HOSTS, u, host);
  394. hosts = ln;
  395. if (ln && (ln = strchr(ln, ' ')))
  396. *ln++ = 0;
  397. }
  398. egg_snprintf(host, sizeof host, "-telnet!telnet@%s", ip);
  399. set_user(&USERENTRY_HOSTS, u, host);
  400. break;
  401. }
  402. if (ln && (ln = strchr(ln, ' ')))
  403. *ln++ = 0;
  404. }
  405. }
  406. /* perm owners */
  407. owner[0] = 0;
  408. egg_snprintf(buf, sizeof buf, "%s", settings.owners);
  409. p = buf;
  410. while (p) {
  411. ln = p;
  412. p = strchr(p, ',');
  413. if (p)
  414. *p++ = 0;
  415. hand = ln;
  416. pass = NULL;
  417. attr = NULL;
  418. hosts = NULL;
  419. for (i = 0; ln; i++) {
  420. switch (i) {
  421. case 0:
  422. hand = ln;
  423. break;
  424. case 1:
  425. pass = ln;
  426. break;
  427. case 2:
  428. hosts = ln;
  429. if (owner[0])
  430. strlcat(owner, ",", 121);
  431. strlcat(owner, hand, 121);
  432. if (!get_user_by_handle(userlist, hand)) {
  433. userlist = adduser(userlist, hand, "none", "-", USER_ADMIN | USER_OWNER | USER_MASTER | USER_OP | USER_PARTY | USER_HUBA | USER_CHUBA, 0);
  434. u = get_user_by_handle(userlist, hand);
  435. set_user(&USERENTRY_PASS, u, pass);
  436. egg_snprintf(tmp, sizeof(tmp), "%li [internal]", now);
  437. set_user(&USERENTRY_ADDED, u, tmp);
  438. while (hosts) {
  439. char x[1024] = "";
  440. if ((ln = strchr(ln, ' ')))
  441. *ln++ = 0;
  442. if ((q = strchr(hosts, '!'))) { /* skip over nick they provided ... */
  443. q++;
  444. if (*q == '*' || *q == '?') /* ... and any '*' or '?' */
  445. q++;
  446. hosts = q;
  447. }
  448. sprintf(x, "-telnet!%s", hosts);
  449. set_user(&USERENTRY_HOSTS, u, x);
  450. hosts = ln;
  451. }
  452. }
  453. break;
  454. }
  455. if (ln && (ln = strchr(ln, ' ')))
  456. *ln++ = 0;
  457. }
  458. }
  459. }
  460. void chanprog()
  461. {
  462. struct utsname un;
  463. struct bot_addr *bi = NULL;
  464. /* cache our ip on load instead of every 30 seconds */
  465. cache_my_ip();
  466. sdprintf("ip4: %s", myipstr(4));
  467. sdprintf("ip6: %s", myipstr(6));
  468. sdprintf("I am: %s", conf.bot->nick);
  469. if (conf.bot->hub) {
  470. egg_snprintf(userfile, 121, "%s/.u", conf.binpath);
  471. loading = 1;
  472. checkchans(0);
  473. readuserfile(userfile, &userlist);
  474. checkchans(1);
  475. loading = 0;
  476. }
  477. load_internal_users();
  478. if (!(conf.bot->u = get_user_by_handle(userlist, conf.bot->nick))) {
  479. /* I need to be on the userlist... doh. */
  480. userlist = adduser(userlist, conf.bot->nick, "none", "-", USER_OP, 1);
  481. conf.bot->u = get_user_by_handle(userlist, conf.bot->nick);
  482. bi = (struct bot_addr *) my_calloc(1, sizeof(struct bot_addr));
  483. if (conf.bot->net.ip)
  484. bi->address = strdup(conf.bot->net.ip);
  485. bi->telnet_port = bi->relay_port = 3333;
  486. if (conf.bot->hub)
  487. bi->hublevel = 99;
  488. else
  489. bi->hublevel = 999;
  490. bi->uplink = (char *) my_calloc(1, 1);
  491. set_user(&USERENTRY_BOTADDR, conf.bot->u, bi);
  492. } else {
  493. bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, conf.bot->u);
  494. }
  495. if (!bi)
  496. fatal("I'm added to userlist but without a bot record!", 0);
  497. if (conf.bot->hub) {
  498. listen_all(bi->telnet_port, 0);
  499. my_port = bi->telnet_port;
  500. }
  501. /* set our shell info */
  502. uname(&un);
  503. set_user(&USERENTRY_OS, conf.bot->u, un.sysname);
  504. set_user(&USERENTRY_USERNAME, conf.bot->u, conf.username);
  505. set_user(&USERENTRY_NODENAME, conf.bot->u, un.nodename);
  506. trigger_cfg_changed();
  507. /* We should be safe now */
  508. reaffirm_owners();
  509. }
  510. /* Reload the user file from disk
  511. */
  512. void reload()
  513. {
  514. FILE *f = fopen(userfile, "r");
  515. if (f == NULL) {
  516. putlog(LOG_MISC, "*", MISC_CANTRELOADUSER);
  517. return;
  518. }
  519. fclose(f);
  520. noshare = 1;
  521. clear_userlist(userlist);
  522. noshare = 0;
  523. userlist = NULL;
  524. loading = 1;
  525. checkchans(0);
  526. if (!readuserfile(userfile, &userlist))
  527. fatal(MISC_MISSINGUSERF, 0);
  528. checkchans(1);
  529. loading = 0;
  530. reaffirm_owners();
  531. hook_read_userfile();
  532. }
  533. /* Oddly enough, written by proton (Emech's coder)
  534. */
  535. int isowner(char *name)
  536. {
  537. if (!owner || !*owner)
  538. return (0);
  539. if (!name || !*name)
  540. return (0);
  541. char *pa = owner, *pb = owner;
  542. size_t nl = strlen(name), pl;
  543. while (1) {
  544. while (1) {
  545. if ((*pb == 0) || (*pb == ',') || (*pb == ' '))
  546. break;
  547. pb++;
  548. }
  549. pl = pb - pa;
  550. if (pl == nl && !egg_strncasecmp(pa, name, nl))
  551. return (1);
  552. while (1) {
  553. if ((*pb == 0) || ((*pb != ',') && (*pb != ' ')))
  554. break;
  555. pb++;
  556. }
  557. if (*pb == 0)
  558. return (0);
  559. pa = pb;
  560. }
  561. }
  562. /* this method is slow, but is only called when sharing and adding/removing chans */
  563. int
  564. botshouldjoin(struct userrec *u, struct chanset_t *chan)
  565. {
  566. /* just return 1 for now */
  567. return 1;
  568. /*
  569. char *chans = NULL;
  570. struct userrec *u = NULL;
  571. u = get_user_by_handle(userlist, bot);
  572. if (!u)
  573. return;
  574. chans = get_user(&USERENTRY_CHANS, u);
  575. */
  576. }
  577. /* future use ?
  578. void
  579. chans_addbot(const char *bot, struct chanset_t *chan)
  580. {
  581. char *chans = NULL;
  582. struct userrec *u = NULL;
  583. u = get_user_by_handle(userlist, bot);
  584. if (!u)
  585. return;
  586. chans = get_user(&USERENTRY_CHANS, u);
  587. if (!botshouldjoin(u, chan)) {
  588. size_t size;
  589. char *buf = NULL;
  590. size = strlen(chans) + strlen(chan->dname) + 2;
  591. buf = (char *) my_calloc(1, size);
  592. egg_snprintf(buf, size, "%s %s", chans, chan->dname);
  593. set_user(&USERENTRY_CHANS, u, buf);
  594. free(buf);
  595. }
  596. }
  597. void
  598. chans_delbot(const char *bot, struct chanset_t *chan)
  599. {
  600. char *chans = NULL;
  601. struct userrec *u = NULL;
  602. u = get_user_by_handle(userlist, bot);
  603. if (!u)
  604. return;
  605. if (botshouldjoin(u, chan)) {
  606. char *chans = NULL, *buf = NULL;
  607. size_t size;
  608. chans = get_user(&USERENTRY_CHANS, u);
  609. size = strlen(chans) - strlen(chan->dname) + 2;
  610. }
  611. }
  612. */
  613. int shouldjoin(struct chanset_t *chan)
  614. {
  615. if (!strncmp(conf.bot->nick, "wtest", 5) && !strcmp(chan->dname, "#wraith"))
  616. return 1;
  617. else if (!strncmp(conf.bot->nick, "wtest", 4)) /* use 5 for all */
  618. return 0;
  619. #ifdef G_BACKUP
  620. struct flag_record fr = { FR_CHAN | FR_GLOBAL, 0, 0, 0 };
  621. struct userrec *u = NULL;
  622. if (!chan || !chan->dname || !chan->dname[0])
  623. return 0;
  624. if ((u = get_user_by_handle(userlist, conf.bot->nick)))
  625. get_user_flagrec(u, &fr, chan->dname);
  626. return (!channel_inactive(chan) && (channel_backup(chan) || !glob_backupbot(fr)));
  627. #else /* !G_BACKUP */
  628. return !channel_inactive(chan);
  629. #endif /* G_BACKUP */
  630. }
  631. /* do_chanset() set (options) on (chan)
  632. * USES DO_LOCAL|DO_NET bits.
  633. */
  634. int do_chanset(char *result, struct chanset_t *chan, const char *options, int local)
  635. {
  636. int ret = OK;
  637. if (local & DO_NET) {
  638. char *buf = NULL;
  639. /* malloc(options,chan,'cset ',' ',+ 1) */
  640. if (chan)
  641. buf = (char *) my_calloc(1, strlen(options) + strlen(chan->dname) + 5 + 1 + 1);
  642. else
  643. buf = (char *) my_calloc(1, strlen(options) + 1 + 5 + 1 + 1);
  644. strcat(buf, "cset ");
  645. if (chan)
  646. strcat(buf, chan->dname);
  647. else
  648. strcat(buf, "*");
  649. strcat(buf, " ");
  650. strcat(buf, options);
  651. buf[strlen(buf)] = 0;
  652. putlog(LOG_DEBUG, "*", "sending out cset: %s", buf);
  653. putallbots(buf);
  654. free(buf);
  655. }
  656. if (local & DO_LOCAL) {
  657. struct chanset_t *ch = NULL;
  658. int all = chan ? 0 : 1;
  659. if (chan)
  660. ch = chan;
  661. else
  662. ch = chanset;
  663. while (ch) {
  664. const char **item = NULL;
  665. int items = 0;
  666. if (SplitList(result, options, &items, &item) == OK) {
  667. ret = channel_modify(result, ch, items, (char **) item);
  668. } else
  669. ret = ERROR;
  670. free(item);
  671. if (all) {
  672. if (ret == ERROR) /* just bail if there was an error, no sense in trying more */
  673. return ret;
  674. ch = ch->next;
  675. } else {
  676. ch = NULL;
  677. }
  678. }
  679. }
  680. return ret;
  681. }
  682. char *
  683. samechans(const char *nick, const char *delim)
  684. {
  685. static char ret[1024] = "";
  686. struct chanset_t *chan = NULL;
  687. ret[0] = 0; /* may be filled from last time */
  688. for (chan = chanset; chan; chan = chan->next) {
  689. if (ismember(chan, nick)) {
  690. strcat(ret, chan->dname);
  691. strcat(ret, delim);
  692. }
  693. }
  694. ret[strlen(ret) - 1] = 0;
  695. return ret;
  696. }