Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
12490 | malex | 1 | ### |
2 | # Copyright (c) 2006, 2007, Oleksandr Moskalenko |
||
3 | # All rights reserved. |
||
4 | # |
||
5 | # This program is free software: you can redistribute it and/or modify |
||
6 | # it under the terms of the GNU General Public License as published by |
||
7 | # the Free Software Foundation, either version 3 of the License, or |
||
8 | # (at your option) any later version. |
||
9 | # |
||
10 | # This program is distributed in the hope that it will be useful, |
||
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
13 | # GNU General Public License for more details. |
||
14 | # |
||
15 | # You should have received a copy of the GNU General Public License |
||
16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
17 | ### |
||
18 | |||
19 | import supybot.conf as conf |
||
20 | import supybot.utils as utils |
||
21 | from supybot.commands import * |
||
22 | import supybot.plugins as plugins |
||
23 | import supybot.ircutils as ircutils |
||
24 | import supybot.ircmsgs as ircmsgs |
||
25 | import supybot.callbacks as callbacks |
||
26 | import urllib2 |
||
27 | import re |
||
28 | from BeautifulSoup import BeautifulSoup |
||
29 | #from BeautifulSoup import Null |
||
30 | |||
31 | class Utils: |
||
32 | """This class contains utils used by several commands.""" |
||
33 | def __init__(self): |
||
34 | pass |
||
35 | def _makeurl(self, server, bugno): |
||
36 | self.bugnumber = str(bugno) |
||
37 | self.server = server |
||
38 | self.url = 'http://'+self.server+'/view.php?id='+self.bugnumber |
||
39 | return self.url |
||
40 | def _bugdata(self, url): |
||
41 | self.url = url |
||
42 | try: |
||
43 | self.srchtml = urllib2.urlopen(self.url).read() |
||
44 | except urllib2.URLError: |
||
45 | print "Cannot fetch the bug page." |
||
46 | self.soup = BeautifulSoup(self.srchtml) |
||
47 | return self.soup |
||
48 | |||
49 | class Mantis(callbacks.PluginRegexp): |
||
50 | """This plugin is non-interactive. No help is needed.""" |
||
51 | regexps = ['bugUrl'] #,'titleSnarfer'] |
||
52 | def __init__(self, irc): |
||
53 | self.__parent = super(Mantis, self) |
||
54 | self.__parent.__init__(irc) |
||
55 | if self.checkbts() == True: |
||
56 | self.btsstatus = 'Mantis BTS is accessible' |
||
57 | else: |
||
58 | self.btsstatus = 'Cannot access the BTS front page' |
||
59 | self.mantisutils = Utils() |
||
60 | |||
61 | def bugtitle(self, irc, msg, args, number): |
||
62 | """<number> |
||
63 | |||
64 | Returns the Title of the bug number <number>. Only positive integers |
||
65 | are accepted as bug numbers. |
||
66 | """ |
||
67 | self.bugno = number |
||
68 | self.url = self.mantisutils._makeurl(self.server, self.bugno) |
||
69 | try: |
||
70 | self.soup = self.mantisutils._bugdata(self.url) |
||
71 | self.title = self.soup.html.head.title.contents |
||
72 | if self.title[0] == 'Mantis Issue Tracker for Scribus': |
||
73 | irc.reply("Sorry, it appears that this bug doesn't exist") |
||
74 | elif self.title == None: |
||
75 | irc.reply("Sorry, this bug is not accessible") |
||
76 | else: |
||
77 | self.shorttitle = self.title[0].strip().split(" - Mantis Issue Tracker")[0] |
||
78 | irc.reply(self.shorttitle) |
||
79 | except: |
||
80 | irc.reply("Sorry, an unknown error has occured. I'll go cry for a while") |
||
81 | |||
82 | bugtitle = wrap(bugtitle, ['int']) |
||
83 | |||
84 | def bugstatus(self, irc, msg, args, number): |
||
85 | """<number> |
||
86 | |||
87 | Returns the status of the bug number <number>. Only positive integers |
||
88 | are accepted as bug numbers. |
||
89 | """ |
||
90 | self.bugno = number |
||
91 | self.url = self.mantisutils._makeurl(self.server, self.bugno) |
||
92 | try: |
||
93 | self.soup = self.mantisutils._bugdata(self.url) |
||
94 | self.title = self.soup.html.head.title.contents |
||
95 | if self.title[0] == 'Mantis Issue Tracker for Scribus': |
||
96 | irc.reply("Sorry, it appears that this bug doesn't exist") |
||
97 | elif self.title == None: |
||
98 | irc.reply("Sorry, this bug is not accessible") |
||
99 | else: |
||
100 | self.status = self.soup.firstText(re.compile('--\ Status')).findNext('td').findNext('td').contents[0].strip() |
||
101 | irc.reply(self.status) |
||
102 | except URLError, e: |
||
103 | irc.reply(str(e)) |
||
104 | |||
105 | bugstatus = wrap(bugstatus, ['int']) |
||
106 | |||
107 | def bugreporter(self, irc, msg, args, number): |
||
108 | """<number> |
||
109 | |||
110 | Returns the reporter of the bug number <number>. Only positive integers |
||
111 | are accepted as bug numbers. |
||
112 | """ |
||
113 | self.bugno = number |
||
114 | self.url = self.mantisutils._makeurl(self.server, self.bugno) |
||
115 | try: |
||
116 | self.soup = self.mantisutils._bugdata(self.url) |
||
117 | self.title = self.soup.html.head.title.contents |
||
118 | if self.title[0] == 'Mantis Issue Tracker for Scribus': |
||
119 | irc.reply("Sorry, it appears that this bug doesn't exist") |
||
120 | elif self.title == None: |
||
121 | irc.reply("Sorry, this bug is not accessible") |
||
122 | else: |
||
123 | self.reporter = self.soup.firstText(re.compile('Reporter')).findNext('td').findNext('td').contents[0].strip() |
||
124 | irc.reply(self.reporter) |
||
125 | except Error, e: |
||
126 | irc.reply(str(e)) |
||
127 | |||
128 | bugreporter = wrap(bugreporter, ['int']) |
||
129 | |||
130 | def assignedto(self, irc, msg, args, number): |
||
131 | """<number> |
||
132 | |||
133 | Returns login name of the developer the bug number <number> is assigned |
||
134 | to. Only positive integers are accepted as bug numbers. |
||
135 | """ |
||
136 | self.bugno = number |
||
137 | self.url = self.mantisutils._makeurl(self.server, self.bugno) |
||
138 | try: |
||
139 | self.soup = self.mantisutils._bugdata(self.url) |
||
140 | self.title = self.soup.html.head.title.contents |
||
141 | if self.title[0] == 'Mantis Issue Tracker for Scribus': |
||
142 | irc.reply("Sorry, it appears that this bug doesn't exist") |
||
143 | elif self.title == None: |
||
144 | irc.reply("Sorry, this bug is not accessible") |
||
145 | else: |
||
146 | self.assignee = self.soup.firstText(re.compile('Assigned\ To')).findNext('td').contents[0].strip() |
||
147 | irc.reply(self.assignee) |
||
148 | except Error, e: |
||
149 | irc.reply(str(e)) |
||
150 | |||
151 | assignedto = wrap(assignedto, ['int']) |
||
152 | |||
153 | def checkbts(self): |
||
154 | """ Example: http://bugs.scribus.net/main_page.php""" |
||
155 | self.defaultsrv='bugs.scribus.net' |
||
156 | self.confsrv = conf.supybot.plugins.Mantis.server() |
||
157 | if not self.confsrv: |
||
158 | self.server = self.defaultsrv |
||
159 | else: |
||
160 | self.server = self.confsrv |
||
161 | self.btsurl = 'http://'+self.server+'/main_page.php' |
||
162 | #self.btsurl = 'http://'+self.defaultsrv+'/main_page.php' |
||
163 | try: |
||
164 | self.mainpage = urllib2.urlopen(self.btsurl) |
||
165 | self.log.debug('Checking the bts at %u', self.btsurl) |
||
166 | return True |
||
167 | except URLError: |
||
168 | print "Cannot open the BTS main page. Unloading plugin." |
||
169 | |||
170 | def bugUrl(self, irc, msg, m): |
||
171 | r"^#\d{4,9}" |
||
172 | """Returns the URL for the bug number passed into the channel.""" |
||
173 | self.msg = msg |
||
174 | self.channel = self.msg.args[0] |
||
175 | if irc.isChannel(self.channel): |
||
176 | if ircmsgs.isAction(self.msg): |
||
177 | self.text = ircmsgs.unAction(self.msg) |
||
178 | else: |
||
179 | self.text = self.msg.args[1] |
||
180 | self.buglist = re.findall(r"^#\d{4,9}", self.text) |
||
181 | for self.bugid in self.buglist: |
||
182 | self.bugno = self.bugid.rstrip()[1:] |
||
183 | self.url = self.mantisutils._makeurl(self.server,self.bugno) |
||
184 | try: |
||
185 | self.soup = self.mantisutils._bugdata(self.url) |
||
186 | self.title = self.soup.html.head.title.contents |
||
187 | if self.title[0] == 'Mantis Issue Tracker for Scribus': |
||
188 | irc.reply("Sorry, it appears that this bug doesn't exist") |
||
189 | elif self.title == None: |
||
190 | irc.reply("Sorry, this bug is not accessible") |
||
191 | else: |
||
192 | self.shorttitle = self.title[0].strip().split(" - Mantis Issue Tracker")[0] |
||
193 | irc.reply(self.shorttitle) |
||
194 | except: |
||
195 | irc.reply("Sorry, an unknown error has occured. I'll go cry for a while") |
||
196 | else: |
||
197 | self.log.debug("We are not in a channel") |
||
198 | |||
199 | Class = Mantis |
||
200 | |||
201 | # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: |