4
0

misc.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Copyright (C) 2000,2001 Florian Sander
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. /* stolen from tcl_duration in tclmisc.c */
  19. char duration_temp[256];
  20. static char *stats_duration(int seconds)
  21. {
  22. char s[256];
  23. time_t sec;
  24. sec = seconds;
  25. s[0] = 0;
  26. if (sec < 1) {
  27. sprintf(duration_temp, "0 seconds");
  28. return duration_temp;
  29. }
  30. if (sec >= 31536000) {
  31. sprintf(s, "%d %s ", (int) (sec / 31536000),
  32. ((int) (sec / 31536000) > 1) ? SLYEARS : SLYEAR);
  33. sec -= (((int) (sec / 31536000)) * 31536000);
  34. }
  35. if (sec >= 604800) {
  36. sprintf(&s[strlen(s)], "%d %s ", (int) (sec / 604800),
  37. ((int) (sec / 604800) > 1) ? SLWEEKS : SLWEEK);
  38. sec -= (((int) (sec / 604800)) * 604800);
  39. }
  40. if (sec >= 86400) {
  41. sprintf(&s[strlen(s)], "%d %s ", (int) (sec / 86400),
  42. ((int) (sec / 86400) > 1) ? SLDAYS : SLDAY);
  43. sec -= (((int) (sec / 86400)) * 86400);
  44. }
  45. if (sec >= 3600) {
  46. sprintf(&s[strlen(s)], "%d %s ", (int) (sec / 3600),
  47. ((int) (sec / 3600) > 1) ? SLHOURS : SLHOUR);
  48. sec -= (((int) (sec / 3600)) * 3600);
  49. }
  50. if (sec >= 60) {
  51. sprintf(&s[strlen(s)], "%d %s ", (int) (sec / 60),
  52. ((int) (sec / 60) > 1) ? SLMINUTES : SLMINUTE);
  53. sec -= (((int) (sec / 60)) * 60);
  54. }
  55. /* is there any place where seconds might be displayed??? I think not. */
  56. /*
  57. if (sec > 0) {
  58. sprintf(&s[strlen(s)], "%d %s", (int) (sec / 1),
  59. ((int) (sec / 1) > 1) ? SLSECONDS : SLSECOND);
  60. }
  61. */
  62. sprintf(duration_temp, "%s", s);
  63. return duration_temp;
  64. }
  65. static int countsmileys(char *text)
  66. {
  67. char buf[512], *pbuf, *smiley, *p;
  68. int ismileys = 0;
  69. sprintf(buf, "%s", smileys);
  70. pbuf = buf;
  71. while (strlen(pbuf) > 0) {
  72. smiley = newsplit(&pbuf);
  73. p = strstr(text, smiley);
  74. while (p) {
  75. ismileys++;
  76. p += strlen(smiley);
  77. p = strstr(p, smiley);
  78. }
  79. }
  80. return ismileys;
  81. }
  82. static int countstatmembers(globstats *gs)
  83. {
  84. int members = 0;
  85. struct userrec *u;
  86. locstats *ls;
  87. Context;
  88. if (!gs)
  89. return 0;
  90. for (ls = gs->local; ls; ls = ls->next) {
  91. u = get_user_by_handle(userlist, ls->user);
  92. if (matchattr(u, nostatsflags, gs->chan))
  93. continue;
  94. members++;
  95. }
  96. return members;
  97. }
  98. static int gettotal(globstats *gs, int type, int today)
  99. {
  100. int total = 0;
  101. struct userrec *u;
  102. struct stats_userlist *su;
  103. locstats *ls;
  104. for (ls = gs->local;ls; ls = ls->next) {
  105. if (use_userfile) {
  106. u = get_user_by_handle(userlist, ls->user);
  107. if (matchattr(u, nostatsflags, gs->chan))
  108. continue;
  109. } else {
  110. su = findsuser_by_name(ls->user);
  111. if (su && !su->list)
  112. continue;
  113. }
  114. total += ls->values[today][type];
  115. }
  116. return total;
  117. }
  118. /* stolen from tcl_matchattr */
  119. static int matchattr (struct userrec *u, char *flags, char *chan) {
  120. struct flag_record plus, minus, user;
  121. int ok = 0, f;
  122. #ifndef OLDBOT
  123. if (u && (!chan || findchan_by_dname(chan))) {
  124. #else
  125. if (u && (!chan || findchan(chan))) {
  126. #endif
  127. user.match = FR_GLOBAL | (chan ? FR_CHAN : 0) | FR_BOT;
  128. get_user_flagrec(u, &user, chan);
  129. plus.match = user.match;
  130. break_down_flags(flags, &plus, &minus);
  131. f = (minus.global || minus.udef_global || minus.chan ||
  132. minus.udef_chan || minus.bot);
  133. if (flagrec_eq(&plus, &user)) {
  134. if (!f)
  135. ok = 1;
  136. else {
  137. minus.match = plus.match ^ (FR_AND | FR_OR);
  138. if (!flagrec_eq(&minus, &user))
  139. ok = 1;
  140. }
  141. }
  142. }
  143. return ok;
  144. }
  145. static int countwords(char *buf)
  146. {
  147. int i, words = 1;
  148. for (i = 0; i < strlen(buf); i++) {
  149. if ((buf[i] == ' ') && (buf[i+1] != ' '))
  150. words++;
  151. }
  152. return words;
  153. }
  154. static int countquestions(char *buf)
  155. {
  156. int i, questions = 0;
  157. for (i = 0; i < strlen(buf); i++) {
  158. if ((buf[i] == '?') && (buf[i+1] != '?'))
  159. questions++;
  160. }
  161. return questions;
  162. }
  163. static char *splitpath(char **rest)
  164. {
  165. register char *o, *r;
  166. if (!rest)
  167. return *rest = "";
  168. o = *rest;
  169. while (*o == ' ')
  170. o++;
  171. r = o;
  172. while (*o && (*o != '/'))
  173. o++;
  174. if (*o)
  175. *o++ = 0;
  176. *rest = o;
  177. return r;
  178. }
  179. static void strlower(char *text)
  180. {
  181. int i;
  182. for (i = 0; i < strlen(text); i++)
  183. text[i] = tolower(text[i]);
  184. }
  185. static void filt(char *text)
  186. {
  187. int i;
  188. for (i = 0; i < strlen(text); i++)
  189. if (text[i] == '%')
  190. text[i] = ' ';
  191. }
  192. char filt2_buf[512];
  193. static char *filt2(char *text)
  194. {
  195. char *buf;
  196. int i = 0;
  197. buf = filt2_buf;
  198. while ((i < 500) && text[0]) {
  199. if (text[0] == '<') {
  200. buf[0] = '&';
  201. buf[1] = 'l';
  202. buf[2] = 't';
  203. buf[3] = ';';
  204. buf += 4;
  205. i += 4;
  206. } else if (text[0] == '>') {
  207. buf[0] = '&';
  208. buf[1] = 'g';
  209. buf[2] = 't';
  210. buf[3] = ';';
  211. buf += 4;
  212. i += 4;
  213. } else {
  214. buf[0] = text[0];
  215. buf++;
  216. i++;
  217. }
  218. text++;
  219. }
  220. buf[0] = 0;
  221. return filt2_buf;
  222. }
  223. static int gethour()
  224. {
  225. char ts[10];
  226. time_t tt;
  227. tt = now;
  228. strftime(ts, 9, "%H", localtime(&tt));
  229. ts[9] = 0;
  230. return atoi(ts);
  231. }
  232. static int getmonth()
  233. {
  234. char ts[10];
  235. time_t tt;
  236. tt = now;
  237. strftime(ts, 9, "%m", localtime(&tt));
  238. ts[9] = 0;
  239. return atoi(ts);
  240. }
  241. static int ismonday()
  242. {
  243. char ts[10];
  244. time_t tt;
  245. tt = now;
  246. strftime(ts, 9, "%a", localtime(&tt));
  247. ts[9] = 0;
  248. return (!strcasecmp(ts, "mon"));
  249. }
  250. static int secretchan(char *chan)
  251. {
  252. struct chanset_t *ch;
  253. if (list_secret_chans)
  254. return 0;
  255. ch = findchan_by_dname(chan);
  256. if (!ch)
  257. return 0;
  258. if (ch->status & CHAN_SECRET)
  259. return 1;
  260. return 0;
  261. }
  262. static void long_dprintf(int idx, char *text)
  263. {
  264. char buf[501];
  265. if (strlen(text) < 500)
  266. dprintf(idx, "%s", text);
  267. else {
  268. while (text[0]) {
  269. strncpy(buf, text, 500);
  270. buf[500] = 0;
  271. dprintf(idx, "%s", buf);
  272. text += strlen(buf);
  273. }
  274. }
  275. }
  276. /* maskstricthost():
  277. * basically the same as maskhost() from src/misc.c, but _never_ stripts
  278. * "~+-^=" off the host
  279. * maskhost() version: * $Id: misc.c,v 1.30 2000/10/27 19:27:32 fabian Exp $
  280. */
  281. static void maskstricthost(const char *s, char *nw)
  282. {
  283. register const char *p, *q, *e, *f;
  284. int i;
  285. *nw++ = '*';
  286. *nw++ = '!';
  287. p = (q = strchr(s, '!')) ? q + 1 : s;
  288. /* Strip of any nick, if a username is found, use last 8 chars */
  289. if ((q = strchr(p, '@'))) {
  290. int fl = 0;
  291. if ((q - p) > 9) {
  292. nw[0] = '*';
  293. p = q - 7;
  294. i = 1;
  295. } else
  296. i = 0;
  297. while (*p != '@') {
  298. if (!fl && strchr("~+-^=", *p)) {
  299. // if (strict_host)
  300. nw[i] = '?';
  301. // else
  302. // i--;
  303. } else
  304. nw[i] = *p;
  305. fl++;
  306. p++;
  307. i++;
  308. }
  309. nw[i++] = '@';
  310. q++;
  311. } else {
  312. nw[0] = '*';
  313. nw[1] = '@';
  314. i = 2;
  315. q = s;
  316. }
  317. nw += i;
  318. e = NULL;
  319. /* Now q points to the hostname, i point to where to put the mask */
  320. if ((!(p = strchr(q, '.')) || !(e = strchr(p + 1, '.'))) && !strchr(q, ':'))
  321. /* TLD or 2 part host */
  322. strcpy(nw, q);
  323. else {
  324. if (e == NULL) { /* IPv6 address? */
  325. const char *mask_str;
  326. f = strrchr(q, ':');
  327. if (strchr(f, '.')) { /* IPv4 wrapped in an IPv6? */
  328. f = strrchr(f, '.');
  329. mask_str = ".*";
  330. } else /* ... no, true IPv6. */
  331. mask_str = ":*";
  332. strncpy(nw, q, f - q);
  333. /* No need to nw[f-q] = 0 here, as the strcpy below will
  334. * terminate the string for us.
  335. */
  336. nw += (f - q);
  337. strcpy(nw, mask_str);
  338. } else {
  339. for (f = e; *f; f++);
  340. f--;
  341. if (*f >= '0' && *f <= '9') { /* Numeric IP address */
  342. while (*f != '.')
  343. f--;
  344. strncpy(nw, q, f - q);
  345. /* No need to nw[f-q] = 0 here, as the strcpy below will
  346. * terminate the string for us.
  347. */
  348. nw += (f - q);
  349. strcpy(nw, ".*");
  350. } else { /* Normal host >= 3 parts */
  351. /* a.b.c -> *.b.c
  352. * a.b.c.d -> *.b.c.d if tld is a country (2 chars)
  353. * OR *.c.d if tld is com/edu/etc (3 chars)
  354. * a.b.c.d.e -> *.c.d.e etc
  355. */
  356. const char *x = strchr(e + 1, '.');
  357. if (!x)
  358. x = p;
  359. else if (strchr(x + 1, '.'))
  360. x = e;
  361. else if (strlen(x) == 3)
  362. x = p;
  363. else
  364. x = e;
  365. sprintf(nw, "*%s", x);
  366. }
  367. }
  368. }
  369. }