tegawa 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8e45c931b802282c0f2c7fdbc24e5150c36133f3e58c9032926020f577205948
4
- data.tar.gz: 3bd6f2a73033766028ae6dcdaded21cd4ea19f62e2726cfd2f290de82ecd9709
3
+ metadata.gz: 72984eb44391704a815c1bca822a1d35990743c037a37453e971447e327d6803
4
+ data.tar.gz: 4532fb57183c614ddc27b5354f57ee7d190f5f5441360296a4f575946329de9b
5
5
  SHA512:
6
- metadata.gz: 89752c96958d392350d58761a92fdcfdac4de29ecd9e02cb66c88f380714458775e910fbb4d30208e307dbca65957cb62d888454e2d3fdc8aa071b8308b774ce
7
- data.tar.gz: 821ec615d76d44112a6c5f3a5d9b6709631cb18cfba8362ef66bf7af355d8033a43e42c3c702a47b28eacc2f6f15fb93271f79e29cf9248db0e7e9ebee1cb2eb
6
+ metadata.gz: 82168fad5c05271a7d7797b3df15982cdec17c53d0d9aaf4f4900369bd638715fb95e4207d7c117ab0eaeca409f06103866b753a8133f9dfc4d73113982adef7
7
+ data.tar.gz: 07725eda7e3dfdab2762abe89627c4e7e70191822b1f4c62b6bd79071adf7309036f0c82bd4a0e70c2413c401c308436897e8afbf740578bb28d300a77a036a2
@@ -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('INT') do
15
+ trap("INT") do
16
16
  puts "#{Time.now}: Interrupted, exit now..."
17
17
  exit 0
18
18
  end
@@ -10,15 +10,15 @@ module Tegawa
10
10
  @queue = Tegawa.queue
11
11
  @logger = Tegawa.logger
12
12
 
13
- @bot ||= Telegram::Bot::Client.new(@token)
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 msg = @queue.pop
19
+ while (message = @queue.pop)
20
20
  @logger.info "Forwarding message to telegram"
21
- @bot.api.send_message(chat_id: @channel_id, text: msg, parse_mode: "markdown")
21
+ @bot.api.send_message(chat_id: @channel_id, text: message)
22
22
  end
23
23
  end
24
24
  end
@@ -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 = "Usage: tegawa [options]"
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
- # opts.on("-wWATCH", "--watch=WATCH", "Which directory to watch for files to forward") do |v|
41
- # unless File.directory?(v)
42
- # puts "Watch dir not a directory."
43
- # exit
44
- # end
45
- # args[:watch_dir] = v
46
- # end
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("", "--version", "Show version") do
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
- # unless args[:watch_dir].nil?
91
- # @watcher = Tegawa::Watcher.new(watch_dir)
92
- # end
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])
@@ -22,4 +22,3 @@ module Tegawa
22
22
  @mail_server = mail_server
23
23
  end
24
24
  end
25
-
@@ -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 = "**Mail-From:** #{@mail.from}\r\n**Subject:** #{@mail.subject}\r\n\r\n#{@mail.body}"
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tegawa
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -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.1.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: