misc.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2013 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * misc.c -- handles:
  22. * split() maskhost() dumplots() daysago() days() daysdur()
  23. * queueing output for the bot (msg and help)
  24. * resync buffers for sharebots
  25. * motd display and %var substitution
  26. *
  27. */
  28. #include "common.h"
  29. #include "misc.h"
  30. #include "settings.h"
  31. #include "binary.h"
  32. #include "rfc1459.h"
  33. #include "botnet.h"
  34. #include "misc_file.h"
  35. #include "egg_timer.h"
  36. #include "dcc.h"
  37. #include "users.h"
  38. #include "shell.h"
  39. #include "main.h"
  40. #include "debug.h"
  41. #include "dccutil.h"
  42. #include "chanprog.h"
  43. #include "color.h"
  44. #include "botmsg.h"
  45. #include "bg.h"
  46. #include "chan.h"
  47. #include "tandem.h"
  48. #include "src/mod/server.mod/server.h"
  49. #include "src/mod/irc.mod/irc.h"
  50. #include "src/mod/channels.mod/channels.h"
  51. #include "userrec.h"
  52. #include "stat.h"
  53. #include "net.h"
  54. #include "EncryptedStream.h"
  55. #include <bdlib/src/String.h>
  56. #include <bdlib/src/Stream.h>
  57. #include <sys/wait.h>
  58. #include <stdarg.h>
  59. #include <sys/types.h>
  60. #include <signal.h>
  61. int server_lag = 0; /* GUESS! */
  62. bool use_invites = 0;
  63. bool use_exempts = 0;
  64. /*
  65. * Misc functions
  66. */
  67. /* low-level stuff for other modules
  68. */
  69. size_t my_strcpy(register char *a, register const char *b)
  70. {
  71. register const char *c = b;
  72. while (*b)
  73. *a++ = *b++;
  74. *a = *b;
  75. return b - c;
  76. }
  77. /* Split first word off of rest and put it in first
  78. */
  79. void splitc(char *first, char *rest, char divider)
  80. {
  81. char *p = strchr(rest, divider);
  82. if (p == NULL) {
  83. if (first != rest && first)
  84. first[0] = 0;
  85. return;
  86. }
  87. *p = 0;
  88. if (first != NULL)
  89. strcpy(first, rest);
  90. if (first != rest)
  91. /* In most circumstances, strcpy with src and dst being the same buffer
  92. * can produce undefined results. We're safe here, as the src is
  93. * guaranteed to be at least 2 bytes higher in memory than dest. <Cybah>
  94. */
  95. strcpy(rest, p + 1);
  96. }
  97. /* As above, but lets you specify the 'max' number of bytes (EXCLUDING the
  98. * terminating null).
  99. *
  100. * Example of use:
  101. *
  102. * char buf[HANDLEN + 1];
  103. *
  104. * splitcn(buf, input, "@", HANDLEN);
  105. *
  106. * <Cybah>
  107. */
  108. void splitcn(char *first, char *rest, char divider, size_t max)
  109. {
  110. char *p = strchr(rest, divider);
  111. if (p == NULL) {
  112. if (first != rest && first)
  113. first[0] = 0;
  114. return;
  115. }
  116. *p = 0;
  117. if (first != NULL)
  118. strlcpy(first, rest, max);
  119. if (first != rest)
  120. /* In most circumstances, strcpy with src and dst being the same buffer
  121. * can produce undefined results. We're safe here, as the src is
  122. * guaranteed to be at least 2 bytes higher in memory than dest. <Cybah>
  123. */
  124. strcpy(rest, p + 1);
  125. }
  126. char *splitnick(char **blah)
  127. {
  128. char *p = NULL, *q = *blah;
  129. p = strchr(*blah, '!');
  130. if (p) {
  131. *p = 0;
  132. *blah = p + 1;
  133. return q;
  134. }
  135. return "";
  136. }
  137. int remove_crlf(char *line)
  138. {
  139. char *p = NULL;
  140. int removed = 0;
  141. if ((p = strchr(line, '\n'))) {
  142. *p = 0;
  143. removed++;
  144. }
  145. if ((p = strchr(line, '\r'))) {
  146. *p = 0;
  147. removed++;
  148. }
  149. return removed;
  150. }
  151. int remove_crlf_r(char *line)
  152. {
  153. char *p = NULL;
  154. int removed = 0;
  155. if ((p = strrchr(line, '\n'))) {
  156. *p = 0;
  157. removed++;
  158. }
  159. if ((p = strrchr(line, '\r'))) {
  160. *p = 0;
  161. removed++;
  162. }
  163. return removed;
  164. }
  165. char *newsplit(char **rest, char delim, bool trim)
  166. {
  167. if (!rest)
  168. return *rest = "";
  169. register char *o = *rest, *r = NULL;
  170. while (*o == delim)
  171. ++o;
  172. r = o;
  173. while (*o && (*o != delim))
  174. ++o;
  175. if (*o)
  176. *o++ = 0;
  177. /* Trim whitespace */
  178. if (trim) {
  179. while (*o == ' ')
  180. ++o;
  181. }
  182. *rest = o;
  183. return r;
  184. }
  185. /* maskhost(), modified to support custom mask types, as defined
  186. * by mIRC.
  187. * Does not require a proper hostmask in 's'. Accepts any strings,
  188. * including empty ones and attempts to provide meaningful results.
  189. *
  190. * Strings containing no '@' character will be parsed as if the
  191. * whole string is a host.
  192. * Strings containing no '!' character will be interpreted as if
  193. * there is no nick.
  194. * '!' as a nick/user separator must precede any '@' characters.
  195. * Otherwise it will be considered a part of the host.
  196. * Supported types are listed in tcl-commands.doc in the maskhost
  197. * command section. Type 3 resembles the older maskhost() most closely.
  198. *
  199. * Specific examples (with type=3):
  200. *
  201. * "nick!user@is.the.lamest.bg" -> *!*user@*.the.lamest.bg (ccTLD)
  202. * "nick!user@is.the.lamest.com" -> *!*user@*.lamest.com (gTLD)
  203. * "lamest.example" -> *!*@lamest.example
  204. * "whatever@lamest.example" -> *!*whatever@lamest.example
  205. * "com.example@user!nick" -> *!*com.example@user!nick
  206. * "!" -> *!*@!
  207. * "@" -> *!*@*
  208. * "" -> *!*@*
  209. * "abc!user@2001:db8:618:5c0:263:15:dead:babe"
  210. * -> *!*user@2001:db8:618:5c0:263:15:dead:*
  211. * "abc!user@0:0:0:0:0:ffff:1.2.3.4"
  212. * -> *!*user@0:0:0:0:0:ffff:1.2.3.*
  213. */
  214. void maskaddr(const char *s, char *nw, int type)
  215. {
  216. int d = type % 5, num = 1;
  217. register const char *u = NULL, *h = NULL, *p = NULL;
  218. /* Look for user and host.. */
  219. if ((u = strchr(s, '!')))
  220. h = strchr(u, '@');
  221. if (!h)
  222. h = strchr(s, '@');
  223. /* Print nick if required and available */
  224. if (!u || (type % 10) < 5)
  225. *nw++ = '*';
  226. else {
  227. strncpy(nw, s, u - s);
  228. nw += u - s;
  229. }
  230. *nw++ = '!';
  231. /* Write user if required and available */
  232. u = (u ? u + 1 : s);
  233. if (!h || (d == 2) || (d == 4))
  234. *nw++ = '*';
  235. else {
  236. if (d) {
  237. *nw++ = '*';
  238. if (strchr("~+-^=", *u))
  239. u++; /* trim leading crap */
  240. }
  241. strncpy(nw, u, h - u);
  242. nw += h - u;
  243. }
  244. *nw++ = '@';
  245. /* The rest is for the host */
  246. h = (h ? h + 1 : s);
  247. for (p = h; *p; p++) /* hostname? */
  248. if ((*p > '9' || *p < '0') && *p != '.') {
  249. num = 0;
  250. break;
  251. }
  252. p = strrchr(h, ':'); /* IPv6? */
  253. /* Mask out after the last colon/dot */
  254. if (p && d > 2) {
  255. if ((u = strrchr(p, '.')))
  256. p = u;
  257. strncpy(nw, h, ++p - h);
  258. nw += p - h;
  259. strcpy(nw, "*");
  260. } else if (!p && !num && type >= 10) {
  261. /* we have a hostname and type
  262. requires us to replace numbers */
  263. num = 0;
  264. for (p = h; *p; p++) {
  265. if (*p < '0' || *p > '9') {
  266. *nw++ = *p;
  267. num = 0;
  268. } else {
  269. if (type < 20)
  270. *nw++ = '?';
  271. else if (!num) {
  272. *nw++ = '*';
  273. num = 1; /* place only one '*'
  274. per numeric sequence */
  275. }
  276. }
  277. }
  278. *nw = 0;
  279. } else if (d > 2 && (p = strrchr(h, '.'))) {
  280. if (num) { /* IPv4 */
  281. strncpy(nw, h, p - h);
  282. nw += p - h;
  283. strcpy(nw, ".*");
  284. return;
  285. }
  286. for (u = h, d = 0; (u = strchr(++u, '.')); d++) ;
  287. if (d < 2) { /* types < 2 don't mask the host */
  288. strcpy(nw, h);
  289. return;
  290. }
  291. u = strchr(h, '.');
  292. if (d > 3 || (d == 3 && strlen(p) > 3))
  293. u = strchr(++u, '.'); /* ccTLD or not? Look above. */
  294. simple_sprintf(nw, "*%s", u);
  295. } else if (!*h) {
  296. /* take care if the mask is empty or contains only '@' */
  297. strcpy(nw, "*");
  298. } else
  299. strcpy(nw, h);
  300. }
  301. /* Convert an interval (in seconds) to one of:
  302. * "19 days ago", "1 day ago", "18:12"
  303. */
  304. void daysago(time_t mynow, time_t then, char *out, size_t outsiz)
  305. {
  306. if (mynow - then > 86400) {
  307. int mydays = (mynow - then) / 86400;
  308. simple_snprintf(out, outsiz, "%d day%s ago", mydays, (mydays == 1) ? "" : "s");
  309. return;
  310. }
  311. strftime(out, 6, "%H:%M", gmtime(&then));
  312. }
  313. /* Convert an interval (in seconds) to one of:
  314. * "in 19 days", "in 1 day", "at 18:12"
  315. */
  316. void days(time_t mynow, time_t then, char *out, size_t outsiz)
  317. {
  318. if (mynow - then > 86400) {
  319. int mydays = (mynow - then) / 86400;
  320. simple_snprintf(out, outsiz, "in %d day%s", mydays, (mydays == 1) ? "" : "s");
  321. return;
  322. }
  323. strftime(out, 9, "at %H:%M", gmtime(&now));
  324. }
  325. /* Convert an interval (in seconds) to one of:
  326. * "for 19 days", "for 1 day", "for 09:10"
  327. */
  328. void daysdur(time_t mynow, time_t then, char *out, size_t outsiz, bool useFor)
  329. {
  330. size_t startIndex = 0;
  331. out[0] = 0;
  332. if (useFor) {
  333. strlcpy(out, "for ", outsiz);
  334. startIndex = 4;
  335. }
  336. if (mynow - then > 86400) {
  337. int mydays = (mynow - then) / 86400;
  338. simple_snprintf(&out[startIndex], outsiz - startIndex, "%d day%s", mydays, (mydays == 1) ? "" : "s");
  339. return;
  340. }
  341. char s[81] = "";
  342. mynow -= then;
  343. int hrs = (int) (mynow / 3600);
  344. int mins = (int) ((mynow - (hrs * 3600)) / 60);
  345. simple_snprintf(s, sizeof(s), "%02d:%02d", hrs, mins);
  346. strlcat(out, s, outsiz);
  347. }
  348. /* show l33t banner */
  349. static const char *wbanner(void) {
  350. /*
  351. __ __ __
  352. __ _ ______________ |__|/ |_| |__
  353. \ \/ \/ /\_ __ \__ \ | \ _\ | \
  354. \ / | | \// __ \| || | | \ \
  355. \/\_/ |__| (____ /__||__| |___| /
  356. \/ \/
  357. */
  358. return STR(" __ __ __\n__ _ ______________ |__|/ |_| |__\n\\ \\/ \\/ /\\_ __ \\__ \\ | \\ _\\ | \\\n \\ / | | \\// __ \\| || | | \\ \\\n \\/\\_/ |__| (____ /__||__| |___| /\n \\/ \\/\n");
  359. }
  360. void show_banner(int idx)
  361. {
  362. /* we use sock so that colors aren't applied to banner */
  363. if (dcc[idx].status & STAT_BANNER)
  364. dumplots(-dcc[idx].sock, "", wbanner());
  365. dprintf(idx, " \n");
  366. dprintf(-dcc[idx].sock, STR(" -------------------------------------------------------- \n"));
  367. dprintf(-dcc[idx].sock, STR("| - http://wraith.botpack.net/ - |\n"));
  368. dprintf(-dcc[idx].sock, STR("| Get Shell/Irc/Web hosting @ http://www.xzibition.com |\n"));
  369. dprintf(-dcc[idx].sock, STR("| Help support wraith development by signing up. |\n"));
  370. dprintf(-dcc[idx].sock, STR("| Use coupon code 'wraith' for 30%% off lifetime |\n"));
  371. dprintf(-dcc[idx].sock, STR(" -------------------------------------------------------- \n"));
  372. dprintf(idx, " \n");
  373. }
  374. /* show motd to dcc chatter */
  375. void show_motd(int idx)
  376. {
  377. if (motd[0]) {
  378. char *who = NULL, *buf = NULL, *buf_ptr = NULL, date[50] = "";
  379. time_t when;
  380. buf = buf_ptr = strdup(motd);
  381. who = newsplit(&buf);
  382. when = atoi(newsplit(&buf));
  383. strftime(date, sizeof date, "%c %Z", gmtime(&when));
  384. dprintf(idx, "Motd set by %s%s%s (%s)\n", BOLD(idx), who, BOLD_END(idx), date);
  385. dumplots(idx, "* ", replace(buf, "\\n", "\n"));
  386. dprintf(idx, " \n");
  387. free(buf_ptr);
  388. } else
  389. dprintf(idx, "Motd: none\n");
  390. }
  391. void show_channels(int idx, char *handle)
  392. {
  393. struct userrec *u = NULL;
  394. size_t maxChannelLength = 0;
  395. bd::Array<bd::String> channelNames;
  396. bd::HashTable<bd::String, struct chanset_t*> channels;
  397. bd::String group;
  398. if (handle && handle[0] != '%') {
  399. u = get_user_by_handle(userlist, handle);
  400. } else {
  401. u = dcc[idx].user;
  402. if (handle && handle[0] == '%') {
  403. group = handle + 1;
  404. }
  405. }
  406. for (struct chanset_t* chan = chanset; chan; chan = chan->next) {
  407. struct flag_record fr = { FR_CHAN | FR_GLOBAL, 0, 0, 0 };
  408. const bd::String chname(chan->dname);
  409. // If a group was passed, ensure it matches
  410. if (group.length() && chan->groups->find(group) == chan->groups->npos) {
  411. continue;
  412. }
  413. get_user_flagrec(u, &fr, chan->dname);
  414. if (group.length() || real_chk_op(fr, chan, 0)) {
  415. if (maxChannelLength < chname.length()) {
  416. maxChannelLength = chname.length();
  417. }
  418. channelNames << chname;
  419. channels[chname] = chan;
  420. }
  421. }
  422. if (channelNames.length()) {
  423. char format[120] = "";
  424. simple_snprintf(format, sizeof(format), " %%c%%-%zus %%-s%%-s%%-s%%-s%%-s%%-s\n", (maxChannelLength+2));
  425. if (group.length()) {
  426. dprintf(idx, "group '%s' is in %zu channel%s:\n", group.c_str(), channelNames.length(), (channelNames.length() > 1) ? "s" : "");
  427. } else {
  428. dprintf(idx, "%s %s access to %zu channel%s:\n", handle ? u->handle : "You", handle ? "has" : "have", channelNames.length(), (channelNames.length() > 1) ? "s" : "");
  429. }
  430. for (size_t i = 0; i < channelNames.length(); ++i) {
  431. const bd::String chname(channelNames[i]);
  432. const struct chanset_t* chan = channels[chname];
  433. dprintf(idx, format, !conf.bot->hub && me_op(chan) ? '@' : ' ', chan->dname, ((conf.bot->hub && channel_inactive(chan)) || (!conf.bot->hub && !shouldjoin(chan))) ? "(inactive) " : "",
  434. channel_privchan(chan) ? "(private) " : "", chan->manop ? "(no manop) " : "",
  435. channel_bitch(chan) && !channel_botbitch(chan) ? "(bitch) " : channel_botbitch(chan) ? "(botbitch) " : "",
  436. channel_closed(chan) ? "(closed) " : "", channel_backup(chan) ? "(backup)" : "");
  437. }
  438. } else {
  439. if (group.length()) {
  440. dprintf(idx, "No channels found for group '%s'\n", group.c_str());
  441. } else {
  442. dprintf(idx, "%s %s not have access to any channels.\n", handle ? u->handle : "You", handle ? "does" : "do");
  443. }
  444. }
  445. }
  446. /* Create a string with random letters and digits
  447. */
  448. void make_rand_str(char *s, size_t len, bool special)
  449. {
  450. int r = 0;
  451. size_t j = 0;
  452. for (j = 0; j < len; j++) {
  453. r = randint(special ? 4 : 3);
  454. if (r == 0)
  455. s[j] = '0' + randint(10);
  456. else if (r == 1)
  457. s[j] = 'a' + randint(26);
  458. else if (r == 2)
  459. s[j] = 'A' + randint(26);
  460. else if (r == 3)
  461. s[j] = RANDSPECIAL[randint(RANDSPECIALLEN)];
  462. if (j && strchr(BADREPEATEDRAND, s[j]) && s[j] == s[j - 1]) {
  463. while (s[j] == s[j - 1])
  464. s[j] = 'A' + randint(26);
  465. }
  466. }
  467. if (strchr(BADPASSCHARS, s[0]))
  468. s[0] = 'a' + randint(26);
  469. s[len] = '\0';
  470. }
  471. /* Return an allocated buffer which contains a copy of the string
  472. * 'str', with all 'div' characters escaped by 'mask'. 'mask'
  473. * characters are escaped too.
  474. *
  475. * Remember to free the returned memory block.
  476. */
  477. char *str_escape(const char *str, const char divc, const char mask)
  478. {
  479. const size_t len = strlen(str);
  480. size_t buflen = (2 * len), blen = 0;
  481. char *buf = NULL, *b = NULL;
  482. const char *s = NULL;
  483. b = buf = (char *) my_calloc(1, buflen + 1);
  484. for (s = str; *s; s++) {
  485. /* Resize buffer. */
  486. if ((buflen - blen) <= 3) {
  487. buflen <<= 1; /* * 2 */
  488. buf = (char *) my_realloc(buf, buflen + 1);
  489. if (!buf)
  490. return NULL;
  491. b = buf + blen;
  492. }
  493. if (*s == divc || *s == mask) {
  494. simple_snprintf(b, buflen, "%c%02x", mask, *s);
  495. b += 3;
  496. blen += 3;
  497. } else {
  498. *(b++) = *s;
  499. blen++;
  500. }
  501. }
  502. *b = 0;
  503. return buf;
  504. }
  505. /* Search for a certain character 'div' in the string 'str', while
  506. * ignoring escaped characters prefixed with 'mask'.
  507. *
  508. * The string
  509. *
  510. * "\\3a\\5c i am funny \\3a):further text\\5c):oink"
  511. *
  512. * as str, '\\' as mask and ':' as div would change the str buffer
  513. * to
  514. *
  515. * ":\\ i am funny :)"
  516. *
  517. * and return a pointer to "further text\\5c):oink".
  518. *
  519. * NOTE: If you look carefully, you'll notice that strchr_unescape()
  520. * behaves differently than strchr().
  521. */
  522. char *strchr_unescape(char *str, const char divc, register const char esc_char)
  523. {
  524. char buf[3] = "";
  525. register char *s = NULL, *p = NULL;
  526. for (s = p = str; *s; s++, p++) {
  527. if (*s == esc_char) { /* Found escape character. */
  528. /* Convert code to character. */
  529. buf[0] = s[1], buf[1] = s[2];
  530. *p = (unsigned char) strtol(buf, NULL, 16);
  531. s += 2;
  532. } else if (*s == divc) {
  533. *p = *s = 0;
  534. return (s + 1); /* Found searched for character. */
  535. } else
  536. *p = *s;
  537. }
  538. *p = 0;
  539. return NULL;
  540. }
  541. char s1_16[3] = "",s2_6[3] = "",s2_7[3] = "";
  542. /* As strchr_unescape(), but converts the complete string, without
  543. * searching for a specific delimiter character.
  544. */
  545. void str_unescape(char *str, register const char esc_char)
  546. {
  547. strchr_unescape(str, 0, esc_char);
  548. return;
  549. }
  550. /* Is every character in a string a digit? */
  551. int str_isdigit(const char *str)
  552. {
  553. if (!str || (str && !*str))
  554. return 0;
  555. if (*str == '-' && str[1])
  556. str++;
  557. for(; *str; ++str) {
  558. if (!egg_isdigit(*str))
  559. return 0;
  560. }
  561. return 1;
  562. }
  563. /* Kills the bot. s1 is the reason shown to other bots,
  564. * s2 the reason shown on the partyline. (Sup 25Jul2001)
  565. */
  566. void kill_bot(char *s1, char *s2)
  567. {
  568. write_userfile(-1);
  569. if (!conf.bot->hub)
  570. server_die();
  571. chatout("*** %s\n", s1);
  572. botnet_send_chat(-1, conf.bot->nick, s1);
  573. botnet_send_bye(s2);
  574. fatal(s2, 0);
  575. }
  576. void
  577. readsocks(const char *fname)
  578. {
  579. /* Don't bother setting this if a hub
  580. ... it is only intended to prevent parting channels (in bot_shouldjoin())
  581. */
  582. if (!conf.bot->hub)
  583. restarting = 1;
  584. char *nick = NULL, *jnick = NULL, *ip4 = NULL, *ip6 = NULL;
  585. time_t old_buildts = 0;
  586. bool cached_005 = 0;
  587. const char salt1[] = SALT1;
  588. EncryptedStream stream(salt1);
  589. stream.loadFile(fname);
  590. bd::String str, type;
  591. reset_chans = 0;
  592. while (stream.tell() < stream.length()) {
  593. str = stream.getline().chomp();
  594. dprintf(DP_DEBUG, "read line: %s\n", str.c_str());
  595. type = newsplit(str);
  596. if (type == STR("-dcc"))
  597. dprintf(DP_DEBUG, STR("Added dcc: %d\n"), dcc_read(stream));
  598. else if (type == STR("-sock"))
  599. dprintf(DP_DEBUG, STR("Added fd: %d\n"), sock_read(stream));
  600. else if (type == STR("+online_since"))
  601. online_since = strtol(str.c_str(), NULL, 10);
  602. else if (type == STR("+server_online"))
  603. server_online = strtol(str.c_str(), NULL, 10);
  604. else if (type == STR("+cursrvname"))
  605. strlcpy(cursrvname, str.c_str(), sizeof(cursrvname));
  606. else if (type == STR("+server_floodless"))
  607. floodless = 1;
  608. else if (type == STR("+in_deaf"))
  609. in_deaf = 1;
  610. else if (type == STR("+in_callerid"))
  611. in_callerid = 1;
  612. else if (type == STR("+chan")) {
  613. bd::String chname = str;
  614. channel_add(NULL, chname.c_str(), NULL);
  615. struct chanset_t* chan = findchan_by_dname(chname.c_str());
  616. strlcpy(chan->name, chan->dname, sizeof(chan->name));
  617. chan->status = chan->ircnet_status = 0;
  618. chan->ircnet_status |= CHAN_PEND;
  619. reset_chans = 2;
  620. }
  621. else if (type == STR("+buildts"))
  622. old_buildts = strtol(str.c_str(), NULL, 10);
  623. else if (type == STR("+botname"))
  624. nick = str.dup();
  625. else if (type == STR("+rolls"))
  626. rolls = atoi(str.c_str());
  627. else if (type == STR("+altnick_char"))
  628. altnick_char = str[0];
  629. else if (type == STR("+burst"))
  630. burst = atoi(str.c_str());
  631. else if (type == STR("+flood_count"))
  632. flood_count = atoi(str.c_str());
  633. else if (type == STR("+my_cookie_counter")) {
  634. my_cookie_counter = strtol(str.c_str(), NULL, 10);
  635. my_cookie_counter += 100; // Increase to avoid race conditions
  636. }
  637. else if (type == STR("+ip4"))
  638. ip4 = str.dup();
  639. else if (type == STR("+ip6"))
  640. ip6 = str.dup();
  641. else if (type == STR("+serv_cache")) {
  642. if (!cached_005 && str.find(STR("005")))
  643. cached_005 = 1;
  644. dprintf(DP_CACHE, "%s", str.c_str());
  645. }
  646. }
  647. restart_time = now;
  648. if (old_buildts && buildts > old_buildts)
  649. restart_was_update = 1;
  650. tell_dcc(DP_DEBUG);
  651. tell_netdebug(DP_DEBUG);
  652. unlink(fname);
  653. if (servidx >= 0) {
  654. char nserv[50] = "";
  655. if ((ip4 && ip6) && (strcmp(ip4, myipstr(AF_INET)) || strcmp(ip6, myipstr(AF_INET6)))) {
  656. if (tands > 0) { /* We're not linked yet.. but for future */
  657. botnet_send_chat(-1, conf.bot->nick, STR("IP changed."));
  658. botnet_send_bye(STR("IP changed."));
  659. }
  660. fatal("brb", 1);
  661. } else if (conf.bot->hub) {
  662. // I became a hub during restart... disconnect from IRC.
  663. if (tands > 0) { /* We're not linked yet.. but for future */
  664. botnet_send_chat(-1, conf.bot->nick, STR("Changing to HUB."));
  665. botnet_send_bye(STR("Changing to HUB."));
  666. }
  667. nuke_server("emoquit");
  668. } else {
  669. simple_snprintf(nserv, sizeof(nserv), "%s:%d", dcc[servidx].host, dcc[servidx].port);
  670. add_server(nserv);
  671. curserv = 0;
  672. keepnick = 0; /* Wait to change nicks until relinking, fixes nick/jupenick switching issues during restart */
  673. reset_flood();
  674. if (!server_online) server_online = now;
  675. rehash_server(cursrvname[0] ? cursrvname : dcc[servidx].host, nick);
  676. if (cached_005)
  677. replay_cache(servidx, NULL);
  678. else
  679. dprintf(DP_DUMP, "VERSION\n");
  680. if (!reset_chans)
  681. reset_chans = 1;
  682. }
  683. }
  684. delete[] nick;
  685. delete[] ip4;
  686. delete[] ip6;
  687. if (jnick)
  688. free(jnick);
  689. if (socksfile)
  690. free(socksfile);
  691. }
  692. /* Update system code
  693. */
  694. void
  695. restart(int idx)
  696. {
  697. const char *reason = updating ? STR("Updating...") : STR("Restarting...");
  698. Tempfile *socks = new Tempfile("socks");
  699. int fd = 0;
  700. sdprintf("%s", reason);
  701. if (tands > 0) {
  702. botnet_send_chat(-1, conf.bot->nick, (char *) reason);
  703. botnet_send_bye(reason);
  704. }
  705. /* kill all connections except STDOUT/server */
  706. for (fd = 0; fd < dcc_total; fd++) {
  707. if (dcc[fd].type && dcc[fd].type != &SERVER_SOCKET && dcc[fd].sock != STDOUT) {
  708. if (dcc[fd].sock >= 0)
  709. killsock(dcc[fd].sock);
  710. lostdcc(fd);
  711. }
  712. }
  713. const char salt1[] = SALT1;
  714. EncryptedStream stream(salt1);
  715. /* write out all leftover dcc[] entries */
  716. for (fd = 0; fd < dcc_total; fd++)
  717. if (dcc[fd].type && dcc[fd].sock != STDOUT)
  718. dcc_write(stream, fd);
  719. /* write out all leftover socklist[] entries */
  720. for (fd = 0; fd < MAXSOCKS; fd++)
  721. if (socklist[fd].sock != STDOUT)
  722. sock_write(stream, fd);
  723. if (server_online) {
  724. if (botname[0])
  725. stream << bd::String::printf(STR("+botname %s\n"), botname);
  726. if (rolls)
  727. stream << bd::String::printf(STR("+rolls %d\n"), rolls);
  728. if (altnick_char)
  729. stream << bd::String::printf(STR("+altnick_char %c\n"), altnick_char);
  730. if (burst)
  731. stream << bd::String::printf(STR("+burst %d\n"), burst);
  732. if (flood_count)
  733. stream << bd::String::printf(STR("+flood_count %d\n"), flood_count);
  734. if (my_cookie_counter)
  735. stream << bd::String::printf(STR("+my_cookie_counter %lu\n"), my_cookie_counter);
  736. stream << bd::String::printf(STR("+server_online %li\n"), (long)server_online);
  737. stream << bd::String::printf(STR("+cursrvname %s\n"), cursrvname);
  738. }
  739. stream << bd::String::printf(STR("+online_since %li\n"), (long)online_since);
  740. if (floodless)
  741. stream << bd::String::printf(STR("+server_floodless %d\n"), floodless);
  742. if (in_deaf)
  743. stream << bd::String::printf(STR("+in_deaf\n"));
  744. if (in_callerid)
  745. stream << bd::String::printf(STR("+in_callerid\n"));
  746. for (struct chanset_t *chan = chanset; chan; chan = chan->next)
  747. if (shouldjoin(chan) && (channel_active(chan) || channel_pending(chan)))
  748. stream << bd::String::printf(STR("+chan %s\n"), chan->dname);
  749. stream << bd::String::printf(STR("+buildts %li\n"), (long)buildts);
  750. stream << bd::String::printf(STR("+ip4 %s\n"), myipstr(AF_INET));
  751. stream << bd::String::printf(STR("+ip6 %s\n"), myipstr(AF_INET6));
  752. replay_cache(-1, &stream);
  753. stream.writeFile(socks->fd);
  754. socks->my_close();
  755. write_userfile(idx);
  756. /*
  757. if (server_online) {
  758. do_chanset(NULL, NULL, STR("+inactive"), DO_LOCAL);
  759. dprintf(DP_DUMP, STR("JOIN 0\n"));
  760. }
  761. */
  762. fixmod(binname);
  763. /* replace image now */
  764. char *argv[4] = { NULL, NULL, NULL, NULL };
  765. argv[0] = strdup(binname);
  766. if (!backgrd || term_z || sdebug) {
  767. char shit[7] = "";
  768. simple_snprintf(shit, sizeof(shit), STR("-%s%s%s"), !backgrd ? "n" : "", term_z ? "t" : "", sdebug ? "D" : "");
  769. argv[1] = strdup(shit);
  770. argv[2] = strdup(conf.bot->nick);
  771. } else {
  772. argv[1] = strdup(conf.bot->nick);
  773. }
  774. unlink(conf.bot->pid_file);
  775. FILE *fp = NULL;
  776. if (!(fp = fopen(conf.bot->pid_file, "w")))
  777. return;
  778. fprintf(fp, "%d %s\n", getpid(), socks->file);
  779. fclose(fp);
  780. execvp(argv[0], &argv[0]);
  781. /* hopefully this is never reached */
  782. putlog(LOG_MISC, "*", STR("Could not restart: %s"), strerror(errno));
  783. return;
  784. }
  785. #ifdef NO
  786. void
  787. hard_restart(int idx)
  788. {
  789. write_userfile(idx);
  790. if (!conf.bot->hub) {
  791. nuke_server((char *) reason); /* let's drop the server connection ASAP */
  792. cycle_time = 0;
  793. }
  794. fatal(idx <= 0x7FF0 ? reason : NULL, 1);
  795. usleep(2000 * 500);
  796. unlink(conf.bot->pid_file); /* if this fails it is ok, cron will restart the bot, *hopefully* */
  797. simple_exec(binname, conf.bot->nick);
  798. exit(0);
  799. }
  800. #endif
  801. int updatebin(int idx, char *par, int secs)
  802. {
  803. if (!par || !par[0]) {
  804. logidx(idx, "Not enough parameters.");
  805. return 1;
  806. }
  807. size_t path_siz = strlen(binname) + strlen(par) + 2;
  808. char *path = (char *) my_calloc(1, path_siz);
  809. char *newbin = NULL, buf[DIRMAX] = "";
  810. const char* argv[5];
  811. int i;
  812. strlcpy(path, binname, path_siz);
  813. newbin = strrchr(path, '/');
  814. if (!newbin) {
  815. free(path);
  816. logidx(idx, STR("Don't know current binary name"));
  817. return 1;
  818. }
  819. newbin++;
  820. if (strchr(par, '/')) {
  821. *newbin = 0;
  822. logidx(idx, STR("New binary must be in %s and name must be specified without path information"), path);
  823. free(path);
  824. return 1;
  825. }
  826. strcpy(newbin, par);
  827. if (!strcmp(path, binname)) {
  828. free(path);
  829. logidx(idx, STR("Can't update with the current binary"));
  830. return 1;
  831. }
  832. if (!can_stat(path)) {
  833. logidx(idx, STR("%s can't be accessed"), path);
  834. free(path);
  835. return 1;
  836. }
  837. if (fixmod(path)) {
  838. logidx(idx, STR("Can't set mode 0600 on %s"), path);
  839. free(path);
  840. return 1;
  841. }
  842. /* Check if the new binary is compatible */
  843. int initialized_code = check_bin_initialized(path);
  844. if (initialized_code == 2) {
  845. logidx(idx, STR("New binary is corrupted or the wrong architecture/operating system."));
  846. free(path);
  847. return 1;
  848. } else if (initialized_code == 1 && !check_bin_compat(path)) {
  849. logidx(idx, STR("New binary must be initialized as pack structure has been changed in new version."));
  850. free(path);
  851. return 1;
  852. }
  853. /* make a backup just in case. */
  854. simple_snprintf(buf, sizeof(buf), STR("%s/.bin.old"), conf.datadir);
  855. copyfile(binname, buf);
  856. write_settings(path, -1, 0, initialized_code ? 0 : 1); /* re-write the binary with our packdata */
  857. Tempfile *conffile = new Tempfile("conf");
  858. if (writeconf(NULL, conffile->fd, CONF_ENC)) {
  859. logidx(idx, STR("Failed to write temporary config file for update."));
  860. delete conffile;
  861. return 1;
  862. }
  863. /* The binary should return '2' when ran with -2, if not it's probably corrupt. */
  864. putlog(LOG_DEBUG, "*", STR("Running for update binary test: %s -2"), path);
  865. argv[0] = path;
  866. argv[1] = "-2";
  867. argv[2] = 0;
  868. i = simple_exec(argv);
  869. if (i == -1 || WEXITSTATUS(i) != 2) {
  870. logidx(idx, STR("Couldn't restart new binary (error %d)"), i);
  871. delete conffile;
  872. return i;
  873. }
  874. /* now to send our config to the new binary */
  875. putlog(LOG_DEBUG, "*", STR("Running for update conf: %s -4 %s"), path, conffile->file);
  876. argv[0] = path;
  877. argv[1] = "-4";
  878. argv[2] = conffile->file;
  879. argv[3] = 0;
  880. i = simple_exec(argv);
  881. delete conffile;
  882. if (i == -1 || WEXITSTATUS(i) != 6) { /* 6 for successfull config read/write */
  883. logidx(idx, STR("Couldn't pass config to new binary (error %d)"), i);
  884. return i;
  885. }
  886. if (movefile(path, binname)) {
  887. logidx(idx, STR("Can't rename %s to %s"), path, binname);
  888. free(path);
  889. return 1;
  890. }
  891. if (updating == UPDATE_EXIT) { /* dont restart/kill/spawn bots, just die ! */
  892. printf(STR("* Moved binary to: %s\n"), binname);
  893. fatal(STR("Binary updated."), 0);
  894. }
  895. if (updating == UPDATE_AUTO) {
  896. /* Make all other bots do a soft restart */
  897. conf_checkpids(conf.bots);
  898. conf_killbot(conf.bots, NULL, NULL, SIGHUP);
  899. if (conf.bot->pid)
  900. kill(conf.bot->pid, SIGHUP);
  901. exit(0);
  902. }
  903. if (!conf.bot->hub && secs > 0) {
  904. /* Make all other bots do a soft restart */
  905. conf_checkpids(conf.bots);
  906. conf_killbot(conf.bots, NULL, NULL, SIGHUP);
  907. /* invoked with -u */
  908. if (updating == UPDATE_AUTO) {
  909. if (conf.bot->pid)
  910. kill(conf.bot->pid, SIGHUP);
  911. exit(0);
  912. }
  913. /* this odd statement makes it so specifying 1 sec will restart other bots running
  914. * and then just restart with no delay */
  915. updating = UPDATE_AUTO;
  916. if (secs > 1) {
  917. egg_timeval_t howlong;
  918. howlong.sec = secs;
  919. howlong.usec = 0;
  920. timer_create_complex(&howlong, STR("restarting for update"), (Function) restart, (void *) (long) idx, 0);
  921. } else
  922. restart(idx);
  923. return 0;
  924. } else
  925. restart(idx); /* no timer */
  926. /* this should never be reached */
  927. return 2;
  928. }
  929. int bot_aggressive_to(struct userrec *u)
  930. {
  931. if (conf.bot->localhub) {
  932. for (conf_bot* bot = conf.bots; bot && bot->nick; bot = bot->next) {
  933. if (!bot->hub && !strcmp(u->handle, bot->nick))
  934. return 1;
  935. }
  936. }
  937. char mypval[HANDLEN + 3 + 1] = "", botpval[HANDLEN + 3 + 1] = "";
  938. link_pref_val(u, botpval);
  939. link_pref_val(conf.bot->u, mypval);
  940. // sdprintf("botpval: %s", botpval);
  941. // sdprintf("mypval: %s", mypval);
  942. if (strcmp(mypval, botpval) < 0)
  943. return 1;
  944. else
  945. return 0;
  946. }
  947. int goodpass(const char *pass, int idx, char *nick)
  948. {
  949. if (!pass[0])
  950. return 0;
  951. char tell[201] = "", last = 0;
  952. int nalpha = 0, lcase = 0, ucase = 0, ocase = 0, tc, repeats = 0, score = 0;
  953. size_t length = strlen(pass);
  954. if (strchr(BADPASSCHARS, pass[0])) {
  955. simple_snprintf(tell, sizeof(tell), "Passes may not begin with '-'");
  956. goto fail;
  957. }
  958. for (int i = 0; i < (signed) length; i++) {
  959. if (pass[i] == last)
  960. repeats++;
  961. tc = (int) pass[i];
  962. if (tc < 58 && tc > 47)
  963. ocase++; /* number */
  964. else if (tc < 91 && tc > 64)
  965. ucase++; /* upper case */
  966. else if (tc < 123 && tc > 96)
  967. lcase++; /* lower case */
  968. else
  969. nalpha++; /* non-alphabet/number */
  970. last = pass[i];
  971. }
  972. score += ocase * 3;
  973. score += nalpha * 2;
  974. score += ucase * 2;
  975. score += lcase * 1;
  976. score += length * 1;
  977. score -= repeats * 1;
  978. simple_snprintf(tell, sizeof(tell), "Password NOT set due to being too weak. Try more characters, numbers, capitals");
  979. if (score < 16) {
  980. fail:
  981. if (idx)
  982. dprintf(idx, "%s\n", tell);
  983. else if (nick[0])
  984. notice(nick, tell, DP_HELP);
  985. return 0;
  986. }
  987. return 1;
  988. }
  989. #define REPLACES 10
  990. char *replace(const char *string, const char *oldie, const char *newbie)
  991. {
  992. if (string == NULL || !string[0])
  993. return (char *) string;
  994. if (oldie == NULL || !oldie[0])
  995. return (char *) string;
  996. char *c = NULL;
  997. if ((c = (char *) strstr(string, oldie)) == NULL)
  998. return (char *) string;
  999. static int n = 0;
  1000. static char newstring_buf[REPLACES][1024];
  1001. char *newstring = newstring_buf[n++];
  1002. memset(newstring, 0, 1024);
  1003. if (n == REPLACES)
  1004. n = 0;
  1005. const size_t new_len = strlen(newbie), old_len = strlen(oldie), end = (strlen(string) - old_len);
  1006. size_t str_index = 0, newstr_index = 0, oldie_index = c - string, cpy_len;
  1007. while(str_index <= end && c != NULL) {
  1008. cpy_len = oldie_index-str_index;
  1009. strncpy(newstring + newstr_index, string + str_index, cpy_len);
  1010. newstr_index += cpy_len;
  1011. str_index += cpy_len;
  1012. strcpy(newstring + newstr_index, newbie);
  1013. newstr_index += new_len;
  1014. str_index += old_len;
  1015. if((c = (char *) strstr(string + str_index, oldie)) != NULL)
  1016. oldie_index = c - string;
  1017. }
  1018. strcpy(newstring + newstr_index, string + str_index);
  1019. return (newstring);
  1020. }
  1021. char* replace_vars(char *buf) {
  1022. return replace(buf, "$n", botname);
  1023. }
  1024. void showhelp(int idx, struct flag_record *flags, const char *string)
  1025. {
  1026. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  1027. size_t help_siz = strlen(string) + 1000 + 1;
  1028. char *helpstr = (char *) my_calloc(1, help_siz);
  1029. char tmp[2] = "", flagstr[10] = "";
  1030. bool ok = 1;
  1031. while (string && string[0]) {
  1032. if (*string == '%') {
  1033. if (!strncmp(string + 1, "{+", 2)) {
  1034. while (*string && *string != '+') {
  1035. string++;
  1036. }
  1037. flagstr[0] = 0;
  1038. while (*string && *string != '}') {
  1039. simple_snprintf(tmp, sizeof(tmp), "%c", *string);
  1040. strlcat(flagstr, tmp, sizeof(flagstr));
  1041. string++;
  1042. }
  1043. string++;
  1044. break_down_flags(flagstr, &fr, NULL);
  1045. if (flagrec_ok(&fr, flags)) {
  1046. ok = 1;
  1047. while (*string && *string != '%') {
  1048. simple_snprintf(tmp, sizeof(tmp), "%c", *string);
  1049. strlcat(helpstr, tmp, help_siz);
  1050. string++;
  1051. }
  1052. if (!strncmp(string + 1, "{-", 2)) {
  1053. ok = 1;
  1054. while (*string && *string != '}') {
  1055. string++;
  1056. }
  1057. string++;
  1058. }
  1059. } else {
  1060. ok = 0;
  1061. }
  1062. } else if (!strncmp(string + 1, "{-", 2)) {
  1063. ok = 1;
  1064. while (*string && *string != '}') {
  1065. string++;
  1066. }
  1067. string++;
  1068. } else if (*string == '{') {
  1069. while (*string && *string != '}') {
  1070. string++;
  1071. }
  1072. } else if (*(string + 1) == 'd') {
  1073. string += 2;
  1074. if (dcc[idx].u.chat->channel >= 0)
  1075. strlcat(helpstr, settings.dcc_prefix, help_siz);
  1076. } else if (*(string + 1) == '%') {
  1077. string += 2;
  1078. strlcat(helpstr, "%", help_siz);
  1079. } else {
  1080. if (ok) {
  1081. simple_snprintf(tmp, sizeof(tmp), "%c", *string);
  1082. strlcat(helpstr, tmp, help_siz);
  1083. }
  1084. string++;
  1085. }
  1086. } else {
  1087. if (ok) {
  1088. simple_snprintf(tmp, sizeof(tmp), "%c", *string);
  1089. strlcat(helpstr, tmp, help_siz);
  1090. }
  1091. string++;
  1092. }
  1093. }
  1094. helpstr[strlen(helpstr)] = 0;
  1095. if (helpstr[0]) dumplots(idx, "", helpstr);
  1096. free(helpstr);
  1097. }
  1098. /* Arrange the N elements of ARRAY in random order. */
  1099. void shuffleArray(char* array[], size_t n)
  1100. {
  1101. for (size_t i = 0; i < n; i++) {
  1102. const size_t j = randint(n);
  1103. char* temp = array[j];
  1104. array[j] = array[i];
  1105. array[i] = temp;
  1106. }
  1107. }
  1108. void shuffle(char *string, char *delim, size_t str_len)
  1109. {
  1110. char *array[501], *str = NULL, *work = NULL;
  1111. size_t len = 0;
  1112. bzero(&array, sizeof array);
  1113. work = strdup(string);
  1114. str = strtok(work, delim);
  1115. while(str && *str)
  1116. {
  1117. array[len] = str;
  1118. len++;
  1119. str = strtok((char*) NULL, delim);
  1120. }
  1121. shuffleArray(array, len);
  1122. string[0] = 0;
  1123. for (size_t i = 0; i < len; i++) {
  1124. strlcat(string, array[i], str_len);
  1125. if (i != len - 1)
  1126. strlcat(string, delim, str_len);
  1127. }
  1128. free(work);
  1129. string[strlen(string)] = 0;
  1130. }
  1131. /* returns
  1132. 1: use ANSI
  1133. 2: use mIRC
  1134. 0: neither
  1135. */
  1136. int
  1137. coloridx(int idx)
  1138. {
  1139. if (idx == -1) { /* who cares, just show color! */
  1140. return 1; /* ANSI */
  1141. } else if (idx == -2) {
  1142. return 2; /* mIRC */
  1143. /* valid idx and NOT relaying */
  1144. } else if (idx >= 0) {
  1145. if (dcc[idx].irc || dcc[idx].bot) {
  1146. return 0;
  1147. } else if ((dcc[idx].status & STAT_COLOR) && (dcc[idx].type && dcc[idx].type != &DCC_RELAYING)) {
  1148. /* telnet probably wants ANSI, even though it might be a relay from an mIRC client; fuck`em */
  1149. if (dcc[idx].status & STAT_TELNET)
  1150. return 1;
  1151. /* non-telnet is probably a /dcc-chat, most irc clients support mIRC codes... */
  1152. else
  1153. return 2;
  1154. }
  1155. }
  1156. return 0;
  1157. }
  1158. const char *
  1159. color(int idx, int type, int which)
  1160. {
  1161. int ansi = 0;
  1162. /* if user is connected over TELNET or !backgrd, show ANSI
  1163. * if they are relaying, they are most likely on an IRC client and should have mIRC codes
  1164. */
  1165. if ((ansi = coloridx(idx)) == 0)
  1166. return "";
  1167. if (ansi == 2)
  1168. ansi = 0;
  1169. if (type == BOLD_OPEN) {
  1170. return ansi ? "\033[1m" : "\002";
  1171. } else if (type == BOLD_CLOSE) {
  1172. // return ansi ? "\033[22m" : "\002";
  1173. return ansi ? "\033[0m" : "\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 (which) {
  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. }
  1208. char *
  1209. strtolower(char *s)
  1210. {
  1211. char *p = s;
  1212. while (*p) {
  1213. *p = tolower(*p);
  1214. p++;
  1215. }
  1216. return s;
  1217. }
  1218. char *
  1219. strtoupper(char *s)
  1220. {
  1221. char *p = s;
  1222. while (*p) {
  1223. *p = toupper(*p);
  1224. p++;
  1225. }
  1226. return s;
  1227. }
  1228. char *step_thru_file(FILE *fd)
  1229. {
  1230. if (fd == NULL) {
  1231. return NULL;
  1232. }
  1233. char tempBuf[1024] = "", *retStr = NULL;
  1234. size_t ret_siz = 0;
  1235. while (!feof(fd)) {
  1236. if (fgets(tempBuf, sizeof(tempBuf), fd) && !feof(fd)) {
  1237. if (retStr == NULL) {
  1238. ret_siz = strlen(tempBuf) + 2;
  1239. retStr = (char *) my_calloc(1, ret_siz);
  1240. strlcpy(retStr, tempBuf, ret_siz);
  1241. } else {
  1242. ret_siz = strlen(retStr) + strlen(tempBuf);
  1243. retStr = (char *) my_realloc(retStr, ret_siz);
  1244. strlcat(retStr, tempBuf, ret_siz);
  1245. }
  1246. if (retStr[strlen(retStr)-1] == '\n') {
  1247. retStr[strlen(retStr)-1] = 0;
  1248. break;
  1249. }
  1250. }
  1251. }
  1252. return retStr;
  1253. }
  1254. char *trim(char *string)
  1255. {
  1256. if (string) {
  1257. char *ibuf = NULL, *obuf = NULL;
  1258. for (ibuf = obuf = string; *ibuf; ) {
  1259. while (*ibuf && (isspace (*ibuf)))
  1260. ibuf++;
  1261. if (*ibuf && (obuf != string))
  1262. *(obuf++) = ' ';
  1263. while (*ibuf && (!isspace (*ibuf)))
  1264. *(obuf++) = *(ibuf++);
  1265. }
  1266. *obuf = '\0';
  1267. }
  1268. return (string);
  1269. }
  1270. int skipline (char *line, int *skip) {
  1271. static int multi = 0;
  1272. if (strstr(line, "*/"))
  1273. multi = 0;
  1274. if ( (!strncmp(line, "#", 1)) || (!strncmp(line, ";", 1)) || (!strncmp(line, "//", 2)) ) {
  1275. (*skip)++;
  1276. } else if ( (strstr(line, "/*")) && (strstr(line, "*/")) ) {
  1277. multi = 0;
  1278. (*skip)++;
  1279. } else if ( (strstr(line, "/*")) ) {
  1280. (*skip)++;
  1281. multi = 1;
  1282. } else if ( (strstr(line, "*/")) ) {
  1283. multi = 0;
  1284. } else {
  1285. if (!multi) (*skip) = 0;
  1286. }
  1287. return (*skip);
  1288. }