chanprog.c 22 KB

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