-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimapFuse.cpp
895 lines (777 loc) · 23.8 KB
/
imapFuse.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
/**
* @file imapFuse.cpp
* @brief This file contain the functions which will be connected to FUSE.
* @author Alejandro Ocampos Veiga
* @date 30/03/2011
*/
#define FUSE_USE_VERSION 26
#include <iostream>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <list>
#include <fuse.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include "fusecpp.h"
#include "ImapLibrary/imap.h"
#include "ImapLibrary/Folder.h"
using namespace fuse_cpp;
static const char *hello_str = "Hello World!\n";
static const char *hello_path = "/hello";
static ACon *connection;
static string imap_server;
static int port_n;
static string user;
static string pass;
static bool usessl;
static list<Folder> folder_list; // List of folders in folder root
// Methods header
string build_header(Message* email);
string pathEmail(char* path);
Message* search_mail(char* path);
Folder* search_folder(char * name);
Folder* search_subfolder_in_folder(char* name, Folder* f);
Folder* search_subfolder(char * name);
int get_folders_list_from_server(string folder_root, Folder* folder);
string trim_email(string mailstring);
char *trimwhitespace(char *str);
// FUSE methods
/**
* This function obtain the attributes of a file.
* @param path The path of the file
* @param stbuf The place where the stat structure with his attributes will be save
* @return 0 on success
*/
static int ImapFuse_getattr(const char *path, struct stat *stbuf) {
int res = 0;
string file;
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
} else if (search_folder(((char *)path)+1) != NULL) { // If it is a dir in folder root
// I do path + 1, because the path is like /folder_name (ignore / in the search)
stbuf->st_mode = S_IFDIR | 0555; // dr-xr-xr-x
stbuf->st_nlink = 2;
stbuf->st_uid = getuid();
stbuf->st_gid = getgid();
stbuf->st_size = 0;
stbuf->st_blksize = 4096;
stbuf->st_blocks = stbuf->st_size/stbuf->st_blksize+((stbuf->st_size%stbuf->st_blksize)? 1:0);
} else if (search_subfolder((char *) path) != NULL) { // If it is a subfolder
stbuf->st_mode = S_IFDIR | 0555; // dr-xr-xr-x
stbuf->st_nlink = 2;
stbuf->st_uid = getuid();
stbuf->st_gid = getgid();
stbuf->st_size = 0;
stbuf->st_blksize = 4096;
stbuf->st_blocks = stbuf->st_size/stbuf->st_blksize+((stbuf->st_size%stbuf->st_blksize)? 1:0);
} else if (search_mail((char *) path) != NULL) {
stbuf->st_mode = S_IFDIR | 0555; // dr-xr-xr-x
stbuf->st_nlink = 2;
stbuf->st_uid = getuid();
stbuf->st_gid = getgid();
stbuf->st_size = 0;
stbuf->st_blksize = 4096;
stbuf->st_blocks = stbuf->st_size/stbuf->st_blksize+((stbuf->st_size%stbuf->st_blksize)? 1:0);
} else if ((file=pathEmail((char*) path)) != "") {
string text;
Message* email;
int len;
// To obtain the size of the file
email = search_mail((char *) file.c_str());
text = path;
if (text.find("body")!=string::npos) { // We must know the size of body file
email->get_Body_From_Server(connection);
text = email->get_Body_Text();
len = text.length();
} else if (text.find("header")!=string::npos) { // We must know the size of header file
text = build_header(email);
len = text.length();
} else // I don't know the file
len = 0;
// Stablish attributes
stbuf->st_mode = S_IFREG | 0444; // -r--r--r--
stbuf->st_nlink = 1;
stbuf->st_uid = getuid();
stbuf->st_gid = getgid();
stbuf->st_size = len;
} else
res = -ENOENT;
return res;
}
/**
* This function verify if a file (email) exists and if it has enough permissions
* @param path The path of the file
* @param fi Information about open files
* @return 0 on success. -ENOENT if the file doesn't exist or -EACCES if the file hasn't permissions
*/
static int ImapFuse_open(const char *path, struct fuse_file_info *fi) {
string file = pathEmail((char*)path);
// Look for the email
if (file == "")
return -ENOENT;
// Verify permissions
if ((fi->flags & 3) != O_RDONLY)
return -EACCES;
return 0;
}
/**
* This function read the content of a file (email)
* @param path The path of the file
* @param buf The buffer where the content of the email will be stored
* @param size The size of the email
* @param offset Position from which we must read
* @param fi Information about open files
* @return 0 on success. -ENOENT if the file doesn't exist or -EACCES if the file hasn't permissions
*/
static int ImapFuse_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
size_t len;
(void) fi;
Message* email;
string text;
string path2 = path;
string file = pathEmail((char*)path); // get the path of the email
// Look for the email
email = search_mail((char *) file.c_str());
if (email == NULL)
return -ENOENT;
if (path2.find("body")!=string::npos) {
// Get body of the email
if (email->have_Body_Text() != 1) // not body
return -ENOENT;
// Print email
text = email->get_Body_Text();
len = text.length();
if (offset < len) {
if (offset + size > len)
size = len - offset;
memcpy(buf, ((char*)text.c_str()) + offset, size);
} else
size = 0;
}
if (path2.find("header")!=string::npos) {
text = build_header(email);
len = text.length();
if (offset < len) {
if (offset + size > len)
size = len - offset;
memcpy(buf, ((char*)text.c_str()) + offset, size);
} else
size = 0;
}
return size;
}
/**
* This method obtains all the archives in path.
* @param path The path of the file
* @param buf The structure which will be filling with folder names
* @param filler The filling function
* @param offset The offset between directory entries
* @param fi The info of fuse file
*/
static int ImapFuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) {
(void) offset;
(void) fi;
if (strcmp(path, "/") != 0) {// If it isn't the folder root
Folder* f;
char* folder1;
char* temppath = (char*) malloc(strlen(path)+1);
strncpy(temppath,path,strlen(path)+1); // get a copy of path, because strtok modifies the string
folder1 = strtok(temppath, "/"); // get the first folder name of the path
if (get_folders_list_from_server("", NULL) != SUCCESS) { // get the folder_list
free(temppath);
return -ENOENT;
}
f = search_folder(folder1);
if (f != NULL) { // If folder exists
// The rigor entries
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
// Obtain subfolders (if exists)
if (get_folders_list_from_server(path+1, f) != SUCCESS) { // path+1 because the path is like /name
// path could be a email. In this case, I have to cut the path to the email,
// check it and check if it's really a email
string temp_path = path;
int lastbar = temp_path.find_last_of("/");
if (lastbar+1 == temp_path.length()) { // if the last character of path is a '/'
temp_path = temp_path.substr(0,temp_path.length() -1);
lastbar = temp_path.find_last_of("/");
}
if (temp_path[0] == '/') // if the first character of path is a '/'
temp_path = temp_path.substr(1,lastbar-1);
else
temp_path = temp_path.substr(0,lastbar);
// Check if is a correct path and a correct email. In affirmative case fill with "header" and "body"
if (get_folders_list_from_server(temp_path, f) != SUCCESS) {
free(temppath);
return -ENOENT; // The path doesn't exist
} else {
if (search_mail((char*)path) != NULL) {
filler(buf, "header", NULL, 0);
filler(buf, "body", NULL, 0);
} else {
free(temppath);
return -ENOENT; // It isn't an email
}
}
} else {
f = search_subfolder((char*)path); // get the actual folder
int num_subfolders = f->get_Num_subFolders(); // num of subfolders
if (num_subfolders > 0) {
int i;
Folder* tempfolder;
for (i=1;i<=num_subfolders;i++) { // Insert in filler all the subfolder names
tempfolder = f->get_subFolder(i);
filler(buf, tempfolder->get_Folder_Name().c_str(), NULL, 0);
}
}
// Get emails (if exists)
string folderOpenStatus;
int numMessages;
int i;
char* path_folder = ((char*) f->get_Full_Folder_Name().c_str())+1;
if (SelectFolder(connection, path_folder, numMessages, folderOpenStatus) != UNKNOWN_MAILBOX) {
i = GetNewMail(connection, *f);
if (i>BEG_FAIL || i==LIST_FAILED || i== FOLDER_NOT_EXIST)
return -ENOENT;
// Show all the messages
Message *email;
string email_from;
string email_subject;
string email_uid;
string email_file;
for (i=1;i<=numMessages;i++) {
email = f->get_Message(i);
// Get from address
email_from = email->get_From_Address();
email_from = trim_email(email_from);
// Get subject
email_subject = email->get_Subject();
email_subject = trimwhitespace((char *)email_subject.c_str());
// Get uid
char* temp = (char*) malloc(4);
sprintf(temp, "%d", email->get_UID());
email_uid = temp;
email_file = "";
email_file.append(email_uid);
email_file.append("-");
email_file.append(email_subject);
email_file.append(" - ");
email_file.append(email_from);
filler(buf, (char*) email_file.c_str(), NULL, 0);
free(temp);
}
} else
cout << "UNKNOW MAILBOX" << endl;
}
free(temppath);
return 0;
}
return -ENOENT;
} else { // We are in folder root
if (get_folders_list_from_server("", NULL) == SUCCESS) { // Obtain list of folders from folder root
// The rigor entries
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
//cout << "." << endl << ".." << endl;
// Obtain all the folders
list<Folder>::iterator itera = folder_list.begin();
while(itera != folder_list.end()) {
filler(buf, ((*itera).get_Folder_Name()).c_str(), NULL, 0);
//cout << (*itera).get_Folder_Name() << endl;
itera++;
}
} else { // An error has been ocurred
return -ENOENT;
}
}
return 0;
}
// Useful Methods
/**
* Add folder to folder list
* @param F A folder object
* @param folder_root name of folder root
*/
void add_Folder(Folder &F, string folder_root) {
F.set_Full_Folder_Name( folder_root + "/" + F.get_Folder_Name() );
folder_list.push_back( F );
return;
}
/**
* Add folder to folder list
* @param F folder object
* @param folder_root name of folder root
* @param folder parent folder
*/
void add_Folder_in_folder(Folder F, string folder_root, Folder* folder) {
F.set_Full_Folder_Name( folder_root + "/" + F.get_Folder_Name() );
folder->add_subFolder(F);
return;
}
/**
* Get the header of the email passed
* @param email The message object
* @return The header of the email
*/
string build_header(Message* email) {
string text;
text = email->get_Header();
return text;
}
/**
* Copy the valid options for fuse_main in dest.
* @param argc the size of argv[]
* @param argv a vector which contains all the mount options
* @param dest a vector which the valid options will be copied
* @return number of options copied
*/
int copy_options(int argc, char* argv[], char* dest[]) {
int i=0, j=0;
while (i < argc) {
if (strcmp(argv[i], "-u")==0 || strcmp(argv[i], "-w")==0) {
i+=2;
continue;
}
dest[j] = argv[i];
i++;
j++;
}
return j;
}
/**
* Convert a list of folder names in a list of Folder objects
* @param folder_names A list<strings> with the folder names
* @param folder_root name of folder root
*/
void create_folder_list( list<string> folder_names, string folder_root) {
Folder *F;
list<string>::iterator it = folder_names.begin();
while( it != folder_names.end() )
{
if( *it != "\r" ) { // Because of the way the names were parsed, there's a \r in the empty name
F = new Folder( *it );
add_Folder(*F, folder_root);
}
it++;
}
return;
}
/**
* Convert a list of folder names in a list of Folder objects
* @param folder_names A list<strings> with the folder names
* @param folder_root name of folder root
*/
void create_folder_list_in_folder( list<string> folder_names, string folder_root, Folder* folder) {
Folder *F;
list<string>::iterator it = folder_names.begin();
while( it != folder_names.end() )
{
if( *it != "\r" ) { // Because of the way the names were parsed, there's a \r in the empty name
F = new Folder( *it );
add_Folder_in_folder(*F, folder_root, folder);
}
it++;
}
return;
}
/**
* If it has the -h option, the help will show
* @param argc the size of argv[]
* @param argv a vector which contains all the mount options
* @return 0 if there isn't -h option.
*/
int help_option(int argc, char *argv[]) {
for (int i = 0; i<argc; i++) {
if (strcmp(argv[i], "-h") == 0) {
cout << "Instruction manual: ./imapFuse PATH -u username -w password [-s server] [-p port] [-l true/false]" << endl;
cout << "Mount options:" << endl;
cout << " -u username : The user name of the email account" << endl;
cout << " -w pasword : The password of the email account" << endl;
cout << " -s server : The name of the email server. By default it uses imap.gmail.com" << endl;
cout << " -p port : The number of the port. By default it uses 993" << endl;
cout << " -l true/false : Use ssl encription. By default it uses true" << endl;
return 1;
}
}
return 0;
}
/**
* Gets the mount option specified in option.
* @param argc the size of argv[]
* @param argv a vector which contains all the mount options
* @param option the option which is looking for
* @param result the result of the searching
*/
void mount_options(int argc, char *argv[], char* option, char** result){
*result = NULL;
for (int i = 0; i<argc; i++) {
if (strcmp(argv[i], option) == 0) {
*result = argv[i+1];
break;
}
}
return;
}
/**
* Check if the path is the header or body file of an email
* @param path The path which must be checked
* @return The path without "header" or "body", or "" if it isn't an email file
*/
string pathEmail(char* path) {
string temp_path = path;
string name;
string email_path;
int lastbar;
lastbar = temp_path.find_last_of("/");
if (lastbar+1 == temp_path.length()) { // if the last character of path is a '/'
temp_path = temp_path.substr(0,temp_path.length() -1);
lastbar = temp_path.find_last_of("/");
}
// Copy the name of the email
name = temp_path.substr(lastbar+1);
// Copy the path to the email
if (temp_path[0] == '/') // if the first character of path is a '/'
email_path = temp_path.substr(1,lastbar-1);
else
email_path = temp_path.substr(0,lastbar);
if (((name.compare("header") == 0) || (name.compare("body") == 0)) && (search_mail((char*)email_path.c_str()) != NULL))
return email_path;
else
return "";
}
/**
* Search for a email in a folder
* @param path The path of the email.
* @return The email object.
*/
Message* search_mail(char * path) {
Message* email;
Folder* f;
string temp_path;
int lastbar;
int numMessages;
int i;
string folderOpenStatus;
string email_name;
string email_path;
char* temp = (char*) malloc(4);
string email_uid;
string email_from;
string email_subject;
string email_file;
temp_path = path;
lastbar = temp_path.find_last_of("/");
if (lastbar+1 == temp_path.length()) { // if the last character of path is a '/'
temp_path = temp_path.substr(0,temp_path.length() -1);
lastbar = temp_path.find_last_of("/");
}
// Copy the name of the email
email_name = temp_path.substr(lastbar+1);
// Copy the path to the email
if (temp_path[0] == '/') // if the first character of path is a '/'
email_path = temp_path.substr(1,lastbar-1);
else
email_path = temp_path.substr(0,lastbar);
// Now, we have the email path (in email_path) an the email name (in email_name)
// Look for the last folder in path
f = search_subfolder((char*) email_path.c_str());
if (f == NULL)
return NULL;
if (SelectFolder(connection, (char*) email_path.c_str(), numMessages, folderOpenStatus) == UNKNOWN_MAILBOX) {
free(temp);
return NULL;
}
i = GetNewMail(connection, *f);
if (i>BEG_FAIL || i==LIST_FAILED || i== FOLDER_NOT_EXIST) {
free(temp);
return NULL;
}
// Look for email in the folder
for (i=1;i<=numMessages;i++) {
email = f->get_Message(i);
// Get from address
email_from = email->get_From_Address();
email_from = trim_email(email_from);
// Get subject
email_subject = email->get_Subject();
email_subject = trimwhitespace((char*) email_subject.c_str());
// Get uid
sprintf(temp, "%d", email->get_UID());
email_uid = temp;
email_file = "";
email_file.append(email_uid);
email_file.append("-");
email_file.append(email_subject);
email_file.append(" - ");
email_file.append(email_from);
if (email_name.compare(email_file) == 0) {
free(temp);
return email;
}
}
free(temp);
return NULL;
}
/**
* Search for a folder in folder_list.
* @param name Name of folder.
* @return The folder object.
*/
Folder* search_folder(char* name) {
list<Folder>::iterator itera = folder_list.begin();
while(itera != folder_list.end()) {
if (strcmp(name, ((*itera).get_Folder_Name()).c_str()) == 0) {
return &(*itera);
}
itera++;
}
return NULL;
}
/**
* Search for a folder in other folder. It needs the complete path to look for it.
* @param name Name of folder.
* @return The folder object.
*/
Folder* search_subfolder(char* name) {
Folder* f = NULL;
char* pathtemp = (char*) malloc(strlen(name)+1);
char* pch;
strncpy(pathtemp, name, strlen(name)+1); // make a copy of name
pch = strtok(pathtemp,"/");
if (pch == NULL) {
free(pathtemp);
return NULL;
}
// Obtain parent folder
f = search_folder(pch);
if (f == NULL) {
free(pathtemp);
return NULL;
}
// Obtain subfolder
while (pch != NULL) {
pch = strtok(NULL, "/");
if (f == NULL) {
free(pathtemp);
return NULL;
}
if (pch == NULL) {
break;
}
f = search_subfolder_in_folder(pch, f);
}
free(pathtemp);
return f;
}
/**
* Search for a subfolder in a specified folder.
* @param name Name of folder.
* @param f folder object.
* @return The folder object.
*/
Folder* search_subfolder_in_folder(char* name, Folder* f) {
int i;
Folder* folder_temp;
string folder_name;
for (i=1; i<=f->get_Num_subFolders(); i++) { // Search in list of subfolders of the current folder
folder_temp = f->get_subFolder(i);
folder_name = folder_temp->get_Folder_Name();
if (strcmp((folder_name.c_str()), name) == 0)
return folder_temp;
}
return NULL;
}
/**
* Only trim the email of a string. The email must be among < and >.
* @param mailstring The string with the email.
* @return Only the email.
*/
string trim_email(string mailstring) {
string email;
size_t posini, posfin;
posini = mailstring.find_last_of("<");
posfin = mailstring.find_last_of(">");
if ((posfin <= posini) || (posini == string::npos) || (posfin == string::npos))
email = mailstring;
else
email = mailstring.substr(posini+1, (posfin-posini)-1);
if (email.find("@") == string::npos)
email = mailstring;
return email;
}
/**
* Trim white spaces
* @param str The string
* @return The string without whitespaces.
*/
char *trimwhitespace(char *str) {
char *end;
// Trim leading space
while(isspace(*str)) str++;
if(*str == 0) // All spaces?
return str;
// Trim trailing space
end = str + strlen(str) - 1;
while(end > str && isspace(*end)) end--;
// Write new null terminator
*(end+1) = 0;
return str;
}
// Imap Methods
/**
* Obtain a list of folders. If it is a subfolder, the function gets the folder list of each folder in the path. If that folder list already exists, refresh it.
* @param folder_root Name of folder root
* @param folder NULL if it is the folder root ("/") or a Folder* if it's a subfolder
* @return SUCCESS or LIST_FAILED
*/
int get_folders_list_from_server(string path, Folder* folder) {
int return_code;
list<string> folder_names;
int numMessages;
string folderOpenStatus;
if (folder == NULL) { // Create folder list in folder root
folder_names.clear();
if ((return_code = SelectFolder(connection, "", numMessages, folderOpenStatus)) != UNKNOWN_MAILBOX) {
return return_code;
}
return_code = getIMAPFolders(connection, folder_names, path);
if (return_code == SUCCESS) {
folder_list.clear();// empty folder_list
create_folder_list(folder_names, "");// obtain a new list
}
} else { // create folder list in a specified folder
char* pathtemp = (char*) malloc(strlen((char*)path.c_str()) + 1);
string pch;
string str;
string path2;
int posBar = 0;
bool salir = false;
str = path;
posBar = str.find_first_of("/");
if (posBar == -1) {
pch = str;
str = "";
} else {
pch = str.substr(0,posBar);
str.erase (0,posBar+1);
}
path2 = pch;
// Obtain de folder root list
folder_names.clear();
return_code = getIMAPFolders(connection, folder_names, ""); // get the folder names in folder root
if (return_code != SUCCESS)
return return_code;
folder_list.clear(); // empty folder_list
create_folder_list(folder_names, "");// obtain a new list
Folder* f = search_folder((char*)pch.c_str()); // gets the first folder in the folder hierarchy
while (!salir) {
folder_names.clear();
return_code = getIMAPFolders(connection, folder_names, path2); // get subfolder names list
f->clear_subfolders(); // empty subfolder_list
create_folder_list_in_folder(folder_names, path2, f); // get a new list
if (posBar == -1)
salir = true;
// Replace strtok
posBar = str.find_first_of("/");
if (posBar == -1) {
pch = str;
str = "";
} else {
pch = str.substr(0,posBar);
str.erase (0,posBar+1);
}
path2.append("/");
path2.append(pch);
f = search_subfolder((char*)path2.c_str()); // Obtain the folder object with the name stored in pch.
// (we pass the absolute path stored in "path2")
if (f == NULL) {
salir = true;
return_code = -1;
}
}
}
return return_code;
}
/**
* Connect with server and login
* @return 0 on success or -1 on failure
*/
int login(char* user, char* pass, char* server, char* port, char* ssl) {
if (server != NULL)
imap_server = server;
else
imap_server = "imap.gmail.com"; // Nombre servidor IMAP
if (port != NULL)
port_n = atoi(port);
else
port_n = 993; // 143 || 993 con encriptación SSL
if (ssl != NULL) {
int i = 0;
while (ssl[i] != '\0') {
cout << ssl[i] << " - ";
ssl[i] = tolower(ssl[i]);
cout << ssl[i] << endl;
i++;
}
if (strcmp(ssl, (char*)"true") == 0) {
usessl = true;
} else {
usessl = false;
}
} else
usessl = true;
int i = IMAPLogin(connection, imap_server, port_n, user, pass, usessl);
if (i == BAD_LOGIN) {
printf("Bad login: %d\n", i);
return -1;
}
if (i == SERVER_NOT_READY_FOR_CONNECTION) {
printf("Server not ready for connection\n");
return -1;
}
return 0;
}
/**
* Main
*/
int main(int argc, char *argv[]) {
FuseDispatcher *dispatcher;
char* help;
char* username;
char* password;
char* server;
char* port;
char* usessl;
int nOptions;
char* dest[50];
if (help_option(argc, argv) != 0)
return 0;
dispatcher = new FuseDispatcher();
dispatcher->set_getattr (&ImapFuse_getattr);
dispatcher->set_readdir (&ImapFuse_readdir);
dispatcher->set_open (&ImapFuse_open);
dispatcher->set_read (&ImapFuse_read);
// Mount options
mount_options(argc, argv, (char*)"-u", &username); // Get the username
mount_options(argc, argv, (char*)"-w", &password); // Get the password
if (username == NULL || password == NULL) {
cout << endl << "** -u and -w options are required!" << endl;
return -1;
}
mount_options(argc, argv, (char*)"-s", &server);
mount_options(argc, argv, (char*)"-p", &port);
mount_options(argc, argv, (char*)"-l", &usessl);
// Copy valid mount options
nOptions = copy_options(argc, argv, dest);
// Make log in
if (login(username, password, server, port, usessl) == -1) {
cout << endl << "** Login failed!" << endl;
return -1;
}
return fuse_main(nOptions, dest, (dispatcher->get_fuseOps()), NULL);
}