This repository has been archived by the owner on Feb 3, 2020. It is now read-only.
/
message.cc
398 lines (331 loc) · 7.52 KB
/
message.cc
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/**
* message.cc - A class for working with a single message.
*
* This file is part of lumail: http://lumail.org/
*
* Copyright (c) 2013 by Steve Kemp. All rights reserved.
*
**
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 dated June, 1991, or (at your
* option) any later version.
*
* On Debian GNU/Linux systems, the complete text of version 2 of the GNU
* General Public License can be found in `/usr/share/common-licenses/GPL-2'
*/
#include <stdint.h>
#include <iostream>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <mimetic/mimetic.h>
#include "message.h"
#include "global.h"
using namespace std;
using namespace mimetic;
/**
* Constructor.
*/
CMessage::CMessage(std::string filename)
{
m_path = filename;
m_me = NULL;
}
/**
* Destructor.
*/
CMessage::~CMessage()
{
if ( m_me != NULL )
delete( m_me );
}
/**
* Get the path to the message on-disk.
*/
std::string CMessage::path()
{
return (m_path);
}
/**
* Get the flags for this message.
*/
std::string CMessage::flags()
{
std::string flags = "";
if (m_path.empty())
return (flags);
size_t offset = m_path.find(":2,");
if (offset != std::string::npos)
flags = m_path.substr(offset + 3);
if ( flags.size() > 3)
flags = "";
/**
* Sleazy Hack.
*/
if ( m_path.find( "/new/" ) != std::string::npos )
flags += "N";
/**
* Pad.
*/
while( (int)strlen(flags.c_str()) < 4 )
flags += " ";
return flags;
}
/**
* Does this message match the given filter.
*/
bool CMessage::matches_filter( std::string *filter )
{
if ( strcmp( filter->c_str(), "all" ) == 0 )
return true;
if ( ( strcmp( filter->c_str(), "new" ) == 0 ) &&
is_new() )
return true;
return false;
}
/**
* Is this message new?
*/
bool CMessage::is_new()
{
std::string f = flags();
if ( f.find( 'N' ) != std::string::npos )
return true;
else
return false;
}
/**
* Mark the given message as read.
*/
bool CMessage::mark_read()
{
/*
* Get the current path, and build a new one.
*/
std::string c_path = m_path;
std::string n_path = "";
size_t offset = std::string::npos;
/**
* If we find /new/ in the path then rename to be /cur/
*/
if ( ( offset = c_path.find( "/new/" ) )!= std::string::npos )
{
/**
* Path component before /new/ + after it.
*/
std::string before = c_path.substr(0,offset);
std::string after = c_path.substr(offset+strlen("/new/"));
n_path = before + "/cur/" + after;
if ( rename( c_path.c_str(), n_path.c_str() ) == 0 ) {
m_path = n_path;
return true;
}
else {
return false;
}
}
else {
/**
* The file is new, but not in the new folder. THat means we need to remove "N" from
* the flag-component of the path.
*
* TODO
*/
return false;
}
}
bool CMessage::mark_new()
{
/*
* Get the current path, and build a new one.
*/
std::string c_path = m_path;
std::string n_path = "";
size_t offset = std::string::npos;
/**
* If we find /cur/ in the path then rename to be /new/
*/
if ( ( offset = c_path.find( "/cur/" ) )!= std::string::npos )
{
/**
* Path component before /cur/ + after it.
*/
std::string before = c_path.substr(0,offset);
std::string after = c_path.substr(offset+strlen("/cur/"));
n_path = before + "/new/" + after;
if ( rename( c_path.c_str(), n_path.c_str() ) == 0 ) {
m_path = n_path;
return true;
}
else {
return false;
}
}
else {
/**
* The file is old, but not in the old folder. That means we need to add "N" to
* the flag-component of the path.
*
* TODO
*/
return false;
}
}
/**
* Format the message for display in the header - via the lua format string.
*/
std::string CMessage::format( std::string fmt )
{
std::string result;
/**
* Get the format-string we'll expand from the global
* setting, if it wasn't supplied.
*/
if ( fmt.empty() ) {
CGlobal *global = CGlobal::Instance();
std::string *fmt = global->get_index_format();
result = std::string(*fmt);
}
else {
result = fmt;
}
/**
* The variables we know about.
*/
const char *fields[6] = { "FLAGS", "FROM", "TO", "SUBJECT", "DATE", 0 };
const char **std_name = fields;
/**
* Iterate over everything we could possibly-expand.
*/
for( int i = 0 ; std_name[i] ; ++i) {
size_t offset = result.find( std_name[i], 0 );
if ( ( offset != std::string::npos ) && ( offset < result.size() ) ) {
/**
* The bit before the variable, the bit after, and the body we'll replace it with.
*/
std::string before = result.substr(0,offset-1);
std::string body = "";
std::string after = result.substr(offset+strlen(std_name[i]));
/**
* Expand the specific variables.
*/
if ( strcmp(std_name[i] , "TO" ) == 0 ) {
body = to();
}
if ( strcmp(std_name[i] , "DATE" ) == 0 ) {
body = date();
}
if ( strcmp(std_name[i] , "FROM" ) == 0 ) {
body += from();
}
if ( strcmp(std_name[i] , "FLAGS" ) == 0 ) {
body = flags();
}
if ( strcmp(std_name[i] , "SUBJECT" ) == 0 ) {
body = subject();
}
result = before + body + after;
}
}
return( result );
}
/**
* Get the value of a header.
*/
std::string CMessage::header( std::string name )
{
if ( m_me == NULL ) {
ifstream file(path().c_str());
m_me = new MimeEntity(file);
}
Header & h = m_me->header();
if (h.hasField(name ) )
return(h.field(name).value() );
else
return "";
}
/**
* Get the sender of the message.
*/
std::string CMessage::from()
{
return(header( "From" ) );
}
/**
* Get the date of the message.
*
* TODO: If date is empty stat() the filename.
*/
std::string CMessage::date()
{
return( header("Date" ) );
}
/**
* Get the recipient of the message.
*/
std::string CMessage::to()
{
return( header( "To" ) );
}
/**
* Get the subject of the message.
*/
std::string CMessage::subject()
{
return( header( "Subject" ) );
}
/**
* Get the body of the message, as a vector of lines.
*/
std::vector<std::string> CMessage::body()
{
std::vector<std::string> result;
/**
* Parse if we've not done so.
*/
if ( m_me == NULL ) {
ifstream file(path().c_str());
m_me = new MimeEntity(file);
}
/**
* The body.
*/
std::string body;
/**
* Iterate over every part.
*/
mimetic::MimeEntityList& parts = m_me->body().parts();
mimetic::MimeEntityList::iterator mbit = parts.begin(), meit = parts.end();
for(; mbit != meit; ++mbit) {
/**
* Get the content-type.
*/
std::string type = (*mbit)->header().contentType().str();
/**
* If we've found text/plain then we're good.
*/
if ( type.find( "text/plain" ) != std::string::npos )
{
if ( body.empty() )
body = (*mbit)->body();
}
}
/**
* If we failed to find a part of text/plain then just grab the whole damn
* thing and hope for the best.
*/
if ( body.empty() )
body = m_me->body();
/**
* Split the body into an array, by newlines newlines.
*/
std::stringstream stream(body);
std::string line;
while (std::getline(stream, line)) {
result.push_back( line );
}
return(result);
}