This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
forked from ConradIrwin/em-imap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.rb
150 lines (116 loc) · 3.59 KB
/
client.rb
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
module EventMachine
module Imap
class Client
include Imap::Authenticators
def initialize(connection)
@connection = connection
end
def disconnect
@connection.close_connection
end
## 6.1 Client Commands - Any State.
def capability
one_data_response("CAPABILITY")
end
def noop
tagged_response("NOOP")
end
# Logout and close the connection.
def logout
tagged_response("LOGOUT").errback do |e|
if e.is_a? Net::IMAP::ByeResponseError
# RFC 3501 says the server MUST send a BYE response and then close the connection.
disconnect
succeed
end
end.callback do |response|
fail Net::IMAP::ResponseParseError.new("Received the wrong response to LOGOUT: #{response}")
end
end
## 6.2 Client Commands - Not Authenticated State
def starttls
raise NotImplementedError
end
def authenticate(auth_type, *args)
auth_type = auth_type.upcase
auth_handler = authenticator(auth_type, *args)
tagged_response('AUTHENTICATE', auth_type).tap do |command|
@connection.send_authentication_data(auth_handler, command)
end
end
def login(username, password)
tagged_response("LOGIN", username, password)
end
## 6.3 Client Commands - Authenticated State
# TODO: Figure out API for EXISTS, RECENT, etc.
def select(mailbox)
tagged_response("SELECT", mailbox)
end
def examine(mailbox)
tagged_response("EXAMINE", mailbox)
end
def create(mailbox)
tagged_response("CREATE", mailbox)
end
def delete(mailbox)
tagged_response("DELETE", mailbox)
end
def rename(mailbox, newname)
tagged_response("RENAME", mailbox, newname)
end
def subscribe(mailbox)
tagged_response("SUBSCRIBE", mailbox)
end
def unsubscribe(mailbox)
tagged_response("UNSUBSCRIBE", mailbox)
end
def list(refname, mailbox)
multi_data_response("LIST", refname, mailbox)
end
def lsub(refname, mailbox)
multi_data_response("LSUB", rename, mailbox)
end
def status(mailbox, attr)
# FIXME: Why is this transform needed?
one_data_response("STATUS", mailbox, attr).transform do |response|
response.attr
end
end
def append(mailbox, message, flags=nil, date_time=nil)
args = [mailbox]
args << flags if flags
args << date_time if date_time
args << Net::IMAP::Literal.new(message)
tagged_response("APPEND" *args)
end
# 6.4 Client Commands - Selected State
def check
tagged_response("CHECK")
end
def close
tagged_response("CLOSE")
end
def expunge
tagged_response("EXPUNGE")
end
def search(keys, charset)
end
private
# The callback of a Command returns both a tagged response,
# and optionally a list of untagged responses that were
# generated at the same time.
def tagged_response(*command)
send_command(*command).transform{ |response, data| response }
end
def one_data_response(*command)
send_command(*command).transform{ |response, data| data.last }
end
def multi_data_response(*command)
send_command(*command).transform{ |response, data| data }
end
def send_command(cmd, *args)
@connection.send_command(cmd, *args)
end
end
end
end