From ea8051299dd24d9e5d120b588aabb139e1e03ebd Mon Sep 17 00:00:00 2001 From: SJ Date: Sat, 22 Sep 2012 11:30:37 +0200 Subject: [PATCH] added a utility to contrib/imap that adds seen flags to all messages in INBOX --- configure | 3 +- configure.in | 2 +- contrib/imap/Makefile.in | 40 ++++++++++ contrib/imap/imap-seen.c | 162 +++++++++++++++++++++++++++++++++++++++ src/pilerimport.c | 2 +- 5 files changed, 206 insertions(+), 3 deletions(-) create mode 100644 contrib/imap/Makefile.in create mode 100644 contrib/imap/imap-seen.c diff --git a/configure b/configure index 5193d528..255c270d 100755 --- a/configure +++ b/configure @@ -4334,7 +4334,7 @@ CFLAGS="$static -O2 -Wall -g" LIBS="$antispam_libs $sunos_libs " OBJS="dirs.o base64.o misc.o counters.o cfg.o sig.o decoder.o list.o parser.o parser_utils.o rules.o session.o message.o attachment.o digest.o store.o archive.o tai.o import.o imap.o extract.o $objs" -ac_config_files="$ac_config_files Makefile src/Makefile etc/Makefile util/Makefile init.d/Makefile test/Makefile" +ac_config_files="$ac_config_files Makefile src/Makefile etc/Makefile util/Makefile init.d/Makefile test/Makefile contrib/imap/Makefile" cat >confcache <<\_ACEOF # This file is a shell script that caches the results of configure @@ -5045,6 +5045,7 @@ do "util/Makefile") CONFIG_FILES="$CONFIG_FILES util/Makefile" ;; "init.d/Makefile") CONFIG_FILES="$CONFIG_FILES init.d/Makefile" ;; "test/Makefile") CONFIG_FILES="$CONFIG_FILES test/Makefile" ;; + "contrib/imap/Makefile") CONFIG_FILES="$CONFIG_FILES contrib/imap/Makefile" ;; *) as_fn_error $? "invalid argument: \`$ac_config_target'" "$LINENO" 5;; esac diff --git a/configure.in b/configure.in index 75550df4..24e9eee1 100644 --- a/configure.in +++ b/configure.in @@ -346,6 +346,6 @@ CFLAGS="$static -O2 -Wall -g" LIBS="$antispam_libs $sunos_libs " OBJS="dirs.o base64.o misc.o counters.o cfg.o sig.o decoder.o list.o parser.o parser_utils.o rules.o session.o message.o attachment.o digest.o store.o archive.o tai.o import.o imap.o extract.o $objs" -AC_CONFIG_FILES([Makefile src/Makefile etc/Makefile util/Makefile init.d/Makefile test/Makefile]) +AC_CONFIG_FILES([Makefile src/Makefile etc/Makefile util/Makefile init.d/Makefile test/Makefile contrib/imap/Makefile]) AC_OUTPUT diff --git a/contrib/imap/Makefile.in b/contrib/imap/Makefile.in new file mode 100644 index 00000000..d3536ce6 --- /dev/null +++ b/contrib/imap/Makefile.in @@ -0,0 +1,40 @@ +SHELL = @SHELL@ + +prefix = @prefix@ +exec_prefix = @exec_prefix@ +bindir = @bindir@ +sbindir = @sbindir@ +includedir = @includedir@ +libdir = @libdir@ +libexecdir = @libexecdir@ +srcdir = @srcdir@ +sysconfdir = @sysconfdir@ +mandir = @mandir@ +datarootdir = @datarootdir@ +localstatedir = @localstatedir@ + +CC = @CC@ +CFLAGS = @CFLAGS@ @CPPFLAGS@ +DEFS = @defs@ +INCDIR = -I. -I.. -I../.. -I../../src -I../src @INCDIR@ @mysql_includes@ +LIBDIR = -L. @LIBDIR@ @LDFLAGS@ -L../src -L../../src +LIBS = @LIBS@ @mysql_libs@ +RUNNING_USER = @RUNNING_USER@ +RUNNING_GROUP = `@id_bin@ -gn $(RUNNING_USER)` +OBJS = + +INSTALL = @INSTALL@ + +all: $(OBJS) imap-seen + +imap-seen: imap-seen.c ../../src/libpiler.a + $(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $< -lpiler $(LIBS) $(LIBDIR) + + +install: + +clean: + rm -f imap-seen + +distclean: clean + rm -f Makefile diff --git a/contrib/imap/imap-seen.c b/contrib/imap/imap-seen.c new file mode 100644 index 00000000..d65daa12 --- /dev/null +++ b/contrib/imap/imap-seen.c @@ -0,0 +1,162 @@ +/* + * imap-seen.c, SJ + */ + +#define _FILE_OFFSET_BITS 64 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define SKIPLIST "junk,trash,spam,draft" +#define MBOX_ARGS 1024 + +extern char *optarg; +extern int optind; + +int quiet=0; + +int connect_to_imap_server(int sd, int *seq, char *imapserver, char *username, char *password); + + +void usage(){ + printf("usage: imap-seen [-c ] -i -u -p \n"); + exit(0); +} + + +int main(int argc, char **argv){ + int c, rc=0, messages=0; + char *configfile=CONFIG_FILE; + char *imapserver=NULL, *username=NULL, *password=NULL, *skiplist=SKIPLIST; + struct __config cfg; + int sd, n; + int seq=1; + char *p, tag[SMALLBUFSIZE], tagok[SMALLBUFSIZE], buf[MAXBUFSIZE]; + + + while(1){ + +#ifdef _GNU_SOURCE + static struct option long_options[] = + { + {"config", required_argument, 0, 'c' }, + {"imapserver", required_argument, 0, 'i' }, + {"username", required_argument, 0, 'u' }, + {"password", required_argument, 0, 'p' }, + {"skiplist", required_argument, 0, 'x' }, + {"help", no_argument, 0, 'h' }, + {0,0,0,0} + }; + + int option_index = 0; + + c = getopt_long(argc, argv, "c:i:u:p:x:h?", long_options, &option_index); +#else + c = getopt(argc, argv, "c:i:u:p:x:h?"); +#endif + + if(c == -1) break; + + switch(c){ + + case 'c' : + configfile = optarg; + break; + + case 'i' : + imapserver = optarg; + break; + + case 'u' : + username = optarg; + break; + + case 'p' : + password = optarg; + break; + + case 'x' : + skiplist = optarg; + break; + + case 'h' : + case '?' : + usage(); + break; + + + default : + break; + } + } + + + + if(!imapserver) usage(); + + cfg = read_config(configfile); + + (void) openlog("imap-seen", LOG_PID, LOG_MAIL); + + if(imapserver && username && password){ + + if((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1){ + printf("cannot create socket\n"); + return ERR; + } + + if(connect_to_imap_server(sd, &seq, imapserver, username, password) == ERR){ + close(sd); + return ERR; + } + + + snprintf(tag, sizeof(tag)-1, "A%d", seq); snprintf(tagok, sizeof(tagok)-1, "\r\nA%d OK", (seq)++); + snprintf(buf, sizeof(buf)-1, "%s SELECT \"INBOX\"\r\n", tag); + send(sd, buf, strlen(buf), 0); + + + n = recvtimeout(sd, buf, MAXBUFSIZE, 10); + + p = strstr(buf, " EXISTS"); + if(p){ + *p = '\0'; + p = strrchr(buf, '\n'); + if(p){ + while(!isdigit(*p)){ p++; } + messages = atoi(p); + } + } + + printf("found %d messages\n", messages); + + snprintf(tag, sizeof(tag)-1, "A%d", seq); snprintf(tagok, sizeof(tagok)-1, "\r\nA%d OK", (seq)++); + snprintf(buf, sizeof(buf)-1, "%s STORE 1:%d +FLAGS (\\Seen)\r\n", tag, messages); + send(sd, buf, strlen(buf), 0); + + n = recvtimeout(sd, buf, MAXBUFSIZE, 10); + + close(sd); + } + + if(quiet == 0) printf("\n"); + + return rc; +} + + diff --git a/src/pilerimport.c b/src/pilerimport.c index 8cf13deb..4fd9e5a2 100644 --- a/src/pilerimport.c +++ b/src/pilerimport.c @@ -293,7 +293,7 @@ int import_from_imap_server(char *imapserver, char *username, char *password, st void usage(){ - printf("usage: pilerimport [-c ] -e | -m | -d | -i -u -p \n"); + printf("usage: pilerimport [-c ] -e | -m | -d | -i -u -p [-F ] [-R]\n"); exit(0); }