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 #ifndef _GNU_SOURCE
char *strcasestr(const char *s, const char *find){ char *strcasestr(const char *s, const char *find){
char c, sc; 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); int is_email_address_on_my_domains(char *email, struct __config *cfg);
void init_session_data(struct session_data *sdata); void init_session_data(struct session_data *sdata);
int read_from_stdin(struct session_data *sdata);
#ifndef _GNU_SOURCE #ifndef _GNU_SOURCE
char *strcasestr(const char *s, const char *find); char *strcasestr(const char *s, const char *find);

View File

@ -27,6 +27,22 @@ int import_message(char *filename, struct session_data *sdata, struct __data *da
struct _state state; struct _state state;
struct __counters counters; struct __counters counters;
init_session_data(sdata);
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){ if(stat(filename, &st) != 0){
printf("cannot read: %s\n", filename); printf("cannot read: %s\n", filename);
return rc; return rc;
@ -37,12 +53,14 @@ int import_message(char *filename, struct session_data *sdata, struct __data *da
return rc; return rc;
} }
init_session_data(sdata);
snprintf(sdata->filename, SMALLBUFSIZE-1, "%s", filename); snprintf(sdata->filename, SMALLBUFSIZE-1, "%s", filename);
sdata->sent = 0;
sdata->tot_len = st.st_size; sdata->tot_len = st.st_size;
}
sdata->sent = 0;
state = parse_message(sdata, cfg); state = parse_message(sdata, cfg);
post_parse(sdata, &state, 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; 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){ if(rule){
printf("discarding %s by archiving policy: %s\n", filename, 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: ENDE:
unlink(sdata->tmpframe); unlink(sdata->tmpframe);
if(strcmp(filename, "-") == 0) unlink(sdata->ttmpfile);
switch(rc) { switch(rc) {
case OK: case OK:
printf("imported: %s\n", filename); printf("imported: %s\n", filename);