# Url plugin for RBOT # $Id: url.rb 81 2004-09-21 22:43:13Z oct $ Url = Struct.new("Url", :id, :channel, :nick, :time, :url, :title, :description, :exported) load "rbotconf/url-conf.rb" require 'yaml' class UrlPlugin < Plugin def initialize super if(FileTest.exist?(UrlConfig::DataPath) && File.stat(UrlConfig::DataPath).size > 0) @data = YAML.load(File.open(UrlConfig::DataPath)) @current_index = @data["current_index"] @urls = @data["urls"] @keys = Hash.new() @urls.each do |key,value| @keys[value["url"]] = key end @date_dump = @data["date_dump"] else @current_index = "A" @urls = Hash.new() @keys = Hash.new() @date_dump = Time.now.to_i end end def help(plugin, topic="") "url delete XX : delete XX from the repository\n"+ "url delete XX 1 : delete comment number 1 from XX\n"+ "url show 12 : show the 12 last entries\n"+ "url show XX : show entry XX\n"+ "url dump : save the db state." end def listen(m) return unless m.kind_of?(PrivMessage) return if m.address? # TODO support multiple urls in one line if m.message =~ /(f|ht)tp:\/\// if m.message =~ /((f|ht)tp:\/\/.*?)(?:\s+|$)/ if @keys.has_key?($1) index = @keys[$1] if @urls[index].has_key?("date") since = Time.now - Time.at(@urls[index]["date"]) day =(since/(24*3600)).floor hour = ((since-day*(24*3600))/3600).floor min = ((since-day*(24*3600)-hour*3600)/60).floor m.reply "I've known this url for#{day>0?" #{day} days, ":""}#{hour>0?" #{hour} hours, ":""} #{min} min, you punk! (#{index})" else m.reply "I know this url, this is #{@keys[$1]}." end return end @urls[@current_index] = Hash.new() @urls[@current_index]["url"]= $1 @urls[@current_index]["nick"] = m.sourcenick @urls[@current_index]["channel"] = m.channel @urls[@current_index]["date"] = Time.now.to_i @keys[$1] = @current_index.clone index = @keys[$1] #m.reply "Got url! #{$1} for index #{index} (!#{index} for title and !!#{index} for comment)." post(m, index) @current_index.succ! dump end end if m.message =~/^!([A-Z]+) *(.*)/ @urls[$1]["title"] = $2 @urls[$1]["date"] = Time.now.to_i end if m.message =~/^!!([A-Z]+) *(.*)/ @urls[$1]["comment"] ||= Array.new() @urls[$1]["comment"] << [m.sourcenick, $2] end #one day has passed if Time.now.to_i-@date_dump > 3600 dump end end def cleanup dump end def dump @date_dump = Time.now.to_i dump_struct = Hash.new() dump_struct["current_index"] = @current_index dump_struct["date_dump"] = @date_dump dump_struct["urls"] = @urls File.open(UrlConfig::DataPath, "w") do |file| file.puts(dump_struct.to_yaml) end end def privmsg(m) case m.params when "dump" dump m.reply "Dump done." when /delete +([A-Z]+)$/ delete(m, $1) m.reply "Deleting #{$1}." when /delete +([A-Z]+) ([0-9]+)$/ delete_comment(m, $1,$2.to_i-1) when /show +([0-9]+)$/ count = $1.to_i m.reply "Last #{count} url read :" a_out = Array.new() @urls.keys.sort.reverse[0..count-1].each do |url| m.reply "#{url} : "+@urls[url]["url"] end when /show +([A-Z]+)$/ display(m, $1) end end def delete_comment(m, key, comment) c = @urls[key]["comment"][comment] @urls[key]["comment"].delete_at(comment) m.reply "Comment #{c.inspect} deleted." end def delete(m, code) if @urls.has_key?(code) url = @urls[code]["url"] del(m, code) @keys.delete(url) @urls.delete(code) end end require 'net/http' require 'uri' def post(m, key) login="deliciouslogin" password="deliciouspassword" url = URI.encode(@urls[key]["url"]) get_uri = URI.parse("http://del.icio.us/api/posts/add?&url=#{url}&description=#{url}&extended=#{key}") Net::HTTP.start(get_uri.host) do |http| req = Net::HTTP::Get.new(get_uri) req.basic_auth login, password response = http.request(req) end end def del(m, key) login="zoyonirc" password="4212" url = URI.encode(@urls[key]["url"]) get_uri = URI.parse("http://del.icio.us/api/posts/delete?&url=#{url}") Net::HTTP.start(get_uri.host) do |http| req = Net::HTTP::Get.new(get_uri) req.basic_auth login, password response = http.request(req) m.reply "Deleting from delicious." end end def display(message, code) if !@urls.has_key?(code) message.reply "Sorry, I don't know anything about this url." return end url = @urls[code] s_out = "#{url["url"]}" s_out = "#{url["title"]} at "+s_out if url.has_key?("title") s_out = s_out + " from #{url["nick"]} on #{url["channel"]}." if url.has_key?("comment") a_out = [] url["comment"].each_with_index do |comment, index| a_out << "#{index+1}) \""+comment.reverse.join("\" by ") end s_out = s_out + a_out.join(", ") end message.reply s_out end end plugin = UrlPlugin.new plugin.register("url")