def get_board(url): m = re.search(r'/2ch_([^/]+)/', url) if not m: return '' board = title.file_decode('dummy_' + m.group(1)) return board
def make_subject(env, board): load_from_net = check_get_cache(env) subjects = [] cachelist = make_subject_cachelist(board) last_stamp = 0 for c in cachelist: if not load_from_net and len(c) == 0: # Because you don't have a permission of getting data from network, # don't need to look a thread that don't have records. continue if last_stamp < c.stamp: last_stamp = c.stamp try: key = keylib.get_datkey(c.datfile) except keylib.DatkeyNotFound: continue title_str = title.file_decode(c.datfile) if title_str is not None: title_str = title_str.replace('\n', '') subjects.append('{key}.dat<>{title} ({num})\n'.format( key=key, title=title_str, num=len(c))) return subjects, last_stamp
def subject_app(env, resp): # utils.log('subject_app') path = env['PATH_INFO'] board = subject_re.match(path).group(1) cachelist = cache.CacheList() cachelist.sort(key=lambda x: x.valid_stamp, reverse=True) subjects = [] last_stamp = 0 for c in cachelist: c.load() if len(c) == 0: continue if last_stamp < c.stamp: last_stamp = c.stamp subjects.append('{key}.dat<>{title} ({num})\n'.format( key=keylib.get_datkey(c.datfile), title=title.file_decode(c.datfile), num=len(c))) resp('200 OK', [('Content-Type', 'text/plain; charset=Shift_JIS'), ('Last-Modified', eutils.formatdate(last_stamp))]) return (s.encode('sjis', 'ignore') for s in subjects)
def make_dat(cache, env, board): dat = [] table = ResTable(cache) for i, k in enumerate(cache.keys()): rec = cache[k] rec.load() name = rec.get('name') if not name: name = '名無しさん' if rec.get('pubkey'): # 2ch trip name += '◆' + rec.get('pubkey')[:10] # 10 is 2ch trip length comment = '{name}<>{mail}<>{date}<>{body}<>'.format( name=name, mail=rec.get('mail', ''), date=_datestr_2ch(rec.get('stamp', 0)), body=_make_body(rec, env, board, table)) if i == 0: # dat title comment += title.file_decode(cache.datfile) comment += '\n' dat.append(comment) rec.free() return dat
def make_dat(cache, env, board): dat = [] table = ResTable(cache) for i, k in enumerate(cache.keys()): rec = cache[k] rec.load() name = rec.get('name') if not name: name = '名無しさん' if rec.get('pubkey'): # 2ch trip name += '◆' + rec.get('pubkey')[:10] # 10 is 2ch trip length comment = '{name}<>{mail}<>{date}<>{body}<>'.format( name=name, mail=rec.get('mail', ''), date=_datestr_2ch(rec.get('stamp', 0)), body=_make_body(rec, env, board, table) ) if i == 0: # dat title comment += title.file_decode(cache.datfile) comment += '\n' dat.append(comment) rec.free() return dat
def post_comment(env, thread_key, name, mail, body, passwd, tag=None): """Post article.""" if config.server_name: dat_host = config.server_name else: host = env['HTTP_HOST'] dat_host = re.sub(r':\d+', ':' + str(config.dat_port), host) p = re.compile(r'https?://' + dat_host + '/test/read.cgi/2ch(?:_[0-9A-Z]+)?/([0-9]+)/') for x in p.finditer(body): try: file = keylib.get_filekey(x.group(1)) body = body.replace(x.group(0), '[[' + title.file_decode(file) + ']]') except keylib.DatkeyNotFound: pass stamp = int(time.time()) recbody = {} if body != '': recbody['body'] = gateway.CGI.escape(None, body) if name != '': recbody['name'] = gateway.CGI.escape(None, name) if mail != '': recbody['mail'] = gateway.CGI.escape(None, mail) c = cache.Cache(thread_key) rec = cache.Record(datfile=c.datfile) id = rec.build(stamp, recbody, passwd=passwd) if spam.check(rec.recstr): raise SpamError() # utils.log('post %s/%d_%s' % (c.datfile, stamp, id)) c.add_data(rec) c.sync_status() if tag: utils.save_tag(c, tag) queue = updatequeue.UpdateQueue() queue.append(c.datfile, stamp, id, None) queue.start()
def post_comment(env, thread_key, name, mail, body, passwd, tag=None): """Post article.""" if config.server_name: dat_host = config.server_name else: host = env['HTTP_HOST'] dat_host = re.sub(r':\d+', ':' + str(config.dat_port), host) p = re.compile(r'https?://' + dat_host + '/test/read.cgi/2ch(?:_[0-9A-Z]+)?/([0-9]+)/') for x in p.finditer(body): try: file = keylib.get_filekey(x.group(1)) body = body.replace(x.group(0),'[[' + title.file_decode(file) + ']]') except keylib.DatkeyNotFound: pass stamp = int(time.time()) recbody = {} if body != '': recbody['body'] = gateway.CGI.escape(None, body) if name != '': recbody['name'] = gateway.CGI.escape(None, name) if mail != '': recbody['mail'] = gateway.CGI.escape(None, mail) c = cache.Cache(thread_key) rec = cache.Record(datfile=c.datfile) id = rec.build(stamp, recbody, passwd=passwd) if spam.check(rec.recstr): raise SpamError() # utils.log('post %s/%d_%s' % (c.datfile, stamp, id)) c.add_data(rec) c.sync_status() if tag: utils.save_tag(c, tag) queue = updatequeue.UpdateQueue() queue.append(c.datfile, stamp, id, None) queue.start()
def subject_app(env, resp): # utils.log('subject_app') path = env['PATH_INFO'] # board is `title.file_encode`ed # example: 2ch_E99B91E8AB87(雑談) board = env['mch.path_match'].group(1) m = re.match('2ch_(\S+)', board) if not (board.startswith('2ch') or m): resp("404 Not Found", [('Content-Type', 'text/plain')]) return [b'404 Not Found'] board_encoded = m and title.str_decode(m.group(1)) if board_encoded: # such as '雑談', 'ニュース', etc... board_name = title.file_decode('dummy_' + board_encoded) else: board_name = None subjects, last_stamp = make_subject(env, board_name) resp('200 OK', [('Content-Type', 'text/plain; charset=Shift_JIS'), ('Last-Modified', eutils.formatdate(last_stamp))]) return (s.encode('cp932', 'replace') for s in subjects)
def post_comment_app(env, resp): # print('post', env) if env['REQUEST_METHOD'] != 'POST': resp("404 Not Found", [('Content-Type', 'text/plain')]) return [b'404 Not Found'] # utils.log('post_comment_app') subject, name, mail, body, datkey = _get_comment_data(env) info = { 'host': env.get('REMOTE_ADDR', ''), 'name': name, 'mail': mail, 'body': body } if body == '': return error_resp('本文がありません.', resp, **info) if subject: key = title.file_encode('thread', subject) else: key = keylib.get_filekey(datkey) has_auth = env.get('shingetsu.isadmin', False) or env.get( 'shingetsu.isfriend', False) referer = env.get('HTTP_REFERER', '') m = re.search(r'/2ch_([^/]+)/', referer) tag = None if m and has_auth: tag = title.file_decode('dummy_' + m.group(1)) if cache.Cache(key).exists(): pass elif has_auth: pass elif subject: return error_resp('掲示版を作る権限がありません', resp, **info) else: return error_resp('掲示版がありません', resp, **info) if (not subject and not key): return error_resp('フォームが変です.', resp, **info) table = dat.ResTable(cache.Cache(key)) def replace(match): no = int(match.group(1)) return '>>' + table[no] # replace number anchor to id anchor body = re.sub(r'>>([1-9][0-9]*)', replace, body) # before escape '>>' if name.find('#') < 0: passwd = '' else: name, passwd = name.split('#', 1) if (passwd and not env['shingetsu.isadmin']): return error_resp('自ノード以外で署名機能は使えません', resp, **info) try: post_comment(env, key, name, mail, body, passwd, tag) except SpamError: return error_resp('スパムとみなされました', resp, **info) resp('200 OK', [('Content-Type', 'text/html; charset=Shift_JIS')]) return [success_msg.encode('cp932', 'replace')]
def post_comment_app(env, resp): # print('post', env) if env['REQUEST_METHOD'] != 'POST': resp("404 Not Found", [('Content-Type', 'text/plain')]) return [b'404 Not Found'] # utils.log('post_comment_app') subject, name, mail, body, datkey = _get_comment_data(env) info = {'host': env.get('REMOTE_ADDR', ''), 'name': name, 'mail': mail, 'body': body} if body == '': return error_resp('本文がありません.', resp, **info) if subject: key = title.file_encode('thread', subject) else: key = keylib.get_filekey(datkey) has_auth = env.get('shingetsu.isadmin', False) or env.get('shingetsu.isfriend', False) referer = env.get('HTTP_REFERER', '') m = re.search(r'/2ch_([^/]+)/', referer) tag = None if m and has_auth: tag = title.file_decode('dummy_' + m.group(1)) if cache.Cache(key).exists(): pass elif has_auth: pass elif subject: return error_resp('掲示版を作る権限がありません', resp, **info) else: return error_resp('掲示版がありません', resp, **info) if (not subject and not key): return error_resp('フォームが変です.', resp, **info) table = dat.ResTable(cache.Cache(key)) def replace(match): no = int(match.group(1)) return '>>' + table[no] # replace number anchor to id anchor body = re.sub(r'>>([1-9][0-9]*)', replace, body) # before escape '>>' if name.find('#') < 0: passwd = '' else: name, passwd = name.split('#', 1) if (passwd and not env['shingetsu.isadmin']): return error_resp('自ノード以外で署名機能は使えません', resp, **info) try: post_comment(env, key, name, mail, body, passwd, tag) except SpamError: return error_resp('スパムとみなされました', resp, **info) resp('200 OK', [('Content-Type', 'text/html; charset=Shift_JIS')]) return [success_msg.encode('cp932', 'replace')]