Blame lang/cpp/src/gpgadduserideditinteractor.cpp

Packit Service 672cf4
/*
Packit Service 672cf4
  gpgadduserideditinteractor.cpp - Edit Interactor to add a new UID to an OpenPGP key
Packit Service 672cf4
  Copyright (C) 2008 Klarälvdalens Datakonsult AB
Packit Service 672cf4
  2016 Bundesamt für Sicherheit in der Informationstechnik
Packit Service 672cf4
  Software engineering by Intevation GmbH
Packit Service 672cf4
Packit Service 672cf4
  This file is part of GPGME++.
Packit Service 672cf4
Packit Service 672cf4
  GPGME++ is free software; you can redistribute it and/or
Packit Service 672cf4
  modify it under the terms of the GNU Library General Public
Packit Service 672cf4
  License as published by the Free Software Foundation; either
Packit Service 672cf4
  version 2 of the License, or (at your option) any later version.
Packit Service 672cf4
Packit Service 672cf4
  GPGME++ is distributed in the hope that it will be useful,
Packit Service 672cf4
  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 672cf4
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 672cf4
  GNU Library General Public License for more details.
Packit Service 672cf4
Packit Service 672cf4
  You should have received a copy of the GNU Library General Public License
Packit Service 672cf4
  along with GPGME++; see the file COPYING.LIB.  If not, write to the
Packit Service 672cf4
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Packit Service 672cf4
  Boston, MA 02110-1301, USA.
Packit Service 672cf4
*/
Packit Service 672cf4
Packit Service 672cf4
#ifdef HAVE_CONFIG_H
Packit Service 672cf4
 #include "config.h"
Packit Service 672cf4
#endif
Packit Service 672cf4
Packit Service 672cf4
#include "gpgadduserideditinteractor.h"
Packit Service 672cf4
Packit Service 672cf4
#include "error.h"
Packit Service 672cf4
Packit Service 672cf4
#include <gpgme.h>
Packit Service 672cf4
Packit Service 672cf4
#include <cstring>
Packit Service 672cf4
Packit Service 672cf4
using std::strcmp;
Packit Service 672cf4
Packit Service 672cf4
// avoid conflict (msvc)
Packit Service 672cf4
#ifdef ERROR
Packit Service 672cf4
# undef ERROR
Packit Service 672cf4
#endif
Packit Service 672cf4
Packit Service 672cf4
using namespace GpgME;
Packit Service 672cf4
Packit Service 672cf4
GpgAddUserIDEditInteractor::GpgAddUserIDEditInteractor()
Packit Service 672cf4
    : EditInteractor(),
Packit Service 672cf4
      m_name(),
Packit Service 672cf4
      m_email(),
Packit Service 672cf4
      m_comment()
