diff --git a/Gemfile b/Gemfile index 4fba74302d2f600fbd56a84d813c9555aa66bfeb..d1801ea960dcff46e7c7aa8c38baa0691465ca09 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,6 @@ source "http://rubygems.org" gem "rake" -gem "net-yail" +gem "cinch" gem "rspec" gem "activerecord" gem "activesupport" diff --git a/Gemfile.lock b/Gemfile.lock index 6fe9f93b1a165813153a35c1fb8f0ada85589cd8..efd71119a07cb5079be48e3f2e40ed921ceacd54 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,59 +2,60 @@ GEM remote: http://rubygems.org/ specs: activerecord (1.6.0) - activesupport (3.2.11) - i18n (~> 0.6) + activesupport (3.2.13) + i18n (= 0.6.1) multi_json (~> 1.0) - builder (3.1.4) - diff-lcs (1.1.3) - faraday (0.8.4) + builder (3.2.0) + cinch (2.0.4) + diff-lcs (1.2.1) + faraday (0.8.7) multipart-post (~> 1.1) - hashie (1.2.0) + hashie (2.0.3) httmultiparty (0.3.8) httparty (>= 0.7.3) multipart-post - httparty (0.9.0) + httparty (0.10.2) multi_json (~> 1.0) - multi_xml + multi_xml (>= 0.5.2) httpauth (0.2.0) i18n (0.6.1) - jwt (0.1.5) - multi_json (>= 1.0) - multi_json (1.5.0) - multi_xml (0.5.1) - multipart-post (1.1.5) - net-yail (1.6.0) - nokogiri (1.5.6) + jwt (0.1.8) + multi_json (>= 1.5) + multi_json (1.7.2) + multi_xml (0.5.3) + multipart-post (1.2.0) + nokogiri (1.5.9) oauth (0.4.7) - oauth2 (0.8.0) + oauth2 (0.9.1) faraday (~> 0.8) httpauth (~> 0.1) jwt (~> 0.1.4) multi_json (~> 1.0) + multi_xml (~> 0.5) rack (~> 1.2) - rack (1.4.3) - rake (10.0.3) - rspec (2.12.0) - rspec-core (~> 2.12.0) - rspec-expectations (~> 2.12.0) - rspec-mocks (~> 2.12.0) - rspec-core (2.12.2) - rspec-expectations (2.12.1) - diff-lcs (~> 1.1.3) - rspec-mocks (2.12.1) - simple_oauth (0.1.9) + rack (1.5.2) + rake (10.0.4) + rspec (2.13.0) + rspec-core (~> 2.13.0) + rspec-expectations (~> 2.13.0) + rspec-mocks (~> 2.13.0) + rspec-core (2.13.1) + rspec-expectations (2.13.0) + diff-lcs (>= 1.1.3, < 2.0) + rspec-mocks (2.13.0) + simple_oauth (0.2.0) soundcloud (0.3.1) hashie httmultiparty (>= 0.3) httparty (>= 0.7.3) - sqlite3 (1.3.6) - youtube_it (2.1.8) + sqlite3 (1.3.7) + youtube_it (2.1.13) builder faraday (~> 0.8) nokogiri (~> 1.5.2) oauth (~> 0.4.4) oauth2 (~> 0.6) - simple_oauth (~> 0.1.5) + simple_oauth (>= 0.1.5) PLATFORMS ruby @@ -62,7 +63,7 @@ PLATFORMS DEPENDENCIES activerecord activesupport - net-yail + cinch rake rspec soundcloud diff --git a/lib/options.rb b/lib/options.rb index 69367af8f72522c88ecef6751831f086692c0a98..5ea90a61e1144b17c4eb5c51c47d3350474b0726 100644 --- a/lib/options.rb +++ b/lib/options.rb @@ -20,8 +20,8 @@ class Options exit end - opts.on('-s', '--silent', 'set log to FATAL') do - @options[:silent] = true + opts.on('-v', '--verbose', 'enable verbose mode') do + @options[:verbose] = true end opts.on('-a', '--admin', 'admin nick') do @@ -29,7 +29,7 @@ class Options end opts.on('-n', '--network', 'server address') do - @options[:address] = arg + @options[:server] = arg end opts.on('-p', '--port', 'server port') do @@ -49,8 +49,11 @@ class Options end end - @options[:silent] ||= false - @options[:nicknames] ||= ['PlayBot', 'Play_Bot', 'Play__Bot', 'Play___Bot'] + @options[:verbose] ||= false + @options[:nick] ||= "PlayBot" + @options[:nicks] ||= ['Play_Bot', 'Play__Bot', 'Play___Bot'] + @options[:realname] || = "PlayBot" + @options[:user] ||= "PlayBot" @options[:channels] ||= ['#hormone'] @options end diff --git a/plugins/soundcloud_plugin.rb b/plugins/soundcloud_plugin.rb index ee82eb532ecc0ac52297c66af0b0717eb67fde03..32d618cc67ef0856f4eaef5ee4403b9f7fa2a096 100644 --- a/plugins/soundcloud_plugin.rb +++ b/plugins/soundcloud_plugin.rb @@ -7,20 +7,21 @@ require 'soundcloud' # SitePlugin for Soundcloud # # Need an client ID (soundcloud_client_id). -class SoundcloudPlugin < SitePlugin - def self.can_handle?(site) - site =~ /^https?:\/\/(www\.)?soundcloud\.com\/[a-zA-Z0-9\/_-]+$/ - end +class SoundCloud + include Cinch::Plugin - public - def initialize(options) - @client = Soundcloud.new(:client_id => options[:soundcloud_client_id]) - end + match /^https?:\/\/(www\.)?soundcloud\.com\/[a-zA-Z0-9\/_-]+$/ def get(url) - track = @client.get('/resolve', :url => url) + client = Soundcloud.new(:client_id => config[:client_id]) + track = client.get('/resolve', :url => url) url.gsub(/http:\/\//, 'https://') {:title => track.title, :author => track.user.username, :url => url} end + + def execute(m, url) + infos = get(url) + m.reply("#{infos.title} | #{infos.author}") + end end diff --git a/plugins/youtube_plugin.rb b/plugins/youtube_plugin.rb index bf0bdf07ace3a2f0fae8ee0890af1b00c707f1ef..4ff48b490b5f2accba4923a4cdc69da836c19e97 100644 --- a/plugins/youtube_plugin.rb +++ b/plugins/youtube_plugin.rb @@ -1,23 +1,23 @@ -require_relative '../lib/site_plugin.rb' - require 'rubygems' require 'bundler/setup' +require 'cinch' require 'youtube_it' # SitePlugin for YouTube -class YoutubePlugin < SitePlugin - def self.can_handle?(site) - site =~ /^https?:\/\/((www.)?youtube.(fr|com)\/watch\?v=|youtu\.be\/)[a-zA-Z0-9-]+$/ - end +class Youtube + include Cinch::Plugin - public - def initialize(options) - @client = YouTubeIt::Client.new - end + match /^https?:\/\/((www.)?youtube.(fr|com)\/watch\?v=|youtu\.be\/)[a-zA-Z0-9-]+$/ def get(url) - video = @client.video_by(url) + client = YouTubeIt::Client.new + video = client.video_by(url) url.gsub(/https?:\/\/(www.)?youtube.(fr|com)\/watch\?v=|youtu\.be/, 'https://www.youtube.com') {:title => video.title, :author => video.author.name, :url => url} end + + def execute(m, url) + infos = get(url) + m.reply("#{infos.title} | #{infos.author}") + end end diff --git a/run.rb b/run.rb index 109a762e5595c0dc972813f4a6a56f89b2d88ff6..740bc7a9537dd9c07b03bb530391b465b19b125f 100755 --- a/run.rb +++ b/run.rb @@ -1,17 +1,58 @@ #!/usr/bin/ruby +require 'rubygems' +require 'bundler/setup' +require 'cinch' require 'active_support/core_ext/hash/except.rb' require 'logger' -require_relative 'lib/playbot' require_relative 'lib/options' -# This code start the PlayBot with somes options. +# -- +# Changing working directory so the inclusion of plugin can be done correctly. +# I don't complety know why, but this is necessary. +Dir.chdir(File.expand_path File.dirname(__FILE__)) + +# -- +# Add plugins folder to LOAD_PATH and subsequently require all plugins. +Dir[File.join('plugins', '*.rb')].each { |file| require_relative file } + + +# Monkey patch for String class +class String + def to_class + chain = self.split "::" + klass = Kernel + chain.each do |klass_string| + klass = klass.const_get klass_string + end + klass.is_a?(Class) ? klass : nil + rescue NameError + nil + end +end + +# This code start the PlayBot with somes options. options = Options.new.read_all ActiveRecord::Base.establish_connection(options[:database]) ActiveRecord::Base.logger = Logger.new(File.open(options[:database][:log], 'a')) -bot = PlayBot.new(options) -bot.irc_loop + +bot = Cinch::Bot.new do + configure do |c| + c = options + + # we add the plugins + c.plugins.plugins = [] + options[:plugins].each do |plugin| + c.plugins.plugins << plugin.to_class + c.plugins.options[plugin.to_class] = options[plugin.to_s]. + end + + c.plugins.prefix = nil + end +end + +bot.start