コード例 #1
0
def newsgroup(G='', F='', C='', A=None, P=None, RESPONSE=None):
    """The article list for group G in framestyle F"""
    if G=='': return "Missing newsgroup name."
    group = G
    showframes = 1
    show_articles = default_show_articles
    if os.environ.has_key('HTTP_USER_AGENT'):
        browser = os.environ['HTTP_USER_AGENT']
    else:
        browser = "unknown"
    if string.find(browser, "Mozilla/") == 0:
        browser_version = string.atof(browser[8:string.index(browser, ' ')])
        if browser_version >= 2.00:
            showframes = 3
    if F != '':
	try:
	    showframes = string.atoi(F)
	except AttributeError:
	    showframes = 0
    if C != '':
        try:
            show_articles = string.atoi(C)
        except AttributeError:
            show_articles = default_show_articles

    user = A
    acc_string = ''
    if A: acc_string = '&A=' + A
    password = P
    pass_string = ''
    if P: pass_string = '&P=' + P
    
    lines = []
    RESPONSE.headers['expires'] = time.asctime(time.gmtime(time.time() + 60))
    RESPONSE.write( """<HTML><HEAD><TITLE>Tokyo PC Users Group: %s</TITLE></HEAD>""" % group)
    try:
        try:
	    news = NNTP(NEWS_SERVER)
        except:
	    RESPONSE.write( "<BODY><B>Can not connect to server:</B> ", NEWS_SERVER)
	    raise NewsError

        try:
	    resp = news.shortcmd('MODE READER')
        except:
	    RESPONSE.write( "<BODY><B>Can not communicate with server:</B> ", NEWS_SERVER)
	    raise NewsError

        if user:
            resp = news.shortcmd('authinfo user '+user)
            if resp[:3] == '381':
                if not password:
		    RESPONSE.write( "<BODY><B>Can not fetch newsgroup</B>")
		    raise NewsError
                else:
                    resp = news.shortcmd('authinfo pass '+password)
                    if resp[:3] != '281':
		        RESPONSE.write( "<BODY><B>Can not fetch newsgroup</B>")
			raise NewsError

        try:
	    resp, count, first, last, name = news.group(group)
        except:
            RESPONSE.write( "<BODY><B>No such newsgroup:</B> " + group )
            raise NewsError

	description = ""
	try:
	    resp, lines = news.xgtitle(group)
	except:
	    pass
	else:
	    for line in lines:
		name, description = line

	if showframes == 0:
	    RESPONSE.write( '<BODY BGCOLOR="#FFFFFF"><H1>%s</H1>' % group)
	    RESPONSE.write( "<EM>%s</EM><P>" % cgi.escape(description))
	elif showframes == 1 or showframes == 3:
	    if description:	description = "&D="+quote(description)
	    RESPONSE.write( '<FRAMESET ROWS="33%,*">')
	    RESPONSE.write( '  <FRAMESET COLS="220,*">')
	    RESPONSE.write( '    <FRAME SRC="/cgi-bin/webnews/logo?G=%s%s" scrolling="auto">' % (group, description))
	    RESPONSE.write( '    <FRAME SRC="/cgi-bin/webnews/newsgroup?G=%s&F=2%s%s#last" scrolling="yes"> ' % (group, acc_string, pass_string))
	    RESPONSE.write( '  </FRAMESET>')
            if string.find(G, "ttalk") >= 0:
                RESPONSE.write( '  <FRAME SRC="http://ttalk.soholutions.com/welcome.html" scrolling="auto" name="d">')
            else:
                RESPONSE.write( '  <FRAME SRC="/webnews/welcome.html" scrolling="auto" name="d">')
	    RESPONSE.write( '</FRAMESET><BODY BGCOLOR="#FFFFFF">')
	else:
	    RESPONSE.write( '<BODY BGCOLOR="#FFFFFF">')

	if showframes == 3:
	    raise NewsError

        if (show_articles > 0):
            ilast = string.atoi(last)
            ifirst = string.atoi(first)
            if ((ilast - ifirst + 1) > show_articles):
                first = "%d" % (ilast - show_articles + 1)
                RESPONSE.write( '<A HREF="/cgi-bin/webnews/newsgroup?G=%s&F=%d&C=0%s%s"><I>Retrieve earlier article headers</I></A> ' % (group, showframes, acc_string, pass_string))

	try:
	    resp, lines = news.xover(first, last)
	except:
	    RESPONSE.write( "<B>Unable to get article list for:</B> " + group)
	    raise NewsError

	RESPONSE.write( '<UL TYPE="none">')

	# pass 1: build a dictionary of message IDs
	ids = {}
	index = 0
	for line in lines:
	    art_nr, subject, poster, date, id, references, size, line_cnt = line
	    ids[id] = index
	    index = index + 1

	# pass 2: discover child articles
	childof = []
	subs = {}
#	subject_re_less = regex.symcomp("\([Rr]e:\)? *\(<real_subject>.*\)")
        subject_re_less = re.compile(r"(re:)?\s*(?P<real_subject>.*)")
	index = 0
	for line in lines:
	    art_nr, subject, poster, date, id, references, size, line_cnt = line
	    childof.append(-1)
