~ubuntu-ru-irc/+junk/irckit

« back to all changes in this revision

Viewing changes to ubuntuhelp/ubuntubot/plugins/TracBot/plugin.py

  • Committer: rmPIC30 at gmail
  • Date: 2010-07-13 09:08:58 UTC
  • Revision ID: rmpic30@gmail.com-20100713090858-w5kkmk093hx38cen
Добавляю бота

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
###
 
2
# Copyright (c) 2005, Andrew Harvey
 
3
# All rights reserved.
 
4
#
 
5
# Redistribution and use in source and binary forms, with or without
 
6
# modification, are permitted provided that the following conditions are met:
 
7
#
 
8
#   * Redistributions of source code must retain the above copyright notice,
 
9
#     this list of conditions, and the following disclaimer.
 
10
#   * Redistributions in binary form must reproduce the above copyright notice,
 
11
#     this list of conditions, and the following disclaimer in the
 
12
#     documentation and/or other materials provided with the distribution.
 
13
#   * Neither the name of the author of this software nor the name of
 
14
#     contributors to this software may be used to endorse or promote products
 
15
#     derived from this software without specific prior written consent.
 
16
#
 
17
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
18
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
19
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
20
# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 
21
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
22
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
23
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
24
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
25
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
26
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
27
# POSSIBILITY OF SUCH DAMAGE.
 
28
###
 
29
 
 
30
import supybot.utils as utils
 
31
from supybot.commands import *
 
32
import supybot.utils.web as web
 
33
import supybot.plugins as plugins
 
34
import supybot.ircutils as ircutils
 
35
import supybot.callbacks as callbacks
 
36
import supybot.ircmsgs as ircmsgs
 
37
 
 
38
import urllib
 
39
import urllib2
 
40
 
 
41
class TracBot(callbacks.Plugin):
 
42
    """TracBot plugin provides plugins for use with the Trac issue tracking
 
43
    system.
 
44
    """
 
45
 
 
46
    def ticket(self, irc, msg, args, channel, ticketno):
 
47
        """<ticket>
 
48
        returns a link to that ticket on the Trac Site
 
49
        """
 
50
        if self.registryValue('tracBase', channel) is not "":
 
51
            base = self.registryValue('tracBase', channel) 
 
52
            reply = '%s/ticket/%d' % (base, ticketno)
 
53
        else:
 
54
            reply = "TracBot does not have a tracBase defined for this channel"
 
55
        irc.reply(reply) 
 
56
    ticket = wrap(ticket,['channel', 'int'])
 
57
 
 
58
    def wiki(self, irc, msg, args, channel, page):
 
59
        """<wikipage>
 
60
        returns a link to the wiki page on the Trac Site
 
61
        """
 
62
        if self.registryValue('tracBase', channel) is not "":
 
63
            base = self.registryValue('tracBase', channel)
 
64
            reply = base + '/wiki/' + page
 
65
        else:
 
66
            reply = "TracBot does not have a tracBase defined for this channel"
 
67
        irc.reply(reply)
 
68
    wiki = wrap(wiki,['channel', 'somethingWithoutSpaces'])
 
69
 
 
70
    def show(self, irc, msg, args, nick, page):
 
71
        """<nick> <page>
 
72
        Gives <nick> the link to <page>. Acceptable values are currently
 
73
        "bugs", "troubleshooting" and "requests"
 
74
        """
 
75
        if page == "bugs":
 
76
            reply = nick +': Learn about reporting bugs here => http://trac.adiumx.com/wiki/ReportingBugs'
 
77
        elif page == "requests":
 
78
            reply = nick + ': Learn about requesting features here => http://trac.adiumx.com/wiki/RequestingFeatures'
 
79
        elif page == "troubleshooting":
 
80
            reply = nick + ': Learn about troubleshooting AdiumX here => http://trac.adiumx.com/wiki/Troubleshooting'
 
81
        irc.reply(reply)
 
82
    show = wrap(show, ['nick', ("literal", ("bugs", "troubleshooting", "requests"), "The only currently available options are bugs, troubleshooting and requests")])
 
83
 
 
84
    def tracsearch(self, irc, msg, args, channel, type, page):
 
85
        """[<type>] <searchstring> 
 
86
        returns a link to a search of the Adium Trac Site for <searchstring>
 
87
        <type> can either be "wiki", "tickets", "changesets" or "commits"
 
88
        """
 
89
        if self.registryValue('tracBase', channel) is not "":
 
90
            base = self.registryValue('tracBase', channel)
 
91
            urlised = utils.web.urlquote(page)
 
92
            if type == "wiki":
 
93
                reply = base + '/search?q=' + urlised + '&wiki=on'
 
