protonbot 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.rubocop.yml +38 -0
  4. data/CODE_OF_CONDUCT.md +74 -0
  5. data/CONFIG.md +45 -0
  6. data/CORE_PLUGIN.md +128 -0
  7. data/Gemfile +7 -0
  8. data/LICENSE.txt +21 -0
  9. data/PLUGIN.md +45 -0
  10. data/README.md +81 -0
  11. data/Rakefile +2 -0
  12. data/bin/console +14 -0
  13. data/bin/setup +8 -0
  14. data/exe/protonbot-gencert +7 -0
  15. data/lib/protonbot/bot.rb +64 -0
  16. data/lib/protonbot/bot_conf.rb +48 -0
  17. data/lib/protonbot/bot_plugins.rb +110 -0
  18. data/lib/protonbot/core_plugin/apis/help.rb +54 -0
  19. data/lib/protonbot/core_plugin/apis/index.rb +2 -0
  20. data/lib/protonbot/core_plugin/apis/perms.rb +118 -0
  21. data/lib/protonbot/core_plugin/codes/index.rb +3 -0
  22. data/lib/protonbot/core_plugin/codes/names.rb +21 -0
  23. data/lib/protonbot/core_plugin/codes/welcome.rb +7 -0
  24. data/lib/protonbot/core_plugin/codes/whois.rb +9 -0
  25. data/lib/protonbot/core_plugin/commands/admin.rb +107 -0
  26. data/lib/protonbot/core_plugin/commands/basic.rb +9 -0
  27. data/lib/protonbot/core_plugin/commands/cross.rb +40 -0
  28. data/lib/protonbot/core_plugin/commands/help.rb +25 -0
  29. data/lib/protonbot/core_plugin/commands/index.rb +6 -0
  30. data/lib/protonbot/core_plugin/commands/joinpart.rb +25 -0
  31. data/lib/protonbot/core_plugin/commands/perms.rb +50 -0
  32. data/lib/protonbot/core_plugin/hooks/ctcp.rb +9 -0
  33. data/lib/protonbot/core_plugin/hooks/index.rb +7 -0
  34. data/lib/protonbot/core_plugin/hooks/joinpart.rb +42 -0
  35. data/lib/protonbot/core_plugin/hooks/kick.rb +17 -0
  36. data/lib/protonbot/core_plugin/hooks/nick.rb +10 -0
  37. data/lib/protonbot/core_plugin/hooks/ping.rb +3 -0
  38. data/lib/protonbot/core_plugin/hooks/privmsg.rb +62 -0
  39. data/lib/protonbot/core_plugin/hooks/raw.rb +32 -0
  40. data/lib/protonbot/core_plugin/plugin.rb +10 -0
  41. data/lib/protonbot/event_lock.rb +25 -0
  42. data/lib/protonbot/hook.rb +22 -0
  43. data/lib/protonbot/log.rb +182 -0
  44. data/lib/protonbot/log_wrapper.rb +52 -0
  45. data/lib/protonbot/numeric.rb +140 -0
  46. data/lib/protonbot/plug.rb +118 -0
  47. data/lib/protonbot/plug_events.rb +51 -0
  48. data/lib/protonbot/plug_io.rb +99 -0
  49. data/lib/protonbot/plug_utils.rb +242 -0
  50. data/lib/protonbot/plugin.rb +101 -0
  51. data/lib/protonbot/version.rb +4 -0
  52. data/lib/protonbot.rb +38 -0
  53. data/protonbot.gemspec +31 -0
  54. metadata +181 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6b2fecb97495ccfcc14a68b256a2d0333ea2fee2
