chanprog.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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. simple_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. simple_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. simple_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 (conf.bot->hub)
  300. dprintf(idx, "Userfile: %s \n", userfile);
  301. dprintf(idx, "Directories:\n");
  302. dprintf(idx, " Temp : %s\n", tempdir);
  303. fr.global = default_flags;
  304. build_flags(s, &fr, NULL);
  305. dprintf(idx, "%s [%s]\n", MISC_NEWUSERFLAGS, s);
  306. if (conf.bot->hub && owner[0])
  307. dprintf(idx, "%s: %s\n", MISC_PERMOWNER, owner);
  308. dprintf(idx, "Ignores last %li mins\n", ignore_time);
  309. }
  310. void reaffirm_owners()
  311. {
  312. /* Make sure perm owners are +a */
  313. if (owner[0]) {
  314. char *q = owner, *p = strchr(q, ','), s[121] = "";
  315. struct userrec *u = NULL;
  316. while (p) {
  317. strlcpy(s, q, (p - q) + 1);
  318. rmspace(s);
  319. u = get_user_by_handle(userlist, s);
  320. if (u)
  321. u->flags = sanity_check(u->flags | USER_ADMIN, 0);
  322. q = p + 1;
  323. p = strchr(q, ',');
  324. }
  325. strcpy(s, q);
  326. rmspace(s);
  327. u = get_user_by_handle(userlist, s);
  328. if (u)
  329. u->flags = sanity_check(u->flags | USER_ADMIN, 0);
  330. }
  331. }
  332. void load_internal_users()
  333. {
  334. char *p = NULL, *ln = NULL, *hand = NULL, *ip = NULL, *port = NULL, *pass = NULL, *q = NULL;
  335. char *hosts = NULL, host[UHOSTMAX] = "", buf[2048] = "", *attr = NULL, tmp[51] = "";
  336. int i, hublevel = 0;
  337. struct bot_addr *bi = NULL;
  338. struct userrec *u = NULL;
  339. /* hubs */
  340. egg_snprintf(buf, sizeof buf, "%s", settings.hubs);
  341. p = buf;
  342. while (p) {
  343. ln = p;
  344. p = strchr(p, ',');
  345. if (p)
  346. *p++ = 0;
  347. hand = ln;
  348. ip = NULL;
  349. port = NULL;
  350. hosts = NULL;
  351. u = NULL;
  352. for (i = 0; ln; i++) {
  353. switch (i) {
  354. case 0:
  355. hand = ln;
  356. break;
  357. case 1:
  358. ip = ln;
  359. break;
  360. case 2:
  361. port = ln;
  362. break;
  363. case 3:
  364. hublevel++; /* We must increment this even if it is already added */
  365. if (!get_user_by_handle(userlist, hand)) {
  366. userlist = adduser(userlist, hand, "none", "-", USER_OP, 1);
  367. u = get_user_by_handle(userlist, hand);
  368. egg_snprintf(tmp, sizeof(tmp), "%li [internal]", now);
  369. set_user(&USERENTRY_ADDED, u, tmp);
  370. bi = (struct bot_addr *) my_calloc(1, sizeof(struct bot_addr));
  371. bi->address = strdup(ip);
  372. bi->telnet_port = atoi(port) ? atoi(port) : 0;
  373. bi->relay_port = bi->telnet_port;
  374. bi->hublevel = hublevel;
  375. if (conf.bot->hub && (!bi->hublevel) && (!strcmp(hand, conf.bot->nick)))
  376. bi->hublevel = 99;
  377. bi->uplink = (char *) my_calloc(1, 1);
  378. set_user(&USERENTRY_BOTADDR, u, bi);
  379. /* set_user(&USERENTRY_PASS, get_user_by_handle(userlist, hand), SALT2); */
  380. }
  381. default:
  382. /* ln = userids for hostlist, add them all */
  383. hosts = ln;
  384. ln = strchr(ln, ' ');
  385. if (ln && (ln = strchr(ln, ' ')))
  386. *ln++ = 0;
  387. if (!u)
  388. u = get_user_by_handle(userlist, hand);
  389. while (hosts) {
  390. egg_snprintf(host, sizeof host, "-telnet!%s@%s", hosts, ip);
  391. set_user(&USERENTRY_HOSTS, u, host);
  392. hosts = ln;
  393. if (ln && (ln = strchr(ln, ' ')))
  394. *ln++ = 0;
  395. }
  396. egg_snprintf(host, sizeof host, "-telnet!telnet@%s", ip);
  397. set_user(&USERENTRY_HOSTS, u, host);
  398. break;
  399. }
  400. if (ln && (ln = strchr(ln, ' ')))
  401. *ln++ = 0;
  402. }
  403. }
  404. /* perm owners */
  405. owner[0] = 0;
  406. egg_snprintf(buf, sizeof buf, "%s", settings.owners);
  407. p = buf;
  408. while (p) {
  409. ln = p;
  410. p = strchr(p, ',');
  411. if (p)
  412. *p++ = 0;
  413. hand = ln;
  414. pass = NULL;
  415. attr = NULL;
  416. hosts = NULL;
  417. for (i = 0; ln; i++) {
  418. switch (i) {
  419. case 0:
  420. hand = ln;
  421. break;
  422. case 1:
  423. pass = ln;
  424. break;
  425. case 2:
  426. hosts = ln;
  427. if (owner[0])
  428. strlcat(owner, ",", 121);
  429. strlcat(owner, hand, 121);
  430. if (!get_user_by_handle(userlist, hand)) {
  431. userlist = adduser(userlist, hand, "none", "-", USER_ADMIN | USER_OWNER | USER_MASTER | USER_OP | USER_PARTY | USER_HUBA | USER_CHUBA, 0);
  432. u = get_user_by_handle(userlist, hand);
  433. set_user(&USERENTRY_PASS, u, pass);
  434. egg_snprintf(tmp, sizeof(tmp), "%li [internal]", now);
  435. set_user(&USERENTRY_ADDED, u, tmp);
  436. while (hosts) {
  437. char x[1024] = "";
  438. if ((ln = strchr(ln, ' ')))
  439. *ln++ = 0;
  440. if ((q = strchr(hosts, '!'))) { /* skip over nick they provided ... */
  441. q++;
  442. if (*q == '*' || *q == '?') /* ... and any '*' or '?' */
  443. q++;
  444. hosts = q;
  445. }
  446. simple_sprintf(x, "-telnet!%s", hosts);
  447. set_user(&USERENTRY_HOSTS, u, x);
  448. hosts = ln;
  449. }
  450. }
  451. break;
  452. }
  453. if (ln && (ln = strchr(ln, ' ')))
  454. *ln++ = 0;
  455. }
  456. }
  457. }
  458. void chanprog()
  459. {
  460. struct utsname un;
  461. struct bot_addr *bi = NULL;
  462. /* cache our ip on load instead of every 30 seconds */
  463. cache_my_ip();
  464. sdprintf("ip4: %s", myipstr(4));
  465. sdprintf("ip6: %s", myipstr(6));
  466. sdprintf("I am: %s", conf.bot->nick);
  467. if (conf.bot->hub) {
  468. egg_snprintf(userfile, 121, "%s/.u", conf.binpath);
  469. loading = 1;
  470. checkchans(0);
  471. readuserfile(userfile, &userlist);
  472. checkchans(1);
  473. loading = 0;
  474. }
  475. load_internal_users();
  476. if (!(conf.bot->u = get_user_by_handle(userlist, conf.bot->nick))) {
  477. /* I need to be on the userlist... doh. */
  478. userlist = adduser(userlist, conf.bot->nick, "none", "-", USER_OP, 1);
  479. conf.bot->u = get_user_by_handle(userlist, conf.bot->nick);
  480. bi = (struct bot_addr *) my_calloc(1, sizeof(struct bot_addr));
  481. if (conf.bot->net.ip)
  482. bi->address = strdup(conf.bot->net.ip);
  483. bi->telnet_port = bi->relay_port = 3333;
  484. if (conf.bot->hub)
  485. bi->hublevel = 99;
  486. else
  487. bi->hublevel = 999;
  488. bi->uplink = (char *) my_calloc(1, 1);
  489. set_user(&USERENTRY_BOTADDR, conf.bot->u, bi);
  490. } else {
  491. bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, conf.bot->u);
  492. }
  493. if (!bi)
  494. fatal("I'm added to userlist but without a bot record!", 0);
  495. if (conf.bot->hub) {
  496. listen_all(bi->telnet_port, 0);
  497. my_port = bi->telnet_port;
  498. }
  499. /* set our shell info */
  500. uname(&un);
  501. set_user(&USERENTRY_OS, conf.bot->u, un.sysname);
  502. set_user(&USERENTRY_USERNAME, conf.bot->u, conf.username);
  503. set_user(&USERENTRY_NODENAME, conf.bot->u, un.nodename);
  504. trigger_cfg_changed();
  505. /* We should be safe now */
  506. reaffirm_owners();
  507. }
  508. /* Reload the user file from disk
  509. */
  510. void reload()
  511. {
  512. FILE *f = fopen(userfile, "r");
  513. if (f == NULL) {
  514. putlog(LOG_MISC, "*", MISC_CANTRELOADUSER);
  515. return;
  516. }
  517. fclose(f);
  518. noshare = 1;
  519. clear_userlist(userlist);
  520. noshare = 0;
  521. userlist = NULL;
  522. loading = 1;
  523. checkchans(0);
  524. if (!readuserfile(userfile, &userlist))
  525. fatal(MISC_MISSINGUSERF, 0);
  526. Auth::FillUsers();
  527. checkchans(1);
  528. loading = 0;
  529. reaffirm_owners();
  530. hook_read_userfile();
  531. }
  532. /* Oddly enough, written by proton (Emech's coder)
  533. */
  534. int isowner(char *name)
  535. {
  536. if (!owner || !*owner)
  537. return (0);
  538. if (!name || !*name)
  539. return (0);
  540. char *pa = owner, *pb = owner;
  541. size_t nl = strlen(name), pl;
  542. while (1) {
  543. while (1) {
  544. if ((*pb == 0) || (*pb == ',') || (*pb == ' '))
  545. break;
  546. pb++;
  547. }
  548. pl = pb - pa;
  549. if (pl == nl && !egg_strncasecmp(pa, name, nl))
  550. return (1);
  551. while (1) {
  552. if ((*pb == 0) || ((*pb != ',') && (*pb != ' ')))
  553. break;
  554. pb++;
  555. }
  556. if (*pb == 0)
  557. return (0);
  558. pa = pb;
  559. }
  560. }
  561. /* this method is slow, but is only called when sharing and adding/removing chans */
  562. int
  563. botshouldjoin(struct userrec *u, struct chanset_t *chan)
  564. {
  565. /* just return 1 for now */
  566. return 1;
  567. /*
  568. char *chans = NULL;
  569. struct userrec *u = NULL;
  570. u = get_user_by_handle(userlist, bot);
  571. if (!u)
  572. return;
  573. chans = get_user(&USERENTRY_CHANS, u);
  574. */
  575. }
  576. /* future use ?
  577. void
  578. chans_addbot(const char *bot, struct chanset_t *chan)
  579. {
  580. char *chans = NULL;
  581. struct userrec *u = NULL;
  582. u = get_user_by_handle(userlist, bot);
  583. if (!u)
  584. return;
  585. chans = get_user(&USERENTRY_CHANS, u);
  586. if (!botshouldjoin(u, chan)) {
  587. size_t size;
  588. char *buf = NULL;
  589. size = strlen(chans) + strlen(chan->dname) + 2;
  590. buf = (char *) my_calloc(1, size);
  591. egg_snprintf(buf, size, "%s %s", chans, chan->dname);
  592. set_user(&USERENTRY_CHANS, u, buf);
  593. free(buf);
  594. }
  595. }
  596. void
  597. chans_delbot(const char *bot, struct chanset_t *chan)
  598. {
  599. char *chans = NULL;
  600. struct userrec *u = NULL;
  601. u = get_user_by_handle(userlist, bot);
  602. if (!u)
  603. return;
  604. if (botshouldjoin(u, chan)) {
  605. char *chans = NULL, *buf = NULL;
  606. size_t size;
  607. chans = get_user(&USERENTRY_CHANS, u);
  608. size = strlen(chans) - strlen(chan->dname) + 2;
  609. }
  610. }
  611. */
  612. int shouldjoin(struct chanset_t *chan)
  613. {
  614. if (!strncmp(conf.bot->nick, "wtest", 5) && !strcmp(chan->dname, "#wraith"))
  615. return 1;
  616. else if (!strncmp(conf.bot->nick, "wtest", 4)) /* use 5 for all */
  617. return 0;
  618. #ifdef G_BACKUP
  619. struct flag_record fr = { FR_CHAN | FR_GLOBAL, 0, 0, 0 };
  620. struct userrec *u = NULL;
  621. if (!chan || !chan->dname || !chan->dname[0])
  622. return 0;
  623. if ((u = get_user_by_handle(userlist, conf.bot->nick)))
  624. get_user_flagrec(u, &fr, chan->dname);
  625. return (!channel_inactive(chan) && (channel_backup(chan) || !glob_backupbot(fr)));
  626. #else /* !G_BACKUP */
  627. return !channel_inactive(chan);
  628. #endif /* G_BACKUP */
  629. }
  630. /* do_chanset() set (options) on (chan)
  631. * USES DO_LOCAL|DO_NET bits.
  632. */
  633. int do_chanset(char *result, struct chanset_t *chan, const char *options, int local)
  634. {
  635. int ret = OK;
  636. if (local & DO_NET) {
  637. char *buf = NULL;
  638. /* malloc(options,chan,'cset ',' ',+ 1) */
  639. if (chan)
  640. buf = (char *) my_calloc(1, strlen(options) + strlen(chan->dname) + 5 + 1 + 1);
  641. else
  642. buf = (char *) my_calloc(1, strlen(options) + 1 + 5 + 1 + 1);
  643. strcat(buf, "cset ");
  644. if (chan)
  645. strcat(buf, chan->dname);
  646. else
  647. strcat(buf, "*");
  648. strcat(buf, " ");
  649. strcat(buf, options);
  650. buf[strlen(buf)] = 0;
  651. putlog(LOG_DEBUG, "*", "sending out cset: %s", buf);
  652. putallbots(buf);
  653. free(buf);
  654. }
  655. if (local & DO_LOCAL) {
  656. struct chanset_t *ch = NULL;
  657. int all = chan ? 0 : 1;
  658. if (chan)
  659. ch = chan;
  660. else
  661. ch = chanset;
  662. while (ch) {
  663. const char **item = NULL;
  664. int items = 0;
  665. if (SplitList(result, options, &items, &item) == OK) {
  666. ret = channel_modify(result, ch, items, (char **) item);
  667. } else
  668. ret = ERROR;
  669. free(item);
  670. if (all) {
  671. if (ret == ERROR) /* just bail if there was an error, no sense in trying more */
  672. return ret;
  673. ch = ch->next;
  674. } else {
  675. ch = NULL;
  676. }
  677. }
  678. }
  679. return ret;
  680. }
  681. char *
  682. samechans(const char *nick, const char *delim)
  683. {
  684. static char ret[1024] = "";
  685. struct chanset_t *chan = NULL;
  686. ret[0] = 0; /* may be filled from last time */
  687. for (chan = chanset; chan; chan = chan->next) {
  688. if (ismember(chan, nick)) {
  689. strcat(ret, chan->dname);
  690. strcat(ret, delim);
  691. }
  692. }
  693. ret[strlen(ret) - 1] = 0;
  694. return ret;
  695. }