From e695a1288278f57025a0389c3d7c56be627b284a Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Wed, 19 Apr 2017 21:46:53 +0200 Subject: [PATCH] add config file --- bin/botish | 18 +++--------------- config.yml.example | 7 +++++++ lib/botish/botish.rb | 22 +++++++++++++--------- 3 files changed, 23 insertions(+), 24 deletions(-) create mode 100644 config.yml.example diff --git a/bin/botish b/bin/botish index 77ecdf9..40a5412 100755 --- a/bin/botish +++ b/bin/botish @@ -10,32 +10,20 @@ options = {} OptionParser.new do |opts| opts.banner = 'Usage: botish --host irc.freenode.net --channel "test" [options]' - opts.on('-c', '--channel CHANNEL') do |channel| - options[:channel] = channel - end - - opts.on('-H', '--host HOST') do |host| - options[:host] = host + opts.on('-c', '--config PATH') do |path| + options[:config_file] = path end opts.on('-h', '--help') do puts opts exit 0 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! Dir["#{File.expand_path('../../modules', __FILE__)}/*"].each do |f| require_relative f end -botish = Botish::Botish.new(options[:host], options[:channel], options[:user], options[:port]) +botish = Botish::Botish.new(options[:config_file]) botish.connect botish.listen diff --git a/config.yml.example b/config.yml.example new file mode 100644 index 0000000..4136ca3 --- /dev/null +++ b/config.yml.example @@ -0,0 +1,7 @@ +--- +host: irc.freenode.net +port: 6667 +user: botish +channels: + - test + - test2 diff --git a/lib/botish/botish.rb b/lib/botish/botish.rb index 47e46fc..9396849 100644 --- a/lib/botish/botish.rb +++ b/lib/botish/botish.rb @@ -1,15 +1,17 @@ #!/usr/bin/ruby +require 'yaml' require 'socket' require 'botish/base' module Botish class Botish < Base - def initialize(host, channel, user, port) - @host = host - @channel = channel - @user = user || 'botish' - @port = port || 6667 + def initialize(config_file) + config = YAML.load_file(config_file) + @host = config['host'] + @channels = config['channels'] + @user = config['user'] || 'botish' + @port = config['port'] || 6667 end def connect @@ -24,8 +26,10 @@ module Botish break if msg.include?('End of /MOTD command.') end - send_msg("JOIN #{@channel}") - send_msg("PRIVMSG #{@channel} :Je suis là :')") + @channels.each do |channel| + send_msg("JOIN ##{channel}") + send_msg("PRIVMSG ##{channel} :Je suis là :')") + end end def listen @@ -43,8 +47,8 @@ module Botish when /^PING (?.+)/ send_msg("PONG #{Regexp.last_match('host')}") - when /^:(?[[:alpha:]]+)([^ ]+)? PRIVMSG #{@channel} :#{@user}: ping/ - send_msg("PRIVMSG #{@channel} :#{Regexp.last_match('user')}: pong") + when /^:(?[[:alpha:]]+)([^ ]+)? PRIVMSG (?#[[:alpha:]]+) :#{@user}: ping/ + send_msg("PRIVMSG #{Regexp.last_match('channel')} :#{Regexp.last_match('user')}: pong") when /^:(?[[:alpha:]]+)([^ ]+)? PRIVMSG (?#?[[:alpha:]]+) :#{@user}: (?[[:lower:]]+)( (?.+))?/ command = Regexp.last_match('command')