misc.c 33 KB

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