misc.cc 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  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. * misc.c -- handles:
  22. * split() maskhost() dumplots() daysago() days() daysdur()
  23. * queueing output for the bot (msg and help)
  24. * resync buffers for sharebots
  25. * motd display and %var substitution
  26. *
  27. */
  28. #include "common.h"
  29. #include "misc.h"
  30. #include "settings.h"
  31. #include "binary.h"
  32. #include "rfc1459.h"
  33. #include "botnet.h"
  34. #include "misc_file.h"
  35. #include "egg_timer.h"
  36. #include "dcc.h"
  37. #include "users.h"
  38. #include "shell.h"
  39. #include "main.h"
  40. #include "debug.h"
  41. #include "dccutil.h"
  42. #include "chanprog.h"
  43. #include "color.h"
  44. #include "botmsg.h"
  45. #include "bg.h"
  46. #include "chan.h"
  47. #include "tandem.h"
  48. #include "src/mod/server.mod/server.h"
  49. #include "src/mod/irc.mod/irc.h"
  50. #include "src/mod/channels.mod/channels.h"
  51. #include "userrec.h"
  52. #include "stat.h"
  53. #include "net.h"
  54. #include "EncryptedStream.h"
  55. #include <bdlib/src/String.h>
  56. #include <bdlib/src/Stream.h>
  57. #include <sys/wait.h>
  58. #include <stdarg.h>
  59. #include <sys/types.h>
  60. #include <signal.h>
  61. int server_lag = 0; /* GUESS! */
  62. bool use_invites = 0;
  63. bool use_exempts = 0;
  64. /*
  65. * Misc functions
  66. */
  67. /* low-level stuff for other modules
  68. */
  69. size_t my_strcpy(char *a, const char *b)
  70. {
  71. const char *c = b;
  72. while (*b)
  73. *a++ = *b++;
  74. *a = *b;
  75. return b - c;
  76. }
  77. /* Split first word off of rest and put it in first
  78. */
  79. void splitc(char *first, char *rest, char divider)
  80. {
  81. char *p = strchr(rest, divider);
  82. if (p == NULL) {
  83. if (first != rest && first)
  84. first[0] = 0;
  85. return;
  86. }
  87. *p = 0;
  88. if (first != NULL)
  89. strcpy(first, rest);
  90. if (first != rest)
  91. /* In most circumstances, strcpy with src and dst being the same buffer
  92. * can produce undefined results. We're safe here, as the src is
  93. * guaranteed to be at least 2 bytes higher in memory than dest. <Cybah>
  94. */
  95. strcpy(rest, p + 1);
  96. }
  97. /* As above, but lets you specify the 'max' number of bytes (EXCLUDING the
  98. * terminating null).
  99. *
  100. * Example of use:
  101. *
  102. * char buf[HANDLEN + 1];
  103. *
  104. * splitcn(buf, input, "@", HANDLEN);
  105. *
  106. * <Cybah>
  107. */
  108. void splitcn(char *first, char *rest, char divider, size_t max)
  109. {
  110. char *p = strchr(rest, divider);
  111. if (p == NULL) {
  112. if (first != rest && first)
  113. first[0] = 0;
  114. return;
  115. }
  116. *p = 0;
  117. if (first != NULL)
  118. strlcpy(first, rest, max);
  119. if (first != rest)
  120. /* In most circumstances, strcpy with src and dst being the same buffer
  121. * can produce undefined results. We're safe here, as the src is
  122. * guaranteed to be at least 2 bytes higher in memory than dest. <Cybah>
  123. */
  124. strcpy(rest, p + 1);
  125. }
  126. const char *splitnick(char **blah)
  127. {
  128. char *p = NULL, *q = *blah;
  129. p = strchr(*blah, '!');
  130. if (p) {
  131. *p = 0;
  132. *blah = p + 1;
  133. return q;
  134. }
  135. return "";
  136. }
  137. int remove_crlf(char *line)
  138. {
  139. char *p = NULL;
  140. int removed = 0;
  141. if ((p = strchr(line, '\n'))) {
  142. *p = 0;
  143. removed++;
  144. }
  145. if ((p = strchr(line, '\r'))) {
  146. *p = 0;
  147. removed++;
  148. }
  149. return removed;
  150. }
  151. int remove_crlf_r(char *line)
  152. {
  153. char *p = NULL;
  154. int removed = 0;
  155. if ((p = strrchr(line, '\n'))) {
  156. *p = 0;
  157. removed++;
  158. }
  159. if ((p = strrchr(line, '\r'))) {
  160. *p = 0;
  161. removed++;
  162. }
  163. return removed;
  164. }
  165. char *newsplit(char **rest, char delim, bool trim)
  166. {
  167. if (!rest) {
  168. static char end[] = "";
  169. return *rest = end;
  170. }
  171. char *o = *rest, *r = NULL;
  172. while (*o == delim)
  173. ++o;
  174. r = o;
  175. while (*o && (*o != delim))
  176. ++o;
  177. if (*o)
  178. *o++ = 0;
  179. /* Trim whitespace */
  180. if (trim) {
  181. while (*o == ' ')
  182. ++o;
  183. }
  184. *rest = o;
  185. return r;
  186. }
  187. /* maskhost(), modified to support custom mask types, as defined
  188. * by mIRC.
  189. * Does not require a proper hostmask in 's'. Accepts any strings,
  190. * including empty ones and attempts to provide meaningful results.
  191. *
  192. * Strings containing no '@' character will be parsed as if the
  193. * whole string is a host.
  194. * Strings containing no '!' character will be interpreted as if
  195. * there is no nick.
  196. * '!' as a nick/user separator must precede any '@' characters.
  197. * Otherwise it will be considered a part of the host.
  198. * Supported types are listed in tcl-commands.doc in the maskhost
  199. * command section. Type 3 resembles the older maskhost() most closely.
  200. *
  201. * Specific examples (with type=3):
  202. *
  203. * "nick!user@is.the.lamest.bg" -> *!*user@*.the.lamest.bg (ccTLD)
  204. * "nick!user@is.the.lamest.com" -> *!*user@*.lamest.com (gTLD)
  205. * "lamest.example" -> *!*@lamest.example
  206. * "whatever@lamest.example" -> *!*whatever@lamest.example
  207. * "com.example@user!nick" -> *!*com.example@user!nick
  208. * "!" -> *!*@!
  209. * "@" -> *!*@*
  210. * "" -> *!*@*
  211. * "abc!user@2001:db8:618:5c0:263:15:dead:babe"
  212. * -> *!*user@2001:db8:618:5c0:263:15:dead:*
  213. * "abc!user@0:0:0:0:0:ffff:1.2.3.4"
  214. * -> *!*user@0:0:0:0:0:ffff:1.2.3.*
  215. */
  216. void maskaddr(const char *s, char *nw, int type)
  217. {
  218. int d = type % 5, num = 1;
  219. const char *u = NULL, *h = NULL, *p = NULL;
  220. /* Look for user and host.. */
  221. if ((u = strchr(s, '!')))
  222. h = strchr(u, '@');
  223. if (!h)
  224. h = strchr(s, '@');
  225. /* Print nick if required and available */
  226. if (!u || (type % 10) < 5)
  227. *nw++ = '*';
  228. else {
  229. strncpy(nw, s, u - s);
  230. nw += u - s;
  231. }
  232. *nw++ = '!';
  233. /* Write user if required and available */
  234. u = (u ? u + 1 : s);
  235. if (!h || (d == 2) || (d == 4))
  236. *nw++ = '*';
  237. else {
  238. if (d) {
  239. *nw++ = '*';
  240. if (strchr("~+-^=", *u))
  241. u++; /* trim leading crap */
  242. /*
  243. * Take last 9 chars to avoid running up against 10-char limit for
  244. * username on ratbox. The older eggdrop code used this limit as well.
  245. */
  246. while (h - u > 9)
  247. u++;
  248. }
  249. strncpy(nw, u, h - u);
  250. nw += h - u;
  251. }
  252. *nw++ = '@';
  253. /* The rest is for the host */
  254. h = (h ? h + 1 : s);
  255. for (p = h; *p; p++) /* hostname? */
  256. if ((*p > '9' || *p < '0') && *p != '.') {
  257. num = 0;
  258. break;
  259. }
  260. p = strrchr(h, ':'); /* IPv6? */
  261. /* Mask out after the last colon/dot */
  262. if (p && d > 2) {
  263. if ((u = strrchr(p, '.')))
  264. p = u;
  265. strncpy(nw, h, ++p - h);
  266. nw += p - h;
  267. strcpy(nw, "*");
  268. } else if (!p && !num && type >= 10) {
  269. /* we have a hostname and type
  270. requires us to replace numbers */
  271. num = 0;
  272. for (p = h; *p; p++) {
  273. if (*p < '0' || *p > '9') {
  274. *nw++ = *p;
  275. num = 0;
  276. } else {
  277. if (type < 20)
  278. *nw++ = '?';
  279. else if (!num) {
  280. *nw++ = '*';
  281. num = 1; /* place only one '*'
  282. per numeric sequence */
  283. }
  284. }
  285. }
  286. *nw = 0;
  287. } else if (d > 2 && (p = strrchr(h, '.'))) {
  288. if (num) { /* IPv4 */
  289. strncpy(nw, h, p - h);
  290. nw += p - h;
  291. strcpy(nw, ".*");
  292. return;
  293. }
  294. for (u = h, d = 0; (u = strchr(++u, '.')); d++) ;
  295. if (d < 2) { /* types < 2 don't mask the host */
  296. strcpy(nw, h);
  297. return;
  298. }
  299. u = strchr(h, '.');
  300. if (d > 3 || (d == 3 && strlen(p) > 3))
  301. u = strchr(++u, '.'); /* ccTLD or not? Look above. */
  302. simple_sprintf(nw, "*%s", u);
  303. } else if (!*h) {
  304. /* take care if the mask is empty or contains only '@' */
  305. strcpy(nw, "*");
  306. } else
  307. strcpy(nw, h);
  308. }
  309. /* Convert an interval (in seconds) to one of:
  310. * "19 days ago", "1 day ago", "18:12"
  311. */
  312. void daysago(time_t mynow, time_t then, char *out, size_t outsiz)
  313. {
  314. if (mynow - then > 86400) {
  315. int mydays = (mynow - then) / 86400;
  316. simple_snprintf(out, outsiz, "%d day%s ago", mydays, (mydays == 1) ? "" : "s");
  317. return;
  318. }
  319. strftime(out, 6, "%H:%M", gmtime(&then));
  320. }
  321. /* Convert an interval (in seconds) to one of:
  322. * "in 19 days", "in 1 day", "at 18:12"
  323. */
  324. void days(time_t mynow, time_t then, char *out, size_t outsiz)
  325. {
  326. if (mynow - then > 86400) {
  327. int mydays = (mynow - then) / 86400;
  328. simple_snprintf(out, outsiz, "in %d day%s", mydays, (mydays == 1) ? "" : "s");
  329. return;
  330. }
  331. strftime(out, 9, "at %H:%M", gmtime(&now));
  332. }
  333. /* Convert an interval (in seconds) to one of:
  334. * "for 19 days", "for 1 day", "for 09:10"
  335. */
  336. void daysdur(time_t mynow, time_t then, char *out, size_t outsiz, bool useFor)
  337. {
  338. size_t startIndex = 0;
  339. out[0] = 0;
  340. if (useFor) {
  341. strlcpy(out, "for ", outsiz);
  342. startIndex = 4;
  343. }
  344. if (mynow - then > 86400) {
  345. int mydays = (mynow - then) / 86400;
  346. simple_snprintf(&out[startIndex], outsiz - startIndex, "%d day%s", mydays, (mydays == 1) ? "" : "s");
  347. return;
  348. }
  349. char s[81] = "";
  350. mynow -= then;
  351. int hrs = (int) (mynow / 3600);
  352. int mins = (int) ((mynow - (hrs * 3600)) / 60);
  353. simple_snprintf(s, sizeof(s), "%02d:%02d", hrs, mins);
  354. strlcat(out, s, outsiz);
  355. }
  356. /* show l33t banner */
  357. static const char *wbanner(void) {
  358. /*
  359. __ __ __
  360. __ _ ______________ |__|/ |_| |__
  361. \ \/ \/ /\_ __ \__ \ | \ _\ | \
  362. \ / | | \// __ \| || | | \ \
  363. \/\_/ |__| (____ /__||__| |___| /
  364. \/ \/
  365. */
  366. return STR(" __ __ __\n__ _ ______________ |__|/ |_| |__\n\\ \\/ \\/ /\\_ __ \\__ \\ | \\ _\\ | \\\n \\ / | | \\// __ \\| || | | \\ \\\n \\/\\_/ |__| (____ /__||__| |___| /\n \\/ \\/\n");
  367. }
  368. void show_banner(int idx)
  369. {
  370. /* we use sock so that colors aren't applied to banner */
  371. if (dcc[idx].status & STAT_BANNER)
  372. dumplots(-dcc[idx].sock, "", wbanner());
  373. dprintf(idx, " \n");
  374. dprintf(-dcc[idx].sock, STR(" -------------------------------------------------------- \n"));
  375. dprintf(-dcc[idx].sock, STR("| - http://wraith.botpack.net/ - |\n"));
  376. dprintf(-dcc[idx].sock, STR("| Get Shell/Irc/Web hosting @ http://www.xzibition.com |\n"));
  377. dprintf(-dcc[idx].sock, STR("| Help support wraith development by signing up. |\n"));
  378. dprintf(-dcc[idx].sock, STR("| Use coupon code 'wraith' for 30%% off lifetime |\n"));
  379. dprintf(-dcc[idx].sock, STR(" -------------------------------------------------------- \n"));
  380. dprintf(idx, " \n");
  381. }
  382. /* show motd to dcc chatter */
  383. void show_motd(int idx)
  384. {
  385. if (motd[0]) {
  386. char *who = NULL, *buf = NULL, *buf_ptr = NULL, date[50] = "";
  387. time_t when;
  388. buf = buf_ptr = strdup(motd);
  389. who = newsplit(&buf);
  390. when = atoi(newsplit(&buf));
  391. strftime(date, sizeof date, "%c %Z", gmtime(&when));
  392. dprintf(idx, "Motd set by %s%s%s (%s)\n", BOLD(idx), who, BOLD_END(idx), date);
  393. dumplots(idx, "* ", replace(buf, "\\n", "\n"));
  394. dprintf(idx, " \n");
  395. free(buf_ptr);
  396. } else
  397. dprintf(idx, "Motd: none\n");
  398. }
  399. void show_channels(int idx, char *handle)
  400. {
  401. struct userrec *u = NULL;
  402. size_t maxChannelLength = 0;
  403. bd::Array<bd::String> channelNames;
  404. bd::String group;
  405. if (handle && handle[0] != '%') {
  406. u = get_user_by_handle(userlist, handle);
  407. } else {
  408. u = dcc[idx].user;
  409. if (handle && handle[0] == '%') {
  410. group = handle + 1;
  411. }
  412. }
  413. for (struct chanset_t* chan = chanset; chan; chan = chan->next) {
  414. struct flag_record fr = { FR_CHAN | FR_GLOBAL, 0, 0, 0 };
  415. const bd::String chname(chan->dname);
  416. // If a group was passed, ensure it matches
  417. if (group.length() && chan->groups->find(group) == chan->groups->npos) {
  418. continue;
  419. }
  420. get_user_flagrec(u, &fr, chan->dname);
  421. if (group.length() || real_chk_op(fr, chan, 0)) {
  422. if (maxChannelLength < chname.length()) {
  423. maxChannelLength = chname.length();
  424. }
  425. channelNames << chname;
  426. }
  427. }
  428. if (channelNames.length()) {
  429. char format[120] = "";
  430. simple_snprintf(format, sizeof(format), " %%c%%-%zus %%-s%%-s%%-s%%-s%%-s%%-s\n", (maxChannelLength+2));
  431. if (group.length()) {
  432. dprintf(idx, "group '%s' is in %zu channel%s:\n", group.c_str(), channelNames.length(), (channelNames.length() > 1) ? "s" : "");
  433. } else {
  434. dprintf(idx, "%s %s access to %zu channel%s:\n", handle ? u->handle : "You", handle ? "has" : "have", channelNames.length(), (channelNames.length() > 1) ? "s" : "");
  435. }
  436. for (const auto& chname : channelNames) {
  437. const struct chanset_t* chan = findchan_by_dname(chname);
  438. dprintf(idx, format, !conf.bot->hub && me_op(chan) ? '@' : ' ', chan->dname, ((conf.bot->hub && channel_inactive(chan)) || (!conf.bot->hub && !shouldjoin(chan))) ? "(inactive) " : "",
  439. channel_privchan(chan) ? "(private) " : "", chan->manop ? "(no manop) " : "",
  440. channel_bitch(chan) && !channel_botbitch(chan) ? "(bitch) " : channel_botbitch(chan) ? "(botbitch) " : "",
  441. channel_closed(chan) ? "(closed) " : "", channel_backup(chan) ? "(backup)" : "");
  442. }
  443. } else {
  444. if (group.length()) {
  445. dprintf(idx, "No channels found for group '%s'\n", group.c_str());
  446. } else {
  447. dprintf(idx, "%s %s not have access to any channels.\n", handle ? u->handle : "You", handle ? "does" : "do");
  448. }
  449. }
  450. }
  451. /* Create a string with random letters and digits
  452. */
  453. void make_rand_str(char *s, size_t len, bool special)
  454. {
  455. int r = 0;
  456. size_t j = 0;
  457. for (j = 0; j < len; j++) {
  458. r = randint(special ? 4 : 3);
  459. if (r == 0)
  460. s[j] = '0' + randint(10);
  461. else if (r == 1)
  462. s[j] = 'a' + randint(26);
  463. else if (r == 2)
  464. s[j] = 'A' + randint(26);
  465. else if (r == 3)
  466. s[j] = RANDSPECIAL[randint(RANDSPECIALLEN)];
  467. if (j && strchr(BADREPEATEDRAND, s[j]) && s[j] == s[j - 1]) {
  468. while (s[j] == s[j - 1])
  469. s[j] = 'A' + randint(26);
  470. }
  471. }
  472. if (strchr(BADPASSCHARS, s[0]))
  473. s[0] = 'a' + randint(26);
  474. s[len] = '\0';
  475. }
  476. /* Return an allocated buffer which contains a copy of the string
  477. * 'str', with all 'div' characters escaped by 'mask'. 'mask'
  478. * characters are escaped too.
  479. *
  480. * Remember to free the returned memory block.
  481. */
  482. char *str_escape(const char *str, const char divc, const char mask)
  483. {
  484. const size_t len = strlen(str);
  485. size_t buflen = (2 * len), blen = 0;
  486. char *buf = NULL, *b = NULL;
  487. const char *s = NULL;
  488. b = buf = (char *) calloc(1, buflen + 1);
  489. for (s = str; *s; s++) {
  490. /* Resize buffer. */
  491. if ((buflen - blen) <= 3) {
  492. buflen <<= 1; /* * 2 */
  493. buf = (char *) realloc(buf, buflen + 1);
  494. if (!buf)
  495. return NULL;
  496. b = buf + blen;
  497. }
  498. if (*s == divc || *s == mask) {
  499. simple_snprintf(b, buflen, "%c%02x", mask, *s);
  500. b += 3;
  501. blen += 3;
  502. } else {
  503. *(b++) = *s;
  504. blen++;
  505. }
  506. }
  507. *b = 0;
  508. return buf;
  509. }
  510. /* Search for a certain character 'div' in the string 'str', while
  511. * ignoring escaped characters prefixed with 'mask'.
  512. *
  513. * The string
  514. *
  515. * "\\3a\\5c i am funny \\3a):further text\\5c):oink"
  516. *
  517. * as str, '\\' as mask and ':' as div would change the str buffer
  518. * to
  519. *
  520. * ":\\ i am funny :)"
  521. *
  522. * and return a pointer to "further text\\5c):oink".
  523. *
  524. * NOTE: If you look carefully, you'll notice that strchr_unescape()
  525. * behaves differently than strchr().
  526. */
  527. char *strchr_unescape(char *str, const char divc, const char esc_char)
  528. {
  529. char buf[3] = "";
  530. char *s = NULL, *p = NULL;
  531. for (s = p = str; *s; s++, p++) {
  532. if (*s == esc_char) { /* Found escape character. */
  533. /* Convert code to character. */
  534. buf[0] = s[1], buf[1] = s[2];
  535. *p = (unsigned char) strtol(buf, NULL, 16);
  536. s += 2;
  537. } else if (*s == divc) {
  538. *p = *s = 0;
  539. return (s + 1); /* Found searched for character. */
  540. } else
  541. *p = *s;
  542. }
  543. *p = 0;
  544. return NULL;
  545. }
  546. char s1_16[3] = "",s2_6[3] = "",s2_7[3] = "";
  547. /* As strchr_unescape(), but converts the complete string, without
  548. * searching for a specific delimiter character.
  549. */
  550. void str_unescape(char *str, const char esc_char)
  551. {
  552. strchr_unescape(str, 0, esc_char);
  553. return;
  554. }
  555. /* Is every character in a string a digit? */
  556. int str_isdigit(const char *str)
  557. {
  558. if (!str || (str && !*str))
  559. return 0;
  560. if (*str == '-' && str[1])
  561. str++;
  562. for(; *str; ++str) {
  563. if (!egg_isdigit(*str))
  564. return 0;
  565. }
  566. return 1;
  567. }
  568. /* Kills the bot. s1 is the reason shown to other bots,
  569. * s2 the reason shown on the partyline. (Sup 25Jul2001)
  570. */
  571. void kill_bot(char *s1, char *s2)
  572. {
  573. write_userfile(-1);
  574. if (!conf.bot->hub)
  575. server_die();
  576. chatout("*** %s\n", s1);
  577. botnet_send_chat(-1, conf.bot->nick, s1);
  578. botnet_send_bye(s2);
  579. fatal(s2, 0);
  580. }
  581. void
  582. readsocks(const char *fname)
  583. {
  584. /* Don't bother setting this if a hub
  585. ... it is only intended to prevent parting channels (in bot_shouldjoin())
  586. */
  587. if (!conf.bot->hub)
  588. restarting = 1;
  589. char *_botname = NULL, *ip4 = NULL, *ip6 = NULL,
  590. *_origbotname = NULL, *_jupenick = NULL;
  591. time_t old_buildts = 0, _server_online = 0;
  592. bool cached_005 = 0;
  593. const char salt1[] = SALT1;
  594. EncryptedStream stream(salt1);
  595. stream.loadFile(fname);
  596. bd::String str, type;
  597. reset_chans = 0;
  598. while (stream.tell() < stream.length()) {
  599. str = stream.getline().chomp();
  600. dprintf(DP_DEBUG, "read line: %s\n", str.c_str());
  601. type = newsplit(str);
  602. if (type == STR("-dcc"))
  603. dprintf(DP_DEBUG, STR("Added dcc: %d\n"), dcc_read(stream));
  604. else if (type == STR("-sock"))
  605. dprintf(DP_DEBUG, STR("Added fd: %d\n"), sock_read(stream));
  606. else if (type == STR("+online_since"))
  607. online_since = strtol(str.c_str(), NULL, 10);
  608. else if (type == STR("+server_online"))
  609. _server_online = strtol(str.c_str(), NULL, 10);
  610. else if (type == STR("+server_floodless"))
  611. floodless = 1;
  612. else if (type == STR("+in_deaf"))
  613. in_deaf = 1;
  614. else if (type == STR("+in_callerid"))
  615. in_callerid = 1;
  616. else if (type == STR("+chan")) {
  617. bd::String chname = str;
  618. channel_add(NULL, chname.c_str(), NULL);
  619. struct chanset_t* chan = findchan_by_dname(chname.c_str());
  620. strlcpy(chan->name, chan->dname, sizeof(chan->name));
  621. chan->status = chan->ircnet_status = 0;
  622. chan->ircnet_status |= CHAN_PEND;
  623. reset_chans = 2;
  624. }
  625. else if (type == STR("+buildts"))
  626. old_buildts = strtol(str.c_str(), NULL, 10);
  627. else if (type == STR("+botname"))
  628. _botname = str.dup();
  629. else if (type == STR("+origbotname"))
  630. _origbotname = str.dup();
  631. else if (type == STR("+jupenick"))
  632. _jupenick = str.dup();
  633. else if (type == STR("+rolls"))
  634. rolls = atoi(str.c_str());
  635. else if (type == STR("+altnick_char"))
  636. altnick_char = str[0];
  637. else if (type == STR("+burst"))
  638. burst = atoi(str.c_str());
  639. else if (type == STR("+flood_count"))
  640. flood_count = atoi(str.c_str());
  641. else if (type == STR("+my_cookie_counter")) {
  642. my_cookie_counter = strtol(str.c_str(), NULL, 10);
  643. my_cookie_counter += 100; // Increase to avoid race conditions
  644. }
  645. else if (type == STR("+ip4"))
  646. ip4 = str.dup();
  647. else if (type == STR("+ip6"))
  648. ip6 = str.dup();
  649. else if (type == STR("+serv_cache")) {
  650. if (!cached_005 && str.find(STR("005")))
  651. cached_005 = 1;
  652. dprintf(DP_CACHE, "%s", str.c_str());
  653. }
  654. }
  655. restart_time = now;
  656. if (old_buildts && buildts > old_buildts)
  657. restart_was_update = 1;
  658. tell_dcc(DP_DEBUG);
  659. tell_netdebug(DP_DEBUG);
  660. unlink(fname);
  661. /* server_online is not yet set so these will safely not send NICK. */
  662. if (_origbotname) {
  663. var_set_by_name(conf.bot->nick, "nick", _origbotname);
  664. }
  665. if (_jupenick) {
  666. var_set_by_name(conf.bot->nick, "jupenick", _jupenick);
  667. }
  668. if (servidx >= 0) {
  669. char nserv[50] = "";
  670. if ((ip4 && ip6) && (strcmp(ip4, myipstr(AF_INET)) || strcmp(ip6, myipstr(AF_INET6)))) {
  671. if (tands > 0) { /* We're not linked yet.. but for future */
  672. botnet_send_chat(-1, conf.bot->nick, STR("IP changed."));
  673. botnet_send_bye(STR("IP changed."));
  674. }
  675. fatal("brb", 1);
  676. } else if (conf.bot->hub) {
  677. // I became a hub during restart... disconnect from IRC.
  678. if (tands > 0) { /* We're not linked yet.. but for future */
  679. botnet_send_chat(-1, conf.bot->nick, STR("Changing to HUB."));
  680. botnet_send_bye(STR("Changing to HUB."));
  681. }
  682. nuke_server("emoquit");
  683. } else {
  684. simple_snprintf(nserv, sizeof(nserv), "%s:%d", dcc[servidx].host, dcc[servidx].port);
  685. add_server(nserv);
  686. curserv = 0;
  687. keepnick = 0; /* Wait to change nicks until relinking, fixes nick/jupenick switching issues during restart */
  688. reset_flood();
  689. if (!_server_online)
  690. _server_online = now;
  691. server_online = _server_online;
  692. rehash_server(dcc[servidx].host, _botname);
  693. if (cached_005)
  694. replay_cache(servidx, NULL);
  695. else
  696. dprintf(DP_DUMP, "VERSION\n");
  697. if (!reset_chans)
  698. reset_chans = 1;
  699. }
  700. }
  701. delete[] _botname;
  702. delete[] _origbotname;
  703. delete[] _jupenick;
  704. delete[] ip4;
  705. delete[] ip6;
  706. if (socksfile)
  707. free(socksfile);
  708. }
  709. /* Update system code
  710. */
  711. void
  712. restart(int idx)
  713. {
  714. const char *reason = updating ? STR("Updating...") : STR("Restarting...");
  715. Tempfile *socks = new Tempfile("socks");
  716. int fd = 0;
  717. sdprintf("%s", reason);
  718. if (tands > 0) {
  719. botnet_send_chat(-1, conf.bot->nick, (char *) reason);
  720. botnet_send_bye(reason);
  721. }
  722. /* Stop sharing to prevent writing LASTON to lostdcc() DCC_BOT sockets. */
  723. noshare = 1;
  724. /* kill all connections except STDOUT/server */
  725. for (fd = 0; fd < dcc_total; fd++) {
  726. if (dcc[fd].type && dcc[fd].type != &SERVER_SOCKET && dcc[fd].sock != STDOUT) {
  727. if (dcc[fd].sock >= 0)
  728. killsock(dcc[fd].sock);
  729. lostdcc(fd);
  730. }
  731. }
  732. const char salt1[] = SALT1;
  733. EncryptedStream stream(salt1);
  734. /* write out all leftover dcc[] entries */
  735. for (fd = 0; fd < dcc_total; fd++)
  736. if (dcc[fd].type && dcc[fd].sock != STDOUT)
  737. dcc_write(stream, fd);
  738. /* write out all leftover socklist[] entries */
  739. for (fd = 0; fd < MAXSOCKS; fd++)
  740. if (socklist[fd].sock != STDOUT)
  741. sock_write(stream, fd);
  742. if (server_online) {
  743. if (botname[0])
  744. stream << bd::String::printf(STR("+botname %s\n"), botname);
  745. if (origbotname[0])
  746. stream << bd::String::printf(STR("+origbotname %s\n"), origbotname);
  747. if (jupenick[0])
  748. stream << bd::String::printf(STR("+jupenick %s\n"), jupenick);
  749. if (rolls)
  750. stream << bd::String::printf(STR("+rolls %d\n"), rolls);
  751. if (altnick_char)
  752. stream << bd::String::printf(STR("+altnick_char %c\n"), altnick_char);
  753. if (burst)
  754. stream << bd::String::printf(STR("+burst %d\n"), burst);
  755. if (flood_count)
  756. stream << bd::String::printf(STR("+flood_count %d\n"), flood_count);
  757. if (my_cookie_counter)
  758. stream << bd::String::printf(STR("+my_cookie_counter %lu\n"), my_cookie_counter);
  759. stream << bd::String::printf(STR("+server_online %li\n"), (long)server_online);
  760. }
  761. stream << bd::String::printf(STR("+online_since %li\n"), (long)online_since);
  762. if (floodless)
  763. stream << bd::String::printf(STR("+server_floodless %d\n"), floodless);
  764. if (in_deaf)
  765. stream << bd::String::printf(STR("+in_deaf\n"));
  766. if (in_callerid)
  767. stream << bd::String::printf(STR("+in_callerid\n"));
  768. for (struct chanset_t *chan = chanset; chan; chan = chan->next)
  769. if (shouldjoin(chan) && (channel_active(chan) || channel_pending(chan)))
  770. stream << bd::String::printf(STR("+chan %s\n"), chan->dname);
  771. stream << bd::String::printf(STR("+buildts %li\n"), (long)buildts);
  772. stream << bd::String::printf(STR("+ip4 %s\n"), myipstr(AF_INET));
  773. stream << bd::String::printf(STR("+ip6 %s\n"), myipstr(AF_INET6));
  774. replay_cache(-1, &stream);
  775. stream.writeFile(socks->fd);
  776. socks->my_close();
  777. write_userfile(idx);
  778. /*
  779. if (server_online) {
  780. do_chanset(NULL, NULL, STR("+inactive"), DO_LOCAL);
  781. dprintf(DP_DUMP, STR("JOIN 0\n"));
  782. }
  783. */
  784. fixmod(binname);
  785. /* replace image now */
  786. char *argv[4] = { NULL, NULL, NULL, NULL };
  787. argv[0] = strdup(binname);
  788. if (!backgrd || term_z || sdebug) {
  789. char shit[7] = "";
  790. simple_snprintf(shit, sizeof(shit), STR("-%s%s%s"), !backgrd ? "n" : "", term_z ? "t" : "", sdebug ? "D" : "");
  791. argv[1] = strdup(shit);
  792. argv[2] = strdup(conf.bot->nick);
  793. } else {
  794. argv[1] = strdup(conf.bot->nick);
  795. }
  796. unlink(conf.bot->pid_file);
  797. FILE *fp = NULL;
  798. if (!(fp = fopen(conf.bot->pid_file, "w")))
  799. return;
  800. fprintf(fp, "%d %s\n", getpid(), socks->file);
  801. fclose(fp);
  802. execvp(argv[0], &argv[0]);
  803. /* hopefully this is never reached */
  804. putlog(LOG_MISC, "*", STR("Could not restart: %s"), strerror(errno));
  805. return;
  806. }
  807. #ifdef NO
  808. void
  809. hard_restart(int idx)
  810. {
  811. write_userfile(idx);
  812. if (!conf.bot->hub) {
  813. nuke_server((char *) reason); /* let's drop the server connection ASAP */
  814. cycle_time = 0;
  815. }
  816. fatal(idx <= 0x7FF0 ? reason : NULL, 1);
  817. usleep(2000 * 500);
  818. unlink(conf.bot->pid_file); /* if this fails it is ok, cron will restart the bot, *hopefully* */
  819. simple_exec(binname, conf.bot->nick);
  820. exit(0);
  821. }
  822. #endif
  823. int updatebin(int idx, char *par, int secs)
  824. {
  825. if (!par || !par[0]) {
  826. logidx(idx, "Not enough parameters.");
  827. return 1;
  828. }
  829. size_t path_siz = strlen(binname) + strlen(par) + 2;
  830. char *path = (char *) calloc(1, path_siz);
  831. char *newbin = NULL, buf[DIRMAX] = "";
  832. const char* argv[5];
  833. int i;
  834. strlcpy(path, binname, path_siz);
  835. newbin = strrchr(path, '/');
  836. if (!newbin) {
  837. free(path);
  838. logidx(idx, STR("Don't know current binary name"));
  839. return 1;
  840. }
  841. newbin++;
  842. if (strchr(par, '/')) {
  843. *newbin = 0;
  844. logidx(idx, STR("New binary must be in %s and name must be specified without path information"), path);
  845. free(path);
  846. return 1;
  847. }
  848. strcpy(newbin, par);
  849. if (!strcmp(path, binname)) {
  850. free(path);
  851. logidx(idx, STR("Can't update with the current binary"));
  852. return 1;
  853. }
  854. if (!can_stat(path)) {
  855. logidx(idx, STR("%s can't be accessed"), path);
  856. free(path);
  857. return 1;
  858. }
  859. if (fixmod(path)) {
  860. logidx(idx, STR("Can't set mode 0600 on %s"), path);
  861. free(path);
  862. return 1;
  863. }
  864. /* Check if the new binary is compatible */
  865. int initialized_code = check_bin_initialized(path);
  866. if (initialized_code == 2) {
  867. logidx(idx, STR("New binary is corrupted or the wrong architecture/operating system."));
  868. free(path);
  869. return 1;
  870. } else if (initialized_code == 1 && !check_bin_compat(path)) {
  871. logidx(idx, STR("New binary must be initialized as pack structure has been changed in new version."));
  872. free(path);
  873. return 1;
  874. }
  875. /* make a backup just in case. */
  876. simple_snprintf(buf, sizeof(buf), STR("%s/.bin.old"), conf.datadir);
  877. copyfile(binname, buf);
  878. write_settings(path, -1, 0, initialized_code ? 0 : 1); /* re-write the binary with our packdata */
  879. Tempfile *conffile = new Tempfile("conf");
  880. if (writeconf(NULL, conffile->fd, CONF_ENC)) {
  881. logidx(idx, STR("Failed to write temporary config file for update."));
  882. delete conffile;
  883. return 1;
  884. }
  885. /* The binary should return '2' when ran with -2, if not it's probably corrupt. */
  886. putlog(LOG_DEBUG, "*", STR("Running for update binary test: %s -2"), path);
  887. argv[0] = path;
  888. argv[1] = "-2";
  889. argv[2] = 0;
  890. i = simple_exec(argv);
  891. if (i == -1 || WEXITSTATUS(i) != 2) {
  892. logidx(idx, STR("Couldn't restart new binary (error %d)"), i);
  893. delete conffile;
  894. return i;
  895. }
  896. /* now to send our config to the new binary */
  897. putlog(LOG_DEBUG, "*", STR("Running for update conf: %s -4 %s"), path, conffile->file);
  898. argv[0] = path;
  899. argv[1] = "-4";
  900. argv[2] = conffile->file;
  901. argv[3] = 0;
  902. i = simple_exec(argv);
  903. delete conffile;
  904. if (i == -1 || WEXITSTATUS(i) != 6) { /* 6 for successfull config read/write */
  905. logidx(idx, STR("Couldn't pass config to new binary (error %d)"), i);
  906. return i;
  907. }
  908. if (movefile(path, binname)) {
  909. logidx(idx, STR("Can't rename %s to %s"), path, binname);
  910. free(path);
  911. return 1;
  912. }
  913. if (updating == UPDATE_EXIT) { /* dont restart/kill/spawn bots, just die ! */
  914. printf(STR("* Moved binary to: %s\n"), binname);
  915. fatal(STR("Binary updated."), 0);
  916. }
  917. if (updating == UPDATE_AUTO) {
  918. /* Make all other bots do a soft restart */
  919. conf_checkpids(conf.bots);
  920. conf_killbot(conf.bots, NULL, NULL, SIGHUP);
  921. if (conf.bot->pid)
  922. kill(conf.bot->pid, SIGHUP);
  923. exit(0);
  924. }
  925. if (!conf.bot->hub && secs > 0) {
  926. /* Make all other bots do a soft restart */
  927. conf_checkpids(conf.bots);
  928. conf_killbot(conf.bots, NULL, NULL, SIGHUP);
  929. /* invoked with -u */
  930. if (updating == UPDATE_AUTO) {
  931. if (conf.bot->pid)
  932. kill(conf.bot->pid, SIGHUP);
  933. exit(0);
  934. }
  935. /* this odd statement makes it so specifying 1 sec will restart other bots running
  936. * and then just restart with no delay */
  937. updating = UPDATE_AUTO;
  938. if (secs > 1) {
  939. egg_timeval_t howlong;
  940. howlong.sec = secs;
  941. howlong.usec = 0;
  942. timer_create_complex(&howlong, STR("restarting for update"), (Function) restart, (void *) (long) idx, 0);
  943. } else
  944. restart(idx);
  945. return 0;
  946. } else
  947. restart(idx); /* no timer */
  948. /* this should never be reached */
  949. return 2;
  950. }
  951. int bot_aggressive_to(struct userrec *u)
  952. {
  953. if (conf.bot->localhub) {
  954. for (conf_bot* bot = conf.bots; bot && bot->nick; bot = bot->next) {
  955. if (!bot->hub && !strcmp(u->handle, bot->nick))
  956. return 1;
  957. }
  958. }
  959. char mypval[HANDLEN + 3 + 1] = "", botpval[HANDLEN + 3 + 1] = "";
  960. link_pref_val(u, botpval);
  961. link_pref_val(conf.bot->u, mypval);
  962. // sdprintf("botpval: %s", botpval);
  963. // sdprintf("mypval: %s", mypval);
  964. if (strcmp(mypval, botpval) < 0)
  965. return 1;
  966. else
  967. return 0;
  968. }
  969. int goodpass(const char *pass, int idx, char *nick)
  970. {
  971. if (!pass[0])
  972. return 0;
  973. char tell[201] = "", last = 0;
  974. int nalpha = 0, lcase = 0, ucase = 0, ocase = 0, tc, repeats = 0, score = 0;
  975. size_t length = strlen(pass);
  976. if (strchr(BADPASSCHARS, pass[0])) {
  977. simple_snprintf(tell, sizeof(tell), "Passes may not begin with '-'");
  978. goto fail;
  979. }
  980. for (int i = 0; i < (signed) length; i++) {
  981. if (pass[i] == last)
  982. repeats++;
  983. tc = (int) pass[i];
  984. if (tc < 58 && tc > 47)
  985. ocase++; /* number */
  986. else if (tc < 91 && tc > 64)
  987. ucase++; /* upper case */
  988. else if (tc < 123 && tc > 96)
  989. lcase++; /* lower case */
  990. else
  991. nalpha++; /* non-alphabet/number */
  992. last = pass[i];
  993. }
  994. score += ocase * 3;
  995. score += nalpha * 2;
  996. score += ucase * 2;
  997. score += lcase * 1;
  998. score += length * 1;
  999. score -= repeats * 1;
  1000. simple_snprintf(tell, sizeof(tell), "Password NOT set due to being too weak. Try more characters, numbers, capitals");
  1001. if (score < 16) {
  1002. fail:
  1003. if (idx)
  1004. dprintf(idx, "%s\n", tell);
  1005. else if (nick[0])
  1006. notice(nick, tell, DP_HELP);
  1007. return 0;
  1008. }
  1009. return 1;
  1010. }
  1011. #define REPLACES 10
  1012. char *replace(const char *string, const char *oldie, const char *newbie)
  1013. {
  1014. if (string == NULL || !string[0])
  1015. return (char *) string;
  1016. if (oldie == NULL || !oldie[0])
  1017. return (char *) string;
  1018. char *c = NULL;
  1019. if ((c = (char *) strstr(string, oldie)) == NULL)
  1020. return (char *) string;
  1021. static int n = 0;
  1022. static char newstring_buf[REPLACES][1024];
  1023. char *newstring = newstring_buf[n++];
  1024. memset(newstring, 0, 1024);
  1025. if (n == REPLACES)
  1026. n = 0;
  1027. const size_t new_len = strlen(newbie), old_len = strlen(oldie), end = (strlen(string) - old_len);
  1028. size_t str_index = 0, newstr_index = 0, oldie_index = c - string, cpy_len;
  1029. while(str_index <= end && c != NULL) {
  1030. cpy_len = oldie_index-str_index;
  1031. strncpy(newstring + newstr_index, string + str_index, cpy_len);
  1032. newstr_index += cpy_len;
  1033. str_index += cpy_len;
  1034. strcpy(newstring + newstr_index, newbie);
  1035. newstr_index += new_len;
  1036. str_index += old_len;
  1037. if((c = (char *) strstr(string + str_index, oldie)) != NULL)
  1038. oldie_index = c - string;
  1039. }
  1040. strcpy(newstring + newstr_index, string + str_index);
  1041. return (newstring);
  1042. }
  1043. char* replace_vars(char *buf) {
  1044. return replace(buf, "$n", botname);
  1045. }
  1046. void showhelp(int idx, struct flag_record *flags, const char *string)
  1047. {
  1048. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  1049. size_t help_siz = strlen(string) + 1000 + 1;
  1050. char *helpstr = (char *) calloc(1, help_siz);
  1051. char tmp[2] = "", flagstr[10] = "";
  1052. bool ok = 1;
  1053. while (string && string[0]) {
  1054. if (*string == '%') {
  1055. if (!strncmp(string + 1, "{+", 2)) {
  1056. while (*string && *string != '+') {
  1057. string++;
  1058. }
  1059. flagstr[0] = 0;
  1060. while (*string && *string != '}') {
  1061. simple_snprintf(tmp, sizeof(tmp), "%c", *string);
  1062. strlcat(flagstr, tmp, sizeof(flagstr));
  1063. string++;
  1064. }
  1065. string++;
  1066. break_down_flags(flagstr, &fr, NULL);
  1067. if (flagrec_ok(&fr, flags)) {
  1068. ok = 1;
  1069. while (*string && *string != '%') {
  1070. simple_snprintf(tmp, sizeof(tmp), "%c", *string);
  1071. strlcat(helpstr, tmp, help_siz);
  1072. string++;
  1073. }
  1074. if (!strncmp(string + 1, "{-", 2)) {
  1075. ok = 1;
  1076. while (*string && *string != '}') {
  1077. string++;
  1078. }
  1079. string++;
  1080. }
  1081. } else {
  1082. ok = 0;
  1083. }
  1084. } else if (!strncmp(string + 1, "{-", 2)) {
  1085. ok = 1;
  1086. while (*string && *string != '}') {
  1087. string++;
  1088. }
  1089. string++;
  1090. } else if (*string == '{') {
  1091. while (*string && *string != '}') {
  1092. string++;
  1093. }
  1094. } else if (*(string + 1) == 'd') {
  1095. string += 2;
  1096. if (dcc[idx].u.chat->channel >= 0)
  1097. strlcat(helpstr, settings.dcc_prefix, help_siz);
  1098. } else if (*(string + 1) == '%') {
  1099. string += 2;
  1100. strlcat(helpstr, "%", help_siz);
  1101. } else {
  1102. if (ok) {
  1103. simple_snprintf(tmp, sizeof(tmp), "%c", *string);
  1104. strlcat(helpstr, tmp, help_siz);
  1105. }
  1106. string++;
  1107. }
  1108. } else {
  1109. if (ok) {
  1110. simple_snprintf(tmp, sizeof(tmp), "%c", *string);
  1111. strlcat(helpstr, tmp, help_siz);
  1112. }
  1113. string++;
  1114. }
  1115. }
  1116. helpstr[strlen(helpstr)] = 0;
  1117. if (helpstr[0]) dumplots(idx, "", helpstr);
  1118. free(helpstr);
  1119. }
  1120. /* Arrange the N elements of ARRAY in random order. */
  1121. void shuffleArray(char* array[], size_t n)
  1122. {
  1123. for (size_t i = 0; i < n; i++) {
  1124. const size_t j = randint(n);
  1125. char* temp = array[j];
  1126. array[j] = array[i];
  1127. array[i] = temp;
  1128. }
  1129. }
  1130. void shuffle(char *string, const char *delim, size_t str_len)
  1131. {
  1132. char *array[501], *str = NULL, *work = NULL;
  1133. size_t len = 0;
  1134. bzero(&array, sizeof array);
  1135. work = strdup(string);
  1136. str = strtok(work, delim);
  1137. while(str && *str)
  1138. {
  1139. array[len] = str;
  1140. len++;
  1141. str = strtok((char*) NULL, delim);
  1142. }
  1143. shuffleArray(array, len);
  1144. string[0] = 0;
  1145. for (size_t i = 0; i < len; i++) {
  1146. strlcat(string, array[i], str_len);
  1147. if (i != len - 1)
  1148. strlcat(string, delim, str_len);
  1149. }
  1150. free(work);
  1151. string[strlen(string)] = 0;
  1152. }
  1153. /* returns
  1154. 1: use ANSI
  1155. 2: use mIRC
  1156. 0: neither
  1157. */
  1158. int
  1159. coloridx(int idx)
  1160. {
  1161. if (idx == -1) { /* who cares, just show color! */
  1162. return 1; /* ANSI */
  1163. } else if (idx == -2) {
  1164. return 2; /* mIRC */
  1165. /* valid idx and NOT relaying */
  1166. } else if (idx >= 0) {
  1167. if (dcc[idx].irc || dcc[idx].bot) {
  1168. return 0;
  1169. } else if ((dcc[idx].status & STAT_COLOR) && (dcc[idx].type && dcc[idx].type != &DCC_RELAYING)) {
  1170. /* telnet probably wants ANSI, even though it might be a relay from an mIRC client */
  1171. if (dcc[idx].status & STAT_TELNET || dcc[idx].sock == STDOUT)
  1172. return 1;
  1173. /* non-telnet is probably a /dcc-chat, most irc clients support mIRC codes... */
  1174. else
  1175. return 2;
  1176. }
  1177. }
  1178. return 0;
  1179. }
  1180. const char *
  1181. color(int idx, int type, int which)
  1182. {
  1183. int ansi = 0;
  1184. /* if user is connected over TELNET or !backgrd, show ANSI
  1185. * if they are relaying, they are most likely on an IRC client and should have mIRC codes
  1186. */
  1187. if ((ansi = coloridx(idx)) == 0)
  1188. return "";
  1189. if (ansi == 2)
  1190. ansi = 0;
  1191. if (type == BOLD_OPEN) {
  1192. return ansi ? "\033[1m" : "\002";
  1193. } else if (type == BOLD_CLOSE) {
  1194. // return ansi ? "\033[22m" : "\002";
  1195. return ansi ? "\033[0m" : "\002";
  1196. } else if (type == UNDERLINE_OPEN) {
  1197. return ansi ? "\033[4m" : "\037";
  1198. } else if (type == UNDERLINE_CLOSE) {
  1199. return ansi ? "\033[24m" : "\037";
  1200. } else if (type == FLASH_OPEN) {
  1201. return ansi ? "\033[5m" : "\002\037";
  1202. } else if (type == FLASH_CLOSE) {
  1203. return ansi ? "\033[0m" : "\037\002";
  1204. } else if (type == COLOR_OPEN) {
  1205. switch (which) {
  1206. case C_BLACK: return ansi ? "\033[30m" : "\00301";
  1207. case C_RED: return ansi ? "\033[31m" : "\00305";
  1208. case C_GREEN: return ansi ? "\033[32m" : "\00303";
  1209. case C_BROWN: return ansi ? "\033[33m" : "\00307";
  1210. case C_BLUE: return ansi ? "\033[34m" : "\00302";
  1211. case C_PURPLE: return ansi ? "\033[35m" : "\00306";
  1212. case C_CYAN: return ansi ? "\033[36m" : "\00310";
  1213. case C_WHITE: return ansi ? "\033[1;37m" : "\00300";
  1214. case C_DARKGREY: return ansi ? "\033[1;30m" : "\00314";
  1215. case C_LIGHTRED: return ansi ? "\033[1;31m" : "\00304";
  1216. case C_LIGHTGREEN: return ansi ? "\033[1;32m" : "\00309";
  1217. case C_LIGHTBLUE: return ansi ? "\033[1;34m" : "\00312";
  1218. case C_LIGHTPURPLE: return ansi ? "\033[1;35m" : "\00313";
  1219. case C_LIGHTCYAN: return ansi ? "\033[1;36m" : "\00311";
  1220. case C_LIGHTGREY: return ansi ? "\033[37m" : "\00315";
  1221. case C_YELLOW: return ansi ? "\033[1;33m" : "\00308";
  1222. default: break;
  1223. }
  1224. } else if (type == COLOR_CLOSE) {
  1225. return ansi ? "\033[0m" : "\00300";
  1226. }
  1227. /* This should never be reached.. */
  1228. return "";
  1229. }
  1230. char *
  1231. strtolower(char *s)
  1232. {
  1233. char *p = s;
  1234. while (*p) {
  1235. *p = tolower(*p);
  1236. p++;
  1237. }
  1238. return s;
  1239. }
  1240. char *
  1241. strtoupper(char *s)
  1242. {
  1243. char *p = s;
  1244. while (*p) {
  1245. *p = toupper(*p);
  1246. p++;
  1247. }
  1248. return s;
  1249. }
  1250. char *step_thru_file(FILE *fd)
  1251. {
  1252. if (fd == NULL) {
  1253. return NULL;
  1254. }
  1255. char tempBuf[1024] = "", *retStr = NULL;
  1256. size_t ret_siz = 0;
  1257. while (!feof(fd)) {
  1258. if (fgets(tempBuf, sizeof(tempBuf), fd) && !feof(fd)) {
  1259. if (retStr == NULL) {
  1260. ret_siz = strlen(tempBuf) + 2;
  1261. retStr = (char *) calloc(1, ret_siz);
  1262. strlcpy(retStr, tempBuf, ret_siz);
  1263. } else {
  1264. ret_siz = strlen(retStr) + strlen(tempBuf);
  1265. retStr = (char *) realloc(retStr, ret_siz);
  1266. strlcat(retStr, tempBuf, ret_siz);
  1267. }
  1268. if (retStr[strlen(retStr)-1] == '\n') {
  1269. retStr[strlen(retStr)-1] = 0;
  1270. break;
  1271. }
  1272. }
  1273. }
  1274. return retStr;
  1275. }
  1276. char *trim(char *string)
  1277. {
  1278. if (string) {
  1279. char *ibuf = NULL, *obuf = NULL;
  1280. for (ibuf = obuf = string; *ibuf; ) {
  1281. while (*ibuf && (isspace (*ibuf)))
  1282. ibuf++;
  1283. if (*ibuf && (obuf != string))
  1284. *(obuf++) = ' ';
  1285. while (*ibuf && (!isspace (*ibuf)))
  1286. *(obuf++) = *(ibuf++);
  1287. }
  1288. *obuf = '\0';
  1289. }
  1290. return (string);
  1291. }
  1292. int skipline (char *line, int *skip) {
  1293. static int multi = 0;
  1294. if (strstr(line, "*/"))
  1295. multi = 0;
  1296. if ( (!strncmp(line, "#", 1)) || (!strncmp(line, ";", 1)) || (!strncmp(line, "//", 2)) ) {
  1297. (*skip)++;
  1298. } else if ( (strstr(line, "/*")) && (strstr(line, "*/")) ) {
  1299. multi = 0;
  1300. (*skip)++;
  1301. } else if ( (strstr(line, "/*")) ) {
  1302. (*skip)++;
  1303. multi = 1;
  1304. } else if ( (strstr(line, "*/")) ) {
  1305. multi = 0;
  1306. } else {
  1307. if (!multi) (*skip) = 0;
  1308. }
  1309. return (*skip);
  1310. }
  1311. /* vim: set sts=2 sw=2 ts=8 et: */