Statistics
| Branch: | Tag: | Revision:

root / libsylph / procmsg.c @ aebfd4cc

History | View | Annotate | Download (4.7 kB)

1
/*
2
 * LibSylph -- E-Mail client library
3
 * Copyright (C) 1999-2011 Hiroyuki Yamamoto
4
 */
5
6
#include "defs.h"
7
8
#include <glib.h>
9
#include <stdio.h>
10
#include <stdlib.h>
11
#include <errno.h>
12
13
#include "utils.h"
14
#include "procmsg.h"
15
#include "procheader.h"
16
#include "procmime.h"
17
#include "codeconv.h"
18
19
typedef struct _MsgFlagInfo {
20
        guint msgnum;
21
        MsgFlags flags;
22
} MsgFlagInfo;
23
24
25
gchar *procmsg_get_message_file_path(MsgInfo *msginfo)
26
{
27
        gchar *file;
28
29
        g_return_val_if_fail(msginfo != NULL, NULL);
30
31
        if (msginfo->encinfo && msginfo->encinfo->plaintext_file)
32
                file = g_strdup(msginfo->encinfo->plaintext_file);
33
        else if (msginfo->file_path)
34
                return g_strdup(msginfo->file_path);
35
        else
36
                return NULL;
37
38
        return file;
39
}
40
41
gchar *procmsg_get_message_file(MsgInfo *msginfo)
42
{
43
        gchar *filename = NULL;
44
45
        g_return_val_if_fail(msginfo != NULL, NULL);
46
47
        if (msginfo->file_path)
48
                return g_strdup(msginfo->file_path);
49
50
        return filename;
51
}
52
53
FILE *procmsg_open_message(MsgInfo *msginfo)
54
{
55
        FILE *fp;
56
        gchar *file;
57
58
        g_return_val_if_fail(msginfo != NULL, NULL);
59
60
        file = procmsg_get_message_file_path(msginfo);
61
        g_return_val_if_fail(file != NULL, NULL);
62
63
        if (!is_file_exist(file)) {
64
                g_free(file);
65
                file = procmsg_get_message_file(msginfo);
66
                if (!file)
67
                        return NULL;
68
        }
69
70
        if ((fp = g_fopen(file, "rb")) == NULL) {
71
                FILE_OP_ERROR(file, "procmsg_open_message: fopen");
72
                g_free(file);
73
                return NULL;
74
        }
75
76
        g_free(file);
77
78
        if (MSG_IS_QUEUED(msginfo->flags)) {
79
                gchar buf[BUFFSIZE];
80
81
                while (fgets(buf, sizeof(buf), fp) != NULL)
82
                        if (buf[0] == '\r' || buf[0] == '\n') break;
83
        }
84
85
        return fp;
86
}
87
88
FILE *procmsg_open_message_decrypted(MsgInfo *msginfo, MimeInfo **mimeinfo)
89
{
90
        FILE *fp;
91
92
        *mimeinfo = NULL;
93
        if ((fp = procmsg_open_message(msginfo)) == NULL)
94
                return NULL;
95
        *mimeinfo = procmime_scan_mime_header(fp);
96
97
        return fp;
98
}
99
100
MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo)
101
{
102
        MsgInfo *newmsginfo;
103
104
        if (msginfo == NULL) return NULL;
105
106
        newmsginfo = g_new0(MsgInfo, 1);
107
108
#define MEMBCOPY(mmb)        newmsginfo->mmb = msginfo->mmb
109
#define MEMBDUP(mmb)        newmsginfo->mmb = msginfo->mmb ? \
110
                        g_strdup(msginfo->mmb) : NULL
111
112
        MEMBCOPY(msgnum);
113
        MEMBCOPY(size);
114
        MEMBCOPY(mtime);
115
        MEMBCOPY(date_t);
116
117
        MEMBCOPY(flags);
118
119
        MEMBDUP(fromname);
120
121
        MEMBDUP(date);
122
        MEMBDUP(from);
123
        MEMBDUP(to);
124
        MEMBDUP(cc);
125
        MEMBDUP(newsgroups);
126
        MEMBDUP(subject);
127
        MEMBDUP(msgid);
128
        MEMBDUP(inreplyto);
129
130
        MEMBCOPY(folder);
131
        MEMBCOPY(to_folder);
132
133
        MEMBDUP(xface);
134
135
        MEMBDUP(file_path);
136
137
        if (msginfo->encinfo) {
138
                newmsginfo->encinfo = g_new0(MsgEncryptInfo, 1);
139
                MEMBDUP(encinfo->plaintext_file);
140
                MEMBDUP(encinfo->sigstatus);
141
                MEMBDUP(encinfo->sigstatus_full);
142
                MEMBCOPY(encinfo->decryption_failed);
143
        }
144
145
        return newmsginfo;
146
}
147
148
MsgInfo *procmsg_msginfo_get_full_info(MsgInfo *msginfo)
149
{
150
        MsgInfo *full_msginfo;
151
        gchar *file;
152
153
        if (msginfo == NULL) return NULL;
154
155
        file = procmsg_get_message_file(msginfo);
156
        if (!file) {
157
                g_warning("procmsg_msginfo_get_full_info(): can't get message file.\n");
158
                return NULL;
159
        }
160
161
        full_msginfo = procheader_parse_file(file, msginfo->flags, TRUE);
162
        g_free(file);
163
        if (!full_msginfo) return NULL;
164
165
        full_msginfo->msgnum = msginfo->msgnum;
166
        full_msginfo->size = msginfo->size;
167
        full_msginfo->mtime = msginfo->mtime;
168
        full_msginfo->folder = msginfo->folder;
169
        full_msginfo->to_folder = msginfo->to_folder;
170
171
        full_msginfo->file_path = g_strdup(msginfo->file_path);
172
173
        if (msginfo->encinfo) {
174
                full_msginfo->encinfo = g_new0(MsgEncryptInfo, 1);
175
                full_msginfo->encinfo->plaintext_file =
176
                        g_strdup(msginfo->encinfo->plaintext_file);
177
                full_msginfo->encinfo->sigstatus =
178
                        g_strdup(msginfo->encinfo->sigstatus);
179
                full_msginfo->encinfo->sigstatus_full =
180
                        g_strdup(msginfo->encinfo->sigstatus_full);
181
                full_msginfo->encinfo->decryption_failed =
182
                        msginfo->encinfo->decryption_failed;
183
        }
184
185
        return full_msginfo;
186
}
187
188
gboolean procmsg_msginfo_equal(MsgInfo *msginfo_a, MsgInfo *msginfo_b)
189
{
190
        if (!msginfo_a || !msginfo_b)
191
                return FALSE;
192
193
        if (msginfo_a == msginfo_b)
194
                return TRUE;
195
196
        if (msginfo_a->folder == msginfo_b->folder &&
197
            msginfo_a->msgnum == msginfo_b->msgnum &&
198
            msginfo_a->size   == msginfo_b->size   &&
199
            msginfo_a->mtime  == msginfo_b->mtime)
200
                return TRUE;
201
202
        return FALSE;
203
}
204
205
void procmsg_msginfo_free(MsgInfo *msginfo)
206
{
207
        if (msginfo == NULL) return;
208
209
        g_free(msginfo->xface);
210
211
        g_free(msginfo->fromname);
212
213
        g_free(msginfo->date);
214
        g_free(msginfo->from);
215
        g_free(msginfo->to);
216
        g_free(msginfo->cc);
217
        g_free(msginfo->newsgroups);
218
        g_free(msginfo->subject);
219
        g_free(msginfo->msgid);
220
        g_free(msginfo->inreplyto);
221
222
        slist_free_strings(msginfo->references);
223
        g_slist_free(msginfo->references);
224
225
        g_free(msginfo->file_path);
226
227
        if (msginfo->encinfo) {
228
                g_free(msginfo->encinfo->plaintext_file);
229
                g_free(msginfo->encinfo->sigstatus);
230
                g_free(msginfo->encinfo->sigstatus_full);
231
                g_free(msginfo->encinfo);
232
        }
233
234
        g_free(msginfo);
235
}