misc.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2014 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("+server_floodless"))
  605. floodless = 1;
  606. else if (type == STR("+in_deaf"))
  607. in_deaf = 1;
  608. else if (type == STR("+in_callerid"))
  609. in_callerid = 1;
  610. else if (type == STR("+chan")) {
  611. bd::String chname = str;
  612. channel_add(NULL, chname.c_str(), NULL);
  613. struct chanset_t* chan = findchan_by_dname(chname.c_str());
  614. strlcpy(chan->name, chan->dname, sizeof(chan->name));
  615. chan->status = chan->ircnet_status = 0;
  616. chan->ircnet_status |= CHAN_PEND;
  617. reset_chans = 2;
  618. }
  619. else if (type == STR("+buildts"))
  620. old_buildts = strtol(str.c_str(), NULL, 10);
  621. else if (type == STR("+botname"))
  622. nick = str.dup();
  623. else if (type == STR("+rolls"))
  624. rolls = atoi(str.c_str());
  625. else if (type == STR("+altnick_char"))
  626. altnick_char = str[0];
  627. else if (type == STR("+burst"))
  628. burst = atoi(str.c_str());
  629. else if (type == STR("+flood_count"))
  630. flood_count = atoi(str.c_str());
  631. else if (type == STR("+my_cookie_counter")) {
  632. my_cookie_counter = strtol(str.c_str(), NULL, 10);
  633. my_cookie_counter += 100; // Increase to avoid race conditions
  634. }
  635. else if (type == STR("+ip4"))
  636. ip4 = str.dup();
  637. else if (type == STR("+ip6"))
  638. ip6 = str.dup();
  639. else if (type == STR("+serv_cache")) {
  640. if (!cached_005 && str.find(STR("005")))
  641. cached_005 = 1;
  642. dprintf(DP_CACHE, "%s", str.c_str());
  643. }
  644. }
  645. restart_time = now;
  646. if (old_buildts && buildts > old_buildts)
  647. restart_was_update = 1;
  648. tell_dcc(DP_DEBUG);
  649. tell_netdebug(DP_DEBUG);
  650. unlink(fname);
  651. if (servidx >= 0) {
  652. char nserv[50] = "";
  653. if ((ip4 && ip6) && (strcmp(ip4, myipstr(AF_INET)) || strcmp(ip6, myipstr(AF_INET6)))) {
  654. if (tands > 0) { /* We're not linked yet.. but for future */
  655. botnet_send_chat(-1, conf.bot->nick, STR("IP changed."));
  656. botnet_send_bye(STR("IP changed."));
  657. }
  658. fatal("brb", 1);
  659. } else if (conf.bot->hub) {
  660. // I became a hub during restart... disconnect from IRC.
  661. if (tands > 0) { /* We're not linked yet.. but for future */
  662. botnet_send_chat(-1, conf.bot->nick, STR("Changing to HUB."));
  663. botnet_send_bye(STR("Changing to HUB."));
  664. }
  665. nuke_server("emoquit");
  666. } else {
  667. simple_snprintf(nserv, sizeof(nserv), "%s:%d", dcc[servidx].host, dcc[servidx].port);
  668. add_server(nserv);
  669. curserv = 0;
  670. keepnick = 0; /* Wait to change nicks until relinking, fixes nick/jupenick switching issues during restart */
  671. reset_flood();
  672. if (!server_online) server_online = now;
  673. rehash_server(dcc[servidx].host, nick);
  674. if (cached_005)
  675. replay_cache(servidx, NULL);
  676. else
  677. dprintf(DP_DUMP, "VERSION\n");
  678. if (!reset_chans)
  679. reset_chans = 1;
  680. }
  681. }
  682. delete[] nick;
  683. delete[] ip4;
  684. delete[] ip6;
  685. if (jnick)
  686. free(jnick);
  687. if (socksfile)
  688. free(socksfile);
  689. }
  690. /* Update system code
  691. */
  692. void
  693. restart(int idx)
  694. {
  695. const char *reason = updating ? STR("Updating...") : STR("Restarting...");
  696. Tempfile *socks = new Tempfile("socks");
  697. int fd = 0;
  698. sdprintf("%s", reason);
  699. if (tands > 0) {
  700. botnet_send_chat(-1, conf.bot->nick, (char *) reason);
  701. botnet_send_bye(reason);
  702. }
  703. /* kill all connections except STDOUT/server */
  704. for (fd = 0; fd < dcc_total; fd++) {
  705. if (dcc[fd].type && dcc[fd].type != &SERVER_SOCKET && dcc[fd].sock != STDOUT) {
  706. if (dcc[fd].sock >= 0)
  707. killsock(dcc[fd].sock);
  708. lostdcc(fd);
  709. }
  710. }
  711. const char salt1[] = SALT1;
  712. EncryptedStream stream(salt1);
  713. /* write out all leftover dcc[] entries */
  714. for (fd = 0; fd < dcc_total; fd++)
  715. if (dcc[fd].type && dcc[fd].sock != STDOUT)
  716. dcc_write(stream, fd);
  717. /* write out all leftover socklist[] entries */
  718. for (fd = 0; fd < MAXSOCKS; fd++)
  719. if (socklist[fd].sock != STDOUT)
  720. sock_write(stream, fd);
  721. if (server_online) {
  722. if (botname[0])
  723. stream << bd::String::printf(STR("+botname %s\n"), botname);
  724. if (rolls)
  725. stream << bd::String::printf(STR("+rolls %d\n"), rolls);
  726. if (altnick_char)
  727. stream << bd::String::printf(STR("+altnick_char %c\n"), altnick_char);
  728. if (burst)
  729. stream << bd::String::printf(STR("+burst %d\n"), burst);
  730. if (flood_count)
  731. stream << bd::String::printf(STR("+flood_count %d\n"), flood_count);
  732. if (my_cookie_counter)
  733. stream << bd::String::printf(STR("+my_cookie_counter %lu\n"), my_cookie_counter);
  734. stream << bd::String::printf(STR("+server_online %li\n"), (long)server_online);
  735. }
  736. stream << bd::String::printf(STR("+online_since %li\n"), (long)online_since);
  737. if (floodless)
  738. stream << bd::String::printf(STR("+server_floodless %d\n"), floodless);
  739. if (in_deaf)
  740. stream << bd::String::printf(STR("+in_deaf\n"));
  741. if (in_callerid)
  742. stream << bd::String::printf(STR("+in_callerid\n"));
  743. for (struct chanset_t *chan = chanset; chan; chan = chan->next)
  744. if (shouldjoin(chan) && (channel_active(chan) || channel_pending(chan)))
  745. stream << bd::String::printf(STR("+chan %s\n"), chan->dname);
  746. stream << bd::String::printf(STR("+buildts %li\n"), (long)buildts);
  747. stream << bd::String::printf(STR("+ip4 %s\n"), myipstr(AF_INET));
  748. stream << bd::String::printf(STR("+ip6 %s\n"), myipstr(AF_INET6));
  749. replay_cache(-1, &stream);
  750. stream.writeFile(socks->fd);
  751. socks->my_close();
  752. write_userfile(idx);
  753. /*
  754. if (server_online) {
  755. do_chanset(NULL, NULL, STR("+inactive"), DO_LOCAL);
  756. dprintf(DP_DUMP, STR("JOIN 0\n"));
  757. }
  758. */
  759. fixmod(binname);
  760. /* replace image now */
  761. char *argv[4] = { NULL, NULL, NULL, NULL };
  762. argv[0] = strdup(binname);
  763. if (!backgrd || term_z || sdebug) {
  764. char shit[7] = "";
  765. simple_snprintf(shit, sizeof(shit), STR("-%s%s%s"), !backgrd ? "n" : "", term_z ? "t" : "", sdebug ? "D" : "");
  766. argv[1] = strdup(shit);
  767. argv[2] = strdup(conf.bot->nick);
  768. } else {
  769. argv[1] = strdup(conf.bot->nick);
  770. }
  771. unlink(conf.bot->pid_file);
  772. FILE *fp = NULL;
  773. if (!(fp = fopen(conf.bot->pid_file, "w")))
  774. return;
  775. fprintf(fp, "%d %s\n", getpid(), socks->file);
  776. fclose(fp);
  777. execvp(argv[0], &argv[0]);
  778. /* hopefully this is never reached */
  779. putlog(LOG_MISC, "*", STR("Could not restart: %s"), strerror(errno));
  780. return;
  781. }
  782. #ifdef NO
  783. void
  784. hard_restart(int idx)
  785. {
  786. write_userfile(idx);
  787. if (!conf.bot->hub) {
  788. nuke_server((char *) reason); /* let's drop the server connection ASAP */
  789. cycle_time = 0;
  790. }
  791. fatal(idx <= 0x7FF0 ? reason : NULL, 1);
  792. usleep(2000 * 500);
  793. unlink(conf.bot->pid_file); /* if this fails it is ok, cron will restart the bot, *hopefully* */
  794. simple_exec(binname, conf.bot->nick);
  795. exit(0);
  796. }
  797. #endif
  798. int updatebin(int idx, char *par, int secs)
  799. {
  800. if (!par || !par[0]) {
  801. logidx(idx, "Not enough parameters.");
  802. return 1;
  803. }
  804. size_t path_siz = strlen(binname) + strlen(par) + 2;
  805. char *path = (char *) my_calloc(1, path_siz);
  806. char *newbin = NULL, buf[DIRMAX] = "";
  807. const char* argv[5];
  808. int i;
  809. strlcpy(path, binname, path_siz);
  810. newbin = strrchr(path, '/');
  811. if (!newbin) {
  812. free(path);
  813. logidx(idx, STR("Don't know current binary name"));
  814. return 1;
  815. }
  816. newbin++;
  817. if (strchr(par, '/')) {
  818. *newbin = 0;
  819. logidx(idx, STR("New binary must be in %s and name must be specified without path information"), path);
  820. free(path);
  821. return 1;
  822. }
  823. strcpy(newbin, par);
  824. if (!strcmp(path, binname)) {
  825. free(path);
  826. logidx(idx, STR("Can't update with the current binary"));
  827. return 1;
  828. }
  829. if (!can_stat(path)) {
  830. logidx(idx, STR("%s can't be accessed"), path);
  831. free(path);
  832. return 1;
  833. }
  834. if (fixmod(path)) {
  835. logidx(idx, STR("Can't set mode 0600 on %s"), path);
  836. free(path);
  837. return 1;
  838. }
  839. /* Check if the new binary is compatible */
  840. int initialized_code = check_bin_initialized(path);
  841. if (initialized_code == 2) {
  842. logidx(idx, STR("New binary is corrupted or the wrong architecture/operating system."));
  843. free(path);
  844. return 1;
  845. } else if (initialized_code == 1 && !check_bin_compat(path)) {
  846. logidx(idx, STR("New binary must be initialized as pack structure has been changed in new version."));
  847. free(path);
  848. return 1;
  849. }
  850. /* make a backup just in case. */
  851. simple_snprintf(buf, sizeof(buf), STR("%s/.bin.old"), conf.datadir);
  852. copyfile(binname, buf);
  853. write_settings(path, -1, 0, initialized_code ? 0 : 1); /* re-write the binary with our packdata */
  854. Tempfile *conffile = new Tempfile("conf");
  855. if (writeconf(NULL, conffile->fd, CONF_ENC)) {
  856. logidx(idx, STR("Failed to write temporary config file for update."));
  857. delete conffile;
  858. return 1;
  859. }
  860. /* The binary should return '2' when ran with -2, if not it's probably corrupt. */
  861. putlog(LOG_DEBUG, "*", STR("Running for update binary test: %s -2"), path);
  862. argv[0] = path;
  863. argv[1] = "-2";
  864. argv[2] = 0;
  865. i = simple_exec(argv);
  866. if (i == -1 || WEXITSTATUS(i) != 2) {
  867. logidx(idx, STR("Couldn't restart new binary (error %d)"), i);
  868. delete conffile;
  869. return i;
  870. }
  871. /* now to send our config to the new binary */
  872. putlog(LOG_DEBUG, "*", STR("Running for update conf: %s -4 %s"), path, conffile->file);
  873. argv[0] = path;
  874. argv[1] = "-4";
  875. argv[2] = conffile->file;
  876. argv[3] = 0;
  877. i = simple_exec(argv);
  878. delete conffile;
  879. if (i == -1 || WEXITSTATUS(i) != 6) { /* 6 for successfull config read/write */
  880. logidx(idx, STR("Couldn't pass config to new binary (error %d)"), i);
  881. return i;
  882. }
  883. if (movefile(path, binname)) {
  884. logidx(idx, STR("Can't rename %s to %s"), path, binname);
  885. free(path);
  886. return 1;
  887. }
  888. if (updating == UPDATE_EXIT) { /* dont restart/kill/spawn bots, just die ! */
  889. printf(STR("* Moved binary to: %s\n"), binname);
  890. fatal(STR("Binary updated."), 0);
  891. }
  892. if (updating == UPDATE_AUTO) {
  893. /* Make all other bots do a soft restart */
  894. conf_checkpids(conf.bots);
  895. conf_killbot(conf.bots, NULL, NULL, SIGHUP);
  896. if (conf.bot->pid)
  897. kill(conf.bot->pid, SIGHUP);
  898. exit(0);
  899. }
  900. if (!conf.bot->hub && secs > 0) {
  901. /* Make all other bots do a soft restart */
  902. conf_checkpids(conf.bots);
  903. conf_killbot(conf.bots, NULL, NULL, SIGHUP);
  904. /* invoked with -u */
  905. if (updating == UPDATE_AUTO) {
  906. if (conf.bot->pid)
  907. kill(conf.bot->pid, SIGHUP);
  908. exit(0);
  909. }
  910. /* this odd statement makes it so specifying 1 sec will restart other bots running
  911. * and then just restart with no delay */
  912. updating = UPDATE_AUTO;
  913. if (secs > 1) {
  914. egg_timeval_t howlong;
  915. howlong.sec = secs;
  916. howlong.usec = 0;
  917. timer_create_complex(&howlong, STR("restarting for update"), (Function) restart, (void *) (long) idx, 0);
  918. } else
  919. restart(idx);
  920. return 0;
  921. } else
  922. restart(idx); /* no timer */
  923. /* this should never be reached */
  924. return 2;
  925. }
  926. int bot_aggressive_to(struct userrec *u)
  927. {
  928. if (conf.bot->localhub) {
  929. for (conf_bot* bot = conf.bots; bot && bot->nick; bot = bot->next) {
  930. if (!bot->hub && !strcmp(u->handle, bot->nick))
  931. return 1;
  932. }
  933. }
  934. char mypval[HANDLEN + 3 + 1] = "", botpval[HANDLEN + 3 + 1] = "";
  935. link_pref_val(u, botpval);
  936. link_pref_val(conf.bot->u, mypval);
  937. // sdprintf("botpval: %s", botpval);
  938. // sdprintf("mypval: %s", mypval);
  939. if (strcmp(mypval, botpval) < 0)
  940. return 1;
  941. else
  942. return 0;
  943. }
  944. int goodpass(const char *pass, int idx, char *nick)
  945. {
  946. if (!pass[0])
  947. return 0;
  948. char tell[201] = "", last = 0;
  949. int nalpha = 0, lcase = 0, ucase = 0, ocase = 0, tc, repeats = 0, score = 0;
  950. size_t length = strlen(pass);
  951. if (strchr(BADPASSCHARS, pass[0])) {
  952. simple_snprintf(tell, sizeof(tell), "Passes may not begin with '-'");
  953. goto fail;
  954. }
  955. for (int i = 0; i < (signed) length; i++) {
  956. if (pass[i] == last)
  957. repeats++;
  958. tc = (int) pass[i];
  959. if (tc < 58 && tc > 47)
  960. ocase++; /* number */
  961. else if (tc < 91 && tc > 64)
  962. ucase++; /* upper case */
  963. else if (tc < 123 && tc > 96)
  964. lcase++; /* lower case */
  965. else
  966. nalpha++; /* non-alphabet/number */
  967. last = pass[i];
  968. }
  969. score += ocase * 3;
  970. score += nalpha * 2;
  971. score += ucase * 2;
  972. score += lcase * 1;
  973. score += length * 1;
  974. score -= repeats * 1;
  975. simple_snprintf(tell, sizeof(tell), "Password NOT set due to being too weak. Try more characters, numbers, capitals");
  976. if (score < 16) {
  977. fail:
  978. if (idx)
  979. dprintf(idx, "%s\n", tell);
  980. else if (nick[0])
  981. notice(nick, tell, DP_HELP);
  982. return 0;
  983. }
  984. return 1;
  985. }
  986. #define REPLACES 10
  987. char *replace(const char *string, const char *oldie, const char *newbie)
  988. {
  989. if (string == NULL || !string[0])
  990. return (char *) string;
  991. if (oldie == NULL || !oldie[0])
  992. return (char *) string;
  993. char *c = NULL;
  994. if ((c = (char *) strstr(string, oldie)) == NULL)
  995. return (char *) string;
  996. static int n = 0;
  997. static char newstring_buf[REPLACES][1024];
  998. char *newstring = newstring_buf[n++];
  999. memset(newstring, 0, 1024);
  1000. if (n == REPLACES)
  1001. n = 0;
  1002. const size_t new_len = strlen(newbie), old_len = strlen(oldie), end = (strlen(string) - old_len);
  1003. size_t str_index = 0, newstr_index = 0, oldie_index = c - string, cpy_len;
  1004. while(str_index <= end && c != NULL) {
  1005. cpy_len = oldie_index-str_index;
  1006. strncpy(newstring + newstr_index, string + str_index, cpy_len);
  1007. newstr_index += cpy_len;
  1008. str_index += cpy_len;
  1009. strcpy(newstring + newstr_index, newbie);
  1010. newstr_index += new_len;
  1011. str_index += old_len;
  1012. if((c = (char *) strstr(string + str_index, oldie)) != NULL)
  1013. oldie_index = c - string;
  1014. }
  1015. strcpy(newstring + newstr_index, string + str_index);
  1016. return (newstring);
  1017. }
  1018. char* replace_vars(char *buf) {
  1019. return replace(buf, "$n", botname);
  1020. }
  1021. void showhelp(int idx, struct flag_record *flags, const char *string)
  1022. {
  1023. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  1024. size_t help_siz = strlen(string) + 1000 + 1;
  1025. char *helpstr = (char *) my_calloc(1, help_siz);
  1026. char tmp[2] = "", flagstr[10] = "";
  1027. bool ok = 1;
  1028. while (string && string[0]) {
  1029. if (*string == '%') {
  1030. if (!strncmp(string + 1, "{+", 2)) {
  1031. while (*string && *string != '+') {
  1032. string++;
  1033. }
  1034. flagstr[0] = 0;
  1035. while (*string && *string != '}') {
  1036. simple_snprintf(tmp, sizeof(tmp), "%c", *string);
  1037. strlcat(flagstr, tmp, sizeof(flagstr));
  1038. string++;
  1039. }
  1040. string++;
  1041. break_down_flags(flagstr, &fr, NULL);
  1042. if (flagrec_ok(&fr, flags)) {
  1043. ok = 1;
  1044. while (*string && *string != '%') {
  1045. simple_snprintf(tmp, sizeof(tmp), "%c", *string);
  1046. strlcat(helpstr, tmp, help_siz);
  1047. string++;
  1048. }
  1049. if (!strncmp(string + 1, "{-", 2)) {
  1050. ok = 1;
  1051. while (*string && *string != '}') {
  1052. string++;
  1053. }
  1054. string++;
  1055. }
  1056. } else {
  1057. ok = 0;
  1058. }
  1059. } else if (!strncmp(string + 1, "{-", 2)) {
  1060. ok = 1;
  1061. while (*string && *string != '}') {
  1062. string++;
  1063. }
  1064. string++;
  1065. } else if (*string == '{') {
  1066. while (*string && *string != '}') {
  1067. string++;
  1068. }
  1069. } else if (*(string + 1) == 'd') {
  1070. string += 2;
  1071. if (dcc[idx].u.chat->channel >= 0)
  1072. strlcat(helpstr, settings.dcc_prefix, help_siz);
  1073. } else if (*(string + 1) == '%') {
  1074. string += 2;
  1075. strlcat(helpstr, "%", help_siz);
  1076. } else {
  1077. if (ok) {
  1078. simple_snprintf(tmp, sizeof(tmp), "%c", *string);
  1079. strlcat(helpstr, tmp, help_siz);
  1080. }
  1081. string++;
  1082. }
  1083. } else {
  1084. if (ok) {
  1085. simple_snprintf(tmp, sizeof(tmp), "%c", *string);
  1086. strlcat(helpstr, tmp, help_siz);
  1087. }
  1088. string++;
  1089. }
  1090. }
  1091. helpstr[strlen(helpstr)] = 0;
  1092. if (helpstr[0]) dumplots(idx, "", helpstr);
  1093. free(helpstr);
  1094. }
  1095. /* Arrange the N elements of ARRAY in random order. */
  1096. void shuffleArray(char* array[], size_t n)
  1097. {
  1098. for (size_t i = 0; i < n; i++) {
  1099. const size_t j = randint(n);
  1100. char* temp = array[j];
  1101. array[j] = array[i];
  1102. array[i] = temp;
  1103. }
  1104. }
  1105. void shuffle(char *string, char *delim, size_t str_len)
  1106. {
  1107. char *array[501], *str = NULL, *work = NULL;
  1108. size_t len = 0;
  1109. bzero(&array, sizeof array);
  1110. work = strdup(string);
  1111. str = strtok(work, delim);
  1112. while(str && *str)
  1113. {
  1114. array[len] = str;
  1115. len++;
  1116. str = strtok((char*) NULL, delim);
  1117. }
  1118. shuffleArray(array, len);
  1119. string[0] = 0;
  1120. for (size_t i = 0; i < len; i++) {
  1121. strlcat(string, array[i], str_len);
  1122. if (i != len - 1)
  1123. strlcat(string, delim, str_len);
  1124. }
  1125. free(work);
  1126. string[strlen(string)] = 0;
  1127. }
  1128. /* returns
  1129. 1: use ANSI
  1130. 2: use mIRC
  1131. 0: neither
  1132. */
  1133. int
  1134. coloridx(int idx)
  1135. {
  1136. if (idx == -1) { /* who cares, just show color! */
  1137. return 1; /* ANSI */
  1138. } else if (idx == -2) {
  1139. return 2; /* mIRC */
  1140. /* valid idx and NOT relaying */
  1141. } else if (idx >= 0) {
  1142. if (dcc[idx].irc || dcc[idx].bot) {
  1143. return 0;
  1144. } else if ((dcc[idx].status & STAT_COLOR) && (dcc[idx].type && dcc[idx].type != &DCC_RELAYING)) {
  1145. /* telnet probably wants ANSI, even though it might be a relay from an mIRC client; fuck`em */
  1146. if (dcc[idx].status & STAT_TELNET)
  1147. return 1;
  1148. /* non-telnet is probably a /dcc-chat, most irc clients support mIRC codes... */
  1149. else
  1150. return 2;
  1151. }
  1152. }
  1153. return 0;
  1154. }
  1155. const char *
  1156. color(int idx, int type, int which)
  1157. {
  1158. int ansi = 0;
  1159. /* if user is connected over TELNET or !backgrd, show ANSI
  1160. * if they are relaying, they are most likely on an IRC client and should have mIRC codes
  1161. */
  1162. if ((ansi = coloridx(idx)) == 0)
  1163. return "";
  1164. if (ansi == 2)
  1165. ansi = 0;
  1166. if (type == BOLD_OPEN) {
  1167. return ansi ? "\033[1m" : "\002";
  1168. } else if (type == BOLD_CLOSE) {
  1169. // return ansi ? "\033[22m" : "\002";
  1170. return ansi ? "\033[0m" : "\002";
  1171. } else if (type == UNDERLINE_OPEN) {
  1172. return ansi ? "\033[4m" : "\037";
  1173. } else if (type == UNDERLINE_CLOSE) {
  1174. return ansi ? "\033[24m" : "\037";
  1175. } else if (type == FLASH_OPEN) {
  1176. return ansi ? "\033[5m" : "\002\037";
  1177. } else if (type == FLASH_CLOSE) {
  1178. return ansi ? "\033[0m" : "\037\002";
  1179. } else if (type == COLOR_OPEN) {
  1180. switch (which) {
  1181. case C_BLACK: return ansi ? "\033[30m" : "\00301";
  1182. case C_RED: return ansi ? "\033[31m" : "\00305";
  1183. case C_GREEN: return ansi ? "\033[32m" : "\00303";
  1184. case C_BROWN: return ansi ? "\033[33m" : "\00307";
  1185. case C_BLUE: return ansi ? "\033[34m" : "\00302";
  1186. case C_PURPLE: return ansi ? "\033[35m" : "\00306";
  1187. case C_CYAN: return ansi ? "\033[36m" : "\00310";
  1188. case C_WHITE: return ansi ? "\033[1;37m" : "\00300";
  1189. case C_DARKGREY: return ansi ? "\033[1;30m" : "\00314";
  1190. case C_LIGHTRED: return ansi ? "\033[1;31m" : "\00304";
  1191. case C_LIGHTGREEN: return ansi ? "\033[1;32m" : "\00309";
  1192. case C_LIGHTBLUE: return ansi ? "\033[1;34m" : "\00312";
  1193. case C_LIGHTPURPLE: return ansi ? "\033[1;35m" : "\00313";
  1194. case C_LIGHTCYAN: return ansi ? "\033[1;36m" : "\00311";
  1195. case C_LIGHTGREY: return ansi ? "\033[37m" : "\00315";
  1196. case C_YELLOW: return ansi ? "\033[1;33m" : "\00308";
  1197. default: break;
  1198. }
  1199. } else if (type == COLOR_CLOSE) {
  1200. return ansi ? "\033[0m" : "\00300";
  1201. }
  1202. /* This should never be reached.. */
  1203. return "";
  1204. }
  1205. char *
  1206. strtolower(char *s)
  1207. {
  1208. char *p = s;
  1209. while (*p) {
  1210. *p = tolower(*p);
  1211. p++;
  1212. }
  1213. return s;
  1214. }
  1215. char *
  1216. strtoupper(char *s)
  1217. {
  1218. char *p = s;
  1219. while (*p) {
  1220. *p = toupper(*p);
  1221. p++;
  1222. }
  1223. return s;
  1224. }
  1225. char *step_thru_file(FILE *fd)
  1226. {
  1227. if (fd == NULL) {
  1228. return NULL;
  1229. }
  1230. char tempBuf[1024] = "", *retStr = NULL;
  1231. size_t ret_siz = 0;
  1232. while (!feof(fd)) {
  1233. if (fgets(tempBuf, sizeof(tempBuf), fd) && !feof(fd)) {
  1234. if (retStr == NULL) {
  1235. ret_siz = strlen(tempBuf) + 2;
  1236. retStr = (char *) my_calloc(1, ret_siz);
  1237. strlcpy(retStr, tempBuf, ret_siz);
  1238. } else {
  1239. ret_siz = strlen(retStr) + strlen(tempBuf);
  1240. retStr = (char *) my_realloc(retStr, ret_siz);
  1241. strlcat(retStr, tempBuf, ret_siz);
  1242. }
  1243. if (retStr[strlen(retStr)-1] == '\n') {
  1244. retStr[strlen(retStr)-1] = 0;
  1245. break;
  1246. }
  1247. }
  1248. }
  1249. return retStr;
  1250. }
  1251. char *trim(char *string)
  1252. {
  1253. if (string) {
  1254. char *ibuf = NULL, *obuf = NULL;
  1255. for (ibuf = obuf = string; *ibuf; ) {
  1256. while (*ibuf && (isspace (*ibuf)))
  1257. ibuf++;
  1258. if (*ibuf && (obuf != string))
  1259. *(obuf++) = ' ';
  1260. while (*ibuf && (!isspace (*ibuf)))
  1261. *(obuf++) = *(ibuf++);
  1262. }
  1263. *obuf = '\0';
  1264. }
  1265. return (string);
  1266. }
  1267. int skipline (char *line, int *skip) {
  1268. static int multi = 0;
  1269. if (strstr(line, "*/"))
  1270. multi = 0;
  1271. if ( (!strncmp(line, "#", 1)) || (!strncmp(line, ";", 1)) || (!strncmp(line, "//", 2)) ) {
  1272. (*skip)++;
  1273. } else if ( (strstr(line, "/*")) && (strstr(line, "*/")) ) {
  1274. multi = 0;
  1275. (*skip)++;
  1276. } else if ( (strstr(line, "/*")) ) {
  1277. (*skip)++;
  1278. multi = 1;
  1279. } else if ( (strstr(line, "*/")) ) {
  1280. multi = 0;
  1281. } else {
  1282. if (!multi) (*skip) = 0;
  1283. }
  1284. return (*skip);
  1285. }
  1286. /* vim: set sts=2 sw=2 ts=8 et: */