def processRegistration(email):
    'Process an SMS address registration'
    # Get userID and code
    match = pattern_registration.match(email.subject)
    if not match:
        return False
    userID, userCode = match.groups()
    userID = int(userID)
    fromWhom = parseaddr(email.fromWhom)[1]
    # Make sure we have a proper email address
    try:
        fromWhom = validators.Email(not_empty=True).to_python(fromWhom)
    except Invalid:
        return False
    # If userID is zero, then the sender wants to unregister his or her address
    if userID == 0:
        db.query(SMSAddress).filter_by(email=fromWhom).delete()
        return True
    # Load
    user = db.query(User).filter_by(id=userID, code=userCode).options(joinedload(User.sms_addresses)).first()
    # If the user doesn't exist,
    if not user:
        return False
    # If we have registered the address already,
    if fromWhom in (x.email for x in user.sms_addresses):
        return True
    # Add
    db.add(SMSAddress(email=fromWhom, user_id=userID))
    return True
def processRegistration(email):
    'Process an SMS address registration'
    # Get userID and code
    match = pattern_registration.match(email.subject)
    if not match:
        return False
    userID, userCode = match.groups()
    userID = int(userID)
    fromWhom = parseaddr(email.fromWhom)[1]
    # Make sure we have a proper email address
    try:
        fromWhom = validators.Email(not_empty=True).to_python(fromWhom)
    except Invalid:
        return False
    # If userID is zero, then the sender wants to unregister his or her address
    if userID == 0:
        db.query(SMSAddress).filter_by(email=fromWhom).delete()
        return True
    # Load
    user = db.query(User).filter_by(id=userID, code=userCode).options(
        joinedload(User.sms_addresses)).first()
    # If the user doesn't exist,
    if not user:
        return False
    # If we have registered the address already,
    if fromWhom in (x.email for x in user.sms_addresses):
        return True
    # Add
    db.add(SMSAddress(email=fromWhom, user_id=userID))
    return True
    # Prepare ticket
    try:
        ticket = make_random_unique_string(TICKET_LEN, 
            lambda x: db.query(User_).filter_by(ticket=x).first() == None)
    except RuntimeError:
        return dict(isOk=0, errorByID={'status': 'Could not generate ticket; please try again later'})
    # Prepare user_
    user_ = User_(
        username=form['username'],
        password=form['password'], 
        nickname=form['nickname'], 
        email=form['email'],
        user_id=user.id if user else None,
        ticket=ticket,
        when_expired=datetime.datetime.utcnow() + datetime.timedelta(hours=TICKET_HOURS))
    db.add(user_)
    # Send message
    get_mailer(request).send_to_queue(Message(
        recipients=[formataddr((user_.nickname, user_.email))],
        subject='Confirm {}'.format(action),
        body=render('users/confirm.mak', {
            'form': form,
            'ticket': ticket,
            'action': action,
            'TICKET_HOURS': TICKET_HOURS,
        }, request)))
    # Return
    return dict(isOk=1)


def apply_user_(ticket):
        except ValueError:
            patentFilingDate = None
        db.merge(
            Patent(
                id=int(patentID),
                technology_id=int(technologyID),
                name=patentName.strip(),
                firm_id=int(patentLawFirmID),
                firm_ref=patentLawFirmCase.strip(),
                date_filed=patentFilingDate,
                status_id=int(patentStatusID),
                type_id=int(patentTypeID),
                country_id=int(countryID),
            )
        )
    for patentID, contactID, piOrder in patentInventors:
        db.merge(PatentInventor(patent_id=int(patentID), contact_id=int(contactID), pi_order=int(piOrder)))
    for patentStatusID, patentStatusName in patentStatuses:
        db.merge(PatentStatus(id=int(patentStatusID), name=patentStatusName.strip()))
    for patentTypeID, patentTypeName in patentTypes:
        db.merge(PatentType(id=int(patentTypeID), name=patentTypeName.strip()))
    for phoneID, contactID, phoneNumber, phoneType in phones:
        db.merge(Phone(id=int(phoneID), contact_id=int(contactID), number=phoneNumber.strip(), type=phoneType.strip()))
    for technologyID, technologyCase, technologyName in technologies:
        db.merge(Technology(id=int(technologyID), ref=technologyCase.strip(), name=technologyName.strip()))
    # Record
    db.add(Upload(ip=get_remote_ip(request), when=datetime.datetime.utcnow()))
    # Return
    region_invalidate(get_patents, None)
    return dict(isOk=1)
            PatentStatus(
                id=int(patentStatusID),
                name=patentStatusName.strip(),
            ))
    for patentTypeID, patentTypeName in patentTypes:
        db.merge(
            PatentType(
                id=int(patentTypeID),
                name=patentTypeName.strip(),
            ))
    for phoneID, contactID, phoneNumber, phoneType in phones:
        db.merge(
            Phone(
                id=int(phoneID),
                contact_id=int(contactID),
                number=phoneNumber.strip(),
                type=phoneType.strip(),
            ))
    for technologyID, technologyCase, technologyName in technologies:
        db.merge(
            Technology(
                id=int(technologyID),
                ref=technologyCase.strip(),
                name=technologyName.strip(),
            ))
    # Record
    db.add(Upload(ip=get_remote_ip(request), when=datetime.datetime.utcnow()))
    # Return
    region_invalidate(get_patents, None)
    return dict(isOk=1)