From 90e5b52953582441b1de78a597acc8e3847f25c0 Mon Sep 17 00:00:00 2001 From: Bernhard Guillon Date: Wed, 7 Mar 2012 17:19:38 +0100 Subject: [PATCH] implement very slow but working headers to hashmap --- configure.ac | 3 +- src/Makefile.am | 2 +- src/imap.vala | 180 ++++++++++++++++++++++++++------------------------------ src/main.vala | 12 +++- 4 files changed, 95 insertions(+), 102 deletions(-) diff --git a/configure.ac b/configure.ac index 321c5c1..dcdcff2 100644 --- a/configure.ac +++ b/configure.ac @@ -11,7 +11,8 @@ AM_PROG_VALAC pkg_modules="glib-2.0 >= 2.24.1 gobject-2.0 >= 2.24.1 - gio-2.0 >= 2.24.1" + gio-2.0 >= 2.24.1 + gee-1.0 >= 0.5" PKG_CHECK_MODULES(ANTIDOTE, [$pkg_modules]) AC_SUBST(ANTIDOTE_CFLAGS) AC_SUBST(ANTIDOTE_LIBS) diff --git a/src/Makefile.am b/src/Makefile.am index e731096..d150d41 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -5,7 +5,7 @@ bin_PROGRAMS = antidote #FIXME: make libs antidote_SOURCES = main.vala imap.vala logger.vala antidote_LDADD = $(ANTIDOTE_LIBS) -antidote_VALAFLAGS = --pkg gio-2.0 +antidote_VALAFLAGS = --pkg gio-2.0 --pkg gee-1.0 CLEANFILES = *.h *.c *.vapi *.stamp diff --git a/src/imap.vala b/src/imap.vala index 4dc40c1..0e6a87e 100644 --- a/src/imap.vala +++ b/src/imap.vala @@ -1,6 +1,7 @@ /* FIXME: refactor the check response stuff */ /* FIXME: try to be doxygen conform */ /* FIXME: refactor a lot ;) */ +using Gee; namespace Antidote { @@ -11,6 +12,15 @@ namespace Antidote { LIST } + /* message struct */ + struct Message { + public string from; + public string to; + public string date; + public string subject; + public string full_header; + } + class IMAP { public IMAP () { logger = new Logger(); @@ -66,128 +76,102 @@ namespace Antidote { } } - /* login into account throw imap error LOGIN */ - public void login() throws GLib.Error, IMAPError { - var q_id = next_q_id(); - var message = @"$q_id LOGIN $username $password\n"; - conn.output_stream.write(message.data); - logger.log(logger.LogLevel.DEBUG,message); + /* simple imap command wrapper */ + private string imap_command(string command) throws GLib.Error, IMAPError { + var ret = ""; + var q_id = next_q_id(); - /* Check response */ - var status_line = response.read_line(null); - if (!status_line.contains(q_id + " OK")) { - throw new IMAPError.LOGIN(status_line); - } - else { - logger.log(logger.LogLevel.DEBUG,status_line+"\n"); - } - } + var message = @"$q_id $command\n"; + conn.output_stream.write(message.data); +// logger.log(logger.LogLevel.DEBUG,message); - /* list dir */ - public string list(string what) throws GLib.Error, IMAPError { - var ret = ""; - var q_id = next_q_id(); + /* Check response */ + var status_line = response.read_line(null); + if (status_line.contains(q_id + " NO") || status_line.contains("BAD")) + throw new IMAPError.LIST(status_line); - var message = @"$q_id LIST \"\" \"$what\"\n"; - conn.output_stream.write(message.data); - logger.log(logger.LogLevel.DEBUG,message); + ret = status_line+"\n"; - /* Check response */ - var status_line = response.read_line(null); - if (status_line.contains(q_id + " NO") || status_line.contains("BAD")) - throw new IMAPError.LIST(status_line); + /* The command has a on line return value - we must return here! */ + if (ret.contains(q_id + " OK")) + return ret; - ret = status_line+"\n"; + //FIXME: NO responses? + while (!(status_line = response.read_line(null)).contains(q_id + " OK")) { + ret = ret + status_line+"\n"; + } - //FIXME: NO responses? - while (!(status_line = response.read_line(null)).contains(q_id + " OK")) { - ret = ret + status_line+"\n"; - } +// logger.log(logger.LogLevel.DEBUG,ret); - logger.log(logger.LogLevel.DEBUG,ret); + return ret; + } + + /* login into account throw imap error LOGIN */ + public void login() throws GLib.Error, IMAPError { + imap_command(@"LOGIN $username $password"); + } - return ret; + /* list dir */ + public string list(string what) throws GLib.Error, IMAPError { + return imap_command(@"LIST \"\" \"$what\""); } /* examine dir */ public string examine(string what) throws GLib.Error, IMAPError { - var ret = ""; - var q_id = next_q_id(); - - var message = @"$q_id EXAMINE $what\n"; - conn.output_stream.write(message.data); - logger.log(logger.LogLevel.DEBUG,message); - - /* Check response */ - var status_line = response.read_line(null); - if (status_line.contains(q_id + " NO") || status_line.contains("BAD")) - throw new IMAPError.LIST(status_line); - - ret = status_line+"\n"; - - //FIXME: NO responses? - while (!(status_line = response.read_line(null)).contains(q_id + " OK")) { - ret = ret + status_line+"\n"; - } - - logger.log(logger.LogLevel.DEBUG,ret); - - return ret; + return imap_command(@"EXAMINE $what"); } /* fetch foo as string */ public string fetch(string start_uid,string end_uid,string what) throws GLib.Error, IMAPError { - var ret = ""; - var q_id = next_q_id(); - - var message = @"$q_id FETCH $start_uid:$end_uid $what\n"; - conn.output_stream.write(message.data); - logger.log(logger.LogLevel.DEBUG,message); - - /* Check response */ - var status_line = response.read_line(null); - if (status_line.contains(q_id + " NO") || status_line.contains("BAD")) - throw new IMAPError.LIST(status_line); - - ret = status_line+"\n"; - - //FIXME: NO responses? - while (!(status_line = response.read_line(null)).contains(q_id + " OK")) { - ret = ret + status_line+"\n"; - } - - logger.log(logger.LogLevel.DEBUG,ret); - - return ret; + return imap_command(@"FETCH $start_uid:$end_uid $what"); } /* get thread */ public string thread(string algorithm,string char_set,string what) throws GLib.Error, IMAPError { - var ret = ""; - var q_id = next_q_id(); - - var message = @"$q_id THREAD $algorithm $char_set $what\n"; - conn.output_stream.write(message.data); - logger.log(logger.LogLevel.DEBUG,message); - - /* Check response */ - var status_line = response.read_line(null); - if (status_line.contains(q_id + " NO") || status_line.contains("BAD")) - throw new IMAPError.LIST(status_line); - - ret = status_line+"\n"; + return imap_command(@"THREAD $algorithm $char_set $what"); + } - //FIXME: NO responses? - while (!(status_line = response.read_line(null)).contains(q_id + " OK")) { - ret = ret + status_line+"\n"; + /* get headers as hash map */ + public HashMap get_headers(string start_uid,string end_uid) { + var regex_to = new Regex ("""^To:*"""); + var regex_from = new Regex ("""^From:*"""); + var regex_subject = new Regex ("""^Subject:*"""); + var regex_date = new Regex ("""^Date:*"""); + var regex_uid = new Regex ("""HEADER"""); + + //FIXME: this code may take very long to complete!!! + var map = new HashMap (); + string all = imap_command(@"FETCH $start_uid:$end_uid RFC822.HEADER"); + var list = all.split("\n)"); + + foreach (string s in list) { + int uid= 0; + Message tmp = Message(); + var lines = s.split("\n"); + foreach (string line in lines ){ + if (regex_uid.match(line)) { + var parts = line.split(" "); + uid=parts[1].to_int(); + } + else if (regex_to.match(line)) + tmp.to = line.replace("To: ","").strip(); + else if (regex_from.match(line)) + tmp.from = (string)line.replace("From: ","").strip(); + else if (regex_subject.match(line)) + tmp.subject = (string)line.replace("Subject: ","").strip(); + else if (regex_date.match(line)) + tmp.date = line.replace("Date: ","").strip(); + } + + /* only push the entry if we found a header in the string */ + if(uid != 0) { + tmp.full_header = s; + map.set (uid,tmp); + } } - logger.log(logger.LogLevel.DEBUG,ret); - - return ret; + return map; } - - } } diff --git a/src/main.vala b/src/main.vala index bb90828..4f21aa5 100644 --- a/src/main.vala +++ b/src/main.vala @@ -16,8 +16,16 @@ void main () { imap.login(); imap.list("*"); imap.examine("INBOX"); - imap.fetch("1","2","RFC822.HEADER"); - imap.thread("REFERENCES", "UTF-8", "ALL"); + //imap.fetch("1","2","RFC822.HEADER"); + //imap.thread("REFERENCES", "UTF-8", "ALL"); +//imap.get_headers("1","2"); + var msgs = imap.get_headers("1","1000"); + Message msg; + for (int i = 1; i <= 1000; i++) { + msg = msgs.get(i); + print(@"$(msg.date) \t$(msg.from) \t $(msg.subject)\n"); + } + } catch (Error e) { stderr.printf ("%s\n", e.message); } -- 2.11.4.GIT