masqmail

view src/message.c @ 8:7e01b9096bb6

removed tests from Makefiles
author meillo@marmaro.de
date Mon, 27 Oct 2008 16:20:41 +0100
parents
children 26e34ae9a3e3
line source
1 /* MasqMail
2 Copyright (C) 1999-2001 Oliver Kurth
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
19 #include "masqmail.h"
21 message *create_message()
22 {
23 message *msg = (message *)g_malloc(sizeof(message));
24 if(msg){
25 memset(msg, 0, sizeof(message));
26 msg->data_size = -1;
27 }
28 return msg;
29 }
31 gint msg_calc_size(message *msg, gboolean is_smtp)
32 {
33 GList *node;
34 gint l_cnt = 0, c_cnt = 0;
36 /* header size */
37 if(msg->hdr_list){
38 for(node = g_list_first(msg->hdr_list); node; node = g_list_next(node)){
39 if(node->data){
40 header *hdr = (header *)(node->data);
41 if(hdr->header){
42 char *p = hdr->header;
43 while(*p){
44 if(*p++ == '\n') l_cnt++;
45 c_cnt++;
46 }
47 }
48 }
49 }
50 }
52 /* empty line separating headers from data: */
53 c_cnt++;
54 l_cnt++;
56 /* data size */
57 if(msg->data_list){
58 for(node = g_list_first(msg->data_list); node; node = g_list_next(node)){
59 if(node->data){
60 char *p = node->data;
61 while(*p){
62 if(*p++ == '\n') l_cnt++;
63 c_cnt++;
64 }
65 }
66 }
67 }
69 return is_smtp ? c_cnt + l_cnt : c_cnt;
70 }
72 void msg_free_data(message *msg)
73 {
74 GList *node;
76 if(msg->data_list){
77 for(node = g_list_first(msg->data_list); node; node = g_list_next(node)){
78 if(node->data)
79 g_free(node->data);
80 }
81 g_list_free(msg->data_list);
82 msg->data_list = NULL;
83 }
84 }
86 void destroy_message(message *msg)
87 {
88 GList *node;
90 if(msg->uid) g_free(msg->uid);
91 if(msg->ident) g_free(msg->ident);
92 if(msg->return_path) g_free(msg->return_path);
94 if(msg->rcpt_list){
95 for(node = g_list_first(msg->rcpt_list); node; node = g_list_next(node)){
96 if(node->data)
97 g_free(node->data);
98 }
99 g_list_free(msg->rcpt_list);
100 }
101 if(msg->hdr_list){
102 for(node = g_list_first(msg->hdr_list); node; node = g_list_next(node)){
103 if(node->data){
104 header *hdr = (header *)(node->data);
105 if(hdr->header)
106 g_free(hdr->header);
107 g_free(node->data);
108 }
109 }
110 g_list_free(msg->hdr_list);
111 }
113 if(msg->full_sender_name)
114 g_free(msg->full_sender_name);
116 msg_free_data(msg);
118 g_free(msg);
119 }
121 void destroy_msg_list(GList *msg_list)
122 {
123 GList *msg_node;
125 foreach(msg_list, msg_node){
126 message *msg = (message *)(msg_node->data);
127 destroy_message(msg);
128 }
129 g_list_free(msg_list);
130 }
132 msg_out *create_msg_out(message *msg)
133 {
134 msg_out *msgout = NULL;
136 msgout = g_malloc(sizeof(msg_out));
137 if(msgout){
138 msgout->msg = msg;
139 msgout->return_path = NULL;
140 msgout->rcpt_list = NULL;
142 msgout->hdr_list = NULL;
143 msgout->xtra_hdr_list = NULL;
144 }
145 return msgout;
146 }
148 msg_out *clone_msg_out(msg_out *msgout_orig)
149 {
150 if(msgout_orig){
151 msg_out *msgout = create_msg_out(msgout_orig->msg);
152 if(msgout){
153 msgout->msg = msgout_orig->msg;
154 if(msgout_orig->return_path)
155 msgout->return_path = copy_address(msgout_orig->return_path);
156 if(msgout_orig->hdr_list)
157 msgout->hdr_list = g_list_copy(msgout_orig->hdr_list);
158 /* FIXME: if this lives longer than the original
159 and we access one of the xtra hdrs, we will segfault
160 or cause some weird bugs: */
161 msgout->xtra_hdr_list = NULL;
162 if(msgout_orig->rcpt_list)
163 msgout->rcpt_list = g_list_copy(msgout_orig->rcpt_list);
164 }
165 return msgout;
166 }
167 return NULL;
168 }
170 GList *create_msg_out_list(GList *msg_list)
171 {
172 GList *msgout_list = NULL;
173 GList *msg_node;
175 foreach(msg_list, msg_node){
176 message *msg = (message *)(msg_node->data);
177 msgout_list = g_list_append(msgout_list, create_msg_out(msg));
178 }
179 return msgout_list;
180 }
182 void destroy_msg_out(msg_out *msgout)
183 {
184 if(msgout){
185 if(msgout->return_path)
186 destroy_address(msgout->return_path);
187 if(msgout->hdr_list)
188 g_list_free(msgout->hdr_list);
189 if(msgout->xtra_hdr_list){
190 GList *hdr_node;
191 foreach(msgout->xtra_hdr_list, hdr_node){
192 header *hdr = (header *)(hdr_node->data);
193 destroy_header(hdr);
194 }
195 g_list_free(msgout->xtra_hdr_list);
196 }
197 g_free(msgout);
198 }
199 }
201 void destroy_msg_out_list(GList *msgout_list)
202 {
203 GList *msgout_node;
205 foreach(msgout_list, msgout_node){
206 msg_out *msgout = (msg_out *)(msgout_node->data);
207 destroy_msg_out(msgout);
208 }
209 g_list_free(msgout_list);
210 }