fixed the pilerexport utility

This commit is contained in:
SJ 2012-01-08 16:16:00 +01:00
parent bafbbc7fa8
commit b0e4debb12
3 changed files with 130 additions and 33 deletions

View File

@ -60,8 +60,8 @@ pilerexport: pilerexport.c libpiler.a
pilerconf: pilerconf.c cfg.o misc.o tai.o pilerconf: pilerconf.c cfg.o misc.o tai.o
$(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $^ $(LIBDIR) $(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $^ $(LIBDIR)
test: test: test.c libpiler.a
$(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o pilertest $(srcdir)/test.c -lpiler $(LIBS) $(LIBDIR) @LDFLAGS@ $(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o pilertest $^ -lpiler $(LIBS) $(LIBDIR) @LDFLAGS@
%.o: $(srcdir)/%.c %.o: $(srcdir)/%.c
$(CC) $(CFLAGS) -fPIC $(INCDIR) $(DEFS) -c $< -o $@ $(CC) $(CFLAGS) -fPIC $(INCDIR) $(DEFS) -c $< -o $@

View File

@ -13,19 +13,30 @@
#include <time.h> #include <time.h>
#include <locale.h> #include <locale.h>
#include <syslog.h> #include <syslog.h>
#include <getopt.h>
#include <piler.h> #include <piler.h>
extern char *optarg; extern char *optarg;
extern int optind; extern int optind;
int dryrun = 0;
char *query=NULL; char *query=NULL;
regex_t regexp; regex_t regexp;
void usage(){ void usage(){
printf("usage: .... \n"); printf("\nusage: pilerexport \n\n");
printf(" [-c|--config <config file>] \n");
printf(" -a|--start-date <YYYY.MM.DD> -b|--stop-date <YYYY.MM.DD> \n");
printf(" -f|--from <email@address> -r|--to <email@address>\n");
printf(" -s|--minsize <number> -S|--maxsize <number>\n");
printf(" -A|--all -d|--dryrun \n");
printf("\n use -A if you don't want to specify the start/stop time nor any from/to address\n\n");
regfree(&regexp);
exit(0); exit(0);
} }
@ -39,6 +50,34 @@ void clean_exit(char *msg, int rc){
} }
unsigned long convert_time(char *yyyymmdd, int h, int m, int s){
char *p;
struct tm tm;
if(yyyymmdd == NULL) return 0;
memset((char*)&tm, 0, sizeof(tm));
tm.tm_isdst = -1;
tm.tm_hour = h;
tm.tm_min = m;
tm.tm_sec = s;
p = strchr(yyyymmdd, '.'); if(!p) return 0;
*p = '\0'; tm.tm_year = atoi(yyyymmdd) - 1900; yyyymmdd = p+1;
p = strchr(yyyymmdd, '.'); if(!p) return 0;
*p = '\0'; tm.tm_mon = atoi(yyyymmdd) - 1; yyyymmdd = p+1;
tm.tm_mday = atoi(yyyymmdd);
tm.tm_isdst = -1;
return mktime(&tm);
}
int append_email_to_buffer(char **buffer, char *email){ int append_email_to_buffer(char **buffer, char *email){
int len, arglen; int len, arglen;
char *s=NULL, emailaddress[SMALLBUFSIZE]; char *s=NULL, emailaddress[SMALLBUFSIZE];
@ -119,25 +158,30 @@ int export_emails_matching_to_query(struct session_data *sdata, char *s, struct
digest = (char*)row[2]; digest = (char*)row[2];
bodydigest = (char*)row[3]; bodydigest = (char*)row[3];
if(dryrun == 0){
snprintf(filename, sizeof(filename)-1, "%llu.eml", id); snprintf(filename, sizeof(filename)-1, "%llu.eml", id);
f = fopen(filename, "w"); f = fopen(filename, "w");
if(f){ if(f){
rc = retrieve_email_from_archive(sdata, f, cfg); rc = retrieve_email_from_archive(sdata, f, cfg);
fclose(f); fclose(f);
snprintf(sdata->filename, SMALLBUFSIZE-1, "%s", filename); snprintf(sdata->filename, SMALLBUFSIZE-1, "%s", filename);
make_digests(sdata, cfg); make_digests(sdata, cfg);
if(strcmp(digest, sdata->digest) == 0 && strcmp(bodydigest, sdata->bodydigest) == 0) if(strcmp(digest, sdata->digest) == 0 && strcmp(bodydigest, sdata->bodydigest) == 0)
printf("exported %s, verification: OK\n", filename); printf("exported %s, verification: OK\n", filename);
else else
printf("exported %s, verification: FAILED\n", filename); printf("exported %s, verification: FAILED\n", filename);
}
else printf("cannot open: %s\n", filename);
}
else {
printf("id:%llu\n", id);
} }
else printf("cannot open: %s\n", filename);
} }
} }
@ -151,10 +195,10 @@ int export_emails_matching_to_query(struct session_data *sdata, char *s, struct
int main(int argc, char **argv){ int main(int argc, char **argv){
int i, rc, exportall=0, minsize=0, maxsize=0; int c, rc, exportall=0, minsize=0, maxsize=0;
int where_condition=0; int where_condition=0;
size_t nmatch=0; size_t nmatch=0;
//unsigned long startdate=0, stopdate=0; unsigned long startdate=0, stopdate=0;
char *configfile=CONFIG_FILE; char *configfile=CONFIG_FILE;
char *to=NULL, *from=NULL; char *to=NULL, *from=NULL;
char s[SMALLBUFSIZE]; char s[SMALLBUFSIZE];
@ -167,24 +211,51 @@ int main(int argc, char **argv){
} }
while((i = getopt(argc, argv, "c:s:S:f:r:ah?")) > 0){ while(1){
switch(i){ #ifdef _GNU_SOURCE
static struct option long_options[] =
{
{"config", required_argument, 0, 'c' },
{"minsize", required_argument, 0, 's' },
{"maxsize", required_argument, 0, 'S' },
{"all", no_argument, 0, 'A' },
{"dry-run", no_argument, 0, 'd' },
{"dryrun", no_argument, 0, 'd' },
{"help", no_argument, 0, 'h' },
{"version", no_argument, 0, 'v' },
{"from", required_argument, 0, 'f' },
{"to", required_argument, 0, 'r' },
{"start-date", required_argument, 0, 'a' },
{"stop-date", required_argument, 0, 'b' },
{0,0,0,0}
};
int option_index = 0;
c = getopt_long(argc, argv, "c:s:S:f:r:a:b:Adhv?", long_options, &option_index);
#else
c = getopt(argc, argv, "c:s:S:f:r:a:b:Adhv?");
#endif
if(c == -1) break;
switch(c){
case 'c' : case 'c' :
configfile = optarg; configfile = optarg;
break; break;
case 's' : case 's' :
maxsize = atoi(optarg);
break;
case 'S' :
minsize = atoi(optarg); minsize = atoi(optarg);
break; break;
case 'S' :
maxsize = atoi(optarg);
break;
case 'a' :
case 'A' :
exportall = 1; exportall = 1;
break; break;
@ -211,19 +282,32 @@ int main(int argc, char **argv){
break; break;
case 'a' :
startdate = convert_time(optarg, 0, 0, 0);
break;
case 'h' :
case '?' : case 'b' :
stopdate = convert_time(optarg, 23, 59, 59);
break;
case 'd' :
dryrun = 1;
break;
default :
usage(); usage();
break; break;
}
default :
break;
}
} }
if(from == NULL && to == NULL && startdate == 0 && stopdate == 0 && exportall == 0) usage();
regfree(&regexp); regfree(&regexp);
@ -268,12 +352,25 @@ int main(int argc, char **argv){
rc += append_string_to_buffer(&query, s); rc += append_string_to_buffer(&query, s);
} }
if(startdate > 0){
if(where_condition) rc = append_string_to_buffer(&query, " AND ");
snprintf(s, sizeof(s)-1, " `sent` >= %ld", startdate);
rc += append_string_to_buffer(&query, s);
}
if(stopdate > 0){
if(where_condition) rc = append_string_to_buffer(&query, " AND ");
snprintf(s, sizeof(s)-1, " `sent` <= %ld", stopdate);
rc += append_string_to_buffer(&query, s);
}
rc += append_string_to_buffer(&query, " ORDER BY id ASC"); rc += append_string_to_buffer(&query, " ORDER BY id ASC");
if(rc) clean_exit("malloc problem building query", 1); if(rc) clean_exit("malloc problem building query", 1);
printf("query: *%s*\n", query);

View File

@ -157,7 +157,7 @@ int import_from_maildir(char *directory, struct session_data *sdata, struct __da
void usage(){ void usage(){
printf("usage: pilerimport -e <eml file> -m <mailbox file> -d <directory>\n"); printf("usage: pilerimport [-c <config file>] -e <eml file> | -m <mailbox file> | -d <directory>\n");
exit(0); exit(0);
} }