2004-07-07 18:15:17 +00:00
|
|
|
# -*- coding: iso-8859-1 -*-
|
2009-01-08 14:18:03 +00:00
|
|
|
# Copyright (C) 2000-2009 Bastian Kleineidam
|
2004-07-07 18:15:17 +00:00
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
|
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2005-01-19 15:08:02 +00:00
|
|
|
"""
|
|
|
|
|
Handle nntp: and news: links.
|
|
|
|
|
"""
|
2004-07-07 18:15:17 +00:00
|
|
|
|
2004-07-22 10:54:47 +00:00
|
|
|
import re
|
|
|
|
|
import time
|
|
|
|
|
import sys
|
|
|
|
|
import nntplib
|
|
|
|
|
import random
|
2004-08-16 19:20:53 +00:00
|
|
|
|
2008-05-09 06:16:03 +00:00
|
|
|
from . import urlbase
|
|
|
|
|
from .. import LinkCheckerError
|
|
|
|
|
from .const import WARN_NNTP_NO_SERVER, WARN_NNTP_NO_NEWSGROUP, WARN_NNTP_BUSY
|
2004-08-16 19:20:53 +00:00
|
|
|
|
2004-07-22 10:54:47 +00:00
|
|
|
random.seed()
|
2004-07-07 18:15:17 +00:00
|
|
|
|
2004-08-16 19:20:53 +00:00
|
|
|
class NntpUrl (urlbase.UrlBase):
|
2005-01-19 01:04:38 +00:00
|
|
|
"""
|
|
|
|
|
Url link with NNTP scheme.
|
|
|
|
|
"""
|
2004-07-07 18:15:17 +00:00
|
|
|
|
2004-08-16 19:20:53 +00:00
|
|
|
def check_connection (self):
|
2005-02-08 12:10:39 +00:00
|
|
|
"""
|
|
|
|
|
Connect to NNTP server and try to request the URL article
|
|
|
|
|
resource (if specified).
|
|
|
|
|
"""
|
2006-05-13 13:44:52 +00:00
|
|
|
nntpserver = self.host or self.aggregate.config["nntpserver"]
|
2004-07-07 18:15:17 +00:00
|
|
|
if not nntpserver:
|
2004-11-19 11:18:06 +00:00
|
|
|
self.add_warning(
|
2005-07-13 15:03:17 +00:00
|
|
|
_("No NNTP server was specified, skipping this URL."),
|
2007-11-29 07:50:22 +00:00
|
|
|
tag=WARN_NNTP_NO_SERVER)
|
2004-07-07 18:15:17 +00:00
|
|
|
return
|
2005-02-08 12:10:39 +00:00
|
|
|
nntp = self._connect_nntp(nntpserver)
|
2004-07-07 18:15:17 +00:00
|
|
|
group = self.urlparts[2]
|
2004-08-16 19:20:53 +00:00
|
|
|
while group[:1] == '/':
|
2004-07-07 18:15:17 +00:00
|
|
|
group = group[1:]
|
|
|
|
|
if '@' in group:
|
2008-05-22 12:10:08 +00:00
|
|
|
# request article info (resp, number mid)
|
|
|
|
|
number = nntp.stat("<"+group+">")[1]
|
2008-05-27 19:44:40 +00:00
|
|
|
self.add_info(_('Article number %(num)s found.') % {"num": number})
|
2004-07-07 18:15:17 +00:00
|
|
|
else:
|
|
|
|
|
# split off trailing articel span
|
2004-08-16 19:20:53 +00:00
|
|
|
group = group.split('/', 1)[0]
|
2004-07-07 18:15:17 +00:00
|
|
|
if group:
|
2008-05-22 12:10:08 +00:00
|
|
|
# request group info (resp, count, first, last, name)
|
|
|
|
|
name = nntp.group(group)[4]
|
2008-05-27 19:44:40 +00:00
|
|
|
self.add_info(_("News group %(name)s found.") % {"name": name})
|
2004-07-07 18:15:17 +00:00
|
|
|
else:
|
|
|
|
|
# group name is the empty string
|
2005-07-13 15:03:17 +00:00
|
|
|
self.add_warning(_("No newsgroup specified in NNTP URL."),
|
2007-11-29 07:50:22 +00:00
|
|
|
tag=WARN_NNTP_NO_NEWSGROUP)
|
2004-07-07 18:15:17 +00:00
|
|
|
|
2005-02-08 12:10:39 +00:00
|
|
|
def _connect_nntp (self, nntpserver):
|
2005-01-19 01:04:38 +00:00
|
|
|
"""
|
|
|
|
|
This is done only once per checking task. Also, the newly
|
2004-07-07 18:15:17 +00:00
|
|
|
introduced error codes 504 and 505 (both inclining "Too busy, retry
|
2005-01-19 01:04:38 +00:00
|
|
|
later", are caught.
|
|
|
|
|
"""
|
2004-07-07 18:15:17 +00:00
|
|
|
tries = 0
|
|
|
|
|
nntp = value = None
|
|
|
|
|
while tries < 5:
|
|
|
|
|
tries += 1
|
|
|
|
|
try:
|
2005-01-24 20:29:55 +00:00
|
|
|
nntp = nntplib.NNTP(nntpserver, usenetrc=False)
|
2004-07-07 18:15:17 +00:00
|
|
|
except nntplib.error_perm:
|
|
|
|
|
value = sys.exc_info()[1]
|
|
|
|
|
if re.compile("^50[45]").search(str(value)):
|
2004-08-16 19:20:53 +00:00
|
|
|
time.sleep(random.randrange(10, 30))
|
2004-07-07 18:15:17 +00:00
|
|
|
else:
|
|
|
|
|
raise
|
|
|
|
|
if nntp is None:
|
2008-05-09 06:16:03 +00:00
|
|
|
raise LinkCheckerError(
|
2007-11-29 07:50:22 +00:00
|
|
|
_("NNTP server too busy; tried more than %d times.") % tries)
|
2004-07-07 18:15:17 +00:00
|
|
|
if value is not None:
|
2008-05-27 19:44:40 +00:00
|
|
|
self.add_warning(_("NNTP busy: %(msg)s.") % {"msg": str(value)},
|
2007-11-29 07:50:22 +00:00
|
|
|
tag=WARN_NNTP_BUSY)
|
2004-07-07 18:15:17 +00:00
|
|
|
return nntp
|
|
|
|
|
|
2004-08-16 19:20:53 +00:00
|
|
|
def can_get_content (self):
|
2005-02-08 12:10:39 +00:00
|
|
|
"""
|
|
|
|
|
NNTP urls have no content.
|
|
|
|
|
|
|
|
|
|
@return: False
|
|
|
|
|
@rtype: bool
|
|
|
|
|
"""
|
2004-07-07 18:15:17 +00:00
|
|
|
return False
|