-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimap.cpp
More file actions
258 lines (231 loc) · 8.76 KB
/
imap.cpp
File metadata and controls
258 lines (231 loc) · 8.76 KB
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
#include "imap.hpp"
using namespace IMAP;
Session::Session(std::function<void()> updateUI):updateUI(updateUI)
{
imapSession = mailimap_new(0, NULL);
}
Message** Session::getMessages()
{
msgCount = get_msg_count();
int msgArrayLength = msgCount+1;
//stop getMessages if message count is 0.
if(msgCount == 0)
{
msgPtr = new Message*[0];
//set NULL so that there is no seg fault when accessing
msgPtr[0] = NULL;
return msgPtr;
}
struct mailimap_set* mailimapSetPtr = mailimap_set_new_interval(1,0);
struct mailimap_fetch_type* fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
struct mailimap_fetch_att* fetch_att = mailimap_fetch_att_new_uid();
mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
clist* fetch_result;
clistiter* cur;
check_error(mailimap_fetch(imapSession, mailimapSetPtr, fetch_type, &fetch_result), "MAILIMAP_ERROR_FETCH");
// //initialising array to store messages
// //http://www.fredosaurus.com/notes-cpp/newdelete/50dynamalloc.html
msgPtr = new Message*[msgArrayLength];
Session* sessionPtr = this;
int messageArrayIterator = 0;
// Going through list of UID messages
for(cur=clist_begin(fetch_result); cur !=NULL; cur = clist_next(cur))
{
struct mailimap_msg_att* msg_att;
uint32_t uid;
msg_att=(struct mailimap_msg_att*)clist_content(cur);
uid=get_uid(msg_att);
//skip over if UID is invalid. Be Careful.
if(uid>0)
{
msgPtr[messageArrayIterator]=new Message(sessionPtr, uid, updateUI);
messageArrayIterator++;
}
}
msgPtr[messageArrayIterator]=NULL;
mailimap_fetch_list_free(fetch_result);
//be careful with this
mailimap_fetch_type_free(fetch_type);
mailimap_set_free(mailimapSetPtr);
return msgPtr;
}
uint32_t Session:: get_uid(struct mailimap_msg_att* msg_att)
{
clistiter* cur;
for(cur=clist_begin(msg_att->att_list); cur!=NULL; cur=clist_next(cur))
{
struct mailimap_msg_att_item* item;
item=(mailimap_msg_att_item*)clist_content(cur);
if(item->att_type!=MAILIMAP_MSG_ATT_ITEM_STATIC)
{
continue;
}
if(item->att_data.att_static->att_type != MAILIMAP_MSG_ATT_UID)
{
continue;
}
return item->att_data.att_static->att_data.att_uid;
}
return 0;
}
int Session::get_msg_count()
{
struct mailimap_status_att_list* attListPtr = mailimap_status_att_list_new_empty();
check_error(mailimap_status_att_list_add(attListPtr, MAILIMAP_STATUS_ATT_MESSAGES),"Status add for messages failed");
struct mailimap_mailbox_data_status* dataStatus;
const char* mb = mailboxStore.c_str();
check_error(mailimap_status(imapSession, mb, attListPtr, &dataStatus), "Obtain ");
clist* theList = dataStatus -> st_info_list;
auto value = ((mailimap_status_info*)(clist_content(clist_begin(theList))))->st_value;
mailimap_mailbox_data_status_free(dataStatus);
mailimap_status_att_list_free(attListPtr);
return value;
}
std::string Message::get_msg_content(struct mailimap_msg_att * msg_att)
{
for(clistiter* cur = clist_begin(msg_att->att_list) ; cur != NULL ; cur = clist_next(cur)) {
struct mailimap_msg_att_item * item;
item = (mailimap_msg_att_item*)clist_content(cur);
if (item->att_type != MAILIMAP_MSG_ATT_ITEM_STATIC) {
continue;
}
if (item->att_data.att_static->att_type != MAILIMAP_MSG_ATT_BODY_SECTION) {
continue;
}
std::string msg = std::string(item->att_data.att_static->att_data.att_body_section->sec_body_part);
return msg;
}
std::string empty = "empty";
return empty;
}
std::string Message::get_msg_header(struct mailimap_msg_att* msg_att, std::string fieldname)
{
for(clistiter* cur = clist_begin(msg_att->att_list) ; cur!= NULL ; cur = clist_next(cur)) {
struct mailimap_msg_att_item * item;
item = (mailimap_msg_att_item*)clist_content(cur);
if (item->att_type != MAILIMAP_MSG_ATT_ITEM_STATIC) {
continue;
}
if (item->att_data.att_static->att_type!= MAILIMAP_MSG_ATT_ENVELOPE) {
continue;
}
std::string Subject = "Subject";
if (Subject.compare(fieldname) == 0)
{
std::string msg = std::string(item->att_data.att_static->att_data.att_env->env_subject);
return msg;
}
std::string From = "From";
if (From.compare(fieldname)==0)
{
clistiter* cur = clist_begin(item->att_data.att_static->att_data.att_env->env_from->frm_list);
struct mailimap_address* msgAddress = (mailimap_address*)clist_content(cur);
std::string name = std::string(msgAddress-> ad_mailbox_name);
std::string mailbox = std::string(msgAddress->ad_host_name);
return name + "@" + mailbox;
}
}
}
void Session::connect(std::string const& server, size_t port)
{
//Based on line 48 of UI.CPP, we should already have a session in place from constructor function.
//https://stackoverflow.com/questions/45268493/cannot-convert-string-to-char-for-argument-error
//note on pointer to const string: http://www.cplusplus.com/reference/string/string/c_str/
mailimap_socket_connect(this->imapSession, server.c_str(), port);
}
void Session::login(std::string const& userid, std::string const& password)
{
check_error(mailimap_login(this->imapSession, userid.c_str(), password.c_str()), "could not login");
}
void Session::selectMailbox(std::string const& mailbox)
{
mailboxStore.assign(mailbox);
const char* mb = mailboxStore.c_str();
check_error(mailimap_select(imapSession, mb), "could not select mailbox");
}
Session::~Session()
{
for(int msgIterator = 0; msgIterator < msgCount; msgIterator++)
{
delete msgPtr[msgIterator];
}
delete [] msgPtr;
mailimap_logout(imapSession);
mailimap_free(imapSession);
}
std::string Message::getBody()
{
struct mailimap_section* section = mailimap_section_new(NULL);
struct mailimap_fetch_att* fetchAttBody = mailimap_fetch_att_new_body_section(section);
struct mailimap_fetch_type* fetchType = mailimap_fetch_type_new_fetch_att_list_empty();
mailimap_fetch_type_new_fetch_att_list_add(fetchType, fetchAttBody);
struct mailimap_set* mailimapSetSingle = mailimap_set_new_single(uid);
clist* fetchResult = NULL;
mailimap_uid_fetch(sessionPtr->imapSession, mailimapSetSingle, fetchType, &fetchResult);
for(clistiter* cur=clist_begin(fetchResult); cur!=NULL; cur=clist_next(cur))
{
struct mailimap_msg_att* msg_att = (mailimap_msg_att*)clist_content(cur);
std::string msg_content = get_msg_content(msg_att);
if(msg_content.empty())
{
continue;
}
freeHelperFunction(mailimapSetSingle, fetchResult, fetchType);
return msg_content;
}
freeHelperFunction(mailimapSetSingle, fetchResult, fetchType);
std::string empty = "empty";
return empty;
}
std::string Message::getField(std::string fieldname)
{
struct mailimap_fetch_att* fetchHeaderAtt=mailimap_fetch_att_new_envelope();
struct mailimap_fetch_type* fetchType = mailimap_fetch_type_new_fetch_att_list_empty();
mailimap_fetch_type_new_fetch_att_list_add(fetchType, fetchHeaderAtt);
struct mailimap_set* mailimapSetSingle = mailimap_set_new_single(uid);
clist* fetchResultHeader;
mailimap_uid_fetch(sessionPtr->imapSession, mailimapSetSingle, fetchType, &fetchResultHeader);
for(clistiter* cur=clist_begin(fetchResultHeader); cur!=NULL; cur=clist_next(cur))
{
struct mailimap_msg_att* msg_att = (mailimap_msg_att*)clist_content(cur);
std::string msg_content = get_msg_header(msg_att, fieldname);
if(msg_content.empty())
{
continue;
}
freeHelperFunction(mailimapSetSingle, fetchResultHeader, fetchType);
return msg_content;
}
freeHelperFunction(mailimapSetSingle, fetchResultHeader, fetchType);
std::string empty = "";
return empty;
}
void Message::freeHelperFunction(struct mailimap_set* mailimapSetSingle, clist* fetchResult, struct mailimap_fetch_type* fetchType)
{
mailimap_set_free(mailimapSetSingle);
mailimap_fetch_list_free(fetchResult);
mailimap_fetch_type_free(fetchType);
}
void Message::deleteFromMailbox()
{
struct mailimap_flag_list* mailimapFlagList = mailimap_flag_list_new_empty();
struct mailimap_flag* flagDelete = mailimap_flag_new_deleted();
mailimap_flag_list_add(mailimapFlagList, flagDelete);
struct mailimap_store_att_flags* storeAttFlags = mailimap_store_att_flags_new_set_flags(mailimapFlagList);
struct mailimap_set* mailimapSetSingle = mailimap_set_new_single(uid);
check_error(mailimap_uid_store(sessionPtr->imapSession, mailimapSetSingle, storeAttFlags), "Error storing flags");
check_error(mailimap_expunge(sessionPtr->imapSession), "Error expunging flags");
mailimap_store_att_flags_free(storeAttFlags);
mailimap_set_free(mailimapSetSingle);
sessionPtr->deleteAllMail();
updateUI();
}
void Session::deleteAllMail()
{
for(int i = 0; i < msgCount; i++)
{
delete msgPtr[i];
}
delete [] msgPtr;
msgPtr = NULL;
}