tegawa 0.1.0 → 0.2.0
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 +4 -4
- data/lib/tegawa.rb +2 -2
- data/lib/tegawa/bot.rb +3 -3
- data/lib/tegawa/cli.rb +26 -13
- data/lib/tegawa/core.rb +0 -1
- data/lib/tegawa/mail_server.rb +1 -3
- data/lib/tegawa/version.rb +1 -1
- data/lib/tegawa/watcher.rb +37 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 72984eb44391704a815c1bca822a1d35990743c037a37453e971447e327d6803
|
4
|
+
data.tar.gz: 4532fb57183c614ddc27b5354f57ee7d190f5f5441360296a4f575946329de9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 82168fad5c05271a7d7797b3df15982cdec17c53d0d9aaf4f4900369bd638715fb95e4207d7c117ab0eaeca409f06103866b753a8133f9dfc4d73113982adef7
|
7
|
+
data.tar.gz: 07725eda7e3dfdab2762abe89627c4e7e70191822b1f4c62b6bd79071adf7309036f0c82bd4a0e70c2413c401c308436897e8afbf740578bb28d300a77a036a2
|
data/lib/tegawa.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "logger"
|
4
|
-
require "byebug"
|
5
4
|
|
6
5
|
require "tegawa/version"
|
7
6
|
require "tegawa/core"
|
8
7
|
require "tegawa/mail_server"
|
8
|
+
require "tegawa/watcher"
|
9
9
|
require "tegawa/bot"
|
10
10
|
|
11
11
|
module Tegawa
|
12
12
|
class Error < StandardError; end
|
13
13
|
# Your code goes here...
|
14
14
|
# try to gracefully shutdown on Ctrl-C
|
15
|
-
trap(
|
15
|
+
trap("INT") do
|
16
16
|
puts "#{Time.now}: Interrupted, exit now..."
|
17
17
|
exit 0
|
18
18
|
end
|
data/lib/tegawa/bot.rb
CHANGED
@@ -10,15 +10,15 @@ module Tegawa
|
|
10
10
|
@queue = Tegawa.queue
|
11
11
|
@logger = Tegawa.logger
|
12
12
|
|
13
|
-
@bot
|
13
|
+
@bot = Telegram::Bot::Client.new(@token)
|
14
14
|
@logger.info "Ready to forward messages to channel: #{@channel_id}"
|
15
15
|
end
|
16
16
|
|
17
17
|
def run
|
18
18
|
# FIXME: needs a thread pool to stop flooding
|
19
|
-
while
|
19
|
+
while (message = @queue.pop)
|
20
20
|
@logger.info "Forwarding message to telegram"
|
21
|
-
@bot.api.send_message(chat_id: @channel_id, text:
|
21
|
+
@bot.api.send_message(chat_id: @channel_id, text: message)
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
data/lib/tegawa/cli.rb
CHANGED
@@ -5,6 +5,7 @@ require "optparse"
|
|
5
5
|
require "tegawa"
|
6
6
|
require "tegawa/bot"
|
7
7
|
require "tegawa/mail_server"
|
8
|
+
require "tegawa/watcher"
|
8
9
|
|
9
10
|
module Tegawa
|
10
11
|
module Cli
|
@@ -24,7 +25,20 @@ module Tegawa
|
|
24
25
|
|
25
26
|
# opt_parser = OptionParser.new do |opts|
|
26
27
|
opt_parser = OptionParser.new { |opts|
|
27
|
-
opts.banner =
|
28
|
+
opts.banner = <<~EOF
|
29
|
+
Usage: tegawa [options]
|
30
|
+
|
31
|
+
TeGaWa stands for Telegram GateWay. This tool is supposed to be run
|
32
|
+
as a daemon and accept local mail as well as watch a directory for
|
33
|
+
text files. It then forwards these mails and files to a Telegram
|
34
|
+
channel so you can easily stay informed about what is happening on
|
35
|
+
your systems.
|
36
|
+
|
37
|
+
It expects $TEGAWA_TOKEN to contain your telegram bot token and it
|
38
|
+
needs the channel_id to which it forwards the information.
|
39
|
+
|
40
|
+
EOF
|
41
|
+
|
28
42
|
opts.on("-cCHANNEL", "--channel=CHANNEL", "The id of the telegram channel that should get the forwarded messages. Make sure your bot is a member first.") do |v|
|
29
43
|
args[:channel] = v
|
30
44
|
end
|
@@ -37,18 +51,18 @@ module Tegawa
|
|
37
51
|
opts.on("-lLOG", "--logfile=LOG", "Where to write logging output. Default is STDOUT.") do |v|
|
38
52
|
args[:log_file] = make_log_file(v)
|
39
53
|
end
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
54
|
+
opts.on("-wWATCH", "--watch=WATCH", "Which directory to watch for files to forward") do |v|
|
55
|
+
unless File.directory?(v)
|
56
|
+
puts "Watch dir not a directory."
|
57
|
+
exit
|
58
|
+
end
|
59
|
+
args[:watch_dir] = v
|
60
|
+
end
|
47
61
|
opts.on("-h", "--help", "Prints this help") do
|
48
62
|
puts opts
|
49
63
|
exit
|
50
64
|
end
|
51
|
-
opts.on("
|
65
|
+
opts.on("--version", "Show version") do
|
52
66
|
puts "Telegram GateWay Version #{Tegawa::VERSION}"
|
53
67
|
exit
|
54
68
|
end
|
@@ -65,7 +79,6 @@ module Tegawa
|
|
65
79
|
setup(args)
|
66
80
|
end
|
67
81
|
|
68
|
-
|
69
82
|
private
|
70
83
|
|
71
84
|
def make_log_file(log_file)
|
@@ -87,9 +100,9 @@ module Tegawa
|
|
87
100
|
Tegawa.mail_server = @mail_server
|
88
101
|
@mail_server.start
|
89
102
|
|
90
|
-
|
91
|
-
|
92
|
-
|
103
|
+
unless args[:watch_dir].nil?
|
104
|
+
@watcher = Tegawa::Watcher.new(args[:watch_dir])
|
105
|
+
end
|
93
106
|
|
94
107
|
# the bot contains the main loop
|
95
108
|
@bot = Tegawa::Bot.new(args[:token], args[:channel])
|
data/lib/tegawa/core.rb
CHANGED
data/lib/tegawa/mail_server.rb
CHANGED
@@ -9,8 +9,6 @@ module Tegawa
|
|
9
9
|
super(port, addr)
|
10
10
|
@queue = Tegawa.queue
|
11
11
|
@logger = Tegawa.logger
|
12
|
-
|
13
|
-
@logger.info "Mailserver running on #{addr}:#{port}"
|
14
12
|
end
|
15
13
|
|
16
14
|
def on_message_data_event(ctx)
|
@@ -23,7 +21,7 @@ module Tegawa
|
|
23
21
|
# handle incoming mail, just show the message source
|
24
22
|
@logger.info "You've got mail! From: #{@mail.from}"
|
25
23
|
|
26
|
-
message = "
|
24
|
+
message = "Mail-From: #{@mail.from}\r\nSubject: #{@mail.subject}\r\n\r\n#{@mail.body}"
|
27
25
|
@queue << message
|
28
26
|
end
|
29
27
|
end
|
data/lib/tegawa/version.rb
CHANGED
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "listen"
|
4
|
+
|
5
|
+
module Tegawa
|
6
|
+
class Watcher
|
7
|
+
def initialize(watch_dir)
|
8
|
+
@queue = Tegawa.queue
|
9
|
+
@logger = Tegawa.logger
|
10
|
+
|
11
|
+
@logger.info "Watching #{watch_dir} for files"
|
12
|
+
|
13
|
+
# TODO: maybe allow to specify extension and recursion?
|
14
|
+
# listener = Listen.to("watch_dir", only: /.message$/) { |_modified, created, _removed|
|
15
|
+
@listener = Listen.to(watch_dir) { |_modified, created, _removed|
|
16
|
+
next if created.empty?
|
17
|
+
|
18
|
+
consume_file(created)
|
19
|
+
}
|
20
|
+
@listener.start # not blocking
|
21
|
+
end
|
22
|
+
|
23
|
+
def consume_file(paths)
|
24
|
+
paths.each do |path|
|
25
|
+
next unless File.exist?(path)
|
26
|
+
|
27
|
+
# FIXME: thread pool
|
28
|
+
Thread.new {
|
29
|
+
content = File.read(path)[0..4095]
|
30
|
+
message = "File: #{path}\r\n\r\n#{content}"
|
31
|
+
@queue << message
|
32
|
+
File.delete(path)
|
33
|
+
}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tegawa
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Otto Strassen
|
@@ -33,6 +33,7 @@ files:
|
|
33
33
|
- lib/tegawa/core.rb
|
34
34
|
- lib/tegawa/mail_server.rb
|
35
35
|
- lib/tegawa/version.rb
|
36
|
+
- lib/tegawa/watcher.rb
|
36
37
|
- tegawa.gemspec
|
37
38
|
homepage: https://github.com/to-str/tegawa
|
38
39
|
licenses:
|