| 
									
										
										
										
											2000-02-04 15:10:34 +00:00
										 |  |  | """An NNTP client class based on RFC 977: Network News Transfer Protocol.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Example: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | >>> from nntplib import NNTP | 
					
						
							|  |  |  | >>> s = NNTP('news') | 
					
						
							|  |  |  | >>> resp, count, first, last, name = s.group('comp.lang.python') | 
					
						
							| 
									
										
										
										
											2007-02-09 20:13:25 +00:00
										 |  |  | >>> print('Group', name, 'has', count, 'articles, range', first, 'to', last) | 
					
						
							| 
									
										
										
										
											2000-02-04 15:10:34 +00:00
										 |  |  | Group comp.lang.python has 51 articles, range 5770 to 5821 | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  | >>> resp, subs = s.xhdr('subject', '{0}-{1}'.format(first, last)) | 
					
						
							| 
									
										
										
										
											2000-02-04 15:10:34 +00:00
										 |  |  | >>> resp = s.quit() | 
					
						
							|  |  |  | >>> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Here 'resp' is the server response line. | 
					
						
							|  |  |  | Error responses are turned into exceptions. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | To post an article from a file: | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  | >>> f = open(filename, 'rb') # file containing article, including header | 
					
						
							| 
									
										
										
										
											2000-02-04 15:10:34 +00:00
										 |  |  | >>> resp = s.post(f) | 
					
						
							|  |  |  | >>> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | For descriptions of all methods, read the comments in the code below. | 
					
						
							|  |  |  | Note that all arguments and return values representing article numbers | 
					
						
							|  |  |  | are strings, not numbers, since they are rarely used for calculations. | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # RFC 977 by Brian Kantor and Phil Lapsley. | 
					
						
							|  |  |  | # xover, xgtitle, xpath, date methods by Kevan Heydon | 
					
						
							| 
									
										
										
										
											1995-09-22 00:52:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-11-05 10:43:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Imports | 
					
						
							| 
									
										
										
										
											1997-10-22 21:00:49 +00:00
										 |  |  | import re | 
					
						
							| 
									
										
										
										
											1992-11-05 10:43:02 +00:00
										 |  |  | import socket | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-06 01:07:02 +00:00
										 |  |  | __all__ = ["NNTP","NNTPReplyError","NNTPTemporaryError", | 
					
						
							|  |  |  |            "NNTPPermanentError","NNTPProtocolError","NNTPDataError", | 
					
						
							|  |  |  |            "error_reply","error_temp","error_perm","error_proto", | 
					
						
							|  |  |  |            "error_data",] | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-02-10 20:25:53 +00:00
										 |  |  | # Exceptions raised when an error or invalid response is received | 
					
						
							|  |  |  | class NNTPError(Exception): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |     """Base class for all nntplib exceptions""" | 
					
						
							|  |  |  |     def __init__(self, *args): | 
					
						
							| 
									
										
										
										
											2003-02-27 20:14:51 +00:00
										 |  |  |         Exception.__init__(self, *args) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         try: | 
					
						
							|  |  |  |             self.response = args[0] | 
					
						
							|  |  |  |         except IndexError: | 
					
						
							|  |  |  |             self.response = 'No response given' | 
					
						
							| 
									
										
										
										
											2000-02-10 20:25:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class NNTPReplyError(NNTPError): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |     """Unexpected [123]xx reply""" | 
					
						
							|  |  |  |     pass | 
					
						
							| 
									
										
										
										
											2000-02-10 20:25:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class NNTPTemporaryError(NNTPError): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |     """4xx errors""" | 
					
						
							|  |  |  |     pass | 
					
						
							| 
									
										
										
										
											2000-02-10 20:25:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class NNTPPermanentError(NNTPError): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |     """5xx errors""" | 
					
						
							|  |  |  |     pass | 
					
						
							| 
									
										
										
										
											2000-02-10 20:25:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class NNTPProtocolError(NNTPError): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |     """Response does not begin with [1-5]""" | 
					
						
							|  |  |  |     pass | 
					
						
							| 
									
										
										
										
											2000-02-10 20:25:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class NNTPDataError(NNTPError): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |     """Error in response data""" | 
					
						
							|  |  |  |     pass | 
					
						
							| 
									
										
										
										
											1992-11-05 10:43:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-02-10 20:25:53 +00:00
										 |  |  | # for backwards compatibility | 
					
						
							|  |  |  | error_reply = NNTPReplyError | 
					
						
							|  |  |  | error_temp = NNTPTemporaryError | 
					
						
							|  |  |  | error_perm = NNTPPermanentError | 
					
						
							|  |  |  | error_proto = NNTPProtocolError | 
					
						
							|  |  |  | error_data = NNTPDataError | 
					
						
							| 
									
										
										
										
											1992-11-05 10:43:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-02-10 20:25:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-11-05 10:43:02 +00:00
										 |  |  | # Standard port used by NNTP servers | 
					
						
							|  |  |  | NNTP_PORT = 119 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Response numbers that are followed by additional text (e.g. article) | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  | LONGRESP = [b'100', b'215', b'220', b'221', b'222', b'224', b'230', b'231', b'282'] | 
					
						
							| 
									
										
										
										
											1992-11-05 10:43:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Line terminators (we always output CRLF, but accept any of CRLF, CR, LF) | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  | CRLF = b'\r\n' | 
					
						
							| 
									
										
										
										
											1992-11-05 10:43:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-11-05 10:43:02 +00:00
										 |  |  | # The class itself | 
					
						
							|  |  |  | class NNTP: | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |     def __init__(self, host, port=NNTP_PORT, user=None, password=None, | 
					
						
							| 
									
										
										
										
											2004-08-03 14:36:32 +00:00
										 |  |  |                  readermode=None, usenetrc=True): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         """Initialize an instance.  Arguments:
 | 
					
						
							|  |  |  |         - host: hostname to connect to | 
					
						
							|  |  |  |         - port: port to connect to (default the standard NNTP port) | 
					
						
							|  |  |  |         - user: username to authenticate with | 
					
						
							|  |  |  |         - password: password to use with username | 
					
						
							|  |  |  |         - readermode: if true, send 'mode reader' command after | 
					
						
							|  |  |  |                       connecting. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         readermode is sometimes necessary if you are connecting to an | 
					
						
							|  |  |  |         NNTP server on the local machine and intend to call | 
					
						
							|  |  |  |         reader-specific comamnds, such as `group'.  If you get | 
					
						
							|  |  |  |         unexpected NNTPPermanentErrors, you might need to set | 
					
						
							|  |  |  |         readermode. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         self.host = host | 
					
						
							|  |  |  |         self.port = port | 
					
						
							| 
									
										
										
										
											2009-05-14 21:30:46 +00:00
										 |  |  |         self.sock = socket.create_connection((host, port)) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         self.file = self.sock.makefile('rb') | 
					
						
							|  |  |  |         self.debugging = 0 | 
					
						
							|  |  |  |         self.welcome = self.getresp() | 
					
						
							| 
									
										
										
										
											2001-01-16 07:12:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-16 06:35:14 +00:00
										 |  |  |         # 'mode reader' is sometimes necessary to enable 'reader' mode. | 
					
						
							| 
									
										
										
										
											2001-01-16 07:12:46 +00:00
										 |  |  |         # However, the order in which 'mode reader' and 'authinfo' need to | 
					
						
							| 
									
										
										
										
											2001-01-16 06:35:14 +00:00
										 |  |  |         # arrive differs between some NNTP servers. Try to send | 
					
						
							|  |  |  |         # 'mode reader', and if it fails with an authorization failed | 
					
						
							|  |  |  |         # error, try again after sending authinfo. | 
					
						
							|  |  |  |         readermode_afterauth = 0 | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         if readermode: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 self.welcome = self.shortcmd('mode reader') | 
					
						
							|  |  |  |             except NNTPPermanentError: | 
					
						
							|  |  |  |                 # error 500, probably 'not implemented' | 
					
						
							|  |  |  |                 pass | 
					
						
							| 
									
										
										
										
											2007-01-10 16:19:56 +00:00
										 |  |  |             except NNTPTemporaryError as e: | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |                 if user and e.response.startswith(b'480'): | 
					
						
							| 
									
										
										
										
											2001-01-16 06:35:14 +00:00
										 |  |  |                     # Need authorization before 'mode reader' | 
					
						
							|  |  |  |                     readermode_afterauth = 1 | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     raise | 
					
						
							| 
									
										
										
										
											2002-11-13 23:05:35 +00:00
										 |  |  |         # If no login/password was specified, try to get them from ~/.netrc | 
					
						
							|  |  |  |         # Presume that if .netc has an entry, NNRP authentication is required. | 
					
						
							| 
									
										
										
										
											2002-11-17 17:53:12 +00:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2004-08-03 14:36:32 +00:00
										 |  |  |             if usenetrc and not user: | 
					
						
							| 
									
										
										
										
											2002-11-17 17:53:12 +00:00
										 |  |  |                 import netrc | 
					
						
							|  |  |  |                 credentials = netrc.netrc() | 
					
						
							|  |  |  |                 auth = credentials.authenticators(host) | 
					
						
							|  |  |  |                 if auth: | 
					
						
							|  |  |  |                     user = auth[0] | 
					
						
							|  |  |  |                     password = auth[2] | 
					
						
							|  |  |  |         except IOError: | 
					
						
							|  |  |  |             pass | 
					
						
							| 
									
										
										
										
											2002-11-13 23:05:35 +00:00
										 |  |  |         # Perform NNRP authentication if needed. | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         if user: | 
					
						
							|  |  |  |             resp = self.shortcmd('authinfo user '+user) | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |             if resp.startswith(b'381'): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |                 if not password: | 
					
						
							|  |  |  |                     raise NNTPReplyError(resp) | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     resp = self.shortcmd( | 
					
						
							|  |  |  |                             'authinfo pass '+password) | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |                     if not resp.startswith(b'281'): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |                         raise NNTPPermanentError(resp) | 
					
						
							| 
									
										
										
										
											2001-01-16 06:35:14 +00:00
										 |  |  |             if readermode_afterauth: | 
					
						
							|  |  |  |                 try: | 
					
						
							|  |  |  |                     self.welcome = self.shortcmd('mode reader') | 
					
						
							|  |  |  |                 except NNTPPermanentError: | 
					
						
							|  |  |  |                     # error 500, probably 'not implemented' | 
					
						
							|  |  |  |                     pass | 
					
						
							| 
									
										
										
										
											2001-01-16 07:12:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Get the welcome message from the server | 
					
						
							|  |  |  |     # (this is read and squirreled away by __init__()). | 
					
						
							|  |  |  |     # If the response code is 200, posting is allowed; | 
					
						
							|  |  |  |     # if it 201, posting is not allowed | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def getwelcome(self): | 
					
						
							|  |  |  |         """Get the welcome message from the server
 | 
					
						
							|  |  |  |         (this is read and squirreled away by __init__()). | 
					
						
							|  |  |  |         If the response code is 200, posting is allowed; | 
					
						
							|  |  |  |         if it 201, posting is not allowed."""
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |         if self.debugging: print('*welcome*', repr(self.welcome)) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         return self.welcome | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def set_debuglevel(self, level): | 
					
						
							|  |  |  |         """Set the debugging level.  Argument 'level' means:
 | 
					
						
							|  |  |  |         0: no debugging output (default) | 
					
						
							|  |  |  |         1: print commands and responses but not body text etc. | 
					
						
							|  |  |  |         2: also print raw lines read and sent before stripping CR/LF"""
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.debugging = level | 
					
						
							|  |  |  |     debug = set_debuglevel | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def putline(self, line): | 
					
						
							|  |  |  |         """Internal: send one line to the server, appending CRLF.""" | 
					
						
							|  |  |  |         line = line + CRLF | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |         if self.debugging > 1: print('*put*', repr(line)) | 
					
						
							| 
									
										
										
										
											2002-02-16 23:06:19 +00:00
										 |  |  |         self.sock.sendall(line) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def putcmd(self, line): | 
					
						
							|  |  |  |         """Internal: send one command to the server (through putline()).""" | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |         if self.debugging: print('*cmd*', repr(line)) | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |         line = bytes(line, "ASCII") | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         self.putline(line) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def getline(self): | 
					
						
							|  |  |  |         """Internal: return one line from the server, stripping CRLF.
 | 
					
						
							|  |  |  |         Raise EOFError if the connection is closed."""
 | 
					
						
							|  |  |  |         line = self.file.readline() | 
					
						
							|  |  |  |         if self.debugging > 1: | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |             print('*get*', repr(line)) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         if not line: raise EOFError | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |         if line[-2:] == CRLF: | 
					
						
							|  |  |  |             line = line[:-2] | 
					
						
							|  |  |  |         elif line[-1:] in CRLF: | 
					
						
							|  |  |  |             line = line[:-1] | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         return line | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def getresp(self): | 
					
						
							|  |  |  |         """Internal: get a response from the server.
 | 
					
						
							|  |  |  |         Raise various errors if the response indicates an error."""
 | 
					
						
							|  |  |  |         resp = self.getline() | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |         if self.debugging: print('*resp*', repr(resp)) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         c = resp[:1] | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |         if c == b'4': | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |             raise NNTPTemporaryError(resp) | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |         if c == b'5': | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |             raise NNTPPermanentError(resp) | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |         if c not in b'123': | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |             raise NNTPProtocolError(resp) | 
					
						
							|  |  |  |         return resp | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-18 20:58:25 +00:00
										 |  |  |     def getlongresp(self, file=None): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         """Internal: get a response plus following text from the server.
 | 
					
						
							|  |  |  |         Raise various errors if the response indicates an error."""
 | 
					
						
							| 
									
										
										
										
											2001-10-01 13:46:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         openedFile = None | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             # If a string was passed then open a file with that name | 
					
						
							| 
									
										
										
											
												Remove uses of the string and types modules:
x in string.whitespace => x.isspace()
type(x) in types.StringTypes => isinstance(x, basestring)
isinstance(x, types.StringTypes) => isinstance(x, basestring)
type(x) is types.StringType => isinstance(x, str)
type(x) == types.StringType => isinstance(x, str)
string.split(x, ...) => x.split(...)
string.join(x, y) => y.join(x)
string.zfill(x, ...) => x.zfill(...)
string.count(x, ...) => x.count(...)
hasattr(types, "UnicodeType") => try: unicode except NameError:
type(x) != types.TupleTuple => not isinstance(x, tuple)
isinstance(x, types.TupleType) => isinstance(x, tuple)
type(x) is types.IntType => isinstance(x, int)
Do not mention the string module in the rlcompleter docstring.
This partially applies SF patch http://www.python.org/sf/562373
(with basestring instead of string). (It excludes the changes to
unittest.py and does not change the os.stat stuff.)
											
										 
											2002-06-03 15:58:32 +00:00
										 |  |  |             if isinstance(file, str): | 
					
						
							| 
									
										
										
										
											2001-10-18 20:58:25 +00:00
										 |  |  |                 openedFile = file = open(file, "w") | 
					
						
							| 
									
										
										
										
											2001-10-01 13:46:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             resp = self.getresp() | 
					
						
							|  |  |  |             if resp[:3] not in LONGRESP: | 
					
						
							|  |  |  |                 raise NNTPReplyError(resp) | 
					
						
							|  |  |  |             list = [] | 
					
						
							|  |  |  |             while 1: | 
					
						
							|  |  |  |                 line = self.getline() | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |                 if line == b'.': | 
					
						
							| 
									
										
										
										
											2001-10-01 13:46:55 +00:00
										 |  |  |                     break | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |                 if line.startswith(b'..'): | 
					
						
							| 
									
										
										
										
											2001-10-01 13:46:55 +00:00
										 |  |  |                     line = line[1:] | 
					
						
							| 
									
										
										
										
											2001-10-18 20:58:25 +00:00
										 |  |  |                 if file: | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |                     file.write(line + b'\n') | 
					
						
							| 
									
										
										
										
											2001-10-01 13:46:55 +00:00
										 |  |  |                 else: | 
					
						
							|  |  |  |                     list.append(line) | 
					
						
							|  |  |  |         finally: | 
					
						
							|  |  |  |             # If this method created the file, then it must close it | 
					
						
							|  |  |  |             if openedFile: | 
					
						
							|  |  |  |                 openedFile.close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         return resp, list | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def shortcmd(self, line): | 
					
						
							|  |  |  |         """Internal: send a command and get the response.""" | 
					
						
							|  |  |  |         self.putcmd(line) | 
					
						
							|  |  |  |         return self.getresp() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-18 20:58:25 +00:00
										 |  |  |     def longcmd(self, line, file=None): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         """Internal: send a command and get the response plus following text.""" | 
					
						
							|  |  |  |         self.putcmd(line) | 
					
						
							| 
									
										
										
										
											2001-10-18 20:58:25 +00:00
										 |  |  |         return self.getlongresp(file) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-19 18:04:57 +00:00
										 |  |  |     def newgroups(self, date, time, file=None): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         """Process a NEWGROUPS command.  Arguments:
 | 
					
						
							|  |  |  |         - date: string 'yymmdd' indicating the date | 
					
						
							|  |  |  |         - time: string 'hhmmss' indicating the time | 
					
						
							|  |  |  |         Return: | 
					
						
							|  |  |  |         - resp: server response if successful | 
					
						
							|  |  |  |         - list: list of newsgroup names"""
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-19 18:04:57 +00:00
										 |  |  |         return self.longcmd('NEWGROUPS ' + date + ' ' + time, file) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-19 18:04:57 +00:00
										 |  |  |     def newnews(self, group, date, time, file=None): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         """Process a NEWNEWS command.  Arguments:
 | 
					
						
							|  |  |  |         - group: group name or '*' | 
					
						
							|  |  |  |         - date: string 'yymmdd' indicating the date | 
					
						
							|  |  |  |         - time: string 'hhmmss' indicating the time | 
					
						
							|  |  |  |         Return: | 
					
						
							|  |  |  |         - resp: server response if successful | 
					
						
							| 
									
										
										
										
											2005-07-17 20:27:41 +00:00
										 |  |  |         - list: list of message ids"""
 | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         cmd = 'NEWNEWS ' + group + ' ' + date + ' ' + time | 
					
						
							| 
									
										
										
										
											2003-04-19 18:04:57 +00:00
										 |  |  |         return self.longcmd(cmd, file) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-19 18:04:57 +00:00
										 |  |  |     def list(self, file=None): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         """Process a LIST command.  Return:
 | 
					
						
							|  |  |  |         - resp: server response if successful | 
					
						
							|  |  |  |         - list: list of (group, last, first, flag) (strings)"""
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-19 18:04:57 +00:00
										 |  |  |         resp, list = self.longcmd('LIST', file) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         for i in range(len(list)): | 
					
						
							|  |  |  |             # Parse lines into "group last first flag" | 
					
						
							| 
									
										
										
										
											2001-02-09 07:02:17 +00:00
										 |  |  |             list[i] = tuple(list[i].split()) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         return resp, list | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-26 12:40:50 +00:00
										 |  |  |     def description(self, group): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         """Get a description for a single group.  If more than one
 | 
					
						
							|  |  |  |         group matches ('group' is a pattern), return the first.  If no | 
					
						
							|  |  |  |         group matches, return an empty string. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         This elides the response code from the server, since it can | 
					
						
							|  |  |  |         only be '215' or '285' (for xgtitle) anyway.  If the response | 
					
						
							|  |  |  |         code is needed, use the 'descriptions' method. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         NOTE: This neither checks for a wildcard in 'group' nor does | 
					
						
							|  |  |  |         it check whether the group actually exists."""
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         resp, lines = self.descriptions(group) | 
					
						
							|  |  |  |         if len(lines) == 0: | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |             return b'' | 
					
						
							| 
									
										
										
										
											2004-07-26 12:40:50 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             return lines[0][1] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def descriptions(self, group_pattern): | 
					
						
							|  |  |  |         """Get descriptions for a range of groups.""" | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |         line_pat = re.compile(b'^(?P<group>[^ \t]+)[ \t]+(.*)$') | 
					
						
							| 
									
										
										
										
											2004-07-26 12:40:50 +00:00
										 |  |  |         # Try the more std (acc. to RFC2980) LIST NEWSGROUPS first | 
					
						
							|  |  |  |         resp, raw_lines = self.longcmd('LIST NEWSGROUPS ' + group_pattern) | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |         if not resp.startswith(b'215'): | 
					
						
							| 
									
										
										
										
											2004-07-26 12:40:50 +00:00
										 |  |  |             # Now the deprecated XGTITLE.  This either raises an error | 
					
						
							|  |  |  |             # or succeeds with the same output structure as LIST | 
					
						
							|  |  |  |             # NEWSGROUPS. | 
					
						
							|  |  |  |             resp, raw_lines = self.longcmd('XGTITLE ' + group_pattern) | 
					
						
							|  |  |  |         lines = [] | 
					
						
							|  |  |  |         for raw_line in raw_lines: | 
					
						
							|  |  |  |             match = line_pat.search(raw_line.strip()) | 
					
						
							|  |  |  |             if match: | 
					
						
							|  |  |  |                 lines.append(match.group(1, 2)) | 
					
						
							|  |  |  |         return resp, lines | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |     def group(self, name): | 
					
						
							|  |  |  |         """Process a GROUP command.  Argument:
 | 
					
						
							|  |  |  |         - group: the group name | 
					
						
							|  |  |  |         Returns: | 
					
						
							|  |  |  |         - resp: server response if successful | 
					
						
							|  |  |  |         - count: number of articles (string) | 
					
						
							|  |  |  |         - first: first article number (string) | 
					
						
							|  |  |  |         - last: last article number (string) | 
					
						
							|  |  |  |         - name: the group name"""
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         resp = self.shortcmd('GROUP ' + name) | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |         if not resp.startswith(b'211'): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |             raise NNTPReplyError(resp) | 
					
						
							| 
									
										
										
										
											2001-02-09 07:02:17 +00:00
										 |  |  |         words = resp.split() | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         count = first = last = 0 | 
					
						
							|  |  |  |         n = len(words) | 
					
						
							|  |  |  |         if n > 1: | 
					
						
							|  |  |  |             count = words[1] | 
					
						
							|  |  |  |             if n > 2: | 
					
						
							|  |  |  |                 first = words[2] | 
					
						
							|  |  |  |                 if n > 3: | 
					
						
							|  |  |  |                     last = words[3] | 
					
						
							|  |  |  |                     if n > 4: | 
					
						
							| 
									
										
										
										
											2001-02-09 07:02:17 +00:00
										 |  |  |                         name = words[4].lower() | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         return resp, count, first, last, name | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-19 18:04:57 +00:00
										 |  |  |     def help(self, file=None): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         """Process a HELP command.  Returns:
 | 
					
						
							|  |  |  |         - resp: server response if successful | 
					
						
							|  |  |  |         - list: list of strings"""
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-19 18:04:57 +00:00
										 |  |  |         return self.longcmd('HELP',file) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def statparse(self, resp): | 
					
						
							|  |  |  |         """Internal: parse the response of a STAT, NEXT or LAST command.""" | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |         if not resp.startswith(b'22'): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |             raise NNTPReplyError(resp) | 
					
						
							| 
									
										
										
										
											2001-02-09 07:02:17 +00:00
										 |  |  |         words = resp.split() | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         nr = 0 | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |         id = b'' | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         n = len(words) | 
					
						
							|  |  |  |         if n > 1: | 
					
						
							|  |  |  |             nr = words[1] | 
					
						
							|  |  |  |             if n > 2: | 
					
						
							|  |  |  |                 id = words[2] | 
					
						
							|  |  |  |         return resp, nr, id | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def statcmd(self, line): | 
					
						
							|  |  |  |         """Internal: process a STAT, NEXT or LAST command.""" | 
					
						
							|  |  |  |         resp = self.shortcmd(line) | 
					
						
							|  |  |  |         return self.statparse(resp) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def stat(self, id): | 
					
						
							|  |  |  |         """Process a STAT command.  Argument:
 | 
					
						
							|  |  |  |         - id: article number or message id | 
					
						
							|  |  |  |         Returns: | 
					
						
							|  |  |  |         - resp: server response if successful | 
					
						
							|  |  |  |         - nr:   the article number | 
					
						
							| 
									
										
										
										
											2005-07-17 20:27:41 +00:00
										 |  |  |         - id:   the message id"""
 | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |         return self.statcmd('STAT {0}'.format(id)) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def next(self): | 
					
						
							|  |  |  |         """Process a NEXT command.  No arguments.  Return as for STAT.""" | 
					
						
							|  |  |  |         return self.statcmd('NEXT') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def last(self): | 
					
						
							|  |  |  |         """Process a LAST command.  No arguments.  Return as for STAT.""" | 
					
						
							|  |  |  |         return self.statcmd('LAST') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-18 20:58:25 +00:00
										 |  |  |     def artcmd(self, line, file=None): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         """Internal: process a HEAD, BODY or ARTICLE command.""" | 
					
						
							| 
									
										
										
										
											2001-10-18 20:58:25 +00:00
										 |  |  |         resp, list = self.longcmd(line, file) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         resp, nr, id = self.statparse(resp) | 
					
						
							|  |  |  |         return resp, nr, id, list | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def head(self, id): | 
					
						
							|  |  |  |         """Process a HEAD command.  Argument:
 | 
					
						
							|  |  |  |         - id: article number or message id | 
					
						
							|  |  |  |         Returns: | 
					
						
							|  |  |  |         - resp: server response if successful | 
					
						
							|  |  |  |         - nr: article number | 
					
						
							|  |  |  |         - id: message id | 
					
						
							|  |  |  |         - list: the lines of the article's header""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |         return self.artcmd('HEAD {0}'.format(id)) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-18 20:58:25 +00:00
										 |  |  |     def body(self, id, file=None): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         """Process a BODY command.  Argument:
 | 
					
						
							|  |  |  |         - id: article number or message id | 
					
						
							| 
									
										
										
										
											2001-10-18 20:58:25 +00:00
										 |  |  |         - file: Filename string or file object to store the article in | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         Returns: | 
					
						
							|  |  |  |         - resp: server response if successful | 
					
						
							|  |  |  |         - nr: article number | 
					
						
							|  |  |  |         - id: message id | 
					
						
							| 
									
										
										
										
											2001-10-01 13:46:55 +00:00
										 |  |  |         - list: the lines of the article's body or an empty list | 
					
						
							| 
									
										
										
										
											2001-10-18 20:58:25 +00:00
										 |  |  |                 if file was used"""
 | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |         return self.artcmd('BODY {0}'.format(id), file) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def article(self, id): | 
					
						
							|  |  |  |         """Process an ARTICLE command.  Argument:
 | 
					
						
							|  |  |  |         - id: article number or message id | 
					
						
							|  |  |  |         Returns: | 
					
						
							|  |  |  |         - resp: server response if successful | 
					
						
							|  |  |  |         - nr: article number | 
					
						
							|  |  |  |         - id: message id | 
					
						
							|  |  |  |         - list: the lines of the article"""
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |         return self.artcmd('ARTICLE {0}'.format(id)) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def slave(self): | 
					
						
							|  |  |  |         """Process a SLAVE command.  Returns:
 | 
					
						
							|  |  |  |         - resp: server response if successful"""
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return self.shortcmd('SLAVE') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-19 18:04:57 +00:00
										 |  |  |     def xhdr(self, hdr, str, file=None): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         """Process an XHDR command (optional server extension).  Arguments:
 | 
					
						
							|  |  |  |         - hdr: the header type (e.g. 'subject') | 
					
						
							|  |  |  |         - str: an article nr, a message id, or a range nr1-nr2 | 
					
						
							|  |  |  |         Returns: | 
					
						
							|  |  |  |         - resp: server response if successful | 
					
						
							|  |  |  |         - list: list of (nr, value) strings"""
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |         pat = re.compile(b'^([0-9]+) ?(.*)\n?') | 
					
						
							|  |  |  |         resp, lines = self.longcmd('XHDR {0} {1}'.format(hdr, str), file) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         for i in range(len(lines)): | 
					
						
							|  |  |  |             line = lines[i] | 
					
						
							|  |  |  |             m = pat.match(line) | 
					
						
							|  |  |  |             if m: | 
					
						
							|  |  |  |                 lines[i] = m.group(1, 2) | 
					
						
							|  |  |  |         return resp, lines | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-19 18:04:57 +00:00
										 |  |  |     def xover(self, start, end, file=None): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         """Process an XOVER command (optional server extension) Arguments:
 | 
					
						
							|  |  |  |         - start: start of range | 
					
						
							|  |  |  |         - end: end of range | 
					
						
							|  |  |  |         Returns: | 
					
						
							|  |  |  |         - resp: server response if successful | 
					
						
							|  |  |  |         - list: list of (art-nr, subject, poster, date, | 
					
						
							|  |  |  |                          id, references, size, lines)"""
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |         resp, lines = self.longcmd('XOVER {0}-{1}'.format(start, end), file) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         xover_lines = [] | 
					
						
							|  |  |  |         for line in lines: | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |             elem = line.split(b'\t') | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |             try: | 
					
						
							|  |  |  |                 xover_lines.append((elem[0], | 
					
						
							|  |  |  |                                     elem[1], | 
					
						
							|  |  |  |                                     elem[2], | 
					
						
							|  |  |  |                                     elem[3], | 
					
						
							|  |  |  |                                     elem[4], | 
					
						
							| 
									
										
										
										
											2001-02-09 07:02:17 +00:00
										 |  |  |                                     elem[5].split(), | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |                                     elem[6], | 
					
						
							|  |  |  |                                     elem[7])) | 
					
						
							|  |  |  |             except IndexError: | 
					
						
							|  |  |  |                 raise NNTPDataError(line) | 
					
						
							|  |  |  |         return resp,xover_lines | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-19 18:04:57 +00:00
										 |  |  |     def xgtitle(self, group, file=None): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         """Process an XGTITLE command (optional server extension) Arguments:
 | 
					
						
							|  |  |  |         - group: group name wildcard (i.e. news.*) | 
					
						
							|  |  |  |         Returns: | 
					
						
							|  |  |  |         - resp: server response if successful | 
					
						
							|  |  |  |         - list: list of (name,title) strings"""
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |         line_pat = re.compile(b'^([^ \t]+)[ \t]+(.*)$') | 
					
						
							| 
									
										
										
										
											2003-04-19 18:04:57 +00:00
										 |  |  |         resp, raw_lines = self.longcmd('XGTITLE ' + group, file) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         lines = [] | 
					
						
							|  |  |  |         for raw_line in raw_lines: | 
					
						
							| 
									
										
										
										
											2001-02-09 07:02:17 +00:00
										 |  |  |             match = line_pat.search(raw_line.strip()) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |             if match: | 
					
						
							|  |  |  |                 lines.append(match.group(1, 2)) | 
					
						
							|  |  |  |         return resp, lines | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def xpath(self,id): | 
					
						
							|  |  |  |         """Process an XPATH command (optional server extension) Arguments:
 | 
					
						
							|  |  |  |         - id: Message id of article | 
					
						
							|  |  |  |         Returns: | 
					
						
							|  |  |  |         resp: server response if successful | 
					
						
							|  |  |  |         path: directory path to article"""
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |         resp = self.shortcmd('XPATH {0}'.format(id)) | 
					
						
							|  |  |  |         if not resp.startswith(b'223'): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |             raise NNTPReplyError(resp) | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2001-02-09 07:02:17 +00:00
										 |  |  |             [resp_num, path] = resp.split() | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         except ValueError: | 
					
						
							|  |  |  |             raise NNTPReplyError(resp) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             return resp, path | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def date (self): | 
					
						
							|  |  |  |         """Process the DATE command. Arguments:
 | 
					
						
							|  |  |  |         None | 
					
						
							|  |  |  |         Returns: | 
					
						
							|  |  |  |         resp: server response if successful | 
					
						
							|  |  |  |         date: Date suitable for newnews/newgroups commands etc. | 
					
						
							|  |  |  |         time: Time suitable for newnews/newgroups commands etc."""
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         resp = self.shortcmd("DATE") | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |         if not resp.startswith(b'111'): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |             raise NNTPReplyError(resp) | 
					
						
							| 
									
										
										
										
											2001-02-09 07:02:17 +00:00
										 |  |  |         elem = resp.split() | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         if len(elem) != 2: | 
					
						
							|  |  |  |             raise NNTPDataError(resp) | 
					
						
							|  |  |  |         date = elem[1][2:8] | 
					
						
							|  |  |  |         time = elem[1][-6:] | 
					
						
							|  |  |  |         if len(date) != 6 or len(time) != 6: | 
					
						
							|  |  |  |             raise NNTPDataError(resp) | 
					
						
							|  |  |  |         return resp, date, time | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |     def _post(self, command, f): | 
					
						
							|  |  |  |         resp = self.shortcmd(command) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         # Raises error_??? if posting is not allowed | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |         if not resp.startswith(b'3'): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |             raise NNTPReplyError(resp) | 
					
						
							|  |  |  |         while 1: | 
					
						
							|  |  |  |             line = f.readline() | 
					
						
							|  |  |  |             if not line: | 
					
						
							|  |  |  |                 break | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |             if line.endswith(b'\n'): | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |                 line = line[:-1] | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |             if line.startswith(b'.'): | 
					
						
							|  |  |  |                 line = b'.' + line | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |             self.putline(line) | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |         self.putline(b'.') | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |         return self.getresp() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |     def post(self, f): | 
					
						
							|  |  |  |         """Process a POST command.  Arguments:
 | 
					
						
							|  |  |  |         - f: file containing the article | 
					
						
							|  |  |  |         Returns: | 
					
						
							|  |  |  |         - resp: server response if successful"""
 | 
					
						
							|  |  |  |         return self._post('POST', f) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |     def ihave(self, id, f): | 
					
						
							|  |  |  |         """Process an IHAVE command.  Arguments:
 | 
					
						
							|  |  |  |         - id: message-id of the article | 
					
						
							|  |  |  |         - f:  file containing the article | 
					
						
							|  |  |  |         Returns: | 
					
						
							|  |  |  |         - resp: server response if successful | 
					
						
							|  |  |  |         Note that if the server refuses the article an exception is raised."""
 | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |         return self._post('IHAVE {0}'.format(id), f) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def quit(self): | 
					
						
							|  |  |  |         """Process a QUIT command and close the socket.  Returns:
 | 
					
						
							|  |  |  |         - resp: server response if successful"""
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         resp = self.shortcmd('QUIT') | 
					
						
							|  |  |  |         self.file.close() | 
					
						
							|  |  |  |         self.sock.close() | 
					
						
							|  |  |  |         del self.file, self.sock | 
					
						
							|  |  |  |         return resp | 
					
						
							| 
									
										
										
										
											1997-08-26 23:26:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-14 02:19:44 +00:00
										 |  |  | # Test retrieval when run as a script. | 
					
						
							| 
									
										
										
										
											2002-11-13 23:05:35 +00:00
										 |  |  | # Assumption: if there's a local news server, it's called 'news'. | 
					
						
							|  |  |  | # Assumption: if user queries a remote news server, it's named | 
					
						
							|  |  |  | # in the environment variable NNTPSERVER (used by slrn and kin) | 
					
						
							|  |  |  | # and we want readermode off. | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     import os | 
					
						
							|  |  |  |     newshost = 'news' and os.environ["NNTPSERVER"] | 
					
						
							|  |  |  |     if newshost.find('.') == -1: | 
					
						
							|  |  |  |         mode = 'readermode' | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         mode = None | 
					
						
							|  |  |  |     s = NNTP(newshost, readermode=mode) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |     resp, count, first, last, name = s.group('comp.lang.python') | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |     print(resp) | 
					
						
							|  |  |  |     print('Group', name, 'has', count, 'articles, range', first, 'to', last) | 
					
						
							| 
									
										
										
										
											2008-11-05 19:44:21 +00:00
										 |  |  |     resp, subs = s.xhdr('subject', '{0}-{1}'.format(first, last)) | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |     print(resp) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |     for item in subs: | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |         print("%7s %s" % item) | 
					
						
							| 
									
										
										
										
											2001-01-15 00:50:52 +00:00
										 |  |  |     resp = s.quit() | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |     print(resp) |