#	    if subject_re_less.match(subject) > 0:
#		subject = subject_re_less.group('real_subject')
	    srl = subject_re_less.match(subject)
	    if srl: subject = srl.group('real_subject')
	    # if there are references, use them (most recent first)
	    if len(references) > 0:
		references.reverse()
		for ref in references:
		    if ids.has_key(ref):
			childof[index] = ids[ref]
			break
	    # if no references (or referee not found), use subject
	    if childof[index] == -1:
		if subs.has_key(subject) :
		    childof[index] = subs[subject]
		else:
		    subs[subject] = index
	    index = index + 1

#   index = 0
#   for line in lines:
#	art_nr, subject, poster, date, id, size, line_cnt, references = line
#	print index,childof[index],subject
#	index = index + 1

        index = 0
	for seq in childof:
	    if seq == -1:
		show_article_and_kids(index, 0, lines, childof,
				      acc_string, pass_string, RESPONSE)
	    index = index + 1

#	art_nr, subject, poster, date, id, size, line_cnt, references = line
#	name, email = parseaddr(poster)
#	print '<LI><A HREF="http:/cgi-bin/readnews.cgi?%s" TARGET="d">%s</A> (%s)  %s, %s' % (quote(id), subject, line_cnt, name, time.strftime('%b %d, %H:%M', parsedate(date)))

	RESPONSE.write('<A NAME="last">&nbsp</A></UL>')

    finally:
        if showframes != 2:
            if string.find(G, "ttalk") >= 0:
                RESPONSE.write( """<P><HR><P>A service of the
                <A HREF="http://www.soholutions.com/">SoHolutions</A>.""")
            else:
                RESPONSE.write( """<P><HR><P>A service of the
                <A HREF="http://www.tpc.ml.org/">Tokyo PC Users Group</A>.""")
        # print "<P><ADDRESS>",os.environ['HTTP_USER_AGENT'],"</ADDRESS>"
        RESPONSE.write( """</BODY></HTML>""")
コード例 #2
0
def readnews(I="", A=None, P=None, RESPONSE=None):
    """Display article in HTML"""
    article = I
    user = A 
    password = P

    RESPONSE.write("""<HTML><HEAD><TITLE>Tokyo PC Users Group</TITLE></HEAD>
             <BODY BGCOLOR="#FFFFFF">""")

    try:
        news = NNTP(NEWS_SERVER)
    except:
        RESPONSE.write("Can not connect to server: " + NEWS_SERVER)
    resp = news.shortcmd('MODE READER')

    if user:
        resp = news.shortcmd('authinfo user '+user)
        if resp[:3] == '381':
            if not password:
	        RESPONSE.write("<B>Can not fetch article</B><P>")
            else:
                resp = news.shortcmd('authinfo pass '+password)
                if resp[:3] != '281':
	            RESPONSE.write("<B>Can not fetch article</B><P>")

    try:
        resp, nr, id, subs = news.head(article)
    except:
        RESPONSE.write("Article %s not available" % quote(article))

    RESPONSE.write('<TABLE WIDTH="100%" BGCOLOR="#CFCFCF"><TR><TD>')

    # build up the header (so we know Subject: by Newsgroups: output time)
    from_line = ""
    newsgroup_line = ""
    subject_line = ""
    keep_lines = ""
    mail_subject = ""
    for line in subs:
        ihdr = interesting_headers.match(line)
        if ihdr:
            if ihdr.group('from'):
	        name, email = parseaddr(line[6:])
	        if name:
		    from_line = 'From: <A HREF="mailto:%s%s">%s</A> &lt;%s&gt;<BR>' % (
		        email, "%s", name, email)
	        else:
		    from_line = 'From: <A HREF="mailto:%s%s">%s</A><BR>' % (
		        email, "%s", email)
	    elif ihdr.group('newsgroups'):
	        newsgroup_line = 'Newsgroups: <A HREF="mailto:tpc-%[email protected]%s">tpc.%s</A>%s<BR>' % (
	        ihdr.group('group'), "%s",
		ihdr.group('group'), ihdr.group('othergroups'))
	    elif ihdr.group('subject'):
	        subject_line = 'Subject: <B>%s</B><BR>' % line[9:]
	        if ihdr.group('re'):
		    mail_subject = "?subject="+line[9:]
	        else:
		    mail_subject = "?subject=Re: "+line[9:]
	    elif ihdr.group('keep'):
	        keep_lines = keep_lines+line+"<BR>"

    if from_line:
        RESPONSE.write(from_line % mail_subject)
    if newsgroup_line:
        RESPONSE.write(newsgroup_line % mail_subject)
    RESPONSE.write(subject_line + keep_lines)

    RESPONSE.write('</TD></TR></TABLE><P>')

    try:
        resp, nr, id, subs = news.body(article)
    except:
        RESPONSE.write("Article %s body not available" % article)

    RESPONSE.write("<CODE>")
    for line in subs:
        RESPONSE.write(re.sub(r'''(?i)(?x)
           (?P<opening>[<(";]?)
           (?P<url>(((?P<web>http:)|(news:)|(mailto:)|(telnet:))(?P<uri>\S*?))
               # a mail address is some non-ws characters followed by @
               # followed by a domain name that has at least one . in it
              |(?P<mailadr>\S+@(\S+\.)+\S+?))
           # either a URL or a mail address will not contain [)">\s]
           # and will not end with punctuation just before the whitespace
           (?P<closing>([)"'>\s]|$|([&.?,:;]\s)+))''',
	    liven_url, line) + "<BR>")
    RESPONSE.write("</CODE>")

    RESPONSE.write("</BODY></HTML>")
    resp = news.quit()