# Path: packages/config.py

# 
# Path: packages/email.py
# def send_new_pacakge_email(package):
#     if _cfg("smtp-host") == "":
#         return
#     smtp = smtplib.SMTP(_cfg("smtp-host"), _cfgi("smtp-port"))
#     smtp.ehlo()
#     smtp.starttls()
#     smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
#     with open("emails/new-package") as f:
#         message = MIMEText(html.parser.HTMLParser().unescape(\
#                 pystache.render(f.read(), { 'url': _cfg("protocol") + "://" + _cfg("domain") + url_for("html.package", name=package.name, repo=package.repo) })))
#     targets = [u.email for u in User.query.filter(User.admin == True)]
#     message['Subject'] = "New package pending approval"
#     message['From'] = _cfg("smtp-user")
#     message['To'] = ';'.join(targets)
#     smtp.sendmail(_cfg("smtp-user"), targets, message.as_string())
#     smtp.quit()


from flask import Blueprint, render_template, abort, request, redirect, session, url_for
from flask.ext.login import current_user, login_user
from sqlalchemy import desc
from shutil import move
from datetime import datetime
from packages.objects import *
from packages.common import *
from packages.config import _cfg
from packages.kpack import PackageInfo
from packages.email import send_new_pacakge_email
import os
import binascii
import zipfile
import urllib
import tempfile
    if not user:
        return { 'success': False, 'error': 'User not found.' }, 404
    if (not current_user or not current_user.admin) and (confirmation != user.confirmation):
        return { 'success': False, 'error': 'You do not have permission to confirm this user.' }, 403
    user.confirmation = None
    db.commit()
    return { 'success': True }

@api.route("/api/v1/user/<username>/unconfirm", methods=["POST"])
@json_output
def unconfirm_user(username):
    user = User.query.filter(User.username == username).first()
    if not user:
        return { 'success': False, 'error': 'User not found.' }, 404
    if not current_user or not current_user.admin:
        return { 'success': False, 'error': 'You do not have permission to unconfirm this user.' }, 403
    if current_user.username == user.username:
        return { 'success': False, 'error': 'You cannot unconfirm your own account.' }, 403
    user.confirmation = binascii.b2a_hex(os.urandom(20)).decode("utf-8")
    db.commit()
    return { 'success': True }

@api.route("/api/v1/<repo>/<name>/remove", methods=["POST"])
@json_output
def remove_package(repo, name):
    package = Package.query.filter(Package.name == name).filter(Package.repo == repo).first()
    if not package:
        return { 'success': False, 'error': 'Package not found.' }, 404
    if not current_user or not current_user.admin:
        return { 'success': False, 'error': 'You do not have permission to remove this package.' }, 403

# Path: packages/config.py

# 
# Path: packages/email.py
# def send_new_pacakge_email(package):
#     if _cfg("smtp-host") == "":
#         return
#     smtp = smtplib.SMTP(_cfg("smtp-host"), _cfgi("smtp-port"))
#     smtp.ehlo()
#     smtp.starttls()
#     smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
#     with open("emails/new-package") as f:
#         message = MIMEText(html.parser.HTMLParser().unescape(\
#                 pystache.render(f.read(), { 'url': _cfg("protocol") + "://" + _cfg("domain") + url_for("html.package", name=package.name, repo=package.repo) })))
#     targets = [u.email for u in User.query.filter(User.admin == True)]
#     message['Subject'] = "New package pending approval"
#     message['From'] = _cfg("smtp-user")
#     message['To'] = ';'.join(targets)
#     smtp.sendmail(_cfg("smtp-user"), targets, message.as_string())
#     smtp.quit()


# Path: packages/blueprints/api.py
from flask import Blueprint, render_template, abort, request, redirect, session, url_for
from flask.ext.login import current_user, login_user
from sqlalchemy import desc
from shutil import move
from datetime import datetime
from packages.objects import *
from packages.common import *
from packages.config import _cfg
from packages.kpack import PackageInfo
from packages.email import send_new_pacakge_email
import os
import binascii
import zipfile
import urllib
import tempfile

    if not user:
        return { 'success': False, 'error': 'User not found.' }, 404
    if (not current_user or not current_user.admin) and (confirmation != user.confirmation):
        return { 'success': False, 'error': 'You do not have permission to confirm this user.' }, 403
    user.confirmation = None
    db.commit()
    return { 'success': True }

@api.route("/api/v1/user/<username>/unconfirm", methods=["POST"])
@json_output
def unconfirm_user(username):
    user = User.query.filter(User.username == username).first()
    if not user:
        return { 'success': False, 'error': 'User not found.' }, 404
    if not current_user or not current_user.admin:
        return { 'success': False, 'error': 'You do not have permission to unconfirm this user.' }, 403
    if current_user.username == user.username:
        return { 'success': False, 'error': 'You cannot unconfirm your own account.' }, 403
    user.confirmation = binascii.b2a_hex(os.urandom(20)).decode("utf-8")
    db.commit()
    return { 'success': True }

@api.route("/api/v1/<repo>/<name>/remove", methods=["POST"])
@json_output
def remove_package(repo, name):
    package = Package.query.filter(Package.name == name).filter(Package.repo == repo).first()
    if not package:
        return { 'success': False, 'error': 'Package not found.' }, 404
    if not current_user or not current_user.admin:
        return { 'success': False, 'error': 'You do not have permission to remove this package.' }, 403

    packagePath = os.path.join(_cfg("storage"), package.repo, "{0}-{1}.pkg".format(package.name, package.version))