misc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. static char *inverted_csplit(char **rest, char divider)
  19. {
  20. char *p;
  21. if (!rest)
  22. return *rest = "";
  23. p = *rest + strlen(*rest) - 1;
  24. while ((p[0] != divider) && (p != *rest))
  25. p--;
  26. p[0] = 0;
  27. return p + 1;
  28. }
  29. /* stolen from tcl_duration in tclmisc.c */
  30. static char duration_temp[256];
  31. static char *stats_duration(int seconds, int details)
  32. {
  33. char s[256];
  34. time_t sec;
  35. int details_shown = 0;
  36. sec = seconds;
  37. s[0] = 0;
  38. if (sec < 1) {
  39. snprintf(duration_temp, sizeof(duration_temp), "%s", SLSOMETIME);
  40. return duration_temp;
  41. }
  42. if (sec >= 31536000) {
  43. sprintf(s, "%d %s ", (int) (sec / 31536000),
  44. ((int) (sec / 31536000) > 1) ? SLYEARS : SLYEAR);
  45. sec -= (((int) (sec / 31536000)) * 31536000);
  46. details_shown++;
  47. }
  48. if ((sec >= 604800) && (details_shown < details)) {
  49. sprintf(&s[strlen(s)], "%d %s ", (int) (sec / 604800),
  50. ((int) (sec / 604800) > 1) ? SLWEEKS : SLWEEK);
  51. sec -= (((int) (sec / 604800)) * 604800);
  52. details_shown++;
  53. }
  54. if ((sec >= 86400) && (details_shown < details)) {
  55. sprintf(&s[strlen(s)], "%d %s ", (int) (sec / 86400),
  56. ((int) (sec / 86400) > 1) ? SLDAYS : SLDAY);
  57. sec -= (((int) (sec / 86400)) * 86400);
  58. details_shown++;
  59. }
  60. if ((sec >= 3600) && (details_shown < details)) {
  61. sprintf(&s[strlen(s)], "%d %s ", (int) (sec / 3600),
  62. ((int) (sec / 3600) > 1) ? SLHOURS : SLHOUR);
  63. sec -= (((int) (sec / 3600)) * 3600);
  64. details_shown++;
  65. }
  66. if ((sec >= 60) && (details_shown < details)) {
  67. sprintf(&s[strlen(s)], "%d %s ", (int) (sec / 60),
  68. ((int) (sec / 60) > 1) ? SLMINUTES : SLMINUTE);
  69. sec -= (((int) (sec / 60)) * 60);
  70. details_shown++;
  71. }
  72. if ((sec > 0) && (details_shown < details)) {
  73. sprintf(&s[strlen(s)], "%d %s", (int) (sec / 1),
  74. ((int) (sec / 1) > 1) ? SLSECONDS : SLSECOND);
  75. }
  76. if (s[strlen(s) - 1] == ' ')
  77. s[strlen(s) - 1]='\0';
  78. snprintf(duration_temp, sizeof(duration_temp), "%s", s);
  79. return duration_temp;
  80. }
  81. static int countsmileys(char *text)
  82. {
  83. char buf[512], *pbuf, *smiley, *p;
  84. int ismileys = 0;
  85. sprintf(buf, "%s", smileys);
  86. pbuf = buf;
  87. while (strlen(pbuf) > 0) {
  88. smiley = newsplit(&pbuf);
  89. p = strstr(text, smiley);
  90. while (p) {
  91. ismileys++;
  92. p += strlen(smiley);
  93. p = strstr(p, smiley);
  94. }
  95. }
  96. return ismileys;
  97. }
  98. static int countwords(char *buf)
  99. {
  100. int i, words = 1;
  101. for (i = 0; i < strlen(buf); i++) {
  102. if ((buf[i] == ' ') && (buf[i+1] != ' '))
  103. words++;
  104. }
  105. return words;
  106. }
  107. static int countquestions(char *buf)
  108. {
  109. int i, questions = 0;
  110. for (i = 0; i < strlen(buf); i++) {
  111. if ((buf[i] == '?') && (buf[i+1] != '?'))
  112. questions++;
  113. }
  114. return questions;
  115. }
  116. // void lower(char*p){for(;*p=tolower(*p);p++);}
  117. static void strlower(char *text)
  118. {
  119. int i;
  120. for (i = 0; i < strlen(text); i++)
  121. text[i] = tolower(text[i]);
  122. }
  123. static int gethour()
  124. {
  125. char ts[10];
  126. time_t tt;
  127. tt = now;
  128. strftime(ts, 9, "%H", localtime(&tt));
  129. ts[9] = 0;
  130. return atoi(ts);
  131. }
  132. static int getmonth()
  133. {
  134. char ts[10];
  135. time_t tt;
  136. tt = now;
  137. strftime(ts, 9, "%m", localtime(&tt));
  138. ts[9] = 0;
  139. return atoi(ts);
  140. }
  141. static int ismonday()
  142. {
  143. char ts[10];
  144. time_t tt;
  145. tt = now;
  146. strftime(ts, 9, "%a", localtime(&tt));
  147. ts[9] = 0;
  148. return (!strcasecmp(ts, "mon"));
  149. }
  150. /* maskstricthost():
  151. * basically the same as maskhost() from src/misc.c, but _never_ stripts
  152. * "~+-^=" off the host
  153. * maskhost() version: * $Id: misc.c,v 1.30 2000/10/27 19:27:32 fabian Exp $
  154. */
  155. static void maskstricthost(const char *s, char *nw)
  156. {
  157. register const char *p, *q, *e, *f;
  158. int i;
  159. *nw++ = '*';
  160. *nw++ = '!';
  161. p = (q = strchr(s, '!')) ? q + 1 : s;
  162. /* Strip of any nick, if a username is found, use last 8 chars */
  163. if ((q = strchr(p, '@'))) {
  164. int fl = 0;
  165. if ((q - p) > 9) {
  166. nw[0] = '*';
  167. p = q - 7;
  168. i = 1;
  169. } else
  170. i = 0;
  171. while (*p != '@') {
  172. if (!fl && strchr("~+-^=", *p)) {
  173. // if (strict_host)
  174. nw[i] = '?';
  175. // else
  176. // i--;
  177. } else
  178. nw[i] = *p;
  179. fl++;
  180. p++;
  181. i++;
  182. }
  183. nw[i++] = '@';
  184. q++;
  185. } else {
  186. nw[0] = '*';
  187. nw[1] = '@';
  188. i = 2;
  189. q = s;
  190. }
  191. nw += i;
  192. e = NULL;
  193. /* Now q points to the hostname, i point to where to put the mask */
  194. if ((!(p = strchr(q, '.')) || !(e = strchr(p + 1, '.'))) && !strchr(q, ':'))
  195. /* TLD or 2 part host */
  196. strcpy(nw, q);
  197. else {
  198. if (e == NULL) { /* IPv6 address? */
  199. const char *mask_str;
  200. f = strrchr(q, ':');
  201. if (strchr(f, '.')) { /* IPv4 wrapped in an IPv6? */
  202. f = strrchr(f, '.');
  203. mask_str = ".*";
  204. } else /* ... no, true IPv6. */
  205. mask_str = ":*";
  206. strncpy(nw, q, f - q);
  207. /* No need to nw[f-q] = 0 here, as the strcpy below will
  208. * terminate the string for us.
  209. */
  210. nw += (f - q);
  211. strcpy(nw, mask_str);
  212. } else {
  213. for (f = e; *f; f++);
  214. f--;
  215. if (*f >= '0' && *f <= '9') { /* Numeric IP address */
  216. while (*f != '.')
  217. f--;
  218. strncpy(nw, q, f - q);
  219. /* No need to nw[f-q] = 0 here, as the strcpy below will
  220. * terminate the string for us.
  221. */
  222. nw += (f - q);
  223. strcpy(nw, ".*");
  224. } else { /* Normal host >= 3 parts */
  225. /* a.b.c -> *.b.c
  226. * a.b.c.d -> *.b.c.d if tld is a country (2 chars)
  227. * OR *.c.d if tld is com/edu/etc (3 chars)
  228. * a.b.c.d.e -> *.c.d.e etc
  229. */
  230. const char *x = strchr(e + 1, '.');
  231. if (!x)
  232. x = p;
  233. else if (strchr(x + 1, '.'))
  234. x = e;
  235. else if (strlen(x) == 3)
  236. x = p;
  237. else
  238. x = e;
  239. sprintf(nw, "*%s", x);
  240. }
  241. }
  242. }
  243. }
  244. /* get_timerange():
  245. *
  246. */
  247. static int get_timerange(char *text)
  248. {
  249. if (!strcasecmp(text, "today"))
  250. return S_TODAY;
  251. else if (!strcasecmp(text, "daily"))
  252. return S_TODAY;
  253. else if (!strcasecmp(text, "weekly"))
  254. return S_WEEKLY;
  255. else if (!strcasecmp(text, "monthly"))
  256. return S_MONTHLY;
  257. else if (!strcasecmp(text, "total"))
  258. return S_TOTAL;
  259. else
  260. return T_ERROR;
  261. // FIXME: Check for slanged timeranges!
  262. }
  263. #define SENDMAIL_ERROR 1
  264. static int email_send(char *to, char *subject, char *body)
  265. {
  266. FILE *f;
  267. f = popen("/usr/sbin/sendmail -t", "w");
  268. if (!f)
  269. return SENDMAIL_ERROR;
  270. fprintf(f, "To: %s\n", to);
  271. fprintf(f, "From: %s\n", botnetnick);
  272. fprintf(f, "Subject: %s\n", subject);
  273. fprintf(f, "\n");
  274. fprintf(f, "%s", body);
  275. fprintf(f, "\n.\n");
  276. debug0("rückmeldung von pclose testen");
  277. if (pclose(f) == -1)
  278. return SENDMAIL_ERROR;
  279. else
  280. return 0;
  281. }