mairix.c 13.8 KB
Newer Older
Richard Curnow's avatar
Richard Curnow committed
1
/*
2
  $Header: /cvs/src/mairix/mairix.c,v 1.12 2003/01/18 00:38:12 richard Exp $
Richard Curnow's avatar
Richard Curnow committed
3 4 5 6

  mairix - message index builder and finder for maildir folders.

 **********************************************************************
7
 * Copyright (C) Richard P. Curnow  2002, 2003
Richard Curnow's avatar
Richard Curnow committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 * 
 **********************************************************************
 */

#include "mairix.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <unistd.h>
#include <ctype.h>
31
#include <locale.h>
Richard Curnow's avatar
Richard Curnow committed
32

33 34 35 36
#ifdef TEST_OOM
int total_bytes=0;
#endif

Richard Curnow's avatar
Richard Curnow committed
37 38 39 40
int verbose = 0;

static char *folder_base = NULL;
static char *folders = NULL;
41
static char *mh_folders = NULL;
Richard Curnow's avatar
Richard Curnow committed
42 43
static char *vfolder = NULL;
static char *database_path = NULL;
44
static enum folder_type output_folder_type = FT_MAILDIR;
Richard Curnow's avatar
Richard Curnow committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

static int file_exists(char *name)/*{{{*/
{
  struct stat sb;

  if (stat(name, &sb) < 0) {
    return 0;
  }
  return 1;
}
/*}}}*/
static char *copy_value(char *text)/*{{{*/
{
  char *p;
  for (p = text; *p && (*p != '='); p++) ;
  if (!*p) return NULL;
  p++;
  return new_string(p);
}
/*}}}*/
Richard Curnow's avatar
Richard Curnow committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
static void add_folders(char **folders, char *extra_folders)/*{{{*/
{
  /* note : extra_pointers is stale after this routine exits. */
  
  if (!*folders) {
    *folders = extra_folders;
  } else {
    char *old_folders = *folders;
    char *new_folders;
    int old_len, extra_len;
    old_len = strlen(old_folders);
    extra_len = strlen(extra_folders);
    new_folders = new_array(char, old_len + extra_len + 2);
    strcpy(new_folders, old_folders);
    strcpy(new_folders + old_len, ":");
    strcpy(new_folders + old_len + 1, extra_folders);
    *folders = new_folders;
    free(old_folders);
  }
}
/*}}}*/
Richard Curnow's avatar
Richard Curnow committed
86 87 88
static void parse_rc_file(char *name)/*{{{*/
{
  FILE *in;
Richard Curnow's avatar
Richard Curnow committed
89
  char line[4096], *p;
Richard Curnow's avatar
Richard Curnow committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
  int len, lineno;
  int all_blank;
  int used_default_name = 0;

  if (!name) {
    /* open default file */
    struct passwd *pw;
    char *home;
    pw = getpwuid(getuid());
    home = pw->pw_dir;
    if (!pw) {
      fprintf(stderr, "Cannot lookup passwd entry for this user\n");
      exit(1);
    }
    home = pw->pw_dir;
    name = new_array(char, strlen(home) + 12);
    strcpy(name, home);
    strcat(name, "/.mairixrc");
    used_default_name = 1;
  }
    
  in = fopen(name, "r");
  if (!in) {
    fprintf(stderr, "Cannot open %s, exiting\n", name);
    exit(1);
  }

  lineno = 0;
  while(fgets(line, sizeof(line), in)) {
    lineno++;
    len = strlen(line);
    if (len > sizeof(line) - 4) {
      fprintf(stderr, "Line %d in %s too long, exiting\n", lineno, name);
      exit(1);
    }

    if (line[len-1] == '\n') {
      line[len-1] = '\0';
    }
    
    /* Strip trailing comments. */
    for (p=line; *p && !strchr("#!;%", *p); p++) ;
    if (*p) *p = '\0';

    /* Discard blank lines */
    all_blank = 1;
    for (p=line; *p; p++) {
      if (!isspace(*p)) {
        all_blank = 0;
        break;
      }
    }
    
    if (all_blank) continue;
    
    /* Now a real line to parse */
    if (!strncasecmp(p, "base", 4)) folder_base = copy_value(p);
Richard Curnow's avatar
Richard Curnow committed
147 148
    else if (!strncasecmp(p, "folders", 7)) add_folders(&folders, copy_value(p));
    else if (!strncasecmp(p, "mh_folders", 10)) add_folders(&mh_folders, copy_value(p));
149 150 151 152 153 154 155
    else if (!strncasecmp(p, "vfolder_format", 14)) {
      char *temp;
      temp = copy_value(p);
      if (!strncasecmp(temp, "mh", 2)) {
        output_folder_type = FT_MH;
      } else if (!strncasecmp(temp, "maildir", 7)) {
        output_folder_type = FT_MAILDIR;
156 157 158 159
      } else if (!strncasecmp(temp, "raw", 3)) {
        output_folder_type = FT_RAW;
      }
      else {
160 161 162 163
        fprintf(stderr, "Unrecognized vfolder_format <%s>\n", temp);
      }
      free(temp);
    }
Richard Curnow's avatar
Richard Curnow committed
164 165 166
    else if (!strncasecmp(p, "vfolder", 7)) vfolder = copy_value(p);
    else if (!strncasecmp(p, "database", 8)) database_path = copy_value(p);
    else {
167 168 169
      if (verbose) {
        fprintf(stderr, "Unrecognized option at line %d in %s\n", lineno, name);
      }
Richard Curnow's avatar
Richard Curnow committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    }
  }
  
  fclose(in);

  if (used_default_name) free(name);
}
/*}}}*/
static int compare_strings(const void *a, const void *b)/*{{{*/
{
  const char **aa = (const char **) a;
  const char **bb = (const char **) b;
  return strcmp(*aa, *bb);
}
/*}}}*/
static int check_message_list_for_duplicates(struct msgpath_array *msgs)/*{{{*/
{
  char **sorted_paths;
  int i, n;
  int result;

  n = msgs->n;
  sorted_paths = new_array(char *, n);
  for (i=0; i<n; i++) {
    sorted_paths[i] = msgs->paths[i].path;
  }
  qsort(sorted_paths, n, sizeof(char *), compare_strings);
#if 0
  for (i=0; i<n; i++) {
    printf("%4d : %s\n", i, sorted_paths[i]);
  }
#endif
  result = 0;
  for (i=1; i<n; i++) {
    if (!strcmp(sorted_paths[i-1], sorted_paths[i])) {
      result = 1;
      break;
    }
  }
  
  free(sorted_paths);
  return result;
}
/*}}}*/

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
static void emit_int(int x)/*{{{*/
{
  char buf1[20], buf2[20];
  char *p, *q;
  int neg=0;
  p = buf1;
  *p = '0'; /* In case x is zero */
  if (x < 0) {
    neg = 1;
    x = -x;
  }
  while (x) {
    *p++ = '0' + (x % 10);
    x /= 10;
  }
  p--;
  q = buf2;
  if (neg) *q++ = '-';
  while (p >= buf1) {
    *q++ = *p--;
  }
  write(2, buf2, q-buf2);  
  return;
}
/*}}}*/
volatile void out_of_mem(char *file, int line, size_t size)/*{{{*/
{
  /* Hairy coding ahead - can't use any [s]printf, itoa etc because
   * those might try to use the heap! */

  int filelen;
  char *p;
  
  static char msg1[] = "Out of memory (at ";
  static char msg2[] = " bytes)\n";
  /* Perhaps even strlen is unsafe in this situation? */
  p = file;
  while (*p) p++;
  filelen = p - file;
  write(2, msg1, sizeof(msg1));
  write(2, file, filelen);
  write(2, ":", 1);
  emit_int(line);
  write(2, ", ", 2);
  emit_int(size);
  write(2, msg2, sizeof(msg2));
  exit(1); 
}
/*}}}*/
Richard Curnow's avatar
Richard Curnow committed
264 265 266
static char *get_version(void)/*{{{*/
{
  static char buffer[256];
267
  static char cvs_version[] = "$Name: V0_11_pre1 $";
Richard Curnow's avatar
Richard Curnow committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
  char *p, *q;
  for (p=cvs_version; *p; p++) {
    if (*p == ':') {
      p++;
      break;
    }
  }
  while (isspace(*p)) p++;
  if (*p == '$') {
    strcpy(buffer, "development version");
  } else {
    for (q=buffer; *p && *p != '$'; p++) {
      if (!isspace(*p)) {
        if (*p == '_') *q++ = '.';
        else *q++ = *p;
      }
    }
    *q = 0;
  }

  return buffer;
}
/*}}}*/
static void print_copyright(void)/*{{{*/
{
  fprintf(stderr,
294
          "mairix %s, Copyright (C) 2002, 2003 Richard P. Curnow\n"
Richard Curnow's avatar
Richard Curnow committed
295 296 297 298 299 300
          "mairix comes with ABSOLUTELY NO WARRANTY.\n"
          "This is free software, and you are welcome to redistribute it\n"
          "under certain conditions; see the GNU General Public License for details.\n\n",
          get_version());
}
/*}}}*/
301
static void usage(void)/*{{{*/
Richard Curnow's avatar
Richard Curnow committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
{
  print_copyright();
  
  printf("mairix [-h]                                    : Show help\n"
         "mairix [-f <rcfile>] [-v] [-p]                 : Build index\n"
         "mairix [-f <rcfile>] [-a] [-t] expr1 ... exprN : Run search\n"
         "-h          : show this help\n"
         "-f <rcfile> : use alternative rc file (default ~/.mairixrc)\n"
         "-v          : be verbose\n"
         "-p          : purge messages that no longer exist\n"
         "-a          : add new matches to virtual folder (default : clear it first)\n"
         "-t          : include all messages in same threads as matching messages\n"
         "expr_i      : search expression (all expr's AND'ed together):\n"
         "    word          : match word in whole message\n"
         "    t:word        : match word in To: header\n"
         "    c:word        : match word in Cc: header\n"
         "    f:word        : match word in From: header\n"
         "    a:word        : match word in To:, Cc: or From: headers (address)\n"
         "    s:word        : match word in Subject: header\n"
         "    b:word        : match word in message body\n"
         "    bs:word       : match word in Subject: header or body (or any other group of prefixes)\n"
         "    s:word1+word2 : match both words in Subject:\n"
         "    s:word1,word2 : match either word or both words in Subject:\n"
         "    s:~word       : match messages not containing word in Subject:\n"
         "    s:substring=  : match substring in any word in Subject:\n"
         "    s:substring=2 : match substring with <=2 errors in any word in Subject:\n"
         "\n"
         "    (See documentation for more examples)\n"
         );
}
332
    /*}}}*/
