chanprog.c 20 KB

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