aboutsummaryrefslogtreecommitdiff
blob: c8d0eb5c57fd5fdf77cb2a3139cb56b952884d8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
# Bugzilla plugin for rbot
# Copyright (c) 2005-2008 Diego Pettenò <flameeyes@gmail.com>
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as
#  published by the Free Software Foundation, either version 3 of the
#  License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Affero General Public License for more details.
#
#  You should have received a copy of the GNU Affero General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

require 'set'
require 'rexml/document'
require 'csv'

require 'httpclient'

class BugzillaPlugin < Plugin

  # Valid statuses
  # 'DONE' and 'OPEN' are special cases that expand to the rest of the statuses in that array
  DONE_STATUS = ['DONE','RESOLVED','VERIFIED','CLOSED']
  OPEN_STATUS = ['OPEN','UNCONFIRMED','NEW','ASSIGNED','REOPENED']
  VALID_RESO  = ['FIXED', 'INVALID', 'WONTFIX', 'LATER', 'REMIND', 'DUPLICATE', 'WORKSFORME', 'CANTFIX', 'NEEDINFO', 'TEST-REQUEST', 'UPSTREAM']
 
  # Each zilla instance may have these parameters
  OPTIONS = [ 'baseurl', 'dataurl', 'showbugurl', 'reporturl', 'useragent' ]

  # Exception class to raise when requesting information about an
  # unknown zilla instance.
  class EMissingZilla < ::Exception
    def initialize(zilla)
      @zilla = zilla
    end

    def message
      "Undefined zilla #{@zilla}"
    end
  end

  # Base Bugzilla exception, to avoid repeating the initialize every
  # time in the next exceptions
  class Exception < ::Exception
    def initialize(zilla, bugno)
      @zilla = zilla
      @bugno = bugno
    end
  end

  # Exception class for an error loading the bug data.
  # It is thrown when REXML can't create a new document from the data
  # returned by HTTP client
  class EErrorLoading < Exception
    def message
      "Unable to load bug ##{@bugno} from #{@zilla}"
    end
  end

  # Exception class for an error parsing the bug data.
  # It is thrown when the XML document does not contain either a <bug>
  # or <issue> element that is recognised.
  class EErrorParsing < Exception
    def message
      "Unable to parse bug ##{@bugno} from #{@zilla}: no valid document element."
    end
  end

  # Exception class for a not found bug.
  # When asking for a non-existant bug, Bugzilla will return a proper
  # status code of 404 on the XML itself.
  class ENotFound < Exception
    def message
      "Bug ##{@bugno} not found in #{@zilla}"
    end
  end

  # Exception class for an invalid bugzilla instance data.
  #
  # When loading a bugzilla instance from the registry, if the data is
  # inconsistent, throw a fit by raising this exception.
  class EInvalidInstance < ::Exception
    def initialize(zilla, extramessage)
      @zilla = zilla
      @extramessage = extramessage
    end

    def message
      "Invalid bugzilla instance #{@zilla}: #{@extramessage}"
    end
  end

  # Class handling the data for a bugzilla instance.
  #
  # This class maintain all the information needed to access the
  # bugzilla, and takes care of getting the information out of it.
  class BugzillaInstance
    attr_reader :name

    def baseurl
      @registry["zilla.#{name}.baseurl"]
    end
    
    def baseurl=(val)
      val = val[0..-2] if val[-1].chr == '/'
      @registry["zilla.#{name}.baseurl"] = val
      @client = nil
    end

    def dataurl
      @dataurl = @registry["zilla.#{name}.dataurl"] unless @dataurl
      
      unless @dataurl
        guess_dataurl
      end

      return @dataurl
    end
    
    def dataurl=(val)
      @dataurl = @registry["zilla.#{name}.dataurl"] = val
      @client = nil
    end

    def showbugurl
      @showbugurl = @registry["zilla.#{name}.showbugurl"] unless @showbugurl
      
      unless @showbugurl
        guess_showbugurl
      end

      return @showbugurl
    end

    def showbugurl=(val)
      @showbugurl = @registry["zilla.#{name}.showbugurl"] = val
      @client = nil
    end

    def reporturl
      @reporturl = @registry["zilla.#{name}.reporturl"] unless @reporturl
      
      unless @reporturl
        guess_reporturl
      end

      return @reporturl
    end

    def reporturl=(val)
      @reporturl = @registry["zilla.#{name}.reporturl"] = val
      @client = nil
    end

    def useragent
      agent = @registry["zilla.#{name}.baseurl"]
      return agent if agent

      return "rbot/$version"
    end

    def useragent=(val)
      @registry["zilla.#{name}.useragent"] = val
      @client = nil
    end

    def lastseenid
      return 0 unless @registry["zilla.#{name}.lastseenid"]
      return @registry["zilla.#{name}.lastseenid"]
    end

    def lastseenid=(val)
      @registry["zilla.#{name}.lastseenid"] = val
    end

    def create(name, baseurl, registry)
      raise EInvalidInstance("", "Missing instance name") unless name
      raise EInvalidInstance("", "Missing instance base URL") unless baseurl
      raise EInvalidInstance("", "Missing registry instance") unless registry

      @registry = registry

      @name = name
      self.baseurl = baseurl

      # Do this otherwise the array is not saved properly in the registry
      @registry["zillas"] = (@registry["zillas"] << @name)
    end

    def delete
      @registry["zillas"] = (@registry["zillas"] - [@name])
      
      OPTIONS.each do |s|
        @registry.delete("zilla.#{name}.#{s}")
      end
    end

    def load(name, registry)
      raise EInvalidInstance("", "Missing instance name") unless name
      raise EInvalidInstance("", "Missing registry instance") unless registry

      @registry = registry
      @name = name
    end

    
    # Guess at the public URL to show for a bug.
    def guess_showbugurl
      @showbugurl = "#{baseurl}/show_bug.cgi?id=@BUGNO@"
    end
  
    # Guess at the URL for the XML format of any given bug.
    # TODO: For Bugzilla3, this needs to be xml.cgi instead of an extra param
    # to show_bug.cgi
    def guess_dataurl
      @dataurl = "#{showbugurl}&ctype=xml"
    end
   
    # Guess at the default URL to use for generating CSV tables format out of reports.
    def guess_reporturl
      @reporturl = "#{baseurl}/report.cgi?action=wrap&ctype=csv&format=table"
    end

    # Provide an HTTP client connected to the current Bugzilla instance.
    #
    # If a client is already open it is returned, otherwise a new one
    # is opened.
    # 
    # SSL support is always set not to verify the validity of the
    # certificate, as a lot of Bugzilla instances used for Free
    # Software development don't have proper SSL certificates, but
    # just self-signed certificates.
    def client
      return @client if @client

      @client = HTTPClient.new(nil, useragent)

      return @client
    end

    # Return the summary for a given bug.
    def summary(bugno)
      bugdata = REXML::Document.new(client.get_content(dataurl.gsub("@BUGNO@", bugno)))

      raise EErrorLoading.new(name, bugno) unless bugdata

      # OpenOffice's issuezilla is tricky, they call it issue_status, so
      # we have to consider the alternative in case there is an <issue>
      # as document element.
      bugxml = bugdata.root.get_elements("bug")[0]
      bugxml = bugdata.root.get_elements("issue")[0] unless bugxml
      
      raise EErrorParsing.new(name, bugno) unless bugxml

      if bugxml.attribute("status_code").to_s == "404" or
          bugxml.attribute("error").to_s.downcase == "notfound"
        raise ENotFound.new(name, bugno)
      end

      product_component =
        "#{bugxml.get_text("product")} | #{bugxml.get_text("component")}".
        chomp(" | ")

      status =
        "#{bugxml.get_text("bug_status")}#{bugxml.get_text("issue_status")}, #{bugxml.get_text("resolution")}".
        chomp(", ")
      
      return "" +
        "Bug #{bugno}; " +
        "\"#{bugxml.get_text("short_desc")}\"; " +
        "#{product_component}; " +
        "#{status}; " +
        "#{bugxml.get_text("reporter")} -> #{bugxml.get_text("assigned_to")}; " +
        "#{showbugurl.gsub('@BUGNO@', bugno)}"
    end

    def add_announcement(channel_name)
      @registry["zilla.#{@name}.announcements"] = Set.new unless @registry["zilla.#{@name}.announcements"]

      @registry["zilla.#{@name}.announcements"] = @registry["zilla.#{@name}.announcements"] + [channel_name]
    end

    def delete_announcement(channel_name)
      return unless @registry["zilla.#{@name}.announcements"]

      @registry["zilla.#{@name}.announcements"] = @registry["zilla.#{@name}.announcements"] - [channel_name]
    end

    def announce(bot)
      buglist = CSV::Reader.create(client.get_content("#{baseurl}/buglist.cgi?ctype=csv&field0-0-0=bug_id&query_format=advaced&remaction=&type0-0-0=greaterthan&value0-0-0=#{lastseenid}&order=bugs.bug_id")).to_a
      buglist.delete_at(0)
      upper_bound = [buglist.size, 5].min
      buglist[-upper_bound..-1].each do |bug|
        bugsummary = summary(bug[0])

        @registry["zilla.#{@name}.announcements"].each do |chan|
          bot.say chan, "New bug: #{bugsummary}"
        end
      end

      lastseenid = buglist[-1][0].to_i
    end

    def report(params)
      url = "#{reporturl}&#{params}"
      reportdata = CSV::Reader.create(client.get_content(url)).to_a
      return reportdata
    end

  end

  # Initialise the bugzilla plugin.
  #
  def initialize
    super
    
    @zillas = {}

    if @registry["zillas"]
      @registry["zillas"].each do |zilla|
        instance = BugzillaInstance.new
        instance.load(zilla, @registry)
        @zillas[zilla] = instance
      end
    else
      @registry["zillas"] = Array.new
    end

    @defaults = Hash.new
    if @registry["channel_defaults"]
      channel_defaults_reload
    else
      @registry["channel_defaults"] = Hash.new
    end

    @bot.timer.add(300) {
      poll_zillas
    }
  end

  # Check for the existence of zilla in the registry.
  # This function checks if a given zilla is present in the registry
  # file by checking for presence of a zilla. entry. It raises
  # an exception if it is missing.
  def check_zilla(name)
    raise EMissingZilla.new(name) unless
      @zillas.has_key?(name)
  end

  # Function "eavesdropping" on all the messages the bot receives.
  #
  # This function is used to check if an user requested bug
  # information inline in the text of a message rather than directly
  # to the bot.
  def listen(m)
    return if m.address?
    return if m.message !~ /\bbug\b.*\b#?([0-9]+)/
    bugno = $1
    
    return unless @defaults[m.channel][:eavesdrop]
    return unless @defaults[m.channel][:zilla]
    
    m.reply @defaults[m.channel][:zilla].summary(bugno)
  end

  # Function checking when a new channel is joined
  #
  # This function will calculate the channel default.
  def join(m)
    return unless m.address?

    @registry["channel_defaults"].each do |chanrexp, defaults|
      if m.channel.to_s =~ Regexp.new(chanrexp)
        @defaults[m.channel] = {
          :eavesdrop => defaults[:eavesdrop],
          :zilla => @zillas[defaults[:zilla]]
        }
        break
      end
    end
  end
  
  # Answer to a bug information request
  #
  # This is the main function of the plugin, answering to bug
  # information requests from users. If the user provides a named
  # zilla, use that, otherwise see if the channel the user asked in
  # has a default.
  def bug(m, params)
    begin
      bugno = params[:number].chomp("#")

      if params[:zilla]
        check_zilla(params[:zilla])
        m.reply @zillas[params[:zilla]].summary(bugno)
      elsif @defaults[m.channel][:zilla]
        m.reply @defaults[m.channel][:zilla].summary(bugno)
      else
        m.reply "Wrong parameters, see 'help bug' for help."
      end
    rescue ::Exception => e
      m.reply e.message
    end
  end

  # Produce architecture statistics using Bugzilla reports
  #
  # Using the bugzilla reporting functionality, we can produce a
  # simple report of bugs by architecture, for any specific
  # status/resolution.
  # x_axis_field=rep_platform
  # x_axis_field=rep_platform&bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED
  # x_axis_field=rep_platform&resolution=FIXED&resolution=INVALID&resolution=WONTFIX
  def archstats(m, params)
    begin
      status = params[:status]
      reso = params[:reso]

      status = status.split(/,/)
      status.each do |s|
          reso = [] unless DONE_STATUS.include?(s)
          raise ENotFound.new("Invalid status (#{s}), see 'help archstats' for help.") if not DONE_STATUS.include?(s) and not OPEN_STATUS.include?(s) and s != 'ALL'
      end
      reso = reso.split(/,/) if reso and reso.is_a?(String)
      reso.each do |r|
            raise ENotFound.new("Invalid resolution (#{r}), see 'help archstats' for help.") if not VALID_RESO.include?(r)
      end
      query = 'x_axis_field=rep_platform'
      status.each { |s| query += "&bug_status=#{s}" }
      reso.each { |r| query += "&resolution=#{r}" }

      title = "Platform bug totals"
      if status or reso
          title += " (#{status.join(',')}"
          title += "/#{reso.join(',')} " if reso
          title += ")"
      end

      if params[:zilla]
        check_zilla(params[:zilla])
        results = @zillas[params[:zilla]].report(query)
        msg = results.each { |b| " #{b[0]}(#{b[1]})" }
        m.reply "#{title} #{msg}"
      elsif @defaults[m.channel][:zilla]
        results = @defaults[m.channel][:zilla].report(query)
        msg = results.each { |b| " #{b[0]}(#{b[1]})" }
        m.reply "#{title} #{msg}"
      else
        m.reply "Wrong parameters, see 'help archstats' for help."
      end
    rescue ::Exception => e
      m.reply e.message
    end
  end

  # Adds a new instance to the available instances
  #
  # This function creates a new BugzillaInstance object, loads the new
  # data on it, and then adds it to the hash of zillas.
  #
  # Only the base url of the instance is needed, the rest of the
  # parameters will either default or get guessed by the bot.
  #
  # To override the settings, use the set zilla command
  def instance_add(m, params)
    if @zillas.has_key?("#{params[:zilla]}")
      m.reply "Bugzilla #{params[:zilla]} already present."
      return
    end

    instance = BugzillaInstance.new
    instance.create(params[:zilla], params[:baseurl], @registry)
    @zillas[params[:zilla]] = instance

    m.reply "Added #{params[:zilla]}"
  end

  # Set parameters for the given bugzilla
  #
  # There is a special bit of behavior here. If you want to UNSET an option, so
  # that the default is used, then set it to 'nil'.
  def instance_set(m, params)
    begin
      # This is to save us from having an 'unset' command
      params[:value] = nil if params[:value].match(/^nil$/)
      
      # We are evil
      @zillas[params[:zilla]].send("#{params[:setting]}=", params[:value])

    rescue ::Exception => e
      m.reply e.message
    end
  end

  # Removes an instance to the available instances.
  #
  # The opposite of instance_add, this function deletes an instance of
  # Bugzilla or Issuezilla from the registry.
  def instance_delete(m, params)
    @zillas[params[:zilla]].delete
    @zillas.delete(params[:zilla])

    m.okay
  end

  # Shows the list of available instances to the users.
  def instance_list(m, params)
    m.reply @registry["zillas"].join(", ")
  end

  # Show the information known about the bugzilla.
  #
  # This function emits a summary of the data regarding the bugzilla,
  # its output can be used to set the bugzilla back up again on this
  # or other instances.
  def instance_show(m, params)
    begin
      check_zilla(params[:zilla])
      
      msg = "#{params[:zilla]}"
      for s in OPTIONS
        if params[:full] == 'full'
          o = @zillas[params[:zilla]].send(s)
        elsif params[:full] == 'registry'
          o = @registry['zilla.' + params[:zilla] + ".#{s}"]
        end
        msg += " #{s}: #{o}" if o
      end

      m.reply msg
    rescue ::Exception => e
      m.reply e.message
    end
  end

  # Reloads the defaults for the current joined channels
  #
  # This function scans through the list of channel defaults found in
  # the registry and report them in the locally accessed objects.
  def channel_defaults_reload
    @bot.server.channels.each do |chan|
      @registry["channel_defaults"].each do |chanrexp, defaults|
        if chan.to_s =~ Regexp.new(chanrexp)
          @defaults[chan] = {
            :eavesdrop => defaults[:eavesdrop],
            :zilla => @zillas[defaults[:zilla]]
          }
          break
        end
      end
    end
  end

  # Sets the default zilla for the given channel regexp
  #
  # The default zilla is the zilla used when an user requests info
  # about a bug number, without saying which zilla to take the data
  # from.
  def channel_defaults_set(m, params)
    begin
      @registry["channel_defaults"] = @registry["channel_defaults"].merge(params[:channel] => { :zilla => params[:zilla], :eavesdrop => params[:eavesdrop] == "on" })
      channel_defaults_reload

      m.okay
    rescue ::Exception => e
      m.reply e.backtrace
    end
  end

  # Unsets the default zilla for the given channel regexp
  def channel_defaults_unset(m, params)
    begin
      @registry["channel_defaults"].delete(params[:channel])
      channel_defaults_reload

      m.okay
    rescue ::Exception => e
      m.reply e.message
    end
  end

  def plugin_sources(m, params)
    m.reply "http://www.flameeyes.eu/projects#rbot-bugzilla"
  end

  def plugin_credits(m, params)
    m.reply "Copyright (C) 2005-2008 Diego Pettenò. Distributed under Affero General Public License version 3."
  end

  # Adds announcement for bugs on the given zilla to the channel
  #
  # When this function is called, the given zilla is added to the list
  # of zilla to announce in the given channel.
  #
  # Zillas being announced mean they get polled at a fixed interval
  # for new bugs, and the summary for those is sent to the channel
  # asking for them.
  #
  # Actually, it's the channel being added to the announcement for
  # the given zilla, as that makes it quite easier to track down which
  # ones to poll.
  def channel_announcement_add(m, params)
    begin
      @zillas[params[:zilla]].add_announcement params[:channel]
      m.okay
    rescue ::Exception => e
      m.reply e.message
    end
  end

  # Removes an announcement of a given zilla on a channel.
  #
  # This basically is an undo function for the function above.
  def channel_announcement_delete(m, params)
    begin
      @zillas[params[:zilla]].delete_announcement(params[:channel])
      m.okay
    rescue ::Exception => e
      m.reply e.message
    end
  end

  def poll_zillas
    @zillas.each do |name, zilla|
      zilla.announce(@bot)
    end
  end
 
  # Help strings to give the users when they are asking for it.
  @@help_zilla = {
    "bug" => "bug #{Bold}[bugzilla]#{Bold} #{Bold}number#{Bold} : show the data about given bugzilla's bug.",

    "archstats" => "archstats #{Bold}[bugzilla]#{Bold} #{Bold}[status]#{Bold} #{Bold}[reso]#{Bold} : show architecture summaries for given bug statuses.",

    "zilla"                 => "zilla #{Bold}instance#{Bold}|#{Bold}default#{Bold}|#{Bold}source#{Bold}|#{Bold}credits#{Bold} : manages bugzilla lists.",
    "zilla instance"        => "zilla instance #{Bold}add#{Bold}|#{Bold}delete#{Bold}|#{Bold}set#{Bold}|#{Bold}show#{Bold}|#{Bold}list#{Bold} : handle bugzilla instances",
    "zilla instance add"    => "zilla instance add #{Bold}name#{Bold} #{Bold}baseurl#{Bold} : adds a new bugzilla (use \#{bugno} in URLs to replace the bug number)",
    "zilla instance delete" => "zilla instance delete #{Bold}name#{Bold} : delete the named bugzilla",
    "zilla instance set"    => "zilla instance set #{Bold}name#{Bold} #{Bold}option#{Bold} #{Bold}value{#Bold} : set the option to a given value for the zilla. Valid options are baseurl, dataurl, showbugurl, useragent",
    "zilla instance list"   => "zilla instance list : shows current querable bugzilla instancess",
    "zilla instance show"   => "zilla instance show #{Bold}name#{Bold} : shows the configuration for the named bugzilla.",

    "zilla default"       => "zilla default #{Bold}set#{Bold}|#{Bold}unset#{Bold} : handles default zilla for channels",
    "zilla default set"   => "zilla default set #{Bold}channel_name#{Bold} #{Bold}zilla_name#{Bold} #{Bold}eavesdrop_on|off#{Bold} : sets the default zilla for a given channel, use on or off to enable or disable eavesdropping for bug references.",
    "zilla default unset" => "zilla default unset #{Bold}channel_name#{Bold} : unsets the default zilla for a given channel",

    "zilla source"  => "zilla source : shows a link to the plugin's sources.",
    "zilla credits" => "zilla credits : shows the plugin's credits and license."
  }

  def help(plugin, topic = "")
    cmd = plugin
    cmd += " "+topic if topic.length > 0
    if @@help_zilla.has_key?(cmd)
      return @@help_zilla[cmd]
    else
      return "no help available for #{cmd}"
    end
  end
