"""RSS CGI methods.
"""
#
# Copyright (c) 2005,2006 shinGETsu Project.
# Copyright (c) 2006 Mutsu.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#
import re
import cgi
import csv
from time import time
import config
import gateway
from cache import *
from rss import RSS, make_rss1, make_rss2
__version__ = "$Revision: 1266 $"
class CGI(gateway.CGI):
"""Class for /rss.cgi."""
appli_type = "rss"
def run(self):
path = self.path_info()
if config.server_name:
self.host = config.server_name
else:
self.host = self.environ.get('HTTP_HOST', 'localhost')
if not self.check_visitor():
self.print403()
return
found = re.search(r'^([^/]+)/?$', path)
if found:
path = found.group(1)
self.print_rss(path)
return
found = re.search(r'^([^/]+)/([0-9a-f]{8})$', path)
if found:
path = found.group(1)
self.print_rss(path)
return
found = re.search(r'^([^/]+)/p([0-9]+)$', path)
if found:
path = found.group(1)
self.print_rss(path)
return
if path == '':
self.print_rss()
return
self.print404()
def rss_text_format(self, plain):
buf = plain.replace("
", " ")
buf = buf.replace("&", "&")
buf = re.sub(r'&(#\d+|lt|gt|amp);', r'&\1;', buf)
buf = buf.replace("<", "<")
buf = buf.replace(">", ">")
buf = buf.replace("\r", "")
buf = buf.replace("\n", "")
return buf
def rss_html_format(self, plain, appli, path):
title = self.str_decode(path)
buf = self.html_format(plain, appli, title, absuri=True)
if buf:
buf = '
%s
' % buf return buf #+ self.rss_cgi + self.sep + path, def print_rss(self, path=""): if path: rss = RSS(encode = "UTF-8", title = self.str_decode(path), parent = "http://" + self.host, uri = "http://" + self.host + self.rss_cgi + self.sep + path, description = self.str_decode(path) + ": " + self.message["description"], xsl = config.xsl) else: rss = RSS(encode = "UTF-8", title = self.message["logo"], parent = "http://" + self.host, uri = "http://" + self.host + self.rss_cgi, description = self.message["description"], xsl = config.xsl) if path: cachelist = [] c = Cache(self.file_encode('thread', path)) cachelist.append(c) #cachelist = [self.file_encode('thread', path)] else: cachelist = CacheList() now = int(time()) for cache in cachelist: if cache.stamp + config.rss_range2 >= now: title = self.escape(self.file_decode(cache.datfile)) path = self.appli[cache.type]+self.sep+self.str_encode(title) for r in cache: if r.stamp + config.rss_range2 < now: continue r.load_body() name = r.get("name", self.message["anonymous"]) desc = self.rss_text_format(r.get("body", "")) content = self.rss_html_format(r.get("body", ""), self.appli[cache.type], title) attach = r.get('attach', '') if attach: suffix = r.get('suffix', '') if not re.search(r'^[0-9A-Za-z]+$', suffix): suffix = txt content += '\n' + \ '%d.%s' % \ (self.host, self.appli[cache.type], self.sep, cache.datfile, r.id, r.stamp, suffix, r.stamp, suffix) + \ '
' if cache.type == "thread": permapath = "%s/%s" % (path[1:], r.id[:8]) else: permapath = path[1:] rss.append(permapath, date = r.stamp, title = name + ': ' + title, description = desc, content = content) self.stdout.write("Content-Type: text/xml; charset=UTF-8\n") try: self.stdout.write("Last-Modified: %s\n" % self.rfc822_time(rss[rss.keys()[0]].date)) except IndexError, KeyError: pass self.stdout.write("\n") if config.rss == "1": self.stdout.write(make_rss1(rss)) else: self.stdout.write(make_rss2(rss)) # End of CGI