pilerimport can read single emails from stdin specifying "-" as the filename

This commit is contained in:
SJ 2012-05-04 21:26:16 +02:00
parent 636b866f1c
commit 77b22c7043
3 changed files with 60 additions and 13 deletions

View File

@ -409,6 +409,31 @@ void init_session_data(struct session_data *sdata){
}
int read_from_stdin(struct session_data *sdata){
int fd, rc=ERR, n;
char buf[MAXBUFSIZE];
fd = open(sdata->ttmpfile, O_CREAT|O_EXCL|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR);
if(fd == -1){
syslog(LOG_PRIORITY, "%s: cannot open ttmpfile", sdata->ttmpfile);
return rc;
}
while((n = read(0, buf, sizeof(buf))) > 0){
sdata->tot_len += write(fd, buf, n);
}
if(fsync(fd)){
syslog(LOG_PRIORITY, "%s: fsync() error", sdata->ttmpfile);
}
else rc = OK;
close(fd);
return rc;
}
#ifndef _GNU_SOURCE
char *strcasestr(const char *s, const char *find){
char c, sc;

View File

@ -29,6 +29,7 @@ int drop_privileges(struct passwd *pwd);
int is_email_address_on_my_domains(char *email, struct __config *cfg);
void init_session_data(struct session_data *sdata);
int read_from_stdin(struct session_data *sdata);
#ifndef _GNU_SOURCE
char *strcasestr(const char *s, const char *find);

View File

@ -27,22 +27,40 @@ int import_message(char *filename, struct session_data *sdata, struct __data *da
struct _state state;
struct __counters counters;
if(stat(filename, &st) != 0){
printf("cannot read: %s\n", filename);
return rc;
}
if(S_ISREG(st.st_mode) == 0){
printf("%s is not a file\n", filename);
return rc;
}
init_session_data(sdata);
snprintf(sdata->filename, SMALLBUFSIZE-1, "%s", filename);
if(strcmp(filename, "-") == 0){
if(read_from_stdin(sdata) == ERR){
printf("error reading from stdin\n");
return rc;
}
snprintf(sdata->filename, SMALLBUFSIZE-1, "%s", sdata->ttmpfile);
}
else {
if(stat(filename, &st) != 0){
printf("cannot read: %s\n", filename);
return rc;
}
if(S_ISREG(st.st_mode) == 0){
printf("%s is not a file\n", filename);
return rc;
}
snprintf(sdata->filename, SMALLBUFSIZE-1, "%s", filename);
sdata->tot_len = st.st_size;
}
sdata->sent = 0;
sdata->tot_len = st.st_size;
state = parse_message(sdata, cfg);
post_parse(sdata, &state, cfg);
@ -54,7 +72,7 @@ int import_message(char *filename, struct session_data *sdata, struct __data *da
if(sdata->sent > 631148400) sdata->retained = sdata->sent;
rule = check_againt_ruleset(data->archiving_rules, &state, st.st_size, sdata->spam_message);
rule = check_againt_ruleset(data->archiving_rules, &state, sdata->tot_len, sdata->spam_message);
if(rule){
printf("discarding %s by archiving policy: %s\n", filename, rule);
@ -69,6 +87,9 @@ int import_message(char *filename, struct session_data *sdata, struct __data *da
ENDE:
unlink(sdata->tmpframe);
if(strcmp(filename, "-") == 0) unlink(sdata->ttmpfile);
switch(rc) {
case OK:
printf("imported: %s\n", filename);