-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathgmailr.R
364 lines (305 loc) · 9.64 KB
/
gmailr.R
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
#' \pkg{gmailr} makes gmail access easy.
#'
#' \code{gmailr} provides an interface to the gmail api \url{https://developers.google.com/gmail/api/}
#' @docType package
#' @name gmailr
#' @import httr
#' @import base64enc
NULL
#' Pipe statements
#'
#' Like dplyr and ggvis gmailr also uses the pipe function, \code{\%>\%} to turn
#' function composition into a series of imperative statements.
#'
#' @importFrom magrittr %>%
#' @name %>%
#' @rdname pipe
#' @export
#' @param lhs,rhs A visualisation and a function to apply to it
#' @examples
#' # Instead of
#' to(mime(), 'someone@@somewhere.com')
#' # you can write
#' mime() %>% to('someone@@somewhere.com')
NULL
the <- new.env(parent = emptyenv())
the$id <- "955034766742-huv7d1b1euegvk5vfmfq7v83u4rpdqb0.apps.googleusercontent.com"
the$secret <- "rpJPeEMnDOh7qNAVjUh_aKlO"
get_token <- function() {
if(!exists("token", the)){
gmail_auth()
}
the$token
}
#' Clear the current oauth token
#' @export
clear_token <- function() {
unlink(".httr-oauth")
the$token <- NULL
}
#' Setup oauth authentication for your gmail
#'
#' @param scope the authentication scope to use
#' @param id the client_id to use for authentication
#' @param secret the client secret to use for authentication
#' @param secret_file the secret json file downloaded from \url{https://console.cloud.google.com}
#' @seealso use_secret_file to set the default id and secret to a different
#' value than the default.
#' @export
#' @examples
#' \dontrun{
#' gmail_auth("compose")
#' }
gmail_auth <- function(scope=c("read_only", "modify", "compose", "full"),
id = the$id,
secret = the$secret,
secret_file = NULL) {
if(!is.null(secret_file)){
if (!(missing(id) && missing(secret))) {
stop("You should set either ", sQuote("secret_file"), " or ",
sQuote("id"), " and ", sQuote("secret"), ", not both",
call. = FALSE)
}
use_secret_file(secret_file)
# Use new ID and secret
id <- the$id
secret <- the$secret
}
myapp <- oauth_app("google", id, secret)
scope_urls <- c(read_only = "https://www.googleapis.com/auth/gmail.readonly",
modify = "https://www.googleapis.com/auth/gmail.modify",
compose = "https://www.googleapis.com/auth/gmail.compose",
full = "https://mail.google.com/")
scope <- scope_urls[match.arg(scope, several.ok=TRUE)]
the$token <- oauth2.0_token(oauth_endpoints("google"), myapp, scope = scope)
}
#' Use information from a secret file
#'
#' This function sets the default secret and client_id to those in the secret
#' file
#' @param filename the filename of the file
#' @export
use_secret_file <- function(filename) {
info <- jsonlite::fromJSON(readChar(filename, nchars=1e5))
the$secret <- info$installed$client_secret
the$id <- info$installed$client_id
}
#' Get the body text of a message or draft
#' @param x the object from which to retrieve the body
#' @param ... other parameters passed to methods
#' @export
#' @examples
#' \dontrun{
#' body(my_message)
#' body(my_draft)
#' }
body <- function(x, ...) UseMethod("body")
#' @export
body.gmail_message <- function(x, type="text/plain", collapse = FALSE, ...){
is_multipart <- !is.null(x$payload$parts)
if (is_multipart) {
if (is.null(type)){
good_parts <- TRUE
} else {
good_parts <- vapply(x$payload$parts, FUN.VALUE = logical(1),
function(part) {
any(
vapply(part$headers, FUN.VALUE = logical(1),
function(header) {
tolower(header$name) %==% "content-type" &&
grepl(type, header$value, ignore.case = TRUE)
})
)
})
}
res <-
lapply(x$payload$parts[good_parts],
function(x){
base64url_decode_to_char(x$body$data)
})
} else { # non_multipart
res <- base64url_decode_to_char(x$payload$body$data)
}
if (collapse){
res <- paste0(collapse = "\n", res)
}
res
}
#' @export
body.gmail_draft <- function(x, ...){ body.gmail_message(x$message, ...) }
#' Get the id of a gmailr object
#' @param x the object from which to retrieve the id
#' @param ... other parameters passed to methods
#' @export
#' @examples
#' \dontrun{
#' id(my_message)
#' id(my_draft)
#' }
id <- function(x, ...) UseMethod("id")
#' @export
id.gmail_message <- function(x, ...) { x$id }
#' @export
id.gmail_thread <- id.gmail_message
#' @export
id.gmail_draft <- id.gmail_message
#' @rdname id
#' @export
#' @inheritParams id
#' @param what the type of id to return
id.gmail_messages <- function(x, what=c("message_id", "thread_id"), ...){
what <- switch(match.arg(what),
message_id = "id",
thread_id = "threadId"
)
unlist(lapply(x, function(page) { vapply(page$messages, "[[", character(1), what) }))
}
#' @export
id.gmail_drafts <- function(x, what=c("draft_id", "message_id", "thread_id"), ...){
what <- switch(match.arg(what),
draft_id = return(
unlist(lapply(x, function(page) { vapply(page$drafts, "[[", character(1), "id")}))
),
message_id = "id",
thread_id = "threadId"
)
unlist(lapply(x, function(page) { vapply(page$drafts, function(x){ x$message[[what]] }, character(1)) }))
}
#' @export
id.gmail_threads <- function(x, ...){
unlist(lapply(x, function(page) { vapply(page$threads, "[[", character(1), "id") }))
}
#' Methods to get values from message or drafts
#' @param x the object from which to get or set the field
#' @param ... other parameters passed to methods
#' @rdname accessors
#' @export
to <- function(x, ...) UseMethod("to")
#' @export
to.gmail_message <- function(x, ...){ header_value(x, "To") }
#' @export
to.gmail_draft <- function(x, ...){ to.gmail_message(x$message, ...) }
#' @rdname accessors
#' @export
from <- function(x, ...) UseMethod("from")
#' @export
from.gmail_message <- function(x, ...){ header_value(x, "From") }
#' @export
from.gmail_draft <- from.gmail_message
#' @export
from <- function(x, ...) UseMethod("from")
#' @rdname accessors
#' @export
cc <- function(x, ...) UseMethod("cc")
#' @export
cc.gmail_message <- function(x, ...){ header_value(x, "Cc") }
#' @export
cc.gmail_draft <- function(x, ...){ from.gmail_message(x$message, ...) }
#' @rdname accessors
#' @export
bcc <- function(x, ...) UseMethod("bcc")
#' @export
bcc.gmail_message <- function(x, ...){ header_value(x, "Bcc") }
#' @export
bcc.gmail_draft <- function(x, ...){ from.gmail_message(x$message, ...) }
#' @rdname accessors
#' @export
date <- function(x, ...) UseMethod("date")
#' @export
date.default <- function(x, ...) { base::date() }
#' @export
date.gmail_message <- function(x, ...){ header_value(x, "Date") }
#' @export
date.gmail_draft <- function(x, ...){ date.gmail_message(x$message, ...) }
#' @rdname accessors
#' @export
subject <- function(x, ...) UseMethod("subject")
#' @export
subject.gmail_message <- function(x, ...) { header_value(x, "Subject") }
#' @export
subject.gmail_draft <- function(x, ...){ subject.gmail_message(x$message, ...) }
header_value <- function(x, name){
Find(function(header) identical(header$name, name), x$payload$headers)$value
}
#' @export
print.gmail_message <- function(x, ...){
to <- to(x)
from <- from(x)
date <- date(x)
subject <- subject(x)
id <- id(x)
cat(p(
crayon::bold("Id: "), id, "\n",
crayon::bold("To: "), to, "\n",
crayon::bold("From: "), from, "\n",
crayon::bold("Date: "), date, "\n",
crayon::bold("Subject: "), subject, "\n",
body(x, collapse = TRUE)), "\n")
}
#' @export
print.gmail_thread <- function(x, ...){
id <- id(x)
cat(strwrap(p(crayon::bold("Thread Id: "), id, "\n")), "\n")
}
#' @export
print.gmail_draft <- function(x, ...){
id <- id(x)
cat(strwrap(p(crayon::bold("Draft Id: "), id, "\n")), "\n")
print(x$message, ...)
}
#' @export
print.gmail_messages <- function(x, ...){
message_ids <- id(x, "message_id")
thread_ids <- id(x, "thread_id")
print(format(data.frame(message_id=message_ids, thread_id=thread_ids)), ...)
}
#' @export
print.gmail_threads <- function(x, ...){
thread_ids <- id(x)
snippets <- unlist(lapply(x, function(page) { vapply(page$threads, "[[", character(1), "snippet") }))
print(format(data.frame(thread_id=thread_ids, snippet=snippets)), ...)
}
#' @export
print.gmail_drafts <- function(x, ...){
draft_ids <- id(x, "draft_id")
message_ids <- id(x, "message_id")
thread_ids <- id(x, "thread_id")
print(format(data.frame(draft_ids, message_id=message_ids, thread_id=thread_ids)), ...)
}
the$last_response <- list()
gmailr_query <- function(fun, location, user_id, class = NULL, ...,
upload = FALSE) {
path_fun <- if (upload) gmail_upload_path else gmail_path
response <- fun(path_fun(user_id, location),
config(token = get_token()), ...)
result <- content(response, "parsed")
the$last_response <- response
if (status_code(response) >= 300) {
cond <- structure(list(
call = sys.call(-1),
content = result,
response = response,
message = paste0("Gmail API error: ", status_code(response), "\n ", result$error$message, "\n")),
class = c("condition", "error", "gmailr_error"))
stop(cond)
}
if (!is.null(class) && !is.null(result)) {
class(result) <- class
}
result
}
#' Response from the last query
#'
#' @export
last_response <- function() {
the$last_response
}
gmailr_POST <- function(location, user_id, class = NULL, ...) {
gmailr_query(POST, location, user_id, class, ...)
}
gmailr_GET <- function(location, user_id, class = NULL, ...) {
gmailr_query(GET, location, user_id, class, ...)
}
gmailr_DELETE <- function(location, user_id, class = NULL, ...) {
gmailr_query(DELETE, location, user_id, class, ...)
}