chanprog.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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. register char *p = NULL, *q = NULL;
  49. if (!s || !*s)
  50. return;
  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. sdprintf("checkchans(%d)", which);
  177. if (which == 0 || which == 2) {
  178. for (chan = chanset; chan; chan = chan->next) {
  179. if (which == 0) {
  180. chan->status |= CHAN_FLAGGED;
  181. } else if (which == 2) {
  182. chan->status &= ~CHAN_FLAGGED;
  183. }
  184. }
  185. } else if (which == 1) {
  186. for (chan = chanset; chan; chan = chan_next) {
  187. chan_next = chan->next;
  188. if (chan->status & CHAN_FLAGGED) {
  189. putlog(LOG_MISC, "*", "No longer supporting channel %s", chan->dname);
  190. remove_channel(chan);
  191. }
  192. }
  193. }
  194. }
  195. /* Dump uptime info out to dcc (guppy 9Jan99)
  196. */
  197. void tell_verbose_uptime(int idx)
  198. {
  199. char s[256] = "", s1[121] = "";
  200. time_t now2, hr, min;
  201. now2 = now - online_since;
  202. s[0] = 0;
  203. if (now2 > 86400) {
  204. /* days */
  205. sprintf(s, "%d day", (int) (now2 / 86400));
  206. if ((int) (now2 / 86400) >= 2)
  207. strcat(s, "s");
  208. strcat(s, ", ");
  209. now2 -= (((int) (now2 / 86400)) * 86400);
  210. }
  211. hr = (time_t) ((int) now2 / 3600);
  212. now2 -= (hr * 3600);
  213. min = (time_t) ((int) now2 / 60);
  214. sprintf(&s[strlen(s)], "%02d:%02d", (int) hr, (int) min);
  215. s1[0] = 0;
  216. if (backgrd)
  217. strcpy(s1, MISC_BACKGROUND);
  218. else {
  219. if (term_z)
  220. strcpy(s1, MISC_TERMMODE);
  221. else
  222. strcpy(s1, MISC_LOGMODE);
  223. }
  224. dprintf(idx, "%s %s (%s)\n", MISC_ONLINEFOR, s, s1);
  225. }
  226. /* Dump status info out to dcc
  227. */
  228. void tell_verbose_status(int idx)
  229. {
  230. char s[256] = "", s1[121] = "", s2[81] = "", *vers_t = NULL, *uni_t = NULL;
  231. int i;
  232. time_t now2, hr, min;
  233. #if HAVE_GETRUSAGE
  234. struct rusage ru;
  235. #else
  236. # if HAVE_CLOCK
  237. clock_t cl;
  238. # endif
  239. #endif /* HAVE_GETRUSAGE */
  240. struct utsname un;
  241. if (!uname(&un) < 0) {
  242. vers_t = " ";
  243. uni_t = "*unknown*";
  244. } else {
  245. vers_t = un.release;
  246. uni_t = un.sysname;
  247. }
  248. i = count_users(userlist);
  249. dprintf(idx, "I am %s, running %s: %d user%s\n", conf.bot->nick, ver, i, i == 1 ? "" : "s");
  250. if (localhub)
  251. dprintf(idx, "I am a localhub.\n");
  252. #ifdef HUB
  253. if (isupdatehub())
  254. dprintf(idx, "I am an update hub.\n");
  255. #endif /* HUB */
  256. now2 = now - online_since;
  257. s[0] = 0;
  258. if (now2 > 86400) {
  259. /* days */
  260. sprintf(s, "%d day", (int) (now2 / 86400));
  261. if ((int) (now2 / 86400) >= 2)
  262. strcat(s, "s");
  263. strcat(s, ", ");
  264. now2 -= (((int) (now2 / 86400)) * 86400);
  265. }
  266. hr = (time_t) ((int) now2 / 3600);
  267. now2 -= (hr * 3600);
  268. min = (time_t) ((int) now2 / 60);
  269. sprintf(&s[strlen(s)], "%02d:%02d", (int) hr, (int) min);
  270. s1[0] = 0;
  271. if (backgrd)
  272. strcpy(s1, MISC_BACKGROUND);
  273. else {
  274. if (term_z)
  275. strcpy(s1, MISC_TERMMODE);
  276. else
  277. strcpy(s1, MISC_LOGMODE);
  278. }
  279. #if HAVE_GETRUSAGE
  280. getrusage(RUSAGE_SELF, &ru);
  281. hr = (int) ((ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) / 60);
  282. min = (int) ((ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) - (hr * 60));
  283. sprintf(s2, "CPU %02d:%02d", (int) hr, (int) min); /* Actally min/sec */
  284. #else
  285. # if HAVE_CLOCK
  286. cl = (clock() / CLOCKS_PER_SEC);
  287. hr = (int) (cl / 60);
  288. min = (int) (cl - (hr * 60));
  289. sprintf(s2, "CPU %02d:%02d", (int) hr, (int) min); /* Actually min/sec */
  290. # else
  291. sprintf(s2, "CPU ???");
  292. # endif
  293. #endif /* HAVE_GETRUSAGE */
  294. dprintf(idx, "%s %s (%s) %s %s %4.1f%%\n", MISC_ONLINEFOR,
  295. s, s1, s2, MISC_CACHEHIT,
  296. 100.0 * ((float) cache_hit) / ((float) (cache_hit + cache_miss)));
  297. if (admin[0])
  298. dprintf(idx, "Admin: %s\n", admin);
  299. dprintf(idx, "OS: %s %s\n", uni_t, vers_t);
  300. dprintf(idx, "Running from: %s\n", binname);
  301. }
  302. /* Show all internal state variables
  303. */
  304. void tell_settings(int idx)
  305. {
  306. char s[1024] = "";
  307. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  308. dprintf(idx, "Botnet Nickname: %s\n", conf.bot->nick);
  309. if (firewall[0])
  310. dprintf(idx, "Firewall: %s, port %d\n", firewall, firewallport);
  311. #ifdef HUB
  312. dprintf(idx, "Userfile: %s \n", userfile);
  313. #endif /* HUB */
  314. dprintf(idx, "Directories:\n");
  315. dprintf(idx, " Temp : %s\n", tempdir);
  316. fr.global = default_flags;
  317. build_flags(s, &fr, NULL);
  318. dprintf(idx, "%s [%s]\n", MISC_NEWUSERFLAGS, s);
  319. #ifdef HUB
  320. if (owner[0])
  321. dprintf(idx, "%s: %s\n", MISC_PERMOWNER, owner);
  322. #endif /* HUB */
  323. dprintf(idx, "Ignores last %d mins\n", ignore_time);
  324. }
  325. void reaffirm_owners()
  326. {
  327. char *p = NULL, *q = NULL, s[121] = "";
  328. struct userrec *u = NULL;
  329. /* Make sure perm owners are +a */
  330. if (owner[0]) {
  331. q = owner;
  332. p = strchr(q, ',');
  333. while (p) {
  334. strncpyz(s, q, (p - q) + 1);
  335. rmspace(s);
  336. u = get_user_by_handle(userlist, s);
  337. if (u)
  338. u->flags = sanity_check(u->flags | USER_ADMIN, 0);
  339. q = p + 1;
  340. p = strchr(q, ',');
  341. }
  342. strcpy(s, q);
  343. rmspace(s);
  344. u = get_user_by_handle(userlist, s);
  345. if (u)
  346. u->flags = sanity_check(u->flags | USER_ADMIN, 0);
  347. }
  348. }
  349. void load_internal_users()
  350. {
  351. char *p = NULL, *ln = NULL, *hand = NULL, *ip = NULL, *port = NULL, *pass = NULL;
  352. char *hosts = NULL, host[UHOSTMAX] = "", buf[2048] = "", *attr = NULL;
  353. int i, hublevel = 0;
  354. struct bot_addr *bi = NULL;
  355. struct userrec *u = NULL;
  356. /* hubs */
  357. egg_snprintf(buf, sizeof buf, "%s", settings.hubs);
  358. p = buf;
  359. while (p) {
  360. ln = p;
  361. p = strchr(p, ',');
  362. if (p)
  363. *p++ = 0;
  364. hand = ln;
  365. ip = NULL;
  366. port = NULL;
  367. hosts = NULL;
  368. for (i = 0; ln; i++) {
  369. switch (i) {
  370. case 0:
  371. hand = ln;
  372. break;
  373. case 1:
  374. ip = ln;
  375. break;
  376. case 2:
  377. port = ln;
  378. break;
  379. case 3:
  380. hublevel++; /* We must increment this even if it is already added */
  381. if (!get_user_by_handle(userlist, hand)) {
  382. userlist = adduser(userlist, hand, "none", "-", USER_OP, 1);
  383. bi = (struct bot_addr *) calloc(1, sizeof(struct bot_addr));
  384. bi->address = strdup(ip);
  385. bi->telnet_port = atoi(port) ? atoi(port) : 0;
  386. bi->relay_port = bi->telnet_port;
  387. bi->hublevel = hublevel;
  388. #ifdef HUB
  389. if ((!bi->hublevel) && (!strcmp(hand, conf.bot->nick)))
  390. bi->hublevel = 99;
  391. #endif /* HUB */
  392. bi->uplink = (char *) calloc(1, 1);
  393. set_user(&USERENTRY_BOTADDR, get_user_by_handle(userlist, hand), bi);
  394. /* set_user(&USERENTRY_PASS, get_user_by_handle(userlist, hand), SALT2); */
  395. }
  396. default:
  397. /* ln = userids for hostlist, add them all */
  398. hosts = ln;
  399. ln = strchr(ln, ' ');
  400. if (ln)
  401. *ln++ = 0;
  402. while (hosts) {
  403. egg_snprintf(host, sizeof host, "*!%s@%s", hosts, ip);
  404. set_user(&USERENTRY_HOSTS, get_user_by_handle(userlist, hand), host);
  405. hosts = ln;
  406. if (ln)
  407. ln = strchr(ln, ' ');
  408. if (ln)
  409. *ln++ = 0;
  410. }
  411. break;
  412. }
  413. if (ln)
  414. ln = strchr(ln, ' ');
  415. if (ln) {
  416. *ln++ = 0;
  417. }
  418. }
  419. }
  420. /* perm owners */
  421. owner[0] = 0;
  422. egg_snprintf(buf, sizeof buf, "%s", settings.owners);
  423. p = buf;
  424. while (p) {
  425. ln = p;
  426. p = strchr(p, ',');
  427. if (p)
  428. *p++ = 0;
  429. hand = ln;
  430. pass = NULL;
  431. attr = NULL;
  432. hosts = NULL;
  433. for (i = 0; ln; i++) {
  434. switch (i) {
  435. case 0:
  436. hand = ln;
  437. break;
  438. case 1:
  439. pass = ln;
  440. break;
  441. case 2:
  442. hosts = ln;
  443. if (owner[0])
  444. strncat(owner, ",", 120);
  445. strncat(owner, hand, 120);
  446. if (!get_user_by_handle(userlist, hand)) {
  447. userlist = adduser(userlist, hand, "none", "-", USER_ADMIN | USER_OWNER | USER_MASTER | USER_OP | USER_PARTY | USER_HUBA | USER_CHUBA, 0);
  448. u = get_user_by_handle(userlist, hand);
  449. set_user(&USERENTRY_PASS, u, pass);
  450. while (hosts) {
  451. char x[1024] = "";
  452. ln = strchr(ln, ' ');
  453. if (ln)
  454. *ln++ = 0;
  455. sprintf(x, "-telnet!%s", hosts);
  456. set_user(&USERENTRY_HOSTS, u, x);
  457. hosts = ln;
  458. }
  459. }
  460. break;
  461. }
  462. if (ln)
  463. ln = strchr(ln, ' ');
  464. if (ln)
  465. *ln++ = 0;
  466. }
  467. }
  468. }
  469. void chanprog()
  470. {
  471. /* char buf[2048] = ""; */
  472. struct bot_addr *bi = NULL;
  473. struct utsname un;
  474. admin[0] = 0;
  475. /* cache our ip on load instead of every 30 seconds */
  476. cache_my_ip();
  477. sdprintf("ip4: %s", myipstr(4));
  478. sdprintf("ip6: %s", myipstr(6));
  479. conmask = 0;
  480. if (!conf.bot->nick)
  481. fatal("I don't have a nickname!!\n", 0);
  482. #ifdef HUB
  483. loading = 1;
  484. checkchans(0);
  485. readuserfile(userfile, &userlist);
  486. checkchans(1);
  487. loading = 0;
  488. #endif /* HUB */
  489. load_internal_users();
  490. if (!(conf.bot->u = get_user_by_handle(userlist, conf.bot->nick))) {
  491. /* I need to be on the userlist... doh. */
  492. userlist = adduser(userlist, conf.bot->nick, "none", "-", USER_OP, 1);
  493. conf.bot->u = get_user_by_handle(userlist, conf.bot->nick);
  494. bi = (struct bot_addr *) calloc(1, sizeof(struct bot_addr));
  495. if (conf.bot->ip)
  496. bi->address = strdup(conf.bot->ip);
  497. /* bi->telnet_port = atoi(buf) ? atoi(buf) : 3333; */
  498. bi->telnet_port = bi->relay_port = 3333;
  499. #ifdef HUB
  500. bi->hublevel = 99;
  501. #else /* !HUB */
  502. bi->hublevel = 0;
  503. #endif /* HUB */
  504. bi->uplink = (char *) calloc(1, 1);
  505. set_user(&USERENTRY_BOTADDR, conf.bot->u, bi);
  506. } else {
  507. bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, conf.bot->u);
  508. }
  509. bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, get_user_by_handle(userlist, conf.bot->nick));
  510. if (!bi)
  511. fatal("I'm added to userlist but without a bot record!", 0);
  512. if (bi->telnet_port != 3333) {
  513. #ifdef HUB
  514. listen_all(bi->telnet_port, 0);
  515. my_port = bi->telnet_port;
  516. #endif /* HUB */
  517. }
  518. /* set our shell info */
  519. uname(&un);
  520. set_user(&USERENTRY_OS, conf.bot->u, un.sysname);
  521. set_user(&USERENTRY_USERNAME, conf.bot->u, conf.username);
  522. set_user(&USERENTRY_NODENAME, conf.bot->u, un.nodename);
  523. trigger_cfg_changed();
  524. /* We should be safe now */
  525. reaffirm_owners();
  526. }
  527. #ifdef HUB
  528. /* Reload the user file from disk
  529. */
  530. void reload()
  531. {
  532. FILE *f = NULL;
  533. f = fopen(userfile, "r");
  534. if (f == NULL) {
  535. putlog(LOG_MISC, "*", MISC_CANTRELOADUSER);
  536. return;
  537. }
  538. fclose(f);
  539. noshare = 1;
  540. clear_userlist(userlist);
  541. noshare = 0;
  542. userlist = NULL;
  543. loading = 1;
  544. checkchans(0);
  545. if (!readuserfile(userfile, &userlist))
  546. fatal(MISC_MISSINGUSERF, 0);
  547. checkchans(1);
  548. loading = 0;
  549. reaffirm_owners();
  550. hook_read_userfile();
  551. }
  552. #endif /* HUB */
  553. /* Oddly enough, written by proton (Emech's coder)
  554. */
  555. int isowner(char *name)
  556. {
  557. char *pa = NULL, *pb = NULL;
  558. char nl, pl;
  559. if (!owner || !*owner)
  560. return (0);
  561. if (!name || !*name)
  562. return (0);
  563. nl = strlen(name);
  564. pa = owner;
  565. pb = owner;
  566. while (1) {
  567. while (1) {
  568. if ((*pb == 0) || (*pb == ',') || (*pb == ' '))
  569. break;
  570. pb++;
  571. }
  572. pl = (unsigned int) pb - (unsigned int) pa;
  573. if (pl == nl && !egg_strncasecmp(pa, name, nl))
  574. return (1);
  575. while (1) {
  576. if ((*pb == 0) || ((*pb != ',') && (*pb != ' ')))
  577. break;
  578. pb++;
  579. }
  580. if (*pb == 0)
  581. return (0);
  582. pa = pb;
  583. }
  584. }
  585. /* this method is slow, but is only called when sharing and adding/removing chans */
  586. int
  587. botshouldjoin(struct userrec *u, struct chanset_t *chan)
  588. {
  589. /* just return 1 for now */
  590. return 1;
  591. /*
  592. char *chans = NULL;
  593. struct userrec *u = NULL;
  594. u = get_user_by_handle(userlist, bot);
  595. if (!u)
  596. return;
  597. chans = get_user(&USERENTRY_CHANS, u);
  598. */
  599. }
  600. /* future use ?
  601. void
  602. chans_addbot(const char *bot, struct chanset_t *chan)
  603. {
  604. char *chans = NULL;
  605. struct userrec *u = NULL;
  606. u = get_user_by_handle(userlist, bot);
  607. if (!u)
  608. return;
  609. chans = get_user(&USERENTRY_CHANS, u);
  610. if (!botshouldjoin(u, chan)) {
  611. size_t size;
  612. char *buf = NULL;
  613. size = strlen(chans) + strlen(chan->dname) + 2;
  614. buf = (char *) calloc(1, size);
  615. egg_snprintf(buf, size, "%s %s", chans, chan->dname);
  616. set_user(&USERENTRY_CHANS, u, buf);
  617. free(buf);
  618. }
  619. }
  620. void
  621. chans_delbot(const char *bot, struct chanset_t *chan)
  622. {
  623. char *chans = NULL;
  624. struct userrec *u = NULL;
  625. u = get_user_by_handle(userlist, bot);
  626. if (!u)
  627. return;
  628. if (botshouldjoin(u, chan)) {
  629. char *chans = NULL, *buf = NULL;
  630. size_t size;
  631. chans = get_user(&USERENTRY_CHANS, u);
  632. size = strlen(chans) - strlen(chan->dname) + 2;
  633. }
  634. }
  635. */
  636. int shouldjoin(struct chanset_t *chan)
  637. {
  638. if (!strncmp(conf.bot->nick, "wtest", 5) && !strcmp(chan->dname, "#wraith"))
  639. return 1;
  640. else if (!strncmp(conf.bot->nick, "wtest", 4)) /* use 5 for all */
  641. return 0;
  642. #ifdef G_BACKUP
  643. struct flag_record fr = { FR_CHAN | FR_GLOBAL, 0, 0, 0 };
  644. struct userrec *u = NULL;
  645. if (!chan || !chan->dname || !chan->dname[0])
  646. return 0;
  647. if ((u = get_user_by_handle(userlist, conf.bot->nick)))
  648. get_user_flagrec(u, &fr, chan->dname);
  649. return (!channel_inactive(chan) && (channel_backup(chan) || !glob_backupbot(fr)));
  650. #else /* !G_BACKUP */
  651. return !channel_inactive(chan);
  652. #endif /* G_BACKUP */
  653. }
  654. /* do_chanset() set (options) on (chan)
  655. * USES DO_LOCAL|DO_NET bits.
  656. */
  657. int do_chanset(char *result, struct chanset_t *chan, const char *options, int local)
  658. {
  659. int ret = OK;
  660. if (local & DO_NET) {
  661. char *buf = NULL;
  662. /* malloc(options,chan,'cset ',' ',+ 1) */
  663. if (chan)
  664. buf = (char *) calloc(1, strlen(options) + strlen(chan->dname) + 5 + 1 + 1);
  665. else
  666. buf = (char *) calloc(1, strlen(options) + 1 + 5 + 1 + 1);
  667. strcat(buf, "cset ");
  668. if (chan)
  669. strcat(buf, chan->dname);
  670. else
  671. strcat(buf, "*");
  672. strcat(buf, " ");
  673. strcat(buf, options);
  674. buf[strlen(buf)] = 0;
  675. putlog(LOG_DEBUG, "*", "sending out cset: %s", buf);
  676. putallbots(buf);
  677. free(buf);
  678. }
  679. if (local & DO_LOCAL) {
  680. struct chanset_t *ch = NULL;
  681. int all = chan ? 0 : 1;
  682. if (chan)
  683. ch = chan;
  684. else
  685. ch = chanset;
  686. while (ch) {
  687. const char **item = NULL;
  688. int items = 0;
  689. if (SplitList(result, options, &items, &item) == OK) {
  690. ret = channel_modify(result, ch, items, (char **) item);
  691. } else
  692. ret = ERROR;
  693. free(item);
  694. if (all) {
  695. if (ret == ERROR) /* just bail if there was an error, no sense in trying more */
  696. return ret;
  697. ch = ch->next;
  698. } else {
  699. ch = NULL;
  700. }
  701. }
  702. }
  703. return ret;
  704. }
  705. char *
  706. samechans(const char *nick, const char *delim)
  707. {
  708. static char ret[1024] = "";
  709. struct chanset_t *chan = NULL;
  710. ret[0] = 0; /* may be filled from last time */
  711. for (chan = chanset; chan; chan = chan->next) {
  712. if (ismember(chan, nick)) {
  713. strcat(ret, chan->dname);
  714. strcat(ret, delim);
  715. }
  716. }
  717. ret[strlen(ret) - 1] = 0;
  718. return ret;
  719. }