masqmail

annotate src/message.c @ 394:c8e3d1a79313

Refactoring in conf.c: read_rval().
author markus schnalke <meillo@marmaro.de>
date Sat, 18 Feb 2012 19:32:40 +0100
parents 41958685480d
children
rev   line source
meillo@367 1 /*
meillo@367 2 ** MasqMail
meillo@367 3 ** Copyright (C) 1999-2001 Oliver Kurth
meillo@367 4 ** Copyright (C) 2010 markus schnalke <meillo@marmaro.de>
meillo@367 5 **
meillo@367 6 ** This program is free software; you can redistribute it and/or modify
meillo@367 7 ** it under the terms of the GNU General Public License as published by
meillo@367 8 ** the Free Software Foundation; either version 2 of the License, or
meillo@367 9 ** (at your option) any later version.
meillo@367 10 **
meillo@367 11 ** This program is distributed in the hope that it will be useful,
meillo@367 12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
meillo@367 13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
meillo@367 14 ** GNU General Public License for more details.
meillo@367 15 **
meillo@367 16 ** You should have received a copy of the GNU General Public License
meillo@367 17 ** along with this program; if not, write to the Free Software
meillo@367 18 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
meillo@0 19 */
meillo@0 20
meillo@0 21 #include "masqmail.h"
meillo@0 22
meillo@10 23 message*
meillo@10 24 create_message()
meillo@0 25 {
meillo@10 26 message *msg = (message *) g_malloc(sizeof(message));
meillo@10 27 if (msg) {
meillo@10 28 memset(msg, 0, sizeof(message));
meillo@10 29 msg->data_size = -1;
meillo@10 30 }
meillo@10 31 return msg;
meillo@0 32 }
meillo@0 33
meillo@118 34 /*
meillo@367 35 ** This function is currently (0.2.24) only used for client side SMTP
meillo@367 36 ** SIZE support (RFC 1870). The flag is_smtp is always true therefore.
meillo@367 37 **
meillo@367 38 ** Their definition of the message size in RFC 1870 is:
meillo@367 39 **
meillo@367 40 ** The message size is defined as the number of octets, including
meillo@367 41 ** CR-LF pairs, but not the SMTP DATA command's terminating dot
meillo@367 42 ** or doubled quoting dots, to be transmitted by the SMTP client
meillo@367 43 ** after receiving reply code 354 to the DATA command.
meillo@367 44 **
meillo@367 45 ** l_cnt (line count) covers '\r' characters which are not present in
meillo@367 46 ** masqmail's internal format. Dots are also not stuffed in the
meillo@367 47 ** internal format. Dot-stuffing is ignored in the size.
meillo@118 48 */
meillo@10 49 gint
meillo@366 50 msg_calc_size(message *msg, gboolean is_smtp)
meillo@0 51 {
meillo@10 52 GList *node;
meillo@118 53 gint l_cnt = 0; /* line count (we need to add so many '\r' for SMTP) */
meillo@118 54 gint c_cnt = 0; /* character count */
meillo@0 55
meillo@118 56 /* message header size */
meillo@10 57 if (msg->hdr_list) {
meillo@10 58 for (node = g_list_first(msg->hdr_list); node; node = g_list_next(node)) {
meillo@10 59 if (node->data) {
meillo@10 60 header *hdr = (header *) (node->data);
meillo@10 61 if (hdr->header) {
meillo@10 62 char *p = hdr->header;
meillo@10 63 while (*p) {
meillo@10 64 if (*p++ == '\n')
meillo@10 65 l_cnt++;
meillo@10 66 c_cnt++;
meillo@10 67 }
meillo@10 68 }
meillo@10 69 }
meillo@10 70 }
meillo@0 71 }
meillo@0 72
meillo@10 73 /* empty line separating headers from data: */
meillo@10 74 c_cnt++;
meillo@10 75 l_cnt++;
meillo@0 76
meillo@118 77 /* message data size */
meillo@10 78 if (msg->data_list) {
meillo@10 79 for (node = g_list_first(msg->data_list); node; node = g_list_next(node)) {
meillo@10 80 if (node->data) {
meillo@10 81 char *p = node->data;
meillo@10 82 while (*p) {
meillo@10 83 if (*p++ == '\n')
meillo@10 84 l_cnt++;
meillo@10 85 c_cnt++;
meillo@10 86 }
meillo@10 87 }
meillo@10 88 }
meillo@0 89 }
meillo@0 90
meillo@10 91 return is_smtp ? c_cnt + l_cnt : c_cnt;
meillo@0 92 }
meillo@0 93
meillo@10 94 void
meillo@366 95 msg_free_data(message *msg)
meillo@0 96 {
meillo@10 97 GList *node;
meillo@0 98
meillo@10 99 if (msg->data_list) {
meillo@10 100 for (node = g_list_first(msg->data_list); node; node = g_list_next(node)) {
meillo@10 101 if (node->data)
meillo@10 102 g_free(node->data);
meillo@10 103 }
meillo@10 104 g_list_free(msg->data_list);
meillo@10 105 msg->data_list = NULL;
meillo@10 106 }
meillo@0 107 }
meillo@0 108
meillo@10 109 void
meillo@366 110 destroy_message(message *msg)
meillo@0 111 {
meillo@10 112 GList *node;
meillo@0 113
meillo@81 114 if (!msg) {
meillo@81 115 return;
meillo@81 116 }
meillo@81 117
meillo@10 118 if (msg->uid)
meillo@10 119 g_free(msg->uid);
meillo@10 120 if (msg->ident)
meillo@10 121 g_free(msg->ident);
meillo@10 122 if (msg->return_path)
meillo@10 123 g_free(msg->return_path);
meillo@0 124
meillo@10 125 if (msg->rcpt_list) {
meillo@10 126 for (node = g_list_first(msg->rcpt_list); node; node = g_list_next(node)) {
meillo@10 127 if (node->data)
meillo@10 128 g_free(node->data);
meillo@10 129 }
meillo@10 130 g_list_free(msg->rcpt_list);
meillo@10 131 }
meillo@10 132 if (msg->hdr_list) {
meillo@10 133 for (node = g_list_first(msg->hdr_list); node; node = g_list_next(node)) {
meillo@10 134 if (node->data) {
meillo@10 135 header *hdr = (header *) (node->data);
meillo@10 136 if (hdr->header)
meillo@10 137 g_free(hdr->header);
meillo@10 138 g_free(node->data);
meillo@10 139 }
meillo@10 140 }
meillo@10 141 g_list_free(msg->hdr_list);
meillo@10 142 }
meillo@0 143
meillo@10 144 if (msg->full_sender_name)
meillo@10 145 g_free(msg->full_sender_name);
meillo@0 146
meillo@10 147 msg_free_data(msg);
meillo@0 148
meillo@10 149 g_free(msg);
meillo@0 150 }
meillo@0 151
meillo@10 152 void
meillo@366 153 destroy_msg_list(GList *msg_list)
meillo@0 154 {
meillo@10 155 GList *msg_node;
meillo@0 156
meillo@10 157 foreach(msg_list, msg_node) {
meillo@10 158 message *msg = (message *) (msg_node->data);
meillo@10 159 destroy_message(msg);
meillo@10 160 }
meillo@10 161 g_list_free(msg_list);
meillo@0 162 }
meillo@0 163
meillo@10 164 msg_out*
meillo@366 165 create_msg_out(message *msg)
meillo@0 166 {
meillo@351 167 msg_out *msgout = g_malloc0(sizeof(msg_out));
meillo@351 168 msgout->msg = msg;
meillo@10 169 return msgout;
meillo@0 170 }
meillo@0 171
meillo@10 172 msg_out*
meillo@366 173 clone_msg_out(msg_out *msgout_orig)
meillo@0 174 {
meillo@10 175 if (msgout_orig) {
meillo@10 176 msg_out *msgout = create_msg_out(msgout_orig->msg);
meillo@10 177 if (msgout) {
meillo@10 178 msgout->msg = msgout_orig->msg;
meillo@10 179 if (msgout_orig->return_path)
meillo@10 180 msgout->return_path = copy_address(msgout_orig->return_path);
meillo@10 181 if (msgout_orig->hdr_list)
meillo@10 182 msgout->hdr_list = g_list_copy(msgout_orig->hdr_list);
meillo@10 183 /* FIXME: if this lives longer than the original
meillo@10 184 and we access one of the xtra hdrs, we will segfault
meillo@10 185 or cause some weird bugs: */
meillo@10 186 msgout->xtra_hdr_list = NULL;
meillo@10 187 if (msgout_orig->rcpt_list)
meillo@10 188 msgout->rcpt_list = g_list_copy(msgout_orig->rcpt_list);
meillo@10 189 }
meillo@10 190 return msgout;
meillo@10 191 }
meillo@10 192 return NULL;
meillo@0 193 }
meillo@0 194
meillo@10 195 void
meillo@366 196 destroy_msg_out(msg_out *msgout)
meillo@0 197 {
meillo@10 198 if (msgout) {
meillo@10 199 if (msgout->return_path)
meillo@10 200 destroy_address(msgout->return_path);
meillo@10 201 if (msgout->hdr_list)
meillo@10 202 g_list_free(msgout->hdr_list);
meillo@10 203 if (msgout->xtra_hdr_list) {
meillo@10 204 GList *hdr_node;
meillo@10 205 foreach(msgout->xtra_hdr_list, hdr_node) {
meillo@10 206 header *hdr = (header *) (hdr_node->data);
meillo@10 207 destroy_header(hdr);
meillo@10 208 }
meillo@10 209 g_list_free(msgout->xtra_hdr_list);
meillo@10 210 }
meillo@10 211 g_free(msgout);
meillo@10 212 }
meillo@0 213 }
meillo@0 214
meillo@10 215 void
meillo@366 216 destroy_msg_out_list(GList *msgout_list)
meillo@0 217 {
meillo@10 218 GList *msgout_node;
meillo@0 219
meillo@10 220 foreach(msgout_list, msgout_node) {
meillo@10 221 msg_out *msgout = (msg_out *) (msgout_node->data);
meillo@10 222 destroy_msg_out(msgout);
meillo@10 223 }
meillo@10 224 g_list_free(msgout_list);
meillo@0 225 }