end

plugin = BugzillaPlugin.new

plugin.default_auth( 'modify', false )
plugin.default_auth( 'view', true )

plugin.map 'bug :zilla :number',
  :requirements => {
    :number => /^#?\d+$/,
    :zilla => /^[^ ]+$/
  },
  :defaults => { :zilla => nil },
  :action => 'bug',
  :auth_path => 'view'

plugin.map 'archstats :zilla :status :reso',
  :action => 'archstats',
  :requirements => {
    :status => /^[\w,]+$/,
    :resolution => /^[\w,]+$/,
    :zilla => /^[^ ]+$/
  },
  :defaults => { 
    :status => 'ALL', 
    :resolution => [],
    :zilla => nil
  },
  :auth_path => 'view'

plugin.map 'zilla instance add :zilla :baseurl',
  :action => 'instance_add',
  :requirements => {
    :zilla => /^[^ ]+$/,
    :baseurl => /^https?:\/\/.*/
  },
  :auth_path => 'modify'

plugin.map 'zilla instance set :zilla :setting :value',
  :action => 'instance_set',
  :requirements => {
    :zilla => /^[^\. ]+$/,
    :setting => /^(baseurl|dataurl|showbugurl|reporturl|useragent)$/
  },
  :auth_path => 'modify'

plugin.map 'zilla instance delete :zilla',
  :action => 'instance_delete',
  :requirements => {
    :zilla => /^[^ ]+$/
  },
  :auth_path => 'modify'

