masqmail

annotate src/message.c @ 6:c9bce6bb2a5d

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