chanprog.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2010 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/irc.mod/irc.h"
  34. #include "src/mod/channels.mod/channels.h"
  35. #include "src/mod/server.mod/server.h"
  36. #include "src/mod/share.mod/share.h"
  37. #include "rfc1459.h"
  38. #include "net.h"
  39. #include "misc.h"
  40. #include "users.h"
  41. #include "botnet.h"
  42. #include "userrec.h"
  43. #include "main.h"
  44. #include "debug.h"
  45. #include "dccutil.h"
  46. #include "botmsg.h"
  47. #if HAVE_GETRUSAGE
  48. #include <sys/resource.h>
  49. #if HAVE_SYS_RUSAGE_H
  50. #include <sys/rusage.h>
  51. #endif
  52. #endif
  53. #include <sys/utsname.h>
  54. #include <bdlib/src/Array.h>
  55. #include <bdlib/src/String.h>
  56. char *def_chanset = "+enforcebans +dynamicbans +userbans -bitch +cycle -inactive +userexempts -dynamicexempts +userinvites -dynamicinvites -nodesynch -closed -take -voice -private -fastop +meankicks ban-type 3 protect-backup 1 groups { main }";
  57. struct chanset_t *chanset = NULL; /* Channel list */
  58. struct chanset_t *chanset_default = NULL; /* Default channel list */
  59. char admin[121] = ""; /* Admin info */
  60. char origbotnick[HANDLEN + 1] = ""; /* from -B (placed into conf.bot->nick .. for backup when conf is cleared */
  61. char origbotname[NICKLEN] = ""; /* Nick to regain */
  62. char jupenick[NICKLEN] = "";
  63. char botname[NICKLEN] = ""; /* IRC nickname */
  64. port_t my_port = 0;
  65. int reset_chans = 0;
  66. bool cookies_disabled = 0;
  67. char s2_4[3] = "",s1_6[3] = "",s1_11[3] = "";
  68. /* Remove leading and trailing whitespaces.
  69. */
  70. void rmspace(char *s)
  71. {
  72. if (!s || !*s)
  73. return;
  74. register char *p = NULL, *q = NULL;
  75. /* Remove trailing whitespaces. */
  76. for (q = s + strlen(s) - 1; q >= s && egg_isspace(*q); q--)
  77. ;
  78. *(q + 1) = 0;
  79. /* Remove leading whitespaces. */
  80. for (p = s; egg_isspace(*p); p++)
  81. ;
  82. if (p != s)
  83. memmove(s, p, q - p + 2);
  84. }
  85. /* Returns memberfields if the nick is in the member list.
  86. */
  87. memberlist *ismember(struct chanset_t *chan, const char *nick)
  88. {
  89. register memberlist *x = NULL;
  90. if (chan && nick && nick[0])
  91. for (x = chan->channel.member; x && x->nick[0]; x = x->next)
  92. if (!rfc_casecmp(x->nick, nick))
  93. return x;
  94. return NULL;
  95. }
  96. /* Find a chanset by channel name as the server knows it (ie !ABCDEchannel)
  97. */
  98. struct chanset_t *findchan(const char *name)
  99. {
  100. register struct chanset_t *chan = NULL;
  101. for (chan = chanset; chan; chan = chan->next)
  102. if (chan->name[0] && !rfc_casecmp(chan->name, name))
  103. return chan;
  104. return NULL;
  105. }
  106. /* Find a chanset by display name (ie !channel)
  107. */
  108. struct chanset_t *findchan_by_dname(const char *name)
  109. {
  110. register struct chanset_t *chan = NULL;
  111. for (chan = chanset; chan; chan = chan->next)
  112. if (!rfc_casecmp(chan->dname, name))
  113. return chan;
  114. return NULL;
  115. }
  116. /*
  117. * "caching" functions
  118. */
  119. /* Shortcut for get_user_by_host -- might have user record in one
  120. * of the channel caches.
  121. */
  122. struct userrec *check_chanlist(const char *host)
  123. {
  124. char *nick = NULL, *uhost = NULL, buf[UHOSTLEN] = "";
  125. register memberlist *m = NULL;
  126. register struct chanset_t *chan = NULL;
  127. strlcpy(buf, host, sizeof buf);
  128. uhost = buf;
  129. nick = splitnick(&uhost);
  130. for (chan = chanset; chan; chan = chan->next)
  131. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  132. if (!rfc_casecmp(nick, m->nick) && !strcasecmp(uhost, m->userhost))
  133. return m->user;
  134. return NULL;
  135. }
  136. /* Shortcut for get_user_by_handle -- might have user record in channels
  137. */
  138. struct userrec *check_chanlist_hand(const char *hand)
  139. {
  140. register struct chanset_t *chan = NULL;
  141. register memberlist *m = NULL;
  142. for (chan = chanset; chan; chan = chan->next)
  143. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  144. if (m->user && !strcasecmp(m->user->handle, hand))
  145. return m->user;
  146. return NULL;
  147. }
  148. /* Clear the user pointers in the chanlists.
  149. *
  150. * Necessary when a hostmask is added/removed, a user is added or a new
  151. * userfile is loaded.
  152. */
  153. void clear_chanlist(void)
  154. {
  155. register memberlist *m = NULL;
  156. register struct chanset_t *chan = NULL;
  157. for (chan = chanset; chan; chan = chan->next)
  158. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  159. m->user = NULL;
  160. m->tried_getuser = 0;
  161. }
  162. }
  163. /* Clear the user pointer of a specific nick in the chanlists.
  164. *
  165. * Necessary when a hostmask is added/removed, a nick changes, etc.
  166. * Does not completely invalidate the channel cache like clear_chanlist().
  167. */
  168. void clear_chanlist_member(const char *nick)
  169. {
  170. register memberlist *m = NULL;
  171. register struct chanset_t *chan = NULL;
  172. for (chan = chanset; chan; chan = chan->next)
  173. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  174. if (!rfc_casecmp(m->nick, nick)) {
  175. m->user = NULL;
  176. m->tried_getuser = 0;
  177. break;
  178. }
  179. }
  180. /* If this user@host is in a channel, set it (it was null)
  181. */
  182. void set_chanlist(const char *host, struct userrec *rec)
  183. {
  184. char *nick = NULL, *uhost = NULL, buf[UHOSTLEN] = "";
  185. register memberlist *m = NULL;
  186. register struct chanset_t *chan = NULL;
  187. strlcpy(buf, host, sizeof buf);
  188. uhost = buf;
  189. nick = splitnick(&uhost);
  190. for (chan = chanset; chan; chan = chan->next)
  191. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  192. if (!rfc_casecmp(nick, m->nick) && !strcasecmp(uhost, m->userhost))
  193. m->user = rec;
  194. }
  195. /* 0 marks all channels
  196. * 1 removes marked channels
  197. * 2 unmarks all channels
  198. */
  199. void checkchans(int which)
  200. {
  201. struct chanset_t *chan = NULL, *chan_next = NULL;
  202. if (which == 0 || which == 2) {
  203. for (chan = chanset; chan; chan = chan->next) {
  204. if (which == 0) {
  205. chan->status |= CHAN_FLAGGED;
  206. } else if (which == 2) {
  207. chan->status &= ~CHAN_FLAGGED;
  208. }
  209. }
  210. } else if (which == 1) {
  211. for (chan = chanset; chan; chan = chan_next) {
  212. chan_next = chan->next;
  213. if (chan->status & CHAN_FLAGGED) {
  214. putlog(LOG_MISC, "*", "No longer supporting channel %s", chan->dname);
  215. remove_channel(chan);
  216. }
  217. }
  218. }
  219. }
  220. void tell_verbose_uptime(int idx)
  221. {
  222. char s[256] = "", s1[121] = "", s2[81] = "", outbuf[501] = "";
  223. time_t total, hr, min;
  224. #if HAVE_GETRUSAGE
  225. struct rusage ru;
  226. #else
  227. # if HAVE_CLOCK
  228. clock_t cl;
  229. # endif
  230. #endif /* HAVE_GETRUSAGE */
  231. daysdur(now, online_since, s, sizeof(s));
  232. if (backgrd)
  233. strlcpy(s1, "background", sizeof(s1));
  234. else {
  235. if (term_z)
  236. strlcpy(s1, "terminal mode", sizeof(s1));
  237. else
  238. strlcpy(s1, "log dump mode", sizeof(s1));
  239. }
  240. simple_snprintf(outbuf, sizeof(outbuf), "Online for %s", s);
  241. if (restart_time) {
  242. daysdur(now, restart_time, s, sizeof(s));
  243. size_t olen = strlen(outbuf);
  244. simple_snprintf(&outbuf[olen], sizeof(outbuf) - olen, " (%s %s)", restart_was_update ? "updated" : "restarted", s);
  245. }
  246. #if HAVE_GETRUSAGE
  247. getrusage(RUSAGE_SELF, &ru);
  248. total = ru.ru_utime.tv_sec + ru.ru_stime.tv_sec;
  249. hr = (int) (total / 60);
  250. min = (int) (total - (hr * 60));
  251. egg_snprintf(s2, sizeof(s2), "CPU %02d:%02d (load avg %3.1f%%)", (int) hr, (int) min, 100.0 * ((float) total / (float) (now - online_since)));
  252. #else
  253. # if HAVE_CLOCK
  254. cl = (clock() / CLOCKS_PER_SEC);
  255. hr = (int) (cl / 60);
  256. min = (int) (cl - (hr * 60));
  257. egg_snprintf(s2, sizeof(s2), "CPU %02d:%02d (load avg %3.1f%%)", (int) hr, (int) min, 100.0 * ((float) cl / (float) (now - online_since)));
  258. # else
  259. simple_snprintf(s2, sizeof(s2), "CPU ???");
  260. # endif
  261. #endif /* HAVE_GETRUSAGE */
  262. dprintf(idx, "%s (%s) %s cache hit %4.1f%%\n",
  263. outbuf, s1, s2,
  264. 100.0 * ((float) cache_hit) / ((float) (cache_hit + cache_miss)));
  265. }
  266. /* Dump status info out to dcc
  267. */
  268. void tell_verbose_status(int idx)
  269. {
  270. char *vers_t = NULL, *uni_t = NULL;
  271. int i;
  272. struct utsname un;
  273. if (!uname(&un) < 0) {
  274. vers_t = " ";
  275. uni_t = "*unknown*";
  276. } else {
  277. vers_t = un.release;
  278. uni_t = un.sysname;
  279. }
  280. i = count_users(userlist);
  281. dprintf(idx, "I am %s, running %s: %d user%s\n", conf.bot->nick, ver, i, i == 1 ? "" : "s");
  282. dprintf(idx, "my user: %s\n", conf.bot->u->handle);
  283. if (conf.bot->localhub)
  284. dprintf(idx, "I am a localhub.\n");
  285. if (conf.bot->hub && isupdatehub())
  286. dprintf(idx, "I am an update hub.\n");
  287. tell_verbose_uptime(idx);
  288. if (admin[0])
  289. dprintf(idx, "Admin: %s\n", admin);
  290. dprintf(idx, "OS: %s %s\n", uni_t, vers_t);
  291. dprintf(idx, "Running from: %s\n", binname);
  292. dprintf(idx, "uid: %s (%d) pid: %lu homedir: %s\n", conf.username, conf.uid, (unsigned long) mypid, conf.homedir);
  293. if (tempdir[0])
  294. dprintf(idx, "Tempdir : %s\n", tempdir);
  295. if (conf.datadir)
  296. dprintf(idx, "Datadir : %s\n", conf.datadir);
  297. }
  298. /* Show all internal state variables
  299. */
  300. void tell_settings(int idx)
  301. {
  302. char s[1024] = "";
  303. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  304. dprintf(idx, "Botnet Nickname: %s\n", conf.bot->nick);
  305. if (conf.bot->hub)
  306. dprintf(idx, "Userfile: %s \n", userfile);
  307. dprintf(idx, "Directories:\n");
  308. dprintf(idx, " Temp : %s\n", tempdir);
  309. fr.global = default_flags;
  310. build_flags(s, &fr, NULL);
  311. dprintf(idx, "New users get flags [%s]\n", s);
  312. if (conf.bot->hub && owner[0])
  313. dprintf(idx, "Permanent owner(s): %s\n", owner);
  314. dprintf(idx, "Ignores last %d mins\n", ignore_time);
  315. }
  316. void reaffirm_owners()
  317. {
  318. /* Make sure perm owners are +a */
  319. if (owner[0]) {
  320. char *q = owner, *p = strchr(q, ','), s[121] = "";
  321. struct userrec *u = NULL;
  322. while (p) {
  323. strlcpy(s, q, (p - q) + 1);
  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. q = p + 1;
  329. p = strchr(q, ',');
  330. }
  331. strlcpy(s, q, sizeof(s));
  332. rmspace(s);
  333. u = get_user_by_handle(userlist, s);
  334. if (u)
  335. u->flags = sanity_check(u->flags | USER_ADMIN, 0);
  336. }
  337. }
  338. bool is_hub(const char* nick) {
  339. char *p = settings.hubs, *p2 = NULL, hubbuf[HANDLEN + 1] ="";
  340. size_t len = 0;
  341. while (p && *p) {
  342. if ((p2 = strchr(p, ' '))) {
  343. len = p2 - p + 1;
  344. if (len > sizeof(hubbuf))
  345. len = sizeof(hubbuf);
  346. strlcpy(hubbuf, p, len);
  347. if (!strncasecmp(nick, hubbuf, HANDLEN))
  348. return 1;
  349. }
  350. if ((p = strchr(p, ',')))
  351. p++;
  352. }
  353. return 0;
  354. }
  355. void load_internal_users()
  356. {
  357. char *p = NULL, *ln = NULL, *hand = NULL, *ip = NULL, *port = NULL, *pass = NULL, *q = NULL;
  358. char *hosts = NULL, buf[2048] = "", *attr = NULL, tmp[51] = "";
  359. int i, hublevel = 0;
  360. struct bot_addr *bi = NULL;
  361. struct userrec *u = NULL;
  362. /* hubs */
  363. strlcpy(buf, settings.hubs, sizeof(buf));
  364. p = buf;
  365. while (p) {
  366. ln = p;
  367. p = strchr(p, ',');
  368. if (p)
  369. *p++ = 0;
  370. hand = ln;
  371. ip = NULL;
  372. port = NULL;
  373. u = NULL;
  374. for (i = 0; ln; i++) {
  375. switch (i) {
  376. case 0:
  377. hand = ln;
  378. break;
  379. case 1:
  380. ip = ln;
  381. break;
  382. case 2:
  383. port = ln;
  384. hublevel++; /* We must increment this even if it is already added */
  385. if (!get_user_by_handle(userlist, hand)) {
  386. userlist = adduser(userlist, hand, "none", "-", USER_OP, 1);
  387. u = get_user_by_handle(userlist, hand);
  388. simple_snprintf(tmp, sizeof(tmp), "%li [internal]", (long)now);
  389. set_user(&USERENTRY_ADDED, u, tmp);
  390. bi = (struct bot_addr *) my_calloc(1, sizeof(struct bot_addr));
  391. bi->address = strdup(ip);
  392. bi->telnet_port = atoi(port) ? atoi(port) : 0;
  393. bi->relay_port = bi->telnet_port;
  394. bi->hublevel = hublevel;
  395. if (conf.bot->hub && (!bi->hublevel) && (!strcasecmp(hand, conf.bot->nick)))
  396. bi->hublevel = 99;
  397. bi->uplink = (char *) my_calloc(1, 1);
  398. set_user(&USERENTRY_BOTADDR, u, bi);
  399. /* set_user(&USERENTRY_PASS, get_user_by_handle(userlist, hand), SALT2); */
  400. }
  401. break;
  402. default:
  403. break;
  404. }
  405. if (ln && (ln = strchr(ln, ' ')))
  406. *ln++ = 0;
  407. }
  408. }
  409. /* perm owners */
  410. owner[0] = 0;
  411. strlcpy(buf, settings.owners, sizeof(buf));
  412. p = buf;
  413. while (p) {
  414. ln = p;
  415. p = strchr(p, ',');
  416. if (p)
  417. *p++ = 0;
  418. hand = ln;
  419. pass = NULL;
  420. attr = NULL;
  421. hosts = NULL;
  422. for (i = 0; ln; i++) {
  423. switch (i) {
  424. case 0:
  425. hand = ln;
  426. break;
  427. case 1:
  428. pass = ln;
  429. if (ln && (ln = strchr(ln, ' ')))
  430. *ln++ = 0;
  431. hosts = ln;
  432. if (owner[0])
  433. strlcat(owner, ",", 121);
  434. strlcat(owner, hand, 121);
  435. if (!get_user_by_handle(userlist, hand)) {
  436. userlist = adduser(userlist, hand, "none", "-", USER_ADMIN | USER_OWNER | USER_MASTER | USER_OP | USER_PARTY | USER_HUBA | USER_CHUBA, 0);
  437. u = get_user_by_handle(userlist, hand);
  438. set_user(&USERENTRY_PASS, u, pass);
  439. simple_snprintf(tmp, sizeof(tmp), "%li [internal]", (long)now);
  440. set_user(&USERENTRY_ADDED, u, tmp);
  441. while (hosts) {
  442. char x[1024] = "";
  443. if ((ln = strchr(ln, ' ')))
  444. *ln++ = 0;
  445. if ((q = strchr(hosts, '!'))) { /* skip over nick they provided ... */
  446. q++;
  447. if (*q == '*' || *q == '?') /* ... and any '*' or '?' */
  448. q++;
  449. hosts = q;
  450. }
  451. simple_snprintf(x, sizeof(x), "-telnet!%s", hosts);
  452. set_user(&USERENTRY_HOSTS, u, x);
  453. hosts = ln;
  454. }
  455. }
  456. break;
  457. default:
  458. break;
  459. }
  460. if (ln && (ln = strchr(ln, ' ')))
  461. *ln++ = 0;
  462. }
  463. }
  464. // Add HQ in if needed
  465. if (!backgrd && term_z) {
  466. strlcat(owner, ",HQ", sizeof(owner));
  467. }
  468. }
  469. static struct userrec* add_bot_userlist(char* bot) {
  470. struct userrec *u = NULL;
  471. if (!(u = get_user_by_handle(userlist, bot))) {
  472. /* I need to be on the userlist... doh. */
  473. userlist = adduser(userlist, bot, "none", "-", USER_OP, 1);
  474. u = get_user_by_handle(userlist, bot);
  475. struct bot_addr *bi = (struct bot_addr *) my_calloc(1, sizeof(struct bot_addr));
  476. bi->uplink = (char *) my_calloc(1, 1);
  477. bi->address = (char *) my_calloc(1, 1);
  478. bi->telnet_port = 3333;
  479. bi->relay_port = 3333;
  480. bi->hublevel = 999;
  481. set_user(&USERENTRY_BOTADDR, u, bi);
  482. }
  483. return u;
  484. }
  485. void add_myself_to_userlist() {
  486. conf.bot->u = add_bot_userlist(conf.bot->nick);
  487. }
  488. void add_child_bots() {
  489. conf_bot* bot = conf.bots->next; //Skip myself
  490. if (bot && bot->nick) {
  491. for (; bot && bot->nick; bot = bot->next) {
  492. add_bot_userlist(bot->nick);
  493. }
  494. }
  495. }
  496. void add_localhub() {
  497. add_bot_userlist(conf.localhub);
  498. }
  499. void rehash_ip() {
  500. /* cache our ip on load instead of every 30 seconds */
  501. char *ip4 = NULL, *ip6 = NULL;
  502. if (cached_ip) {
  503. ip4 = strdup(myipstr(AF_INET));
  504. ip6 = strdup(myipstr(AF_INET6));
  505. }
  506. cache_my_ip();
  507. sdprintf("ip4: %s", myipstr(AF_INET));
  508. sdprintf("ip6: %s", myipstr(AF_INET6));
  509. /* Check if our ip changed during a rehash */
  510. if (ip4) {
  511. if (strcmp(ip4, myipstr(AF_INET)) || strcmp(ip6, myipstr(AF_INET6))) {
  512. if (tands > 0) {
  513. botnet_send_chat(-1, conf.bot->nick, "IP changed.");
  514. botnet_send_bye("IP changed.");
  515. }
  516. fatal("brb", 1);
  517. }
  518. free(ip4);
  519. free(ip6);
  520. }
  521. if (conf.bot->hub) {
  522. struct bot_addr *bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, conf.bot->u);
  523. listen_all(bi->telnet_port, 0, 1);
  524. my_port = bi->telnet_port;
  525. } else if (conf.bot->localhub) {
  526. // If not listening on the domain socket, open it up
  527. bool listening = 0;
  528. for (int i = 0; i < dcc_total; i++) {
  529. if (dcc[i].type && (dcc[i].type == &DCC_TELNET) && (!strcmp(dcc[i].host, conf.localhub_socket)) && (!strcmp(dcc[i].nick, "(unix_domain"))) {
  530. listening = 1;
  531. break;
  532. }
  533. }
  534. if (!listening) {
  535. // Listen on the unix domain socket
  536. port_t port;
  537. int i = open_listen_addr_by_af(conf.localhub_socket, &port, AF_UNIX);
  538. if (i < 0) {
  539. putlog(LOG_ERRORS, "*", "Can't listen on %s - %s", conf.localhub_socket, i == -1 ? "it's taken." : "couldn't assign file.");
  540. } else {
  541. /* now setup dcc entry */
  542. int idx = new_dcc(&DCC_TELNET, 0);
  543. dcc[idx].addr = 0L;
  544. strlcpy(dcc[idx].host, conf.localhub_socket, sizeof(dcc[idx].host));
  545. dcc[idx].port = 0;
  546. dcc[idx].sock = i;
  547. dcc[idx].timeval = now;
  548. strlcpy(dcc[idx].nick, "(unix_domain)", sizeof(dcc[idx].nick));
  549. putlog(LOG_DEBUG, "*", "Listening on telnet %s", conf.localhub_socket);
  550. }
  551. }
  552. }
  553. }
  554. void chanprog()
  555. {
  556. struct utsname un;
  557. sdprintf("I am: %s", conf.bot->nick);
  558. /* Add the 'default' virtual channel.
  559. The flags will be overwritten when the userfile is loaded,
  560. but if there is no userfile yet, or no 'default' channel,
  561. then it will take on the properties of 'def_chanset',
  562. and then later save this to the userfile.
  563. */
  564. channel_add(NULL, "default", def_chanset, 1);
  565. if (conf.bot->hub) {
  566. simple_snprintf(userfile, 121, "%s/.u", dirname(binname));
  567. loading = 1;
  568. checkchans(0);
  569. readuserfile(userfile, &userlist);
  570. checkchans(1);
  571. var_parse_my_botset();
  572. loading = 0;
  573. }
  574. load_internal_users();
  575. add_myself_to_userlist();
  576. if (conf.bot->localhub)
  577. add_child_bots();
  578. else if (!conf.bot->hub)
  579. add_localhub();
  580. rehash_ip();
  581. /* set our shell info */
  582. uname(&un);
  583. set_user(&USERENTRY_OS, conf.bot->u, un.sysname);
  584. set_user(&USERENTRY_USERNAME, conf.bot->u, conf.username);
  585. set_user(&USERENTRY_NODENAME, conf.bot->u, un.nodename);
  586. set_user(&USERENTRY_ARCH, conf.bot->u, un.machine);
  587. set_user(&USERENTRY_OSVER, conf.bot->u, un.release);
  588. var_parse_my_botset();
  589. /* We should be safe now */
  590. reaffirm_owners();
  591. }
  592. /* Reload the user file from disk
  593. */
  594. void reload()
  595. {
  596. FILE *f = fopen(userfile, "r");
  597. if (f == NULL) {
  598. putlog(LOG_MISC, "*", "Can't reload user file!");
  599. return;
  600. }
  601. fclose(f);
  602. noshare = 1;
  603. clear_userlist(userlist);
  604. noshare = 0;
  605. userlist = NULL;
  606. loading = 1;
  607. checkchans(0);
  608. if (!readuserfile(userfile, &userlist))
  609. fatal("User file is missing!", 0);
  610. /* ensure we did not lose our internal users */
  611. load_internal_users();
  612. /* make sure I am added and conf.bot->u is set */
  613. add_myself_to_userlist();
  614. if (conf.bot->localhub)
  615. add_child_bots();
  616. else if (!conf.bot->hub)
  617. add_localhub();
  618. /* Make sure no removed users/bots are still connected. */
  619. check_stale_dcc_users();
  620. for (tand_t* bot = tandbot; bot; bot = bot->next)
  621. bot->u = get_user_by_handle(userlist, bot->bot);
  622. /* I don't think these will ever be called anyway. */
  623. if (!conf.bot->hub) {
  624. Auth::FillUsers();
  625. check_hostmask();
  626. }
  627. checkchans(1);
  628. loading = 0;
  629. var_parse_my_botset();
  630. reaffirm_owners();
  631. hook_read_userfile();
  632. }
  633. void setup_HQ(int n) {
  634. dcc[n].addr = iptolong(getmyip());
  635. dcc[n].sock = STDOUT;
  636. dcc[n].timeval = now;
  637. dcc[n].u.chat->con_flags = conmask | LOG_ALL;
  638. dcc[n].u.chat->strip_flags = STRIP_ALL;
  639. dcc[n].status = STAT_ECHO;
  640. strlcpy(dcc[n].nick, STR("HQ"), sizeof(dcc[n].nick));
  641. strlcpy(dcc[n].host, STR("llama@console"), sizeof(dcc[n].host));
  642. dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
  643. /* Make sure there's an innocuous HQ user if needed */
  644. if (!dcc[n].user) {
  645. userlist = adduser(userlist, dcc[n].nick, "none", "-", USER_ADMIN | USER_OWNER | USER_MASTER | USER_VOICE | USER_OP | USER_PARTY | USER_CHUBA | USER_HUBA, 0);
  646. dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
  647. }
  648. }
  649. /* Oddly enough, written by proton (Emech's coder)
  650. */
  651. int isowner(char *name)
  652. {
  653. if (!owner[0])
  654. return (0);
  655. if (!name || !name[0])
  656. return (0);
  657. char *pa = owner, *pb = owner;
  658. size_t nl = strlen(name), pl;
  659. while (1) {
  660. while (1) {
  661. if ((*pb == 0) || (*pb == ',') || (*pb == ' '))
  662. break;
  663. pb++;
  664. }
  665. pl = pb - pa;
  666. if (pl == nl && !strncasecmp(pa, name, nl))
  667. return (1);
  668. while (1) {
  669. if ((*pb == 0) || ((*pb != ',') && (*pb != ' ')))
  670. break;
  671. pb++;
  672. }
  673. if (*pb == 0)
  674. return (0);
  675. pa = pb;
  676. }
  677. }
  678. bool bot_shouldjoin(struct userrec* u, struct flag_record* fr, struct chanset_t* chan, bool ignore_inactive)
  679. {
  680. // If restarting, keep this channel.
  681. if (restarting && (reset_chans == 2) && (channel_active(chan) || channel_pending(chan))) return 1;
  682. /* If the bot is restarting (and hasn't finished getting the userfile for the first time) DO NOT JOIN channels - breaks +B/+backup */
  683. if (restarting || loading) return 0;
  684. // No user record, can't make any safe assumptions really
  685. if (!u) return 0;
  686. #ifdef DEBUG
  687. /* Force debugging bots to only join 3 channels */
  688. if (!strncmp(u->handle, "wtest", 5)) {
  689. if (!strcmp(chan->dname, "#skynet") ||
  690. !strcmp(chan->dname, "#bryan") ||
  691. !strcmp(chan->dname, "#wraith-devel"))
  692. return 1;
  693. else
  694. return 0;
  695. }
  696. #endif
  697. // Am I in the groups that this channel has?
  698. bd::Array<bd::String> my_groupsArray(bd::String(groups).split(','));
  699. bool group_match = 0;
  700. if (chan->groups && chan->groups->length()) {
  701. for (size_t i = 0; i < my_groupsArray.length(); ++i) {
  702. if (chan->groups->find(my_groupsArray[i]) != chan->groups->npos) {
  703. group_match = 1;
  704. break;
  705. }
  706. }
  707. }
  708. // Ignore +inactive during cmd_slowjoin to ensure that +backup bots join
  709. return (!glob_kick(*fr) && !chan_kick(*fr) && // Not being kicked
  710. ((ignore_inactive || !channel_inactive(chan)) && // Not inactive
  711. ((channel_backup(chan) && (glob_backup(*fr) || chan_backup(*fr) || group_match)) || // Is +backup and I'm a backup bot or my group matches
  712. (!channel_backup(chan) && !glob_backup(*fr) && !chan_backup(*fr) && group_match))) // is -backup and I am not a backup bot and my group matches
  713. );
  714. }
  715. bool shouldjoin(struct chanset_t *chan)
  716. {
  717. struct flag_record fr = { FR_CHAN|FR_GLOBAL|FR_BOT, 0, 0, 0 };
  718. get_user_flagrec(conf.bot->u, &fr, chan->dname, chan);
  719. return bot_shouldjoin(conf.bot->u, &fr, chan);
  720. }
  721. /* do_chanset() set (options) on (chan)
  722. * USES DO_LOCAL|DO_NET bits.
  723. */
  724. int do_chanset(char *result, struct chanset_t *chan, const char *options, int flags)
  725. {
  726. int ret = OK;
  727. if (flags & DO_NET) {
  728. size_t bufsiz = 0;
  729. /* malloc(options,chan,'cset ',' ',+ 1) */
  730. if (chan)
  731. bufsiz = strlen(options) + strlen(chan->dname) + 5 + 1 + 1;
  732. else
  733. bufsiz = strlen(options) + 1 + 5 + 1 + 1;
  734. char *buf = (char*) my_calloc(1, bufsiz);
  735. strlcat(buf, "cset ", bufsiz);
  736. if (chan)
  737. strlcat(buf, chan->dname, bufsiz);
  738. else
  739. strlcat(buf, "*", bufsiz);
  740. strlcat(buf, " ", bufsiz);
  741. strlcat(buf, options, bufsiz);
  742. putlog(LOG_DEBUG, "*", "sending out cset: %s", buf);
  743. putallbots(buf);
  744. free(buf);
  745. }
  746. if (flags & DO_LOCAL) {
  747. bool cmd = (flags & CMD);
  748. struct chanset_t *ch = NULL;
  749. int all = chan ? 0 : 1;
  750. if (chan)
  751. ch = chan;
  752. else
  753. ch = chanset_default; //First iteration changes default, then move on to all chans
  754. while (ch) {
  755. const char **item = NULL;
  756. int items = 0;
  757. if (SplitList(result, options, &items, &item) == OK) {
  758. ret = channel_modify(result, ch, items, (char **) item, cmd);
  759. } else
  760. ret = ERROR;
  761. free(item);
  762. if (all) {
  763. if (ret == ERROR) /* just bail if there was an error, no sense in trying more */
  764. return ret;
  765. if (ch == chanset_default)
  766. ch = chanset;
  767. else
  768. ch = ch->next;
  769. } else {
  770. ch = NULL;
  771. }
  772. }
  773. }
  774. return ret;
  775. }
  776. char *
  777. samechans(const char *nick, const char *delim)
  778. {
  779. static char ret[1024] = "";
  780. struct chanset_t *chan = NULL;
  781. ret[0] = 0; /* may be filled from last time */
  782. for (chan = chanset; chan; chan = chan->next) {
  783. if (ismember(chan, nick)) {
  784. strlcat(ret, chan->dname, sizeof(ret));
  785. strlcat(ret, delim, sizeof(ret));
  786. }
  787. }
  788. return ret;
  789. }
  790. struct chanset_t* find_common_opped_chan(bd::String nick) {
  791. for (struct chanset_t* chan = chanset; chan; chan = chan->next) {
  792. if (channel_active(chan) && (me_op(chan) || me_voice(chan))) {
  793. if (ismember(chan, nick.c_str()))
  794. return chan;
  795. }
  796. }
  797. return NULL;
  798. }
  799. void privmsg(bd::String target, bd::String msg, int idx) {
  800. struct chanset_t* chan = NULL;
  801. if (have_cprivmsg && !strchr(CHANMETA, target[0]))
  802. chan = find_common_opped_chan(target);
  803. bool cleartextPrefix = (msg(0, 3) == "+p ");
  804. // Encrypt with FiSH?
  805. if (!strchr(CHANMETA, target[0]) && !cleartextPrefix && FishKeys.contains(target) && FishKeys[target]->sharedKey.length()) {
  806. msg = "+OK " + egg_bf_encrypt(msg, FishKeys[target]->sharedKey);
  807. }
  808. if (cleartextPrefix) {
  809. msg += static_cast<size_t>(3);
  810. }
  811. if (chan)
  812. dprintf(idx, "CPRIVMSG %s %s :%s\n", target.c_str(), chan->name, msg.c_str());
  813. else
  814. dprintf(idx, "PRIVMSG %s :%s\n", target.c_str(), msg.c_str());
  815. }
  816. void notice(bd::String target, bd::String msg, int idx) {
  817. struct chanset_t* chan = NULL;
  818. if (have_cnotice && !strchr(CHANMETA, target[0]))
  819. chan = find_common_opped_chan(target);
  820. if (chan)
  821. dprintf(idx, "CNOTICE %s %s :%s\n", target.c_str(), chan->name, msg.c_str());
  822. else
  823. dprintf(idx, "NOTICE %s :%s\n", target.c_str(), msg.c_str());
  824. }
  825. void keyx(const bd::String &target) {
  826. bd::String myPublicKeyB64, myPrivateKey, sharedKey;
  827. DH1080_gen(myPrivateKey, myPublicKeyB64);
  828. putlog(LOG_MSGS, "*", "[FiSH] Initiating DH1080 key-exchange with %s - sending my public key", target.c_str());
  829. notice(target, "DH1080_INIT " + myPublicKeyB64, DP_HELP);
  830. fish_data_t* fishData = new fish_data_t;
  831. fishData->myPublicKeyB64 = myPublicKeyB64;
  832. fishData->myPrivateKey = myPrivateKey;
  833. fishData->timestamp = now;
  834. FishKeys[target] = fishData;
  835. }