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