mail_daemon 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9ecde2f676af4e347e1b2bb397cb159f90c3ff32
4
- data.tar.gz: 31cfa71d1badd6db7ede971d9dc8d81b9a2ad806
3
+ metadata.gz: 684e8ea1da51fa99792aadf676aa68154e371161
4
+ data.tar.gz: 8ea4c2ea49a4b4b3601ea21ed591123509e03990
5
5
  SHA512:
6
- metadata.gz: df0bbced18789453cab87582caf191167b05f512078a2e415456f4d0026eec91fc65de4342a8cef125425e210f55230b9aca4976ce716a8cca6a3d8ab8032ce7
7
- data.tar.gz: 72dd6f014d8b61917d575f572f95774db97651d1d9f9943fc33f9ae8275f4bc88b680bc849b2113592c3c902bf0abff127ca5855e8f3aa34fc3c63708ddd95e0
6
+ metadata.gz: ecc74948196e37b832a972f6304928a5bbc0783a63785b6e1a1579fe4f7e2b81a9ee303bb75d63a08bd3339911100e179c562c6a079d00e87eb475196a40cac7
7
+ data.tar.gz: cbac2d2ec474d95ef29d2cebc9f2d53a7a817f854a2a99e3214cd9c15a075eca04d72e52805cd4eb4fde3c57efba8ba123d1d9abaf2b1af996ae36d0e10ebef1
@@ -19,7 +19,7 @@ module MailDaemon
19
19
  if exit_code == 0
20
20
  result
21
21
  else
22
- puts result
22
+ puts result if @options[:debug]
23
23
  raise error
24
24
  end
25
25
  end
@@ -10,7 +10,7 @@ module MailDaemon
10
10
  @options = options
11
11
  @email = Mail.read_from_string(options[:inbound_message])
12
12
 
13
- puts "Handling inbound email from #{@email.from}"
13
+ puts "Handling inbound email from #{@email.from}" if @options[:debug]
14
14
 
15
15
  if @email.multipart?
16
16
  @email.parts.each do |part|
@@ -58,8 +58,7 @@ module MailDaemon
58
58
  puts "Selecting folder '#{@options[:folder]}'" if @options[:debug]
59
59
  @imap.select(@options[:folder])
60
60
 
61
- # puts "starting tls"
62
- # @imap.starttls({}, verify=false) if @options[:start_tls]
61
+ @imap.starttls({}, verify=false) if @options[:start_tls]
63
62
 
64
63
  @idle_required = true
65
64
  puts "Logged on." if @options[:debug]
@@ -87,6 +86,7 @@ module MailDaemon
87
86
  @imap.idle_done
88
87
  rescue => e
89
88
  puts e.message
89
+ puts e.backtrace.join("\n")
90
90
  end
91
91
  end
92
92
 
@@ -100,6 +100,7 @@ module MailDaemon
100
100
  end
101
101
  rescue => e
102
102
  puts e.message
103
+ puts e.backtrace.join("\n")
103
104
  end
104
105
  end
105
106
  recycle_thread.kill
@@ -1,3 +1,3 @@
1
1
  module MailDaemon
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
data/lib/mail_daemon.rb CHANGED
@@ -62,7 +62,7 @@ module MailDaemon
62
62
  @options[:connections].each do |mailbox|
63
63
  mailbox[:ssl_options] = {:verify_mode => OpenSSL::SSL::VERIFY_NONE} if mailbox[:ssl]
64
64
 
65
- puts "Setting up watcher for #{mailbox[:username]}"
65
+ puts "Setting up watcher for #{mailbox[:username]}" if @options[:debug]
66
66
  @watchers << MailDaemon::Imap::Watcher.new(mailbox.merge(:debug => @options[:debug]))
67
67
  end
68
68
  @threads = []
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mail_daemon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - EmergeAdapt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-14 00:00:00.000000000 Z
11
+ date: 2016-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -73,7 +73,6 @@ files:
73
73
  - lib/mail_daemon.rb
74
74
  - lib/mail_daemon/email_body_parser.rb
75
75
  - lib/mail_daemon/email_handler.rb
76
- - lib/mail_daemon/email_watcher.rb
77
76
  - lib/mail_daemon/encryption.rb
78
77
  - lib/mail_daemon/helpers.rb
79
78
  - lib/mail_daemon/imap/connection.rb
