1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
/*
* Attachment Tool Plug-in -- an attachment processing plug-in for Sylpheed
* Copyright (C) 2010 Hiroyuki Yamamoto
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "defs.h"
#include <glib.h>
#include <gtk/gtk.h>
#include <stdio.h>
#include <sys/stat.h>
#include "sylmain.h"
#include "plugin.h"
#include "procmsg.h"
#include "procmime.h"
#include "utils.h"
#include <glib/gi18n-lib.h>
static SylPluginInfo info = {
"Attachment Tool Plug-in",
"0.1.0",
"Hiroyuki Yamamoto",
"Attachment processing plug-in for Sylpheed"
};
static void remove_attachment_cb(void);
void plugin_load(void)
{
debug_print("initializing attachment_tool plug-in\n");
syl_plugin_add_menuitem("/Tools", NULL, NULL, NULL);
syl_plugin_add_menuitem("/Tools", _("Remove attachments"), remove_attachment_cb, NULL);
debug_print("attachment_tool plug-in loading done.\n");
}
void plugin_unload(void)
{
debug_print("attachment_tool plug-in unloaded.\n");
}
SylPluginInfo *plugin_info(void)
{
return &info;
}
gint plugin_interface_version(void)
{
return SYL_PLUGIN_INTERFACE_VERSION;
}
static gboolean remove_attachment_rec(MimeInfo *partinfo, FILE *fp, FILE *outfp)
{
gchar buf[BUFFSIZE];
gchar *boundary = NULL;
gint boundary_len = 0;
gboolean err = FALSE;
while (partinfo) {
gboolean is_attach_part = FALSE;
gint count;
debug_print("------------------------------------------------- begin %s\n", partinfo->content_type);
debug_print("fpos = %ld\n", partinfo->fpos);
debug_print("content_type = %s\n", partinfo->content_type);
if (partinfo->filename || partinfo->name) {
is_attach_part = TRUE;
debug_print("skip this body\n");
}
if (fseek(fp, partinfo->fpos, SEEK_SET) < 0) {
perror("fseek");
err = TRUE;
break;
}
/* write part headers */
count = 0;
while (fgets(buf, sizeof(buf), fp) != NULL) {
count++;
fputs(buf, outfp);
if (buf[0] == '\r' || buf[0] == '\n') break;
}
debug_print("wrote header: %d lines\n", count);
/* write until first boundary */
count = 0;
if (partinfo->children && partinfo->boundary) {
boundary = partinfo->boundary;
boundary_len = strlen(boundary);
debug_print("write until %s\n", boundary);
while (fgets(buf, sizeof(buf), fp) != NULL) {
count++;
if (IS_BOUNDARY(buf, boundary, boundary_len)) {
fputs(buf, outfp);
debug_print("start boundary: %s", buf);
break;
} else {
fputs(buf, outfp);
}
}
debug_print("wrote %d lines\n", count);
}
if (partinfo->sub) {
debug_print("enter rfc822 -----------------------------\n");
err = !remove_attachment_rec(partinfo->sub, fp, outfp);
debug_print("leave rfc822 -----------------------------\n");
if (err)
break;
} else if (partinfo->children) {
debug_print("enter child -----------------------------\n");
err = !remove_attachment_rec
(partinfo->children, fp, outfp);
debug_print("leave child -----------------------------\n");
if (err)
break;
}
if (partinfo->parent && partinfo->parent->boundary) {
boundary = partinfo->parent->boundary;
boundary_len = strlen(boundary);
debug_print("boundary = %s\n", boundary);
} else {
boundary = NULL;
boundary_len = 0;
}
/* write (or skip) part body and end boundary */
if (boundary && !partinfo->main) {
count = 0;
debug_print("write until %s\n", boundary);
while (fgets(buf, sizeof(buf), fp) != NULL) {
if (IS_BOUNDARY(buf, boundary, boundary_len)) {
count++;
fputs(buf, outfp);
debug_print("end boundary: %s", buf);
break;
} else {
if (!is_attach_part)
count++;
if (!is_attach_part)
fputs(buf, outfp);
}
}
debug_print("wrote body: %d lines\n", count);
}
debug_print("------------------------------------------------- end %s\n", partinfo->content_type);
partinfo = partinfo->next;
}
return !err;
}
static gboolean has_attach_part(MimeInfo *mimeinfo)
{
for (; mimeinfo != NULL; mimeinfo = procmime_mimeinfo_next(mimeinfo)) {
if (mimeinfo->filename || mimeinfo->name)
return TRUE;
}
return FALSE;
}
static gboolean remove_attachment(MsgInfo *msginfo)
{
MimeInfo *mimeinfo;
FILE *fp, *outfp;
gchar *infile, *outfile;
gboolean err = FALSE;
mimeinfo = procmime_scan_message(msginfo);
g_return_val_if_fail(mimeinfo != NULL, FALSE);
if (!has_attach_part(mimeinfo)) {
debug_print("remove_attachment: this message doesn't have attachments\n");
procmime_mimeinfo_free_all(mimeinfo);
return TRUE;
}
if ((fp = procmsg_open_message(msginfo)) == NULL) {
procmime_mimeinfo_free_all(mimeinfo);
return FALSE;
}
infile = procmsg_get_message_file(msginfo);
outfile = g_strconcat(infile, ".tmp", NULL);
debug_print("infile: %s\n", infile);
debug_print("outfile: %s\n", outfile);
if ((outfp = g_fopen(outfile, "wb")) == NULL) {
g_free(outfile);
g_free(infile);
fclose(fp);
procmime_mimeinfo_free_all(mimeinfo);
return FALSE;
}
err = !remove_attachment_rec(mimeinfo, fp, outfp);
if (fclose(outfp) == EOF) {
FILE_OP_ERROR(outfile, "fclose");
err = TRUE;
}
fclose(fp);
if (!err) {
debug_print("overwriting original message file: %s\n", infile);
if (copy_file(outfile, infile, FALSE) == 0) {
GStatBuf s;
if (g_stat(infile, &s) == 0) {
msginfo->size = s.st_size;
msginfo->mtime = s.st_mtime;
}
if (msginfo->folder)
msginfo->folder->cache_dirty = TRUE;
} else
err = TRUE;
}
g_unlink(outfile);
g_free(outfile);
g_free(infile);
procmime_mimeinfo_free_all(mimeinfo);
return !err;
}
static void remove_attachment_cb(void)
{
FolderItem *item;
GSList *mlist, *cur;
gint val;
debug_print("remove_attachment_cb\n");
if (syl_plugin_summary_is_locked())
return;
item = syl_plugin_summary_get_current_folder();
if (!item || !item->folder || FOLDER_TYPE(item->folder) != F_MH ||
item->stype == F_VIRTUAL) {
syl_plugin_alertpanel_message(_("Error"), _("This tool is available on local folders only."), 3);
return;
}
mlist = syl_plugin_summary_get_selected_msg_list();
if (!mlist)
return;
val = syl_plugin_alertpanel(_("Remove attachments"),
_("Do you really remove attached files from the selected messages?"),
GTK_STOCK_YES, GTK_STOCK_NO, NULL);
if (val != 0) {
g_slist_free(mlist);
return;
}
for (cur = mlist; cur != NULL; cur = cur->next) {
MsgInfo *msginfo = (MsgInfo *)cur->data;
if (!msginfo)
continue;
if (!MSG_IS_MIME(msginfo->flags)) {
debug_print("message %u is not multi-part. skipping.\n", msginfo->msgnum);
continue;
}
debug_print("Removing attachments of message: %u: %s\n", msginfo->msgnum, msginfo->subject ? msginfo->subject : "(No Subject)");
if (!remove_attachment(msginfo)) {
g_warning("Cannot remove attachments from %u: %s\n", msginfo->msgnum, msginfo->subject ? msginfo->subject : "(No Subject)");
break;
}
syl_plugin_summary_update_by_msgnum(msginfo->msgnum);
}
g_slist_free(mlist);
syl_plugin_summary_redisplay_msg();
}
|