chanprog.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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. bool is_hub(const char* nick) {
  331. char *p = settings.hubs, *p2 = NULL, hubbuf[HANDLEN + 1] ="";
  332. size_t len = 0;
  333. while (p && *p) {
  334. if ((p2 = strchr(p, ' '))) {
  335. len = p2 - p;
  336. simple_snprintf(hubbuf, len + 1, "%s", p);
  337. if (!egg_strncasecmp(nick, hubbuf, HANDLEN))
  338. return 1;
  339. }
  340. if ((p = strchr(p, ',')))
  341. p++;
  342. }
  343. return 0;
  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, 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. simple_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. if (conf.bot->hub && (!bi->hublevel) && (!egg_strcasecmp(hand, conf.bot->nick)))
  389. bi->hublevel = 99;
  390. bi->uplink = (char *) my_calloc(1, 1);
  391. set_user(&USERENTRY_BOTADDR, u, bi);
  392. /* set_user(&USERENTRY_PASS, get_user_by_handle(userlist, hand), SALT2); */
  393. }
  394. break;
  395. default:
  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. default:
  451. break;
  452. }
  453. if (ln && (ln = strchr(ln, ' ')))
  454. *ln++ = 0;
  455. }
  456. }
  457. }
  458. void add_myself_to_userlist() {
  459. struct bot_addr *bi = NULL;
  460. if (!(conf.bot->u = get_user_by_handle(userlist, conf.bot->nick))) {
  461. /* I need to be on the userlist... doh. */
  462. userlist = adduser(userlist, conf.bot->nick, "none", "-", USER_OP, 1);
  463. conf.bot->u = get_user_by_handle(userlist, conf.bot->nick);
  464. bi = (struct bot_addr *) my_calloc(1, sizeof(struct bot_addr));
  465. /* Assume hub has a record added from load_internal_users();
  466. why would it think it was a hub if it wasn't in the hub list??
  467. */
  468. if (!conf.bot->hub) {
  469. if (conf.bot->net.ip)
  470. bi->address = strdup(conf.bot->net.ip);
  471. bi->telnet_port = bi->relay_port = 3333;
  472. bi->hublevel = 999;
  473. bi->uplink = (char *) my_calloc(1, 1);
  474. set_user(&USERENTRY_BOTADDR, conf.bot->u, bi);
  475. }
  476. }
  477. }
  478. void chanprog()
  479. {
  480. struct utsname un;
  481. /* cache our ip on load instead of every 30 seconds */
  482. char *ip4 = NULL, *ip6 = NULL;
  483. if (cached_ip) {
  484. ip4 = strdup(myipstr(AF_INET));
  485. ip6 = strdup(myipstr(AF_INET6));
  486. }
  487. cache_my_ip();
  488. sdprintf("ip4: %s", myipstr(AF_INET));
  489. sdprintf("ip6: %s", myipstr(AF_INET6));
  490. /* Check if our ip changed during a rehash */
  491. if (ip4) {
  492. if (strcmp(ip4, myipstr(AF_INET)) || strcmp(ip6, myipstr(AF_INET6))) {
  493. if (tands > 0) {
  494. botnet_send_chat(-1, conf.bot->nick, "IP changed.");
  495. botnet_send_bye("IP changed.");
  496. }
  497. fatal("brb", 1);
  498. }
  499. free(ip4);
  500. free(ip6);
  501. }
  502. sdprintf("I am: %s", conf.bot->nick);
  503. if (conf.bot->hub) {
  504. simple_snprintf(userfile, 121, "%s/.u", conf.binpath);
  505. loading = 1;
  506. checkchans(0);
  507. readuserfile(userfile, &userlist);
  508. checkchans(1);
  509. var_parse_my_botset();
  510. loading = 0;
  511. }
  512. load_internal_users();
  513. add_myself_to_userlist();
  514. if (conf.bot->hub) {
  515. struct bot_addr *bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, conf.bot->u);
  516. listen_all(bi->telnet_port, 0);
  517. my_port = bi->telnet_port;
  518. }
  519. /* set our shell info */
  520. uname(&un);
  521. set_user(&USERENTRY_OS, conf.bot->u, un.sysname);
  522. set_user(&USERENTRY_USERNAME, conf.bot->u, conf.username);
  523. set_user(&USERENTRY_NODENAME, conf.bot->u, un.nodename);
  524. set_user(&USERENTRY_ARCH, conf.bot->u, un.machine);
  525. set_user(&USERENTRY_OSVER, conf.bot->u, un.release);
  526. var_parse_my_botset();
  527. /* We should be safe now */
  528. reaffirm_owners();
  529. }
  530. /* Reload the user file from disk
  531. */
  532. void reload()
  533. {
  534. FILE *f = fopen(userfile, "r");
  535. if (f == NULL) {
  536. putlog(LOG_MISC, "*", "Can't reload user file!");
  537. return;
  538. }
  539. fclose(f);
  540. noshare = 1;
  541. clear_userlist(userlist);
  542. noshare = 0;
  543. userlist = NULL;
  544. loading = 1;
  545. checkchans(0);
  546. if (!readuserfile(userfile, &userlist))
  547. fatal("User file is missing!", 0);
  548. /* ensure we did not lose our internal users */
  549. load_internal_users();
  550. /* make sure I am added and conf.bot->u is set */
  551. add_myself_to_userlist();
  552. /* Make sure no removed users/bots are still connected. */
  553. check_stale_dcc_users();
  554. for (tand_t* bot = tandbot; bot; bot = bot->next)
  555. bot->u = get_user_by_handle(userlist, bot->bot);
  556. /* I don't think these will ever be called anyway. */
  557. if (!conf.bot->hub) {
  558. Auth::FillUsers();
  559. check_hostmask();
  560. }
  561. checkchans(1);
  562. loading = 0;
  563. var_parse_my_botset();
  564. reaffirm_owners();
  565. hook_read_userfile();
  566. }
  567. /* Oddly enough, written by proton (Emech's coder)
  568. */
  569. int isowner(char *name)
  570. {
  571. if (!owner || !*owner)
  572. return (0);
  573. if (!name || !*name)
  574. return (0);
  575. char *pa = owner, *pb = owner;
  576. size_t nl = strlen(name), pl;
  577. while (1) {
  578. while (1) {
  579. if ((*pb == 0) || (*pb == ',') || (*pb == ' '))
  580. break;
  581. pb++;
  582. }
  583. pl = pb - pa;
  584. if (pl == nl && !egg_strncasecmp(pa, name, nl))
  585. return (1);
  586. while (1) {
  587. if ((*pb == 0) || ((*pb != ',') && (*pb != ' ')))
  588. break;
  589. pb++;
  590. }
  591. if (*pb == 0)
  592. return (0);
  593. pa = pb;
  594. }
  595. }
  596. /* this method is slow, but is only called when sharing and adding/removing chans */
  597. int
  598. botshouldjoin(struct userrec *u, struct chanset_t *chan)
  599. {
  600. /* just return 1 for now */
  601. return 1;
  602. /*
  603. char *chans = NULL;
  604. struct userrec *u = NULL;
  605. u = get_user_by_handle(userlist, bot);
  606. if (!u)
  607. return;
  608. chans = get_user(&USERENTRY_CHANS, u);
  609. */
  610. }
  611. /* future use ?
  612. void
  613. chans_addbot(const char *bot, struct chanset_t *chan)
  614. {
  615. char *chans = NULL;
  616. struct userrec *u = NULL;
  617. u = get_user_by_handle(userlist, bot);
  618. if (!u)
  619. return;
  620. chans = get_user(&USERENTRY_CHANS, u);
  621. if (!botshouldjoin(u, chan)) {
  622. size_t size;
  623. char *buf = NULL;
  624. size = strlen(chans) + strlen(chan->dname) + 2;
  625. buf = (char *) my_calloc(1, size);
  626. simple_snprintf(buf, size, "%s %s", chans, chan->dname);
  627. set_user(&USERENTRY_CHANS, u, buf);
  628. free(buf);
  629. }
  630. }
  631. void
  632. chans_delbot(const char *bot, struct chanset_t *chan)
  633. {
  634. char *chans = NULL;
  635. struct userrec *u = NULL;
  636. u = get_user_by_handle(userlist, bot);
  637. if (!u)
  638. return;
  639. if (botshouldjoin(u, chan)) {
  640. char *chans = NULL, *buf = NULL;
  641. size_t size;
  642. chans = get_user(&USERENTRY_CHANS, u);
  643. size = strlen(chans) - strlen(chan->dname) + 2;
  644. }
  645. }
  646. */
  647. bool bot_shouldjoin(struct userrec* u, struct flag_record* fr, struct chanset_t* chan)
  648. {
  649. /* If the bot is restarting (and hasn't finished getting the userfile for the first time) DO NOT JOIN channels - breaks +B/+backup */
  650. if (restarting || loading) return 0;
  651. if (!strncmp(u->handle, "wtest", 5)) {
  652. if (!strcmp(chan->dname, "#skynet") ||
  653. !strcmp(chan->dname, "#bryan") ||
  654. !strcmp(chan->dname, "#wraith"))
  655. return 1;
  656. else
  657. return 0;
  658. }
  659. return (!channel_inactive(chan) && (channel_backup(chan) || (!glob_backup(*fr) && !chan_backup(*fr))));
  660. }
  661. bool shouldjoin(struct chanset_t *chan)
  662. {
  663. if (conf.bot->u) {
  664. struct flag_record fr = { FR_CHAN|FR_GLOBAL|FR_BOT, 0, 0, 0 };
  665. get_user_flagrec(conf.bot->u, &fr, chan->dname, chan);
  666. return bot_shouldjoin(conf.bot->u, &fr, chan);
  667. }
  668. return 0;
  669. }
  670. /* do_chanset() set (options) on (chan)
  671. * USES DO_LOCAL|DO_NET bits.
  672. */
  673. int do_chanset(char *result, struct chanset_t *chan, const char *options, int local)
  674. {
  675. int ret = OK;
  676. if (local & DO_NET) {
  677. char *buf = NULL;
  678. /* malloc(options,chan,'cset ',' ',+ 1) */
  679. if (chan)
  680. buf = (char *) my_calloc(1, strlen(options) + strlen(chan->dname) + 5 + 1 + 1);
  681. else
  682. buf = (char *) my_calloc(1, strlen(options) + 1 + 5 + 1 + 1);
  683. strcat(buf, "cset ");
  684. if (chan)
  685. strcat(buf, chan->dname);
  686. else
  687. strcat(buf, "*");
  688. strcat(buf, " ");
  689. strcat(buf, options);
  690. buf[strlen(buf)] = 0;
  691. putlog(LOG_DEBUG, "*", "sending out cset: %s", buf);
  692. putallbots(buf);
  693. free(buf);
  694. }
  695. if (local & DO_LOCAL) {
  696. bool cmd = (local & CMD);
  697. struct chanset_t *ch = NULL;
  698. int all = chan ? 0 : 1;
  699. if (chan)
  700. ch = chan;
  701. else
  702. ch = chanset;
  703. while (ch) {
  704. const char **item = NULL;
  705. int items = 0;
  706. if (SplitList(result, options, &items, &item) == OK) {
  707. ret = channel_modify(result, ch, items, (char **) item, cmd);
  708. } else
  709. ret = ERROR;
  710. free(item);
  711. if (all) {
  712. if (ret == ERROR) /* just bail if there was an error, no sense in trying more */
  713. return ret;
  714. ch = ch->next;
  715. } else {
  716. ch = NULL;
  717. }
  718. }
  719. }
  720. return ret;
  721. }
  722. char *
  723. samechans(const char *nick, const char *delim)
  724. {
  725. static char ret[1024] = "";
  726. struct chanset_t *chan = NULL;
  727. ret[0] = 0; /* may be filled from last time */
  728. for (chan = chanset; chan; chan = chan->next) {
  729. if (ismember(chan, nick)) {
  730. strcat(ret, chan->dname);
  731. strcat(ret, delim);
  732. }
  733. }
  734. ret[strlen(ret) - 1] = 0;
  735. return ret;
  736. }