chanprog.c 19 KB

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