-
Notifications
You must be signed in to change notification settings - Fork 6
/
code_migration.Rmd
826 lines (536 loc) · 18.5 KB
/
code_migration.Rmd
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
---
title: "Migrating old code to the new mRpostman's syntax"
output:
rmarkdown::html_vignette:
toc: yes
vignette: >
%\VignetteIndexEntry{Migrating old code to the new mRpostman's syntax}
%\VignetteEngine{knitr::rmarkdown}
\usepackage[utf8]{inputenc}
---
<style>
.col2 {
columns: 2 100px; /* number of columns and width in pixels*/
-webkit-columns: 2 100px; /* chrome, safari */
-moz-columns: 2 100px; /* firefox */
}
<!-- .col3 { -->
<!-- columns: 3 100px; -->
<!-- -webkit-columns: 3 100px; -->
<!-- -moz-columns: 3 100px; -->
<!-- } -->
</style>
## Introduction
This vignette aims to provide an easy source of code migration examples so old `mRpostman` users can transition between the old version and the new format which implements an OO approach trough the use of R6 class and methods.
This is the summary of the main modifications in the package between versions `0.3.1` and `0.9.X`:
* All main functions, except `list_attachments` and the custom-search helper functions, now are methods of the R6 class `ImapConf`;
* The way the connection token is passed through functions (now methods) has changed. The connection handle is created inside `configure_imap()` (or `ImapCon$new()`) and modified with custom requests from the methods that trigger IMAP commands. As a consequence, the password is now hidden inside the curl handle C pointer, resulting in a more secure token chain. This resulted in changes in every request function. These functions (that are methods now) do not use `config_handle()` anymore, and a call to `curl::set_opt()` is made in every request function so that a custom request is supplied or replaced by a new one in the original handle.
* The "by" argument used in search and fetch functions was replaced by `use_uid`, which is a logical now with the default value set as `FALSE`. This is equivalent to the former `by = MSN` default configuration.
* all functions that returned `invisible(0L)` now return `invisible(TRUE)`
* Users that prefer a tidy approach, only need to apply the pipe `%>%` operator between search and fetch methods (or other complementary operations), in which messages' ids are carried from one level to another. The exposition pipe `%$%` is not necessary anymore.
In the following sections, we present some code migration examples between the old and the new version of `mRpostman`.
## 1) Configuring the IMAP connection
<div class="col2">
Previous code:
```{r, message = FALSE, eval=FALSE}
library(mRpostman)
# Outlook - Office 365
imapconf <- configure_imap(url="imaps://outlook.office365.com",
username="your_user@company.com",
password=rstudioapi::askForPassword())
# other mail providers that were tested: Gmail (imaps://imap.gmail.com),
# Hotmail ("imaps://imap-mail.outlook.com"), Yahoo (imaps://imap.mail.yahoo.com/),
# AOL (imaps://export.imap.aol.com/), Yandex (imaps://imap.yandex.com)
```
<br>
New format:
```{r, message = FALSE, eval=FALSE}
library(mRpostman)
# Outlook - Office 365
con <- configure_imap(url="imaps://outlook.office365.com",
username="your_user@company.com",
password=rstudioapi::askForPassword())
# alternative
con <- ImapCon$new(url="imaps://outlook.office365.com",
username="your_user@company.com",
password=rstudioapi::askForPassword())
```
</div>
Although the object that is created is different (now we have an R6 `ImapCon` class), the code to create a connection object is pretty much the same, but with an alternative that uses the "initialize" method of the R6 class.
Other useful options are `timeout_ms`, `verbose = TRUE`, and `buffersize`. Further 'curl' options related to IMAP functionalities can be passed to `configure_imap()`, depending on the libcurl version installed on the user's machine. See `curl::curl_options()`. In this new version, there is an important curl parameter exposed, `xoauth2_bearer`, which enables OAuth2.0 authentication. For more details, check the [_"IMAP OAuth2.0 authentication in mRpostman"_](https://allanvc.github.io/mRpostman/articles/xoauth2.0.html) vignette.
## 2) Listing Server Capabilities
<div class="col2">
Previous code:
```{r, message = FALSE, eval=FALSE}
imapconf %>%
list_server_capabilities()
```
New format:
```{r, message = FALSE, eval=FALSE}
con$list_server_capabilities()
```
</div>
## 3) Mailbox Commands
### 3.1) Listing mail folders
<div class="col2">
Previous code:
```{r, message = FALSE, eval=FALSE}
# Listing
imapconf %>%
list_mailboxes()
```
New format:
```{r, message = FALSE, eval=FALSE}
# Listing
con$list_folders()
```
</div>
### 3.2) Folder selection
<div class="col2">
Previous code:
```{r, message = FALSE, eval = FALSE}
imapconf %>%
select_mailbox(mbox = "INBOX") # only INBOX is case sensitive
```
New format:
```{r, message = FALSE, eval = FALSE}
con$select_folder(name = "INBOX")
```
</div>
### 3.3) Examining a mail folder
<div class="col2">
Previous code:
```{r, message = FALSE, eval=FALSE}
imapconf %>%
select_mailbox(mbox = "K-State") %>%
examine_mailbox()
```
New format:
```{r, message = FALSE, eval = FALSE}
con$select_folder(name = "K-State")
con$examine_folder()
```
</div>
In the new format, the previously selected folder is kept during the session unless you execute a new selection.
### 3.4) Renaming a mail folder
<div class="col2">
Previous code:
```{r, message = FALSE, eval=FALSE}
imapconf %>%
select_mailbox(mbox = "CRAN messages") %>%
rename_mailbox(new_name = "CRAN messages2") %>%
list_mailboxes() # and list again to check
```
<br>
<br>
New format:
```{r, message = FALSE, eval = FALSE}
con$select_folder(name = "CRAN messages")
con$rename_folder(new_name = "CRAN messages2")
con$list_mail_folders() # and list again to check
```
</div>
## 4) Single Search
All search functions now return only the message ids.
<div class="col2">
Previous code:
```{r, message = FALSE, eval=FALSE}
result <- imapconf %>%
select_mailbox(mbox = "K-State") %>%
search_before(date_char = "02-May-2019",
by = "UID",
flag = "UNANSWERED")
result$msg_id
```
<br>
<br>
<br>
<br>
New format:
```{r, message = FALSE, eval = FALSE}
con$select_folder(name = "K-State")
result <- con$search_before(date_char = "02-May-2019",
use_uid = TRUE,
flag = "UNANSWERED")
result
```
</div>
You can also **NEGATE** the statement to search for messages **NOT BEFORE a date**:
<div class="col2">
Previous code:
```{r, message = FALSE, eval=FALSE}
results <- imapconf %>%
select_mailbox(mbox = "K-State") %>%
search_before(date_char = "02-May-2019",
negate = TRUE,
by = "UID",
flag = "UNANSWERED",
esearch = TRUE)
results$msg_id
```
New format:
```{r, message = FALSE, eval = FALSE}
con$select_folder(name = "K-State")
result <- con$search_before(date_char = "02-May-2019",
use_uid = TRUE,
flag = "UNANSWERED",
negate = TRUE)
result
```
</div>
This pattern is applied to all SEARCH functions.
Special attention should be paid to the `search_string` and `search_flag` methods due to changes in the names and order of the arguments.
#### Search by string - example 1
<div class="col2">
Previous code:
```{r, message = FALSE, eval=FALSE}
results <- imapconf %>%
select_mailbox(mbox = "INBOX") %>%
search_string(section_or_field = "FROM", string = "hadley@rstudio.com",
negate = TRUE) # not FROM "hadley@rstudio.com"
results$msg_id
```
<br>
<br>
New format:
```{r, message = FALSE, eval=FALSE}
con$select_folder(name = "INBOX")
result <- con$search_string(expr = "hadley@rstudio.com",
where = "FROM",
negate = TRUE) # not FROM "hadley@rstudio.com"
result
```
</div>
#### Search by string - example 2
<div class="col2">
Previous code:
```{r, message = FALSE, eval=FALSE}
results <- imapconf %>%
select_mailbox(mbox = "K-State") %>%
search_string(section_or_field = "TEXT", string = "Dear Allan")
```
<br>
<br>
New format:
```{r, message = FALSE, eval=FALSE}
con$select_folder(name = "K-State")
result <- con$search_string(expr = "Dear Allan", where = "TEXT")
result
```
#### Search by flag
<div class="col2">
Previous code:
```{r, message = FALSE, eval = FALSE}
results <- imapconf %>%
select_mailbox(mbox = "INBOX") %>%
search_flag(flag = "RECENT", by = "UID")
```
<br>
<br>
<br>
New format:
```{r, message = FALSE, eval=FALSE}
con$select_folder(name = "INBOX")
result <- con$search_flag(expr = "RECENT", use_uid = TRUE)
result
```
</div>
Now you can check which flags are available in a mail folder with the `list_flags()` method. Also, you can pass multiple flags as the search criterion.
## 5) Custom Search
<div class="col2">
Previous code:
```{r, message = FALSE, eval=FALSE}
results <- imapconf %>%
select_mailbox(mbox = "INBOX") %>%
custom_search(custom_request =
AND(
string(section_or_field = "FROM", string = "@toronto"),
before(date_char = "12-Apr-2019")
)
)
results$msg_id
```
New format:
```{r, message = FALSE, eval=FALSE}
con$select_folder(name = "INBOX")
result <- con$search(custom_request =
AND(
string(expr = "@toronto", where = "FROM"),
before(date_char = "12-Apr-2019")
)
)
result
```
</div>
## 6) Fetch
### 6.1) Fetch body
<div class="col2">
Previous code:
```{r, message = FALSE, eval = FALSE}
results <- imapconf %>%
select_mailbox(mbox = "K-State") %>%
search_before(date_char = "10-Mar-2019", by = "UID") %$% #exposition pipe operator
fetch_full_msg(imapconf = imapconf, msg_id = msg_id,
by="UID", write_to_disk = TRUE, keep_in_mem = TRUE,
partial = "0.789")
```
New format:
```{r, message = FALSE, eval = FALSE}
con$select_folder(name = "K-State")
results <- con$search_before(date_char = "10-Mar-2019", use_uid = TRUE) %>% #regular pipe
con$fetch_body(use_uid = TRUE, write_to_disk = TRUE,
keep_in_mem = TRUE, partial = "0.789")
```
</div>
### 6.2) Fetch header
<div class="col2">
Previous code:
```{r, message = FALSE, eval=FALSE}
results <- imapconf %>%
select_mailbox(mbox = "K-State") %>%
search_since(date_char = "15-Aug-2019", by = "UID") %$% #exposition pipe operator
fetch_msg_header(imapconf = imapconf,
msg_id = msg_id,
fields = c("DATE", "SUBJECT"),
by = "UID")
```
New format:
```{r, message = FALSE, eval=FALSE}
con$select_folder(name = "K-State")
results <- con$search_since(date_char = "15-Aug-2019", use_uid = TRUE) %>% #regular pipe
con$fetch_header(use_uid = TRUE, fields = c("DATE", "SUBJECT"))
```
</div>
### 6.3) Fetch text
<div class="col2">
Previous code:
```{r, message = FALSE, eval = FALSE}
results <- imapconf %>%
select_mailbox(mbox = "INBOX") %>%
search_since(date_char = "17-Aug-2019", by = "UID") %$% #exposition pipe operator
fetch_msg_text(imapconf = imapconf, msg_id = msg_id, by = "UID", try_b64decode = TRUE)
```
New format:
```{r, message = FALSE, eval=FALSE}
con$select_folder(name = "INBOX")
results <- con$search_since(date_char = "17-Aug-2019", use_uid = TRUE) %>% #regular pipe
con$fetch_text(use_uid = TRUE, base64_decode = TRUE))
```
</div>
### 6.4) Fetch metadata
<div class="col2">
Previous code:
```{r, message = FALSE, eval = FALSE}
results <- imapconf %>%
select_mailbox(mbox = "INBOX") %>%
search_on(date_char = "10-May-2019", by = "UID") %$% #exposition pipe operator
fetch_msg_metadata(imapconf, msg_id = msg_id, by = "UID",
metadata = c("INTERNALDATE", "UID", "ENVELOPE"))
```
New format:
```{r, message = FALSE, eval=FALSE}
con$select_folder(name = "INBOX")
results <- con$search_since(date_char = "10-May-2019", use_uid = TRUE) %>% #regular pipe
con$fetch_text(use_uid = TRUE, attribute = c("INTERNALDATE", "UID", "ENVELOPE"))
```
</div>
You can check the available metadata attributes with `metadata_options()`.
## 7) Attachments
### 7.1) Attachments listing
<div class="col2">
Previous code:
```{r, message = FALSE, eval = FALSE}
imapconf %>%
select_mailbox(mbox = "INBOX") %>%
search_on(date_char = "23-Sep-2019") %$%
fetch_full_msg(imapconf, msg_id=msg_id) %>%
list_attachments()
```
<br>
<br>
New format:
```{r, message = FALSE, eval=FALSE}
con$select_folder(name = "INBOX")
results <- con$search_on(date_char = "23-Sep-2019") %>% #regular pipe
con$fetch_body() %>% # or fetch_text()
list_attachments()
```
</div>
Note that `list_attachment()` is an independent function. It is not a method of the `ImapCon` class that depends on the connection object.
The new version brings an alternative fetch method (`fetch_atachments_list()`) for listing attachments without the need of previously executing a `fetch_body` or `fetch_text` operation:
```{r, message = FALSE, eval=FALSE}
con$select_folder(name = "INBOX")
con$search_on(date_char = "23-Sep-2019") %>% #regular pipe
con$fetch_attachments_list()
```
### 7.2) Attachments Extraction
<div class="col2">
Previous code:
```{r, message = FALSE, eval = FALSE}
imapconf %>%
select_mailbox(mbox = "INBOX") %>%
search_on(date_char = "23-Sep-2019") %$%
fetch_full_msg(imapconf, msg_id=msg_id) %>%
get_attachments()
```
<br>
<br>
New format:
```{r, message = FALSE, eval=FALSE}
con$select_folder(name = "INBOX")
results <- con$search_on(date_char = "23-Sep-2019") %>% #regular pipe
con$fetch_body() %>% # or fetch_text()
get_attachments()
```
</div>
The new version brings an alternative fetch method (`fetch_atachments()`) for downloading attachments without the need of previously executing a `fetch_body` or `fetch_text` operation:
```{r, message = FALSE, eval=FALSE}
con$select_folder(name = "INBOX")
con$search_on(date_char = "23-Sep-2019") %>% #regular pipe
con$fetch_attachments()
```
## 8) Complementary Operations
### 8.1) Copy message(s)
<div class="col2">
Previous code:
```{r, message = FALSE, eval = FALSE}
results <- imapconf %>%
select_mailbox(mbox = "CRAN messages2") %>%
search_since(date_char = "10-May-2019") %$% #exposition pipe operator
copy_msg(imapconf = imapconf, msg_id = msg_id, to_mbox = "INBOX")
```
New format:
```{r, message = FALSE, eval = FALSE}
con$select_folder(name = "CRAN messages2")
results <- con$search_since(date_char = "10-May-2019") %>%
con$copy_msg(to_folder = "INBOX", reselect = FALSE)
```
</div>
### 8.2) Move message(s) (depends on MOVE capability)
<div class="col2">
Previous code:
```{r, message = FALSE, eval = FALSE}
results <- imapconf %>%
select_mailbox(mbox = "[Gmail]/Sent") %>%
search_before(date_char = "10-may-2012") %$% #exposition pipe operator
move_msg(imapconf = imapconf, msg_id = msg_id, to_mbox = "CRAN messages2")
```
New format:
```{r, message = FALSE, eval = FALSE}
con$select_folder(name = "[Gmail]/Sent")
con$search_before(date_char = "10-May-2012") %>%
con$move_msg(to_folder = "CRAN messages2", reselect = FALSE)
```
</div>
### 8.3) Search the minimum message ID with specific flag(s) (depends on ESEARCH capability)
<div class="col2">
Previous code:
```{r, message = FALSE, eval = FALSE}
results <- imapconf %>%
select_mailbox(mbox = "[GMail]/Trash") %>%
get_min_id(flag = "UNSEEN")
```
New format:
```{r, message = FALSE, eval = FALSE}
con$select_folder(name = "[GMail]/Trash")
con$esearch_min_id(flag = "UNSEEN")
```
</div>
### 8.4) Search the maximum message ID with specific flag(s) (depends on ESEARCH capability)
<div class="col2">
Previous code:
```{r, message = FALSE, eval = FALSE}
results <- imapconf %>%
select_mailbox(mbox = "[Gmail]/Trash") %>%
get_max_id(flag = "UNSEEN")
```
New format:
```{r, message = FALSE, eval = FALSE}
con$select_folder(name = "[GMail]/Trash")
con$esearch_max_id(flag = "UNSEEN")
```
</div>
### 8.5) Delete message(s)
<div class="col2">
Previous code:
```{r, message = FALSE, eval = FALSE}
results <- imapconf %>%
select_mailbox(mbox = "INBOX") %>%
delete_msg(msg_id = 66128)
```
New format:
```{r, message = FALSE, eval = FALSE}
con$select_folder(name = "INBOX")
con$delete_msg(msg_id = 66128)
```
</div>
### 8.6) Expunge
<div class="col2">
Previous code:
```{r, message = FALSE, eval = FALSE}
results <- imapconf %>%
select_mailbox(mbox = "[Gmail]/Trash") %>%
delete_msg(msg_id = 71772, by = "UID") %$%
expunge(imapconf = imapconf, specific_UID = msg_id)
```
<br>
<br>
<br>
New format:
```{r, message = FALSE, eval = FALSE}
con$select_folder(name = "[GMail]/Trash")
con$delete_msg(msg_id = 71772, use_uid = TRUE) %>%
con$expunge() # expunge the specific UID
# or, if the user wants to expunge the entire mail folder:
con$expunge()
```
</div>
### 8.7) Add/Remove/Replace flags
#### 8.7.1) Add flags
<div class="col2">
Previous code:
```{r, message = FALSE, eval = FALSE}
results <- imapconf %>%
select_mailbox(mbox = "INBOX") %>%
search_since(date_char = "18-Aug-2020", by = "UID") %$% #exposition pipe operator
add_flags(imapconf = imapconf, msg_id = msg_id, flags_to_set = "\\Seen")
```
New format:
```{r, message = FALSE, eval = FALSE}
con$select_folder(name = "INBOX")
con$search_since(date_char = "18-Aug-2020", use_uid = TRUE) %>%
con$add_flags(use_uid = TRUE, flags_to_set = "\\Seen") # system flags need the "\\" prefix
```
</div>
#### 8.7.2) Remove flags
<div class="col2">
Previous code:
```{r, message = FALSE, eval = FALSE}
results <- imapconf %>%
select_mailbox(mbox = "INBOX") %>%
search_since(date_char = "18-Aug-2020", by = "UID") %$% #exposition pipe operator
remove_flags(imapconf = imapconf, msg_id = msg_id, flags_to_unset = "\\Seen")
```
New format:
```{r, message = FALSE, eval = FALSE}
con$select_folder(name = "INBOX")
con$search_since(date_char = "18-Aug-2020", use_uid = TRUE) %>%
con$remove_flags(use_uid = TRUE, flags_to_unset = "\\Seen") # system flags need the "\\" prefix
```
</div>
#### 8.7.3) Replace flags
<div class="col2">
Previous code:
```{r, message = FALSE, eval = FALSE}
results <- imapconf %>%
select_mailbox(mbox = "INBOX") %>%
search_since(date_char = "18-Aug-2020", by = "UID") %$% #exposition pipe operator
replace_flags(imapconf = imapconf, msg_id = msg_id, flags_to_set = "\\Seen")
```
New format:
```{r, message = FALSE, eval = FALSE}
con$select_folder(name = "INBOX")
con$search_since(date_char = "18-Aug-2020", use_uid = TRUE) %>%
con$replace_flags(use_uid = TRUE, flags_to_set = "\\Seen") # system flags need the "\\" prefix
```
</div>