misc.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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. p = strchr(*line, '\n');
  159. if (p != NULL)
  160. *p = 0;
  161. p = strchr(*line, '\r');
  162. if (p != NULL)
  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. #ifdef S_UTCTIME
  334. egg_strftime(out, 6, "%H:%M", gmtime(&then));
  335. #else /* !S_UTCTIME */
  336. egg_strftime(out, 6, "%H:%M", localtime(&then));
  337. #endif /* S_UTCTIME */
  338. }
  339. /* Convert an interval (in seconds) to one of:
  340. * "in 19 days", "in 1 day", "at 18:12"
  341. */
  342. void days(time_t mynow, time_t then, char *out)
  343. {
  344. if (mynow - then > 86400) {
  345. int mydays = (mynow - then) / 86400;
  346. sprintf(out, "in %d day%s", mydays, (mydays == 1) ? "" : "s");
  347. return;
  348. }
  349. #ifdef S_UTCTIME
  350. egg_strftime(out, 9, "at %H:%M", gmtime(&now));
  351. #else /* !S_UTCTIME */
  352. egg_strftime(out, 9, "at %H:%M", localtime(&now));
  353. #endif /* S_UTCTIME */
  354. }
  355. /* Convert an interval (in seconds) to one of:
  356. * "for 19 days", "for 1 day", "for 09:10"
  357. */
  358. void daysdur(time_t mynow, time_t then, char *out)
  359. {
  360. char s[81] = "";
  361. int hrs, mins;
  362. if (mynow - then > 86400) {
  363. int mydays = (mynow - then) / 86400;
  364. sprintf(out, "for %d day%s", mydays, (mydays == 1) ? "" : "s");
  365. return;
  366. }
  367. strcpy(out, "for ");
  368. mynow -= then;
  369. hrs = (int) (mynow / 3600);
  370. mins = (int) ((mynow - (hrs * 3600)) / 60);
  371. sprintf(s, "%02d:%02d", hrs, mins);
  372. strcat(out, s);
  373. }
  374. /* show l33t banner */
  375. static char *fuckyou = " _ _ _ __ _ ____\n| | | (_) / _|_ _ ___| | __ _ _ ___ _ _ _| _ \\\n| |_| | | | |_| | | |/ __| |/ / | | | |/ _ \\| | | | (_) | | |\n| _ | |_ | _| |_| | (__| < | |_| | (_) | |_| | _| |_| |\n|_| |_|_( ) |_| \\__,_|\\___|_|\\_\\ \\__, |\\___/ \\__,_| (_)____/\n |/ |___/\n";
  376. char *wbanner() {
  377. if (fuckyou) {
  378. ;
  379. }
  380. switch (randint(9)) {
  381. case 0: return STR(" .__ __ .__\n__ _ ______________ |__|/ |_| |__\n\\ \\/ \\/ /\\_ __ \\__ \\ | \\ __\\ | \\\n \\ / | | \\// __ \\| || | | Y \\\n \\/\\_/ |__| (____ /__||__| |___| /\n \\/ \\/\n");
  382. case 1: return STR(" _ _ _ \n__ ___ __ __ _(_) |_| |__ \n\\ \\ /\\ / / '__/ _` | | __| '_ \\ \n \\ V V /| | | (_| | | |_| | | |\n \\_/\\_/ |_| \\__,_|_|\\__|_| |_|\n");
  383. case 2: return STR("@@@ @@@ @@@ @@@@@@@ @@@@@@ @@@ @@@@@@@ @@@ @@@\n@@@ @@@ @@@ @@@@@@@@ @@@@@@@@ @@@ @@@@@@@ @@@ @@@\n@@! @@! @@! @@! @@@ @@! @@@ @@! @@! @@! @@@\n!@! !@! !@! !@! @!@ !@! @!@ !@! !@! !@! @!@\n@!! !!@ @!@ @!@!!@! @!@!@!@! !!@ @!! @!@!@!@!\n!@! !!! !@! !!@!@! !!!@!!!! !!! !!! !!!@!!!!\n!!: !!: !!: !!: :!! !!: !!! !!: !!: !!: !!!\n:!: :!: :!: :!: !:! :!: !:! :!: :!: :!: !:!\n :::: :: ::: :: ::: :: ::: :: :: :: :::\n :: : : : : : : : : : : : : : :\n");
  384. 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");
  385. case 4: return STR(" *\n * * **\n** *** ** **\n** * ** **\n ** *** **** *** **** ******** **\n ** *** *** * **** **** * **** *** ******** ** ***\n ** *** **** ** **** * *** * *** ** ** * ***\n ** ** ** ** * **** ** ** *** ***\n ** ** ** ** ** ** ** ** ** **\n ** ** ** ** ** ** ** ** ** **\n ** ** ** ** ** ** ** ** ** **\n ** ** * ** ** ** ** ** ** **\n ******* ******* *** ** ** ** ** ** **\n ***** ***** *** ***** ** *** * ** ** **\n *** ** *** ** **\n *\n *\n *\n *\n");
  386. case 5: return STR(" ::: === === :::==== :::==== ::: :::==== ::: ===\n ::: === === ::: === ::: === ::: :::==== ::: ===\n === === === ======= ======== === === ========\n =========== === === === === === === === ===\n ==== ==== === === === === === === === ===\n");
  387. case 6: return STR(" _ _ _ ______ _______ _____ _______ _ _\n | | | |_____/ |_____| | | |_____|\n |__|__| | \\_ | | __|__ | | |\n");
  388. 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");
  389. case 8: return STR(" /\n # #/\n ### # ##\n## # ## ##\n## ## ##\n ## ### #### ### /### /### ### ########## /##\n ## ### ###/ ###/ #### / ###/ ########### ## / ###\n ## ### ### ## ###/ ### ## ## ##/ ###\n ## ## ## ## ## ## ## ## ## ##\n ## ## ## ## ## ## ## ## ## ##\n ## ## ## ## ## ## ## ## ## ##\n ## ## ## ## ## ## ## ## ## ##\n ## /# / ## ## /# ## ## ## ##\n ######/ ######/ ### ####/ ## ### /## ## ##\n ##### ##### ### ### ##/##/ ## ## ##\n /\n /\n /\n /");
  390. }
  391. return "";
  392. }
  393. void show_banner(int idx)
  394. {
  395. dumplots(-dcc[idx].sock, "", wbanner()); /* we use sock so that colors aren't applied to banner */
  396. dprintf(idx, "\n \n");
  397. dprintf(idx, STR("%sConributions welcomed by paypal: bryan@shatow.net%s\n"), BOLD(idx), BOLD_END(idx));
  398. dprintf(idx, STR("info, bugs, suggestions, comments:\n- http://wraith.shatow.net/ -\n \n"));
  399. }
  400. /* show motd to dcc chatter */
  401. void show_motd(int idx)
  402. {
  403. if (CFG_MOTD.gdata && *(char *) CFG_MOTD.gdata) {
  404. char *who = NULL, *buf = NULL, *buf_ptr = NULL, date[50] = "";
  405. time_t when;
  406. buf = buf_ptr = strdup(CFG_MOTD.gdata);
  407. who = newsplit(&buf);
  408. when = atoi(newsplit(&buf));
  409. #ifdef S_UTCTIME
  410. egg_strftime(date, sizeof date, "%c %Z", gmtime(&when));
  411. #else /* !S_UTCTIME */
  412. egg_strftime(date, sizeof date, "%c %Z", localtime(&when));
  413. #endif /* S_UTCTIME */
  414. dprintf(idx, "Motd set by %s%s%s (%s)\n", BOLD(idx), who, BOLD_END(idx), date);
  415. dumplots(idx, "* ", replace(buf, "\\n", "\n"));
  416. dprintf(idx, " \n");
  417. free(buf_ptr);
  418. } else
  419. dprintf(idx, "Motd: none\n");
  420. }
  421. void show_channels(int idx, char *handle)
  422. {
  423. struct chanset_t *chan = NULL;
  424. struct flag_record fr = { FR_CHAN | FR_GLOBAL, 0, 0};
  425. struct userrec *u = NULL;
  426. int first = 0, total = 0;
  427. size_t l = 0;
  428. char format[120] = "";
  429. if (handle)
  430. u = get_user_by_handle(userlist, handle);
  431. else
  432. u = dcc[idx].user;
  433. for (chan = chanset;chan;chan = chan->next) {
  434. get_user_flagrec(u, &fr, chan->dname);
  435. if (l < strlen(chan->dname)) {
  436. l = strlen(chan->dname);
  437. }
  438. if (chk_op(fr, chan))
  439. total++;
  440. }
  441. egg_snprintf(format, sizeof format, " %%c%%-%us %%-s%%-s%%-s%%-s%%-s\n", (l+2));
  442. for (chan = chanset;chan;chan = chan->next) {
  443. int opped = 0;
  444. #ifdef LEAF
  445. opped = me_op(chan);
  446. #endif /* LEAF */
  447. get_user_flagrec(u, &fr, chan->dname);
  448. if (chk_op(fr, chan)) {
  449. if (!first) {
  450. dprintf(idx, "%s %s access to %d channel%s:\n", handle ? u->handle : "You", handle ? "has" : "have", total, (total > 1) ? "s" : "");
  451. first = 1;
  452. }
  453. dprintf(idx, format, opped ? '@' : ' ', chan->dname, !shouldjoin(chan) ? "(inactive) " : "",
  454. channel_private(chan) ? "(private) " : "", !channel_manop(chan) ? "(no manop) " : "",
  455. channel_bitch(chan) ? "(bitch) " : "", channel_closed(chan) ? "(closed)" : "");
  456. }
  457. }
  458. if (!first)
  459. dprintf(idx, "%s %s not have access to any channels.\n", handle ? u->handle : "You", handle ? "does" : "do");
  460. }
  461. /* Create a string with random letters and digits
  462. */
  463. void make_rand_str(char *s, int len)
  464. {
  465. int j, r = 0;
  466. for (j = 0; j < len; j++) {
  467. r = randint(4);
  468. if (r == 0)
  469. s[j] = '0' + randint(10);
  470. else if (r == 1)
  471. s[j] = 'a' + randint(26);
  472. else if (r == 2)
  473. s[j] = 'A' + randint(26);
  474. else if (r == 3)
  475. s[j] = '!' + randint(15);
  476. if (s[j] == 33 || s[j] == 37 || s[j] == 34 || s[j] == 40 || s[j] == 41 || s[j] == 38 || s[j] == 36) /* no % ( ) & */
  477. s[j] = 35;
  478. }
  479. s[len] = '\0';
  480. }
  481. /* Return an allocated buffer which contains a copy of the string
  482. * 'str', with all 'div' characters escaped by 'mask'. 'mask'
  483. * characters are escaped too.
  484. *
  485. * Remember to free the returned memory block.
  486. */
  487. char *str_escape(const char *str, const char divc, const char mask)
  488. {
  489. const int len = strlen(str);
  490. int buflen = (2 * len), blen = 0;
  491. char *buf = NULL, *b = NULL;
  492. const char *s = NULL;
  493. b = buf = calloc(1, buflen + 1);
  494. if (!buf)
  495. return NULL;
  496. for (s = str; *s; s++) {
  497. /* Resize buffer. */
  498. if ((buflen - blen) <= 3) {
  499. buflen = (buflen * 2);
  500. buf = realloc(buf, buflen + 1);
  501. if (!buf)
  502. return NULL;
  503. b = buf + blen;
  504. }
  505. if (*s == divc || *s == mask) {
  506. sprintf(b, "%c%02x", mask, *s);
  507. b += 3;
  508. blen += 3;
  509. } else {
  510. *(b++) = *s;
  511. blen++;
  512. }
  513. }
  514. *b = 0;
  515. return buf;
  516. }
  517. /* Is every character in a string a digit? */
  518. int str_isdigit(const char *str)
  519. {
  520. if (!str || (str && !*str))
  521. return 0;
  522. for(; *str; ++str) {
  523. if (!egg_isdigit(*str))
  524. return 0;
  525. }
  526. return 1;
  527. }
  528. /* Search for a certain character 'div' in the string 'str', while
  529. * ignoring escaped characters prefixed with 'mask'.
  530. *
  531. * The string
  532. *
  533. * "\\3a\\5c i am funny \\3a):further text\\5c):oink"
  534. *
  535. * as str, '\\' as mask and ':' as div would change the str buffer
  536. * to
  537. *
  538. * ":\\ i am funny :)"
  539. *
  540. * and return a pointer to "further text\\5c):oink".
  541. *
  542. * NOTE: If you look carefully, you'll notice that strchr_unescape()
  543. * behaves differently than strchr().
  544. */
  545. char *strchr_unescape(char *str, const char divc, register const char esc_char)
  546. {
  547. char buf[3] = "";
  548. register char *s = NULL, *p = NULL;
  549. for (s = p = str; *s; s++, p++) {
  550. if (*s == esc_char) { /* Found escape character. */
  551. /* Convert code to character. */
  552. buf[0] = s[1], buf[1] = s[2];
  553. *p = (unsigned char) strtol(buf, NULL, 16);
  554. s += 2;
  555. } else if (*s == divc) {
  556. *p = *s = 0;
  557. return (s + 1); /* Found searched for character. */
  558. } else
  559. *p = *s;
  560. }
  561. *p = 0;
  562. return NULL;
  563. }
  564. /* As strchr_unescape(), but converts the complete string, without
  565. * searching for a specific delimiter character.
  566. */
  567. __inline__ void str_unescape(char *str, register const char esc_char)
  568. {
  569. (void) strchr_unescape(str, 0, esc_char);
  570. }
  571. /* Kills the bot. s1 is the reason shown to other bots,
  572. * s2 the reason shown on the partyline. (Sup 25Jul2001)
  573. */
  574. void kill_bot(char *s1, char *s2)
  575. {
  576. #ifdef HUB
  577. write_userfile(-1);
  578. #endif /* HUB */
  579. #ifdef LEAF
  580. server_die();
  581. #endif /* LEAF */
  582. chatout("*** %s\n", s1);
  583. botnet_send_chat(-1, conf.bot->nick, s1);
  584. botnet_send_bye();
  585. fatal(s2, 0);
  586. }
  587. /* Update system code
  588. */
  589. #ifdef LEAF
  590. static void updatelocal() __attribute__((noreturn));
  591. static void updatelocal(void)
  592. {
  593. /* let's drop the server connection ASAP */
  594. nuke_server("Updating...");
  595. botnet_send_chat(-1, conf.bot->nick, "Updating...");
  596. botnet_send_bye();
  597. fatal("Updating...", 1);
  598. usleep(2000 * 500);
  599. unlink(conf.bot->pid_file); /* if this fails it is ok, cron will restart the bot, *hopefully* */
  600. system(binname); /* start new bot. */
  601. exit(0);
  602. }
  603. #endif /* LEAF */
  604. int updatebin(int idx, char *par, int autoi)
  605. {
  606. char *path = NULL, *newbin = NULL;
  607. char buf[DIRMAX] = "", old[DIRMAX] = "", testbuf[DIRMAX] = "";
  608. struct stat sb;
  609. int i;
  610. path = newsplit(&par);
  611. par = path;
  612. if (!par[0]) {
  613. logidx(idx, "Not enough parameters.");
  614. return 1;
  615. }
  616. path = calloc(1, strlen(binname) + strlen(par) + 2);
  617. strcpy(path, binname);
  618. newbin = strrchr(path, '/');
  619. if (!newbin) {
  620. free(path);
  621. logidx(idx, "Don't know current binary name");
  622. return 1;
  623. }
  624. newbin++;
  625. if (strchr(par, '/')) {
  626. *newbin = 0;
  627. logidx(idx, "New binary must be in %s and name must be specified without path information", path);
  628. free(path);
  629. return 1;
  630. }
  631. strcpy(newbin, par);
  632. if (!strcmp(path, binname)) {
  633. free(path);
  634. logidx(idx, "Can't update with the current binary");
  635. return 1;
  636. }
  637. if (stat(path, &sb)) {
  638. logidx(idx, "%s can't be accessed", path);
  639. free(path);
  640. return 1;
  641. }
  642. if (fixmod(path)) {
  643. logidx(idx, "Can't set mode 0600 on %s", path);
  644. free(path);
  645. return 1;
  646. }
  647. /* make a backup just in case. */
  648. egg_snprintf(old, sizeof old, "%s.bin.old", tempdir);
  649. copyfile(binname, old);
  650. /* The binary should return '2' when ran with -2, if not it's probably corrupt. */
  651. egg_snprintf(testbuf, sizeof testbuf, "%s -2", path);
  652. i = system(testbuf);
  653. if (i == -1 || WEXITSTATUS(i) != 2) {
  654. dprintf(idx, "Couldn't restart new binary (error %d)\n", i);
  655. putlog(LOG_MISC, "*", "Couldn't restart new binary (error %d)", i);
  656. return i;
  657. }
  658. if (movefile(path, binname)) {
  659. logidx(idx, "Can't rename %s to %s", path, binname);
  660. free(path);
  661. return 1;
  662. }
  663. egg_snprintf(buf, sizeof buf, "%s", binname);
  664. /* safe to run new binary.. */
  665. #ifdef HUB
  666. listen_all(my_port, 1); /* close the listening port... */
  667. usleep(5000);
  668. #endif /* HUB */
  669. #ifdef LEAF
  670. if (!autoi && localhub) {
  671. /* let's drop the server connection ASAP */
  672. nuke_server("Updating...");
  673. #endif /* LEAF */
  674. putlog(LOG_DEBUG, "*", "Running for update: %s", buf);
  675. logidx(idx, "Updating...bye");
  676. putlog(LOG_MISC, "*", "Updating...");
  677. botnet_send_chat(-1, conf.bot->nick, "Updating...");
  678. botnet_send_bye();
  679. fatal("Updating...", 1);
  680. usleep(2000 * 500);
  681. unlink(conf.bot->pid_file); /* delete pid so new binary doesnt exit. */
  682. system(buf); /* run the binary, it SHOULD work from earlier tests.. */
  683. exit(0);
  684. #ifdef LEAF
  685. } else if (localhub && autoi) {
  686. egg_timeval_t howlong;
  687. egg_snprintf(buf, sizeof buf, "%s -L %s -P %d", binname, conf.bot->nick, getpid());
  688. putlog(LOG_DEBUG, "*", "Running for update: %s", buf);
  689. /* will exit after run, cron will restart us later */
  690. system(buf);
  691. howlong.sec = 300;
  692. howlong.usec = 0;
  693. timer_create(&howlong, "updatelocal()", (Function) updatelocal);
  694. return 0;
  695. }
  696. #endif /* LEAF */
  697. /* this should never be reached */
  698. return 2;
  699. }
  700. int bot_aggressive_to(struct userrec *u)
  701. {
  702. char mypval[20] = "", botpval[20] = "";
  703. link_pref_val(u, botpval);
  704. link_pref_val(conf.bot->u, mypval);
  705. if (strcmp(mypval, botpval) < 0)
  706. return 1;
  707. else
  708. return 0;
  709. }
  710. char kickprefix[25] = "";
  711. char bankickprefix[25] = "";
  712. char *kickreason(int kind) {
  713. switch (kind) {
  714. case KICK_BANNED:
  715. switch (randint(6)) {
  716. case 0: return "bye";
  717. case 1: return "banned";
  718. case 2: return "see you in hell";
  719. case 3: return "go away";
  720. case 4: return "cya around lewser";
  721. case 5: return "unwanted!";
  722. }
  723. case KICK_KUSER:
  724. switch (randint(4)) {
  725. case 0: return "not wanted";
  726. case 1: return "something tells me you're annoying";
  727. case 2: return "don't bug me lewser";
  728. case 3: return "creep";
  729. }
  730. case KICK_KICKBAN:
  731. switch (randint(4)) {
  732. case 0: return "gone";
  733. case 1: return "stupid";
  734. case 2: return "lewser";
  735. case 3: return "...";
  736. }
  737. case KICK_MASSDEOP:
  738. switch (randint(8)) {
  739. case 0: return "spammer!";
  740. case 1: return "easy on the modes now";
  741. case 2: return "mode this";
  742. case 3: return "nice try";
  743. case 4: return "really?";
  744. case 5: return "you lose";
  745. case 6: return "scary... really scary...";
  746. case 7: return "i win kthx";
  747. }
  748. case KICK_BADOP:
  749. switch (randint(5)) {
  750. case 0: return "neat...";
  751. case 1: return "oh, no you don't. go away.";
  752. case 2: return "didn't you forget something now?";
  753. case 3: return "no";
  754. case 4: return "hijack this";
  755. }
  756. case KICK_BADOPPED:
  757. switch (randint(5)) {
  758. case 0: return "fuck off kid";
  759. case 1: return "asl?";
  760. case 2: return "whoa... what a hacker... skills!";
  761. case 3: return "yes! yes! yes! hit me baby one more time!";
  762. case 4: return "with your skills, you're better off jacking off than hijacking";
  763. }
  764. case KICK_MANUALOP:
  765. switch (randint(6)) {
  766. case 0: return "naughty kid";
  767. case 1: return "didn't someone tell you that is bad?";
  768. case 2: return "want perm?";
  769. case 3: return "see how much good that did you?";
  770. case 4: return "not a smart move...";
  771. case 5: return "jackass!";
  772. }
  773. case KICK_MANUALOPPED:
  774. switch (randint(8)) {
  775. case 0: return "your pal got mean friends. like me.";
  776. case 1: return "uhh now.. don't wake me up...";
  777. case 2: return "hi hun. missed me?";
  778. case 3: return "spammer! die!";
  779. case 4: return "boo!";
  780. case 5: return "that @ was useful, don't ya think?";
  781. case 6: return "not in my book";
  782. case 7: return "lol, really?";
  783. }
  784. case KICK_CLOSED:
  785. switch (randint(17)) {
  786. case 0: return "locked";
  787. case 1: return "later";
  788. case 2: return "closed for now";
  789. case 3: return "come back later";
  790. case 4: return "better safe than sorry";
  791. case 5: return "cleanup, come back later";
  792. case 6: return "this channel is closed";
  793. case 7: return "shutting down for now";
  794. case 8: return "lockdown";
  795. case 9: return "reopening later";
  796. case 10: return "not for the public atm";
  797. case 11: return "private channel for now";
  798. case 12: return "might reopen soon, might reopen later";
  799. case 13: return "you're not supposed to be here right now";
  800. case 14: return "sorry, closed";
  801. case 15: return "try us later, atm we're locked down";
  802. case 16: return "closed. try tomorrow";
  803. }
  804. case KICK_FLOOD:
  805. switch (randint(7)) {
  806. case 0: return "so much bullshit in such a short time. amazing.";
  807. case 1: return "slow down. i'm trying to read here.";
  808. case 2: return "uhm... you actually think irc is for talking?";
  809. case 3: return "talk talk talk";
  810. case 4: return "blabbering are we?";
  811. case 5: return "... and i don't even like you!";
  812. case 6: return "and you're outa here...";
  813. }
  814. case KICK_NICKFLOOD:
  815. switch (randint(7)) {
  816. case 0: return "make up your mind?";
  817. case 1: return "be schizofrenic elsewhere";
  818. case 2: return "I'm loosing track of you... not!";
  819. case 3: return "that is REALLY annoying";
  820. case 4: return "try this: /NICK n00b";
  821. case 5: return "playing hide 'n' seek?";
  822. case 6: return "gotcha!";
  823. }
  824. case KICK_KICKFLOOD:
  825. switch (randint(6)) {
  826. case 0: return "easier to just leave if you wan't to be alone";
  827. case 1: return "cool down";
  828. case 2: return "don't be so damned aggressive. that's my job.";
  829. case 3: return "kicking's fun, isn't it?";
  830. case 4: return "what's the rush?";
  831. case 5: return "next time you do that, i'll kick you again";
  832. }
  833. case KICK_BOGUSUSERNAME:
  834. return "bogus username";
  835. case KICK_MEAN:
  836. switch (randint(11)) {
  837. case 0: return "hey! that wasn't very nice!";
  838. case 1: return "don't fuck with my pals";
  839. case 2: return "meanie!";
  840. case 3: return "I can be a bitch too...";
  841. case 4: return "leave the bots alone, will ya?";
  842. case 5: return "not very clever";
  843. case 6: return "watch it";
  844. case 7: return "fuck off";
  845. case 8: return "easy now. that's a friend.";
  846. case 9: return "abuse of power. leave that to me, will ya?";
  847. case 10: return "there as some things you cannot do, and that was one of them...";
  848. }
  849. case KICK_BOGUSKEY:
  850. return "I have a really hard time reading that key";
  851. default:
  852. return "OMFG@YUO";
  853. }
  854. }
  855. /*
  856. plain cookie:
  857. Last 6 digits of time
  858. Last 5 chars of nick
  859. Last 4 regular chars of chan
  860. */
  861. void makeplaincookie(char *chname, char *nick, char *buf)
  862. {
  863. char work[256] = "", work2[256] = "";
  864. int i, n;
  865. sprintf(work, "%010li", (now + timesync));
  866. strcpy(buf, (char *) &work[4]);
  867. work[0] = 0;
  868. if (strlen(nick) < 5)
  869. while (strlen(work) + strlen(nick) < 5)
  870. strcat(work, " ");
  871. else
  872. strcpy(work, (char *) &nick[strlen(nick) - 5]);
  873. strcat(buf, work);
  874. n = 3;
  875. for (i = strlen(chname) - 1; (i >= 0) && (n >= 0); i--)
  876. if (((unsigned char) chname[i] < 128) && ((unsigned char) chname[i] > 32)) {
  877. work2[n] = tolower(chname[i]);
  878. n--;
  879. }
  880. while (n >= 0)
  881. work2[n--] = ' ';
  882. work2[4] = 0;
  883. strcat(buf, work2);
  884. }
  885. int goodpass(char *pass, int idx, char *nick)
  886. {
  887. char tell[501] = "";
  888. #ifdef S_NAZIPASS
  889. int i, nalpha = 0, lcase = 0, ucase = 0, ocase = 0, tc;
  890. #endif /* S_NAZIPASS */
  891. if (!pass[0])
  892. return 0;
  893. #ifdef S_NAZIPASS
  894. for (i = 0; i < strlen(pass); i++) {
  895. tc = (int) pass[i];
  896. if (tc < 58 && tc > 47)
  897. ocase++; /* number */
  898. else if (tc < 91 && tc > 64)
  899. ucase++; /* upper case */
  900. else if (tc < 123 && tc > 96)
  901. lcase++; /* lower case */
  902. else
  903. nalpha++; /* non-alphabet/number */
  904. }
  905. /* if (ocase < 1 || lcase < 2 || ucase < 2 || nalpha < 1 || strlen(pass) < 8) { */
  906. if (ocase < 1 || lcase < 2 || ucase < 2 || strlen(pass) < 8) {
  907. #else /* !S_NAZIPASS */
  908. if (strlen(pass) < 8) {
  909. #endif /* S_NAZIPASS */
  910. sprintf(tell, "Insecure pass, must be: ");
  911. #ifdef S_NAZIPASS
  912. if (ocase < 1)
  913. strcat(tell, "\002>= 1 number\002, ");
  914. else
  915. strcat(tell, ">= 1 number, ");
  916. if (lcase < 2)
  917. strcat(tell, "\002>= 2 lcase\002, ");
  918. else
  919. strcat(tell, ">= 2 lowercase, ");
  920. if (ucase < 2)
  921. strcat(tell, "\002>= 2 ucase\002, ");
  922. else
  923. strcat(tell, ">= 2 uppercase, ");
  924. /* This is annoying as hell
  925. if (nalpha < 1)
  926. strcat(tell, "\002>= 1 non-alpha/num\002, ");
  927. else
  928. strcat(tell, ">= 1 non-alpha/num, ");
  929. */
  930. #endif /* S_NAZIPASS */
  931. if (strlen(pass) < 8)
  932. strcat(tell, "\002>= 8 chars.\002");
  933. else
  934. strcat(tell, ">= 8 chars.");
  935. if (idx)
  936. dprintf(idx, "%s\n", tell);
  937. else if (nick[0])
  938. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, tell);
  939. return 0;
  940. }
  941. return 1;
  942. }
  943. char *replace(const char *string, const char *oldie, const char *newbie)
  944. {
  945. static char newstring[1024] = "";
  946. int str_index, newstr_index, oldie_index, end, new_len, old_len, cpy_len;
  947. char *c = NULL;
  948. if (string == NULL) return "";
  949. if ((c = (char *) strstr(string, oldie)) == NULL) return (char *) string;
  950. new_len = strlen(newbie);
  951. old_len = strlen(oldie);
  952. end = strlen(string) - old_len;
  953. oldie_index = c - string;
  954. newstr_index = 0;
  955. str_index = 0;
  956. while(str_index <= end && c != NULL) {
  957. cpy_len = oldie_index-str_index;
  958. strncpy(newstring + newstr_index, string + str_index, cpy_len);
  959. newstr_index += cpy_len;
  960. str_index += cpy_len;
  961. strcpy(newstring + newstr_index, newbie);
  962. newstr_index += new_len;
  963. str_index += old_len;
  964. if((c = (char *) strstr(string + str_index, oldie)) != NULL)
  965. oldie_index = c - string;
  966. }
  967. strcpy(newstring + newstr_index, string + str_index);
  968. return (newstring);
  969. }
  970. #define HELP_BOLD 1
  971. #define HELP_REV 2
  972. #define HELP_UNDER 4
  973. #define HELP_FLASH 8
  974. void showhelp(int idx, struct flag_record *flags, char *string)
  975. {
  976. static int help_flags;
  977. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0};
  978. char *helpstr = NULL, tmp[2] = "", flagstr[10] = "";
  979. int ok = 1;
  980. helpstr = calloc(1, strlen(string) + 1000 + 1);
  981. while (string && string[0]) {
  982. if (*string == '%') {
  983. if (!strncmp(string + 1, "{+", 2)) {
  984. while (*string && *string != '+') {
  985. string++;
  986. }
  987. flagstr[0] = 0;
  988. while (*string && *string != '}') {
  989. sprintf(tmp, "%c", *string);
  990. strcat(flagstr, tmp);
  991. string++;
  992. }
  993. string++;
  994. break_down_flags(flagstr, &fr, NULL);
  995. if (flagrec_ok(&fr, flags)) {
  996. ok = 1;
  997. while (*string && *string != '%') {
  998. sprintf(tmp, "%c", *string);
  999. strcat(helpstr, tmp);
  1000. string++;
  1001. }
  1002. if (!strncmp(string + 1, "{-", 2)) {
  1003. ok = 1;
  1004. while (*string && *string != '}') {
  1005. string++;
  1006. }
  1007. string++;
  1008. }
  1009. } else {
  1010. ok = 0;
  1011. }
  1012. } else if (!strncmp(string + 1, "{-", 2)) {
  1013. ok = 1;
  1014. while (*string && *string != '}') {
  1015. string++;
  1016. }
  1017. string++;
  1018. } else if (*string == '{') {
  1019. while (*string && *string != '}') {
  1020. string++;
  1021. }
  1022. } else if (*(string + 1) == 'b') {
  1023. string += 2;
  1024. if (help_flags & HELP_BOLD) {
  1025. help_flags &= ~HELP_BOLD;
  1026. strcat(helpstr, color(idx, BOLD_CLOSE, 0));
  1027. } else {
  1028. help_flags |= HELP_BOLD;
  1029. strcat(helpstr, color(idx, BOLD_OPEN, 0));
  1030. }
  1031. } else if (*(string + 1) == 'f') {
  1032. string += 2;
  1033. if (help_flags & HELP_FLASH) {
  1034. strcat(helpstr, color(idx, FLASH_CLOSE, 0));
  1035. help_flags &= ~HELP_FLASH;
  1036. } else {
  1037. help_flags |= HELP_FLASH;
  1038. strcat(helpstr, color(idx, FLASH_OPEN, 0));
  1039. }
  1040. } else if (*(string + 1) == 'd') {
  1041. string += 2;
  1042. strcat(helpstr, dcc_prefix);
  1043. } else if (*(string + 1) == '%') {
  1044. string += 2;
  1045. strcat(helpstr, "%");
  1046. } else {
  1047. if (ok) {
  1048. sprintf(tmp, "%c", *string);
  1049. strcat(helpstr, tmp);
  1050. }
  1051. string++;
  1052. }
  1053. } else {
  1054. if (ok) {
  1055. sprintf(tmp, "%c", *string);
  1056. strcat(helpstr, tmp);
  1057. }
  1058. string++;
  1059. }
  1060. }
  1061. helpstr[strlen(helpstr)] = 0;
  1062. if (helpstr[0]) dumplots(idx, "", helpstr);
  1063. free(helpstr);
  1064. }
  1065. /* Arrange the N elements of ARRAY in random order. */
  1066. static void shuffleArray(char *array[], int n)
  1067. {
  1068. int i;
  1069. for (i = 0; i < n; i++) {
  1070. int j = i + random() / (RAND_MAX / (n - i) + 1);
  1071. char *t = array[j];
  1072. array[j] = array[i];
  1073. array[i] = t;
  1074. }
  1075. }
  1076. void shuffle(char *string, char *delim)
  1077. {
  1078. char *array[501], *str = NULL, *work = NULL;
  1079. int len = 0, i = 0;
  1080. egg_bzero(&array, sizeof array);
  1081. work = strdup(string);
  1082. str = strtok(work, delim);
  1083. while(str && *str)
  1084. {
  1085. array[len] = str;
  1086. len++;
  1087. str = strtok((char*) NULL, delim);
  1088. }
  1089. shuffleArray(array, len);
  1090. string[0] = 0;
  1091. for (i = 0; i < len; i++) {
  1092. strcat(string, array[i]);
  1093. if (i != len - 1)
  1094. strcat(string, delim);
  1095. }
  1096. free(work);
  1097. string[strlen(string)] = 0;
  1098. }
  1099. /* returns
  1100. 1: use ANSI
  1101. 2: use mIRC
  1102. 0: neither
  1103. */
  1104. int
  1105. coloridx(int idx)
  1106. {
  1107. if (idx == -1) { /* who cares, just show color! */
  1108. return 1; /* ANSI */
  1109. } else if (idx == -2) {
  1110. return 2; /* mIRC */
  1111. /* valid idx and NOT relaying */
  1112. } else if (idx >= 0 && (dcc[idx].status & STAT_COLOR) && (dcc[idx].type && dcc[idx].type != &DCC_RELAYING)) {
  1113. /* telnet probably wants ANSI, even though it might be a relay from an mIRC client; fuck`em */
  1114. if (dcc[idx].status & STAT_TELNET)
  1115. return 1;
  1116. /* non-telnet is probably a /dcc-chat, most irc clients support mIRC codes... */
  1117. else
  1118. return 2;
  1119. }
  1120. return 0;
  1121. }
  1122. char *color(int idx, int type, int which)
  1123. {
  1124. int ansi = 0;
  1125. /* if user is connected over TELNET or !backgrd, show ANSI
  1126. * if they are relaying, they are most likely on an IRC client and should have mIRC codes
  1127. */
  1128. if ((ansi = coloridx(idx)) == 0)
  1129. return "";
  1130. if (ansi == 2)
  1131. ansi = 0;
  1132. if (type == BOLD_OPEN) {
  1133. return ansi ? "\033[1m" : "\002";
  1134. } else if (type == BOLD_CLOSE) {
  1135. return ansi ? "\033[22m" : "\002";
  1136. } else if (type == UNDERLINE_OPEN) {
  1137. return ansi ? "\033[4m" : "\037";
  1138. } else if (type == UNDERLINE_CLOSE) {
  1139. return ansi ? "\033[24m" : "\037";
  1140. } else if (type == FLASH_OPEN) {
  1141. return ansi ? "\033[5m" : "\002\037";
  1142. } else if (type == FLASH_CLOSE) {
  1143. return ansi ? "\033[0m" : "\037\002";
  1144. } else if (type == COLOR_OPEN) {
  1145. switch (which) {
  1146. case C_BLACK: return ansi ? "\033[30m" : "\00301";
  1147. case C_RED: return ansi ? "\033[31m" : "\00305";
  1148. case C_GREEN: return ansi ? "\033[32m" : "\00303";
  1149. case C_BROWN: return ansi ? "\033[33m" : "\00307";
  1150. case C_BLUE: return ansi ? "\033[34m" : "\00302";
  1151. case C_PURPLE: return ansi ? "\033[35m" : "\00306";
  1152. case C_CYAN: return ansi ? "\033[36m" : "\00310";
  1153. case C_WHITE: return ansi ? "\033[1;37m" : "\00300";
  1154. case C_DARKGREY: return ansi ? "\033[1;30m" : "\00314";
  1155. case C_LIGHTRED: return ansi ? "\033[1;31m" : "\00304";
  1156. case C_LIGHTGREEN: return ansi ? "\033[1;32m" : "\00309";
  1157. case C_LIGHTBLUE: return ansi ? "\033[1;34m" : "\00312";
  1158. case C_LIGHTPURPLE: return ansi ? "\033[1;35m" : "\00313";
  1159. case C_LIGHTCYAN: return ansi ? "\033[1;36m" : "\00311";
  1160. case C_LIGHTGREY: return ansi ? "\033[37m" : "\00315";
  1161. case C_YELLOW: return ansi ? "\033[1;33m" : "\00308";
  1162. default: break;
  1163. }
  1164. } else if (type == COLOR_CLOSE) {
  1165. return ansi ? "\033[0m" : "\00300";
  1166. }
  1167. /* This should never be reached.. */
  1168. return "";
  1169. }