misc.c 33 KB

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