4
+ data.tar.gz: 9fa5a77d24a729f5fe20b05c03902b8928bc8efb
5
+ SHA512:
6
+ metadata.gz: 299ee28b838107170e2e2de59a4a1a75745194a8d7d973ec7dccdb50701dc1b4e23f7cb2daa0475c407178ed3bc089fb9d128cb8d10f9dd63f7714c6022a4ad7
7
+ data.tar.gz: 88cb469c9f7b42b49ddda112c1100fa35af92e4875cdb01ea32213ee9f73c59e3365adab0fa1dd405dc07f8caadff6f213df3c80ecec953de0aaad51fb07d10d
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ *.sublime*
12
+ /bottest/
13
+ TODO
data/.rubocop.yml ADDED
@@ -0,0 +1,38 @@
1
+ Metrics/MethodLength:
2
+ Enabled: false
3
+
4
+ Metrics/ModuleLength:
5
+ Enabled: false
6
+
7
+ Metrics/ClassLength:
8
+ Enabled: false
9
+
10
+ Metrics/BlockLength:
11
+ Enabled: false
12
+
13
+ Metrics/AbcSize:
14
+ Enabled: false
15
+
16
+ Metrics/CyclomaticComplexity:
17
+ Enabled: false
18
+
19
+ Style/Documentation:
20
+ Enabled: false
21
+
22
+ Style/ClassAndModuleChildren:
23
+ Enabled: false
24
+
25
+ Security/Eval:
26
+ Enabled: false
27
+
28
+ Metrics/LineLength:
29
+ Enabled: false
30
+
31
+ Metrics/PerceivedComplexity:
32
+ Enabled: false
33
+
34
+ Style/LambdaCall:
35
+ Enabled: false
36
+
37
+ Lint/AssignmentInCondition:
38
+ Enabled: false
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at nickolay02@inbox.ru. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/CONFIG.md ADDED
@@ -0,0 +1,45 @@
1
+ Full-fledged config-hash looks like that:
2
+
3
+ ```ruby
4
+ {
5
+ # Global section. Basically sets default values for server-hashes
6
+ 'global': {
7
+ 'user' => 'ProtonBot',
8
+ 'nick' => 'YetAnotherBot',
9
+ 'rnam' => 'An IRC bot in Ruby' # `rnam` is shortened version of `realname`
10
+ },
11
+ # Define servers here
12
+ 'servers' => {
13
+ 'server1' => {}, # Will use default config
14
+ 'server2' => {
15
+ 'host' => 'irc.foo.bar.ru',
16
+ 'port' => 6697,
17
+ 'pass' => 'example_password',
18
+ 'encoding' => 'koi-8r',
19
+ 'cmdchar' => '%',
20
+ 'autojoin' => ['#protonbot'],
21
+ 'ssl' => true, # This is how you can use SSL
22
+ 'ssl_crt' => './server2.crt',
23
+ 'ssl_key' => './server2.key'
24
+ }
25
+ }
26
+ }
27
+ ```
28
+
29
+ Server-Hashes are merged in next way: `default.merge(global).merge(server)`
30
+
31
+ Default server-hash:
32
+
33
+ ```ruby
34
+ {
35
+ 'host' => '127.0.0.1',
36
+ 'port' => 6667,
37
+ 'user' => 'ProtonBot',
38
+ 'nick' => 'ProtonBot',
39
+ 'rnam' => 'An IRC bot in Ruby',
40
+ 'queue_delay' => 0.7,
41
+ 'cmdchar' => '\\',
42
+ 'encoding' => 'UTF-8',
43
+ 'autojoin' => []
44
+ }
45
+ ```
data/CORE_PLUGIN.md ADDED
@@ -0,0 +1,128 @@
1
+ Finish this, write other stuff, allow requesting multiple permissions, fix YARD, and after - publish gem.
2
+
3
+ # Core Plugin
4
+ Core plugin implements features which are essential for bot to exist: hooks (including ping-response), command engine, permission system, help system etc.
5
+
6
+ Core plugin can be accessed by using `core` (or `@core`) attr-reader.
7
+
8
+ ## APIs
9
+ Here you can find documentation for core-plugin's APIs. Use them in your plugins.
10
+
11
+ ### Permissions
12
+ Core plugin implements permission system - it allows checking user's permissions, editing them, preventing users from running stuff without permission.
13
+
14
+ Permissions are stored and checked by hostname - so ensure that target user has stable hostmask (static IP address or freenode-like host cloak)
15
+
16
+ Checking permissions is done by using `ProtonBot::Hook#perm!(*perms_to_check)` after defining hook. For example:
17
+
18
+ ```ruby
19
+ cmd(cmd: 'sample_command') do |dat| # Defining hook. This time - command.
20
+ # Do something...
21
+ end.perm!('perm1') # Applying #perm! method which adds permission
22
+ # checks before running hook.
23
+
24
+ # You can also request more than one permission by simply passing more arguments
25
+
26
+ cmd(cmd: 'whatever_command') do |dat|
27
+ dat.reply 'Foo!'
28
+ end.perm!('foo', 'bar') # This command requires both `foo` and `bar` permissions
29
+
30
+ ```
31
+
32
+ There's no need to define new permissions, but you may want to have own permission group where you'll find all your permissions. These act like normal permissions, but they are expanded into their members while checking permissions
33
+
34
+ ```ruby
35
+ # Create permission group 'my_plugin' which will give all perms mentioned above
36
+ core.permhash['my_plugin'] = %w(perm1 foo bar)
37
+ # Add our permission group to 'admin' group so every admin will have access to these permissions
38
+ core.permhash['admin'] << 'my_plugin'
39
+ ```
40
+
41
+ Default permission hash:
42
+
43
+ ```ruby
44
+ {
45
+ 'owner' => %w(
46
+ reload
47
+ eval
48
+ exec
49
+ raw
50
+ cross
51
+ admin
52
+ perms
53
+ ),
54
+ 'admin' => %w(
55
+ joinpart
56
+ nick
57
+ msg
58
+ ctcp
59
+ notice
60
+ nctcp
61
+ flushq
62
+ ),
63
+ 'cross' => %w(
64
+ crossuser
65
+ crosschan
66
+ crossplug
67
+ )
68
+ }
69
+ ```
70
+
71
+ ### Help
72
+ Core plugin implements help system. To add help for new command, just use `core.help_add(group_name, command_name, syntax, description)`:
73
+
74
+ ```ruby
75
+ core.help_add('group_name', 'hello', 'hello [nickname]', 'Say hello!')
76
+
77
+ # How arguments are marked:
78
+ # <required>
79
+ # [optional]
80
+ # {0 or more}
81
+ # (1 or more)
82
+ ```
83
+
84
+ ## Commands
85
+ This is list of available commands. Use help engine for more info.
86
+
87
+ ### Basic commands
88
+ 1. `ping`
89
+ 2. `echo {message}`
90
+
91
+ ### Admin commands
92
+ 1. `key [key]`
93
+ 2. `nick <nick>`
94
+ 3. `msg [target] <message>`
95
+ 4. `notice [target] <message>`
96
+ 5. `ctcp [target] <message>`
97
+ 6. `nctcp [target] <message>`
98
+ 7. `flushq`
99
+ 8. `join (chans|sep=,) [pass]`
100
+ 9. `part (chans|sep=,) [reason]`
101
+
102
+ ### Owner commands
103
+ 1. `rb {code}`
104
+ 2. `eval {command}`
105
+ 3. `raw {message}`
106
+ 4. `r`
107
+
108
+ ### Permission-management commands
109
+ 1. `perms {groups}`
110
+ 2. `uperms <+|-|?> <user> {perms}`
111
+
112
+ ### Cross commands
113
+ 1. `as`
114
+ 2. `at`
115
+ 3. `on`
116
+
117
+ ### Help commands
118
+ 1. `help {commands}`
119
+ 2. `list {groups}`
120
+
121
+ ## Emitted events (except for `raw`, which is emitted by plug)
122
+ 1. `privmsg` (parameters - `nick`, `user`, `host`, `target`, `reply_to`, `message`)`
123
+ 2. `notice` (same as with `privmsg`, but without `reply_to`)
124
+ 3. `ctcp` and `nctcp` (have `split` and `cmd` so you can parse them)
125
+ 4. `command` (inherited from PRIVMSG; parameters - `split` (array), `cmd`)
126
+ 5. `ukick`, `unick`, `upart`, `ujoin`, `uquit`
127
+ 6. `utopic` (unused)
128
+ 7. `ping`
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ ruby '2.4.0'
4
+
5
+ # Specify your gem's dependencies in protonbot.gemspec
6
+ gem 'protonbot-chanop', '~> 0.1.0'
7
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Nickolay
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.
data/PLUGIN.md ADDED
@@ -0,0 +1,45 @@
1
+ # Plugin-development-tutorial
2
+
3
+ ## Preparing environment
4
+ You have 2 ways of creating plugins:
5
+
6
+ ### Gem. This way is recommended.
7
+ Create gem by doing `bundler gem <gemname>`. Ensure that this name is not taken on https://rubygems.org.
8
+
9
+ Create `lib/<gemname>/plugin.rb` file, where `gemname` - name of your gem with `-` replaced with `/`:
10
+
11
+ ```ruby
12
+ # Your requires here...
13
+
14
+ ProtonBot::Plugin.new do
15
+ @name = 'My first plugin'
16
+ @version = MyAmazingGem::VERSION
17
+ @description = 'Yay! First plugin!'
18
+
19
+ # Adding permissions using permission API (described in core_plugin.md)
20
+ core.permhash['admin'] << 'my_new_plugin'
21
+ core.permhash['my_new_plugin'] = %w(
22
+ my_sample_permission
23
+ )
24
+
25
+ # Hooking to `001` (WELCOME) code:
26
+ hook(type: :code, code: @numeric::WELCOME) do |dat|
27
+ # Do something...
28
+ end
29
+
30
+ # Hooking to command `ohaider`. This command cannot be run by users without
31
+ # permission 'my_sample_permission'
32
+ cmd(cmd: 'ohaider') do |dat|
33
+ dat.reply "Ohaider! Your nickname: #{dat[:nick]}. " +
34
+ "Your username: #{dat[:plug].getuser(dat[:nick])}. " +
35
+ "Your hostname: #{dat[:plug].gethost(dat[:nick])}. "
36
+ end.perm!('my_sample_permission')
37
+ end
38
+ ```
39
+
40
+ Command arguments can always be accessed by `dat[:split]` array.
41
+
42
+ Now you just need to install your gem (`rake install`) and add it to bot by writing `gem 'gemname'` in `plugin_loader` statement
43
+
44
+ ### Folder in plugins/ directory
45
+ Same as above, but instead of creating new gem, you are just creating folder `plugins/folder_name` and adding `plugin('folder_name')` to `plugin_loader` statement
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # Protonbot
2
+
3
+
4
+ ## Making basic bot
5
+
6
+ This small guide covers making working bot with YAML config
7
+
8
+ Create Gemfile with next content:
9
+
10
+ ```ruby
11
+ ruby '2.4.0'
12
+ source 'https://rubygems.org'
13
+ gem 'protonbot'
14
+ # gem 'protonbot-chanop' # If you want channel-operator commands
15
+ ```
16
+
17
+ Execute `bundler install`
18
+
19
+ Create `main.rb` with next content:
20
+
21
+ ```ruby
22
+ #!/usr/bin/env ruby
23
+
24
+ require 'protonbot'
25
+ require 'yaml'
26
+
27
+ ProtonBot::Bot.new do
28
+ configure do
29
+ gset YAML.load_file(File.expand_path('./conf.yml'))
30
+ end
31
+
32
+ plugin_loader do
33
+ # gem 'protonbot-chanop' # If you want channel-operator commands
34
+ end
35
+ end
36
+ ```
37
+
38
+ Create `conf.yml` with content like next: (just remove first 3 lines and omit `port` to disable SSL connection)
39
+
40
+ ```yaml
41
+ servers:
42
+ servername:
43
+ ssl: true
44
+ ssl_crt: ./example.crt
45
+ ssl_key: ./example.key
46
+ host: irc.example.net
47
+ port: 6697
48
+ autojoin:
49
+ - "#channel"
50
+ ```
51
+
52
+ If you need an SSL certificate, just run `protonbot-gencert <name>`. You need OpenSSL executable for that.
53
+
54
+ Start bot by doing `bundler exec ./main.rb`
55
+
56
+ ## Usage
57
+
58
+ After you have started your bot, you need to gain permissions.
59
+
60
+ 1. Ensure that your hostname is not dynamic. If you have no static IP for that,
61
+ get a host cloak on your NickServ account.
62
+ 2. Send `\key` command to your bot (where `\` - your command char).
63
+ 3. Check your terminal to find key there.
64
+ 4. Send `\key <key>` to your bot, where `<key>` - generated key.
65
+ 5. Now you have owner permissions. To receive help, send `\help` to your bot.
66
+
67
+ ## Development
68
+
69
+ After checking out the repo, run `bundler` to install dependencies. `bottest` directory is .gitignored, so you can make test bot there.
70
+
71
+ To install this gem onto your local machine, run `bundler exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundler exec rake release \[origin\]`, 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).
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/handicraftsman/protonbot. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
76
+
77
+
78
+ ## License
79
+
80
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
81
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler/gem_tasks'
2
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'protonbot'
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
data/bin/setup ADDED
@@ -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,7 @@
1
+ #!/usr/bin/ruby
2
+ if ARGV[0]
3
+ `openssl req -x509 -new -newkey rsa:4096 -sha256 -days 1000 -nodes -out #{ARGV[0]}.crt -keyout #{ARGV[0]}.key`
4
+ else
5
+ puts 'No filename provided!'
6
+ puts "Usage: protonbot-cert <filename>"
7
+ end
@@ -0,0 +1,64 @@
1
+ # Main bot class. Use it to create the bot
2
+ class ProtonBot::Bot
3
+ attr_reader :log, :_log, :dbs, :plugins, :conf, :plugs, :plugthrs
4
+
5
+ # @yield Main bot's block. You'll use it for configuring bot.
6
+ def initialize(&block)
7
+ @_log = ProtonBot::Log.new
8
+ @log = @_log.wrap('main')
9
+ @log.info('Hi there!')
10
+
11
+ instance_exec(&block)
12
+ @log.info('Processed main block')
13
+
14
+ @conf = {}
15
+ configure
16
+ @log.info('Processed config block')
17
+
18
+ @plugins = {}
19
+ plugins_load
20
+ @log.info('Processed plugins block')
21
+
22
+ @dbs = {}
23
+ @plugs = {}
24
+ @plugthrs = {}
25
+ Dir.mkdir('dbs/') unless File.exist?(File.expand_path('./dbs/'))
26
+ @conf['servers'].each do |k_, v_|
27
+ k = k_.clone
28
+ v = v_.clone
29
+ @plugthrs[k] = Thread.new do
30
+ @dbs[k] = Heliodor::DB.new("dbs/#{k}.db", true) unless k.nil?
31
+ @plugs[k] = ProtonBot::Plug.new(self, k.clone, v.clone)
32
+ begin
33
+ @plugs[k].connect! if v['enabled'] || v['enabled'].nil?
34
+ rescue => e
35
+ @plugs[k].log_err(e)
36
+ end
37
+ end
38
+ end
39
+
40
+ Signal.trap('INT') do
41
+ @plugs.each do |_k, v|
42
+ @log.info('Stopping...')
43
+ v.write_('QUIT :Stopping...')
44
+ v.running = false
45
+ end
46
+ @_log.stop
47
+ exit
48
+ end
49
+
50
+ @plugthrs.each do |_k, v|
51
+ v.join
52
+ end
53
+
54
+ @_log.stop
55
+ end
56
+
57
+ def inspect
58
+ %(<#ProtonBot::Bot:#{object_id.to_s(16)}>)
59
+ end
60
+
61
+ module Messages
62
+ NOT_ENOUGH_PARAMETERS = 'Not enough parameters!'.freeze
63
+ end
64
+ end
@@ -0,0 +1,48 @@
1
+ class ProtonBot::Bot
2
+ attr_reader :conf
3
+
4
+ # @!group Config
5
+
6
+ # Default server-config
7
+ DEFAULT_SERVER_CONFIG = {
8
+ 'host' => '127.0.0.1',
9
+ 'port' => 6667,
10
+ 'user' => 'ProtonBot',
11
+ 'nick' => 'ProtonBot',
12
+ 'rnam' => 'An IRC bot in Ruby',
13
+ 'queue_delay' => 0.7,
14
+ 'cmdchar' => '\\',
15
+ 'encoding' => 'UTF-8',
16
+ 'autojoin' => []
17
+ }.freeze
18
+
19
+ # @yield Do your config-loading here
20
+ # @see https://github.com/handicraftsman/heliodor/blob/master/config.md
21
+ # Config-description
22
+ def configure(&block)
23
+ if block
24
+ @configure_block = block
25
+ else
26
+ @configure_block.call if @configure_block
27
+ set 'tsafe', true
28
+ @conf['servers'].each do |k, v|
29
+ @conf['servers'][k] =
30
+ DEFAULT_SERVER_CONFIG.merge(@conf['global'].to_h).merge(v.to_h)
31
+ end
32
+ end
33
+ end
34
+
35
+ # @param dat [Hash] Sets config variable to given value
36
+ def gset(dat)
37
+ raise(ArgumentError, '`dat` is not hash!') unless dat.instance_of?(Hash)
38
+ @conf = dat
39
+ end
40
+
41
+ # Binds `val` to `key` in `@conf`
42
+ # @param key [Symbol]
43
+ # @param val [Object]
44
+ def set(key, val)
45
+ @conf[key.to_sym] = val
46
+ end
47
+ # @!endgroup
48
+ end