-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmailcap.cpp
377 lines (335 loc) · 9.43 KB
/
mailcap.cpp
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* $Id$
*
* EFiler - EDE File Manager
* Part of Equinox Desktop Environment (EDE).
* Copyright (c) 2000-2007 EDE Authors.
*
* This program is licenced under terms of the
* GNU General Public Licence version 2 or newer.
* See COPYING for details.
*/
// References:
// [1] Mutt documentation, chapter on mailcap
// http://www.mutt.org/doc/manual/manual-5.html
// [2] RFC 1524
// http://www.faqs.org/rfcs/rfc1524.html
#include "mailcap.h"
#include <edelib/String.h>
#include <edelib/List.h>
#include <edelib/Util.h>
#include <edelib/Directory.h>
#include <edelib/File.h>
#include <edelib/FileTest.h>
#include <edelib/StrUtil.h>
#include <cstdio>
#include <string>
#include <fstream>
// See: [2] Appendix A
#define MAILCAPS "/etc/mailcap:/usr/etc/mailcap:/usr/local/etc/mailcap"
// FIXME: use favourite aplications to find xterm
#define TERM "xterm"
// Maximum length of a mailcap spec
// We could've used edelib::String, but that would make the whole code a bit
// more complex and a bit slower. There's no reason for mailcap to be longer
// (very long mailcaps are usually application specific codes /e.g. mutt/ which
// isn't useful to us)
#define BUFLEN 1000
using namespace edelib; // this drastically shortened the code...
struct mime_db_struct {
String type;
String viewcmd;
String createcmd;
String editcmd;
String printcmd;
bool copiousoutput;
bool needsterminal;
};
// database of parsed mimetypes
list < mime_db_struct > mime_db;
// flag to know if we should call read_files()
bool files_read = false;
// Expand various macros in mailcap command spec and prevent
// certain security issues
String mailcap_sanitize(char *cmd, String type)
{
String s(cmd);
// no %s, file should be piped to stdin
unsigned int k = s.find("%s");
if (k == String::npos) {
s = String("cat '%s' | ") + cmd;
} else {
// replace %s with '%s' (see: [1])
while (k != String::npos) {
s = s.substr(0, k) + "'%s'" + s.substr(k + 2);
k = s.find("%s", k + 2);
}
}
// replace %t with mimetype
k = s.find("%t");
while (k != String::npos) {
s = s.substr(0, k) + type + s.substr(k + 2);
k = s.find("%t", k + 1);
}
// we don't know the charset
k = s.find("%{charset}");
while (k != String::npos) {
s = s.substr(0, k) + "us-ascii" + s.substr(k + 10);
k = s.find("%{charset}", k + 1);
}
// ignore other mutt specific codes
k = s.find("%{");
while (k != String::npos) {
s = s.substr(0, k) + s.substr(k + 2);
k = s.find("}", k); // there has to be a closing brace...
if (k != String::npos)
s = s.substr(0, k) + s.substr(k + 1);
k = s.find("%{", k + 1);
}
return s;
}
// Read contents of mailcap file
void read_files()
{
list < String > locations;
// RFC compliant list of mailcap locations
stringtok(locations, String(MAILCAPS), ":");
// Add ~/.mailcap
locations.push_front(dir_home() + E_DIR_SEPARATOR_STR ".mailcap");
// Add EDE specific configuration
if (user_config_dir() != "")
locations.push_front(user_config_dir() +
E_DIR_SEPARATOR_STR "ede"
E_DIR_SEPARATOR_STR "mailcap");
// Test all these locations
const char *mailcap = NULL;
{
list < String >::iterator it = locations.begin(), it_end =
locations.end();
for (; it != it_end; ++it) {
if (file_test((*it).c_str(), edelib::FILE_TEST_EXISTS)) {
mailcap = (*it).c_str();
break;
}
}
}
fprintf(stderr, " ------ mailcap: %s\n", mailcap);
// If mailcap was not found, create an empty one in the user's home
// directory.
if (mailcap == NULL) {
fprintf(stderr,
" --- Missing mailcap. Crating a new mailcap: %s\n",
mailcap);
using namespace std;
string tmpvar =
(string) dir_home().c_str() + E_DIR_SEPARATOR_STR +
".mailcap";
ofstream tmpfile;
tmpfile.open(tmpvar.c_str());
if (tmpfile.is_open()) {
mailcap = tmpvar.c_str();
// Read data into list
FILE *fp = fopen(mailcap, "r");
char buffer[BUFLEN];
while (fgets(buffer, BUFLEN, fp)) {
str_trim(buffer);
if (buffer[0] == '#')
continue; //comment
if (strlen(buffer) < 2)
continue; //empty line
// Multiline
if (buffer[strlen(buffer) - 1] == '\\') {
String line(buffer); //Use string for dynamic growing
while (fgets(buffer, BUFLEN, fp)) {
str_trim(buffer);
line =
line.substr(0,
line.length() -
2);
line += buffer;
if (buffer[strlen(buffer) - 1]
!= '\\')
break;
}
// Use just the first BUFLEN chars
strncpy(buffer, line.c_str(), BUFLEN);
buffer[BUFLEN - 1] = '\0';
}
// Parse line
mime_db_struct parsed;
char *tmp = strtok(buffer, ";");
str_trim(tmp);
parsed.type = tmp;
// Command for viewing file
tmp = strtok(NULL, ";");
if (tmp == NULL)
continue; // no view cmd in line!?
str_trim(tmp);
parsed.viewcmd =
mailcap_sanitize(tmp, parsed.type);
// Other mailcap parameters...
parsed.copiousoutput = parsed.needsterminal =
false;
while ((tmp = strtok(NULL, ";")) != NULL) {
str_trim(tmp);
if (strcmp(tmp, "copiousoutput") == 0)
parsed.copiousoutput = true;
else if (strcmp(tmp, "needsterminal") ==
0)
parsed.needsterminal = true;
else if (strncmp(tmp, "compose=", 8) ==
0)
parsed.createcmd =
mailcap_sanitize(tmp + 8,
parsed.
type);
else if (strncmp(tmp, "print=", 6) == 0)
parsed.printcmd =
mailcap_sanitize(tmp + 6,
parsed.
type);
else if (strncmp(tmp, "edit=", 5) == 0)
parsed.editcmd =
mailcap_sanitize(tmp + 5,
parsed.
type);
// We ignore other parameters
}
mime_db.push_back(parsed);
}
fclose(fp);
} else
files_read = false;
tmpfile.close();
}
files_read = true;
}
// Helper function for searching mime_db
list < mime_db_struct >::iterator find_type(const char *type)
{
list < mime_db_struct >::iterator it = mime_db.begin(), it_end =
mime_db.end();
for (; it != it_end; ++it)
if ((*it).type == type)
return it;
it = mime_db.begin();
for (; it != it_end; ++it) {
String s((*it).type);
if (s[s.length() - 1] == '*'
&& strncmp(type, s.c_str(), s.length() - 1) == 0)
return it;
}
return 0;
}
// Command for performing given action on files of given type
const char *mailcap_opener(const char *type, MailcapAction action)
{
static char buffer[BUFLEN];
if (!files_read)
read_files();
list < mime_db_struct >::iterator it = find_type(type);
if (it == 0)
return 0;
if (action == MAILCAP_VIEW) {
const char *c = (*it).viewcmd.c_str();
if ((*it).copiousoutput) {
// copiousoutput and needsterminal are mutually exclusive [2]
// TODO: use enotepad? - when it supports stdin
snprintf(buffer, BUFLEN - 1, TERM " -e \"%s\" | less",
c);
} else if ((*it).needsterminal) {
snprintf(buffer, BUFLEN - 1, TERM " -e \"%s\"", c);
} else
return c;
return buffer;
}
else if (action == MAILCAP_CREATE)
return (*it).createcmd.c_str();
else if (action == MAILCAP_EDIT)
return (*it).editcmd.c_str();
else if (action == MAILCAP_PRINT)
return (*it).printcmd.c_str();
else
return 0;
}
// List of actions available for given type
int mailcap_actions(const char *type)
{
if (!files_read)
read_files();
list < mime_db_struct >::iterator it = find_type(type);
if (it == 0)
return 0;
int result = 0;
if ((*it).viewcmd != "")
result += MAILCAP_VIEW;
if ((*it).createcmd != "")
result += MAILCAP_CREATE;
if ((*it).editcmd != "")
result += MAILCAP_EDIT;
if ((*it).printcmd != "")
result += MAILCAP_PRINT;
return result;
}
// Add a new opener for given type
// NOTE: This will be written in EDE-specific location. If EDE-specific
// mailcap file doesn't exist, it will be created and existing mimetypes
// will be added. Thus, users of EDE will get a new type in addition to
// old ones, however other applications will not.
void mailcap_add_type(const char *type, const char *opener)
{
// Find old type, edit if it exists
list < mime_db_struct >::iterator it = find_type(type);
if (it != 0) {
(*it).viewcmd = opener;
(*it).viewcmd += " %s";
} else {
mime_db_struct newtype;
newtype.type = type;
newtype.viewcmd = opener;
newtype.viewcmd += " %s";
newtype.createcmd = "";
newtype.editcmd = "";
newtype.printcmd = "";
newtype.copiousoutput = false;
newtype.needsterminal = false;
mime_db.push_back(newtype);
}
// Write mime_db contents to file
String ede_mailcap =
user_config_dir() +
E_DIR_SEPARATOR_STR "ede" E_DIR_SEPARATOR_STR "mailcap";
FILE *fp = fopen(ede_mailcap.c_str(), "w");
it = mime_db.begin();
list < mime_db_struct >::iterator it_end = mime_db.end();
while (it != it_end) {
char buffer[BUFLEN];
snprintf(buffer, BUFLEN - 1, "%s; %s", (*it).type.c_str(),
(*it).viewcmd.c_str());
if ((*it).createcmd != "") {
strncat(buffer, "; compose=",
BUFLEN - 1 - strlen(buffer));
strncat(buffer, (*it).createcmd.c_str(),
BUFLEN - 1 - strlen(buffer));
}
if ((*it).editcmd != "") {
strncat(buffer, "; edit=", BUFLEN - 1 - strlen(buffer));
strncat(buffer, (*it).editcmd.c_str(),
BUFLEN - 1 - strlen(buffer));
}
if ((*it).printcmd != "") {
strncat(buffer, "; print=",
BUFLEN - 1 - strlen(buffer));
strncat(buffer, (*it).printcmd.c_str(),
BUFLEN - 1 - strlen(buffer));
}
if (strlen(buffer) > BUFLEN - 2)
// Even if line is too long, we don't want to mangle the next one
buffer[strlen(buffer) - 1] = '\n';
else
strcat(buffer, "\n");
fputs(buffer, fp);
++it;
}
fclose(fp);
}