masqmail

view src/message.c @ 81:71ce3a1568e9

moved check for NULL into destroy_message()
author meillo@marmaro.de
date Sat, 19 Jun 2010 11:14:34 +0200
parents 26e34ae9a3e3
children 7f1f364c2a29
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*
22 create_message()
23 {
24 message *msg = (message *) g_malloc(sizeof(message));
25 if (msg) {
26 memset(msg, 0, sizeof(message));
27 msg->data_size = -1;
28 }
29 return msg;
30 }
32 gint
33 msg_calc_size(message * msg, gboolean is_smtp)
34 {
35 GList *node;
36 gint l_cnt = 0, c_cnt = 0;
38 /* header size */
39 if (msg->hdr_list) {
40 for (node = g_list_first(msg->hdr_list); node; node = g_list_next(node)) {
41 if (node->data) {
42 header *hdr = (header *) (node->data);
43 if (hdr->header) {
44 char *p = hdr->header;
45 while (*p) {
46 if (*p++ == '\n')
47 l_cnt++;
48 c_cnt++;
49 }
50 }
51 }
52 }
53 }
55 /* empty line separating headers from data: */
56 c_cnt++;
57 l_cnt++;
59 /* data size */
60 if (msg->data_list) {
61 for (node = g_list_first(msg->data_list); node; node = g_list_next(node)) {
62 if (node->data) {
63 char *p = node->data;
64 while (*p) {
65 if (*p++ == '\n')
66 l_cnt++;
67 c_cnt++;
68 }
69 }
70 }
71 }
73 return is_smtp ? c_cnt + l_cnt : c_cnt;
74 }
76 void
77 msg_free_data(message * msg)
78 {
79 GList *node;
81 if (msg->data_list) {
82 for (node = g_list_first(msg->data_list); node; node = g_list_next(node)) {
83 if (node->data)
84 g_free(node->data);
85 }
86 g_list_free(msg->data_list);
87 msg->data_list = NULL;
88 }
89 }
91 void
92 destroy_message(message * msg)
93 {
94 GList *node;
96 if (!msg) {
97 return;
98 }
100 if (msg->uid)
101 g_free(msg->uid);
102 if (msg->ident)
103 g_free(msg->ident);
104 if (msg->return_path)
105 g_free(msg->return_path);
107 if (msg->rcpt_list) {
108 for (node = g_list_first(msg->rcpt_list); node; node = g_list_next(node)) {
109 if (node->data)
110 g_free(node->data);
111 }
112 g_list_free(msg->rcpt_list);
113 }
114 if (msg->hdr_list) {
115 for (node = g_list_first(msg->hdr_list); node; node = g_list_next(node)) {
116 if (node->data) {
117 header *hdr = (header *) (node->data);
118 if (hdr->header)
119 g_free(hdr->header);
120 g_free(node->data);
121 }
122 }
123 g_list_free(msg->hdr_list);
124 }
126 if (msg->full_sender_name)
127 g_free(msg->full_sender_name);
129 msg_free_data(msg);
131 g_free(msg);
132 }
134 void
135 destroy_msg_list(GList * msg_list)
136 {
137 GList *msg_node;
139 foreach(msg_list, msg_node) {
140 message *msg = (message *) (msg_node->data);
141 destroy_message(msg);
142 }
143 g_list_free(msg_list);
144 }
146 msg_out*
147 create_msg_out(message * msg)
148 {
149 msg_out *msgout = NULL;
151 msgout = g_malloc(sizeof(msg_out));
152 if (msgout) {
153 msgout->msg = msg;
154 msgout->return_path = NULL;
155 msgout->rcpt_list = NULL;
157 msgout->hdr_list = NULL;
158 msgout->xtra_hdr_list = NULL;
159 }
160 return msgout;
161 }
163 msg_out*
164 clone_msg_out(msg_out * msgout_orig)
165 {
166 if (msgout_orig) {
167 msg_out *msgout = create_msg_out(msgout_orig->msg);
168 if (msgout) {
169 msgout->msg = msgout_orig->msg;
170 if (msgout_orig->return_path)
171 msgout->return_path = copy_address(msgout_orig->return_path);
172 if (msgout_orig->hdr_list)
173 msgout->hdr_list = g_list_copy(msgout_orig->hdr_list);
174 /* FIXME: if this lives longer than the original
175 and we access one of the xtra hdrs, we will segfault
176 or cause some weird bugs: */
177 msgout->xtra_hdr_list = NULL;
178 if (msgout_orig->rcpt_list)
179 msgout->rcpt_list = g_list_copy(msgout_orig->rcpt_list);
180 }
181 return msgout;
182 }
183 return NULL;
184 }
186 GList*
187 create_msg_out_list(GList * msg_list)
188 {
189 GList *msgout_list = NULL;
190 GList *msg_node;
192 foreach(msg_list, msg_node) {
193 message *msg = (message *) (msg_node->data);
194 msgout_list = g_list_append(msgout_list, create_msg_out(msg));
195 }
196 return msgout_list;
197 }
199 void
200 destroy_msg_out(msg_out * msgout)
201 {
202 if (msgout) {
203 if (msgout->return_path)
204 destroy_address(msgout->return_path);
205 if (msgout->hdr_list)
206 g_list_free(msgout->hdr_list);
207 if (msgout->xtra_hdr_list) {
208 GList *hdr_node;
209 foreach(msgout->xtra_hdr_list, hdr_node) {
210 header *hdr = (header *) (hdr_node->data);
211 destroy_header(hdr);
212 }
213 g_list_free(msgout->xtra_hdr_list);
214 }
215 g_free(msgout);
216 }
217 }
219 void
220 destroy_msg_out_list(GList * msgout_list)
221 {
222 GList *msgout_node;
224 foreach(msgout_list, msgout_node) {
225 msg_out *msgout = (msg_out *) (msgout_node->data);
226 destroy_msg_out(msgout);
227 }
228 g_list_free(msgout_list);
229 }