From 2a800b10bc1ff30dd9d655802daf33f0af3aae70 Mon Sep 17 00:00:00 2001 From: Anicka Bernathova Date: Sat, 30 Aug 2008 17:53:15 +0200 Subject: [PATCH] let us start debugging --- brum.c | 6 ++- brum.h | 1 + ham.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- int.c | 6 +++ lock.c | 2 +- 5 files changed, 126 insertions(+), 10 deletions(-) diff --git a/brum.c b/brum.c index d72fa6a..facd0f9 100644 --- a/brum.c +++ b/brum.c @@ -11,6 +11,8 @@ main(int argc, char** argv) save_gids(); read_conf(argv[1]); + //FIXME: + get_default_mailbox("/var/mail/anicka"); // yydebug=1; res = yyparse (); @@ -18,7 +20,7 @@ main(int argc, char** argv) if (res) return res; - print_tree(input_tree,0); +// print_tree(input_tree,0); var_hash = new_var_hash(); @@ -29,7 +31,7 @@ main(int argc, char** argv) save_current_headers(var_hash); interp(input_tree, var_hash); - print_vars(var_hash); +// print_vars(var_hash); return 0; } diff --git a/brum.h b/brum.h index 9942a92..c3931b2 100644 --- a/brum.h +++ b/brum.h @@ -104,6 +104,7 @@ struct action { }; struct list* var_hash; +char* default_mailbox; void print_tree(struct tree* t, int ind); void interp(struct tree* t, struct list* hash); diff --git a/ham.c b/ham.c index 16d7b36..ddc8e3c 100644 --- a/ham.c +++ b/ham.c @@ -2,12 +2,18 @@ #include #include +#include + #include "brum.h" #define BUFSIZE 1024 int curbufsize; +char mbox_write_buf[BUFSIZE]; +int mbox_write_pos; +int mbox_write_err; + static void new_header(char* buf, struct list* h) { @@ -96,15 +102,116 @@ print_headers(struct list* l) printf("%s:%s",p->name,p->value); } -void -do_action(struct action* a) +static void +write_char_to_mailbox(char c, int fd) +{ + int res; + + if (mbox_write_err) + return; + + if (!c){ + if (!mbox_write_pos) + return; + res = write(fd, mbox_write_buf, mbox_write_pos); + if (res < 0) + mbox_write_err++; + mbox_write_pos = 0; + return; + } + if (mbox_write_pos >= BUFSIZE){ + res = write(fd, mbox_write_buf, BUFSIZE); + if (res < 0) + mbox_write_err++; + mbox_write_pos = 0; + return; + } + mbox_write_buf[mbox_write_pos++] = c; +} + +/* try to copy e-mail to mailbox, if it fails, + truncate mailbox to the original size */ +static int +copy_email(int fd, struct email* email) { - //just deliver e-mail, do not look at left side now -/* if (! a->r ){ + off_t mb_end; + + mb_end = lseek(fd, 0, SEEK_END); + if (mb_end < 1) + return -1; + + /* init globals */ + mbox_write_err = 0; + mbox_write_pos = 0; + + /* headers */ + struct hlist* ph; + char* pc; + LIST_FOREACH(ph, email->headers){ + for (pc = ph->name; *pc; pc++) + write_char_to_mailbox(*pc, fd); + write_char_to_mailbox(':', fd); + write_char_to_mailbox(' ', fd); + for (pc = ph->value; *pc; pc++) + write_char_to_mailbox(*pc, fd); + write_char_to_mailbox('\n', fd); + } - } else if (!strcmp(a->r,"pipe")){ - 1; + write_char_to_mailbox('\n', fd); + /* body */ + //FIXME that nasty FROM + for (pc = email->body; *pc; pc++){ + write_char_to_mailbox(*pc, fd); } -*/ + /* flush the buffer */ + write_char_to_mailbox(0, fd); + + /* errors? */ + if (mbox_write_err) + return -1; + + return 0; +} + +static int +deliver_local_email(char* folder, struct email* email) +{ + int res = -1; + int is_default_mailbox = 0; + int fd; + + if (!strcmp(default_mailbox, folder)) + is_default_mailbox = 1; + + fd = open_mailbox(folder, is_default_mailbox); + if (fd < 0){ + if (is_default_mailbox) + return res; + else /* try to save to default mailbox instead */ + return deliver_local_email(default_mailbox, email); + } + + res = copy_email(fd, email); + if (res < 0){ + + /* try to deliver to the default mailbox */ + if (is_default_mailbox) + return res; + else + return deliver_local_email(default_mailbox, email); + } + + close_mailbox(fd, folder, is_default_mailbox); + + return res; +} + +void +do_action(struct action* a) +{ + /* -> address */ + if (! a->r && !a->l ){ + deliver_local_email(a->s, &a->e); + } } diff --git a/int.c b/int.c index 795b843..13a3077 100644 --- a/int.c +++ b/int.c @@ -386,3 +386,9 @@ save_current_headers(struct list* hash) } } + +void +get_default_mailbox(char* mb) +{ + default_mailbox = mb; +} diff --git a/lock.c b/lock.c index 571de8d..d6d1155 100644 --- a/lock.c +++ b/lock.c @@ -11,7 +11,7 @@ #include "brum.h" -#define LOCK_MAX_TRIES 30 +#define LOCK_MAX_TRIES 3 gid_t rgid, egid, sgid; void -- 2.39.5