misc.c 37 KB

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