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 Error, e: |
||
80 | irc.reply("Error in 'bugtitle' call.") |
||
81 | irc.reply(str(e)) |
||
82 | |||
83 | bugtitle = wrap(bugtitle, ['int']) |
||
84 | |||
85 | def bugstatus(self, irc, msg, args, number): |
||
86 | """<number> |
||
87 | |||
88 | Returns the status of the bug number <number>. Only positive integers |
||
89 | are accepted as bug numbers. |
||
90 | """ |
||
91 | self.bugno = number |
||
92 | self.url = self.mantisutils._makeurl(self.server, self.bugno) |
||
93 | try: |
||
94 | self.soup = self.mantisutils._bugdata(self.url) |
||
95 | self.title = self.soup.html.head.title.contents |
||
96 | if self.title[0] == 'Mantis Issue Tracker for Scribus': |
||
97 | irc.reply("Sorry, it appears that this bug doesn't exist") |
||
98 | elif self.title == None: |
||
99 | irc.reply("Sorry, this bug is not accessible") |
||
100 | else: |
||
101 | self.status = self.soup.firstText(re.compile('--\ Status')).findNext('td').findNext('td').contents[0].strip() |
||
102 | irc.reply(self.status) |
||
103 | except Error, e: |
||
104 | irc.reply("Error in 'bugstatus' call.") |
||
105 | irc.reply(str(e)) |
||
106 | |||
107 | bugstatus = wrap(bugstatus, ['int']) |
||
108 | |||
109 | def bugreporter(self, irc, msg, args, number): |
||
110 | """<number> |
||
111 | |||
112 | Returns the reporter of the bug number <number>. Only positive integers |
||
113 | are accepted as bug numbers. |
||
114 | """ |
||
115 | self.bugno = number |
||
116 | self.url = self.mantisutils._makeurl(self.server, self.bugno) |
||
117 | try: |
||
118 | self.soup = self.mantisutils._bugdata(self.url) |
||
119 | self.title = self.soup.html.head.title.contents |
||
120 | if self.title[0] == 'Mantis Issue Tracker for Scribus': |
||
121 | irc.reply("Sorry, it appears that this bug doesn't exist") |
||
122 | elif self.title == None: |
||
123 | irc.reply("Sorry, this bug is not accessible") |
||
124 | else: |
||
125 | self.reporter = self.soup.firstText(re.compile('Reporter')).findNext('td').findNext('td').contents[0].strip() |
||
126 | irc.reply(self.reporter) |
||
127 | except Error, e: |
||
128 | irc.reply("Error in 'bugreporter' call.") |
||
129 | irc.reply(str(e)) |
||
130 | |||
131 | bugreporter = wrap(bugreporter, ['int']) |
||
132 | |||
133 | def bugassign(self, irc, msg, args, number): |
||
134 | """<number> |
||
135 | |||
136 | Returns login name of the developer the bug number <number> is assigned |
||
137 | to. Only positive integers are accepted as bug numbers. |
||
138 | """ |
||
139 | self.bugno = number |
||
140 | self.url = self.mantisutils._makeurl(self.server, self.bugno) |
||
141 | try: |
||
142 | self.soup = self.mantisutils._bugdata(self.url) |
||
143 | self.title = self.soup.html.head.title.contents |
||
144 | if self.title[0] == 'Mantis Issue Tracker for Scribus': |
||
145 | irc.reply("Sorry, it appears that this bug doesn't exist") |
||
146 | elif self.title == None: |
||
147 | irc.reply("Sorry, this bug is not accessible") |
||
148 | else: |
||
149 | self.assignee = self.soup.firstText(re.compile('Assigned\ To')).findNext('td').contents[0].strip() |
||
150 | irc.reply(self.assignee) |
||
151 | except Error, e: |
||
152 | irc.reply("Error in 'bugassign' call.") |
||
153 | irc.reply(str(e)) |
||
154 | |||
155 | bugassign = wrap(bugassign, ['int']) |
||
156 | |||
157 | def bugsummary(self, irc, msg, args, number): |
||
158 | """<number> |
||
159 | |||
160 | Returns Summary of the bug number <number>. Only positive integers |
||
161 | are accepted as bug numbers. |
||
162 | """ |
||
163 | self.bugno = number |
||
164 | self.url = self.mantisutils._makeurl(self.server, self.bugno) |
||
165 | try: |
||
166 | self.soup = self.mantisutils._bugdata(self.url) |
||
167 | self.title = self.soup.html.head.title.contents |
||
168 | if self.title[0] == 'Mantis Issue Tracker for Scribus': |
||
169 | irc.reply("Sorry, it appears that this bug doesn't exist") |
||
170 | elif self.title == None: |
||
171 | irc.reply("Sorry, this bug is not accessible") |
||
172 | else: |
||
173 | self.s = self.soup.find(text=re.compile("Summary")) |
||
174 | self.indata = self.s.next.next.td.nextSibling.nextSibling |
||
175 | self.outdata = self.indata.contents[0].encode('utf8').split(":")[1].strip() |
||
176 | irc.reply(self.outdata) |
||
177 | except Error, e: |
||
178 | irc.reply("Error in 'bugsummary' call.") |
||
179 | irc.reply(str(e)) |
||
180 | |||
181 | bugsummary = wrap(bugsummary, ['int']) |
||
182 | |||
183 | def bugdesc(self, irc, msg, args, number): |
||
184 | """<number> |
||
185 | |||
186 | Returns Description of the bug number <number>. Only positive integers |
||
187 | are accepted as bug numbers. |
||
188 | """ |
||
189 | self.bugno = number |
||
190 | self.url = self.mantisutils._makeurl(self.server, self.bugno) |
||
191 | try: |
||
192 | self.soup = self.mantisutils._bugdata(self.url) |
||
193 | self.title = self.soup.html.head.title.contents |
||
194 | if self.title[0] == 'Mantis Issue Tracker for Scribus': |
||
195 | irc.reply("Sorry, it appears that this bug doesn't exist") |
||
196 | elif self.title == None: |
||
197 | irc.reply("Sorry, this bug is not accessible") |
||
198 | else: |
||
199 | self.s = self.soup.find(text=re.compile("Description")) |
||
200 | self.indata = self.s.next.next.td.nextSibling.nextSibling |
||
201 | self.outdata = self.indata.contents[0].encode('utf8').strip() |
||
202 | irc.reply(self.outdata) |
||
203 | except Error, e: |
||
204 | irc.reply(str(e)) |
||
205 | |||
206 | bugdesc = wrap(bugdesc, ['int']) |
||
207 | |||
208 | |||
209 | def bugurl(self, irc, msg, args, number): |
||
210 | """Returns the URL for the bug number passed into the channel.""" |
||
211 | self.bugno = number |
||
212 | self.url = self.mantisutils._makeurl(self.server, self.bugno) |
||
213 | try: |
||
214 | self.soup = self.mantisutils._bugdata(self.url) |
||
215 | self.title = self.soup.html.head.title.contents |
||
216 | if self.title[0] == 'Mantis Issue Tracker for Scribus': |
||
217 | irc.reply("Sorry, it appears that this bug doesn't exist") |
||
218 | elif self.title == None: |
||
219 | irc.reply("Sorry, this bug is not accessible") |
||
220 | else: |
||
221 | irc.reply(self.url) |
||
222 | except Error, e: |
||
223 | irc.reply(str(e)) |
||
224 | |||
225 | bugurl = wrap(bugurl, ['int']) |
||
226 | |||
227 | def checkbts(self): |
||
228 | """ Example: http://bugs.scribus.net/main_page.php""" |
||
229 | self.defaultsrv='bugs.scribus.net' |
||
230 | self.confsrv = conf.supybot.plugins.Mantis.server() |
||
231 | if not self.confsrv: |
||
232 | self.server = self.defaultsrv |
||
233 | else: |
||
234 | self.server = self.confsrv |
||
235 | self.btsurl = 'http://'+self.server+'/main_page.php' |
||
236 | #self.btsurl = 'http://'+self.defaultsrv+'/main_page.php' |
||
237 | try: |
||
238 | self.mainpage = urllib2.urlopen(self.btsurl) |
||
239 | self.log.debug('Checking the bts at %u', self.btsurl) |
||
240 | return True |
||
241 | except URLError: |
||
242 | print "Cannot open the BTS main page. Unloading plugin." |
||
243 | |||
244 | def bugUrl(self, irc, msg, m): |
||
245 | r"^#\d{4,9}" |
||
246 | """Returns the URL for the bug number passed into the channel.""" |
||
247 | self.msg = msg |
||
248 | self.channel = self.msg.args[0] |
||
249 | if irc.isChannel(self.channel): |
||
250 | if ircmsgs.isAction(self.msg): |
||
251 | self.text = ircmsgs.unAction(self.msg) |
||
252 | else: |
||
253 | self.text = self.msg.args[1] |
||
254 | self.buglist = re.findall(r"^#\d{4,9}", self.text) |
||
255 | for self.bugid in self.buglist: |
||
256 | self.bugno = self.bugid.rstrip()[1:] |
||
257 | self.url = self.mantisutils._makeurl(self.server,self.bugno) |
||
258 | try: |
||
259 | self.soup = self.mantisutils._bugdata(self.url) |
||
260 | self.title = self.soup.html.head.title.contents |
||
261 | if self.title[0] == 'Mantis Issue Tracker for Scribus': |
||
262 | irc.reply("Sorry, it appears that this bug doesn't exist") |
||
263 | elif self.title == None: |
||
264 | irc.reply("Sorry, this bug is not accessible") |
||
265 | else: |
||
266 | self.shorttitle = self.title[0].strip().split(" - Mantis Issue Tracker")[0] |
||
267 | irc.reply(self.shorttitle) |
||
268 | except: |
||
269 | irc.reply("Sorry, an unknown error has occured. I'll go cry for a while") |
||
270 | else: |
||
271 | self.log.debug("We are not in a channel") |
||
272 | |||
273 | Class = Mantis |
||
274 | |||
275 | # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: |