chanprog.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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. /* char buf[2048] = ""; */
  476. struct bot_addr *bi = NULL;
  477. struct utsname un;
  478. admin[0] = 0;
  479. /* cache our ip on load instead of every 30 seconds */
  480. cache_my_ip();
  481. sdprintf("ip4: %s", myipstr(4));
  482. sdprintf("ip6: %s", myipstr(6));
  483. conmask = 0;
  484. if (!conf.bot->nick)
  485. fatal("I don't have a nickname!!\n", 0);
  486. sdprintf("I am: %s", conf.bot->nick);
  487. #ifdef HUB
  488. loading = 1;
  489. checkchans(0);
  490. readuserfile(userfile, &userlist);
  491. checkchans(1);
  492. loading = 0;
  493. #endif /* HUB */
  494. load_internal_users();
  495. if (!(conf.bot->u = get_user_by_handle(userlist, conf.bot->nick))) {
  496. /* I need to be on the userlist... doh. */
  497. userlist = adduser(userlist, conf.bot->nick, "none", "-", USER_OP, 1);
  498. conf.bot->u = get_user_by_handle(userlist, conf.bot->nick);
  499. bi = (struct bot_addr *) my_calloc(1, sizeof(struct bot_addr));
  500. if (conf.bot->net.ip)
  501. bi->address = strdup(conf.bot->net.ip);
  502. /* bi->telnet_port = atoi(buf) ? atoi(buf) : 3333; */
  503. bi->telnet_port = bi->relay_port = 3333;
  504. #ifdef HUB
  505. bi->hublevel = 99;
  506. #else /* !HUB */
  507. bi->hublevel = 0;
  508. #endif /* HUB */
  509. bi->uplink = (char *) my_calloc(1, 1);
  510. set_user(&USERENTRY_BOTADDR, conf.bot->u, bi);
  511. } else {
  512. bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, conf.bot->u);
  513. }
  514. bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, get_user_by_handle(userlist, conf.bot->nick));
  515. if (!bi)
  516. fatal("I'm added to userlist but without a bot record!", 0);
  517. if (bi->telnet_port != 3333) {
  518. #ifdef HUB
  519. listen_all(bi->telnet_port, 0);
  520. my_port = bi->telnet_port;
  521. #endif /* HUB */
  522. }
  523. /* set our shell info */
  524. uname(&un);
  525. set_user(&USERENTRY_OS, conf.bot->u, un.sysname);
  526. set_user(&USERENTRY_USERNAME, conf.bot->u, conf.username);
  527. set_user(&USERENTRY_NODENAME, conf.bot->u, un.nodename);
  528. trigger_cfg_changed();
  529. /* We should be safe now */
  530. reaffirm_owners();
  531. }
  532. #ifdef HUB
  533. /* Reload the user file from disk
  534. */
  535. void reload()
  536. {
  537. FILE *f = fopen(userfile, "r");
  538. if (f == NULL) {
  539. putlog(LOG_MISC, "*", MISC_CANTRELOADUSER);
  540. return;
  541. }
  542. fclose(f);
  543. noshare = 1;
  544. clear_userlist(userlist);
  545. noshare = 0;
  546. userlist = NULL;
  547. loading = 1;
  548. checkchans(0);
  549. if (!readuserfile(userfile, &userlist))
  550. fatal(MISC_MISSINGUSERF, 0);
  551. checkchans(1);
  552. loading = 0;
  553. reaffirm_owners();
  554. hook_read_userfile();
  555. }
  556. #endif /* HUB */
  557. /* Oddly enough, written by proton (Emech's coder)
  558. */
  559. int isowner(char *name)
  560. {
  561. if (!owner || !*owner)
  562. return (0);
  563. if (!name || !*name)
  564. return (0);
  565. char *pa = owner, *pb = owner;
  566. size_t nl = strlen(name), pl;
  567. while (1) {
  568. while (1) {
  569. if ((*pb == 0) || (*pb == ',') || (*pb == ' '))
  570. break;
  571. pb++;
  572. }
  573. pl = pb - pa;
  574. if (pl == nl && !egg_strncasecmp(pa, name, nl))
  575. return (1);
  576. while (1) {
  577. if ((*pb == 0) || ((*pb != ',') && (*pb != ' ')))
  578. break;
  579. pb++;
  580. }
  581. if (*pb == 0)
  582. return (0);
  583. pa = pb;
  584. }
  585. }
  586. /* this method is slow, but is only called when sharing and adding/removing chans */
  587. int
  588. botshouldjoin(struct userrec *u, struct chanset_t *chan)
  589. {
  590. /* just return 1 for now */
  591. return 1;
  592. /*
  593. char *chans = NULL;
  594. struct userrec *u = NULL;
  595. u = get_user_by_handle(userlist, bot);
  596. if (!u)
  597. return;
  598. chans = get_user(&USERENTRY_CHANS, u);
  599. */
  600. }
  601. /* future use ?
  602. void
  603. chans_addbot(const char *bot, struct chanset_t *chan)
  604. {
  605. char *chans = NULL;
  606. struct userrec *u = NULL;
  607. u = get_user_by_handle(userlist, bot);
  608. if (!u)
  609. return;
  610. chans = get_user(&USERENTRY_CHANS, u);
  611. if (!botshouldjoin(u, chan)) {
  612. size_t size;
  613. char *buf = NULL;
  614. size = strlen(chans) + strlen(chan->dname) + 2;
  615. buf = (char *) my_calloc(1, size);
  616. egg_snprintf(buf, size, "%s %s", chans, chan->dname);
  617. set_user(&USERENTRY_CHANS, u, buf);
  618. free(buf);
  619. }
  620. }
  621. void
  622. chans_delbot(const char *bot, struct chanset_t *chan)
  623. {
  624. char *chans = NULL;
  625. struct userrec *u = NULL;
  626. u = get_user_by_handle(userlist, bot);
  627. if (!u)
  628. return;
  629. if (botshouldjoin(u, chan)) {
  630. char *chans = NULL, *buf = NULL;
  631. size_t size;
  632. chans = get_user(&USERENTRY_CHANS, u);
  633. size = strlen(chans) - strlen(chan->dname) + 2;
  634. }
  635. }
  636. */
  637. int shouldjoin(struct chanset_t *chan)
  638. {
  639. if (!strncmp(conf.bot->nick, "wtest", 5) && !strcmp(chan->dname, "#wraith"))
  640. return 1;
  641. else if (!strncmp(conf.bot->nick, "wtest", 4)) /* use 5 for all */
  642. return 0;
  643. #ifdef G_BACKUP
  644. struct flag_record fr = { FR_CHAN | FR_GLOBAL, 0, 0, 0 };
  645. struct userrec *u = NULL;
  646. if (!chan || !chan->dname || !chan->dname[0])
  647. return 0;
  648. if ((u = get_user_by_handle(userlist, conf.bot->nick)))
  649. get_user_flagrec(u, &fr, chan->dname);
  650. return (!channel_inactive(chan) && (channel_backup(chan) || !glob_backupbot(fr)));
  651. #else /* !G_BACKUP */
  652. return !channel_inactive(chan);
  653. #endif /* G_BACKUP */
  654. }
  655. /* do_chanset() set (options) on (chan)
  656. * USES DO_LOCAL|DO_NET bits.
  657. */
  658. int do_chanset(char *result, struct chanset_t *chan, const char *options, int local)
  659. {
  660. int ret = OK;
  661. if (local & DO_NET) {
  662. char *buf = NULL;
  663. /* malloc(options,chan,'cset ',' ',+ 1) */
  664. if (chan)
  665. buf = (char *) my_calloc(1, strlen(options) + strlen(chan->dname) + 5 + 1 + 1);
  666. else
  667. buf = (char *) my_calloc(1, strlen(options) + 1 + 5 + 1 + 1);
  668. strcat(buf, "cset ");
  669. if (chan)
  670. strcat(buf, chan->dname);
  671. else
  672. strcat(buf, "*");
  673. strcat(buf, " ");
  674. strcat(buf, options);
  675. buf[strlen(buf)] = 0;
  676. putlog(LOG_DEBUG, "*", "sending out cset: %s", buf);
  677. putallbots(buf);
  678. free(buf);
  679. }
  680. if (local & DO_LOCAL) {
  681. struct chanset_t *ch = NULL;
  682. int all = chan ? 0 : 1;
  683. if (chan)
  684. ch = chan;
  685. else
  686. ch = chanset;
  687. while (ch) {
  688. const char **item = NULL;
  689. int items = 0;
  690. if (SplitList(result, options, &items, &item) == OK) {
  691. ret = channel_modify(result, ch, items, (char **) item);
  692. } else
  693. ret = ERROR;
  694. free(item);
  695. if (all) {
  696. if (ret == ERROR) /* just bail if there was an error, no sense in trying more */
  697. return ret;
  698. ch = ch->next;
  699. } else {
  700. ch = NULL;
  701. }
  702. }
  703. }
  704. return ret;
  705. }
  706. char *
  707. samechans(const char *nick, const char *delim)
  708. {
  709. static char ret[1024] = "";
  710. struct chanset_t *chan = NULL;
  711. ret[0] = 0; /* may be filled from last time */
  712. for (chan = chanset; chan; chan = chan->next) {
  713. if (ismember(chan, nick)) {
  714. strcat(ret, chan->dname);
  715. strcat(ret, delim);
  716. }
  717. }
  718. ret[strlen(ret) - 1] = 0;
  719. return ret;
  720. }