masqmail
diff src/message.c @ 0:08114f7dcc23
this is masqmail-0.2.21 from oliver kurth
author | meillo@marmaro.de |
---|---|
date | Fri, 26 Sep 2008 17:05:23 +0200 |
parents | |
children | 26e34ae9a3e3 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/message.c Fri Sep 26 17:05:23 2008 +0200 1.3 @@ -0,0 +1,210 @@ 1.4 +/* MasqMail 1.5 + Copyright (C) 1999-2001 Oliver Kurth 1.6 + 1.7 + This program is free software; you can redistribute it and/or modify 1.8 + it under the terms of the GNU General Public License as published by 1.9 + the Free Software Foundation; either version 2 of the License, or 1.10 + (at your option) any later version. 1.11 + 1.12 + This program is distributed in the hope that it will be useful, 1.13 + but WITHOUT ANY WARRANTY; without even the implied warranty of 1.14 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.15 + GNU General Public License for more details. 1.16 + 1.17 + You should have received a copy of the GNU General Public License 1.18 + along with this program; if not, write to the Free Software 1.19 + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 1.20 +*/ 1.21 + 1.22 +#include "masqmail.h" 1.23 + 1.24 +message *create_message() 1.25 +{ 1.26 + message *msg = (message *)g_malloc(sizeof(message)); 1.27 + if(msg){ 1.28 + memset(msg, 0, sizeof(message)); 1.29 + msg->data_size = -1; 1.30 + } 1.31 + return msg; 1.32 +} 1.33 + 1.34 +gint msg_calc_size(message *msg, gboolean is_smtp) 1.35 +{ 1.36 + GList *node; 1.37 + gint l_cnt = 0, c_cnt = 0; 1.38 + 1.39 + /* header size */ 1.40 + if(msg->hdr_list){ 1.41 + for(node = g_list_first(msg->hdr_list); node; node = g_list_next(node)){ 1.42 + if(node->data){ 1.43 + header *hdr = (header *)(node->data); 1.44 + if(hdr->header){ 1.45 + char *p = hdr->header; 1.46 + while(*p){ 1.47 + if(*p++ == '\n') l_cnt++; 1.48 + c_cnt++; 1.49 + } 1.50 + } 1.51 + } 1.52 + } 1.53 + } 1.54 + 1.55 + /* empty line separating headers from data: */ 1.56 + c_cnt++; 1.57 + l_cnt++; 1.58 + 1.59 + /* data size */ 1.60 + if(msg->data_list){ 1.61 + for(node = g_list_first(msg->data_list); node; node = g_list_next(node)){ 1.62 + if(node->data){ 1.63 + char *p = node->data; 1.64 + while(*p){ 1.65 + if(*p++ == '\n') l_cnt++; 1.66 + c_cnt++; 1.67 + } 1.68 + } 1.69 + } 1.70 + } 1.71 + 1.72 + return is_smtp ? c_cnt + l_cnt : c_cnt; 1.73 +} 1.74 + 1.75 +void msg_free_data(message *msg) 1.76 +{ 1.77 + GList *node; 1.78 + 1.79 + if(msg->data_list){ 1.80 + for(node = g_list_first(msg->data_list); node; node = g_list_next(node)){ 1.81 + if(node->data) 1.82 + g_free(node->data); 1.83 + } 1.84 + g_list_free(msg->data_list); 1.85 + msg->data_list = NULL; 1.86 + } 1.87 +} 1.88 + 1.89 +void destroy_message(message *msg) 1.90 +{ 1.91 + GList *node; 1.92 + 1.93 + if(msg->uid) g_free(msg->uid); 1.94 + if(msg->ident) g_free(msg->ident); 1.95 + if(msg->return_path) g_free(msg->return_path); 1.96 + 1.97 + if(msg->rcpt_list){ 1.98 + for(node = g_list_first(msg->rcpt_list); node; node = g_list_next(node)){ 1.99 + if(node->data) 1.100 + g_free(node->data); 1.101 + } 1.102 + g_list_free(msg->rcpt_list); 1.103 + } 1.104 + if(msg->hdr_list){ 1.105 + for(node = g_list_first(msg->hdr_list); node; node = g_list_next(node)){ 1.106 + if(node->data){ 1.107 + header *hdr = (header *)(node->data); 1.108 + if(hdr->header) 1.109 + g_free(hdr->header); 1.110 + g_free(node->data); 1.111 + } 1.112 + } 1.113 + g_list_free(msg->hdr_list); 1.114 + } 1.115 + 1.116 + if(msg->full_sender_name) 1.117 + g_free(msg->full_sender_name); 1.118 + 1.119 + msg_free_data(msg); 1.120 + 1.121 + g_free(msg); 1.122 +} 1.123 + 1.124 +void destroy_msg_list(GList *msg_list) 1.125 +{ 1.126 + GList *msg_node; 1.127 + 1.128 + foreach(msg_list, msg_node){ 1.129 + message *msg = (message *)(msg_node->data); 1.130 + destroy_message(msg); 1.131 + } 1.132 + g_list_free(msg_list); 1.133 +} 1.134 + 1.135 +msg_out *create_msg_out(message *msg) 1.136 +{ 1.137 + msg_out *msgout = NULL; 1.138 + 1.139 + msgout = g_malloc(sizeof(msg_out)); 1.140 + if(msgout){ 1.141 + msgout->msg = msg; 1.142 + msgout->return_path = NULL; 1.143 + msgout->rcpt_list = NULL; 1.144 + 1.145 + msgout->hdr_list = NULL; 1.146 + msgout->xtra_hdr_list = NULL; 1.147 + } 1.148 + return msgout; 1.149 +} 1.150 + 1.151 +msg_out *clone_msg_out(msg_out *msgout_orig) 1.152 +{ 1.153 + if(msgout_orig){ 1.154 + msg_out *msgout = create_msg_out(msgout_orig->msg); 1.155 + if(msgout){ 1.156 + msgout->msg = msgout_orig->msg; 1.157 + if(msgout_orig->return_path) 1.158 + msgout->return_path = copy_address(msgout_orig->return_path); 1.159 + if(msgout_orig->hdr_list) 1.160 + msgout->hdr_list = g_list_copy(msgout_orig->hdr_list); 1.161 + /* FIXME: if this lives longer than the original 1.162 + and we access one of the xtra hdrs, we will segfault 1.163 + or cause some weird bugs: */ 1.164 + msgout->xtra_hdr_list = NULL; 1.165 + if(msgout_orig->rcpt_list) 1.166 + msgout->rcpt_list = g_list_copy(msgout_orig->rcpt_list); 1.167 + } 1.168 + return msgout; 1.169 + } 1.170 + return NULL; 1.171 +} 1.172 + 1.173 +GList *create_msg_out_list(GList *msg_list) 1.174 +{ 1.175 + GList *msgout_list = NULL; 1.176 + GList *msg_node; 1.177 + 1.178 + foreach(msg_list, msg_node){ 1.179 + message *msg = (message *)(msg_node->data); 1.180 + msgout_list = g_list_append(msgout_list, create_msg_out(msg)); 1.181 + } 1.182 + return msgout_list; 1.183 +} 1.184 + 1.185 +void destroy_msg_out(msg_out *msgout) 1.186 +{ 1.187 + if(msgout){ 1.188 + if(msgout->return_path) 1.189 + destroy_address(msgout->return_path); 1.190 + if(msgout->hdr_list) 1.191 + g_list_free(msgout->hdr_list); 1.192 + if(msgout->xtra_hdr_list){ 1.193 + GList *hdr_node; 1.194 + foreach(msgout->xtra_hdr_list, hdr_node){ 1.195 + header *hdr = (header *)(hdr_node->data); 1.196 + destroy_header(hdr); 1.197 + } 1.198 + g_list_free(msgout->xtra_hdr_list); 1.199 + } 1.200 + g_free(msgout); 1.201 + } 1.202 +} 1.203 + 1.204 +void destroy_msg_out_list(GList *msgout_list) 1.205 +{ 1.206 + GList *msgout_node; 1.207 + 1.208 + foreach(msgout_list, msgout_node){ 1.209 + msg_out *msgout = (msg_out *)(msgout_node->data); 1.210 + destroy_msg_out(msgout); 1.211 + } 1.212 + g_list_free(msgout_list); 1.213 +}