]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - rbot/plugins/url.rb
initial import of rbot
[user/henk/code/ruby/rbot.git] / rbot / plugins / url.rb
1 Url = Struct.new("Url", :channel, :nick, :time, :url)
2
3 class UrlPlugin < Plugin
4   def initialize
5     super
6     @registry.set_default(Array.new)
7   end
8   def help(plugin, topic="")
9     "urls [<max>=4] => list <max> last urls mentioned in current channel, urls <channel> [<max>=4] => list <max> last urls mentioned in <channel>, urls search <regexp> => search for matching urls, urls search <channel> <regexp>, search for matching urls in channel <channel>"
10   end
11   def listen(m)
12     return unless m.kind_of?(PrivMessage)
13     return if m.address?
14     # TODO support multiple urls in one line
15     if m.message =~ /(f|ht)tp:\/\//
16       if m.message =~ /((f|ht)tp:\/\/.*?)(?:\s+|$)/
17         url = Url.new(m.target, m.sourcenick, Time.new, $1)
18         list = @registry[m.target]
19         debug "#{list.length} urls so far"
20         if list.length > 50
21           list.pop
22         end
23         debug "storing url #{url.url}"
24         list.unshift url
25         debug "#{list.length} urls now"
26         @registry[m.target] = list
27       end
28     end
29   end
30   def privmsg(m)
31     case m.params
32     when nil
33       if m.public?
34         urls m, m.target
35       else
36         m.reply "in a private message, you need to specify a channel name for urls"
37       end
38     when (/^(\d+)$/)
39       max = $1.to_i
40       if m.public?
41         urls m, m.target, max
42       else
43         m.reply "in a private message, you need to specify a channel name for urls"
44       end
45     when (/^(#.*?)\s+(\d+)$/)
46       channel = $1
47       max = $2.to_i
48       urls m, channel, max
49     when (/^(#.*?)$/)
50       channel = $1
51       urls m, channel
52     when (/^search\s+(#.*?)\s+(.*)$/)
53       channel = $1
54       string = $2
55       search m, channel, string
56     when (/^search\s+(.*)$/)
57       string = $1
58       if m.public?
59         search m, m.target, string
60       else
61         m.reply "in a private message, you need to specify a channel name for urls"
62       end
63     else
64       m.reply "incorrect usage: " + help(m.plugin)
65     end
66   end
67
68   def urls(m, channel, max=4)
69     max = 10 if max > 10
70     max = 1 if max < 1
71     list = @registry[channel]
72     if list.empty?
73       m.reply "no urls seen yet for channel #{channel}"
74     else
75       list[0..(max-1)].each do |url|
76         m.reply "[#{url.time.strftime('%Y/%m/%d %H:%M:%S')}] <#{url.nick}> #{url.url}"
77       end
78     end
79   end
80
81   def search(m, channel, string, max=4)
82     max = 10 if max > 10
83     max = 1 if max < 1
84     regex = Regexp.new(string)
85     list = @registry[channel].find_all {|url|
86       regex.match(url.url) || regex.match(url.nick)
87     }
88     if list.empty?
89       m.reply "no matches for channel #{channel}"
90     else
91       list[0..(max-1)].each do |url|
92         m.reply "[#{url.time.strftime('%Y/%m/%d %H:%M:%S')}] <#{url.nick}> #{url.url}"
93       end
94     end
95   end
96 end
97 plugin = UrlPlugin.new
98 plugin.register("urls")