Packit Service 672cf4
{
Packit Service 672cf4
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
GpgAddUserIDEditInteractor::~GpgAddUserIDEditInteractor() {}
Packit Service 672cf4
Packit Service 672cf4
void GpgAddUserIDEditInteractor::setNameUtf8(const std::string &name)
Packit Service 672cf4
{
Packit Service 672cf4
    m_name = name;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
void GpgAddUserIDEditInteractor::setEmailUtf8(const std::string &email)
Packit Service 672cf4
{
Packit Service 672cf4
    m_email = email;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
void GpgAddUserIDEditInteractor::setCommentUtf8(const std::string &comment)
Packit Service 672cf4
{
Packit Service 672cf4
    m_comment = comment;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
// work around --enable-final
Packit Service 672cf4
namespace GpgAddUserIDEditInteractor_Private
Packit Service 672cf4
{
Packit Service 672cf4
enum {
Packit Service 672cf4
    START = EditInteractor::StartState,
Packit Service 672cf4
    COMMAND,
Packit Service 672cf4
    NAME,
Packit Service 672cf4
    EMAIL,
Packit Service 672cf4
    COMMENT,
Packit Service 672cf4
    QUIT,
Packit Service 672cf4
    SAVE,
Packit Service 672cf4
Packit Service 672cf4
    ERROR = EditInteractor::ErrorState
Packit Service 672cf4
};
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
const char *GpgAddUserIDEditInteractor::action(Error &err) const
Packit Service 672cf4
{
Packit Service 672cf4
Packit Service 672cf4
    using namespace GpgAddUserIDEditInteractor_Private;
Packit Service 672cf4
Packit Service 672cf4
    switch (state()) {
Packit Service 672cf4
    case COMMAND:
Packit Service 672cf4
        return "adduid";
Packit Service 672cf4
    case NAME:
Packit Service 672cf4
        return m_name.c_str();
Packit Service 672cf4
    case EMAIL:
Packit Service 672cf4
        return m_email.c_str();
Packit Service 672cf4
    case COMMENT:
Packit Service 672cf4
        return m_comment.c_str();
Packit Service 672cf4
    case QUIT:
Packit Service 672cf4
        return "quit";
Packit Service 672cf4
    case SAVE:
Packit Service 672cf4
        return "Y";
Packit Service 672cf4
    case START:
Packit Service 672cf4
    case ERROR:
Packit Service 6c01f9
        return 0;
Packit Service 672cf4
    default:
Packit Service 672cf4
        err = Error::fromCode(GPG_ERR_GENERAL);
Packit Service 6c01f9
        return 0;
Packit Service 672cf4
    }
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
unsigned int GpgAddUserIDEditInteractor::nextState(unsigned int status, const char *args, Error &err) const
Packit Service 672cf4
{
Packit Service 672cf4
Packit Service 672cf4
    static const Error GENERAL_ERROR     = Error::fromCode(GPG_ERR_GENERAL);
Packit Service 672cf4
    static const Error INV_NAME_ERROR    = Error::fromCode(GPG_ERR_INV_NAME);
Packit Service 672cf4
    static const Error INV_EMAIL_ERROR   = Error::fromCode(GPG_ERR_INV_USER_ID);
Packit Service 672cf4
    static const Error INV_COMMENT_ERROR = Error::fromCode(GPG_ERR_INV_USER_ID);
Packit Service 672cf4
Packit Service 672cf4
    if (needsNoResponse(status)) {
Packit Service 672cf4
        return state();
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
    using namespace GpgAddUserIDEditInteractor_Private;
Packit Service 672cf4
Packit Service 672cf4
    switch (state()) {
Packit Service 672cf4
    case START:
Packit Service 672cf4
        if (status == GPGME_STATUS_GET_LINE &&
Packit Service 672cf4
                strcmp(args, "keyedit.prompt") == 0) {
Packit Service 672cf4
            return COMMAND;
Packit Service 672cf4
        }
Packit Service 672cf4
        err = GENERAL_ERROR;
Packit Service 672cf4
        return ERROR;
Packit Service 672cf4
    case COMMAND:
Packit Service 672cf4
        if (status == GPGME_STATUS_GET_LINE &&
Packit Service 672cf4
                strcmp(args, "keygen.name") == 0) {
Packit Service 672cf4
            return NAME;
Packit Service 672cf4
        }
Packit Service 672cf4
        err = GENERAL_ERROR;
Packit Service 672cf4
        return ERROR;
Packit Service 672cf4
    case NAME:
Packit Service 672cf4
        if (status == GPGME_STATUS_GET_LINE &&
Packit Service 672cf4
                strcmp(args, "keygen.email") == 0) {
Packit Service 672cf4
            return EMAIL;
Packit Service 672cf4
        }
Packit Service 672cf4
        err = GENERAL_ERROR;
Packit Service 672cf4
        if (status == GPGME_STATUS_GET_LINE &&
Packit Service 672cf4
                strcmp(args, "keygen.name") == 0) {
Packit Service 672cf4
            err = INV_NAME_ERROR;
Packit Service 672cf4
        }
Packit Service 672cf4
        return ERROR;
Packit Service 672cf4
    case EMAIL:
Packit Service 672cf4
        if (status == GPGME_STATUS_GET_LINE &&
Packit Service 672cf4
                strcmp(args, "keygen.comment") == 0) {
Packit Service 672cf4
            return COMMENT;
Packit Service 672cf4
        }
Packit Service 672cf4
        err = GENERAL_ERROR;
Packit Service 672cf4
        if (status == GPGME_STATUS_GET_LINE &&
Packit Service 672cf4
                strcmp(args, "keygen.email") == 0) {
Packit Service 672cf4
            err = INV_EMAIL_ERROR;
Packit Service 672cf4
        }
Packit Service 672cf4
        return ERROR;
Packit Service 672cf4
    case COMMENT:
Packit Service 672cf4
        if (status == GPGME_STATUS_GET_LINE &&
Packit Service 672cf4
                strcmp(args, "keyedit.prompt") == 0) {
Packit Service 672cf4
            return QUIT;
Packit Service 672cf4
        }
Packit Service 672cf4
        err = GENERAL_ERROR;
Packit Service 672cf4
        if (status == GPGME_STATUS_GET_LINE &&
Packit Service 672cf4
                strcmp(args, "keygen.comment") == 0) {
Packit Service 672cf4
            err = INV_COMMENT_ERROR;
Packit Service 672cf4
        }
Packit Service 672cf4
        return ERROR;
Packit Service 672cf4
    case QUIT:
Packit Service 672cf4
        if (status == GPGME_STATUS_GET_BOOL &&
Packit Service 672cf4
                strcmp(args, "keyedit.save.okay") == 0) {
Packit Service 672cf4
            return SAVE;
Packit Service 672cf4
        }
Packit Service 672cf4
        err = GENERAL_ERROR;
Packit Service 672cf4
        return ERROR;
Packit Service 672cf4
    case ERROR:
Packit Service 672cf4
        if (status == GPGME_STATUS_GET_LINE &&
Packit Service 672cf4
                strcmp(args, "keyedit.prompt") == 0) {
Packit Service 672cf4
            return QUIT;
Packit Service 672cf4
        }
Packit Service 672cf4
        err = lastError();
Packit Service 672cf4
        return ERROR;
Packit Service 672cf4
    default:
Packit Service 672cf4
        err = GENERAL_ERROR;
Packit Service 672cf4
        return ERROR;
Packit Service 672cf4
    }
Packit Service 672cf4
}