1
0

misc.c 36 KB

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