1
0

chanprog.cc 26 KB

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