summaryrefslogtreecommitdiff
path: root/data/rbot
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2009-03-13 11:50:27 +0100
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2009-03-13 23:50:37 +0100
commit55eb59a4d712752c75d1e5ec6622d591bd69110a (patch)
tree82047a0a326c6ad17751f00f7ca5bd9238a8482a /data/rbot
parentd6998e9b33f57c66d8026ba6b07054cf0b6a7003 (diff)
rss: refactor rss types loading
Make use of the new custom filter loading procedure to move most of the type definitions into its own file (data/rbot/filters/rss.rb) and only define some essential ones in the plugin itself. As an added benefit, user types can be loaded from filters/rss.rb and rss/types.rb in the botclass directory.
Diffstat (limited to 'data/rbot')
-rw-r--r--data/rbot/filters/rss.rb72
-rw-r--r--data/rbot/plugins/rss.rb102
2 files changed, 101 insertions, 73 deletions
diff --git a/data/rbot/filters/rss.rb b/data/rbot/filters/rss.rb
new file mode 100644
index 00000000..159c98cd
--- /dev/null
+++ b/data/rbot/filters/rss.rb
@@ -0,0 +1,72 @@
+#-- vim:sw=2:et
+#++
+
+rss_type :blog do |s|
+ author = s[:author] ? (s[:author] + " ") : ""
+ abt = s[:category] ? "about #{s[:category]} " : ""
+ line1 = "%{handle}%{date}%{author}blogged %{abt}at %{link}"
+ line2 = "%{handle}%{title} - %{desc}"
+ make_stream(line1, line2, s, :author => author, :abt => abt)
+end
+
+rss_type :forum do |s|
+ author = s[:author] ? (s[:author] + " ") : ""
+ abt = s[:category] ? "on #{s[:category]} " : ""
+ line1 = "%{handle}%{date}%{author}posted %{abt}at %{link}"
+ line2 = "%{handle}%{title} - %{desc}"
+ make_stream(line1, line2, s, :author => author, :abt => abt)
+end
+
+rss_type :git do |s|
+ author = s[:author].sub(/@\S+?\s*>/, "@...>") + " " if s[:author]
+ line1 = "%{handle}%{date}%{author}committed %{title}%{at}%{link}"
+ make_stream(line1, nil, s, :author => author)
+end
+
+rss_type :gmane do |s|
+ line1 = "%{handle}%{date}Message %{title} sent by %{author}. %{desc}"
+ make_stream(line1, nil, s)
+end
+
+rss_type :headlines do |s|
+ line1 = (s[:handle].empty? ? "%{date}" : "%{handle}") << "%{title}"
+ make_stream(line1, nil, s)
+end
+
+rss_type :news do |s|
+ line1 = "%{handle}%{date}%{title}%{at}%{link}" % s
+ line2 = "%{handle}%{date}%{desc}" % s
+ make_stream(line1, line2, s)
+end
+
+rss_type :photoblog do |s|
+ author = s[:author] ? (s[:author] + " ") : ""
+ abt = s[:category] ? "under #{s[:category]} " : ""
+ line1 = "%{handle}%{date}%{author}added an image %{abt}at %{link}"
+ line2 = "%{handle}%{title} - %{desc}"
+ make_stream(line1, line2, s, :author => author, :abt => abt)
+end
+
+rss_type :trac do |s|
+ author = s[:author].sub(/@\S+?\s*>/, "@...>") + ": " if s[:author]
+ line1 = "%{handle}%{date}%{author}%{title}%{at}%{link}"
+ line2 = nil
+ unless s[:item].title =~ /^(?:Changeset \[(?:[\da-f]+)\]|\(git commit\))/
+ line2 = "%{handle}%{date}%{desc}"
+ end
+ make_stream(line1, line2, s, :author => author)
+end
+
+rss_type :wiki do |s|
+ line1 = "%{handle}%{date}%{title}%{at}%{link}"
+ line1 << "has been edited by %{author}. %{desc}"
+ make_stream(line1, nil, s)
+end
+
+rss_type :"/." do |s|
+ dept = "(from the #{s[:item].slash_department} dept) " rescue nil
+ sec = " in section #{s[:item].slash_section}" rescue nil
+ line1 = "%{handle}%{date}%{dept}%{title}%{at}%{link} "
+ line1 << "(posted by %{author}%{sec})"
+ make_stream(line1, nil, s, :dept => dept, :sec => sec)
+end
diff --git a/data/rbot/plugins/rss.rb b/data/rbot/plugins/rss.rb
index 510db009..de7678a8 100644
--- a/data/rbot/plugins/rss.rb
+++ b/data/rbot/plugins/rss.rb
@@ -332,85 +332,41 @@ class RSSFeedsPlugin < Plugin
DataStream.new([line1, line2].compact.join("\n") % ss, ss)
end
- # Define default RSS filters
- #
- # TODO: load personal ones
+ # Auxiliary method used to define rss output filters
+ def rss_type(key, &block)
+ @bot.register_filter(key, @outkey, &block)
+ end
+
+ # Define default output filters (rss types), and load custom ones.
+ # Custom filters are looked for in the plugin's default filter locations
+ # and in rss/types under botclass.
+ # Preferably, the rss_type method should be used in these files, e.g.:
+ # rss_type :my_type do |s|
+ # line1 = "%{handle} and some %{author} info"
+ # make_stream(line1, nil, s)
+ # end
+ # to define the new type 'my_type'
def define_filters
- @outkey = :"rss.out"
- @bot.register_filter(:headlines, @outkey) { |s|
- line1 = (s[:handle].empty? ? "%{date}" : "%{handle}") << "%{title}"
- make_stream(line1, nil, s)
- }
- @bot.register_filter(:blog, @outkey) { |s|
- author = s[:author] ? (s[:author] + " ") : ""
- abt = s[:category] ? "about #{s[:category]} " : ""
- line1 = "%{handle}%{date}%{author}blogged %{abt}at %{link}"
- line2 = "%{handle}%{title} - %{desc}"
- make_stream(line1, line2, s, :author => author, :abt => abt)
- }
- @bot.register_filter(:photoblog, @outkey) { |s|
- author = s[:author] ? (s[:author] + " ") : ""
- abt = s[:category] ? "under #{s[:category]} " : ""
- line1 = "%{handle}%{date}%{author}added an image %{abt}at %{link}"
- line2 = "%{handle}%{title} - %{desc}"
- make_stream(line1, line2, s, :author => author, :abt => abt)
- }
- @bot.register_filter(:news, @outkey) { |s|
- line1 = "%{handle}%{date}%{title}%{at}%{link}" % s
- line2 = "%{handle}%{date}%{desc}" % s
- make_stream(line1, line2, s)
- }
- @bot.register_filter(:git, @outkey) { |s|
- author = s[:author].sub(/@\S+?\s*>/, "@...>") + " " if s[:author]
- line1 = "%{handle}%{date}%{author}committed %{title}%{at}%{link}"
- make_stream(line1, nil, s, :author => author)
- }
- @bot.register_filter(:forum, @outkey) { |s|
- author = s[:author] ? (s[:author] + " ") : ""
- abt = s[:category] ? "on #{s[:category]} " : ""
- line1 = "%{handle}%{date}%{author}posted %{abt}at %{link}"
- line2 = "%{handle}%{title} - %{desc}"
- make_stream(line1, line2, s, :author => author, :abt => abt)
- }
- @bot.register_filter(:wiki, @outkey) { |s|
- line1 = "%{handle}%{date}%{title}%{at}%{link}"
- line1 << "has been edited by %{author}. %{desc}"
- make_stream(line1, nil, s)
- }
- @bot.register_filter(:gmane, @outkey) { |s|
- line1 = "%{handle}%{date}Message %{title} sent by %{author}. %{desc}"
- make_stream(line1, nil, s)
- }
- @bot.register_filter(:trac, @outkey) { |s|
- author = s[:author].sub(/@\S+?\s*>/, "@...>") + ": " if s[:author]
- line1 = "%{handle}%{date}%{author}%{title}%{at}%{link}"
- line2 = nil
- unless s[:item].title =~ /^(?:Changeset \[(?:[\da-f]+)\]|\(git commit\))/
- line2 = "%{handle}%{date}%{desc}"
- end
- make_stream(line1, line2, s, :author => author)
- }
- @bot.register_filter(:"/.", @outkey) { |s|
- dept = "(from the #{s[:item].slash_department} dept) " rescue nil
- sec = " in section #{s[:item].slash_section}" rescue nil
- line1 = "%{handle}%{date}%{dept}%{title}%{at}%{link} "
- line1 << "(posted by %{author}%{sec})"
- make_stream(line1, nil, s, :dept => dept, :sec => sec)
- }
- @bot.register_filter(:default, @outkey) { |s|
- line1 = "%{handle}%{date}%{title}%{at}%{link}"
- line1 << " (by %{author})" if s[:author]
- make_stream(line1, nil, s)
- }
+ @outkey ||= :"rss.out"
- # Define an HTML info filter too
+ # Define an HTML info filter
@bot.register_filter(:rss, :htmlinfo) { |s| htmlinfo_filter(s) }
-
# This is the output format used by the input filter
- @bot.register_filter(:htmlinfo, @outkey) { |s|
+ rss_type :htmlinfo do |s|
line1 = "%{title}%{at}%{link}"
make_stream(line1, nil, s)
- }
+ end
+
+ # the default filter
+ rss_type :default do |s|
+ line1 = "%{handle}%{date}%{title}%{at}%{link}"
+ line1 << " (by %{author})" if s[:author]
+ make_stream(line1, nil, s)
+ end
+
+ @user_types ||= datafile 'types.rb'
+ load_filters
+ load_filters :path => @user_types
end
FEED_NS = %r{xmlns.*http://(purl\.org/rss|www.w3c.org/1999/02/22-rdf)}