@@ -1,155 +0,0 @@
1
- require 'redis'
2
- require 'ap'
3
- require 'mysql2'
4
-
5
- module MailDaemon
6
- class EmailWatcher
7
-
8
- def initialize
9
-
10
- raise "REDIS_URL environment variable is required (eg redis://localhost:6739)" unless ENV["REDIS_URL"]
11
- raise "MYSQL_HOST environment variable is required" unless ENV["MYSQL_HOST"]
12
- raise "MYSQL_DATABASE environment variable is required" unless ENV["MYSQL_DATABASE"]
13
- raise "MYSQL_USERNAME environment variable is required" unless ENV["MYSQL_USERNAME"]
14
- raise "MYSQL_PASSWORD environment variable is required" unless ENV["MYSQL_PASSWORD"]
15
-
16
- ENV["MYSQL_PASSWORD"] = "" unless ENV["MYSQL_PASSWORD"]
17
- ENV["MYSQL_PORT"] = "3306"
18
-
19
- redis_url = URI.parse(ENV["REDIS_URL"])
20
-
21
- $redis = Redis.new(:host => redis_url.host, :port => redis_url.port)
22
-
23
- Signal.trap("INT") {
24
- Thread.new {self.stop}.join
25
- }
26
- # Trap `Kill `
27
- Signal.trap("TERM") {
28
- Thread.new {self.stop}.join
29
- }
30
-
31
-
32
-
33
- start
34
- end
35
-
36
- def configuration
37
- mailboxes = []
38
- mysql_client do |mysql|
39
- statement = mysql.prepare("SELECT case_blocks_email_accounts.id, case_blocks_accounts.nickname, case_blocks_email_accounts.imap_username, case_blocks_email_accounts.imap_encrypted_password, case_blocks_email_accounts.imap_host, case_blocks_email_accounts.imap_port, case_blocks_email_accounts.imap_ssl, case_blocks_email_accounts.imap_start_tls, case_blocks_email_accounts.imap_folder_name, case_blocks_email_accounts.imap_search_command, case_blocks_email_accounts.imap_messages_processed, case_blocks_email_accounts.imap_last_processed_at FROM case_blocks_email_accounts JOIN case_blocks_accounts ON case_blocks_email_accounts.account_id = case_blocks_accounts.id where imap_enabled=1")
40
- result = statement.execute()
41
- result.each do |row|
42
- ssl_options = row["imap_ssl"]==1 ? {:verify_mode => OpenSSL::SSL::VERIFY_NONE} : false
43
- decrypted_password = Encryption.new.decrypt(row["imap_encrypted_password"])
44
- mailboxes << {:id => row["id"], :account_code => row["nickname"], :username => row["imap_username"], :host => row["imap_host"], :port => row["imap_port"], :password => decrypted_password, :ssl_options => ssl_options, :start_tls => row["imap_start_tls"]==1, :name => row["imap_folder_nam"], :search_command => row["imap_search_command"], :message_count => row["imap_messages_processed"], :last_delivered_at => row["imap_last_processed_at"]}
45
- end
46
- end
47
- mailboxes
48
- end
49
-
50
- def restart
51
- self.stop if self.running?
52
- self.reload unless @mailbox_config
53
- self.start
54
- end
55
-
56
- def running?
57
- @watchers.detect{|watcher| watcher.running?}
58
- end
59
-
60
- # Signal catching
61
- def stop
62
- @watchers.map{|watcher| watcher.stop}
63
- @threads.map{|t| t.kill }
64
- end
65
-
66
- def start
67
- @watchers = []
68
-
69
- configuration.each do |mailbox|
70
- puts "Setting up watcher for #{mailbox[:username]}"
71
- @watchers << MailDaemon::Imap::Watcher.new(mailbox)
72
- end
73
-
74
- @threads = []
75
- @watchers.each do |watcher|
76
- @threads << Thread.new do
77
- watcher.start do |message|
78
- if message[:type] == "status_update"
79
- update_status(message)
80
- elsif message[:type] == "incoming_email"
81
- handle_incoming_message(message)
82
- else
83
- puts "Unknown message type"
84
- ap message
85
- end
86
- end
87
- end
88
- end
89
- @threads.map{|t| t.join}
90
-
91
- end
92
-
93
- def handle_incoming_message(options)
94
- @handler = MailDaemon::EmailHandler.new(options)
95
- @handler.sanitize!
96
- @handler.clean_replies!
97
-
98
- hash = @handler.generate_message_hash
99
-
100
- $redis.lpush(redis_key(options), hash)
101
- puts "Queued."
102
- end
103
-
104
- def redis_key(options)
105
- "cb:comms:#{options[:mailbox][:account_code]}:inbox"
106
- end
107
-
108
- def update_status(options)
109
- puts "Status Update: #{options[:status]} for #{options[:mailbox][:username]}"
110
- mysql_client do |mysql|
111
- status_message = options[:status]
112
- statement = mysql.prepare("UPDATE case_blocks_email_accounts SET imap_status='#{options[:status]}', imap_status_message='#{status_message}' where id=?")
113
- statement.execute(options[:mailbox][:id])
114
- end
115
- end
116
-
117
- private
118
-
119
- def mysql_client(&block)
120
- client = Mysql2::Client.new(:host => ENV["MYSQL_HOST"], :port => ENV["MYSQL_PORT"], :username => ENV["MYSQL_USERNAME"], :password => ENV["MYSQL_PASSWORD"], :database => ENV["MYSQL_DATABASE"])
121
- yield client
122
- client.close
123
- end
124
-
125
- def mailbox_status_message(watcher)
126
- if watcher.logging_in?
127
- "Connecting and Logging into server"
128
- elsif watcher.logged_in?
129
- "Connected"
130
- else
131
- "Disconnected"
132
- end
133
- end
134
- def mailbox_status(watcher)
135
- if watcher.logging_in?
136
- :logging_in
137
- elsif watcher.logged_in?
138
- :connected
139
- else
140
- :disconnected
141
- end
142
- end
143
- def mailbox_status_colour(watcher)
144
- if watcher.logging_in?
145
- "orange"
146
- elsif watcher.logged_in?
147
- "green"
148
- else
149
- "red"
150
- end
151
- end
152
-
153
-
154
- end
155
- end