mirror of
https://github.com/nishiki/botish.git
synced 2025-02-20 02:10:04 +00:00
add botish::base
This commit is contained in:
parent
c148828690
commit
377d32f4b1
5 changed files with 44 additions and 28 deletions
|
@ -2,7 +2,7 @@
|
|||
|
||||
$LOAD_PATH << File.expand_path('../../lib', __FILE__)
|
||||
|
||||
require 'botish'
|
||||
require 'botish/botish'
|
||||
|
||||
Dir["#{File.expand_path('../../modules', __FILE__)}/*"].each do |f|
|
||||
require_relative f
|
||||
|
|
20
lib/botish/base.rb
Normal file
20
lib/botish/base.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
module Botish
|
||||
class Base
|
||||
def initialize(connection)
|
||||
@connection = connection
|
||||
end
|
||||
|
||||
def run(args) end
|
||||
|
||||
private
|
||||
|
||||
def send_msg(msg)
|
||||
log("-> #{msg}")
|
||||
@connection.puts(msg)
|
||||
end
|
||||
|
||||
def log(msg)
|
||||
puts "#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%L')} #{msg}"
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,9 +1,10 @@
|
|||
#!/usr/bin/ruby
|
||||
|
||||
require 'socket'
|
||||
require 'botish/base'
|
||||
|
||||
module Botish
|
||||
class Botish
|
||||
class Botish < Base
|
||||
HOST = 'irc.freenode.net'
|
||||
PORT = 6667
|
||||
USERNAME = 'botish'
|
||||
|
@ -12,15 +13,15 @@ module Botish
|
|||
def initialize
|
||||
@connection = TCPSocket.open(HOST, PORT)
|
||||
|
||||
forward("NICK #{USERNAME}")
|
||||
forward("USER #{USERNAME} localhost * :#{USERNAME}")
|
||||
send_msg("NICK #{USERNAME}")
|
||||
send_msg("USER #{USERNAME} localhost * :#{USERNAME}")
|
||||
Kernel.loop do
|
||||
msg = @connection.gets
|
||||
log("<- #{msg}")
|
||||
break if msg.include?('End of /MOTD command.')
|
||||
end
|
||||
forward("JOIN #{CHANNEL}")
|
||||
forward("PRIVMSG #{CHANNEL} :Je suis là :')")
|
||||
send_msg("JOIN #{CHANNEL}")
|
||||
send_msg("PRIVMSG #{CHANNEL} :Je suis là :')")
|
||||
end
|
||||
|
||||
def listen
|
||||
|
@ -36,10 +37,10 @@ module Botish
|
|||
def parse(msg)
|
||||
case msg
|
||||
when /^PING (?<host>.+)/
|
||||
forward("PONG #{Regexp.last_match('host')}")
|
||||
send_msg("PONG #{Regexp.last_match('host')}")
|
||||
|
||||
when /^:(?<user>[[:alpha:]]+)([^ ]+)? PRIVMSG #{CHANNEL} :#{USERNAME}: ping/
|
||||
forward("PRIVMSG #{CHANNEL} :#{Regexp.last_match('user')}: pong")
|
||||
send_msg("PRIVMSG #{CHANNEL} :#{Regexp.last_match('user')}: pong")
|
||||
|
||||
when /^:(?<user>[[:alpha:]]+)([^ ]+)? PRIVMSG (?<channel>#?[[:alpha:]]+) :#{USERNAME}: (?<command>[[:lower:]]+)( (?<args>.+))?/
|
||||
command = Regexp.last_match('command')
|
||||
|
@ -48,17 +49,13 @@ module Botish
|
|||
opts[key] = Regexp.last_match(key)
|
||||
end
|
||||
|
||||
Kernel.const_get("Botish::#{command.capitalize}").new(@connection, options)
|
||||
plugin_class = "Botish::#{command.capitalize}"
|
||||
if Object.const_defined?(plugin_class)
|
||||
Object.const_get(plugin_class).new(@connection).run(options)
|
||||
else
|
||||
send_msg("PRIVMSG #{args[:channel]} :#{args[:user]}: unknown command")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def forward(msg)
|
||||
log("-> #{msg}")
|
||||
@connection.puts(msg)
|
||||
end
|
||||
|
||||
def log(msg)
|
||||
puts "#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%L')} #{msg}"
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,9 +1,9 @@
|
|||
module Botish
|
||||
class Coucou
|
||||
def initialize(connection, args)
|
||||
@connection = connection
|
||||
require 'botish/base'
|
||||
|
||||
@connection.puts("PRIVMSG #{args[:channel]} :#{args[:user]}: coucou mon petit")
|
||||
module Botish
|
||||
class Coucou < Base
|
||||
def run(args)
|
||||
send_msg("PRIVMSG #{args[:channel]} :#{args[:user]}: coucou mon petit")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
require 'json'
|
||||
require 'net/http'
|
||||
require 'botish/base'
|
||||
|
||||
module Botish
|
||||
class Wikipedia
|
||||
def initialize(connection, args)
|
||||
@connection = connection
|
||||
|
||||
class Wikipedia < Base
|
||||
def run(args)
|
||||
params = {
|
||||
format: 'json',
|
||||
action: 'query',
|
||||
|
@ -20,7 +19,7 @@ module Botish
|
|||
title = data['title']
|
||||
url = URI.encode("https://fr.wikipedia.org/wiki/#{title.tr(' ', '_')}")
|
||||
|
||||
@connection.puts("PRIVMSG #{args[:channel]} :#{args[:user]}: #{data['title']} => #{url}")
|
||||
send_msg("PRIVMSG #{args[:channel]} :#{args[:user]}: #{data['title']} => #{url}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue