|
|
#
# delicious bot mark I: accept no substitutes.
#
# author: Erik Hollensbe
# note: sooner or later this'll have a permanent home, and cleaner code.
#
require 'rubygems'
gem 'net-yail'
gem 'www-delicious'
require 'www/delicious'
require 'net/yail'
require 'net/yail/IRCBot'
require 'uri'
require 'yaml'
class DeliciousBot < IRCBot
def initialize(config, *args)
@del = WWW::Delicious.new(config['delicious_username'], config['delicious_password'])
@account = config['delicious_username']
super(*args)
end
def add_custom_handlers
@irc.prepend_handler :incoming_msg, method(:read_commands)
end
private
#
# actual commands
#
def tag_url(out, nick, url, tags)
get_url(out, url) do |post|
post.tags.push(*tags)
write_post(out, nick, post, "tagging")
end
end
def retitle_url(out, nick, url, title)
get_url(out, url) do |post|
post.title = title
write_post(out, nick, post, "retitling")
end
end
def store_urls(out, urls)
urls.each { |url| @del.posts_add(:url => url, :title => url, :replace => true) }
msg(out, "stored: #{urls.join(', ')}")
rescue WWW::Delicious::Error => e
p e.message
msg(out, "error storing: #{urls.join(', ')}. sorry, bub.")
end
def delete_url(out, nick, url)
get_url(out, url) do |post|
@del.posts_delete(post.url)
msg(out, "done, #{nick}")
end
rescue WWW::Delicious::Error
msg(out, "error deleting #{url}")
end
#
# command parser
#
def read_commands(hostinfo, nick, channel, text)
out = channel or nick
parts = text.split(/\s+/)
command = parts.shift
case command
when "!help"
msg(nick, "I respond to these commands:")
msg(nick, %w(!help !ping !title !tag !delete !account).join(", "))
msg(nick, "I also add any seen urls to our delicious account.")
when "!account"
msg(out, "Visit http://delicious.com/#{@account} to see links that have been stored.")
when "!ping"
msg(out, "Johnny five is alive!")
when "!delete"
if parts.length < 1
msg(out, "usage: !delete ")
else
delete_url(out, nick, parts.shift)
end
when "!tag"
if parts.length < 2
msg(out, "usage: !tag ")
else
tag_url(out, nick, parts.shift, parts.collect { |part| part.sub(/,*$/, '') })
end
when "!title"
if parts.length < 2
msg(out, "usage: !title ")
else
retitle_url(out, nick, parts[0], parts[1..-1].join(" "))
end
else
urls = URI.extract(text, %w(http))
unless urls.empty?
store_urls(out, urls)
end
end
end
#
# utility methods
#
def get_url(out, url)
posts = @del.posts_get(:url => url)
if posts.empty?
msg(out, "no such url: #{url}")
else
# XXX hack around a WWW::Delicious bug
post = posts[0]
post.url = post.url.to_s
yield post
end
rescue WWW::Delicious::Error
msg(out, "error handling url: #{url}")
end
def write_post(out, nick, post, action)
# XXX hack around a WWW::Delicious bug
@del.posts_delete(post.url)
@del.posts_add(post)
msg(out, "done, #{nick}.")
rescue WWW::Delicious::Error
msg(out, "error #{action} #{post.url}")
end
#
# ircbot class hooks
#
def welcome(text, args)
@channels.each do |channel|
@irc.join(channel)
msg(channel, "Hello! Say '!help' for more information.")
end
end
end
unless ARGV[0]
STDERR.puts "usage: $0 "
exit 1
end
config = YAML.load_file(ARGV[0])
t = DeliciousBot.new(
config,
{
:irc_network => config["server"],
:channels => config["channels"],
:username => config["username"],
:realname => config["realname"],
:nicknames => config["nicknames"],
:loud => config["loud"],
}
)
t.connect_socket
t.start_listening
t.irc_loop
|