# HG changeset patch # User meillo@marmaro.de # Date 1276943708 -7200 # Node ID 83a182793503ab42833238287df981837ded673d # Parent 71ce3a1568e957f640f15eccb5055090bd990740 refactoring diff -r 71ce3a1568e9 -r 83a182793503 src/queue.c --- a/src/queue.c Sat Jun 19 11:14:34 2010 +0200 +++ b/src/queue.c Sat Jun 19 12:35:08 2010 +0200 @@ -140,48 +140,50 @@ msg_list = read_queue(FALSE); - if (msg_list != NULL) { - foreach(msg_list, msg_node) { - message *msg = (message *) (msg_node->data); - GList *rcpt_node; - gchar *size_str = NULL; - gchar *time_str = NULL; - gchar *host_str = NULL; - gchar *ident_str = NULL; + if (msg_list == NULL) { + printf("mail queue is empty.\n"); + return; + } - if (msg->data_size >= 0) - size_str = g_strdup_printf(" size=%d", msg->data_size); - if (msg->received_time > 0) { - gchar *tmp_str; - time_str = g_strdup_printf(" age=%s", tmp_str = format_difftime(difftime(time(NULL), msg->received_time))); - g_free(tmp_str); - } - if (msg->received_host != NULL) - host_str = g_strdup_printf(" host=%s", msg->received_host); - if (msg->ident != NULL) - ident_str = g_strdup_printf(" ident=%s", msg->ident); + foreach(msg_list, msg_node) { + message *msg = (message *) (msg_node->data); + GList *rcpt_node; + gchar *size_str = NULL; + gchar *time_str = NULL; + gchar *host_str = NULL; + gchar *ident_str = NULL; - printf("%s <= %s%s%s%s%s\n", msg->uid, addr_string(msg->return_path), size_str ? size_str : "", - time_str ? time_str : "", host_str ? host_str : "", ident_str ? ident_str : ""); + if (msg->data_size >= 0) + size_str = g_strdup_printf(" size=%d", msg->data_size); + if (msg->received_time > 0) { + gchar *tmp_str; + time_str = g_strdup_printf(" age=%s", tmp_str = format_difftime(difftime(time(NULL), msg->received_time))); + g_free(tmp_str); + } + if (msg->received_host != NULL) + host_str = g_strdup_printf(" host=%s", msg->received_host); + if (msg->ident != NULL) + ident_str = g_strdup_printf(" ident=%s", msg->ident); - if (size_str) - g_free(size_str); - if (time_str) - g_free(time_str); - if (host_str) - g_free(host_str); - if (ident_str) - g_free(ident_str); + printf("%s <= %s%s%s%s%s\n", msg->uid, addr_string(msg->return_path), size_str ? size_str : "", + time_str ? time_str : "", host_str ? host_str : "", ident_str ? ident_str : ""); - foreach(msg->rcpt_list, rcpt_node) { - address *rcpt = (address *) (rcpt_node->data); + if (size_str) + g_free(size_str); + if (time_str) + g_free(time_str); + if (host_str) + g_free(host_str); + if (ident_str) + g_free(ident_str); - printf(" %s %s\n", addr_is_delivered(rcpt) ? "=>" : (addr_is_failed(rcpt) ? "!=" : "=="), addr_string(rcpt)); - } - g_free(msg); + foreach(msg->rcpt_list, rcpt_node) { + address *rcpt = (address *) (rcpt_node->data); + + printf(" %s %s\n", addr_is_delivered(rcpt) ? "=>" : (addr_is_failed(rcpt) ? "!=" : "=="), addr_string(rcpt)); } - } else - printf("mail queue is empty.\n"); + g_free(msg); + } } gboolean @@ -193,35 +195,32 @@ gchar *dat_name = g_strdup_printf("%s/input/%s-D", conf.spool_dir, uid); struct stat stat_buf; - if (spool_lock(uid)) { - - if (stat(hdr_name, &stat_buf) == 0) { - if (unlink(hdr_name) != 0) { - fprintf(stderr, "could not unlink %s: %s\n", hdr_name, strerror(errno)); - hdr_ok = FALSE; - } - } else { - fprintf(stderr, "could not stat file %s: %s\n", hdr_name, strerror(errno)); - hdr_ok = FALSE; - } - if (stat(dat_name, &stat_buf) == 0) { - if (unlink(dat_name) != 0) { - fprintf(stderr, "could not unlink %s: %s\n", dat_name, strerror(errno)); - dat_ok = FALSE; - } - } else { - fprintf(stderr, "could not stat file %s: %s\n", dat_name, strerror(errno)); - dat_ok = FALSE; - } - printf("message %s deleted\n", uid); - - spool_unlock(uid); - - } else { - + if (!spool_lock(uid)) { fprintf(stderr, "message %s is locked.\n", uid); return FALSE; } + if (stat(hdr_name, &stat_buf) == 0) { + if (unlink(hdr_name) != 0) { + fprintf(stderr, "could not unlink %s: %s\n", hdr_name, strerror(errno)); + hdr_ok = FALSE; + } + } else { + fprintf(stderr, "could not stat file %s: %s\n", hdr_name, strerror(errno)); + hdr_ok = FALSE; + } + if (stat(dat_name, &stat_buf) == 0) { + if (unlink(dat_name) != 0) { + fprintf(stderr, "could not unlink %s: %s\n", dat_name, strerror(errno)); + dat_ok = FALSE; + } + } else { + fprintf(stderr, "could not stat file %s: %s\n", dat_name, strerror(errno)); + dat_ok = FALSE; + } + printf("message %s deleted\n", uid); + + spool_unlock(uid); + return (dat_ok && hdr_ok); } diff -r 71ce3a1568e9 -r 83a182793503 src/spool.c --- a/src/spool.c Sat Jun 19 11:14:34 2010 +0200 +++ b/src/spool.c Sat Jun 19 12:35:08 2010 +0200 @@ -91,106 +91,110 @@ spool_read_data(message * msg) { FILE *in; - gboolean ok = FALSE; gchar *spool_file; DEBUG(5) debugf("spool_read_data entered\n"); spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid); DEBUG(5) debugf("reading data spool file '%s'\n", spool_file); - if ((in = fopen(spool_file, "r"))) { - char buf[MAX_DATALINE]; - int len; + in = fopen(spool_file, "r"); + if (!in) { + logwrite(LOG_ALERT, "could not open spool data file %s: %s\n", spool_file, strerror(errno)); + return FALSE; + } - /* msg uid */ - read_line(in, buf, MAX_DATALINE); + char buf[MAX_DATALINE]; + int len; - /* data */ - msg->data_list = NULL; - while ((len = read_line(in, buf, MAX_DATALINE)) > 0) { - msg->data_list = g_list_prepend(msg->data_list, g_strdup(buf)); - } - msg->data_list = g_list_reverse(msg->data_list); - fclose(in); - ok = TRUE; - } else - logwrite(LOG_ALERT, "could not open spool data file %s: %s\n", spool_file, strerror(errno)); - return ok; + /* msg uid */ + read_line(in, buf, MAX_DATALINE); + + /* data */ + msg->data_list = NULL; + while ((len = read_line(in, buf, MAX_DATALINE)) > 0) { + msg->data_list = g_list_prepend(msg->data_list, g_strdup(buf)); + } + msg->data_list = g_list_reverse(msg->data_list); + fclose(in); + return TRUE; } gboolean spool_read_header(message * msg) { FILE *in; - gboolean ok = FALSE; gchar *spool_file; /* header spool: */ spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid); - if ((in = fopen(spool_file, "r"))) { - header *hdr = NULL; - char buf[MAX_DATALINE]; - int len; + in = fopen(spool_file, "r"); + if (!in) { + logwrite(LOG_ALERT, "could not open spool header file %s: %s\n", + spool_file, strerror(errno)); + return FALSE; + } - /* msg uid */ - read_line(in, buf, MAX_DATALINE); + header *hdr = NULL; + char buf[MAX_DATALINE]; + int len; - /* envelope header */ - while ((len = read_line(in, buf, MAX_DATALINE)) > 0) { - if (buf[0] == '\n') - break; - else if (strncasecmp(buf, "MF:", 3) == 0) { - msg->return_path = create_address(&(buf[3]), TRUE); - DEBUG(3) debugf("spool_read: MAIL FROM: %s", msg->return_path->address); - } else if (strncasecmp(buf, "RT:", 3) == 0) { - address *addr; - addr = spool_scan_rcpt(buf); - if (!addr_is_delivered(addr) && !addr_is_failed(addr)) { - msg->rcpt_list = g_list_append(msg->rcpt_list, addr); - } else { - msg->non_rcpt_list = g_list_append(msg->non_rcpt_list, addr); + /* msg uid */ + read_line(in, buf, MAX_DATALINE); + + /* envelope header */ + while ((len = read_line(in, buf, MAX_DATALINE)) > 0) { + if (buf[0] == '\n') { + break; + } else if (strncasecmp(buf, "MF:", 3) == 0) { + msg->return_path = create_address(&(buf[3]), TRUE); + DEBUG(3) debugf("spool_read: MAIL FROM: %s", msg->return_path->address); + } else if (strncasecmp(buf, "RT:", 3) == 0) { + address *addr; + addr = spool_scan_rcpt(buf); + if (!addr_is_delivered(addr) && !addr_is_failed(addr)) { + msg->rcpt_list = g_list_append(msg->rcpt_list, addr); + } else { + msg->non_rcpt_list = g_list_append(msg->non_rcpt_list, addr); + } + } else if (strncasecmp(buf, "PR:", 3) == 0) { + prot_id i; + for (i = 0; i < PROT_NUM; i++) { + if (strncasecmp(prot_names[i], &(buf[3]), strlen(prot_names[i])) == 0) { + break; } - } else if (strncasecmp(buf, "PR:", 3) == 0) { - prot_id i; - for (i = 0; i < PROT_NUM; i++) { - if (strncasecmp(prot_names[i], &(buf[3]), strlen(prot_names[i])) == 0) { - break; - } - } - msg->received_prot = i; - } else if (strncasecmp(buf, "RH:", 3) == 0) { - g_strchomp(buf); - msg->received_host = g_strdup(&(buf[3])); - } else if (strncasecmp(buf, "ID:", 3) == 0) { - g_strchomp(buf); - msg->ident = g_strdup(&(buf[3])); - } else if (strncasecmp(buf, "DS:", 3) == 0) { - msg->data_size = atoi(&(buf[3])); - } else if (strncasecmp(buf, "TR:", 3) == 0) { - msg->received_time = (time_t) (atoi(&(buf[3]))); - } else if (strncasecmp(buf, "TW:", 3) == 0) { - msg->warned_time = (time_t) (atoi(&(buf[3]))); } - /* so far ignore other tags */ + msg->received_prot = i; + } else if (strncasecmp(buf, "RH:", 3) == 0) { + g_strchomp(buf); + msg->received_host = g_strdup(&(buf[3])); + } else if (strncasecmp(buf, "ID:", 3) == 0) { + g_strchomp(buf); + msg->ident = g_strdup(&(buf[3])); + } else if (strncasecmp(buf, "DS:", 3) == 0) { + msg->data_size = atoi(&(buf[3])); + } else if (strncasecmp(buf, "TR:", 3) == 0) { + msg->received_time = (time_t) (atoi(&(buf[3]))); + } else if (strncasecmp(buf, "TW:", 3) == 0) { + msg->warned_time = (time_t) (atoi(&(buf[3]))); } + /* so far ignore other tags */ + } - /* mail headers */ - while ((len = read_line(in, buf, MAX_DATALINE)) > 0) { - if (strncasecmp(buf, "HD:", 3) == 0) { - hdr = get_header(&(buf[3])); - msg->hdr_list = g_list_append(msg->hdr_list, hdr); - } else if ((buf[0] == ' ' || buf[0] == '\t') && hdr) { - char *tmp = hdr->header; - /* header continuation */ - hdr->header = g_strconcat(hdr->header, buf, NULL); - hdr->value = hdr->header + (hdr->value - tmp); - } else - break; + /* mail headers */ + while ((len = read_line(in, buf, MAX_DATALINE)) > 0) { + if (strncasecmp(buf, "HD:", 3) == 0) { + hdr = get_header(&(buf[3])); + msg->hdr_list = g_list_append(msg->hdr_list, hdr); + } else if ((buf[0] == ' ' || buf[0] == '\t') && hdr) { + char *tmp = hdr->header; + /* header continuation */ + hdr->header = g_strconcat(hdr->header, buf, NULL); + hdr->value = hdr->header + (hdr->value - tmp); + } else { + break; } - fclose(in); - ok = TRUE; - } else - logwrite(LOG_ALERT, "could not open spool header file %s: %s\n", spool_file, strerror(errno)); - return ok; + } + fclose(in); + return TRUE; } message* @@ -308,39 +312,38 @@ /* header spool: */ ok = spool_write_header(msg); - if (ok) { + if (ok && do_write_data) { + /* data spool: */ + tmp_file = g_strdup_printf("%s/input/%d-D.tmp", conf.spool_dir, getpid()); + DEBUG(4) debugf("tmp_file = %s\n", tmp_file); - if (do_write_data) { - /* data spool: */ - tmp_file = g_strdup_printf("%s/input/%d-D.tmp", conf.spool_dir, getpid()); - DEBUG(4) debugf("tmp_file = %s\n", tmp_file); + if ((out = fopen(tmp_file, "w"))) { + fprintf(out, "%s\n", msg->uid); + for (list = g_list_first(msg->data_list); list != NULL; list = g_list_next(list)) { + fprintf(out, "%s", (gchar *) (list->data)); + } - if ((out = fopen(tmp_file, "w"))) { - fprintf(out, "%s\n", msg->uid); - for (list = g_list_first(msg->data_list); list != NULL; list = g_list_next(list)) { - fprintf(out, "%s", (gchar *) (list->data)); + /* possibly paranoid ;-) */ + if (fflush(out) == EOF) { + ok = FALSE; + } else if (fdatasync(fileno(out)) != 0) { + if (errno != EINVAL) { /* some fs do not support this.. I hope this also means that it is not necessary */ + ok = FALSE; } - - /* possibly paranoid ;-) */ - if (fflush(out) == EOF) - ok = FALSE; - else if (fdatasync(fileno(out)) != 0) { - if (errno != EINVAL) /* some fs do not support this.. I hope this also means that it is not necessary */ - ok = FALSE; - } - fclose(out); - if (ok) { - spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid); - DEBUG(4) debugf("spool_file = %s\n", spool_file); - ok = (rename(tmp_file, spool_file) != -1); - g_free(spool_file); - } - } else { - logwrite(LOG_ALERT, "could not open temporary data spool file: %s\n", strerror(errno)); - ok = FALSE; } - g_free(tmp_file); + fclose(out); + if (ok) { + spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid); + DEBUG(4) debugf("spool_file = %s\n", spool_file); + ok = (rename(tmp_file, spool_file) != -1); + g_free(spool_file); + } + } else { + logwrite(LOG_ALERT, "could not open temporary data spool file: %s\n", + strerror(errno)); + ok = FALSE; } + g_free(tmp_file); } /* set uid and gid back */ @@ -421,14 +424,16 @@ /* header spool: */ spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid); - if (unlink(spool_file) != 0) + if (unlink(spool_file) != 0) { logwrite(LOG_ALERT, "could not delete spool file %s: %s\n", spool_file, strerror(errno)); + } g_free(spool_file); /* data spool: */ spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid); - if (unlink(spool_file) != 0) + if (unlink(spool_file) != 0) { logwrite(LOG_ALERT, "could not delete spool file %s: %s\n", spool_file, strerror(errno)); + } g_free(spool_file); /* set uid and gid back */