chanprog.c 19 KB

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