Make plasma libs build.
[amarok.git] / supplementary_scripts / rbot / bugzilla.rb
blob7d7d2d7e49f1f725a39c6c0332cb1a2159aa4af3
1 # Copyright (c) 2005 Diego Pettenò
3 # Permission is hereby granted, free of charge, to any person obtaining a copy
4 # of this software and associated documentation files (the "Software"), to
5 # deal in the Software without restriction, including without limitation the
6 # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 # sell copies of the Software, and to permit persons to whom the Software is
8 # furnished to do so, subject to the following conditions:
10 # The above copyright notice and this permission notice shall be included in
11 # all copies of the Software and its documentation and acknowledgment shall be
12 # given in the documentation and software packages that this Software was
13 # used.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 # THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 require 'open-uri'
24 class Bugzilla
25   def initialize(bughash)
26     @name = bughash["name"]
27     @dataurl = bughash["dataurl"]
28     @reporturl = bughash["reporturl"]
29     @useragent = bughash["useragent"]
30   end
32   def fetch(uri_str)
33     puts "wgetting #{uri_str}"
34     wget = IO.popen("wget -q -O - '" + uri_str + "'")
35     data = wget.read
36     wget.close
37     return data
38   end
40   def summary(bugno)
41     # OpenOffice's issuezilla is tricky, they call it issue_status
42     status_txt = "bug_status"
44     require 'uri'
46     # doc = OpenURI.open_uri(@dataurl.gsub("${BUGID}", bugno))
47     # bugdata = REXML::Document.new(doc)
48     bugdata = REXML::Document.new(fetch(@dataurl.gsub("${BUGID}", bugno)))
50     return "Unable to load bug # #{bugno}" unless bugdata
52     bugxml = bugdata.root.get_elements("bug")[0]
53     unless bugxml
54       bugxml = bugdata.root.get_elements("issue")[0]
55       status_txt = "issue_status"
56     end
58     return "Unable to parse bug # #{bugno}" unless bugxml
60     return "Bug # #{bugno} not found" if bugxml.attribute("status_code").to_s == "404" or
61       bugxml.attribute("error")
63     return "Bug #{bugno}; \"#{bugxml.get_text("short_desc")}\"; #{bugxml.get_text("product")} | #{bugxml.get_text("component")}; #{bugxml.get_text(status_txt)}, #{bugxml.get_text("resolution")}; #{bugxml.get_text("reporter")} -> #{bugxml.get_text("assigned_to")}; #{@reporturl.gsub("${BUGID}", bugno)}"
64   end
65 end
67 class BugzillaPlugin < Plugin
68   def initialize
69     super
70     if FileTest.exists?(File.dirname(__FILE__) + "/bugzillas.db")
71       data = File.open(File.dirname(__FILE__) + "/bugzillas.db").read
72     else
73       data = nil
74     end
76     if data
77       @bugdb = Marshal.load(data)
78     else
79       @bugdb = Array.new
80     end
82     @zillas = Hash.new
83     @bugdb.each { |bugzilla|
84       @zillas[bugzilla["name"]] = Bugzilla.new(bugzilla)
85     }
87     return true
88   end
90   def help(plugin, topic = "")
91     case plugin
92       when "bug"
93         return "bug <bugzilla> <number> => show the data about given bugzilla's bug."
94       when "addzilla"
95         return "addzilla <name> <dataurl> <reporturl> [<useragent>] => add a new bugzilla."
96       when "listzilla"
97         return "listzilla => shows the configured bugzillas"
98     end
99   end
101   def privmsg(m)
102     case m.plugin
103       when "bug"
104         unless m.params =~ /^(\w+) #?(\d+)/
105           m.reply "incorrect usage. " + help(m.plugin)
106           return
107         end
109         bugzilla = $1 # => bugzilla to search for
110         bugno = $2    # => bug number to search for
112         m.reply @zillas[bugzilla].summary(bugno)
113       when "addzilla"
114         unless m.params =~ /^(\S+)\s+(\S+)\s+(\S+)(\s+(\S+))?$/
115           m.reply "incorrect usage. " + help(m.plugin)
116           return
117         end
119         zilla = Hash.new
120         zilla["name"] = $1
121         zilla["dataurl"] = $2
122         zilla["reporturl"] = $3
123         zilla["useragent"] = $4 ? $4 : "rBot/#{$version}"
125         @bugdb.push(zilla)
127         Marshal.dump(@bugdb, File.open(File.dirname(__FILE__) + "/bugzillas.db", "w") )
128         @zillas[zilla["name"]] = Bugzilla.new(zilla)
130         m.reply "Added #{zilla["name"]}"
131       when "listzilla"
132         m.reply @zillas.keys.join(", ")
133     end
134   end
137 plugin = BugzillaPlugin.new
138 plugin.register("bug")
139 plugin.register("addzilla")
140 plugin.register("listzilla")
142 ## Kate modeline: leave at the end
143 # kate: indent-width 2; replace-trailing-space-save 1; space-indent 1; backspace-indents 1;