PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/mboxgrep-0.7.9/src/scan.c

#
C | 349 lines | 285 code | 38 blank | 26 comment | 145 complexity | 9510d085bf1fceeb24d0a1905e5043b2 MD5 | raw file
Possible License(s): GPL-2.0
  1. /* -*- C -*-
  2. mboxgrep - scan mailbox for messages matching a regular expression
  3. Copyright (C) 2000, 2001, 2002, 2003 Daniel Spiljar
  4. Mboxgrep is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. Mboxgrep is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with mboxgrep; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. $Id: scan.c,v 1.21 2003/04/06 21:01:49 dspiljar Exp $ */
  16. #include <config.h>
  17. #include <unistd.h>
  18. #include <stdio.h>
  19. #include <regex.h>
  20. #include <stdlib.h>
  21. #include <sys/types.h>
  22. #ifdef HAVE_DIRENT_H
  23. # include <dirent.h>
  24. # define NAMLEN(dirent) strlen((dirent)->d_name)
  25. #else
  26. # define dirent direct
  27. # define NAMLEN(dirent) (dirent)->d_namlen
  28. # ifdef HAVE_SYS_NDIR_H
  29. # include <sys/ndir.h>
  30. # endif /* HAVE_SYS_NDIR_H */
  31. # ifdef HAVE_SYS_DIR_H
  32. # include <sys/dir.h>
  33. # endif /* HAVE_SYS_DIR_H */
  34. # ifdef HAVE_NDIR_H
  35. # include <ndir.h>
  36. # endif /* HAVE_NDIR_H */
  37. #endif /* HAVE_DIRENT_H */
  38. #include <time.h>
  39. #include <errno.h>
  40. #include <string.h>
  41. #ifdef HAVE_LIBZ
  42. # include <zlib.h>
  43. #define BUFLEN 16384
  44. #endif /* HAVE_LIBZ */
  45. #ifdef HAVE_LIBPCRE
  46. # include <pcre.h>
  47. #endif /* HAVE_LIBPCRE */
  48. #include "scan.h"
  49. #include "mbox.h"
  50. #include "mh.h"
  51. #include "maildir.h"
  52. #include "wrap.h"
  53. #include "md5.h"
  54. #ifdef HAVE_FTS_OPEN
  55. # include <sys/stat.h>
  56. # include <fts.h>
  57. #else
  58. # ifdef HAVE_FTW
  59. # include <ftw.h>
  60. # endif /* HAVE_FTW */
  61. #endif /* HAVE_FTS_OPEN */
  62. #ifdef HAVE_LIBDMALLOC
  63. #include <dmalloc.h>
  64. #endif /* HAVE_LIBDMALLOC */
  65. void scan_mailbox (char path[])
  66. /* {{{ */
  67. {
  68. static FILE *outf;
  69. extern FILE *tmpp;
  70. static mbox_t *mbox, *out;
  71. #ifdef HAVE_LIBPCRE
  72. extern pcre *pcre_pattern;
  73. extern pcre_extra *hints;
  74. int of[BUFSIZ];
  75. #endif /* HAVE_LIBPCRE */
  76. static DIR *boxd, *foo;
  77. static maildir_t *maildird;
  78. static message_t *msg;
  79. extern regex_t posix_pattern;
  80. extern char *pipecmd, *outboxname;
  81. extern int count;
  82. int delete = 0;
  83. char date_str[80];
  84. int isdup = 0;
  85. time_t tt;
  86. struct tm *ct;
  87. extern checksum_t *cs;
  88. extern option_t config;
  89. if (config.format == MAILDIR && config.action == WRITE)
  90. {
  91. foo = opendir (outboxname); /* do NOT change this to m_opendir! */
  92. if (foo == NULL && errno == ENOENT)
  93. maildir_create (outboxname);
  94. else closedir (foo);
  95. if (-1 == maildir_check (outboxname))
  96. {
  97. if (config.merr)
  98. fprintf (stderr, "%s: %s: Not a maildir folder\n", APPNAME,
  99. outboxname);
  100. exit (2);
  101. }
  102. }
  103. count = 0;
  104. if (config.action == DELETE)
  105. delete = 1;
  106. if ((config.format == MBOX) || (config.format == ZMBOX) ||
  107. (config.format == BZ2MBOX))
  108. {
  109. mbox = (mbox_t *) mbox_open (path, "r");
  110. if (mbox == NULL) return;
  111. }
  112. else if ((config.format == MH) || (config.format == NNMH) ||
  113. (config.format == NNML))
  114. {
  115. boxd = mh_open (path);
  116. if (boxd == NULL) return;
  117. }
  118. else if (config.format == MAILDIR)
  119. {
  120. maildird = maildir_open (path);
  121. if (maildird == NULL) return;
  122. }
  123. for (;;)
  124. {
  125. int res1 = 1, res2 = 1;
  126. if ((config.format == MBOX) || (config.format == ZMBOX) ||
  127. (config.format == BZ2MBOX))
  128. msg = (message_t *) mbox_read_message (mbox);
  129. else if ((config.format == MH) || (config.format == NNMH) ||
  130. (config.format == NNML))
  131. msg = (message_t *) mh_read_message (boxd);
  132. else if (config.format == MAILDIR)
  133. msg = (message_t *) maildir_read_message (maildird);
  134. if (msg == NULL) break;
  135. if (msg->from == NULL) msg->from = (char *) xstrdup ("nobody");
  136. #ifdef HAVE_LIBPCRE
  137. if (config.perl)
  138. {
  139. if (config.headers)
  140. res1 = pcre_exec (pcre_pattern, hints, msg->headers,
  141. (int) strlen (msg->headers), 0, 0, of, BUFSIZ);
  142. if (config.body)
  143. res2 = pcre_exec (pcre_pattern, hints, msg->body,
  144. (int) strlen (msg->body), 0, 0, of, BUFSIZ);
  145. res1 = res1 ^ 1;
  146. res2 = res2 ^ 1;
  147. }
  148. else
  149. #endif /* HAVE_LIBPCRE */
  150. {
  151. if (config.headers)
  152. res1 = regexec (&posix_pattern, msg->headers, 0, NULL, 0);
  153. if (config.body)
  154. res2 = regexec (&posix_pattern, msg->body, 0, NULL, 0);
  155. }
  156. if (config.dedup)
  157. isdup = md5_check_message (msg->body, cs);
  158. if (((res1 == 0) | (res2 == 0)) ^ ((config.invert ^ delete)) &&
  159. ((config.dedup && !isdup) || !config.dedup))
  160. {
  161. if (config.action == DISPLAY)
  162. {
  163. if (config.format != MBOX && config.format != ZMBOX
  164. && config.format != BZ2MBOX
  165. && 0 != strncmp ("From ", msg->headers, 5))
  166. {
  167. tt = time (NULL);
  168. ct = localtime (&tt);
  169. strftime (date_str, 80, "%a %b %d %H:%M:%S %Y", ct);
  170. if (msg->from)
  171. fprintf (stdout, "From %s %s\n", msg->from, date_str);
  172. else
  173. fprintf (stdout, "From nobody %s\n", date_str);
  174. }
  175. fprintf (stdout, "%s\n%s", msg->headers, msg->body);
  176. }
  177. else if (config.action == WRITE)
  178. {
  179. if (config.format == MAILDIR)
  180. maildir_write_message (msg, outboxname);
  181. else if (config.format == MH || config.format == NNMH ||
  182. config.format == NNML)
  183. mh_write_message (msg, outboxname);
  184. else if (config.format == MBOX)
  185. {
  186. out = mbox_open (outboxname, "w");
  187. fprintf (out->fp, "%s\n%s", msg->headers, msg->body);
  188. mbox_close (out);
  189. }
  190. }
  191. else if (config.action == PIPE)
  192. {
  193. outf = popen (pipecmd, "w");
  194. if (outf == NULL)
  195. {
  196. if (config.merr)
  197. {
  198. fprintf (stderr, "%s: %s: ", APPNAME, pipecmd);
  199. perror (NULL);
  200. }
  201. exit (2);
  202. } /* if */
  203. fprintf (outf, "%s\n%s", msg->headers, msg->body);
  204. pclose (outf);
  205. }
  206. else if (config.action == COUNT)
  207. ++count;
  208. else if (config.action == DELETE && config.format == MBOX)
  209. fprintf (tmpp, "%s\n%s", msg->headers, msg->body);
  210. #ifdef HAVE_LIBZ
  211. else if (config.action == DELETE && config.format == ZMBOX)
  212. {
  213. int quux, len, baz;
  214. quux = 0;
  215. baz = strlen (msg->headers);
  216. for (;;)
  217. {
  218. len = gzwrite (tmpp, (msg->headers+quux),
  219. (((quux + BUFLEN) < baz) ? BUFLEN :
  220. (baz - quux)));
  221. quux += len;
  222. if (quux == baz)
  223. break;
  224. }
  225. gzwrite(tmpp, "\n", 1);
  226. quux = 0;
  227. baz = strlen(msg->body);
  228. for (;;)
  229. {
  230. len = gzwrite(tmpp, (msg->body+quux),
  231. (((quux + BUFLEN) < baz) ? BUFLEN :
  232. (baz - quux)));
  233. quux += len;
  234. if (quux == baz)
  235. break;
  236. }
  237. }
  238. #endif /* HAVE_LIBZ */
  239. } /* if */
  240. else if (((((res1 == 0) | (res2 == 0)) ^ config.invert) && delete) &&
  241. ((config.format == MH) || (config.format == NNMH) ||
  242. (config.format == NNML) || (config.format == MAILDIR)))
  243. m_unlink(msg->filename);
  244. free(msg->body);
  245. free(msg->headers);
  246. free(msg);
  247. } /* for */
  248. if ((config.format == MBOX) || (config.format == ZMBOX) ||
  249. (config.format == BZ2MBOX))
  250. mbox_close (mbox);
  251. else if ((config.format == MH) || (config.format == NNMH) ||
  252. (config.format == NNML))
  253. mh_close(boxd);
  254. }
  255. /* }}} */
  256. void recursive_scan (char path[])
  257. /* {{{ */
  258. {
  259. #ifdef HAVE_FTS_OPEN
  260. FTS *ftsfoo;
  261. FTSENT *ftsbar;
  262. #endif /* HAVE_FTS_OPEN */
  263. #ifdef HAVE_FTS_OPEN
  264. {
  265. char *p[2];
  266. p[0] = strdup (path);
  267. p[1] = 0;
  268. ftsfoo = fts_open (p, FTS_NOCHDIR, NULL);
  269. if (ftsfoo == NULL)
  270. {
  271. /* fixme (?) */
  272. perror(APPNAME);
  273. exit (2);
  274. }
  275. while ((ftsbar = fts_read (ftsfoo)))
  276. scan_mailbox (ftsbar->fts_path);
  277. fts_close (ftsfoo);
  278. }
  279. #else
  280. ftw (path, (void *) scan_mailbox, 1);
  281. #endif /* HAVE_FTS_OPEN */
  282. }
  283. /* }}} */
  284. int md5_check_message (char *body, checksum_t *chksum)
  285. /* {{{ */
  286. {
  287. struct md5_ctx a;
  288. unsigned char b[16];
  289. int i;
  290. md5_init_ctx (&a);
  291. if (body == NULL)
  292. md5_process_bytes ("", 0, &a);
  293. else
  294. md5_process_bytes (body, strlen(body), &a);
  295. md5_finish_ctx(&a, b);
  296. for (i = 0; i < chksum->n; i++)
  297. {
  298. if (0 == strncmp (chksum->md5[i], b, 16))
  299. return 1;
  300. }
  301. chksum->md5 =
  302. (char **) xrealloc (chksum->md5, (1 + chksum->n) * sizeof (char *));
  303. chksum->md5[chksum->n] = xstrdup (b);
  304. (chksum->n)++;
  305. return 0;
  306. }
  307. /* }}} */