#
# Client code for Update Agent
# Copyright (c) 1999--2016 Red Hat, Inc.  Distributed under GPLv2.
#
# Author: Preston Brown <pbrown@redhat.com>
#         Adrian Likins <alikins@redhat.com
#         Cristian Gafton <gafton@redhat.com>
#
#
# In addition, as a special exception, the copyright holders give
# permission to link the code of portions of this program with the
# OpenSSL library under certain conditions as described in each
# individual source file, and distribute linked combinations
# including the two.
# You must obey the GNU General Public License in all respects
# for all of the code used other than OpenSSL.  If you modify
# file(s) with this exception, you may extend this exception to your
# version of the file(s), but you are not obligated to do so.  If you
# do not wish to do so, delete this exception statement from your
# version.  If you delete this exception statement from all source
# files in the program, then also delete it here.

import gettext
t = gettext.translation('rhn-client-tools', fallback=True)
# Python 3 translations don't have a ugettext method
if not hasattr(t, 'ugettext'):
    t.ugettext = t.gettext
_ = t.ugettext
from rhn.i18n import ustr
from up2date_client import up2dateLog

import sys
sys.path = sys.path[1:] + sys.path[:1]

try:
    from yum.Errors import YumBaseError as PmBaseError
except ImportError:
    try:
        from dnf.exceptions import Error as PmBaseError
    except ImportError:
        class PmBaseError(Exception):
            def __init__(self, errmsg):
                self.value = errmsg
            def __getattr__(self, name):
                raise AttributeError(_("class %s has no attribute '%s'") % (self.__class__.__name__, name))
            def __setattr__(self, name, value):
                if name in ['errmsg', 'value']:
                    self.__dict__['value'] = value
                else:
                    self.__dict__[name] = value


class Error(PmBaseError):
    """base class for errors"""
    premsg = ''
    def __init__(self, errmsg):
        errmsg = ustr(errmsg)
        PmBaseError.__init__(self, errmsg)
        self.value = 'rhn-plugin: ' + self.premsg + errmsg
        self.log = up2dateLog.initLog()

    def __repr__(self):
        self.log.log_me(self.value)
        return self.value

    def __getattr__(self, name):
        """ Spacewalk backend still use errmsg, let have errmsg as alias to value """
        if name == 'errmsg':
            return self.value
        else:
            if hasattr(PmBaseError, '__getattr__'):
                # rhel5 has no __getattribute__()
                return PmBaseError.__getattr__(self, name)
            else:
                if name in self.__dict__:
                    return self.__dict__[name]
                else:
                    raise AttributeError(_("class %s has no attribute '%s'") % (self.__class__.__name__, name))

    def __setattr__(self, name, value):
        """ Spacewalk backend still use errmsg, let have errmsg as alias to value """
        if name == 'errmsg':
            self.__dict__['value'] = value
        else:
            if hasattr(PmBaseError, '__setattr__'):
                # rhel5 has no __setattr__()
                PmBaseError.__setattr__(self, name, value)
            else:
                self.__dict__[name] = value

class DebAndSuseRepoError(Error):
   pass

try:
    from yum.Errors import RepoError
except ImportError:
    try:
        from dnf.exceptions import RepoError
    except ImportError:
        RepoError = DebAndSuseRepoError


class RhnServerException(Error):
    pass

class PasswordError(RhnServerException):
    """Raise when the server responds with that a password is incorrect"""
    premsg = _("Password error. The message was:\n")


class CommunicationError(RhnServerException):
    """Indicates a problem doing xml-rpc http communication with the server"""
    premsg = _("Error communicating with server. "\
                 "The message was:\n")

class FileNotFoundError(Error):
    """
    Raise when a package or header that is requested returns
    a 404 error code"""
    premsg =  _("File Not Found: \n")


class DelayError(RhnServerException):
    """
    Raise when the expected response from a xml-rpc call
    exceeds a timeout"""
    premsg =  _("Delay error from server.  The message was:\n")


class NoLogError(Error):
    def __init__(self, msg):
        msg = ustr(msg)
        self.value = self.premsg + msg

    def __repr__(self):
        return self.value

class AbuseError(Error):
    pass

class AuthenticationTicketError(NoLogError, RhnServerException):
    pass

class AuthenticationError(NoLogError):
    pass

class ValidationError(NoLogError, RhnServerException):
    """indicates an error during server input validation"""
    premsg = _("Error validating data at server:\n")

class InvalidRegistrationNumberError(ValidationError):
    pass

class RegistrationDeniedError(RhnServerException):
    def __init__(self):
        RhnServerException.__init__(self, self.changeExplanation())

    def __repr__(self):
        return self.value

    def changeExplanation(self):
        return _("""
CloudLinux Network Classic is not supported.
To register with CloudLinux Subscription Management please run:

    subscription-manager register --auto-attach

Get more information at www.cloudlinux.com
    """)

class NoBaseChannelError(NoLogError, RhnServerException):
    """No valid base channel was found for this system"""
    pass

class UnknownMethodException(NoLogError, RhnServerException):
    pass

class RhnUuidUniquenessError(NoLogError, RhnServerException):
    pass

class NetworkError(Error):
    """ some generic network error occurred, e.g. connection reset by peer """
    premsg = _("Network error: ")

class SSLCertificateVerifyFailedError(RepoError, Error):
    def __init__(self):
        RepoError.__init__(self, "The SSL certificate failed verification.")

    __getattr__ = Error.__getattr__

class SSLCertificateFileNotFound(Error):
    pass


class AuthenticationOrAccountCreationError(ValidationError):
    """Class that can represent different things depending on context:
    While logging in with an existing user it represents a username or password
    being incorrect.
    While creating a new account, it represents the username already being
    taken or the user not being allowed to create an account.
    Optimally these different things would be different exceptions, but there
    are single fault codes the server can return to the client that can mean
    more than one of them so we have no way of knowing which is actually
    intended.

    """
    pass

class NotEntitlingError(Error):
    pass

class UnableToCreateUser(NoLogError):
     pass

class ActivationKeyUsageLimitError(NoLogError):
    pass

class LoginMinLengthError(NoLogError):
    pass

class PasswordMinLengthError(NoLogError):
    pass

class PasswordMaxLengthError(NoLogError):
    pass


class InsuffMgmntEntsError(RhnServerException):
    def __init__(self, msg ):
        RhnServerException.__init__(self, self.changeExplanation(msg))

    def __repr__(self):
        return self.value

    def changeExplanation(self, msg):
        newExpln = _("""
    Your organization does not have enough Management entitlements to register this
    system to CloudLinux Network. Please notify your organization administrator of this error.
    You should be able to register this system after your organization frees existing
    or purchases additional entitlements. Additional entitlements may be purchased by your
    organization administrator by logging into CloudLinux Network and visiting

    A common cause of this error code is due to having mistakenly setup an
    Activation Key which is set as the universal default.  If an activation key is set
    on the account as a universal default, you can disable this key and retry to avoid
    requiring a Management entitlement.""")
        term = "Explanation:"
        loc = msg.rindex(term) + len(term)
        return msg[:loc] + newExpln

class InvalidRedirectionError(NoLogError):
    """ Raise when redirect requests could'nt return a package"""
    pass
