<commit_before>import os
import zmq
import json
from tornado import web, ioloop, httpserver, escape

import gpg
import ssss
import webcfg
import bitcoin

INVALID = json.dumps({'error': 'Invalid data'})
MISSING_EMAIL = json.dumps({'error': 'Email missing'})

MAX_FIELD_LEN = 128
MAX_NM = 64

class IndexHandler(web.RequestHandler):

    def initialize(self, sock):
        self.sock = sock

    def get(self):
        return self.render(os.path.join(webcfg.html_path, "index.html"))

    def post(self):
        # Validate input.
        try:
            data = escape.json_decode(self.request.body)
        except Exception, e:
            print "POST failed: %s (%s)" % (e, self.request.body)
            self.write(INVALID)
            return
        try:
            n = int(data['n'])
            m = int(data['m'])
            if n < 2 or m < n or m > MAX_NM or n > MAX_NM:
                raise Exception("Invalid values for n,m")
        except Exception, e:
            print "POST failed: %s (%s, %s)" % (e, n, m)
            self.write(INVALID)
            return
        if 'email' not in data or '' in data['email']:
            self.write(MISSING_EMAIL)
            return
        for item in data['email']:
            if not isinstance(item, list) or len(item) != 3:
                self.write(INVALID)
                return
            if not item[0] or not item[1] or not isinstance(item[2], bool):
                self.write(MISSING_EMAIL)
                return
            try:
                item[0] = item[0].encode('ascii')
                item[1] = item[1].encode('ascii')
                if (len(item[0]) > MAX_FIELD_LEN or
                        len(item[1]) > MAX_FIELD_LEN):
                    raise Exception("field too long")
            except Exception, e:
                print "Invalid email: %s" % e
                self.write(INVALID)
                return
        note = data.get('note', u'').encode('utf8')
        if len(note) > MAX_FIELD_LEN:
            self.write(INVALID)
            return

        # Test GPG.
        using_gpg = False
        for item in data['email']:
            recipient, use_gpg = item[1], item[2]
            if not use_gpg:
                continue
            using_gpg = True
            _, failed = gpg.encrypt('test', recipient)
            if failed:
                reply = {'error': 'Failed to obtain public for key %s' %
                        recipient}
                self.write(json.dumps(reply))
                return
        if using_gpg:
            gpg_note = ('If GPG fails for whatever reason, one or more emails '
                    'will be sent in plain text.')
        else:
            gpg_note = ''

        # Generate a new private key, and a bitcoin address from it.
        pk, wif_pk = bitcoin.privatekey()
        addr = bitcoin.address(pk)
        # Split the private key in m parts.
        shares = ssss.split(wif_pk, n, m)
        # Send the shares by email.
        for share, email in zip(shares, data['email']):
            self.sock.send_multipart([note, share, addr,
                email[0], email[1], str(int(email[2]))])

        self.write(json.dumps({'success': addr, 'note': gpg_note}))

class GPGHandler(web.RequestHandler):
    def post(self):
        print self.request.files, "<<"
        if 'pubkey' not in self.request.files:
            # Invalid POST
            self.write(INVALID)
            return
        data = self.request.files['pubkey'][0]
        body = data['body'].strip()<commit_msg>dummy print<commit_after>import os
import zmq
import json
from tornado import web, ioloop, httpserver, escape

import gpg
import ssss
import webcfg
import bitcoin

INVALID = json.dumps({'error': 'Invalid data'})
MISSING_EMAIL = json.dumps({'error': 'Email missing'})

MAX_FIELD_LEN = 128
MAX_NM = 64

class IndexHandler(web.RequestHandler):

    def initialize(self, sock):
        self.sock = sock

    def get(self):
        return self.render(os.path.join(webcfg.html_path, "index.html"))

    def post(self):
        # Validate input.
        try:
            data = escape.json_decode(self.request.body)
        except Exception, e:
            print "POST failed: %s (%s)" % (e, self.request.body)
            self.write(INVALID)
            return
        try:
            n = int(data['n'])
            m = int(data['m'])
            if n < 2 or m < n or m > MAX_NM or n > MAX_NM:
                raise Exception("Invalid values for n,m")
        except Exception, e:
            print "POST failed: %s (%s, %s)" % (e, n, m)
            self.write(INVALID)
            return
        if 'email' not in data or '' in data['email']:
            self.write(MISSING_EMAIL)
            return
        for item in data['email']:
            if not isinstance(item, list) or len(item) != 3:
                self.write(INVALID)
                return
            if not item[0] or not item[1] or not isinstance(item[2], bool):
                self.write(MISSING_EMAIL)
                return
            try:
                item[0] = item[0].encode('ascii')
                item[1] = item[1].encode('ascii')
                if (len(item[0]) > MAX_FIELD_LEN or
                        len(item[1]) > MAX_FIELD_LEN):
                    raise Exception("field too long")
            except Exception, e:
                print "Invalid email: %s" % e
                self.write(INVALID)
                return
        note = data.get('note', u'').encode('utf8')
        if len(note) > MAX_FIELD_LEN:
            self.write(INVALID)
            return

        # Test GPG.
        using_gpg = False
        for item in data['email']:
            recipient, use_gpg = item[1], item[2]
            if not use_gpg:
                continue
            using_gpg = True
            _, failed = gpg.encrypt('test', recipient)
            if failed:
                reply = {'error': 'Failed to obtain public for key %s' %
                        recipient}
                self.write(json.dumps(reply))
                return
        if using_gpg:
            gpg_note = ('If GPG fails for whatever reason, one or more emails '
                    'will be sent in plain text.')
        else:
            gpg_note = ''

        # Generate a new private key, and a bitcoin address from it.
        pk, wif_pk = bitcoin.privatekey()
        addr = bitcoin.address(pk)
        # Split the private key in m parts.
        shares = ssss.split(wif_pk, n, m)
        # Send the shares by email.
        for share, email in zip(shares, data['email']):
            self.sock.send_multipart([note, share, addr,
                email[0], email[1], str(int(email[2]))])

        self.write(json.dumps({'success': addr, 'note': gpg_note}))

class GPGHandler(web.RequestHandler):
    def post(self):
        if 'pubkey' not in self.request.files:
            # Invalid POST
            self.write(INVALID)
            return
        data = self.request.files['pubkey'][0]
        body = data['body'].strip()
{'fraction_non_alphanumeric': 0.11367850520610469, 'fraction_numerical': 0.006561118242761375, 'mean_word_length': 1.6490366452587835, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6006738', 'n_tokens_mistral': 2092, 'n_tokens_neox': 2009, 'n_words': 580}