From 27d8d2fd75840f3c41ab4a85b6b4720b01254b3f Mon Sep 17 00:00:00 2001 From: Zach Bloomquist Date: Wed, 2 Jul 2014 05:11:10 -0400 Subject: [PATCH 1/4] Adding basic Quotes plugin --- data/quotes.txt | 0 plugins/quotes.rb | 86 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 data/quotes.txt create mode 100644 plugins/quotes.rb diff --git a/data/quotes.txt b/data/quotes.txt new file mode 100644 index 0000000..e69de29 diff --git a/plugins/quotes.rb b/plugins/quotes.rb new file mode 100644 index 0000000..5e01470 --- /dev/null +++ b/plugins/quotes.rb @@ -0,0 +1,86 @@ +# -*- coding: utf-8 -*- +# +# = Cinch Quotes plugin +# This plugin adds a simple quotes system to Cinch. +# +# == Author +# Zach Bloomquist +# +# == License +# A quotes plugin for Cinch. +# Copyright © 2014 Zach Bloomquist +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program. If not, see . +class Cinch::Quotes + include Cinch::Plugin + require 'net/http' + match /quote add (.+)$/, :method => :add + match /quote$/, :method => :random_quote + match /quote ([0-9]+)$/, :method => :specific_quote + match /quote find (.+)$/, :method => :search + match /quote list$/, :method => :list + listen_to :connect, :method => :on_connect + set :help, <<-HELP +cinch quote + See a random quote from the quotes database. +cinch quote + View a specific quote by its numerical ID. +cinch quote add + Add a quote to the database. +cinch quote find + Search the quote database for a quote matching and return + a random match. +cinch quote list + Get a list of all the quotes in the database. + HELP + def on_connect(*) + @db_location = 'data/quotes.txt' + end + def add(msg,quote) + fd = File.open(@db_location,'a') + fd.puts(quote+"\n") + fd.close + lines = File.readlines(@db_location).length-1 + msg.reply('Quote #'+lines.to_s+' added.') + end + def random_quote(msg) + quotes = File.readlines(@db_location) + msg.reply('There are no quotes defined!') and return unless quotes.length > 0 + quote_id = rand(quotes.length) + msg.reply('Quote #'+quote_id.to_s+': '+quotes[quote_id]) + end + def specific_quote(msg,id) + quotes = File.readlines(@db_location) + msg.reply('Error 404 Quote Not Found') and return unless quotes[id.to_i] + msg.reply('Quote #'+id+': '+quotes[id.to_i]) + end + def search(msg,terms) + candidates = Hash.new + fd = File.new(@db_location) + fd.each { |line| candidates[fd.lineno] = line if line.include? terms } + msg.reply('No quotes found matching those search terms.') and return if candidates.length == 0 + rand_id = rand(candidates.length) + msg.reply('Quote #'+candidates.keys[rand_id].to_s+': '+candidates[candidates.keys[rand_id]]) + end + def list(msg) + # POST the entire quote database, formatted, to sprunge.us + quotes = '================================ Quote Listing ================================'+"\n" + fd = File.new(@db_location) + fd.each { |line| quotes << 'Quote #'+fd.lineno.to_s+': '+line if line.strip } + http = Net::HTTP.new('sprunge.us') + response = http.post('/','sprunge='+quotes) + msg.reply('Quote posting failed.') and return unless response.body + msg.reply('Quote list: '+response.body) + end +end \ No newline at end of file From 160dde1527b3a6514eba30b8b557419672d6324c Mon Sep 17 00:00:00 2001 From: Zach Bloomquist Date: Wed, 2 Jul 2014 05:19:53 -0400 Subject: [PATCH 2/4] Fixing bug in Quotes where wrong ID is displayed on quote find --- plugins/quotes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/quotes.rb b/plugins/quotes.rb index 5e01470..f4f7f27 100644 --- a/plugins/quotes.rb +++ b/plugins/quotes.rb @@ -71,7 +71,7 @@ def search(msg,terms) fd.each { |line| candidates[fd.lineno] = line if line.include? terms } msg.reply('No quotes found matching those search terms.') and return if candidates.length == 0 rand_id = rand(candidates.length) - msg.reply('Quote #'+candidates.keys[rand_id].to_s+': '+candidates[candidates.keys[rand_id]]) + msg.reply('Quote #'+(candidates.keys[rand_id]-1).to_s+': '+candidates[candidates.keys[rand_id]]) end def list(msg) # POST the entire quote database, formatted, to sprunge.us From 40df7572c9ce6be507243b25ac5ac5722111a3f0 Mon Sep 17 00:00:00 2001 From: Zach Bloomquist Date: Wed, 2 Jul 2014 06:55:52 -0400 Subject: [PATCH 3/4] Adding Quotes to README --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 6d8c09a..e8f0985 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,9 @@ Logging PidFile : Allows you to create a PID file for your Cinch process. +Quotes +: Simple IRC quotes plugin. + Self : This is not a plugin, but rather a helper for writing plugins that make Cinch understand messages with start by Cinch’s nickname From a0885baac28bbf1cdd28dc07c60f9c811b6441b9 Mon Sep 17 00:00:00 2001 From: Zach Bloomquist Date: Wed, 22 Apr 2015 11:27:17 -0400 Subject: [PATCH 4/4] Fixing issue where .quote list would truncate --- plugins/quotes.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/quotes.rb b/plugins/quotes.rb index f4f7f27..df8ee2a 100644 --- a/plugins/quotes.rb +++ b/plugins/quotes.rb @@ -8,7 +8,7 @@ # # == License # A quotes plugin for Cinch. -# Copyright © 2014 Zach Bloomquist +# Copyright © 2014 Zach Bloomquist # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by @@ -78,9 +78,9 @@ def list(msg) quotes = '================================ Quote Listing ================================'+"\n" fd = File.new(@db_location) fd.each { |line| quotes << 'Quote #'+fd.lineno.to_s+': '+line if line.strip } - http = Net::HTTP.new('sprunge.us') - response = http.post('/','sprunge='+quotes) + uri = URI('http://p.chary.us/') + response = Net::HTTP.post_form(uri,'sprunge' => quotes) msg.reply('Quote posting failed.') and return unless response.body msg.reply('Quote list: '+response.body) end -end \ No newline at end of file +end