misc.c 32 KB

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