[signups, better wiki markup raph.levien@gmail.com**20061227231956] { hunk ./barghest.py 9 +uname_re_str = r'^[a-zA-Z0-9][a-zA-Z0-9\.\-_]{,19}$' + hunk ./barghest.py 35 + self.urlbase = '' hunk ./barghest.py 67 - self.db = MySQLdb.connect("localhost", "root", "", "junk") + self.db = MySQLdb.connect(getattr(self, 'dbhost', 'localhost'), + getattr(self, 'dbuname', 'root'), + getattr(self, 'dbpass', ''), + getattr(self, 'dbdbname', 'junk')) hunk ./barghest.py 107 +# Convert a blob to a Unicode string +def uniblob(s): + return unicode(s.tostring(), 'utf-8') + hunk ./barghest.py 127 +def randhex(n): + randstr = file('/dev/urandom', 'rb').read(n) + return ''.join(['%x' % (ord(x) & 15) for x in randstr]) + hunk ./barghest.py 151 + 'A.user {text-decoration: none; color: #040}', + 'A.new:hover {text-decoration: underline}', hunk ./barghest.py 164 +def navbar(ctx, items = []): + items.append(wikilink(ctx, '', True)) + items.append(wikilink(ctx, 'RecentChanges', True)) + ctx.write('
[ ' + ' | '.join(items) + ' ]
') + hunk ./barghest.py 171 + if auth_user(ctx) != 'f': + navbar(ctx) + else: + ctx.write( + ['Log in below, or go straight to the wiki.
', + '', + '', + 'If you don\'t have an account and want one, please sign up.
']) + +def serve_signup(ctx, extra = None): + stdpage(ctx, 'Sign up for a new account') hunk ./barghest.py 194 - ['Log in below, or go straight to the wiki.
', - '']) - + '', + '']) + hunk ./barghest.py 218 - sql = """SELECT * FROM foo""" hunk ./barghest.py 235 + navbar(ctx) + +def serve_signupsub(ctx): + uname = ctx.query['u'] + if not re.match(uname_re_str, uname): + serve_signup(ctx, 'Invalid username. The username must be no longer than 20 characters, begin with an alphanumeric, and consist of alphanumerics, period, underscore, or dash. In other words, it must match the regular expression syntax: ' + uname_re_str + '
') + c = ctx.cursor() + c.execute("select realname from users where uname = %s", uname) + response = c.fetchone() + if response: + realname = uniblob(response[0]) + serve_signup(ctx, 'User already exists. The username ' + uname + ' already belongs to ' + htmlquote(realname) + '.
') + passwd = ctx.query['pass'] + if len(passwd) < 4: + serve_signup(ctx, 'Password too short. The password must be at least 4 characters.
') + realname = ctx.query['realname'].strip() + if len(realname) < 1: + serve_signup(ctx, 'Give your real name. You must put include your name.
') + entropy = randhex(52) + cookie = entropy[:40] + salt = entropy[40:] + hash = sha.sha(salt + passwd.encode('utf-8')).hexdigest() + c.execute("insert into users (uname, salt, hash, status, cookie, realname, ctime) values (%s, %s, %s, 'y', %s, %s, now())", + (uname, salt, hash, cookie, realname.encode('utf-8'))) + ctx.header('Set-Cookie', 'id=' + uname + ':' + cookie) + stdpage(ctx, 'Signup successful') + ctx.write("Congratulations! You have created the account " + uname + + ". Next, you'll probably want to edit the wiki page for your account: " + + userlink(ctx, uname) + ".
") + navbar(ctx) hunk ./barghest.py 275 +def userlink(ctx, uname): + c = ctx.cursor() + c.execute('select realname from users where uname = %s', uname) + response = c.fetchone() + if response: + realname = uniblob(response[0]) + return '' + htmlquote(realname) + '' + else: + return '[deleted account ' + uname + ']' + hunk ./barghest.py 312 - data = unicode(response[0].tostring(), 'utf-8') + data = uniblob(response[0]) hunk ./barghest.py 319 +def wikihref(ctx, page): + if re.match('user/', page): + pref = '' + else: + pref = 'wiki/' + return ctx.urlbase + pref + urlquoteplus(page) + hunk ./barghest.py 327 - url_re = re.compile(r'^[a-z\+]*://') - if url_re.match(str): - return '' + htmlquote(str) + '' + url_re = re.compile(r'^([a-z\+]*://\S+)(\s(.+))?') + m = url_re.match(str) + if m: + if m.group(3): + text = m.group(3) + else: + text = m.group(1) + return '' + htmlquote(text) + '' + if re.match('user/', str): + return userlink(ctx, str[5:]) hunk ./barghest.py 342 - str = './' - textstr = 'the home page' + textstr = 'wiki home page' hunk ./barghest.py 345 - return pref + 'href="' + urlquoteplus(str) + '">' + textstr + '' + return pref + 'href="' + wikihref(ctx, str) + '">' + textstr + '' hunk ./barghest.py 350 +class ListState: + def __init__(self): + self.inlist = False + def set(self, inlist, result): + if inlist and not self.inlist: + result.append(''] + if len(par) == 0: continue + if rule_re.match(par): + liststate.set(False, result) + result.append('
'] + pos = 0 hunk ./barghest.py 399 - pos = 0 hunk ./barghest.py 402 + if len(stack): + # Handle closing tags + pos2 = line.find(stack[-1][0], pos) + if pos2 >= 0 and (not m or pos2 <= m.start()): + rline += line[pos:pos2] + stack[-1][1] + pos = pos2 + len(stack[-1][0]) + del stack[-1] + continue hunk ./barghest.py 416 - # I don't like this inline toggling BS - if repls[g][:2] == '': - repls[g] = '<' + repls[g][2:] - elif repls[g][:1] == '<': - repls[g] = '' + repls[g][1:] hunk ./barghest.py 417 - elif g == '[': - endpos = line.find(']', mend) - if endpos >= 0: - rline += wikilink(ctx, line[m.end():endpos]) - pos = endpos + 1 - else: - rline += g - pos = mend - elif g == '#': - endpos = line.find('#', mend) + elif emphs.has_key(g): + rline += '<' + emphs[g] + '>' + stack.append((g, '' + emphs[g] + '>')) + pos = mend + elif g in embeds: + wikiclose, embedfn = embeds[g] + endpos = line.find(wikiclose, mend) + # todo: search successive lines hunk ./barghest.py 426 - rline += wikigh(ctx, line[m.end():endpos]) - pos = endpos + 1 - else: - rline += g - pos = mend + rline += embedfn(ctx, line[m.end():endpos]) + pos = endpos + len(wikiclose) hunk ./barghest.py 432 - else: - rline += g - pos = mend - else: - # This shouldn't happen, but if it does, just pass - # the string verbatim. + if pos < mend: + # Nothing matched above, pass verbatim. hunk ./barghest.py 441 - rpar.append('
') + pos = 0 + if stack: + stack.reverse() + rpar.append(''.join([htmlclose for wikiclose, htmlclose in stack])) + rpar.append('' + rpar[0][1:]) hunk ./barghest.py 447 + liststate.set(False, result) hunk ./barghest.py 458 + navbar(ctx) hunk ./barghest.py 470 - ctx.write(['']) + '', + '']) hunk ./barghest.py 500 - ctx.write('Last edited ' + mtime.isoformat(' ') + ' by ' + htmlquote(uname) + '
') + ctx.write('Last edited ' + mtime.isoformat(' ') + ' by ' + userlink(ctx, uname) + '
') hunk ./barghest.py 525 + if extra != None: ctx.write(extra) hunk ./barghest.py 528 - ctx.write('') + items = [] + if auth_user(ctx) == 'y': + items.append('Edit') + navbar(ctx, items) + +def serve_wiki(ctx): + if len(ctx.path_l) == 1: + page = urllib.unquote_plus(ctx.path_l[0]) + else: + page = '' + + if page == 'RecentChanges': + return serve_wiki_recent(ctx) + + serve_wiki_page(ctx, page) + +def serve_user(ctx): + if len(ctx.path_l) == 1 and re.match(uname_re_str, ctx.path_l[0]): + uname = ctx.path_l[0] + c = ctx.cursor() + c.execute('select realname from users where uname = %s', uname) + response = c.fetchone() + if response: + realname = uniblob(response[0]) + extra = ['Information for ' + htmlquote(realname) + + ' (username ' + uname + ').
'] + c.execute('select what, mtime from log where uname = %s order by seqno desc limit 1', uname) + response = c.fetchone() + if response: + page, mtime = response + page = unicode(page, 'utf-8') + extra.append('Last page edited: ' + wikilink(ctx, page) + + ' on ' + mtime.isoformat(' ') + '.
') + extra.append('