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.
@@ -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