Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 1 | """An NNTP client class based on RFC 977: Network News Transfer Protocol. |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 3 | Example: |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 5 | >>> from nntplib import NNTP |
| 6 | >>> s = NNTP('news') |
| 7 | >>> resp, count, first, last, name = s.group('comp.lang.python') |
| 8 | >>> print 'Group', name, 'has', count, 'articles, range', first, 'to', last |
| 9 | Group comp.lang.python has 51 articles, range 5770 to 5821 |
| 10 | >>> resp, subs = s.xhdr('subject', first + '-' + last) |
| 11 | >>> resp = s.quit() |
| 12 | >>> |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 14 | Here 'resp' is the server response line. |
| 15 | Error responses are turned into exceptions. |
| 16 | |
| 17 | To post an article from a file: |
| 18 | >>> f = open(filename, 'r') # file containing article, including header |
| 19 | >>> resp = s.post(f) |
| 20 | >>> |
| 21 | |
| 22 | For descriptions of all methods, read the comments in the code below. |
| 23 | Note that all arguments and return values representing article numbers |
| 24 | are strings, not numbers, since they are rarely used for calculations. |
| 25 | """ |
| 26 | |
| 27 | # RFC 977 by Brian Kantor and Phil Lapsley. |
| 28 | # xover, xgtitle, xpath, date methods by Kevan Heydon |
Guido van Rossum | 8421c4e | 1995-09-22 00:52:38 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 30 | |
| 31 | # Imports |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 32 | import re |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 33 | import socket |
| 34 | import string |
| 35 | |
| 36 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 37 | |
Barry Warsaw | 9dd7872 | 2000-02-10 20:25:53 +0000 | [diff] [blame] | 38 | # Exceptions raised when an error or invalid response is received |
| 39 | class NNTPError(Exception): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 40 | """Base class for all nntplib exceptions""" |
| 41 | def __init__(self, *args): |
| 42 | apply(Exception.__init__, (self,)+args) |
| 43 | try: |
| 44 | self.response = args[0] |
| 45 | except IndexError: |
| 46 | self.response = 'No response given' |
Barry Warsaw | 9dd7872 | 2000-02-10 20:25:53 +0000 | [diff] [blame] | 47 | |
| 48 | class NNTPReplyError(NNTPError): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 49 | """Unexpected [123]xx reply""" |
| 50 | pass |
Barry Warsaw | 9dd7872 | 2000-02-10 20:25:53 +0000 | [diff] [blame] | 51 | |
| 52 | class NNTPTemporaryError(NNTPError): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 53 | """4xx errors""" |
| 54 | pass |
Barry Warsaw | 9dd7872 | 2000-02-10 20:25:53 +0000 | [diff] [blame] | 55 | |
| 56 | class NNTPPermanentError(NNTPError): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 57 | """5xx errors""" |
| 58 | pass |
Barry Warsaw | 9dd7872 | 2000-02-10 20:25:53 +0000 | [diff] [blame] | 59 | |
| 60 | class NNTPProtocolError(NNTPError): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 61 | """Response does not begin with [1-5]""" |
| 62 | pass |
Barry Warsaw | 9dd7872 | 2000-02-10 20:25:53 +0000 | [diff] [blame] | 63 | |
| 64 | class NNTPDataError(NNTPError): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 65 | """Error in response data""" |
| 66 | pass |
Barry Warsaw | 9dd7872 | 2000-02-10 20:25:53 +0000 | [diff] [blame] | 67 | |
| 68 | # for backwards compatibility |
| 69 | error_reply = NNTPReplyError |
| 70 | error_temp = NNTPTemporaryError |
| 71 | error_perm = NNTPPermanentError |
| 72 | error_proto = NNTPProtocolError |
| 73 | error_data = NNTPDataError |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 74 | |
| 75 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 76 | |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 77 | # Standard port used by NNTP servers |
| 78 | NNTP_PORT = 119 |
| 79 | |
| 80 | |
| 81 | # Response numbers that are followed by additional text (e.g. article) |
Guido van Rossum | 8421c4e | 1995-09-22 00:52:38 +0000 | [diff] [blame] | 82 | LONGRESP = ['100', '215', '220', '221', '222', '224', '230', '231', '282'] |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 83 | |
| 84 | |
| 85 | # Line terminators (we always output CRLF, but accept any of CRLF, CR, LF) |
| 86 | CRLF = '\r\n' |
| 87 | |
| 88 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 89 | |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 90 | # The class itself |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 91 | class NNTP: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 92 | def __init__(self, host, port=NNTP_PORT, user=None, password=None, |
| 93 | readermode=None): |
| 94 | """Initialize an instance. Arguments: |
| 95 | - host: hostname to connect to |
| 96 | - port: port to connect to (default the standard NNTP port) |
| 97 | - user: username to authenticate with |
| 98 | - password: password to use with username |
| 99 | - readermode: if true, send 'mode reader' command after |
| 100 | connecting. |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 101 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 102 | readermode is sometimes necessary if you are connecting to an |
| 103 | NNTP server on the local machine and intend to call |
| 104 | reader-specific comamnds, such as `group'. If you get |
| 105 | unexpected NNTPPermanentErrors, you might need to set |
| 106 | readermode. |
| 107 | """ |
| 108 | self.host = host |
| 109 | self.port = port |
| 110 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 111 | self.sock.connect((self.host, self.port)) |
| 112 | self.file = self.sock.makefile('rb') |
| 113 | self.debugging = 0 |
| 114 | self.welcome = self.getresp() |
Tim Peters | dfb673b | 2001-01-16 07:12:46 +0000 | [diff] [blame] | 115 | |
Thomas Wouters | 47adcba | 2001-01-16 06:35:14 +0000 | [diff] [blame] | 116 | # 'mode reader' is sometimes necessary to enable 'reader' mode. |
Tim Peters | dfb673b | 2001-01-16 07:12:46 +0000 | [diff] [blame] | 117 | # However, the order in which 'mode reader' and 'authinfo' need to |
Thomas Wouters | 47adcba | 2001-01-16 06:35:14 +0000 | [diff] [blame] | 118 | # arrive differs between some NNTP servers. Try to send |
| 119 | # 'mode reader', and if it fails with an authorization failed |
| 120 | # error, try again after sending authinfo. |
| 121 | readermode_afterauth = 0 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 122 | if readermode: |
| 123 | try: |
| 124 | self.welcome = self.shortcmd('mode reader') |
| 125 | except NNTPPermanentError: |
| 126 | # error 500, probably 'not implemented' |
| 127 | pass |
Thomas Wouters | 47adcba | 2001-01-16 06:35:14 +0000 | [diff] [blame] | 128 | except NNTPTemporaryError, e: |
| 129 | if user and e.response[:3] == '480': |
| 130 | # Need authorization before 'mode reader' |
| 131 | readermode_afterauth = 1 |
| 132 | else: |
| 133 | raise |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 134 | if user: |
| 135 | resp = self.shortcmd('authinfo user '+user) |
| 136 | if resp[:3] == '381': |
| 137 | if not password: |
| 138 | raise NNTPReplyError(resp) |
| 139 | else: |
| 140 | resp = self.shortcmd( |
| 141 | 'authinfo pass '+password) |
| 142 | if resp[:3] != '281': |
| 143 | raise NNTPPermanentError(resp) |
Thomas Wouters | 47adcba | 2001-01-16 06:35:14 +0000 | [diff] [blame] | 144 | if readermode_afterauth: |
| 145 | try: |
| 146 | self.welcome = self.shortcmd('mode reader') |
| 147 | except NNTPPermanentError: |
| 148 | # error 500, probably 'not implemented' |
| 149 | pass |
Tim Peters | dfb673b | 2001-01-16 07:12:46 +0000 | [diff] [blame] | 150 | |
Barry Warsaw | 9dd7872 | 2000-02-10 20:25:53 +0000 | [diff] [blame] | 151 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 152 | # Get the welcome message from the server |
| 153 | # (this is read and squirreled away by __init__()). |
| 154 | # If the response code is 200, posting is allowed; |
| 155 | # if it 201, posting is not allowed |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 156 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 157 | def getwelcome(self): |
| 158 | """Get the welcome message from the server |
| 159 | (this is read and squirreled away by __init__()). |
| 160 | If the response code is 200, posting is allowed; |
| 161 | if it 201, posting is not allowed.""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 162 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 163 | if self.debugging: print '*welcome*', `self.welcome` |
| 164 | return self.welcome |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 165 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 166 | def set_debuglevel(self, level): |
| 167 | """Set the debugging level. Argument 'level' means: |
| 168 | 0: no debugging output (default) |
| 169 | 1: print commands and responses but not body text etc. |
| 170 | 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] | 171 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 172 | self.debugging = level |
| 173 | debug = set_debuglevel |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 174 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 175 | def putline(self, line): |
| 176 | """Internal: send one line to the server, appending CRLF.""" |
| 177 | line = line + CRLF |
| 178 | if self.debugging > 1: print '*put*', `line` |
| 179 | self.sock.send(line) |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 180 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 181 | def putcmd(self, line): |
| 182 | """Internal: send one command to the server (through putline()).""" |
| 183 | if self.debugging: print '*cmd*', `line` |
| 184 | self.putline(line) |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 185 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 186 | def getline(self): |
| 187 | """Internal: return one line from the server, stripping CRLF. |
| 188 | Raise EOFError if the connection is closed.""" |
| 189 | line = self.file.readline() |
| 190 | if self.debugging > 1: |
| 191 | print '*get*', `line` |
| 192 | if not line: raise EOFError |
| 193 | if line[-2:] == CRLF: line = line[:-2] |
| 194 | elif line[-1:] in CRLF: line = line[:-1] |
| 195 | return line |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 196 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 197 | def getresp(self): |
| 198 | """Internal: get a response from the server. |
| 199 | Raise various errors if the response indicates an error.""" |
| 200 | resp = self.getline() |
| 201 | if self.debugging: print '*resp*', `resp` |
| 202 | c = resp[:1] |
| 203 | if c == '4': |
| 204 | raise NNTPTemporaryError(resp) |
| 205 | if c == '5': |
| 206 | raise NNTPPermanentError(resp) |
| 207 | if c not in '123': |
| 208 | raise NNTPProtocolError(resp) |
| 209 | return resp |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 210 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 211 | def getlongresp(self): |
| 212 | """Internal: get a response plus following text from the server. |
| 213 | Raise various errors if the response indicates an error.""" |
| 214 | resp = self.getresp() |
| 215 | if resp[:3] not in LONGRESP: |
| 216 | raise NNTPReplyError(resp) |
| 217 | list = [] |
| 218 | while 1: |
| 219 | line = self.getline() |
| 220 | if line == '.': |
| 221 | break |
| 222 | if line[:2] == '..': |
| 223 | line = line[1:] |
| 224 | list.append(line) |
| 225 | return resp, list |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 226 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 227 | def shortcmd(self, line): |
| 228 | """Internal: send a command and get the response.""" |
| 229 | self.putcmd(line) |
| 230 | return self.getresp() |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 231 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 232 | def longcmd(self, line): |
| 233 | """Internal: send a command and get the response plus following text.""" |
| 234 | self.putcmd(line) |
| 235 | return self.getlongresp() |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 236 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 237 | def newgroups(self, date, time): |
| 238 | """Process a NEWGROUPS command. Arguments: |
| 239 | - date: string 'yymmdd' indicating the date |
| 240 | - time: string 'hhmmss' indicating the time |
| 241 | Return: |
| 242 | - resp: server response if successful |
| 243 | - list: list of newsgroup names""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 244 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 245 | return self.longcmd('NEWGROUPS ' + date + ' ' + time) |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 246 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 247 | def newnews(self, group, date, time): |
| 248 | """Process a NEWNEWS command. Arguments: |
| 249 | - group: group name or '*' |
| 250 | - date: string 'yymmdd' indicating the date |
| 251 | - time: string 'hhmmss' indicating the time |
| 252 | Return: |
| 253 | - resp: server response if successful |
| 254 | - list: list of article ids""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 255 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 256 | cmd = 'NEWNEWS ' + group + ' ' + date + ' ' + time |
| 257 | return self.longcmd(cmd) |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 258 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 259 | def list(self): |
| 260 | """Process a LIST command. Return: |
| 261 | - resp: server response if successful |
| 262 | - list: list of (group, last, first, flag) (strings)""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 263 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 264 | resp, list = self.longcmd('LIST') |
| 265 | for i in range(len(list)): |
| 266 | # Parse lines into "group last first flag" |
| 267 | list[i] = tuple(string.split(list[i])) |
| 268 | return resp, list |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 269 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 270 | def group(self, name): |
| 271 | """Process a GROUP command. Argument: |
| 272 | - group: the group name |
| 273 | Returns: |
| 274 | - resp: server response if successful |
| 275 | - count: number of articles (string) |
| 276 | - first: first article number (string) |
| 277 | - last: last article number (string) |
| 278 | - name: the group name""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 279 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 280 | resp = self.shortcmd('GROUP ' + name) |
| 281 | if resp[:3] != '211': |
| 282 | raise NNTPReplyError(resp) |
| 283 | words = string.split(resp) |
| 284 | count = first = last = 0 |
| 285 | n = len(words) |
| 286 | if n > 1: |
| 287 | count = words[1] |
| 288 | if n > 2: |
| 289 | first = words[2] |
| 290 | if n > 3: |
| 291 | last = words[3] |
| 292 | if n > 4: |
| 293 | name = string.lower(words[4]) |
| 294 | return resp, count, first, last, name |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 295 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 296 | def help(self): |
| 297 | """Process a HELP command. Returns: |
| 298 | - resp: server response if successful |
| 299 | - list: list of strings""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 300 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 301 | return self.longcmd('HELP') |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 302 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 303 | def statparse(self, resp): |
| 304 | """Internal: parse the response of a STAT, NEXT or LAST command.""" |
| 305 | if resp[:2] != '22': |
| 306 | raise NNTPReplyError(resp) |
| 307 | words = string.split(resp) |
| 308 | nr = 0 |
| 309 | id = '' |
| 310 | n = len(words) |
| 311 | if n > 1: |
| 312 | nr = words[1] |
| 313 | if n > 2: |
| 314 | id = words[2] |
| 315 | return resp, nr, id |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 316 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 317 | def statcmd(self, line): |
| 318 | """Internal: process a STAT, NEXT or LAST command.""" |
| 319 | resp = self.shortcmd(line) |
| 320 | return self.statparse(resp) |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 321 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 322 | def stat(self, id): |
| 323 | """Process a STAT command. Argument: |
| 324 | - id: article number or message id |
| 325 | Returns: |
| 326 | - resp: server response if successful |
| 327 | - nr: the article number |
| 328 | - id: the article id""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 329 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 330 | return self.statcmd('STAT ' + id) |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 331 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 332 | def next(self): |
| 333 | """Process a NEXT command. No arguments. Return as for STAT.""" |
| 334 | return self.statcmd('NEXT') |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 335 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 336 | def last(self): |
| 337 | """Process a LAST command. No arguments. Return as for STAT.""" |
| 338 | return self.statcmd('LAST') |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 339 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 340 | def artcmd(self, line): |
| 341 | """Internal: process a HEAD, BODY or ARTICLE command.""" |
| 342 | resp, list = self.longcmd(line) |
| 343 | resp, nr, id = self.statparse(resp) |
| 344 | return resp, nr, id, list |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 345 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 346 | def head(self, id): |
| 347 | """Process a HEAD command. Argument: |
| 348 | - id: article number or message id |
| 349 | Returns: |
| 350 | - resp: server response if successful |
| 351 | - nr: article number |
| 352 | - id: message id |
| 353 | - list: the lines of the article's header""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 354 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 355 | return self.artcmd('HEAD ' + id) |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 356 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 357 | def body(self, id): |
| 358 | """Process a BODY command. Argument: |
| 359 | - id: article number or message id |
| 360 | Returns: |
| 361 | - resp: server response if successful |
| 362 | - nr: article number |
| 363 | - id: message id |
| 364 | - list: the lines of the article's body""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 365 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 366 | return self.artcmd('BODY ' + id) |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 367 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 368 | def article(self, id): |
| 369 | """Process an ARTICLE command. Argument: |
| 370 | - id: article number or message id |
| 371 | Returns: |
| 372 | - resp: server response if successful |
| 373 | - nr: article number |
| 374 | - id: message id |
| 375 | - list: the lines of the article""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 376 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 377 | return self.artcmd('ARTICLE ' + id) |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 378 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 379 | def slave(self): |
| 380 | """Process a SLAVE command. Returns: |
| 381 | - resp: server response if successful""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 382 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 383 | return self.shortcmd('SLAVE') |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 384 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 385 | def xhdr(self, hdr, str): |
| 386 | """Process an XHDR command (optional server extension). Arguments: |
| 387 | - hdr: the header type (e.g. 'subject') |
| 388 | - str: an article nr, a message id, or a range nr1-nr2 |
| 389 | Returns: |
| 390 | - resp: server response if successful |
| 391 | - list: list of (nr, value) strings""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 392 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 393 | pat = re.compile('^([0-9]+) ?(.*)\n?') |
| 394 | resp, lines = self.longcmd('XHDR ' + hdr + ' ' + str) |
| 395 | for i in range(len(lines)): |
| 396 | line = lines[i] |
| 397 | m = pat.match(line) |
| 398 | if m: |
| 399 | lines[i] = m.group(1, 2) |
| 400 | return resp, lines |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 401 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 402 | def xover(self,start,end): |
| 403 | """Process an XOVER command (optional server extension) Arguments: |
| 404 | - start: start of range |
| 405 | - end: end of range |
| 406 | Returns: |
| 407 | - resp: server response if successful |
| 408 | - list: list of (art-nr, subject, poster, date, |
| 409 | id, references, size, lines)""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 410 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 411 | resp, lines = self.longcmd('XOVER ' + start + '-' + end) |
| 412 | xover_lines = [] |
| 413 | for line in lines: |
| 414 | elem = string.splitfields(line,"\t") |
| 415 | try: |
| 416 | xover_lines.append((elem[0], |
| 417 | elem[1], |
| 418 | elem[2], |
| 419 | elem[3], |
| 420 | elem[4], |
| 421 | string.split(elem[5]), |
| 422 | elem[6], |
| 423 | elem[7])) |
| 424 | except IndexError: |
| 425 | raise NNTPDataError(line) |
| 426 | return resp,xover_lines |
Guido van Rossum | 8421c4e | 1995-09-22 00:52:38 +0000 | [diff] [blame] | 427 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 428 | def xgtitle(self, group): |
| 429 | """Process an XGTITLE command (optional server extension) Arguments: |
| 430 | - group: group name wildcard (i.e. news.*) |
| 431 | Returns: |
| 432 | - resp: server response if successful |
| 433 | - list: list of (name,title) strings""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 434 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 435 | line_pat = re.compile("^([^ \t]+)[ \t]+(.*)$") |
| 436 | resp, raw_lines = self.longcmd('XGTITLE ' + group) |
| 437 | lines = [] |
| 438 | for raw_line in raw_lines: |
| 439 | match = line_pat.search(string.strip(raw_line)) |
| 440 | if match: |
| 441 | lines.append(match.group(1, 2)) |
| 442 | return resp, lines |
Guido van Rossum | 8421c4e | 1995-09-22 00:52:38 +0000 | [diff] [blame] | 443 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 444 | def xpath(self,id): |
| 445 | """Process an XPATH command (optional server extension) Arguments: |
| 446 | - id: Message id of article |
| 447 | Returns: |
| 448 | resp: server response if successful |
| 449 | path: directory path to article""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 450 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 451 | resp = self.shortcmd("XPATH " + id) |
| 452 | if resp[:3] != '223': |
| 453 | raise NNTPReplyError(resp) |
| 454 | try: |
| 455 | [resp_num, path] = string.split(resp) |
| 456 | except ValueError: |
| 457 | raise NNTPReplyError(resp) |
| 458 | else: |
| 459 | return resp, path |
Guido van Rossum | 8421c4e | 1995-09-22 00:52:38 +0000 | [diff] [blame] | 460 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 461 | def date (self): |
| 462 | """Process the DATE command. Arguments: |
| 463 | None |
| 464 | Returns: |
| 465 | resp: server response if successful |
| 466 | date: Date suitable for newnews/newgroups commands etc. |
| 467 | time: Time suitable for newnews/newgroups commands etc.""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 468 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 469 | resp = self.shortcmd("DATE") |
| 470 | if resp[:3] != '111': |
| 471 | raise NNTPReplyError(resp) |
| 472 | elem = string.split(resp) |
| 473 | if len(elem) != 2: |
| 474 | raise NNTPDataError(resp) |
| 475 | date = elem[1][2:8] |
| 476 | time = elem[1][-6:] |
| 477 | if len(date) != 6 or len(time) != 6: |
| 478 | raise NNTPDataError(resp) |
| 479 | return resp, date, time |
Guido van Rossum | 8421c4e | 1995-09-22 00:52:38 +0000 | [diff] [blame] | 480 | |
| 481 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 482 | def post(self, f): |
| 483 | """Process a POST command. Arguments: |
| 484 | - f: file containing the article |
| 485 | Returns: |
| 486 | - resp: server response if successful""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 487 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 488 | resp = self.shortcmd('POST') |
| 489 | # Raises error_??? if posting is not allowed |
| 490 | if resp[0] != '3': |
| 491 | raise NNTPReplyError(resp) |
| 492 | while 1: |
| 493 | line = f.readline() |
| 494 | if not line: |
| 495 | break |
| 496 | if line[-1] == '\n': |
| 497 | line = line[:-1] |
| 498 | if line[:1] == '.': |
| 499 | line = '.' + line |
| 500 | self.putline(line) |
| 501 | self.putline('.') |
| 502 | return self.getresp() |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 503 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 504 | def ihave(self, id, f): |
| 505 | """Process an IHAVE command. Arguments: |
| 506 | - id: message-id of the article |
| 507 | - f: file containing the article |
| 508 | Returns: |
| 509 | - resp: server response if successful |
| 510 | Note that if the server refuses the article an exception is raised.""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 511 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 512 | resp = self.shortcmd('IHAVE ' + id) |
| 513 | # Raises error_??? if the server already has it |
| 514 | if resp[0] != '3': |
| 515 | raise NNTPReplyError(resp) |
| 516 | while 1: |
| 517 | line = f.readline() |
| 518 | if not line: |
| 519 | break |
| 520 | if line[-1] == '\n': |
| 521 | line = line[:-1] |
| 522 | if line[:1] == '.': |
| 523 | line = '.' + line |
| 524 | self.putline(line) |
| 525 | self.putline('.') |
| 526 | return self.getresp() |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 527 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 528 | def quit(self): |
| 529 | """Process a QUIT command and close the socket. Returns: |
| 530 | - resp: server response if successful""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 531 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 532 | resp = self.shortcmd('QUIT') |
| 533 | self.file.close() |
| 534 | self.sock.close() |
| 535 | del self.file, self.sock |
| 536 | return resp |
Guido van Rossum | e2ed9df | 1997-08-26 23:26:18 +0000 | [diff] [blame] | 537 | |
| 538 | |
Guido van Rossum | e2ed9df | 1997-08-26 23:26:18 +0000 | [diff] [blame] | 539 | def _test(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 540 | """Minimal test function.""" |
| 541 | s = NNTP('news', readermode='reader') |
| 542 | resp, count, first, last, name = s.group('comp.lang.python') |
| 543 | print resp |
| 544 | print 'Group', name, 'has', count, 'articles, range', first, 'to', last |
| 545 | resp, subs = s.xhdr('subject', first + '-' + last) |
| 546 | print resp |
| 547 | for item in subs: |
| 548 | print "%7s %s" % item |
| 549 | resp = s.quit() |
| 550 | print resp |
Guido van Rossum | e2ed9df | 1997-08-26 23:26:18 +0000 | [diff] [blame] | 551 | |
| 552 | |
| 553 | # Run the test when run as a script |
| 554 | if __name__ == '__main__': |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 555 | _test() |