plugin.map 'zilla instance list',
  :action => 'instance_list',
  :auth_path => 'view'

plugin.map 'zilla instance show :zilla :full',
  :action => 'instance_show',
  :requirements => {
    :zilla => /^[^ ]+$/,
    :full => /^full|registry$/
  },
  :defaults => { :full => "registry" },
  :auth_path => 'view'

plugin.map 'zilla default set :channel :zilla :eavesdrop',
  :action => 'channel_defaults_set',
  :requirements => {
    :channel => /^[^ ]+$/,
    :zilla => /^[^ ]+$/
  },
  :defaults => { :eavesdrop => "off" },
  :auth_path => 'modify'

plugin.map 'zilla default unset :channel',
  :action => 'channel_defaults_unset',
  :requirements => {
    :channel => /^#[^ ]+$/
  },
  :auth_path => 'modify'

plugin.map 'zilla announcement add :zilla :channel',
  :action => 'channel_announcement_add',
  :requirements => {
    :channel => /^#[^ ]+$/,
    :zilla => /^[^ ]+$/
  },
  :auth_path => 'modify'

plugin.map 'zilla announcement remove :zilla :channel',
  :action => 'channel_announcement_delete',
  :requirements => {
    :channel => /^#[^ ]+$/,
    :zilla => /^[^ ]+$/
  },
  :auth_path => 'modify'

plugin.map 'zilla source',
  :action => 'plugin_sources',
  :auth_path => 'view'

plugin.map 'zilla credits',
  :action => 'plugin_credits',
  :auth_path => 'view'