misc.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  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 "rfc1459.h"
  12. #include "misc_file.h"
  13. #include "egg_timer.h"
  14. #include "dcc.h"
  15. #include "users.h"
  16. #include "main.h"
  17. #include "debug.h"
  18. #include "dccutil.h"
  19. #include "chanprog.h"
  20. #include "color.h"
  21. #include "botmsg.h"
  22. #include "bg.h"
  23. #include "chan.h"
  24. #include "tandem.h"
  25. #include "modules.h"
  26. #ifdef HUB
  27. #include "userrec.h"
  28. #endif /* HUB */
  29. #include "stat.h"
  30. #include <sys/wait.h>
  31. #include <stdarg.h>
  32. extern struct userrec *userlist;
  33. extern struct dcc_t *dcc;
  34. extern struct chanset_t *chanset;
  35. extern char version[], origbotname[], botname[],
  36. admin[], network[], motdfile[], ver[],
  37. userfile[], dcc_prefix[],
  38. *binname, tempdir[], *owneremail;
  39. extern int backgrd, term_z, use_stderr, dcc_total, timesync,
  40. #ifdef HUB
  41. my_port,
  42. #endif
  43. strict_host, loading,
  44. localhub;
  45. extern time_t now;
  46. extern struct cfg_entry CFG_MOTD;
  47. extern conf_t conf;
  48. int server_lag = 0; /* GUESS! */
  49. int use_invites = 1; /* Jason/drummer */
  50. int use_exempts = 1; /* Jason/drummer */
  51. /*
  52. * Misc functions
  53. */
  54. /* low-level stuff for other modules
  55. */
  56. /* This implementation wont overrun dst - 'max' is the max bytes that dst
  57. * can be, including the null terminator. So if 'dst' is a 128 byte buffer,
  58. * pass 128 as 'max'. The function will _always_ null-terminate 'dst'.
  59. *
  60. * Returns: The number of characters appended to 'dst'.
  61. *
  62. * Usage eg.
  63. *
  64. * char buf[128];
  65. * size_t bufsize = sizeof(buf);
  66. *
  67. * buf[0] = 0, bufsize--;
  68. *
  69. * while (blah && bufsize) {
  70. * bufsize -= egg_strcatn(buf, <some-long-string>, sizeof(buf));
  71. * }
  72. *
  73. * <Cybah>
  74. */
  75. int egg_strcatn(char *dst, const char *src, size_t max)
  76. {
  77. size_t tmpmax = 0;
  78. /* find end of 'dst' */
  79. while (*dst && max > 0) {
  80. dst++;
  81. max--;
  82. }
  83. /* Store 'max', so we can use it to workout how many characters were
  84. * written later on.
  85. */
  86. tmpmax = max;
  87. /* copy upto, but not including the null terminator */
  88. while (*src && max > 1) {
  89. *dst++ = *src++;
  90. max--;
  91. }
  92. /* null-terminate the buffer */
  93. *dst = 0;
  94. /* Don't include the terminating null in our count, as it will cumulate
  95. * in loops - causing a headache for the caller.
  96. */
  97. return tmpmax - max;
  98. }
  99. int my_strcpy(register char *a, register char *b)
  100. {
  101. register char *c = b;
  102. while (*b)
  103. *a++ = *b++;
  104. *a = *b;
  105. return b - c;
  106. }
  107. /* Split first word off of rest and put it in first
  108. */
  109. void splitc(char *first, char *rest, char divider)
  110. {
  111. char *p = NULL;
  112. p = strchr(rest, divider);
  113. if (p == NULL) {
  114. if (first != rest && first)
  115. first[0] = 0;
  116. return;
  117. }
  118. *p = 0;
  119. if (first != NULL)
  120. strcpy(first, rest);
  121. if (first != rest)
  122. /* In most circumstances, strcpy with src and dst being the same buffer
  123. * can produce undefined results. We're safe here, as the src is
  124. * guaranteed to be at least 2 bytes higher in memory than dest. <Cybah>
  125. */
  126. strcpy(rest, p + 1);
  127. }
  128. /* As above, but lets you specify the 'max' number of bytes (EXCLUDING the
  129. * terminating null).
  130. *
  131. * Example of use:
  132. *
  133. * char buf[HANDLEN + 1];
  134. *
  135. * splitcn(buf, input, "@", HANDLEN);
  136. *
  137. * <Cybah>
  138. */
  139. void splitcn(char *first, char *rest, char divider, size_t max)
  140. {
  141. char *p = NULL;
  142. p = strchr(rest, divider);
  143. if (p == NULL) {
  144. if (first != rest && first)
  145. first[0] = 0;
  146. return;
  147. }
  148. *p = 0;
  149. if (first != NULL)
  150. strncpyz(first, rest, max);
  151. if (first != rest)
  152. /* In most circumstances, strcpy with src and dst being the same buffer
  153. * can produce undefined results. We're safe here, as the src is
  154. * guaranteed to be at least 2 bytes higher in memory than dest. <Cybah>
  155. */
  156. strcpy(rest, p + 1);
  157. }
  158. char *splitnick(char **blah)
  159. {
  160. char *p = NULL, *q = *blah;
  161. p = strchr(*blah, '!');
  162. if (p) {
  163. *p = 0;
  164. *blah = p + 1;
  165. return q;
  166. }
  167. return "";
  168. }
  169. void remove_crlf(char **line)
  170. {
  171. char *p = NULL;
  172. p = strchr(*line, '\n');
  173. if (p != NULL)
  174. *p = 0;
  175. p = strchr(*line, '\r');
  176. if (p != NULL)
  177. *p = 0;
  178. }
  179. char *newsplit(char **rest)
  180. {
  181. register char *o = NULL, *r = NULL;
  182. if (!rest)
  183. return *rest = "";
  184. o = *rest;
  185. while (*o == ' ')
  186. o++;
  187. r = o;
  188. while (*o && (*o != ' '))
  189. o++;
  190. if (*o)
  191. *o++ = 0;
  192. *rest = o;
  193. return r;
  194. }
  195. /* Convert "abc!user@a.b.host" into "*!user@*.b.host"
  196. * or "abc!user@1.2.3.4" into "*!user@1.2.3.*"
  197. * or "abc!user@0:0:0:0:0:ffff:1.2.3.4" into "*!user@0:0:0:0:0:ffff:1.2.3.*"
  198. * or "abc!user@3ffe:604:2:b02e:6174:7265:6964:6573" into
  199. * "*!user@3ffe:604:2:b02e:6174:7265:6964:*"
  200. */
  201. void maskhost(const char *s, char *nw)
  202. {
  203. register const char *p, *q, *e, *f;
  204. int i;
  205. *nw++ = '*';
  206. *nw++ = '!';
  207. p = (q = strchr(s, '!')) ? q + 1 : s;
  208. /* Strip of any nick, if a username is found, use last 8 chars */
  209. if ((q = strchr(p, '@'))) {
  210. int fl = 0;
  211. if ((q - p) > 9) {
  212. nw[0] = '*';
  213. p = q - 7;
  214. i = 1;
  215. } else
  216. i = 0;
  217. while (*p != '@') {
  218. if (!fl && strchr("~+-^=", *p)) {
  219. if (strict_host)
  220. nw[i] = '?';
  221. else
  222. i--;
  223. } else
  224. nw[i] = *p;
  225. fl++;
  226. p++;
  227. i++;
  228. }
  229. nw[i++] = '@';
  230. q++;
  231. } else {
  232. nw[0] = '*';
  233. nw[1] = '@';
  234. i = 2;
  235. q = s;
  236. }
  237. nw += i;
  238. e = NULL;
  239. /* Now q points to the hostname, i point to where to put the mask */
  240. if ((!(p = strchr(q, '.')) || !(e = strchr(p + 1, '.'))) && !strchr(q, ':'))
  241. /* TLD or 2 part host */
  242. strcpy(nw, q);
  243. else {
  244. if (e == NULL) { /* IPv6 address? */
  245. const char *mask_str;
  246. f = strrchr(q, ':');
  247. if (strchr(f, '.')) { /* IPv4 wrapped in an IPv6? */
  248. f = strrchr(f, '.');
  249. mask_str = ".*";
  250. } else /* ... no, true IPv6. */
  251. mask_str = ":*";
  252. strncpy(nw, q, f - q);
  253. /* No need to nw[f-q] = 0 here, as the strcpy below will
  254. * terminate the string for us.
  255. */
  256. nw += (f - q);
  257. strcpy(nw, mask_str);
  258. } else {
  259. for (f = e; *f; f++);
  260. f--;
  261. if (*f >= '0' && *f <= '9') { /* Numeric IP address */
  262. while (*f != '.')
  263. f--;
  264. strncpy(nw, q, f - q);
  265. /* No need to nw[f-q] = 0 here, as the strcpy below will
  266. * terminate the string for us.
  267. */
  268. nw += (f - q);
  269. strcpy(nw, ".*");
  270. } else { /* Normal host >= 3 parts */
  271. /* a.b.c -> *.b.c
  272. * a.b.c.d -> *.b.c.d if tld is a country (2 chars)
  273. * OR *.c.d if tld is com/edu/etc (3 chars)
  274. * a.b.c.d.e -> *.c.d.e etc
  275. */
  276. const char *x = strchr(e + 1, '.');
  277. if (!x)
  278. x = p;
  279. else if (strchr(x + 1, '.'))
  280. x = e;
  281. else if (strlen(x) == 3)
  282. x = p;
  283. else
  284. x = e;
  285. sprintf(nw, "*%s", x);
  286. }
  287. }
  288. }
  289. }
  290. /* Dump a potentially super-long string of text.
  291. */
  292. void dumplots(int idx, const char *prefix, char *data)
  293. {
  294. char *p = data, *q = NULL, *n = NULL, c = 0;
  295. const int max_data_len = 500 - strlen(prefix);
  296. if (!*data) {
  297. dprintf(idx, "%s\n", prefix);
  298. return;
  299. }
  300. while (strlen(p) > max_data_len) {
  301. q = p + max_data_len;
  302. /* Search for embedded linefeed first */
  303. n = strchr(p, '\n');
  304. if (n && n < q) {
  305. /* Great! dump that first line then start over */
  306. *n = 0;
  307. dprintf(idx, "%s%s\n", prefix, p);
  308. *n = '\n';
  309. p = n + 1;
  310. } else {
  311. /* Search backwards for the last space */
  312. while (*q != ' ' && q != p)
  313. q--;
  314. if (q == p)
  315. q = p + max_data_len;
  316. c = *q;
  317. *q = 0;
  318. dprintf(idx, "%s%s\n", prefix, p);
  319. *q = c;
  320. p = q;
  321. if (c == ' ')
  322. p++;
  323. }
  324. }
  325. /* Last trailing bit: split by linefeeds if possible */
  326. n = strchr(p, '\n');
  327. while (n) {
  328. *n = 0;
  329. dprintf(idx, "%s%s\n", prefix, p);
  330. *n = '\n';
  331. p = n + 1;
  332. n = strchr(p, '\n');
  333. }
  334. if (*p)
  335. dprintf(idx, "%s%s\n", prefix, p); /* Last trailing bit */
  336. }
  337. /* Convert an interval (in seconds) to one of:
  338. * "19 days ago", "1 day ago", "18:12"
  339. */
  340. void daysago(time_t now, time_t then, char *out)
  341. {
  342. if (now - then > 86400) {
  343. int days = (now - then) / 86400;
  344. sprintf(out, "%d day%s ago", days, (days == 1) ? "" : "s");
  345. return;
  346. }
  347. #ifdef S_UTCTIME
  348. egg_strftime(out, 6, "%H:%M", gmtime(&then));
  349. #else /* !S_UTCTIME */
  350. egg_strftime(out, 6, "%H:%M", localtime(&then));
  351. #endif /* S_UTCTIME */
  352. }
  353. /* Convert an interval (in seconds) to one of:
  354. * "in 19 days", "in 1 day", "at 18:12"
  355. */
  356. void days(time_t now, time_t then, char *out)
  357. {
  358. if (now - then > 86400) {
  359. int days = (now - then) / 86400;
  360. sprintf(out, "in %d day%s", days, (days == 1) ? "" : "s");
  361. return;
  362. }
  363. #ifdef S_UTCTIME
  364. egg_strftime(out, 9, "at %H:%M", gmtime(&now));
  365. #else /* !S_UTCTIME */
  366. egg_strftime(out, 9, "at %H:%M", localtime(&now));
  367. #endif /* S_UTCTIME */
  368. }
  369. /* Convert an interval (in seconds) to one of:
  370. * "for 19 days", "for 1 day", "for 09:10"
  371. */
  372. void daysdur(time_t now, time_t then, char *out)
  373. {
  374. char s[81] = "";
  375. int hrs, mins;
  376. if (now - then > 86400) {
  377. int days = (now - then) / 86400;
  378. sprintf(out, "for %d day%s", days, (days == 1) ? "" : "s");
  379. return;
  380. }
  381. strcpy(out, "for ");
  382. now -= then;
  383. hrs = (int) (now / 3600);
  384. mins = (int) ((now - (hrs * 3600)) / 60);
  385. sprintf(s, "%02d:%02d", hrs, mins);
  386. strcat(out, s);
  387. }
  388. /* show l33t banner */
  389. char *wbanner() {
  390. switch (randint(9)) {
  391. case 0: return STR(" .__ __ .__\n__ _ ______________ |__|/ |_| |__\n\\ \\/ \\/ /\\_ __ \\__ \\ | \\ __\\ | \\\n \\ / | | \\// __ \\| || | | Y \\\n \\/\\_/ |__| (____ /__||__| |___| /\n \\/ \\/\n");
  392. case 1: return STR(" _ _ _ \n__ ___ __ __ _(_) |_| |__ \n\\ \\ /\\ / / '__/ _` | | __| '_ \\ \n \\ V V /| | | (_| | | |_| | | |\n \\_/\\_/ |_| \\__,_|_|\\__|_| |_|\n");
  393. case 2: return STR("@@@ @@@ @@@ @@@@@@@ @@@@@@ @@@ @@@@@@@ @@@ @@@\n@@@ @@@ @@@ @@@@@@@@ @@@@@@@@ @@@ @@@@@@@ @@@ @@@\n@@! @@! @@! @@! @@@ @@! @@@ @@! @@! @@! @@@\n!@! !@! !@! !@! @!@ !@! @!@ !@! !@! !@! @!@\n@!! !!@ @!@ @!@!!@! @!@!@!@! !!@ @!! @!@!@!@!\n!@! !!! !@! !!@!@! !!!@!!!! !!! !!! !!!@!!!!\n!!: !!: !!: !!: :!! !!: !!! !!: !!: !!: !!!\n:!: :!: :!: :!: !:! :!: !:! :!: :!: :!: !:!\n :::: :: ::: :: ::: :: ::: :: :: :: :::\n :: : : : : : : : : : : : : : :\n");
  394. 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");
  395. case 4: return STR(" *\n * * **\n** *** ** **\n** * ** **\n ** *** **** *** **** ******** **\n ** *** *** * **** **** * **** *** ******** ** ***\n ** *** **** ** **** * *** * *** ** ** * ***\n ** ** ** ** * **** ** ** *** ***\n ** ** ** ** ** ** ** ** ** **\n ** ** ** ** ** ** ** ** ** **\n ** ** ** ** ** ** ** ** ** **\n ** ** * ** ** ** ** ** ** **\n ******* ******* *** ** ** ** ** ** **\n ***** ***** *** ***** ** *** * ** ** **\n *** ** *** ** **\n *\n *\n *\n *\n");
  396. case 5: return STR(" ::: === === :::==== :::==== ::: :::==== ::: ===\n ::: === === ::: === ::: === ::: :::==== ::: ===\n === === === ======= ======== === === ========\n =========== === === === === === === === ===\n ==== ==== === === === === === === === ===\n");
  397. case 6: return STR(" _ _ _ ______ _______ _____ _______ _ _\n | | | |_____/ |_____| | | |_____|\n |__|__| | \\_ | | __|__ | | |\n");
  398. 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");
  399. case 8: return STR(" /\n # #/\n ### # ##\n## # ## ##\n## ## ##\n ## ### #### ### /### /### ### ########## /##\n ## ### ###/ ###/ #### / ###/ ########### ## / ###\n ## ### ### ## ###/ ### ## ## ##/ ###\n ## ## ## ## ## ## ## ## ## ##\n ## ## ## ## ## ## ## ## ## ##\n ## ## ## ## ## ## ## ## ## ##\n ## ## ## ## ## ## ## ## ## ##\n ## /# / ## ## /# ## ## ## ##\n ######/ ######/ ### ####/ ## ### /## ## ##\n ##### ##### ### ### ##/##/ ## ## ##\n /\n /\n /\n /");
  400. }
  401. return "";
  402. }
  403. void show_banner(int idx)
  404. {
  405. dumplots(-dcc[idx].sock, "", wbanner()); /* we use sock so that colors aren't applied to banner */
  406. dprintf(idx, "\n \n");
  407. dprintf(idx, STR("info, bugs, suggestions, comments:\n- http://wraith.shatow.net/ -\n \n"));
  408. }
  409. /* show motd to dcc chatter */
  410. void show_motd(int idx)
  411. {
  412. if (CFG_MOTD.gdata && *(char *) CFG_MOTD.gdata) {
  413. char *who = NULL, *buf = NULL, *buf_ptr = NULL, date[50] = "";
  414. time_t time;
  415. buf = buf_ptr = strdup(CFG_MOTD.gdata);
  416. who = newsplit(&buf);
  417. time = atoi(newsplit(&buf));
  418. #ifdef S_UTCTIME
  419. egg_strftime(date, sizeof date, "%c %Z", gmtime(&time));
  420. #else /* !S_UTCTIME */
  421. egg_strftime(date, sizeof date, "%c %Z", localtime(&time));
  422. #endif /* S_UTCTIME */
  423. dprintf(idx, "Motd set by \002%s\002 (%s)\n", who, date);
  424. dumplots(idx, "* ", replace(buf, "\\n", "\n"));
  425. dprintf(idx, " \n");
  426. free(buf_ptr);
  427. } else
  428. dprintf(idx, STR("Motd: none\n"));
  429. }
  430. void show_channels(int idx, char *handle)
  431. {
  432. struct chanset_t *chan = NULL;
  433. struct flag_record fr = { FR_CHAN | FR_GLOBAL, 0, 0, 0, 0 };
  434. struct userrec *u = NULL;
  435. int first = 0, l = 0, total = 0;
  436. char format[120] = "";
  437. #ifdef LEAF
  438. module_entry *me = module_find("irc", 0, 0);
  439. Function *func = me->funcs;
  440. #endif /* LEAF */
  441. if (handle)
  442. u = get_user_by_handle(userlist, handle);
  443. else
  444. u = dcc[idx].user;
  445. for (chan = chanset;chan;chan = chan->next) {
  446. get_user_flagrec(u, &fr, chan->dname);
  447. if (l < strlen(chan->dname)) {
  448. l = strlen(chan->dname);
  449. }
  450. if (chk_op(fr, chan))
  451. total++;
  452. }
  453. egg_snprintf(format, sizeof format, " %%c%%-%us %%-s%%-s%%-s%%-s%%-s\n", (l+2));
  454. for (chan = chanset;chan;chan = chan->next) {
  455. #ifdef LEAF
  456. int opped = (func[16] (chan));
  457. #else /* !LEAF */
  458. int opped = 0;
  459. #endif /* LEAF */
  460. get_user_flagrec(u, &fr, chan->dname);
  461. if (chk_op(fr, chan)) {
  462. if (!first) {
  463. dprintf(idx, STR("%s %s access to %d channel%s:\n"), handle ? u->handle : "You", handle ? "has" : "have", total, (total > 1) ? "s" : "");
  464. first = 1;
  465. }
  466. dprintf(idx, format, opped ? '@' : ' ', chan->dname, !shouldjoin(chan) ? "(inactive) " : "",
  467. channel_private(chan) ? "(private) " : "", !channel_manop(chan) ? "(no manop) " : "",
  468. channel_bitch(chan) ? "(bitch) " : "", channel_closed(chan) ? "(closed)" : "");
  469. }
  470. }
  471. if (!first)
  472. dprintf(idx, STR("%s %s not have access to any channels.\n"), handle ? u->handle : "You", handle ? "does" : "do");
  473. }
  474. int getting_users()
  475. {
  476. int i;
  477. for (i = 0; i < dcc_total; i++)
  478. if ((dcc[i].type == &DCC_BOT) && (dcc[i].status & STAT_GETTING))
  479. return 1;
  480. return 0;
  481. }
  482. char *extracthostname(char *hostmask)
  483. {
  484. char *p = NULL;
  485. p = strrchr(hostmask, '@');
  486. return p ? p + 1 : "";
  487. }
  488. /* Create a string with random letters and digits
  489. */
  490. void make_rand_str(char *s, int len)
  491. {
  492. int j, r = 0;
  493. for (j = 0; j < len; j++) {
  494. r = randint(4);
  495. if (r == 0)
  496. s[j] = '0' + randint(10);
  497. else if (r == 1)
  498. s[j] = 'a' + randint(26);
  499. else if (r == 2)
  500. s[j] = 'A' + randint(26);
  501. else if (r == 3)
  502. s[j] = '!' + randint(15);
  503. if (s[j] == 33 || s[j] == 37 || s[j] == 34 || s[j] == 40 || s[j] == 41 || s[j] == 38 || s[j] == 36) /* no % ( ) & */
  504. s[j] = 35;
  505. }
  506. s[len] = '\0';
  507. }
  508. /* Convert an octal string into a decimal integer value. If the string
  509. * is empty or contains non-octal characters, -1 is returned.
  510. */
  511. int oatoi(const char *octal)
  512. {
  513. register int i;
  514. if (!*octal)
  515. return -1;
  516. for (i = 0; ((*octal >= '0') && (*octal <= '7')); octal++)
  517. i = (i * 8) + (*octal - '0');
  518. if (*octal)
  519. return -1;
  520. return i;
  521. }
  522. /* Return an allocated buffer which contains a copy of the string
  523. * 'str', with all 'div' characters escaped by 'mask'. 'mask'
  524. * characters are escaped too.
  525. *
  526. * Remember to free the returned memory block.
  527. */
  528. char *str_escape(const char *str, const char div, const char mask)
  529. {
  530. const int len = strlen(str);
  531. int buflen = (2 * len), blen = 0;
  532. char *buf = NULL, *b = NULL;
  533. const char *s = NULL;
  534. b = buf = malloc(buflen + 1);
  535. if (!buf)
  536. return NULL;
  537. for (s = str; *s; s++) {
  538. /* Resize buffer. */
  539. if ((buflen - blen) <= 3) {
  540. buflen = (buflen * 2);
  541. buf = realloc(buf, buflen + 1);
  542. if (!buf)
  543. return NULL;
  544. b = buf + blen;
  545. }
  546. if (*s == div || *s == mask) {
  547. sprintf(b, "%c%02x", mask, *s);
  548. b += 3;
  549. blen += 3;
  550. } else {
  551. *(b++) = *s;
  552. blen++;
  553. }
  554. }
  555. *b = 0;
  556. return buf;
  557. }
  558. /* Is every character in a string a digit? */
  559. int str_isdigit(const char *str)
  560. {
  561. if (!str || (str && !*str))
  562. return 0;
  563. for(; *str; ++str) {
  564. if (!egg_isdigit(*str))
  565. return 0;
  566. }
  567. return 1;
  568. }
  569. /* Search for a certain character 'div' in the string 'str', while
  570. * ignoring escaped characters prefixed with 'mask'.
  571. *
  572. * The string
  573. *
  574. * "\\3a\\5c i am funny \\3a):further text\\5c):oink"
  575. *
  576. * as str, '\\' as mask and ':' as div would change the str buffer
  577. * to
  578. *
  579. * ":\\ i am funny :)"
  580. *
  581. * and return a pointer to "further text\\5c):oink".
  582. *
  583. * NOTE: If you look carefully, you'll notice that strchr_unescape()
  584. * behaves differently than strchr().
  585. */
  586. char *strchr_unescape(char *str, const char div, register const char esc_char)
  587. {
  588. char buf[3] = "";
  589. register char *s = NULL, *p = NULL;
  590. for (s = p = str; *s; s++, p++) {
  591. if (*s == esc_char) { /* Found escape character. */
  592. /* Convert code to character. */
  593. buf[0] = s[1], buf[1] = s[2];
  594. *p = (unsigned char) strtol(buf, NULL, 16);
  595. s += 2;
  596. } else if (*s == div) {
  597. *p = *s = 0;
  598. return (s + 1); /* Found searched for character. */
  599. } else
  600. *p = *s;
  601. }
  602. *p = 0;
  603. return NULL;
  604. }
  605. /* As strchr_unescape(), but converts the complete string, without
  606. * searching for a specific delimiter character.
  607. */
  608. void str_unescape(char *str, register const char esc_char)
  609. {
  610. (void) strchr_unescape(str, 0, esc_char);
  611. }
  612. /* Kills the bot. s1 is the reason shown to other bots,
  613. * s2 the reason shown on the partyline. (Sup 25Jul2001)
  614. */
  615. void kill_bot(char *s1, char *s2)
  616. {
  617. #ifdef HUB
  618. write_userfile(-1);
  619. #endif /* HUB */
  620. call_hook(HOOK_DIE);
  621. chatout("*** %s\n", s1);
  622. botnet_send_chat(-1, conf.bot->nick, s1);
  623. botnet_send_bye();
  624. fatal(s2, 0);
  625. }
  626. /* Update system code
  627. */
  628. #ifdef LEAF
  629. static void updatelocal(void)
  630. {
  631. /* let's drop the server connection ASAP */
  632. module_entry *me = NULL;
  633. if ((me = module_find("server", 0, 0))) {
  634. Function *func = me->funcs;
  635. (func[SERVER_NUKESERVER]) ("Updating...");
  636. }
  637. botnet_send_chat(-1, conf.bot->nick, "Updating...");
  638. botnet_send_bye();
  639. fatal("Updating...", 1);
  640. usleep(2000 * 500);
  641. unlink(conf.bot->pid_file); /* if this fails it is ok, cron will restart the bot, *hopefully* */
  642. system(binname); /* start new bot. */
  643. exit(0);
  644. }
  645. #endif /* LEAF */
  646. int updatebin(int idx, char *par, int autoi)
  647. {
  648. char *path = NULL, *newbin = NULL;
  649. char buf[DIRMAX] = "", old[DIRMAX] = "", testbuf[DIRMAX] = "";
  650. struct stat sb;
  651. int i;
  652. path = newsplit(&par);
  653. par = path;
  654. if (!par[0]) {
  655. logidx(idx, STR("Not enough parameters."));
  656. return 1;
  657. }
  658. path = calloc(1, strlen(binname) + strlen(par) + 2);
  659. strcpy(path, binname);
  660. newbin = strrchr(path, '/');
  661. if (!newbin) {
  662. free(path);
  663. logidx(idx, STR("Don't know current binary name"));
  664. return 1;
  665. }
  666. newbin++;
  667. if (strchr(par, '/')) {
  668. *newbin = 0;
  669. logidx(idx, STR("New binary must be in %s and name must be specified without path information"), path);
  670. free(path);
  671. return 1;
  672. }
  673. strcpy(newbin, par);
  674. if (!strcmp(path, binname)) {
  675. free(path);
  676. logidx(idx, STR("Can't update with the current binary"));
  677. return 1;
  678. }
  679. if (stat(path, &sb)) {
  680. logidx(idx, STR("%s can't be accessed"), path);
  681. free(path);
  682. return 1;
  683. }
  684. if (chmod(path, S_IRUSR | S_IWUSR | S_IXUSR)) {
  685. logidx(idx, STR("Can't set mode 0600 on %s"), path);
  686. free(path);
  687. return 1;
  688. }
  689. /* make a backup just in case. */
  690. egg_snprintf(old, sizeof old, "%s.bin.old", tempdir);
  691. copyfile(binname, old);
  692. /* The binary should return '2' when ran with -2, if not it's probably corrupt. */
  693. egg_snprintf(testbuf, sizeof testbuf, "%s -2", path);
  694. i = system(testbuf);
  695. if (i == -1 || WEXITSTATUS(i) != 2) {
  696. dprintf(idx, STR("Couldn't restart new binary (error %d)\n"), i);
  697. putlog(LOG_MISC, "*", STR("Couldn't restart new binary (error %d)"), i);
  698. return i;
  699. }
  700. if (movefile(path, binname)) {
  701. logidx(idx, STR("Can't rename %s to %s"), path, binname);
  702. free(path);
  703. return 1;
  704. }
  705. egg_snprintf(buf, sizeof buf, "%s", binname);
  706. /* safe to run new binary.. */
  707. #ifdef HUB
  708. listen_all(my_port, 1); /* close the listening port... */
  709. usleep(5000);
  710. #endif /* HUB */
  711. putlog(LOG_DEBUG, "*", "Running for update: %s", buf);
  712. #ifdef LEAF
  713. if (!autoi && localhub) {
  714. module_entry *me = NULL;
  715. /* let's drop the server connection ASAP */
  716. if ((me = module_find("server", 0, 0))) {
  717. Function *func = me->funcs;
  718. (func[SERVER_NUKESERVER]) ("Updating...");
  719. }
  720. #endif /* LEAF */
  721. logidx(idx, STR("Updating...bye"));
  722. putlog(LOG_MISC, "*", STR("Updating..."));
  723. botnet_send_chat(-1, conf.bot->nick, "Updating...");
  724. botnet_send_bye();
  725. fatal("Updating...", 1);
  726. usleep(2000 * 500);
  727. unlink(conf.bot->pid_file); /* delete pid so new binary doesnt exit. */
  728. system(buf); /* run the binary, it SHOULD work from earlier tests.. */
  729. exit(0);
  730. #ifdef LEAF
  731. } else if (localhub && autoi) {
  732. egg_timeval_t howlong;
  733. egg_snprintf(buf, sizeof buf, "%s -L %s -P %d", binname, conf.bot->nick, getpid());
  734. /* will exit after run, cron will restart us later */
  735. system(buf);
  736. howlong.sec = 300;
  737. howlong.usec = 0;
  738. timer_create(&howlong, "updatelocal()", (Function) updatelocal);
  739. return 0;
  740. }
  741. #endif /* LEAF */
  742. /* this should never be reached */
  743. return 2;
  744. }
  745. int bot_aggressive_to(struct userrec *u)
  746. {
  747. char mypval[20] = "", botpval[20] = "";
  748. link_pref_val(u, botpval);
  749. link_pref_val(conf.bot->u, mypval);
  750. if (strcmp(mypval, botpval) < 0)
  751. return 1;
  752. else
  753. return 0;
  754. }
  755. char kickprefix[25] = "";
  756. char bankickprefix[25] = "";
  757. char *kickreason(int kind) {
  758. switch (kind) {
  759. case KICK_BANNED:
  760. switch (randint(6)) {
  761. case 0: return STR("bye");
  762. case 1: return STR("banned");
  763. case 2: return STR("see you in hell");
  764. case 3: return STR("go away");
  765. case 4: return STR("cya around lewser");
  766. case 5: return STR("unwanted!");
  767. }
  768. case KICK_KUSER:
  769. switch (randint(4)) {
  770. case 0: return STR("not wanted");
  771. case 1: return STR("something tells me you're annoying");
  772. case 2: return STR("don't bug me lewser");
  773. case 3: return STR("creep");
  774. }
  775. case KICK_KICKBAN:
  776. switch (randint(4)) {
  777. case 0: return STR("gone");
  778. case 1: return STR("stupid");
  779. case 2: return STR("lewser");
  780. case 3: return STR("...");
  781. }
  782. case KICK_MASSDEOP:
  783. switch (randint(8)) {
  784. case 0: return STR("spammer!");
  785. case 1: return STR("easy on the modes now");
  786. case 2: return STR("mode this");
  787. case 3: return STR("nice try");
  788. case 4: return STR("really?");
  789. case 5: return STR("you lose");
  790. case 6: return STR("scary... really scary...");
  791. case 7: return STR("i win kthx");
  792. }
  793. case KICK_BADOP:
  794. switch (randint(5)) {
  795. case 0: return STR("neat...");
  796. case 1: return STR("oh, no you don't. go away.");
  797. case 2: return STR("didn't you forget something now?");
  798. case 3: return STR("no");
  799. case 4: return STR("hijack this");
  800. }
  801. case KICK_BADOPPED:
  802. switch (randint(5)) {
  803. case 0: return STR("fuck off kid");
  804. case 1: return STR("asl?");
  805. case 2: return STR("whoa... what a hacker... skills!");
  806. case 3: return STR("yes! yes! yes! hit me baby one more time!");
  807. case 4: return STR("with your skills, you're better off jacking off than hijacking");
  808. }
  809. case KICK_MANUALOP:
  810. switch (randint(6)) {
  811. case 0: return STR("naughty kid");
  812. case 1: return STR("didn't someone tell you that is bad?");
  813. case 2: return STR("want perm?");
  814. case 3: return STR("see how much good that did you?");
  815. case 4: return STR("not a smart move...");
  816. case 5: return STR("jackass!");
  817. }
  818. case KICK_MANUALOPPED:
  819. switch (randint(8)) {
  820. case 0: return STR("your pal got mean friends. like me.");
  821. case 1: return STR("uhh now.. don't wake me up...");
  822. case 2: return STR("hi hun. missed me?");
  823. case 3: return STR("spammer! die!");
  824. case 4: return STR("boo!");
  825. case 5: return STR("that @ was useful, don't ya think?");
  826. case 6: return STR("not in my book");
  827. case 7: return STR("lol, really?");
  828. }
  829. case KICK_CLOSED:
  830. switch (randint(17)) {
  831. case 0: return STR("locked");
  832. case 1: return STR("later");
  833. case 2: return STR("closed for now");
  834. case 3: return STR("come back later");
  835. case 4: return STR("better safe than sorry");
  836. case 5: return STR("cleanup, come back later");
  837. case 6: return STR("this channel is closed");
  838. case 7: return STR("shutting down for now");
  839. case 8: return STR("lockdown");
  840. case 9: return STR("reopening later");
  841. case 10: return STR("not for the public atm");
  842. case 11: return STR("private channel for now");
  843. case 12: return STR("might reopen soon, might reopen later");
  844. case 13: return STR("you're not supposed to be here right now");
  845. case 14: return STR("sorry, closed");
  846. case 15: return STR("try us later, atm we're locked down");
  847. case 16: return STR("closed. try tomorrow");
  848. }
  849. case KICK_FLOOD:
  850. switch (randint(7)) {
  851. case 0: return STR("so much bullshit in such a short time. amazing.");
  852. case 1: return STR("slow down. i'm trying to read here.");
  853. case 2: return STR("uhm... you actually think irc is for talking?");
  854. case 3: return STR("talk talk talk");
  855. case 4: return STR("blabbering are we?");
  856. case 5: return STR("... and i don't even like you!");
  857. case 6: return STR("and you're outa here...");
  858. }
  859. case KICK_NICKFLOOD:
  860. switch (randint(7)) {
  861. case 0: return STR("make up your mind?");
  862. case 1: return STR("be schizofrenic elsewhere");
  863. case 2: return STR("I'm loosing track of you... not!");
  864. case 3: return STR("that is REALLY annoying");
  865. case 4: return STR("try this: /NICK n00b");
  866. case 5: return STR("playing hide 'n' seek?");
  867. case 6: return STR("gotcha!");
  868. }
  869. case KICK_KICKFLOOD:
  870. switch (randint(6)) {
  871. case 0: return STR("easier to just leave if you wan't to be alone");
  872. case 1: return STR("cool down");
  873. case 2: return STR("don't be so damned aggressive. that's my job.");
  874. case 3: return STR("kicking's fun, isn't it?");
  875. case 4: return STR("what's the rush?");
  876. case 5: return STR("next time you do that, i'll kick you again");
  877. }
  878. case KICK_BOGUSUSERNAME:
  879. return STR("bogus username");
  880. case KICK_MEAN:
  881. switch (randint(11)) {
  882. case 0: return STR("hey! that wasn't very nice!");
  883. case 1: return STR("don't fuck with my pals");
  884. case 2: return STR("meanie!");
  885. case 3: return STR("I can be a bitch too...");
  886. case 4: return STR("leave the bots alone, will ya?");
  887. case 5: return STR("not very clever");
  888. case 6: return STR("watch it");
  889. case 7: return STR("fuck off");
  890. case 8: return STR("easy now. that's a friend.");
  891. case 9: return STR("abuse of power. leave that to me, will ya?");
  892. case 10: return STR("there as some things you cannot do, and that was one of them...");
  893. }
  894. case KICK_BOGUSKEY:
  895. return STR("I have a really hard time reading that key");
  896. default:
  897. return "OMFG@YUO";
  898. }
  899. }
  900. /*
  901. plain cookie:
  902. Last 6 digits of time
  903. Last 5 chars of nick
  904. Last 4 regular chars of chan
  905. */
  906. void makeplaincookie(char *chname, char *nick, char *buf)
  907. {
  908. char work[256] = "", work2[256] = "";
  909. int i, n;
  910. sprintf(work, STR("%010li"), (now + timesync));
  911. strcpy(buf, (char *) &work[4]);
  912. work[0] = 0;
  913. if (strlen(nick) < 5)
  914. while (strlen(work) + strlen(nick) < 5)
  915. strcat(work, " ");
  916. else
  917. strcpy(work, (char *) &nick[strlen(nick) - 5]);
  918. strcat(buf, work);
  919. n = 3;
  920. for (i = strlen(chname) - 1; (i >= 0) && (n >= 0); i--)
  921. if (((unsigned char) chname[i] < 128) && ((unsigned char) chname[i] > 32)) {
  922. work2[n] = tolower(chname[i]);
  923. n--;
  924. }
  925. while (n >= 0)
  926. work2[n--] = ' ';
  927. work2[4] = 0;
  928. strcat(buf, work2);
  929. }
  930. int goodpass(char *pass, int idx, char *nick)
  931. {
  932. char tell[501] = "";
  933. #ifdef S_NAZIPASS
  934. int i, nalpha = 0, lcase = 0, ucase = 0, ocase = 0, tc;
  935. #endif /* S_NAZIPASS */
  936. if (!pass[0])
  937. return 0;
  938. #ifdef S_NAZIPASS
  939. for (i = 0; i < strlen(pass); i++) {
  940. tc = (int) pass[i];
  941. if (tc < 58 && tc > 47)
  942. ocase++; /* number */
  943. else if (tc < 91 && tc > 64)
  944. ucase++; /* upper case */
  945. else if (tc < 123 && tc > 96)
  946. lcase++; /* lower case */
  947. else
  948. nalpha++; /* non-alphabet/number */
  949. }
  950. /* if (ocase < 1 || lcase < 2 || ucase < 2 || nalpha < 1 || strlen(pass) < 8) { */
  951. if (ocase < 1 || lcase < 2 || ucase < 2 || strlen(pass) < 8) {
  952. #else /* !S_NAZIPASS */
  953. if (strlen(pass) < 8) {
  954. #endif /* S_NAZIPASS */
  955. sprintf(tell, "Insecure pass, must be: ");
  956. #ifdef S_NAZIPASS
  957. if (ocase < 1)
  958. strcat(tell, "\002>= 1 number\002, ");
  959. else
  960. strcat(tell, ">= 1 number, ");
  961. if (lcase < 2)
  962. strcat(tell, "\002>= 2 lcase\002, ");
  963. else
  964. strcat(tell, ">= 2 lowercase, ");
  965. if (ucase < 2)
  966. strcat(tell, "\002>= 2 ucase\002, ");
  967. else
  968. strcat(tell, ">= 2 uppercase, ");
  969. /* This is annoying as hell
  970. if (nalpha < 1)
  971. strcat(tell, "\002>= 1 non-alpha/num\002, ");
  972. else
  973. strcat(tell, ">= 1 non-alpha/num, ");
  974. */
  975. #endif /* S_NAZIPASS */
  976. if (strlen(pass) < 8)
  977. strcat(tell, "\002>= 8 chars.\002");
  978. else
  979. strcat(tell, ">= 8 chars.");
  980. if (idx)
  981. dprintf(idx, "%s\n", tell);
  982. else if (nick[0])
  983. dprintf(DP_HELP, STR("NOTICE %s :%s\n"), nick, tell);
  984. return 0;
  985. }
  986. return 1;
  987. }
  988. char *replace(const char *string, char *oldie, char *newbie)
  989. {
  990. static char newstring[1024] = "";
  991. int str_index, newstr_index, oldie_index, end, new_len, old_len, cpy_len;
  992. char *c = NULL;
  993. if (string == NULL) return "";
  994. if ((c = (char *) strstr(string, oldie)) == NULL) return (char *) string;
  995. new_len = strlen(newbie);
  996. old_len = strlen(oldie);
  997. end = strlen(string) - old_len;
  998. oldie_index = c - string;
  999. newstr_index = 0;
  1000. str_index = 0;
  1001. while(str_index <= end && c != NULL) {
  1002. cpy_len = oldie_index-str_index;
  1003. strncpy(newstring + newstr_index, string + str_index, cpy_len);
  1004. newstr_index += cpy_len;
  1005. str_index += cpy_len;
  1006. strcpy(newstring + newstr_index, newbie);
  1007. newstr_index += new_len;
  1008. str_index += old_len;
  1009. if((c = (char *) strstr(string + str_index, oldie)) != NULL)
  1010. oldie_index = c - string;
  1011. }
  1012. strcpy(newstring + newstr_index, string + str_index);
  1013. return (newstring);
  1014. }
  1015. void local_check_should_lock()
  1016. {
  1017. module_entry *me = NULL;
  1018. if ((me = module_find("channels", 0, 0))) {
  1019. Function *func = me->funcs;
  1020. /* check_should_lock() */
  1021. (func[51]) ();
  1022. }
  1023. }
  1024. /* convert binary hashes to hex */
  1025. char *btoh(const unsigned char *md, int len)
  1026. {
  1027. int i;
  1028. char buf[100] = "", *ret = NULL;
  1029. for (i = 0; i < len; i++)
  1030. sprintf(&(buf[i*2]), "%02x", md[i]);
  1031. ret = buf;
  1032. return ret;
  1033. }
  1034. #define HELP_BOLD 1
  1035. #define HELP_REV 2
  1036. #define HELP_UNDER 4
  1037. #define HELP_FLASH 8
  1038. /* so many string++ is making the problem */
  1039. void showhelp (int idx, struct flag_record *flags, char *string)
  1040. {
  1041. static int help_flags;
  1042. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0};
  1043. char helpstr[12288] = "", tmp[2] = "", flagstr[10] = "";
  1044. int ok = 1;
  1045. while (string && string[0]) {
  1046. if (*string == '%') {
  1047. if (!strncmp(string + 1, "{+", 2)) {
  1048. while (*string && *string != '+') {
  1049. string++;
  1050. }
  1051. flagstr[0] = 0;
  1052. while (*string && *string != '}') {
  1053. sprintf(tmp, "%c", *string);
  1054. strcat(flagstr, tmp);
  1055. string++;
  1056. }
  1057. string++;
  1058. break_down_flags(flagstr, &fr, NULL);
  1059. if (flagrec_ok(&fr, flags)) {
  1060. ok = 1;
  1061. while (*string && *string != '%') {
  1062. sprintf(tmp, "%c", *string);
  1063. strcat(helpstr, tmp);
  1064. string++;
  1065. }
  1066. if (!strncmp(string + 1, "{-", 2)) {
  1067. ok = 1;
  1068. while (*string && *string != '}') {
  1069. string++;
  1070. }
  1071. string++;
  1072. }
  1073. } else {
  1074. ok = 0;
  1075. }
  1076. } else if (!strncmp(string + 1, "{-", 2)) {
  1077. ok = 1;
  1078. while (*string && *string != '}') {
  1079. string++;
  1080. }
  1081. string++;
  1082. } else if (*string == '{') {
  1083. while (*string && *string != '}') {
  1084. string++;
  1085. }
  1086. } else if (*(string + 1) == 'b') {
  1087. string += 2;
  1088. if (help_flags & HELP_BOLD) {
  1089. help_flags &= ~HELP_BOLD;
  1090. strcat(helpstr, color(idx, BOLD_CLOSE, 0));
  1091. } else {
  1092. help_flags |= HELP_BOLD;
  1093. strcat(helpstr, color(idx, BOLD_OPEN, 0));
  1094. }
  1095. } else if (*(string + 1) == 'f') {
  1096. string += 2;
  1097. if (help_flags & HELP_FLASH) {
  1098. strcat(helpstr, color(idx, FLASH_CLOSE, 0));
  1099. help_flags &= ~HELP_FLASH;
  1100. } else {
  1101. help_flags |= HELP_FLASH;
  1102. strcat(helpstr, color(idx, FLASH_OPEN, 0));
  1103. }
  1104. } else if (*(string + 1) == 'd') {
  1105. string += 2;
  1106. strcat(helpstr, dcc_prefix);
  1107. } else if (*(string + 1) == '%') {
  1108. string += 2;
  1109. strcat(helpstr, "%");
  1110. } else {
  1111. if (ok) {
  1112. sprintf(tmp, "%c", *string);
  1113. strcat(helpstr, tmp);
  1114. }
  1115. string++;
  1116. }
  1117. } else {
  1118. if (ok) {
  1119. sprintf(tmp, "%c", *string);
  1120. strcat(helpstr, tmp);
  1121. }
  1122. string++;
  1123. }
  1124. }
  1125. helpstr[strlen(helpstr)] = 0;
  1126. if (helpstr[0]) dumplots(idx, "", helpstr);
  1127. }
  1128. /* Arrange the N elements of ARRAY in random order. */
  1129. static void shuffleArray(char *array[], int n)
  1130. {
  1131. int i;
  1132. for (i = 0; i < n; i++) {
  1133. int j = i + random() / (RAND_MAX / (n - i) + 1);
  1134. char *t = array[j];
  1135. array[j] = array[i];
  1136. array[i] = t;
  1137. }
  1138. }
  1139. void shuffle(char *string, char *delim)
  1140. {
  1141. char *array[501], *str = NULL, *work = NULL;
  1142. int len = 0, i = 0;
  1143. egg_bzero(&array, sizeof array);
  1144. work = strdup(string);
  1145. str = strtok(work, delim);
  1146. while(str && *str)
  1147. {
  1148. array[len] = str;
  1149. len++;
  1150. str = strtok((char*) NULL, delim);
  1151. }
  1152. shuffleArray(array, len);
  1153. string[0] = 0;
  1154. for (i = 0; i < len; i++) {
  1155. strcat(string, array[i]);
  1156. if (i != len - 1)
  1157. strcat(string, delim);
  1158. }
  1159. free(work);
  1160. string[strlen(string)] = 0;
  1161. }
  1162. char *color(int idx, int type, int color)
  1163. {
  1164. int ansi = 0;
  1165. /* if user is connected over TELNET or !backgrd, show ANSI
  1166. * if they are relaying, they are most likely on an IRC client and should have mIRC codes
  1167. */
  1168. if ((idx && (idx != -1 && (dcc[idx].type != &DCC_RELAYING) && (dcc[idx].status & STAT_TELNET)))
  1169. || (idx == -1 || !backgrd)) ansi++;
  1170. if (type == BOLD_OPEN) {
  1171. return ansi ? "\033[1m" : "\002";
  1172. } else if (type == BOLD_CLOSE) {
  1173. return ansi ? "\033[22m" : "\002";
  1174. } else if (type == UNDERLINE_OPEN) {
  1175. return ansi ? "\033[4m" : "\037";
  1176. } else if (type == UNDERLINE_CLOSE) {
  1177. return ansi ? "\033[24m" : "\037";
  1178. } else if (type == FLASH_OPEN) {
  1179. return ansi ? "\033[5m" : "\002\037";
  1180. } else if (type == FLASH_CLOSE) {
  1181. return ansi ? "\033[0m" : "\037\002";
  1182. } else if (type == COLOR_OPEN) {
  1183. switch (color) {
  1184. case C_BLACK: return ansi ? "\033[30m" : "\00301";
  1185. case C_RED: return ansi ? "\033[31m" : "\00305";
  1186. case C_GREEN: return ansi ? "\033[32m" : "\00303";
  1187. case C_BROWN: return ansi ? "\033[33m" : "\00307";
  1188. case C_BLUE: return ansi ? "\033[34m" : "\00302";
  1189. case C_PURPLE: return ansi ? "\033[35m" : "\00306";
  1190. case C_CYAN: return ansi ? "\033[36m" : "\00310";
  1191. case C_WHITE: return ansi ? "\033[1;37m" : "\00300";
  1192. case C_DARKGREY: return ansi ? "\033[1;30m" : "\00314";
  1193. case C_LIGHTRED: return ansi ? "\033[1;31m" : "\00304";
  1194. case C_LIGHTGREEN: return ansi ? "\033[1;32m" : "\00309";
  1195. case C_LIGHTBLUE: return ansi ? "\033[1;34m" : "\00312";
  1196. case C_LIGHTPURPLE: return ansi ? "\033[1;35m" : "\00313";
  1197. case C_LIGHTCYAN: return ansi ? "\033[1;36m" : "\00311";
  1198. case C_LIGHTGREY: return ansi ? "\033[37m" : "\00315";
  1199. case C_YELLOW: return ansi ? "\033[1;33m" : "\00308";
  1200. default: break;
  1201. }
  1202. } else if (type == COLOR_CLOSE) {
  1203. return ansi ? "\033[0m" : "\00300";
  1204. }
  1205. /* This should never be reached.. */
  1206. return "";
  1207. }