Richard Curnow's avatar
Richard Curnow committed
333
/* Notes on folder management: {{{
Richard Curnow's avatar
Richard Curnow committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
 
   Assumption is that the user wants to keep the 'vfolder' directories under a
   common root with the real maildir folders.  This allows a common value for
   mutt's 'folder' variable => the '+' and '=' prefixes work better.  This
   means the indexer here can't just scan down all subdirectories of a single
   ancestor, because it'll pick up its own vfolders.  So, use environment
   variables to tailor the folders.
 
   MAIRIX_FOLDER_BASE is the common ancestor directory of the folders (aka
   mutt's 'folder' variable)

   MAIRIX_FOLDERS is a colon-separated list of folders underneath that, with
   the feature that '...' after a component means any maildir underneath that,
   e.g.

   MAIRIX_VFOLDER is the parent of the vfolders underneath the base
   
   MAIRIX_FOLDER_BASE = "/home/foobar/mail"
   MAIRIX_FOLDERS = "inbox:lists...:action:archive..."
   MAIRIX_VFOLDER = "vf"

   so /home/foobar/mail/vf/search1/{new,cur,tmp} contain the output for search1 etc.
Richard Curnow's avatar
Richard Curnow committed
356
   }}} */
Richard Curnow's avatar
Richard Curnow committed
357

Richard Curnow's avatar
Richard Curnow committed
358
int main (int argc, char **argv)/*{{{*/
Richard Curnow's avatar
Richard Curnow committed
359 360 361 362 363
{
  struct msgpath_array *msgs;
  struct database *db;

  char *arg_rc_file_path = NULL;
Richard Curnow's avatar
Richard Curnow committed
364
  char *arg_vfolder = NULL;
Richard Curnow's avatar
Richard Curnow committed
365 366 367 368 369 370
  int do_augment = 0;
  int do_threads = 0;
  int do_search = 0;
  int do_purge = 0;
  int any_updates = 0;
  int any_purges = 0;
Richard Curnow's avatar
Richard Curnow committed
371
  int do_help = 0;
Richard Curnow's avatar
Richard Curnow committed
372

373 374
  setlocale(LC_CTYPE, "");

Richard Curnow's avatar
Richard Curnow committed
375 376 377
  while (++argv, --argc) {
    if (!*argv) {
      break;
Richard Curnow's avatar
Richard Curnow committed
378
    } else if (!strcmp(*argv, "-f") || !strcmp(*argv, "--rcfile")) {
Richard Curnow's avatar
Richard Curnow committed
379 380
      ++argv, --argc;
      arg_rc_file_path = *argv;
Richard Curnow's avatar
Richard Curnow committed
381
    } else if (!strcmp(*argv, "-t") || !strcmp(*argv, "--threads")) {
Richard Curnow's avatar
Richard Curnow committed
382 383
      do_search = 1;
      do_threads = 1;
Richard Curnow's avatar
Richard Curnow committed
384
    } else if (!strcmp(*argv, "-a") || !strcmp(*argv, "--augment")) {
Richard Curnow's avatar
Richard Curnow committed
385 386
      do_search = 1;
      do_augment = 1;
Richard Curnow's avatar
Richard Curnow committed
387 388 389 390
    } else if (!strcmp(*argv, "-o") || !strcmp(*argv, "--vfolder")) {
      ++argv, --argc;
      arg_vfolder = *argv;
    } else if (!strcmp(*argv, "-p") || !strcmp(*argv, "--purge")) {
Richard Curnow's avatar
Richard Curnow committed
391
      do_purge = 1;
Richard Curnow's avatar
Richard Curnow committed
392
    } else if (!strcmp(*argv, "-v") || !strcmp(*argv, "--verbose")) {
Richard Curnow's avatar
Richard Curnow committed
393
      verbose = 1;
Richard Curnow's avatar
Richard Curnow committed
394 395 396
    } else if (!strcmp(*argv, "-h") ||
               !strcmp(*argv, "--help")) {
      do_help = 1;
Richard Curnow's avatar
Richard Curnow committed
397 398 399 400 401 402 403 404 405 406 407
    } else if ((*argv)[0] == '-') {
      fprintf(stderr, "Unrecognized option %s\n", *argv);
    } else if (!strcmp(*argv, "--")) {
      /* End of args */
      break;
    } else {
      /* standard args start */
      break;
    }
  }

Richard Curnow's avatar
Richard Curnow committed
408 409 410 411 412 413 414 415 416
  if (do_help) {
    usage();
    exit(0);
  }

  if (verbose) {
    print_copyright();
  }
  
Richard Curnow's avatar
Richard Curnow committed
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
  if (*argv) {
    /* There are still args to process */
    do_search = 1;
  }
      
  parse_rc_file(arg_rc_file_path);

  if (getenv("MAIRIX_FOLDER_BASE")) {
    folder_base = getenv("MAIRIX_FOLDER_BASE");
  }

  if (getenv("MAIRIX_FOLDERS")) {
    folders = getenv("MAIRIX_FOLDERS");
  }

432 433 434 435
  if (getenv("MAIRIX_MH_FOLDERS")) {
    mh_folders = getenv("MAIRIX_MH_FOLDERS");
  }

Richard Curnow's avatar
Richard Curnow committed
436 437 438 439 440 441 442 443
  if (getenv("MAIRIX_VFOLDER")) {
    vfolder = getenv("MAIRIX_VFOLDER");
  }

  if (getenv("MAIRIX_DATABASE")) {
    database_path = getenv("MAIRIX_DATABASE");
  }

Richard Curnow's avatar
Richard Curnow committed
444 445 446 447
  if (arg_vfolder) {
    vfolder = arg_vfolder;
  }
  
Richard Curnow's avatar
Richard Curnow committed
448 449 450 451 452 453 454 455 456 457 458 459
  if (!folder_base) {
    fprintf(stderr, "No folder_base/MAIRIX_FOLDER_BASE set\n");
    exit(1);
  }
  
  if (!database_path) {
    fprintf(stderr, "No database/MAIRIX_DATABASE set\n");
    exit(1);
  }
  
  if (do_search) {
    if (!vfolder) {
460 461 462 463 464
      if (output_folder_type != FT_RAW) {
        fprintf(stderr, "No vfolder/MAIRIX_VFOLDER set\n");
        exit(1);
      }
      vfolder = new_string("");
Richard Curnow's avatar
Richard Curnow committed
465 466
    }

467
    search_top(do_threads, do_augment, database_path, folder_base, vfolder, argv, output_folder_type, verbose);
Richard Curnow's avatar
Richard Curnow committed
468 469
    
  } else {
470 471
    if (!folders && !mh_folders) {
      fprintf(stderr, "No [mh_]folders/MAIRIX_[MH_]FOLDERS set\n");
Richard Curnow's avatar
Richard Curnow committed
472 473 474
      exit(1);
    }
    
Richard Curnow's avatar
Richard Curnow committed
475
    if (verbose) printf("Finding all currently existing messages...\n");
476 477 478 479 480 481 482 483
    msgs = new_msgpath_array();
    if (folders) {
      build_message_list(folder_base, folders, FT_MAILDIR, msgs);
    }
    if (mh_folders) {
      build_message_list(folder_base, mh_folders, FT_MH, msgs);
    }

Richard Curnow's avatar
Richard Curnow committed
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
    if (check_message_list_for_duplicates(msgs)) {
      fprintf(stderr, "Message list contains duplicates - check your 'folders' setting\n");
      exit(1);
    }

    /* Try to open existing database */
    if (file_exists(database_path)) {
      if (verbose) printf("Reading existing database...\n");
      db = new_database_from_file(database_path);
      if (verbose) printf("Loaded %d existing messages\n", db->n_paths);
    } else {
      if (verbose) printf("Starting new database\n");
      db = new_database();
    }
    
    any_updates = update_database(db, msgs->paths, msgs->n);
    if (do_purge) {
      any_purges = cull_dead_messages(db);
    }
    if (1 || any_updates || any_purges) {
      /* For now write it every time.  This is obviously the most reliable method. */
      write_database(db, database_path);
    }

#if 0
    get_db_stats(db);
#endif

    free_database(db);
    free_msgpath_array(msgs);
  }
  
  return 0;
  
}
Richard Curnow's avatar
Richard Curnow committed
519
/*}}}*/