mail_room 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +2 -0
- data/README.md +47 -18
- data/Rakefile +5 -0
- data/bin/mail_room +1 -1
- data/lib/mail_room/cli.rb +7 -89
- data/lib/mail_room/configuration.rb +6 -9
- data/lib/mail_room/coordinator.rb +13 -5
- data/lib/mail_room/delivery/letter_opener.rb +17 -0
- data/lib/mail_room/delivery/logger.rb +20 -0
- data/lib/mail_room/delivery/noop.rb +11 -0
- data/lib/mail_room/delivery/postback.rb +23 -0
- data/lib/mail_room/mailbox.rb +30 -1
- data/lib/mail_room/mailbox_handler.rb +36 -0
- data/lib/mail_room/mailbox_watcher.rb +93 -0
- data/lib/mail_room/version.rb +1 -1
- data/lib/mail_room.rb +5 -3
- data/mail_room.gemspec +12 -3
- data/spec/fixtures/test_config.yml +14 -0
- data/spec/lib/cli_spec.rb +56 -0
- data/spec/lib/configuration_spec.rb +25 -0
- data/spec/lib/coordinator_spec.rb +43 -0
- data/spec/lib/delivery/letter_opener_spec.rb +29 -0
- data/spec/lib/delivery/logger_spec.rb +46 -0
- data/spec/lib/delivery/postback_spec.rb +31 -0
- data/spec/lib/mailbox_handler_spec.rb +35 -0
- data/spec/lib/mailbox_spec.rb +53 -0
- data/spec/lib/mailbox_watcher_spec.rb +228 -0
- data/spec/spec_helper.rb +23 -0
- metadata +149 -8
- data/lib/mail_room/message_handler.rb +0 -125
@@ -1,125 +0,0 @@
|
|
1
|
-
module MailRoom
|
2
|
-
class MessageHandler
|
3
|
-
include Celluloid
|
4
|
-
|
5
|
-
def initialize(mailbox)
|
6
|
-
@mailbox = mailbox
|
7
|
-
@imap = Net::IMAP.new('imap.gmail.com', :port => 993, :ssl => true)
|
8
|
-
|
9
|
-
@running = false
|
10
|
-
@logged_in = false
|
11
|
-
@idling = false
|
12
|
-
end
|
13
|
-
|
14
|
-
def running?
|
15
|
-
@running
|
16
|
-
end
|
17
|
-
|
18
|
-
def logged_in?
|
19
|
-
@logged_in
|
20
|
-
end
|
21
|
-
|
22
|
-
def idling?
|
23
|
-
@idling
|
24
|
-
end
|
25
|
-
|
26
|
-
def run
|
27
|
-
setup
|
28
|
-
|
29
|
-
@running = true
|
30
|
-
|
31
|
-
@idling_thread = Thread.start do
|
32
|
-
while(running?) do
|
33
|
-
idle
|
34
|
-
fetch_new_messages
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def setup
|
40
|
-
log_in
|
41
|
-
examine_mailbox
|
42
|
-
end
|
43
|
-
|
44
|
-
def log_in
|
45
|
-
@imap.login(@mailbox.email, @mailbox.password)
|
46
|
-
# @imap.authenticate('XOAUTH2', @mailbox["email"], @mailbox["token"])
|
47
|
-
@logged_in = true
|
48
|
-
end
|
49
|
-
|
50
|
-
def examine_mailbox
|
51
|
-
return unless logged_in?
|
52
|
-
|
53
|
-
@imap.select(@mailbox.name)
|
54
|
-
end
|
55
|
-
|
56
|
-
def fetch_new_messages
|
57
|
-
return if idling? || !running?
|
58
|
-
|
59
|
-
new_messages.each do |msg|
|
60
|
-
puts msg.attr['RFC822']
|
61
|
-
# post_message(msg)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
def new_messages
|
66
|
-
messages_for_ids(new_message_ids)
|
67
|
-
end
|
68
|
-
|
69
|
-
# def label_message_with(id, lbl)
|
70
|
-
# in_current_fiber do |f|
|
71
|
-
# @imap.store(id, "+X-GM-LABELS", [lbl]).errback {f.resume}.callback {f.resume}
|
72
|
-
# end
|
73
|
-
# end
|
74
|
-
|
75
|
-
def new_message_ids
|
76
|
-
@imap.search('UNSEEN')
|
77
|
-
end
|
78
|
-
|
79
|
-
def messages_for_ids(ids)
|
80
|
-
return [] if ids.empty?
|
81
|
-
|
82
|
-
@imap.fetch(ids, "RFC822")
|
83
|
-
end
|
84
|
-
|
85
|
-
def post_message(msg)
|
86
|
-
# connection = Faraday.new
|
87
|
-
# connection.token_auth @mailbox["delivery_token"]
|
88
|
-
|
89
|
-
# connection.post do |request|
|
90
|
-
# request.url @mailbox["delivery_url"]
|
91
|
-
# request.options[:timeout] = 3
|
92
|
-
# request.headers['Content-Type'] = 'application/json'
|
93
|
-
# request.body = msg
|
94
|
-
# end
|
95
|
-
end
|
96
|
-
|
97
|
-
def idle
|
98
|
-
return unless logged_in?
|
99
|
-
|
100
|
-
@idling = true
|
101
|
-
|
102
|
-
@imap.idle do |response|
|
103
|
-
if response.respond_to?(:name) && response.name == 'EXISTS'
|
104
|
-
@imap.idle_done
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
@idling = false
|
109
|
-
end
|
110
|
-
|
111
|
-
def stop_idling
|
112
|
-
return unless idling?
|
113
|
-
|
114
|
-
@imap.idle_done
|
115
|
-
@idling_thread.join
|
116
|
-
@idling = false
|
117
|
-
end
|
118
|
-
|
119
|
-
def quit
|
120
|
-
@running = false
|
121
|
-
puts "Quitting"
|
122
|
-
stop_idling
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|