tegawa 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8e45c931b802282c0f2c7fdbc24e5150c36133f3e58c9032926020f577205948
4
+ data.tar.gz: 3bd6f2a73033766028ae6dcdaded21cd4ea19f62e2726cfd2f290de82ecd9709
5
+ SHA512:
6
+ metadata.gz: 89752c96958d392350d58761a92fdcfdac4de29ecd9e02cb66c88f380714458775e910fbb4d30208e307dbca65957cb62d888454e2d3fdc8aa071b8308b774ce
7
+ data.tar.gz: 821ec615d76d44112a6c5f3a5d9b6709631cb18cfba8362ef66bf7af355d8033a43e42c3c702a47b28eacc2f6f15fb93271f79e29cf9248db0e7e9ebee1cb2eb
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .byebug_history
10
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in tegawa.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "telegram-bot-ruby", "~> 0.12.0"
8
+ gem "midi-smtp-server", "~> 2.3"
9
+ gem "mail", "~> 2.7"
10
+ gem "listen", "~> 3.2"
11
+
12
+ gem "standardrb", "~> 1.0", group: :development
13
+ gem "byebug", "~> 11.1", group: :development
14
+ gem "irb", "~> 1.2", group: :development
@@ -0,0 +1,94 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tegawa (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.1)
10
+ axiom-types (0.1.1)
11
+ descendants_tracker (~> 0.0.4)
12
+ ice_nine (~> 0.11.0)
13
+ thread_safe (~> 0.3, >= 0.3.1)
14
+ byebug (11.1.3)
15
+ coercible (1.0.0)
16
+ descendants_tracker (~> 0.0.1)
17
+ descendants_tracker (0.0.4)
18
+ thread_safe (~> 0.3, >= 0.3.1)
19
+ equalizer (0.0.11)
20
+ faraday (1.0.1)
21
+ multipart-post (>= 1.2, < 3)
22
+ ffi (1.13.1)
23
+ ice_nine (0.11.2)
24
+ inflecto (0.0.2)
25
+ io-console (0.5.6)
26
+ irb (1.2.4)
27
+ reline (>= 0.0.1)
28
+ listen (3.2.1)
29
+ rb-fsevent (~> 0.10, >= 0.10.3)
30
+ rb-inotify (~> 0.9, >= 0.9.10)
31
+ mail (2.7.1)
32
+ mini_mime (>= 0.1.1)
33
+ midi-smtp-server (2.3.2)
34
+ mini_mime (1.0.2)
35
+ multipart-post (2.1.1)
36
+ parallel (1.19.2)
37
+ parser (2.7.1.4)
38
+ ast (~> 2.4.1)
39
+ rainbow (3.0.0)
40
+ rake (12.3.3)
41
+ rb-fsevent (0.10.4)
42
+ rb-inotify (0.10.1)
43
+ ffi (~> 1.0)
44
+ regexp_parser (1.7.1)
45
+ reline (0.1.4)
46
+ io-console (~> 0.5)
47
+ rexml (3.2.4)
48
+ rubocop (0.85.1)
49
+ parallel (~> 1.10)
50
+ parser (>= 2.7.0.1)
51
+ rainbow (>= 2.2.2, < 4.0)
52
+ regexp_parser (>= 1.7)
53
+ rexml
54
+ rubocop-ast (>= 0.0.3)
55
+ ruby-progressbar (~> 1.7)
56
+ unicode-display_width (>= 1.4.0, < 2.0)
57
+ rubocop-ast (0.1.0)
58
+ parser (>= 2.7.0.1)
59
+ rubocop-performance (1.6.1)
60
+ rubocop (>= 0.71.0)
61
+ ruby-progressbar (1.10.1)
62
+ standard (0.4.7)
63
+ rubocop (~> 0.85.0)
64
+ rubocop-performance (~> 1.6.0)
65
+ standardrb (1.0.0)
66
+ standard
67
+ telegram-bot-ruby (0.12.0)
68
+ faraday
69
+ inflecto
70
+ virtus
71
+ thread_safe (0.3.6)
72
+ unicode-display_width (1.7.0)
73
+ virtus (1.0.5)
74
+ axiom-types (~> 0.1)
75
+ coercible (~> 1.0)
76
+ descendants_tracker (~> 0.0, >= 0.0.3)
77
+ equalizer (~> 0.0, >= 0.0.9)
78
+
79
+ PLATFORMS
80
+ ruby
81
+
82
+ DEPENDENCIES
83
+ byebug (~> 11.1)
84
+ irb (~> 1.2)
85
+ listen (~> 3.2)
86
+ mail (~> 2.7)
87
+ midi-smtp-server (~> 2.3)
88
+ rake (~> 12.0)
89
+ standardrb (~> 1.0)
90
+ tegawa!
91
+ telegram-bot-ruby (~> 0.12.0)
92
+
93
+ BUNDLED WITH
94
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Otto Strassen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ # Tegawa
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tegawa`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'tegawa'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install tegawa
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tegawa.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tegawa"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "tegawa/cli"
4
+
5
+ Tegawa::Cli::Application.start
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "logger"
4
+ require "byebug"
5
+
6
+ require "tegawa/version"
7
+ require "tegawa/core"
8
+ require "tegawa/mail_server"
9
+ require "tegawa/bot"
10
+
11
+ module Tegawa
12
+ class Error < StandardError; end
13
+ # Your code goes here...
14
+ # try to gracefully shutdown on Ctrl-C
15
+ trap('INT') do
16
+ puts "#{Time.now}: Interrupted, exit now..."
17
+ exit 0
18
+ end
19
+
20
+ # setup exit code
21
+ at_exit do
22
+ Tegawa.mail_server&.stop
23
+ # puts "#{Time.now}: TeGaWa down!\n"
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "telegram/bot"
4
+
5
+ module Tegawa
6
+ class Bot
7
+ def initialize(token, channel_id)
8
+ @token = token
9
+ @channel_id = channel_id
10
+ @queue = Tegawa.queue
11
+ @logger = Tegawa.logger
12
+
13
+ @bot ||= Telegram::Bot::Client.new(@token)
14
+ @logger.info "Ready to forward messages to channel: #{@channel_id}"
15
+ end
16
+
17
+ def run
18
+ # FIXME: needs a thread pool to stop flooding
19
+ while msg = @queue.pop
20
+ @logger.info "Forwarding message to telegram"
21
+ @bot.api.send_message(chat_id: @channel_id, text: msg, parse_mode: "markdown")
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "optparse"
4
+
5
+ require "tegawa"
6
+ require "tegawa/bot"
7
+ require "tegawa/mail_server"
8
+
9
+ module Tegawa
10
+ module Cli
11
+ class Application
12
+ attr_reader :mail_server
13
+
14
+ class << self
15
+ def start
16
+ args = {
17
+ port: 2525,
18
+ addr: "127.0.0.1",
19
+ # watch_dir: nil,
20
+ log_file: STDOUT,
21
+ channel: nil,
22
+ token: nil
23
+ }
24
+
25
+ # opt_parser = OptionParser.new do |opts|
26
+ opt_parser = OptionParser.new { |opts|
27
+ opts.banner = "Usage: tegawa [options]"
28
+ 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
+ args[:channel] = v
30
+ end
31
+ opts.on("-pPORT", "--port=PORT", "Port on which the mailserver should listen. Default 2525.") do |v|
32
+ args[:port] = v.to_i
33
+ end
34
+ opts.on("-aADDR", "--addr=ADDR", "IP on which to listen for connections. Default 127.0.0.1.") do |v|
35
+ args[:addr] = v
36
+ end
37
+ opts.on("-lLOG", "--logfile=LOG", "Where to write logging output. Default is STDOUT.") do |v|
38
+ args[:log_file] = make_log_file(v)
39
+ 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
47
+ opts.on("-h", "--help", "Prints this help") do
48
+ puts opts
49
+ exit
50
+ end
51
+ opts.on("", "--version", "Show version") do
52
+ puts "Telegram GateWay Version #{Tegawa::VERSION}"
53
+ exit
54
+ end
55
+ }
56
+ opt_parser.parse!(ARGV)
57
+
58
+ args[:token] = ENV["TEGAWA_TOKEN"]
59
+
60
+ if args[:token].nil? || args[:channel].nil?
61
+ puts "Please set $TEGAWA_TOKEN to your telegram bot token and provide a --channel argument."
62
+ exit
63
+ end
64
+
65
+ setup(args)
66
+ end
67
+
68
+
69
+ private
70
+
71
+ def make_log_file(log_file)
72
+ return log_file if File.file?(log_file)
73
+ return log_file if File.directory?(File.dirname(log_file))
74
+
75
+ puts "Log file directory does not exist: #{File.dirname(log_file)}"
76
+ exit 1
77
+ end
78
+
79
+ def setup(args)
80
+ Tegawa.logger = Logger.new(args[:log_file])
81
+ @logger = Tegawa.logger
82
+ @queue = Tegawa.queue
83
+
84
+ @logger.info "Starting TeGaWa..."
85
+
86
+ @mail_server = Tegawa::MailServer.new(args[:addr], args[:port])
87
+ Tegawa.mail_server = @mail_server
88
+ @mail_server.start
89
+
90
+ # unless args[:watch_dir].nil?
91
+ # @watcher = Tegawa::Watcher.new(watch_dir)
92
+ # end
93
+
94
+ # the bot contains the main loop
95
+ @bot = Tegawa::Bot.new(args[:token], args[:channel])
96
+ @bot.run
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tegawa
4
+ def self.logger
5
+ @logger ||= Logger.new(STDOUT)
6
+ end
7
+
8
+ def self.logger=(logger)
9
+ @logger = logger
10
+ end
11
+
12
+ def self.queue
13
+ @queue ||= Queue.new
14
+ end
15
+
16
+ # mail server accessor for exiting cleanly with Ctrl-C
17
+ def self.mail_server
18
+ @mail_server
19
+ end
20
+
21
+ def self.mail_server=(mail_server)
22
+ @mail_server = mail_server
23
+ end
24
+ end
25
+
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "midi-smtp-server"
4
+ require "mail"
5
+
6
+ module Tegawa
7
+ class MailServer < MidiSmtpServer::Smtpd
8
+ def initialize(addr, port)
9
+ super(port, addr)
10
+ @queue = Tegawa.queue
11
+ @logger = Tegawa.logger
12
+
13
+ @logger.info "Mailserver running on #{addr}:#{port}"
14
+ end
15
+
16
+ def on_message_data_event(ctx)
17
+ # Output for debug
18
+ @logger.debug("mail reveived at: [#{ctx[:server][:local_ip]}:#{ctx[:server][:local_port]}] from: [#{ctx[:envelope][:from]}] for recipient(s): [#{ctx[:envelope][:to]}]...")
19
+
20
+ # Just decode message once to make sure, that this message ist readable
21
+ @mail = Mail.read_from_string(ctx[:message][:data])
22
+
23
+ # handle incoming mail, just show the message source
24
+ @logger.info "You've got mail! From: #{@mail.from}"
25
+
26
+ message = "**Mail-From:** #{@mail.from}\r\n**Subject:** #{@mail.subject}\r\n\r\n#{@mail.body}"
27
+ @queue << message
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tegawa
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,29 @@
1
+ require_relative "lib/tegawa/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "tegawa"
5
+ spec.version = Tegawa::VERSION
6
+ spec.authors = ["Otto Strassen"]
7
+ spec.email = ["otto@tostr.io"]
8
+
9
+ spec.summary = "Small daemon that takes information from various places and relays them over telegram."
10
+ # spec.description = %q{TODO: Write a longer description or delete this line.}
11
+ spec.homepage = "https://github.com/to-str/tegawa"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/to-str/tegawa"
19
+ spec.metadata["changelog_uri"] = "https://github.com/to-str/tegawa"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tegawa
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Otto Strassen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-07-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - otto@tostr.io
16
+ executables:
17
+ - tegawa
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - bin/console
28
+ - bin/setup
29
+ - exe/tegawa
30
+ - lib/tegawa.rb
31
+ - lib/tegawa/bot.rb
32
+ - lib/tegawa/cli.rb
33
+ - lib/tegawa/core.rb
34
+ - lib/tegawa/mail_server.rb
35
+ - lib/tegawa/version.rb
36
+ - tegawa.gemspec
37
+ homepage: https://github.com/to-str/tegawa
38
+ licenses:
39
+ - MIT
40
+ metadata:
41
+ homepage_uri: https://github.com/to-str/tegawa
42
+ source_code_uri: https://github.com/to-str/tegawa
43
+ changelog_uri: https://github.com/to-str/tegawa
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.3.0
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.1.2
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Small daemon that takes information from various places and relays them over
63
+ telegram.
64
+ test_files: []