📄 bogoreader.c
字号:
static bool mailbox_next_mail(void){ bool val = mail_first || have_message; mail_first = false; return val;}/* iterates over files in a directory */static bool dir_next_mail(void){ struct dirent *dirent; struct stat st; for (;;) { if (reader_dir == NULL) { char *x = dir_name; /* open next directory */ if (mailstore_type == MS_MAILDIR) { size_t siz; if (*maildir_sub == NULL) return false; /* IMPORTANT for termination */ siz = strlen(dir_name) + 4 + 1; x = xmalloc(siz); strlcpy(x, dir_name, siz); strlcat(x, *(maildir_sub++), siz); } reader_dir = opendir(x); if (!reader_dir) { fprintf(stderr, "cannot open directory '%s': %s", x, strerror(errno)); } if (x != dir_name) xfree(x); } while ((errno = 0, dirent = readdir(reader_dir)) != NULL) { /* skip private files */ if ((mailstore_type == MS_MAILDIR && dirent->d_name[0] != '.') || (mailstore_type == MS_MH && isdigit((unsigned char)dirent->d_name[0]))) break; } if (errno) { fprintf(stderr, "Cannot read directory %s: %s", dir_name, strerror(errno)); exit(EX_ERROR); } if (dirent == NULL) { if (reader_dir) closedir(reader_dir); reader_dir = NULL; if (mailstore_type == MS_MAILDIR) continue; if (mailstore_type == MS_MH) return false; } filename = namebuff; snprintf(namebuff, sizeof(namebuff), "%s%s%c%s", dir_name, (mailstore_type == MS_MH) ? "" : *(maildir_sub-1), DIRSEP_C, dirent->d_name); bogoreader_close(); fpin = fopen( filename, "r" ); if (fpin == NULL) { fprintf(stderr, "Warning: can't open file '%s': %s\n", filename, strerror(errno)); /* don't barf, the file may have been changed by another MUA, * or a directory that just doesn't belong there, just skip it */ continue; } /* skip non-regular files */ if (0 == fstat(fileno(fpin), &st) && !S_ISREG(st.st_mode)) continue; if (DEBUG_READER(0)) fprintf(dbgout, "%s:%d - reading %s (%p)\n", __FILE__, __LINE__, filename, (void *)fpin); return true; }}/*** _getline functions ***********************************************//* reads from a mailbox, paying attention to ^From lines */static int mailbox_getline(buff_t *buff){ uint used = buff->t.leng; byte *buf = buff->t.text + used; int count; static word_t *saved = NULL; static bool emptyline = false; /* for mailbox /^From / match */ if (saved != NULL) { count = saved->leng; buff_add(buff, saved); word_free(saved); saved = NULL; return count; } count = buff_fgetsl(buff, fpin); have_message = false; /* XXX FIXME: do we need to unescape the >From, >>From, >>>From, ... lines * by discarding the first ">"? */ /* DR 08/25/03 - NO!!! */ if ((firstline || emptyline) && seplen != 0 && count >= (int) seplen && memcmp(separator, buf, seplen) == 0) { if (firstline) { firstline = false; } else { have_message = true; saved = word_new(buf, count); count = EOF; } } else { if (buff->t.leng < buff->size) /* for easier debugging - removable */ Z(buff->t.text[buff->t.leng]); /* for easier debugging - removable */ } emptyline = is_eol((char *)buf, count); return count;}/* reads from an rmail batch, paying attention to ^#! rmail lines */static int rmail_getline(buff_t *buff){ int count; uint used = buff->t.leng; byte *buf = buff->t.text + used; static word_t *saved = NULL; static unsigned long bytesleft = 0; if (saved != NULL) { count = saved->leng; buff_add(buff, saved); word_free(saved); saved = NULL; return count; } if (bytesleft) { count = buff_fgetsln(buff, fpin, bytesleft); bytesleft -= count; return count; } count = buff_fgetsl(buff, fpin); have_message = false; if (count >= (int) seplen && memcmp(separator, buf, seplen) == 0) { uint i; bytesleft = 0; for (i = seplen; i < (uint) count; i++) { if (isspace(buf[i])) continue; if (!isdigit(buf[i])) break; bytesleft = bytesleft * 10 + (buf[i] - '0'); } if (firstline) { firstline = false; } else { have_message = true; saved = word_new(buf, count); count = EOF; } } else { if (buff->t.leng < buff->size) /* for easier debugging - removable */ Z(buff->t.text[buff->t.leng]); /* for easier debugging - removable */ } return count;}/* reads from an ANT batch, paying attention to ^#! rmail lines */static int ant_getline(buff_t *buff){ int count; uint used = buff->t.leng; byte *buf = buff->t.text + used; static word_t *saved = NULL; static bool dot_found = true; if (saved != NULL) { count = saved->leng; buff_add(buff, saved); word_free(saved); saved = NULL; return count; } count = buff_fgetsl(buff, fpin); have_message = false; if (dot_found && count >= (int) seplen && memcmp(separator, buf, seplen) == 0) { dot_found = false; /* ignore until dot */ if (firstline) { firstline = false; } else { have_message = true; saved = word_new(buf, count); count = EOF; } } else { if ((count == 2 || count == 3) && (buf[0] == '.') && (buf[1] == '\r' || buf[1] == '\n')) dot_found = true; /* dot found. look for separator */ if (buff->t.leng < buff->size) /* for easier debugging - removable */ Z(buff->t.text[buff->t.leng]); /* for easier debugging - removable */ } return count;}/* reads a file as a single mail ( no ^From detection ). */static int simple_getline(buff_t *buff){ int count = buff_fgetsl(buff, fpin); if (buff->t.leng < buff->size) /* for easier debugging - removable */ Z(buff->t.text[buff->t.leng]); /* for easier debugging - removable */ return count;}/* initialize for MH directory and * Maildir subdirectories (cur and new). */static void dir_init(const char *name){ reader_dir = NULL; fini = dir_fini; if (mailstore_type == MS_MAILDIR) maildir_sub = maildir_subs; save_dirname(name); return;}/* finish up MH/Maildir store */static void dir_fini(void){ if (reader_dir) closedir(reader_dir); reader_dir = NULL; return;}/* returns current file name */static const char *get_filename(void){ return filename;}/* global reader initialization, exported */void bogoreader_init(int _argc, char **_argv){ mailstore_first = mail_first = true; reader_more = reader__next_mail; fini = dummy_fini; switch (bulk_mode) { case B_NORMAL: /* read mail (mbox) from stdin */ yy_file = fpin; mailstore_next_store = stdin_next_mailstore; if (run_type & (REG_SPAM|REG_GOOD|UNREG_SPAM|UNREG_GOOD)) mbox_mode = true; break; case B_STDIN: /* '-b' - streaming (stdin) mode */ mailstore_next_store = b_stdin_next_mailstore; break; case B_CMDLINE: /* '-B' - command line mode */ argc = _argc; argv = _argv; mailstore_next_store = b_args_next_mailstore; mailstore_next_mail = NULL; break; default: fprintf(stderr, "Unknown bulk_mode = %d\n", (int) bulk_mode); abort(); break; } reader_filename = get_filename;}/* For bogoconfig to distinguish '-I file' from '-I dir' *//* global reader initialization, exported */void bogoreader_name(const char *name){ bool ok; switch (isdir(name)) { case IS_FILE: fpin = fopen( name, "r" ); ok = fpin != NULL; break; case IS_DIR: ok = open_mailstore(name); break; default: ok = false; break; } if (!ok) { fprintf(stderr, "Can't read '%s'\n", name); exit(EX_ERROR); }}/* cleanup after reading a message, exported. *//* Only called if the passthrough code says it is ok to close the file. */void bogoreader_close_ifeof(void){ if (fpin && feof(fpin)) bogoreader_close();}/* global cleanup, exported */void bogoreader_fini(void){ bogoreader_close(); fini();}static void bogoreader_close(void){ if (fpin && fpin != stdin) fclose(fpin); fpin = NULL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -