chanprog.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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. strlcpy(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. strlcpy(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. strlcpy(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, *q = NULL;
  348. char *hosts = NULL, host[UHOSTMAX] = "", buf[2048] = "", *attr = NULL, tmp[51] = "";
  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. u = NULL;
  365. for (i = 0; ln; i++) {
  366. switch (i) {
  367. case 0:
  368. hand = ln;
  369. break;
  370. case 1:
  371. ip = ln;
  372. break;
  373. case 2:
  374. port = ln;
  375. break;
  376. case 3:
  377. hublevel++; /* We must increment this even if it is already added */
  378. if (!get_user_by_handle(userlist, hand)) {
  379. userlist = adduser(userlist, hand, "none", "-", USER_OP, 1);
  380. u = get_user_by_handle(userlist, hand);
  381. egg_snprintf(tmp, sizeof(tmp), "%li [internal]", now);
  382. set_user(&USERENTRY_ADDED, u, tmp);
  383. bi = (struct bot_addr *) my_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 *) my_calloc(1, 1);
  393. set_user(&USERENTRY_BOTADDR, u, 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 && (ln = strchr(ln, ' ')))
  401. *ln++ = 0;
  402. if (!u)
  403. u = get_user_by_handle(userlist, hand);
  404. while (hosts) {
  405. egg_snprintf(host, sizeof host, "-telnet!%s@%s", hosts, ip);
  406. set_user(&USERENTRY_HOSTS, u, host);
  407. hosts = ln;
  408. if (ln && (ln = strchr(ln, ' ')))
  409. *ln++ = 0;
  410. }
  411. egg_snprintf(host, sizeof host, "-telnet!telnet@%s", ip);
  412. set_user(&USERENTRY_HOSTS, u, host);
  413. break;
  414. }
  415. if (ln && (ln = strchr(ln, ' ')))
  416. *ln++ = 0;
  417. }
  418. }
  419. /* perm owners */
  420. owner[0] = 0;
  421. egg_snprintf(buf, sizeof buf, "%s", settings.owners);
  422. p = buf;
  423. while (p) {
  424. ln = p;
  425. p = strchr(p, ',');
  426. if (p)
  427. *p++ = 0;
  428. hand = ln;
  429. pass = NULL;
  430. attr = NULL;
  431. hosts = NULL;
  432. for (i = 0; ln; i++) {
  433. switch (i) {
  434. case 0:
  435. hand = ln;
  436. break;
  437. case 1:
  438. pass = ln;
  439. break;
  440. case 2:
  441. hosts = ln;
  442. if (owner[0])
  443. strlcat(owner, ",", 121);
  444. strlcat(owner, hand, 121);
  445. if (!get_user_by_handle(userlist, hand)) {
  446. userlist = adduser(userlist, hand, "none", "-", USER_ADMIN | USER_OWNER | USER_MASTER | USER_OP | USER_PARTY | USER_HUBA | USER_CHUBA, 0);
  447. u = get_user_by_handle(userlist, hand);
  448. set_user(&USERENTRY_PASS, u, pass);
  449. egg_snprintf(tmp, sizeof(tmp), "%li [internal]", now);
  450. set_user(&USERENTRY_ADDED, u, tmp);
  451. while (hosts) {
  452. char x[1024] = "";
  453. if ((ln = strchr(ln, ' ')))
  454. *ln++ = 0;
  455. if ((q = strchr(hosts, '!'))) { /* skip over nick they provided ... */
  456. q++;
  457. if (*q == '*' || *q == '?') /* ... and any '*' or '?' */
  458. q++;
  459. hosts = q;
  460. }
  461. sprintf(x, "-telnet!%s", hosts);
  462. set_user(&USERENTRY_HOSTS, u, x);
  463. hosts = ln;
  464. }
  465. }
  466. break;
  467. }
  468. if (ln && (ln = strchr(ln, ' ')))
  469. *ln++ = 0;
  470. }
  471. }
  472. }
  473. void chanprog()
  474. {
  475. struct utsname un;
  476. struct bot_addr *bi = NULL;
  477. /* cache our ip on load instead of every 30 seconds */
  478. cache_my_ip();
  479. sdprintf("ip4: %s", myipstr(4));
  480. sdprintf("ip6: %s", myipstr(6));
  481. sdprintf("I am: %s", conf.bot->nick);
  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 *) my_calloc(1, sizeof(struct bot_addr));
  495. if (conf.bot->net.ip)
  496. bi->address = strdup(conf.bot->net.ip);
  497. bi->telnet_port = bi->relay_port = 3333;
  498. #ifdef HUB
  499. bi->hublevel = 99;
  500. #else /* !HUB */
  501. bi->hublevel = 999;
  502. #endif /* HUB */
  503. bi->uplink = (char *) my_calloc(1, 1);
  504. set_user(&USERENTRY_BOTADDR, conf.bot->u, bi);
  505. } else {
  506. bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, conf.bot->u);
  507. }
  508. if (!bi)
  509. fatal("I'm added to userlist but without a bot record!", 0);
  510. if (bi->telnet_port != 3333) {
  511. #ifdef HUB
  512. listen_all(bi->telnet_port, 0);
  513. my_port = bi->telnet_port;
  514. #endif /* HUB */
  515. }
  516. /* set our shell info */
  517. uname(&un);
  518. set_user(&USERENTRY_OS, conf.bot->u, un.sysname);
  519. set_user(&USERENTRY_USERNAME, conf.bot->u, conf.username);
  520. set_user(&USERENTRY_NODENAME, conf.bot->u, un.nodename);
  521. trigger_cfg_changed();
  522. /* We should be safe now */
  523. reaffirm_owners();
  524. }
  525. #ifdef HUB
  526. /* Reload the user file from disk
  527. */
  528. void reload()
  529. {
  530. FILE *f = fopen(userfile, "r");
  531. if (f == NULL) {
  532. putlog(LOG_MISC, "*", MISC_CANTRELOADUSER);
  533. return;
  534. }
  535. fclose(f);
  536. noshare = 1;
  537. clear_userlist(userlist);
  538. noshare = 0;
  539. userlist = NULL;
  540. loading = 1;
  541. checkchans(0);
  542. if (!readuserfile(userfile, &userlist))
  543. fatal(MISC_MISSINGUSERF, 0);
  544. checkchans(1);
  545. loading = 0;
  546. reaffirm_owners();
  547. hook_read_userfile();
  548. }
  549. #endif /* HUB */
  550. /* Oddly enough, written by proton (Emech's coder)
  551. */
  552. int isowner(char *name)
  553. {
  554. if (!owner || !*owner)
  555. return (0);
  556. if (!name || !*name)
  557. return (0);
  558. char *pa = owner, *pb = owner;
  559. size_t nl = strlen(name), pl;
  560. while (1) {
  561. while (1) {
  562. if ((*pb == 0) || (*pb == ',') || (*pb == ' '))
  563. break;
  564. pb++;
  565. }
  566. pl = pb - pa;
  567. if (pl == nl && !egg_strncasecmp(pa, name, nl))
  568. return (1);
  569. while (1) {
  570. if ((*pb == 0) || ((*pb != ',') && (*pb != ' ')))
  571. break;
  572. pb++;
  573. }
  574. if (*pb == 0)
  575. return (0);
  576. pa = pb;
  577. }
  578. }
  579. /* this method is slow, but is only called when sharing and adding/removing chans */
  580. int
  581. botshouldjoin(struct userrec *u, struct chanset_t *chan)
  582. {
  583. /* just return 1 for now */
  584. return 1;
  585. /*
  586. char *chans = NULL;
  587. struct userrec *u = NULL;
  588. u = get_user_by_handle(userlist, bot);
  589. if (!u)
  590. return;
  591. chans = get_user(&USERENTRY_CHANS, u);
  592. */
  593. }
  594. /* future use ?
  595. void
  596. chans_addbot(const char *bot, struct chanset_t *chan)
  597. {
  598. char *chans = NULL;
  599. struct userrec *u = NULL;
  600. u = get_user_by_handle(userlist, bot);
  601. if (!u)
  602. return;
  603. chans = get_user(&USERENTRY_CHANS, u);
  604. if (!botshouldjoin(u, chan)) {
  605. size_t size;
  606. char *buf = NULL;
  607. size = strlen(chans) + strlen(chan->dname) + 2;
  608. buf = (char *) my_calloc(1, size);
  609. egg_snprintf(buf, size, "%s %s", chans, chan->dname);
  610. set_user(&USERENTRY_CHANS, u, buf);
  611. free(buf);
  612. }
  613. }
  614. void
  615. chans_delbot(const char *bot, struct chanset_t *chan)
  616. {
  617. char *chans = NULL;
  618. struct userrec *u = NULL;
  619. u = get_user_by_handle(userlist, bot);
  620. if (!u)
  621. return;
  622. if (botshouldjoin(u, chan)) {
  623. char *chans = NULL, *buf = NULL;
  624. size_t size;
  625. chans = get_user(&USERENTRY_CHANS, u);
  626. size = strlen(chans) - strlen(chan->dname) + 2;
  627. }
  628. }
  629. */
  630. int shouldjoin(struct chanset_t *chan)
  631. {
  632. if (!strncmp(conf.bot->nick, "wtest", 5) && !strcmp(chan->dname, "#wraith"))
  633. return 1;
  634. else if (!strncmp(conf.bot->nick, "wtest", 4)) /* use 5 for all */
  635. return 0;
  636. #ifdef G_BACKUP
  637. struct flag_record fr = { FR_CHAN | FR_GLOBAL, 0, 0, 0 };
  638. struct userrec *u = NULL;
  639. if (!chan || !chan->dname || !chan->dname[0])
  640. return 0;
  641. if ((u = get_user_by_handle(userlist, conf.bot->nick)))
  642. get_user_flagrec(u, &fr, chan->dname);
  643. return (!channel_inactive(chan) && (channel_backup(chan) || !glob_backupbot(fr)));
  644. #else /* !G_BACKUP */
  645. return !channel_inactive(chan);
  646. #endif /* G_BACKUP */
  647. }
  648. /* do_chanset() set (options) on (chan)
  649. * USES DO_LOCAL|DO_NET bits.
  650. */
  651. int do_chanset(char *result, struct chanset_t *chan, const char *options, int local)
  652. {
  653. int ret = OK;
  654. if (local & DO_NET) {
  655. char *buf = NULL;
  656. /* malloc(options,chan,'cset ',' ',+ 1) */
  657. if (chan)
  658. buf = (char *) my_calloc(1, strlen(options) + strlen(chan->dname) + 5 + 1 + 1);
  659. else
  660. buf = (char *) my_calloc(1, strlen(options) + 1 + 5 + 1 + 1);
  661. strcat(buf, "cset ");
  662. if (chan)
  663. strcat(buf, chan->dname);
  664. else
  665. strcat(buf, "*");
  666. strcat(buf, " ");
  667. strcat(buf, options);
  668. buf[strlen(buf)] = 0;
  669. putlog(LOG_DEBUG, "*", "sending out cset: %s", buf);
  670. putallbots(buf);
  671. free(buf);
  672. }
  673. if (local & DO_LOCAL) {
  674. struct chanset_t *ch = NULL;
  675. int all = chan ? 0 : 1;
  676. if (chan)
  677. ch = chan;
  678. else
  679. ch = chanset;
  680. while (ch) {
  681. const char **item = NULL;
  682. int items = 0;
  683. if (SplitList(result, options, &items, &item) == OK) {
  684. ret = channel_modify(result, ch, items, (char **) item);
  685. } else
  686. ret = ERROR;
  687. free(item);
  688. if (all) {
  689. if (ret == ERROR) /* just bail if there was an error, no sense in trying more */
  690. return ret;
  691. ch = ch->next;
  692. } else {
  693. ch = NULL;
  694. }
  695. }
  696. }
  697. return ret;
  698. }
  699. char *
  700. samechans(const char *nick, const char *delim)
  701. {
  702. static char ret[1024] = "";
  703. struct chanset_t *chan = NULL;
  704. ret[0] = 0; /* may be filled from last time */
  705. for (chan = chanset; chan; chan = chan->next) {
  706. if (ismember(chan, nick)) {
  707. strcat(ret, chan->dname);
  708. strcat(ret, delim);
  709. }
  710. }
  711. ret[strlen(ret) - 1] = 0;
  712. return ret;
  713. }