emn 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/bin/emn +7 -0
  3. data/lib/emn/version.rb +3 -0
  4. data/lib/emn.rb +193 -0
  5. metadata +75 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b0f059daf1f7b203fd38adb480b7c2df68c851ba
4
+ data.tar.gz: aaaea7cf03035c6e13b062e68821b380c8cbe1ae
5
+ SHA512:
6
+ metadata.gz: 34f5749ff2f14a9a8c7505e445b7d1f64ee1c3a989a8bba90b0003f78e65a3a88d38649472dbba4121acd1df60aba8d8c485abf17ddbad378be5551a10218175
7
+ data.tar.gz: 90932542c0783857db08928f797c2bfcc0c321e08a52272fce30fc871841e07f51911e48451a637504c31eed47806b5ef4decade19c63f3d85c035749a28d2e6
data/bin/emn ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'emn'
4
+
5
+ emn = EMN.new
6
+ emn.parse_options
7
+ emn.send!
@@ -0,0 +1,3 @@
1
+ class EMN
2
+ VERSION = "0.0.1"
3
+ end
data/lib/emn.rb ADDED
@@ -0,0 +1,193 @@
1
+ require 'eaal'
2
+ require 'yaml'
3
+ require 'pushover'
4
+ require 'optparse'
5
+ require 'pp'
6
+ require 'logger'
7
+ require 'emn/version'
8
+
9
+ class EMN
10
+ OPTIONS = {
11
+ :verbose => false,
12
+ :debug => false,
13
+ :config => File.expand_path("~/.emn"),
14
+ :seen => File.expand_path("~/.emn_seen"),
15
+ :logger => nil
16
+ }
17
+
18
+ def logger
19
+ if OPTIONS[:debug] || OPTIONS[:verbose]
20
+ return Logger.new(STDOUT)
21
+ else
22
+ return Logger.new(nil)
23
+ end
24
+ end
25
+
26
+ def parse_options
27
+ opt = OptionParser.new
28
+
29
+ opt.banner = "New mail notifications for Eve Online"
30
+ opt.separator ""
31
+
32
+ opt.on("--debug", "Enable debug mode") do
33
+ OPTIONS[:debug] = true
34
+ end
35
+
36
+ opt.on("--verbose", "Verbose logging") do
37
+ OPTIONS[:verbose] = true
38
+ end
39
+
40
+ opt.on("--config [CONFIG]", "Config file location (%s)" % OPTIONS[:config]) do |v|
41
+ OPTIONS[:config] = File.expand_path(v)
42
+ end
43
+
44
+ opt.on("--seen [SEEN_FILE]", "Seen file location (%s)" % OPTIONS[:seen]) do |v|
45
+ OPTIONS[:seen] = File.expand_path(v)
46
+ end
47
+
48
+ opt.separator ""
49
+ opt.separator "http://github.com/ripienaar/emn"
50
+
51
+ opt.parse!
52
+
53
+ logger.info("emn %s starting with config %s and seen file %s" % [ EMN::VERSION, OPTIONS[:config], OPTIONS[:seen]])
54
+ end
55
+
56
+ def config
57
+ @config ||= YAML.load(File.read(File.expand_path(OPTIONS[:config])))
58
+ end
59
+
60
+ def seen
61
+ @seen ||= YAML.load(File.read(File.expand_path(OPTIONS[:seen])))
62
+ rescue
63
+ @seen = {}
64
+ end
65
+
66
+ def save_seen!
67
+ return if OPTIONS[:debug]
68
+
69
+ File.open(File.expand_path(OPTIONS[:seen]), "w") do |file|
70
+ file.puts seen.to_yaml
71
+ end
72
+ end
73
+
74
+ def pushover
75
+ Pushover.user = config[:pushover][:user_token]
76
+ Pushover.token = config[:pushover][:app_token]
77
+ Pushover
78
+ end
79
+
80
+ def eve_api
81
+ unless EAAL.cache.is_a?(EAAL::Cache::FileCache)
82
+ EAAL.cache = EAAL::Cache::FileCache.new
83
+ end
84
+
85
+ @eve_api ||= EAAL::API.new(
86
+ config[:eve][:key_id],
87
+ config[:eve][:verification_code]
88
+ )
89
+ end
90
+
91
+ def mails(char)
92
+ eve_api.scope = "char"
93
+ eve_api.MailMessages("characterID" => char.characterID).messages.sort_by{|m| m.messageID}
94
+ end
95
+
96
+ def characters
97
+ eve_api.scope = "account"
98
+ eve_api.Characters.characters
99
+ end
100
+
101
+ def check_mail
102
+ notifications = {}
103
+
104
+ characters.each do |toon|
105
+ logger.debug("Checking character %s" % toon.name)
106
+
107
+ messages = mails(toon)
108
+
109
+ logger.debug("Found %d messages for %s" % [messages.size, toon.name])
110
+
111
+ messages.each do |mail|
112
+ logger.debug("Processing email %s: from: %s subject: %s" % [mail.messageID, mail.senderName, mail.title])
113
+
114
+ seen[toon.name] ||= "0"
115
+
116
+ if seen[toon.name] < mail.messageID
117
+ seen[toon.name] = mail.messageID
118
+
119
+ if mail.senderName == toon.name
120
+ logger.debug("Skipping mail %s as it's from the character" % mail.messageID)
121
+ next
122
+ end
123
+
124
+ logger.debug("Adding email %s: %s to the queue" % [mail.messageID, mail.title])
125
+
126
+ notifications[toon.name] ||= []
127
+ notifications[toon.name] << {
128
+ :from => mail.senderName,
129
+ :date => mail.sentDate,
130
+ :subject => mail.title
131
+ }
132
+ else
133
+ logger.debug("Skipping email %s: it's been seen before (%s)" % [mail.messageID, seen[toon.name]])
134
+ end
135
+ end
136
+ end
137
+
138
+ notifications.empty? ? nil : notifications
139
+ end
140
+
141
+ def message_body(notifications)
142
+ summary = ["<b>"]
143
+ body = []
144
+
145
+ notifications.keys.sort.each do |toon|
146
+ summary << "%s: %d " % [toon, notifications[toon].size]
147
+
148
+ body << "<b>%s</b>" % toon
149
+ notifications[toon].reverse[0..config[:max_notifications] - 1].each do |notification|
150
+ body << " %s: %s" % [notification[:from], notification[:subject]]
151
+ end
152
+
153
+ if notifications[toon].size > config[:max_notifications]
154
+ body << " ... and %d more" % [notifications[toon].size - config[:max_notifications]]
155
+ end
156
+
157
+ body << ""
158
+ end
159
+
160
+ summary << "</b>"
161
+
162
+ [summary, "", body].flatten.join("\n")
163
+ end
164
+
165
+ def publish(message)
166
+ if OPTIONS[:debug]
167
+ logger.debug("Would have published the message: ")
168
+ puts message
169
+ return true
170
+ else
171
+ responce = pushover.notification(:html => 1, :message => message)
172
+
173
+ if responce.code == 200
174
+ return true
175
+ else
176
+ STDERR.puts("Failed to notify pushover: %s" % responce.inspect)
177
+ return false
178
+ end
179
+ end
180
+ end
181
+
182
+ def send!
183
+ if notifications = check_mail
184
+ logger.debug("Found mail for %d characters" % [notifications.keys.size])
185
+
186
+ if publish(message_body(notifications))
187
+ save_seen!
188
+ end
189
+ else
190
+ logger.debug("No new mail found, not sending any messages")
191
+ end
192
+ end
193
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: emn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - R.I.Pienaar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: eaal
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pushover
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Get Pushover notification when your characters get mail
42
+ email: rip@devco.net
43
+ executables:
44
+ - emn
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - bin/emn
49
+ - lib/emn.rb
50
+ - lib/emn/version.rb
51
+ homepage: http://devco.net/
52
+ licenses: []
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 2.0.14
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: Eve Online New Mail Notifier
75
+ test_files: []