Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 1 | """An NNTP client class based on: |
| 2 | - RFC 977: Network News Transfer Protocol |
| 3 | - RFC 2980: Common NNTP Extensions |
| 4 | - RFC 3977: Network News Transfer Protocol (version 2) |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 6 | Example: |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 8 | >>> from nntplib import NNTP |
| 9 | >>> s = NNTP('news') |
| 10 | >>> resp, count, first, last, name = s.group('comp.lang.python') |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 11 | >>> print('Group', name, 'has', count, 'articles, range', first, 'to', last) |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 12 | Group comp.lang.python has 51 articles, range 5770 to 5821 |
Christian Heimes | 933238a | 2008-11-05 19:44:21 +0000 | [diff] [blame] | 13 | >>> resp, subs = s.xhdr('subject', '{0}-{1}'.format(first, last)) |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 14 | >>> resp = s.quit() |
| 15 | >>> |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 17 | Here 'resp' is the server response line. |
| 18 | Error responses are turned into exceptions. |
| 19 | |
| 20 | To post an article from a file: |
Christian Heimes | 933238a | 2008-11-05 19:44:21 +0000 | [diff] [blame] | 21 | >>> f = open(filename, 'rb') # file containing article, including header |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 22 | >>> resp = s.post(f) |
| 23 | >>> |
| 24 | |
| 25 | For descriptions of all methods, read the comments in the code below. |
| 26 | Note that all arguments and return values representing article numbers |
| 27 | are strings, not numbers, since they are rarely used for calculations. |
| 28 | """ |
| 29 | |
| 30 | # RFC 977 by Brian Kantor and Phil Lapsley. |
| 31 | # xover, xgtitle, xpath, date methods by Kevan Heydon |
Guido van Rossum | 8421c4e | 1995-09-22 00:52:38 +0000 | [diff] [blame] | 32 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 33 | # Incompatible changes from the 2.x nntplib: |
| 34 | # - all commands are encoded as UTF-8 data (using the "surrogateescape" |
| 35 | # error handler), except for raw message data (POST, IHAVE) |
| 36 | # - all responses are decoded as UTF-8 data (using the "surrogateescape" |
| 37 | # error handler), except for raw message data (ARTICLE, HEAD, BODY) |
| 38 | # - the `file` argument to various methods is keyword-only |
| 39 | # |
| 40 | # - NNTP.date() returns a datetime object |
| 41 | # - NNTP.newgroups() and NNTP.newnews() take a datetime (or date) object, |
| 42 | # rather than a pair of (date, time) strings. |
| 43 | # - NNTP.newgroups() and NNTP.list() return a list of GroupInfo named tuples |
| 44 | # - NNTP.descriptions() returns a dict mapping group names to descriptions |
| 45 | # - NNTP.xover() returns a list of dicts mapping field names (header or metadata) |
| 46 | # to field values; each dict representing a message overview. |
| 47 | # - NNTP.article(), NNTP.head() and NNTP.body() return a (response, ArticleInfo) |
| 48 | # tuple. |
| 49 | # - the "internal" methods have been marked private (they now start with |
| 50 | # an underscore) |
| 51 | |
| 52 | # Other changes from the 2.x/3.1 nntplib: |
| 53 | # - automatic querying of capabilities at connect |
| 54 | # - New method NNTP.getcapabilities() |
| 55 | # - New method NNTP.over() |
| 56 | # - New helper function decode_header() |
| 57 | # - NNTP.post() and NNTP.ihave() accept file objects, bytes-like objects and |
| 58 | # arbitrary iterables yielding lines. |
| 59 | # - An extensive test suite :-) |
| 60 | |
| 61 | # TODO: |
| 62 | # - return structured data (GroupInfo etc.) everywhere |
| 63 | # - support HDR |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 64 | |
| 65 | # Imports |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 66 | import re |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 67 | import socket |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 68 | import collections |
| 69 | import datetime |
| 70 | import warnings |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 71 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 72 | from email.header import decode_header as _email_decode_header |
| 73 | from socket import _GLOBAL_DEFAULT_TIMEOUT |
| 74 | |
| 75 | __all__ = ["NNTP", |
| 76 | "NNTPReplyError", "NNTPTemporaryError", "NNTPPermanentError", |
| 77 | "NNTPProtocolError", "NNTPDataError", |
| 78 | "decode_header", |
| 79 | ] |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 80 | |
Barry Warsaw | 9dd7872 | 2000-02-10 20:25:53 +0000 | [diff] [blame] | 81 | # Exceptions raised when an error or invalid response is received |
| 82 | class NNTPError(Exception): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 83 | """Base class for all nntplib exceptions""" |
| 84 | def __init__(self, *args): |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 85 | Exception.__init__(self, *args) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 86 | try: |
| 87 | self.response = args[0] |
| 88 | except IndexError: |
| 89 | self.response = 'No response given' |
Barry Warsaw | 9dd7872 | 2000-02-10 20:25:53 +0000 | [diff] [blame] | 90 | |
| 91 | class NNTPReplyError(NNTPError): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 92 | """Unexpected [123]xx reply""" |
| 93 | pass |
Barry Warsaw | 9dd7872 | 2000-02-10 20:25:53 +0000 | [diff] [blame] | 94 | |
| 95 | class NNTPTemporaryError(NNTPError): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 96 | """4xx errors""" |
| 97 | pass |
Barry Warsaw | 9dd7872 | 2000-02-10 20:25:53 +0000 | [diff] [blame] | 98 | |
| 99 | class NNTPPermanentError(NNTPError): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 100 | """5xx errors""" |
| 101 | pass |
Barry Warsaw | 9dd7872 | 2000-02-10 20:25:53 +0000 | [diff] [blame] | 102 | |
| 103 | class NNTPProtocolError(NNTPError): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 104 | """Response does not begin with [1-5]""" |
| 105 | pass |
Barry Warsaw | 9dd7872 | 2000-02-10 20:25:53 +0000 | [diff] [blame] | 106 | |
| 107 | class NNTPDataError(NNTPError): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 108 | """Error in response data""" |
| 109 | pass |
Barry Warsaw | 9dd7872 | 2000-02-10 20:25:53 +0000 | [diff] [blame] | 110 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 111 | |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 112 | # Standard port used by NNTP servers |
| 113 | NNTP_PORT = 119 |
| 114 | |
| 115 | |
| 116 | # Response numbers that are followed by additional text (e.g. article) |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 117 | _LONGRESP = { |
| 118 | '100', # HELP |
| 119 | '101', # CAPABILITIES |
| 120 | '211', # LISTGROUP (also not multi-line with GROUP) |
| 121 | '215', # LIST |
| 122 | '220', # ARTICLE |
| 123 | '221', # HEAD, XHDR |
| 124 | '222', # BODY |
| 125 | '224', # OVER, XOVER |
| 126 | '225', # HDR |
| 127 | '230', # NEWNEWS |
| 128 | '231', # NEWGROUPS |
| 129 | '282', # XGTITLE |
| 130 | } |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 131 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 132 | # Default decoded value for LIST OVERVIEW.FMT if not supported |
| 133 | _DEFAULT_OVERVIEW_FMT = [ |
| 134 | "subject", "from", "date", "message-id", "references", ":bytes", ":lines"] |
| 135 | |
| 136 | # Alternative names allowed in LIST OVERVIEW.FMT response |
| 137 | _OVERVIEW_FMT_ALTERNATIVES = { |
| 138 | 'bytes': ':bytes', |
| 139 | 'lines': ':lines', |
| 140 | } |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 141 | |
| 142 | # Line terminators (we always output CRLF, but accept any of CRLF, CR, LF) |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 143 | _CRLF = b'\r\n' |
| 144 | |
| 145 | GroupInfo = collections.namedtuple('GroupInfo', |
| 146 | ['group', 'last', 'first', 'flag']) |
| 147 | |
| 148 | ArticleInfo = collections.namedtuple('ArticleInfo', |
| 149 | ['number', 'message_id', 'lines']) |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 150 | |
| 151 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 152 | # Helper function(s) |
| 153 | def decode_header(header_str): |
| 154 | """Takes an unicode string representing a munged header value |
| 155 | and decodes it as a (possibly non-ASCII) readable value.""" |
| 156 | parts = [] |
| 157 | for v, enc in _email_decode_header(header_str): |
| 158 | if isinstance(v, bytes): |
| 159 | parts.append(v.decode(enc or 'ascii')) |
| 160 | else: |
| 161 | parts.append(v) |
| 162 | return ' '.join(parts) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 163 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 164 | def _parse_overview_fmt(lines): |
| 165 | """Parse a list of string representing the response to LIST OVERVIEW.FMT |
| 166 | and return a list of header/metadata names. |
| 167 | Raises NNTPDataError if the response is not compliant |
| 168 | (cf. RFC 3977, section 8.4).""" |
| 169 | fmt = [] |
| 170 | for line in lines: |
| 171 | if line[0] == ':': |
| 172 | # Metadata name (e.g. ":bytes") |
| 173 | name, _, suffix = line[1:].partition(':') |
| 174 | name = ':' + name |
| 175 | else: |
| 176 | # Header name (e.g. "Subject:" or "Xref:full") |
| 177 | name, _, suffix = line.partition(':') |
| 178 | name = name.lower() |
| 179 | name = _OVERVIEW_FMT_ALTERNATIVES.get(name, name) |
| 180 | # Should we do something with the suffix? |
| 181 | fmt.append(name) |
| 182 | defaults = _DEFAULT_OVERVIEW_FMT |
| 183 | if len(fmt) < len(defaults): |
| 184 | raise NNTPDataError("LIST OVERVIEW.FMT response too short") |
| 185 | if fmt[:len(defaults)] != defaults: |
| 186 | raise NNTPDataError("LIST OVERVIEW.FMT redefines default fields") |
| 187 | return fmt |
| 188 | |
| 189 | def _parse_overview(lines, fmt, data_process_func=None): |
| 190 | """Parse the response to a OVER or XOVER command according to the |
| 191 | overview format `fmt`.""" |
| 192 | n_defaults = len(_DEFAULT_OVERVIEW_FMT) |
| 193 | overview = [] |
| 194 | for line in lines: |
| 195 | fields = {} |
| 196 | article_number, *tokens = line.split('\t') |
| 197 | article_number = int(article_number) |
| 198 | for i, token in enumerate(tokens): |
| 199 | if i >= len(fmt): |
| 200 | # XXX should we raise an error? Some servers might not |
| 201 | # support LIST OVERVIEW.FMT and still return additional |
| 202 | # headers. |
| 203 | continue |
| 204 | field_name = fmt[i] |
| 205 | is_metadata = field_name.startswith(':') |
| 206 | if i >= n_defaults and not is_metadata: |
| 207 | # Non-default header names are included in full in the response |
| 208 | h = field_name + ":" |
| 209 | if token[:len(h)].lower() != h: |
| 210 | raise NNTPDataError("OVER/XOVER response doesn't include " |
| 211 | "names of additional headers") |
| 212 | token = token[len(h):].lstrip(" ") |
| 213 | fields[fmt[i]] = token |
| 214 | overview.append((article_number, fields)) |
| 215 | return overview |
| 216 | |
| 217 | def _parse_datetime(date_str, time_str=None): |
| 218 | """Parse a pair of (date, time) strings, and return a datetime object. |
| 219 | If only the date is given, it is assumed to be date and time |
| 220 | concatenated together (e.g. response to the DATE command). |
| 221 | """ |
| 222 | if time_str is None: |
| 223 | time_str = date_str[-6:] |
| 224 | date_str = date_str[:-6] |
| 225 | hours = int(time_str[:2]) |
| 226 | minutes = int(time_str[2:4]) |
| 227 | seconds = int(time_str[4:]) |
| 228 | year = int(date_str[:-4]) |
| 229 | month = int(date_str[-4:-2]) |
| 230 | day = int(date_str[-2:]) |
| 231 | # RFC 3977 doesn't say how to interpret 2-char years. Assume that |
| 232 | # there are no dates before 1970 on Usenet. |
| 233 | if year < 70: |
| 234 | year += 2000 |
| 235 | elif year < 100: |
| 236 | year += 1900 |
| 237 | return datetime.datetime(year, month, day, hours, minutes, seconds) |
| 238 | |
| 239 | def _unparse_datetime(dt, legacy=False): |
| 240 | """Format a date or datetime object as a pair of (date, time) strings |
| 241 | in the format required by the NEWNEWS and NEWGROUPS commands. If a |
| 242 | date object is passed, the time is assumed to be midnight (00h00). |
| 243 | |
| 244 | The returned representation depends on the legacy flag: |
| 245 | * if legacy is False (the default): |
| 246 | date has the YYYYMMDD format and time the HHMMSS format |
| 247 | * if legacy is True: |
| 248 | date has the YYMMDD format and time the HHMMSS format. |
| 249 | RFC 3977 compliant servers should understand both formats; therefore, |
| 250 | legacy is only needed when talking to old servers. |
| 251 | """ |
| 252 | if not isinstance(dt, datetime.datetime): |
| 253 | time_str = "000000" |
| 254 | else: |
| 255 | time_str = "{0.hour:02d}{0.minute:02d}{0.second:02d}".format(dt) |
| 256 | y = dt.year |
| 257 | if legacy: |
| 258 | y = y % 100 |
| 259 | date_str = "{0:02d}{1.month:02d}{1.day:02d}".format(y, dt) |
| 260 | else: |
| 261 | date_str = "{0:04d}{1.month:02d}{1.day:02d}".format(y, dt) |
| 262 | return date_str, time_str |
| 263 | |
| 264 | |
| 265 | # The classes themselves |
| 266 | class _NNTPBase: |
| 267 | # UTF-8 is the character set for all NNTP commands and responses: they |
| 268 | # are automatically encoded (when sending) and decoded (and receiving) |
| 269 | # by this class. |
| 270 | # However, some multi-line data blocks can contain arbitrary bytes (for |
| 271 | # example, latin-1 or utf-16 data in the body of a message). Commands |
| 272 | # taking (POST, IHAVE) or returning (HEAD, BODY, ARTICLE) raw message |
| 273 | # data will therefore only accept and produce bytes objects. |
| 274 | # Furthermore, since there could be non-compliant servers out there, |
| 275 | # we use 'surrogateescape' as the error handler for fault tolerance |
| 276 | # and easy round-tripping. This could be useful for some applications |
| 277 | # (e.g. NNTP gateways). |
| 278 | |
| 279 | encoding = 'utf-8' |
| 280 | errors = 'surrogateescape' |
| 281 | |
| 282 | def __init__(self, file, user=None, password=None, |
| 283 | readermode=None, usenetrc=True, |
| 284 | timeout=_GLOBAL_DEFAULT_TIMEOUT): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 285 | """Initialize an instance. Arguments: |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 286 | - file: file-like object (open for read/write in binary mode) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 287 | - user: username to authenticate with |
| 288 | - password: password to use with username |
| 289 | - readermode: if true, send 'mode reader' command after |
| 290 | connecting. |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 291 | - usenetrc: allow loading username and password from ~/.netrc file |
| 292 | if not specified explicitly |
| 293 | - timeout: timeout (in seconds) used for socket connections |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 294 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 295 | readermode is sometimes necessary if you are connecting to an |
| 296 | NNTP server on the local machine and intend to call |
| 297 | reader-specific comamnds, such as `group'. If you get |
| 298 | unexpected NNTPPermanentErrors, you might need to set |
| 299 | readermode. |
| 300 | """ |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 301 | self.file = file |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 302 | self.debugging = 0 |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 303 | self.welcome = self._getresp() |
Tim Peters | dfb673b | 2001-01-16 07:12:46 +0000 | [diff] [blame] | 304 | |
Thomas Wouters | 47adcba | 2001-01-16 06:35:14 +0000 | [diff] [blame] | 305 | # 'mode reader' is sometimes necessary to enable 'reader' mode. |
Tim Peters | dfb673b | 2001-01-16 07:12:46 +0000 | [diff] [blame] | 306 | # However, the order in which 'mode reader' and 'authinfo' need to |
Thomas Wouters | 47adcba | 2001-01-16 06:35:14 +0000 | [diff] [blame] | 307 | # arrive differs between some NNTP servers. Try to send |
| 308 | # 'mode reader', and if it fails with an authorization failed |
| 309 | # error, try again after sending authinfo. |
| 310 | readermode_afterauth = 0 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 311 | if readermode: |
| 312 | try: |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 313 | self.welcome = self._shortcmd('mode reader') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 314 | except NNTPPermanentError: |
| 315 | # error 500, probably 'not implemented' |
| 316 | pass |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 317 | except NNTPTemporaryError as e: |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 318 | if user and e.response.startswith('480'): |
Thomas Wouters | 47adcba | 2001-01-16 06:35:14 +0000 | [diff] [blame] | 319 | # Need authorization before 'mode reader' |
| 320 | readermode_afterauth = 1 |
| 321 | else: |
| 322 | raise |
Eric S. Raymond | b2db587 | 2002-11-13 23:05:35 +0000 | [diff] [blame] | 323 | # If no login/password was specified, try to get them from ~/.netrc |
| 324 | # Presume that if .netc has an entry, NNRP authentication is required. |
Eric S. Raymond | 782d940 | 2002-11-17 17:53:12 +0000 | [diff] [blame] | 325 | try: |
Martin v. Löwis | 9513e34 | 2004-08-03 14:36:32 +0000 | [diff] [blame] | 326 | if usenetrc and not user: |
Eric S. Raymond | 782d940 | 2002-11-17 17:53:12 +0000 | [diff] [blame] | 327 | import netrc |
| 328 | credentials = netrc.netrc() |
| 329 | auth = credentials.authenticators(host) |
| 330 | if auth: |
| 331 | user = auth[0] |
| 332 | password = auth[2] |
| 333 | except IOError: |
| 334 | pass |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 335 | # Perform NNTP authentication if needed. |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 336 | if user: |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 337 | resp = self._shortcmd('authinfo user '+user) |
| 338 | if resp.startswith('381'): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 339 | if not password: |
| 340 | raise NNTPReplyError(resp) |
| 341 | else: |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 342 | resp = self._shortcmd( |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 343 | 'authinfo pass '+password) |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 344 | if not resp.startswith('281'): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 345 | raise NNTPPermanentError(resp) |
Thomas Wouters | 47adcba | 2001-01-16 06:35:14 +0000 | [diff] [blame] | 346 | if readermode_afterauth: |
| 347 | try: |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 348 | self.welcome = self._shortcmd('mode reader') |
Thomas Wouters | 47adcba | 2001-01-16 06:35:14 +0000 | [diff] [blame] | 349 | except NNTPPermanentError: |
| 350 | # error 500, probably 'not implemented' |
| 351 | pass |
Tim Peters | dfb673b | 2001-01-16 07:12:46 +0000 | [diff] [blame] | 352 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 353 | # Inquire about capabilities (RFC 3977) |
| 354 | self.nntp_version = 1 |
| 355 | try: |
| 356 | resp, caps = self.capabilities() |
| 357 | except NNTPPermanentError: |
| 358 | # Server doesn't support capabilities |
| 359 | self._caps = {} |
| 360 | else: |
| 361 | self._caps = caps |
| 362 | if 'VERSION' in caps: |
| 363 | self.nntp_version = int(caps['VERSION'][0]) |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 364 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 365 | def getwelcome(self): |
| 366 | """Get the welcome message from the server |
| 367 | (this is read and squirreled away by __init__()). |
| 368 | If the response code is 200, posting is allowed; |
| 369 | if it 201, posting is not allowed.""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 370 | |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 371 | if self.debugging: print('*welcome*', repr(self.welcome)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 372 | return self.welcome |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 373 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 374 | def getcapabilities(self): |
| 375 | """Get the server capabilities, as read by __init__(). |
| 376 | If the CAPABILITIES command is not supported, an empty dict is |
| 377 | returned.""" |
| 378 | return self._caps |
| 379 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 380 | def set_debuglevel(self, level): |
| 381 | """Set the debugging level. Argument 'level' means: |
| 382 | 0: no debugging output (default) |
| 383 | 1: print commands and responses but not body text etc. |
| 384 | 2: also print raw lines read and sent before stripping CR/LF""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 385 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 386 | self.debugging = level |
| 387 | debug = set_debuglevel |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 388 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 389 | def _putline(self, line): |
| 390 | """Internal: send one line to the server, appending CRLF. |
| 391 | The `line` must be a bytes-like object.""" |
| 392 | line = line + _CRLF |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 393 | if self.debugging > 1: print('*put*', repr(line)) |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 394 | self.file.write(line) |
| 395 | self.file.flush() |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 396 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 397 | def _putcmd(self, line): |
| 398 | """Internal: send one command to the server (through _putline()). |
| 399 | The `line` must be an unicode string.""" |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 400 | if self.debugging: print('*cmd*', repr(line)) |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 401 | line = line.encode(self.encoding, self.errors) |
| 402 | self._putline(line) |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 403 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 404 | def _getline(self, strip_crlf=True): |
| 405 | """Internal: return one line from the server, stripping _CRLF. |
| 406 | Raise EOFError if the connection is closed. |
| 407 | Returns a bytes object.""" |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 408 | line = self.file.readline() |
| 409 | if self.debugging > 1: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 410 | print('*get*', repr(line)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 411 | if not line: raise EOFError |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 412 | if strip_crlf: |
| 413 | if line[-2:] == _CRLF: |
| 414 | line = line[:-2] |
| 415 | elif line[-1:] in _CRLF: |
| 416 | line = line[:-1] |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 417 | return line |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 418 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 419 | def _getresp(self): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 420 | """Internal: get a response from the server. |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 421 | Raise various errors if the response indicates an error. |
| 422 | Returns an unicode string.""" |
| 423 | resp = self._getline() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 424 | if self.debugging: print('*resp*', repr(resp)) |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 425 | resp = resp.decode(self.encoding, self.errors) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 426 | c = resp[:1] |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 427 | if c == '4': |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 428 | raise NNTPTemporaryError(resp) |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 429 | if c == '5': |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 430 | raise NNTPPermanentError(resp) |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 431 | if c not in '123': |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 432 | raise NNTPProtocolError(resp) |
| 433 | return resp |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 434 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 435 | def _getlongresp(self, file=None): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 436 | """Internal: get a response plus following text from the server. |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 437 | Raise various errors if the response indicates an error. |
| 438 | |
| 439 | Returns a (response, lines) tuple where `response` is an unicode |
| 440 | string and `lines` is a list of bytes objects. |
| 441 | If `file` is a file-like object, it must be open in binary mode. |
| 442 | """ |
Guido van Rossum | d1d584f | 2001-10-01 13:46:55 +0000 | [diff] [blame] | 443 | |
| 444 | openedFile = None |
| 445 | try: |
| 446 | # If a string was passed then open a file with that name |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 447 | if isinstance(file, (str, bytes)): |
| 448 | openedFile = file = open(file, "wb") |
Guido van Rossum | d1d584f | 2001-10-01 13:46:55 +0000 | [diff] [blame] | 449 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 450 | resp = self._getresp() |
| 451 | if resp[:3] not in _LONGRESP: |
Guido van Rossum | d1d584f | 2001-10-01 13:46:55 +0000 | [diff] [blame] | 452 | raise NNTPReplyError(resp) |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 453 | |
| 454 | lines = [] |
| 455 | if file is not None: |
| 456 | # XXX lines = None instead? |
| 457 | terminators = (b'.' + _CRLF, b'.\n') |
| 458 | while 1: |
| 459 | line = self._getline(False) |
| 460 | if line in terminators: |
| 461 | break |
| 462 | if line.startswith(b'..'): |
| 463 | line = line[1:] |
| 464 | file.write(line) |
| 465 | else: |
| 466 | terminator = b'.' |
| 467 | while 1: |
| 468 | line = self._getline() |
| 469 | if line == terminator: |
| 470 | break |
| 471 | if line.startswith(b'..'): |
| 472 | line = line[1:] |
| 473 | lines.append(line) |
Guido van Rossum | d1d584f | 2001-10-01 13:46:55 +0000 | [diff] [blame] | 474 | finally: |
| 475 | # If this method created the file, then it must close it |
| 476 | if openedFile: |
| 477 | openedFile.close() |
| 478 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 479 | return resp, lines |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 480 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 481 | def _shortcmd(self, line): |
| 482 | """Internal: send a command and get the response. |
| 483 | Same return value as _getresp().""" |
| 484 | self._putcmd(line) |
| 485 | return self._getresp() |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 486 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 487 | def _longcmd(self, line, file=None): |
| 488 | """Internal: send a command and get the response plus following text. |
| 489 | Same return value as _getlongresp().""" |
| 490 | self._putcmd(line) |
| 491 | return self._getlongresp(file) |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 492 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 493 | def _longcmdstring(self, line, file=None): |
| 494 | """Internal: send a command and get the response plus following text. |
| 495 | Same as _longcmd() and _getlongresp(), except that the returned `lines` |
| 496 | are unicode strings rather than bytes objects. |
| 497 | """ |
| 498 | self._putcmd(line) |
| 499 | resp, list = self._getlongresp(file) |
| 500 | return resp, [line.decode(self.encoding, self.errors) |
| 501 | for line in list] |
| 502 | |
| 503 | def _getoverviewfmt(self): |
| 504 | """Internal: get the overview format. Queries the server if not |
| 505 | already done, else returns the cached value.""" |
| 506 | try: |
| 507 | return self._cachedoverviewfmt |
| 508 | except AttributeError: |
| 509 | pass |
| 510 | try: |
| 511 | resp, lines = self._longcmdstring("LIST OVERVIEW.FMT") |
| 512 | except NNTPPermanentError: |
| 513 | # Not supported by server? |
| 514 | fmt = _DEFAULT_OVERVIEW_FMT[:] |
| 515 | else: |
| 516 | fmt = _parse_overview_fmt(lines) |
| 517 | self._cachedoverviewfmt = fmt |
| 518 | return fmt |
| 519 | |
| 520 | def _grouplist(self, lines): |
| 521 | # Parse lines into "group last first flag" |
| 522 | return [GroupInfo(*line.split()) for line in lines] |
| 523 | |
| 524 | def capabilities(self): |
| 525 | """Process a CAPABILITIES command. Not supported by all servers. |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 526 | Return: |
| 527 | - resp: server response if successful |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 528 | - caps: a dictionary mapping capability names to lists of tokens |
| 529 | (for example {'VERSION': ['2'], 'OVER': [], LIST: ['ACTIVE', 'HEADERS'] }) |
| 530 | """ |
| 531 | caps = {} |
| 532 | resp, lines = self._longcmdstring("CAPABILITIES") |
| 533 | for line in lines: |
| 534 | name, *tokens = line.split() |
| 535 | caps[name] = tokens |
| 536 | return resp, caps |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 537 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 538 | def newgroups(self, date, *, file=None): |
| 539 | """Process a NEWGROUPS command. Arguments: |
| 540 | - date: a date or datetime object |
| 541 | Return: |
| 542 | - resp: server response if successful |
| 543 | - list: list of newsgroup names |
| 544 | """ |
| 545 | if not isinstance(date, (datetime.date, datetime.date)): |
| 546 | raise TypeError( |
| 547 | "the date parameter must be a date or datetime object, " |
| 548 | "not '{:40}'".format(date.__class__.__name__)) |
| 549 | date_str, time_str = _unparse_datetime(date, self.nntp_version < 2) |
| 550 | cmd = 'NEWGROUPS {0} {1}'.format(date_str, time_str) |
| 551 | resp, lines = self._longcmdstring(cmd, file) |
| 552 | return resp, self._grouplist(lines) |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 553 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 554 | def newnews(self, group, date, *, file=None): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 555 | """Process a NEWNEWS command. Arguments: |
| 556 | - group: group name or '*' |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 557 | - date: a date or datetime object |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 558 | Return: |
| 559 | - resp: server response if successful |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 560 | - list: list of message ids |
| 561 | """ |
| 562 | if not isinstance(date, (datetime.date, datetime.date)): |
| 563 | raise TypeError( |
| 564 | "the date parameter must be a date or datetime object, " |
| 565 | "not '{:40}'".format(date.__class__.__name__)) |
| 566 | date_str, time_str = _unparse_datetime(date, self.nntp_version < 2) |
| 567 | cmd = 'NEWNEWS {0} {1} {2}'.format(group, date_str, time_str) |
| 568 | return self._longcmdstring(cmd, file) |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 569 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 570 | def list(self, *, file=None): |
| 571 | """Process a LIST command. Argument: |
| 572 | - file: Filename string or file object to store the result in |
| 573 | Returns: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 574 | - resp: server response if successful |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 575 | - list: list of (group, last, first, flag) (strings) |
| 576 | """ |
| 577 | resp, lines = self._longcmdstring('LIST', file) |
| 578 | return resp, self._grouplist(lines) |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 579 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 580 | def _getdescriptions(self, group_pattern, return_all): |
| 581 | line_pat = re.compile('^(?P<group>[^ \t]+)[ \t]+(.*)$') |
| 582 | # Try the more std (acc. to RFC2980) LIST NEWSGROUPS first |
| 583 | resp, lines = self._longcmdstring('LIST NEWSGROUPS ' + group_pattern) |
| 584 | if not resp.startswith('215'): |
| 585 | # Now the deprecated XGTITLE. This either raises an error |
| 586 | # or succeeds with the same output structure as LIST |
| 587 | # NEWSGROUPS. |
| 588 | resp, lines = self._longcmdstring('XGTITLE ' + group_pattern) |
| 589 | groups = {} |
| 590 | for raw_line in lines: |
| 591 | match = line_pat.search(raw_line.strip()) |
| 592 | if match: |
| 593 | name, desc = match.group(1, 2) |
| 594 | if not return_all: |
| 595 | return desc |
| 596 | groups[name] = desc |
| 597 | if return_all: |
| 598 | return resp, groups |
| 599 | else: |
| 600 | # Nothing found |
| 601 | return '' |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 602 | |
Martin v. Löwis | cc0f932 | 2004-07-26 12:40:50 +0000 | [diff] [blame] | 603 | def description(self, group): |
Martin v. Löwis | cc0f932 | 2004-07-26 12:40:50 +0000 | [diff] [blame] | 604 | """Get a description for a single group. If more than one |
| 605 | group matches ('group' is a pattern), return the first. If no |
| 606 | group matches, return an empty string. |
| 607 | |
| 608 | This elides the response code from the server, since it can |
| 609 | only be '215' or '285' (for xgtitle) anyway. If the response |
| 610 | code is needed, use the 'descriptions' method. |
| 611 | |
| 612 | NOTE: This neither checks for a wildcard in 'group' nor does |
| 613 | it check whether the group actually exists.""" |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 614 | return self._getdescriptions(group, False) |
Martin v. Löwis | cc0f932 | 2004-07-26 12:40:50 +0000 | [diff] [blame] | 615 | |
| 616 | def descriptions(self, group_pattern): |
| 617 | """Get descriptions for a range of groups.""" |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 618 | return self._getdescriptions(group_pattern, True) |
Martin v. Löwis | cc0f932 | 2004-07-26 12:40:50 +0000 | [diff] [blame] | 619 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 620 | def group(self, name): |
| 621 | """Process a GROUP command. Argument: |
| 622 | - group: the group name |
| 623 | Returns: |
| 624 | - resp: server response if successful |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 625 | - count: number of articles |
| 626 | - first: first article number |
| 627 | - last: last article number |
| 628 | - name: the group name |
| 629 | """ |
| 630 | resp = self._shortcmd('GROUP ' + name) |
| 631 | if not resp.startswith('211'): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 632 | raise NNTPReplyError(resp) |
Eric S. Raymond | b9c24fb | 2001-02-09 07:02:17 +0000 | [diff] [blame] | 633 | words = resp.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 634 | count = first = last = 0 |
| 635 | n = len(words) |
| 636 | if n > 1: |
| 637 | count = words[1] |
| 638 | if n > 2: |
| 639 | first = words[2] |
| 640 | if n > 3: |
| 641 | last = words[3] |
| 642 | if n > 4: |
Eric S. Raymond | b9c24fb | 2001-02-09 07:02:17 +0000 | [diff] [blame] | 643 | name = words[4].lower() |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 644 | return resp, int(count), int(first), int(last), name |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 645 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 646 | def help(self, *, file=None): |
| 647 | """Process a HELP command. Argument: |
| 648 | - file: Filename string or file object to store the result in |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 649 | Returns: |
| 650 | - resp: server response if successful |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 651 | - list: list of strings returned by the server in response to the |
| 652 | HELP command |
| 653 | """ |
| 654 | return self._longcmdstring('HELP', file) |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 655 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 656 | def _statparse(self, resp): |
| 657 | """Internal: parse the response line of a STAT, NEXT, LAST, |
| 658 | ARTICLE, HEAD or BODY command.""" |
| 659 | if not resp.startswith('22'): |
| 660 | raise NNTPReplyError(resp) |
| 661 | words = resp.split() |
| 662 | art_num = int(words[1]) |
| 663 | message_id = words[2] |
| 664 | return resp, art_num, message_id |
| 665 | |
| 666 | def _statcmd(self, line): |
| 667 | """Internal: process a STAT, NEXT or LAST command.""" |
| 668 | resp = self._shortcmd(line) |
| 669 | return self._statparse(resp) |
| 670 | |
| 671 | def stat(self, message_spec=None): |
| 672 | """Process a STAT command. Argument: |
| 673 | - message_spec: article number or message id (if not specified, |
| 674 | the current article is selected) |
| 675 | Returns: |
| 676 | - resp: server response if successful |
| 677 | - art_num: the article number |
| 678 | - message_id: the message id |
| 679 | """ |
| 680 | if message_spec: |
| 681 | return self._statcmd('STAT {0}'.format(message_spec)) |
| 682 | else: |
| 683 | return self._statcmd('STAT') |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 684 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 685 | def next(self): |
| 686 | """Process a NEXT command. No arguments. Return as for STAT.""" |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 687 | return self._statcmd('NEXT') |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 688 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 689 | def last(self): |
| 690 | """Process a LAST command. No arguments. Return as for STAT.""" |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 691 | return self._statcmd('LAST') |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 692 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 693 | def _artcmd(self, line, file=None): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 694 | """Internal: process a HEAD, BODY or ARTICLE command.""" |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 695 | resp, lines = self._longcmd(line, file) |
| 696 | resp, art_num, message_id = self._statparse(resp) |
| 697 | return resp, ArticleInfo(art_num, message_id, lines) |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 698 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 699 | def head(self, message_spec=None, *, file=None): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 700 | """Process a HEAD command. Argument: |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 701 | - message_spec: article number or message id |
| 702 | - file: filename string or file object to store the headers in |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 703 | Returns: |
| 704 | - resp: server response if successful |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 705 | - ArticleInfo: (article number, message id, list of header lines) |
| 706 | """ |
| 707 | if message_spec is not None: |
| 708 | cmd = 'HEAD {0}'.format(message_spec) |
| 709 | else: |
| 710 | cmd = 'HEAD' |
| 711 | return self._artcmd(cmd, file) |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 712 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 713 | def body(self, message_spec=None, *, file=None): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 714 | """Process a BODY command. Argument: |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 715 | - message_spec: article number or message id |
| 716 | - file: filename string or file object to store the body in |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 717 | Returns: |
| 718 | - resp: server response if successful |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 719 | - ArticleInfo: (article number, message id, list of body lines) |
| 720 | """ |
| 721 | if message_spec is not None: |
| 722 | cmd = 'BODY {0}'.format(message_spec) |
| 723 | else: |
| 724 | cmd = 'BODY' |
| 725 | return self._artcmd(cmd, file) |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 726 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 727 | def article(self, message_spec=None, *, file=None): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 728 | """Process an ARTICLE command. Argument: |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 729 | - message_spec: article number or message id |
| 730 | - file: filename string or file object to store the article in |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 731 | Returns: |
| 732 | - resp: server response if successful |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 733 | - ArticleInfo: (article number, message id, list of article lines) |
| 734 | """ |
| 735 | if message_spec is not None: |
| 736 | cmd = 'ARTICLE {0}'.format(message_spec) |
| 737 | else: |
| 738 | cmd = 'ARTICLE' |
| 739 | return self._artcmd(cmd, file) |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 740 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 741 | def slave(self): |
| 742 | """Process a SLAVE command. Returns: |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 743 | - resp: server response if successful |
| 744 | """ |
| 745 | return self._shortcmd('SLAVE') |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 746 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 747 | def xhdr(self, hdr, str, *, file=None): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 748 | """Process an XHDR command (optional server extension). Arguments: |
| 749 | - hdr: the header type (e.g. 'subject') |
| 750 | - str: an article nr, a message id, or a range nr1-nr2 |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 751 | - file: Filename string or file object to store the result in |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 752 | Returns: |
| 753 | - resp: server response if successful |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 754 | - list: list of (nr, value) strings |
| 755 | """ |
| 756 | pat = re.compile('^([0-9]+) ?(.*)\n?') |
| 757 | resp, lines = self._longcmdstring('XHDR {0} {1}'.format(hdr, str), file) |
| 758 | def remove_number(line): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 759 | m = pat.match(line) |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 760 | return m.group(1, 2) if m else line |
| 761 | return resp, [remove_number(line) for line in lines] |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 762 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 763 | def xover(self, start, end, *, file=None): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 764 | """Process an XOVER command (optional server extension) Arguments: |
| 765 | - start: start of range |
| 766 | - end: end of range |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 767 | - file: Filename string or file object to store the result in |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 768 | Returns: |
| 769 | - resp: server response if successful |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 770 | - list: list of dicts containing the response fields |
| 771 | """ |
| 772 | resp, lines = self._longcmdstring('XOVER {0}-{1}'.format(start, end), |
| 773 | file) |
| 774 | fmt = self._getoverviewfmt() |
| 775 | return resp, _parse_overview(lines, fmt) |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 776 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 777 | def over(self, message_spec, *, file=None): |
| 778 | """Process an OVER command. If the command isn't supported, fall |
| 779 | back to XOVER. Arguments: |
| 780 | - message_spec: |
| 781 | - either a message id, indicating the article to fetch |
| 782 | information about |
| 783 | - or a (start, end) tuple, indicating a range of article numbers; |
| 784 | if end is None, information up to the newest message will be |
| 785 | retrieved |
| 786 | - or None, indicating the current article number must be used |
| 787 | - file: Filename string or file object to store the result in |
| 788 | Returns: |
| 789 | - resp: server response if successful |
| 790 | - list: list of dicts containing the response fields |
Guido van Rossum | 8421c4e | 1995-09-22 00:52:38 +0000 | [diff] [blame] | 791 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 792 | NOTE: the "message id" form isn't supported by XOVER |
| 793 | """ |
| 794 | cmd = 'OVER' if 'OVER' in self._caps else 'XOVER' |
| 795 | if isinstance(message_spec, (tuple, list)): |
| 796 | start, end = message_spec |
| 797 | cmd += ' {0}-{1}'.format(start, end or '') |
| 798 | elif message_spec is not None: |
| 799 | cmd = cmd + ' ' + message_spec |
| 800 | resp, lines = self._longcmdstring(cmd, file) |
| 801 | fmt = self._getoverviewfmt() |
| 802 | return resp, _parse_overview(lines, fmt) |
| 803 | |
| 804 | def xgtitle(self, group, *, file=None): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 805 | """Process an XGTITLE command (optional server extension) Arguments: |
| 806 | - group: group name wildcard (i.e. news.*) |
| 807 | Returns: |
| 808 | - resp: server response if successful |
| 809 | - list: list of (name,title) strings""" |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 810 | warnings.warn("The XGTITLE extension is not actively used, " |
| 811 | "use descriptions() instead", |
| 812 | PendingDeprecationWarning, 2) |
| 813 | line_pat = re.compile('^([^ \t]+)[ \t]+(.*)$') |
| 814 | resp, raw_lines = self._longcmdstring('XGTITLE ' + group, file) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 815 | lines = [] |
| 816 | for raw_line in raw_lines: |
Eric S. Raymond | b9c24fb | 2001-02-09 07:02:17 +0000 | [diff] [blame] | 817 | match = line_pat.search(raw_line.strip()) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 818 | if match: |
| 819 | lines.append(match.group(1, 2)) |
| 820 | return resp, lines |
Guido van Rossum | 8421c4e | 1995-09-22 00:52:38 +0000 | [diff] [blame] | 821 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 822 | def xpath(self, id): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 823 | """Process an XPATH command (optional server extension) Arguments: |
| 824 | - id: Message id of article |
| 825 | Returns: |
| 826 | resp: server response if successful |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 827 | path: directory path to article |
| 828 | """ |
| 829 | warnings.warn("The XPATH extension is not actively used", |
| 830 | PendingDeprecationWarning, 2) |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 831 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 832 | resp = self._shortcmd('XPATH {0}'.format(id)) |
| 833 | if not resp.startswith('223'): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 834 | raise NNTPReplyError(resp) |
| 835 | try: |
Eric S. Raymond | b9c24fb | 2001-02-09 07:02:17 +0000 | [diff] [blame] | 836 | [resp_num, path] = resp.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 837 | except ValueError: |
| 838 | raise NNTPReplyError(resp) |
| 839 | else: |
| 840 | return resp, path |
Guido van Rossum | 8421c4e | 1995-09-22 00:52:38 +0000 | [diff] [blame] | 841 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 842 | def date(self): |
| 843 | """Process the DATE command. |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 844 | Returns: |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 845 | - resp: server response if successful |
| 846 | - date: datetime object |
| 847 | """ |
| 848 | resp = self._shortcmd("DATE") |
| 849 | if not resp.startswith('111'): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 850 | raise NNTPReplyError(resp) |
Eric S. Raymond | b9c24fb | 2001-02-09 07:02:17 +0000 | [diff] [blame] | 851 | elem = resp.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 852 | if len(elem) != 2: |
| 853 | raise NNTPDataError(resp) |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 854 | date = elem[1] |
| 855 | if len(date) != 14: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 856 | raise NNTPDataError(resp) |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 857 | return resp, _parse_datetime(date, None) |
Guido van Rossum | 8421c4e | 1995-09-22 00:52:38 +0000 | [diff] [blame] | 858 | |
Christian Heimes | 933238a | 2008-11-05 19:44:21 +0000 | [diff] [blame] | 859 | def _post(self, command, f): |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 860 | resp = self._shortcmd(command) |
| 861 | # Raises a specific exception if posting is not allowed |
| 862 | if not resp.startswith('3'): |
Christian Heimes | 933238a | 2008-11-05 19:44:21 +0000 | [diff] [blame] | 863 | raise NNTPReplyError(resp) |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 864 | if isinstance(f, (bytes, bytearray)): |
| 865 | f = f.splitlines() |
| 866 | # We don't use _putline() because: |
| 867 | # - we don't want additional CRLF if the file or iterable is already |
| 868 | # in the right format |
| 869 | # - we don't want a spurious flush() after each line is written |
| 870 | for line in f: |
| 871 | if not line.endswith(_CRLF): |
| 872 | line = line.rstrip(b"\r\n") + _CRLF |
Christian Heimes | 933238a | 2008-11-05 19:44:21 +0000 | [diff] [blame] | 873 | if line.startswith(b'.'): |
| 874 | line = b'.' + line |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 875 | self.file.write(line) |
| 876 | self.file.write(b".\r\n") |
| 877 | self.file.flush() |
| 878 | return self._getresp() |
Guido van Rossum | 8421c4e | 1995-09-22 00:52:38 +0000 | [diff] [blame] | 879 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 880 | def post(self, data): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 881 | """Process a POST command. Arguments: |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 882 | - data: bytes object, iterable or file containing the article |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 883 | Returns: |
| 884 | - resp: server response if successful""" |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 885 | return self._post('POST', data) |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 886 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 887 | def ihave(self, message_id, data): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 888 | """Process an IHAVE command. Arguments: |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 889 | - message_id: message-id of the article |
| 890 | - data: file containing the article |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 891 | Returns: |
| 892 | - resp: server response if successful |
| 893 | Note that if the server refuses the article an exception is raised.""" |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 894 | return self._post('IHAVE {0}'.format(message_id), data) |
| 895 | |
| 896 | def _close(self): |
| 897 | self.file.close() |
| 898 | del self.file |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 899 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 900 | def quit(self): |
| 901 | """Process a QUIT command and close the socket. Returns: |
| 902 | - resp: server response if successful""" |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 903 | try: |
| 904 | resp = self._shortcmd('QUIT') |
| 905 | finally: |
| 906 | self._close() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 907 | return resp |
Guido van Rossum | e2ed9df | 1997-08-26 23:26:18 +0000 | [diff] [blame] | 908 | |
| 909 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 910 | class NNTP(_NNTPBase): |
| 911 | |
| 912 | def __init__(self, host, port=NNTP_PORT, user=None, password=None, |
| 913 | readermode=None, usenetrc=True, |
| 914 | timeout=_GLOBAL_DEFAULT_TIMEOUT): |
| 915 | """Initialize an instance. Arguments: |
| 916 | - host: hostname to connect to |
| 917 | - port: port to connect to (default the standard NNTP port) |
| 918 | - user: username to authenticate with |
| 919 | - password: password to use with username |
| 920 | - readermode: if true, send 'mode reader' command after |
| 921 | connecting. |
| 922 | - usenetrc: allow loading username and password from ~/.netrc file |
| 923 | if not specified explicitly |
| 924 | - timeout: timeout (in seconds) used for socket connections |
| 925 | |
| 926 | readermode is sometimes necessary if you are connecting to an |
| 927 | NNTP server on the local machine and intend to call |
| 928 | reader-specific comamnds, such as `group'. If you get |
| 929 | unexpected NNTPPermanentErrors, you might need to set |
| 930 | readermode. |
| 931 | """ |
| 932 | self.host = host |
| 933 | self.port = port |
| 934 | self.sock = socket.create_connection((host, port), timeout) |
| 935 | file = self.sock.makefile("rwb") |
| 936 | _NNTPBase.__init__(self, file, user, password, |
| 937 | readermode, usenetrc, timeout) |
| 938 | |
| 939 | def _close(self): |
| 940 | try: |
| 941 | _NNTPBase._close(self) |
| 942 | finally: |
| 943 | self.sock.close() |
| 944 | |
| 945 | |
Neal Norwitz | ef67956 | 2002-11-14 02:19:44 +0000 | [diff] [blame] | 946 | # Test retrieval when run as a script. |
Eric S. Raymond | b2db587 | 2002-11-13 23:05:35 +0000 | [diff] [blame] | 947 | if __name__ == '__main__': |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 948 | import argparse |
| 949 | from email.utils import parsedate |
| 950 | |
| 951 | parser = argparse.ArgumentParser(description="""\ |
| 952 | nntplib built-in demo - display the latest articles in a newsgroup""") |
| 953 | parser.add_argument('-g', '--group', default='gmane.comp.python.general', |
| 954 | help='group to fetch messages from (default: %(default)s)') |
| 955 | parser.add_argument('-s', '--server', default='news.gmane.org', |
| 956 | help='NNTP server hostname (default: %(default)s)') |
| 957 | parser.add_argument('-p', '--port', default=NNTP_PORT, type=int, |
| 958 | help='NNTP port number (default: %(default)s)') |
| 959 | parser.add_argument('-n', '--nb-articles', default=10, type=int, |
| 960 | help='number of articles to fetch (default: %(default)s)') |
| 961 | args = parser.parse_args() |
| 962 | |
| 963 | s = NNTP(host=args.server, port=args.port) |
| 964 | resp, count, first, last, name = s.group(args.group) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 965 | print('Group', name, 'has', count, 'articles, range', first, 'to', last) |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 966 | |
| 967 | def cut(s, lim): |
| 968 | if len(s) > lim: |
| 969 | s = s[:lim - 4] + "..." |
| 970 | return s |
| 971 | |
| 972 | first = str(int(last) - args.nb_articles + 1) |
| 973 | resp, overviews = s.xover(first, last) |
| 974 | for artnum, over in overviews: |
| 975 | author = decode_header(over['from']).split('<', 1)[0] |
| 976 | subject = decode_header(over['subject']) |
| 977 | lines = int(over[':lines']) |
| 978 | print("{:7} {:20} {:42} ({})".format( |
| 979 | artnum, cut(author, 20), cut(subject, 42), lines) |
| 980 | ) |
| 981 | |
| 982 | s.quit() |