blob: a5e023430a3c11ae566d57c719a4c719e1ed1343 [file] [log] [blame]
Guido van Rossumc629d341992-11-05 10:43:02 +00001# An NNTP client class. Based on RFC 977: Network News Transfer
2# Protocol, by Brian Kantor and Phil Lapsley.
3
4
5# Example:
6#
Guido van Rossum18fc5691992-11-26 09:17:19 +00007# >>> from nntplib import NNTP
Guido van Rossum8421c4e1995-09-22 00:52:38 +00008# >>> s = NNTP('news')
9# >>> resp, count, first, last, name = s.group('comp.lang.python')
Guido van Rossumc629d341992-11-05 10:43:02 +000010# >>> print 'Group', name, 'has', count, 'articles, range', first, 'to', last
Guido van Rossum8421c4e1995-09-22 00:52:38 +000011# Group comp.lang.python has 51 articles, range 5770 to 5821
Guido van Rossumc629d341992-11-05 10:43:02 +000012# >>> resp, subs = s.xhdr('subject', first + '-' + last)
13# >>> resp = s.quit()
14# >>>
15#
16# Here 'resp' is the server response line.
17# Error responses are turned into exceptions.
18#
19# To post an article from a file:
20# >>> f = open(filename, 'r') # file containing article, including header
21# >>> resp = s.post(f)
22# >>>
23#
24# For descriptions of all methods, read the comments in the code below.
25# Note that all arguments and return values representing article numbers
26# are strings, not numbers, since they are rarely used for calculations.
27
Guido van Rossum8421c4e1995-09-22 00:52:38 +000028# (xover, xgtitle, xpath, date methods by Kevan Heydon)
29
Guido van Rossumc629d341992-11-05 10:43:02 +000030
31# Imports
32import regex
33import socket
34import string
35
36
Guido van Rossum18fc5691992-11-26 09:17:19 +000037# Exception raised when an error or invalid response is received
Guido van Rossumc629d341992-11-05 10:43:02 +000038
Guido van Rossum18fc5691992-11-26 09:17:19 +000039error_reply = 'nntplib.error_reply' # unexpected [123]xx reply
40error_temp = 'nntplib.error_temp' # 4xx errors
41error_perm = 'nntplib.error_perm' # 5xx errors
42error_proto = 'nntplib.error_proto' # response does not begin with [1-5]
Guido van Rossum8421c4e1995-09-22 00:52:38 +000043error_data = 'nntplib.error_data' # error in response data
Guido van Rossumc629d341992-11-05 10:43:02 +000044
45
46# Standard port used by NNTP servers
47NNTP_PORT = 119
48
49
50# Response numbers that are followed by additional text (e.g. article)
Guido van Rossum8421c4e1995-09-22 00:52:38 +000051LONGRESP = ['100', '215', '220', '221', '222', '224', '230', '231', '282']
Guido van Rossumc629d341992-11-05 10:43:02 +000052
53
54# Line terminators (we always output CRLF, but accept any of CRLF, CR, LF)
55CRLF = '\r\n'
56
57
58# The class itself
59
60class NNTP:
61
62 # Initialize an instance. Arguments:
63 # - host: hostname to connect to
64 # - port: port to connect to (default the standard NNTP port)
65
Guido van Rossumb6775db1994-08-01 11:34:53 +000066 def __init__(self, host, port = NNTP_PORT):
Guido van Rossumc629d341992-11-05 10:43:02 +000067 self.host = host
68 self.port = port
69 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
70 self.sock.connect(self.host, self.port)
Jack Jansen2bb57b81996-02-14 16:06:24 +000071 self.file = self.sock.makefile('rb')
Guido van Rossumc629d341992-11-05 10:43:02 +000072 self.debugging = 0
73 self.welcome = self.getresp()
Guido van Rossumc629d341992-11-05 10:43:02 +000074
75 # Get the welcome message from the server
Guido van Rossum7bc817d1993-12-17 15:25:27 +000076 # (this is read and squirreled away by __init__()).
Guido van Rossumc629d341992-11-05 10:43:02 +000077 # If the response code is 200, posting is allowed;
78 # if it 201, posting is not allowed
79
80 def getwelcome(self):
81 if self.debugging: print '*welcome*', `self.welcome`
82 return self.welcome
83
84 # Set the debugging level. Argument level means:
85 # 0: no debugging output (default)
86 # 1: print commands and responses but not body text etc.
87 # 2: also print raw lines read and sent before stripping CR/LF
88
Guido van Rossumcf5394f1995-03-30 10:42:34 +000089 def set_debuglevel(self, level):
Guido van Rossumc629d341992-11-05 10:43:02 +000090 self.debugging = level
Guido van Rossumcf5394f1995-03-30 10:42:34 +000091 debug = set_debuglevel
Guido van Rossumc629d341992-11-05 10:43:02 +000092
93 # Internal: send one line to the server, appending CRLF
94 def putline(self, line):
95 line = line + CRLF
96 if self.debugging > 1: print '*put*', `line`
97 self.sock.send(line)
98
99 # Internal: send one command to the server (through putline())
100 def putcmd(self, line):
101 if self.debugging: print '*cmd*', `line`
102 self.putline(line)
103
104 # Internal: return one line from the server, stripping CRLF.
105 # Raise EOFError if the connection is closed
106 def getline(self):
107 line = self.file.readline()
108 if self.debugging > 1:
109 print '*get*', `line`
110 if not line: raise EOFError
111 if line[-2:] == CRLF: line = line[:-2]
112 elif line[-1:] in CRLF: line = line[:-1]
113 return line
114
115 # Internal: get a response from the server.
116 # Raise various errors if the response indicates an error
117 def getresp(self):
118 resp = self.getline()
119 if self.debugging: print '*resp*', `resp`
120 c = resp[:1]
121 if c == '4':
Guido van Rossum18fc5691992-11-26 09:17:19 +0000122 raise error_temp, resp
Guido van Rossumc629d341992-11-05 10:43:02 +0000123 if c == '5':
Guido van Rossum18fc5691992-11-26 09:17:19 +0000124 raise error_perm, resp
Guido van Rossumc629d341992-11-05 10:43:02 +0000125 if c not in '123':
Guido van Rossum18fc5691992-11-26 09:17:19 +0000126 raise error_proto, resp
Guido van Rossumc629d341992-11-05 10:43:02 +0000127 return resp
128
129 # Internal: get a response plus following text from the server.
130 # Raise various errors if the response indicates an error
131 def getlongresp(self):
132 resp = self.getresp()
133 if resp[:3] not in LONGRESP:
134 raise error_reply, resp
135 list = []
136 while 1:
137 line = self.getline()
138 if line == '.':
139 break
Guido van Rossume2ed9df1997-08-26 23:26:18 +0000140 if line[:2] == '..':
141 line = line[1:]
Guido van Rossumc629d341992-11-05 10:43:02 +0000142 list.append(line)
143 return resp, list
144
145 # Internal: send a command and get the response
146 def shortcmd(self, line):
147 self.putcmd(line)
148 return self.getresp()
149
150 # Internal: send a command and get the response plus following text
151 def longcmd(self, line):
152 self.putcmd(line)
153 return self.getlongresp()
154
155 # Process a NEWGROUPS command. Arguments:
156 # - date: string 'yymmdd' indicating the date
157 # - time: string 'hhmmss' indicating the time
158 # Return:
159 # - resp: server response if succesful
160 # - list: list of newsgroup names
161
162 def newgroups(self, date, time):
163 return self.longcmd('NEWGROUPS ' + date + ' ' + time)
164
165 # Process a NEWNEWS command. Arguments:
166 # - group: group name or '*'
167 # - date: string 'yymmdd' indicating the date
168 # - time: string 'hhmmss' indicating the time
169 # Return:
170 # - resp: server response if succesful
171 # - list: list of article ids
172
173 def newnews(self, group, date, time):
174 cmd = 'NEWNEWS ' + group + ' ' + date + ' ' + time
175 return self.longcmd(cmd)
176
177 # Process a LIST command. Return:
178 # - resp: server response if succesful
Guido van Rossumbe9f2121995-01-10 10:35:55 +0000179 # - list: list of (group, last, first, flag) (strings)
Guido van Rossumc629d341992-11-05 10:43:02 +0000180
181 def list(self):
182 resp, list = self.longcmd('LIST')
183 for i in range(len(list)):
Guido van Rossumbe9f2121995-01-10 10:35:55 +0000184 # Parse lines into "group last first flag"
Guido van Rossumc69955341997-03-14 04:18:20 +0000185 list[i] = tuple(string.split(list[i]))
Guido van Rossumc629d341992-11-05 10:43:02 +0000186 return resp, list
187
188 # Process a GROUP command. Argument:
189 # - group: the group name
190 # Returns:
191 # - resp: server response if succesful
192 # - count: number of articles (string)
193 # - first: first article number (string)
194 # - last: last article number (string)
195 # - name: the group name
196
197 def group(self, name):
198 resp = self.shortcmd('GROUP ' + name)
199 if resp[:3] <> '211':
200 raise error_reply, resp
201 words = string.split(resp)
202 count = first = last = 0
203 n = len(words)
204 if n > 1:
205 count = words[1]
206 if n > 2:
207 first = words[2]
208 if n > 3:
209 last = words[3]
210 if n > 4:
211 name = string.lower(words[4])
212 return resp, count, first, last, name
213
214 # Process a HELP command. Returns:
215 # - resp: server response if succesful
216 # - list: list of strings
217
218 def help(self):
219 return self.longcmd('HELP')
220
221 # Internal: parse the response of a STAT, NEXT or LAST command
222 def statparse(self, resp):
223 if resp[:2] <> '22':
224 raise error_reply, resp
225 words = string.split(resp)
226 nr = 0
227 id = ''
228 n = len(words)
229 if n > 1:
230 nr = words[1]
231 if n > 2:
232 id = string.lower(words[2])
233 return resp, nr, id
234
235 # Internal: process a STAT, NEXT or LAST command
236 def statcmd(self, line):
237 resp = self.shortcmd(line)
238 return self.statparse(resp)
239
240 # Process a STAT command. Argument:
241 # - id: article number or message id
242 # Returns:
243 # - resp: server response if succesful
244 # - nr: the article number
245 # - id: the article id
246
247 def stat(self, id):
248 return self.statcmd('STAT ' + id)
249
250 # Process a NEXT command. No arguments. Return as for STAT
251
252 def next(self):
253 return self.statcmd('NEXT')
254
255 # Process a LAST command. No arguments. Return as for STAT
256
257 def last(self):
258 return self.statcmd('LAST')
259
260 # Internal: process a HEAD, BODY or ARTICLE command
261 def artcmd(self, line):
262 resp, list = self.longcmd(line)
263 resp, nr, id = self.statparse(resp)
264 return resp, nr, id, list
265
266 # Process a HEAD command. Argument:
267 # - id: article number or message id
268 # Returns:
269 # - resp: server response if succesful
270 # - list: the lines of the article's header
271
272 def head(self, id):
273 return self.artcmd('HEAD ' + id)
274
275 # Process a BODY command. Argument:
276 # - id: article number or message id
277 # Returns:
278 # - resp: server response if succesful
279 # - list: the lines of the article's body
280
281 def body(self, id):
282 return self.artcmd('BODY ' + id)
283
284 # Process an ARTICLE command. Argument:
285 # - id: article number or message id
286 # Returns:
287 # - resp: server response if succesful
288 # - list: the lines of the article
289
290 def article(self, id):
291 return self.artcmd('ARTICLE ' + id)
292
293 # Process a SLAVE command. Returns:
294 # - resp: server response if succesful
295
296 def slave(self):
297 return self.shortcmd('SLAVE')
298
299 # Process an XHDR command (optional server extension). Arguments:
300 # - hdr: the header type (e.g. 'subject')
301 # - str: an article nr, a message id, or a range nr1-nr2
302 # Returns:
303 # - resp: server response if succesful
304 # - list: list of (nr, value) strings
305
306 def xhdr(self, hdr, str):
307 resp, lines = self.longcmd('XHDR ' + hdr + ' ' + str)
308 for i in range(len(lines)):
309 line = lines[i]
310 n = regex.match('^[0-9]+', line)
311 nr = line[:n]
312 if n < len(line) and line[n] == ' ': n = n+1
313 lines[i] = (nr, line[n:])
314 return resp, lines
315
Guido van Rossum8421c4e1995-09-22 00:52:38 +0000316 # Process an XOVER command (optional server extension) Arguments:
317 # - start: start of range
318 # - end: end of range
319 # Returns:
320 # - resp: server response if succesful
321 # - list: list of (art-nr, subject, poster, date, id, refrences, size, lines)
322
323 def xover(self,start,end):
324 resp, lines = self.longcmd('XOVER ' + start + '-' + end)
325 xover_lines = []
326 for line in lines:
327 elem = string.splitfields(line,"\t")
328 try:
Guido van Rossumc3fb88b1997-07-17 15:21:52 +0000329 xover_lines.append((elem[0],
330 elem[1],
331 elem[2],
332 elem[3],
333 elem[4],
334 string.split(elem[5]),
335 elem[6],
336 elem[7]))
Guido van Rossum8421c4e1995-09-22 00:52:38 +0000337 except IndexError:
338 raise error_data,line
339 return resp,xover_lines
340
341 # Process an XGTITLE command (optional server extension) Arguments:
342 # - group: group name wildcard (i.e. news.*)
343 # Returns:
344 # - resp: server response if succesful
345 # - list: list of (name,title) strings
346
347 def xgtitle(self, group):
348 line_pat = regex.compile("^\([^ \t]+\)[ \t]+\(.*\)$")
349 resp, raw_lines = self.longcmd('XGTITLE ' + group)
350 lines = []
351 for raw_line in raw_lines:
352 if line_pat.search(string.strip(raw_line)) == 0:
353 lines.append(line_pat.group(1),
354 line_pat.group(2))
355
356 return resp, lines
357
358 # Process an XPATH command (optional server extension) Arguments:
359 # - id: Message id of article
360 # Returns:
361 # resp: server response if succesful
362 # path: directory path to article
363
364 def xpath(self,id):
365 resp = self.shortcmd("XPATH " + id)
366 if resp[:3] <> '223':
367 raise error_reply, resp
368 try:
369 [resp_num, path] = string.split(resp)
370 except ValueError:
371 raise error_reply, resp
372 else:
373 return resp, path
374
375 # Process the DATE command. Arguments:
376 # None
377 # Returns:
378 # resp: server response if succesful
379 # date: Date suitable for newnews/newgroups commands etc.
380 # time: Time suitable for newnews/newgroups commands etc.
381
382 def date (self):
383 resp = self.shortcmd("DATE")
384 if resp[:3] <> '111':
385 raise error_reply, resp
386 elem = string.split(resp)
387 if len(elem) != 2:
388 raise error_data, resp
389 date = elem[1][2:8]
390 time = elem[1][-6:]
391 if len(date) != 6 or len(time) != 6:
392 raise error_data, resp
393 return resp, date, time
394
395
Guido van Rossumc629d341992-11-05 10:43:02 +0000396 # Process a POST command. Arguments:
397 # - f: file containing the article
398 # Returns:
399 # - resp: server response if succesful
400
401 def post(self, f):
402 resp = self.shortcmd('POST')
403 # Raises error_??? if posting is not allowed
404 if resp[0] <> '3':
405 raise error_reply, resp
406 while 1:
407 line = f.readline()
408 if not line:
409 break
410 if line[-1] == '\n':
411 line = line[:-1]
Guido van Rossume2ed9df1997-08-26 23:26:18 +0000412 if line[:1] == '.':
413 line = '.' + line
Guido van Rossumc629d341992-11-05 10:43:02 +0000414 self.putline(line)
415 self.putline('.')
416 return self.getresp()
417
418 # Process an IHAVE command. Arguments:
419 # - id: message-id of the article
420 # - f: file containing the article
421 # Returns:
422 # - resp: server response if succesful
423 # Note that if the server refuses the article an exception is raised
424
425 def ihave(self, id, f):
426 resp = self.shortcmd('IHAVE ' + id)
Guido van Rossum18fc5691992-11-26 09:17:19 +0000427 # Raises error_??? if the server already has it
Guido van Rossumc629d341992-11-05 10:43:02 +0000428 if resp[0] <> '3':
429 raise error_reply, resp
430 while 1:
431 line = f.readline()
432 if not line:
433 break
434 if line[-1] == '\n':
435 line = line[:-1]
Guido van Rossume2ed9df1997-08-26 23:26:18 +0000436 if line[:1] == '.':
437 line = '.' + line
Guido van Rossumc629d341992-11-05 10:43:02 +0000438 self.putline(line)
439 self.putline('.')
440 return self.getresp()
441
442 # Process a QUIT command and close the socket. Returns:
443 # - resp: server response if succesful
444
445 def quit(self):
446 resp = self.shortcmd('QUIT')
447 self.file.close()
448 self.sock.close()
449 del self.file, self.sock
450 return resp
Guido van Rossume2ed9df1997-08-26 23:26:18 +0000451
452
453# Minimal test function
454def _test():
455 s = NNTP('news')
456 resp, count, first, last, name = s.group('comp.lang.python')
457 print resp
458 print 'Group', name, 'has', count, 'articles, range', first, 'to', last
459 resp, subs = s.xhdr('subject', first + '-' + last)
460 print resp
461 for item in subs:
462 print "%7s %s" % item
463 resp = s.quit()
464 print resp
465
466
467# Run the test when run as a script
468if __name__ == '__main__':
469 _test()