X-Git-Url: https://codewiz.org/gitweb?p=geekigeeki.git;a=blobdiff_plain;f=geekigeeki.py;h=651d98db20e984506af833ebf7c810e20a8ae635;hp=f89a844037a4d5428b31d86b89cac4ba8bbf984e;hb=aaa8d1b0c0c584f64ab3f1f08a7b8480c05161d4;hpb=14e7251c799a568ee62e876b3d00ac13b2d1e943 diff --git a/geekigeeki.py b/geekigeeki.py index f89a844..651d98d 100755 --- a/geekigeeki.py +++ b/geekigeeki.py @@ -30,7 +30,7 @@ import cgi, sys, os, re, errno, stat word_re = re.compile(r"^\b((([A-Z][a-z0-9]+){2,}/)*([A-Z][a-z0-9]+){2,})\b$") # FIXME: we accept stuff like foo/../bar and we shouldn't file_re = re.compile(r"^\b([A-Za-z0-9_\-][A-Za-z0-9_\.\-/]*)\b$") -img_re = re.compile(r"^.*\.(png|gif|jpg|jpeg|bmp|ico)$", re.IGNORECASE) +img_re = re.compile(r"^.*\.(png|gif|jpg|jpeg|bmp|ico|ogm|ogg|mkv|mpg|mpeg|mp4|avi|asf|flv|wmv|qt)$", re.IGNORECASE) video_re = re.compile(r"^.*\.(ogm|ogg|mkv|mpg|mpeg|mp4|avi|asf|flv|wmv|qt)$", re.IGNORECASE) url_re = re.compile(r"^[a-z]{3,8}://[^\s'\"]+\S$") link_re = re.compile("(?:\[\[|{{)([^\s\|]+)(?:\s*\|\s*([^\]]+)|)(?:\]\]|}})") @@ -62,8 +62,11 @@ def get_hostname(addr): except Exception: return addr +def is_external_url(pathname): + return (url_re.match(pathname) or pathname.startswith('/')) + def relative_url(pathname, privileged=False): - if not (url_re.match(pathname) or pathname.startswith('/')): + if not is_external_url(pathname): if privileged: url = privileged_path() else: @@ -78,6 +81,13 @@ def permalink(s): def emit_header(mime_type="text/html"): print "Content-type: " + mime_type + "; charset=utf-8\n" +def sendfile(dest_file, src_file): + """Efficiently copy file data between file descriptors""" + while 1: + data = src_file.read(65536) + if not data: break + dest_file.write(data) + def send_guru(msg_text, msg_type): if not msg_text: return print '
'
@@ -87,28 +97,10 @@ def send_guru(msg_text, msg_type):
     if msg_type == 'error':
         print '\n      Guru Meditation #DEADBEEF.ABADC0DE'
     print '
' - # FIXME: This little JS snippet is harder to pass than ACID 3.0 - print """ - """ + try: + sendfile(sys.stdout, open('gurumeditation.js', 'rb')) + except IOError, err: + pass def send_title(name, text="Limbo", msg_text=None, msg_type='error', writable=False): global title_done @@ -187,17 +179,26 @@ def send_httperror(status="403 Not Found", query=""): send_title(None, msg_text=("%s: on query '%s'" % (status, query))) send_footer() -def link_tag(params, text=None, ss_class=None, privileged=False): +def link_tag(params, text=None, link_class=None, privileged=False): if text is None: text = params # default - classattr = '' - if ss_class: - classattr += 'class="%s" ' % ss_class - # Prevent crawlers from following links potentially added by spammers or to generated pages - if ss_class == 'external' or ss_class == 'navlink': - classattr += 'rel="nofollow" ' - elif url_re.match(params): - classattr += 'rel="nofollow" ' + elif img_re.match(text): + text = '' + + if not link_class: + if is_external_url(params): + link_class = 'external' + elif file_re.match(params) and Page(params).exists(): + link_class = 'wikilink' + else: + params = nonexist_pfx + params + link_class = 'nonexistent' + + classattr = 'class="%s" ' % link_class + # Prevent crawlers from following links potentially added by spammers or to generated pages + if link_class == 'external' or link_class == 'navlink': + classattr += 'rel="nofollow"' + return '%s' % (classattr, relative_url(params, privileged=privileged), text) # Search --------------------------------------------------- @@ -221,7 +222,7 @@ def handle_fullsearch(needle): print "