misc.c 34 KB

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