Statistics
| Branch: | Tag: | Revision:

root / libsylph / procmsg.c @ 8d7dcace

History | View | Annotate | Download (5.4 kB)

1
/*
2
 * LibSylph -- E-Mail client library
3
 * Copyright (C) 1999-2011 Hiroyuki Yamamoto
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
20
#include "defs.h"
21
22
#include <glib.h>
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <errno.h>
26
27
#include "utils.h"
28
#include "procmsg.h"
29
#include "procheader.h"
30
#include "procmime.h"
31
#include "codeconv.h"
32
33
typedef struct _MsgFlagInfo {
34
        guint msgnum;
35
        MsgFlags flags;
36
} MsgFlagInfo;
37
38
39
gchar *procmsg_get_message_file_path(MsgInfo *msginfo)
40
{
41
        gchar *path, *file;
42
43
        g_return_val_if_fail(msginfo != NULL, NULL);
44
45
        if (msginfo->encinfo && msginfo->encinfo->plaintext_file)
46
                file = g_strdup(msginfo->encinfo->plaintext_file);
47
        else if (msginfo->file_path)
48
                return g_strdup(msginfo->file_path);
49
        else
50
                return NULL;
51
52
        return file;
53
}
54
55
gchar *procmsg_get_message_file(MsgInfo *msginfo)
56
{
57
        gchar *filename = NULL;
58
59
        g_return_val_if_fail(msginfo != NULL, NULL);
60
61
        if (msginfo->file_path)
62
                return g_strdup(msginfo->file_path);
63
64
        return filename;
65
}
66
67
FILE *procmsg_open_message(MsgInfo *msginfo)
68
{
69
        FILE *fp;
70
        gchar *file;
71
72
        g_return_val_if_fail(msginfo != NULL, NULL);
73
74
        file = procmsg_get_message_file_path(msginfo);
75
        g_return_val_if_fail(file != NULL, NULL);
76
77
        if (!is_file_exist(file)) {
78
                g_free(file);
79
                file = procmsg_get_message_file(msginfo);
80
                if (!file)
81
                        return NULL;
82
        }
83
84
        if ((fp = g_fopen(file, "rb")) == NULL) {
85
                FILE_OP_ERROR(file, "procmsg_open_message: fopen");
86
                g_free(file);
87
                return NULL;
88
        }
89
90
        g_free(file);
91
92
        if (MSG_IS_QUEUED(msginfo->flags)) {
93
                gchar buf[BUFFSIZE];
94
95
                while (fgets(buf, sizeof(buf), fp) != NULL)
96
                        if (buf[0] == '\r' || buf[0] == '\n') break;
97
        }
98
99
        return fp;
100
}
101
102
FILE *procmsg_open_message_decrypted(MsgInfo *msginfo, MimeInfo **mimeinfo)
103
{
104
        FILE *fp;
105
106
        *mimeinfo = NULL;
107
        if ((fp = procmsg_open_message(msginfo)) == NULL)
108
                return NULL;
109
        *mimeinfo = procmime_scan_mime_header(fp);
110
111
        return fp;
112
}
113
114
MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
115
{
116
        MsgInfo *newmsginfo;
117
118
        if (msginfo == NULL) return NULL;
119
120
        newmsginfo = g_new0(MsgInfo, 1);
121
122
#define MEMBCOPY(mmb)        newmsginfo->mmb = msginfo->mmb
123
#define MEMBDUP(mmb)        newmsginfo->mmb = msginfo->mmb ? \
124
                        g_strdup(msginfo->mmb) : NULL
125
126
        MEMBCOPY(msgnum);
127
        MEMBCOPY(size);
128
        MEMBCOPY(mtime);
129
        MEMBCOPY(date_t);
130
131
        MEMBCOPY(flags);
132
133
        MEMBDUP(fromname);
134
135
        MEMBDUP(date);
136
        MEMBDUP(from);
137
        MEMBDUP(to);
138
        MEMBDUP(cc);
139
        MEMBDUP(newsgroups);
140
        MEMBDUP(subject);
141
        MEMBDUP(msgid);
142
        MEMBDUP(inreplyto);
143
144
        MEMBCOPY(folder);
145
        MEMBCOPY(to_folder);
146
147
        MEMBDUP(xface);
148
149
        MEMBDUP(file_path);
150
151
        if (msginfo->encinfo) {
152
                newmsginfo->encinfo = g_new0(MsgEncryptInfo, 1);
153
                MEMBDUP(encinfo->plaintext_file);
154
                MEMBDUP(encinfo->sigstatus);
155
                MEMBDUP(encinfo->sigstatus_full);
156
                MEMBCOPY(encinfo->decryption_failed);
157
        }
158
159
        return newmsginfo;
160
}
161
162
MsgInfo *procmsg_msginfo_get_full_info(MsgInfo *msginfo)
163
{
164
        MsgInfo *full_msginfo;
165
        gchar *file;
166
167
        if (msginfo == NULL) return NULL;
168
169
        file = procmsg_get_message_file(msginfo);
170
        if (!file) {
171
                g_warning("procmsg_msginfo_get_full_info(): can't get message file.\n");
172
                return NULL;
173
        }
174
175
        full_msginfo = procheader_parse_file(file, msginfo->flags, TRUE);
176
        g_free(file);
177
        if (!full_msginfo) return NULL;
178
179
        full_msginfo->msgnum = msginfo->msgnum;
180
        full_msginfo->size = msginfo->size;
181
        full_msginfo->mtime = msginfo->mtime;
182
        full_msginfo->folder = msginfo->folder;
183
        full_msginfo->to_folder = msginfo->to_folder;
184
185
        full_msginfo->file_path = g_strdup(msginfo->file_path);
186
187
        if (msginfo->encinfo) {
188
                full_msginfo->encinfo = g_new0(MsgEncryptInfo, 1);
189
                full_msginfo->encinfo->plaintext_file =
190
                        g_strdup(msginfo->encinfo->plaintext_file);
191
                full_msginfo->encinfo->sigstatus =
192
                        g_strdup(msginfo->encinfo->sigstatus);
193
                full_msginfo->encinfo->sigstatus_full =
194
                        g_strdup(msginfo->encinfo->sigstatus_full);
195
                full_msginfo->encinfo->decryption_failed =
196
                        msginfo->encinfo->decryption_failed;
197
        }
198
199
        return full_msginfo;
200
}
201
202
gboolean procmsg_msginfo_equal(MsgInfo *msginfo_a, MsgInfo *msginfo_b)
203
{
204
        if (!msginfo_a || !msginfo_b)
205
                return FALSE;
206
207
        if (msginfo_a == msginfo_b)
208
                return TRUE;
209
210
        if (msginfo_a->folder == msginfo_b->folder &&
211
            msginfo_a->msgnum == msginfo_b->msgnum &&
212
            msginfo_a->size   == msginfo_b->size   &&
213
            msginfo_a->mtime  == msginfo_b->mtime)
214
                return TRUE;
215
216
        return FALSE;
217
}
218
219
void procmsg_msginfo_free(MsgInfo *msginfo)
220
{
221
        if (msginfo == NULL) return;
222
223
        g_free(msginfo->xface);
224
225
        g_free(msginfo->fromname);
226
227
        g_free(msginfo->date);
228
        g_free(msginfo->from);
229
        g_free(msginfo->to);
230
        g_free(msginfo->cc);
231
        g_free(msginfo->newsgroups);
232
        g_free(msginfo->subject);
233
        g_free(msginfo->msgid);
234
        g_free(msginfo->inreplyto);
235
236
        slist_free_strings(msginfo->references);
237
        g_slist_free(msginfo->references);
238
239
        g_free(msginfo->file_path);
240
241
        if (msginfo->encinfo) {
242
                g_free(msginfo->encinfo->plaintext_file);
243
                g_free(msginfo->encinfo->sigstatus);
244
                g_free(msginfo->encinfo->sigstatus_full);
245
                g_free(msginfo->encinfo);
246
        }
247
248
        g_free(msginfo);
249
}