1
0
Fork 0
mirror of https://github.com/nishiki/botish.git synced 2024-11-27 01:33:05 +00:00

add config file

This commit is contained in:
Adrien Waksberg 2017-04-19 21:46:53 +02:00
parent b4d303747b
commit e695a12882
3 changed files with 23 additions and 24 deletions

View file

@ -10,32 +10,20 @@ options = {}
OptionParser.new do |opts| OptionParser.new do |opts|
opts.banner = 'Usage: botish --host irc.freenode.net --channel "test" [options]' opts.banner = 'Usage: botish --host irc.freenode.net --channel "test" [options]'
opts.on('-c', '--channel CHANNEL') do |channel| opts.on('-c', '--config PATH') do |path|
options[:channel] = channel options[:config_file] = path
end
opts.on('-H', '--host HOST') do |host|
options[:host] = host
end end
opts.on('-h', '--help') do opts.on('-h', '--help') do
puts opts puts opts
exit 0 exit 0
end end
opts.on('-p', '--port PORT') do |port|
options[:port] = port.to_i
end
opts.on('-u', '--user USER') do |user|
options[:user] = user
end
end.parse! end.parse!
Dir["#{File.expand_path('../../modules', __FILE__)}/*"].each do |f| Dir["#{File.expand_path('../../modules', __FILE__)}/*"].each do |f|
require_relative f require_relative f
end end
botish = Botish::Botish.new(options[:host], options[:channel], options[:user], options[:port]) botish = Botish::Botish.new(options[:config_file])
botish.connect botish.connect
botish.listen botish.listen

7
config.yml.example Normal file
View file

@ -0,0 +1,7 @@
---
host: irc.freenode.net
port: 6667
user: botish
channels:
- test
- test2

View file

@ -1,15 +1,17 @@
#!/usr/bin/ruby #!/usr/bin/ruby
require 'yaml'
require 'socket' require 'socket'
require 'botish/base' require 'botish/base'
module Botish module Botish
class Botish < Base class Botish < Base
def initialize(host, channel, user, port) def initialize(config_file)
@host = host config = YAML.load_file(config_file)
@channel = channel @host = config['host']
@user = user || 'botish' @channels = config['channels']
@port = port || 6667 @user = config['user'] || 'botish'
@port = config['port'] || 6667
end end
def connect def connect
@ -24,8 +26,10 @@ module Botish
break if msg.include?('End of /MOTD command.') break if msg.include?('End of /MOTD command.')
end end
send_msg("JOIN #{@channel}") @channels.each do |channel|
send_msg("PRIVMSG #{@channel} :Je suis là :')") send_msg("JOIN ##{channel}")
send_msg("PRIVMSG ##{channel} :Je suis là :')")
end
end end
def listen def listen
@ -43,8 +47,8 @@ module Botish
when /^PING (?<host>.+)/ when /^PING (?<host>.+)/
send_msg("PONG #{Regexp.last_match('host')}") send_msg("PONG #{Regexp.last_match('host')}")
when /^:(?<user>[[:alpha:]]+)([^ ]+)? PRIVMSG #{@channel} :#{@user}: ping/ when /^:(?<user>[[:alpha:]]+)([^ ]+)? PRIVMSG (?<channel>#[[:alpha:]]+) :#{@user}: ping/
send_msg("PRIVMSG #{@channel} :#{Regexp.last_match('user')}: pong") send_msg("PRIVMSG #{Regexp.last_match('channel')} :#{Regexp.last_match('user')}: pong")
when /^:(?<user>[[:alpha:]]+)([^ ]+)? PRIVMSG (?<channel>#?[[:alpha:]]+) :#{@user}: (?<command>[[:lower:]]+)( (?<args>.+))?/ when /^:(?<user>[[:alpha:]]+)([^ ]+)? PRIVMSG (?<channel>#?[[:alpha:]]+) :#{@user}: (?<command>[[:lower:]]+)( (?<args>.+))?/
command = Regexp.last_match('command') command = Regexp.last_match('command')