misc.c 32 KB

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