94
            elif type == "tickets":
 
95
                reply = base + '/search?q=' + urlised + '&ticket=on'
 
96
            elif type == "changesets": 
 
97
                reply = base + '/search?q=' + urlised + '&changeset=on'
 
98
            elif type == "commits":
 
99
                reply = base + '/search?q=' + urlised + '&changeset=on'
 
100
            else:
 
101
                reply = base + '/search?q=' + urlised + '&wiki=on&ticket=on&changeset=on'
 
102
        else:
 
103
            reply = "TracBot does not have a tracBase defined for this channel"
 
104
        irc.reply(reply)
 
105
    tracsearch = wrap(tracsearch,['channel', optional(('literal', ("wiki", "tickets", "changesets",  "commits"), "Borked.")), 'text'])
 
106
    
 
107
    def newticket(self, irc, msg, args, channel):
 
108
        """
 
109
        Gives the link to the trac ticket submission page.
 
110
        """
 
111
        if self.registryValue('tracBase', channel) is not "":
 
112
            base = self.registryValue('tracBase', channel)
 
113
            reply = base + '/newticket'
 
114
        else:
 
115
            reply = "TracBot does not have a tracBase defined for this channel"
 
116
        irc.reply(reply)
 
117
    newticket = wrap(newticket, ['channel'])
 
118
    
 
119
    
 
120
    def changeset(self, irc, msg, args, channel, changesetno):
 
121
        """<changeset number>
 
122
        Returns a link to changeset <changeset number> on the Adium trac site
 
123
        """
 
124
        if self.registryValue('tracBase', channel) is not "":
 
125
            base = self.registryValue('tracBase', channel)
 
126
            reply = '%s/changeset/%d' % (base, changesetno)
 
127
        else:
 
128
            reply = "TracBot does not have a tracBase defined for this channel"
 
129
        irc.reply(reply)
 
130
    changeset = wrap(changeset, ['channel', 'int'])
 
131
 
 
132
 
 
133
    def maketicket(self, irc, msg, args, channel, optlist, summary, description):
 
134
        """[--type=<type>] [--reporter=<reporter>] [--component=<component>] [--owner=<owner>] \
 
135
           [--milestone=<milestone>] [--version=<version>] "<summary>" <description> 
 
136
 
 
137
           Submit a new ticket with summary <summary> and description <description> with the supplied
 
138
           options.
 
139
        """
 
140
        if self.registryValue('tracBase', channel) is not "":
 
141
            base = self.registryValue('tracBase', channel)
 
142
            url = '%s/newticket' % base.rstrip('/')
 
143
            postdata={'reporter': msg.nick,
 
144
                      'summary': summary,
 
145
                      'description': description,
 
146
                      'action': 'create',
 
147
                      'status': 'new'}
 
148
            for (opt, value) in optlist:
 
149
                postdata[opt] = value
 
150
            try:
 
151
                page = self._openUrl(url, postdata)
 
152
            except:
 
153
                page = ''
 
154
            if 'id="ticket"' in page:
 
155
                number = page.split('<title>', 1)[-1].split(' ', 1)[0]
 
156
                number = number.strip('#')
 
157
                reply = ('New ticket at: %s/ticket/%s' %
 
158
                         (base.rstrip('/'), number))
 
159
            else:
 
160
                reply = 'Posting a new ticket failed.'
 
161
        else:
 
162
            reply = 'TracBot does not have a tracBase defined for this channel.'
 
163
        irc.reply(reply)
 
164
    maketicket = wrap(maketicket, ['channel',
 
165
                                   getopts({'type': None,
 
166
                                            'reporter': None,
 
167
                                            'component': None,
 
168
                                            'version': None,
 
169
                                            'milestone': None,
 
170
                                            'owner': None}),
 
171
                                   'something', 'text'])
 
172
 
 
173
    def _openUrl(self, url, postdata={}):
 
174
        data = urllib.urlencode(postdata)
 
175
        request = urllib2.Request(url, data=data)
 
176
        return web.getUrl(request)
 
177
 
 
178
    def wikipedia(self, irc, msg, args, wiki_string):
 
179
        """<wiki page>
 
180
        Returns a link to the page on wikipedia for <wiki string>
 
181
        """
 
182
        import string, urllib
 
183
        to_underscore = string.maketrans(' ', '_')
 
184
        reply = "http://en.wikipedia.org/wiki/" + urllib.quote(wiki_string.translate(to_underscore))
 
185
        irc.reply(reply)
 
186
    wikipedia = wrap(wikipedia, ['text'])
 
187
 
 
188
Class = TracBot
 
189
 
 
190
    
 
191
 
 
192
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: