misc.c 36 KB

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