A task-oriented GnuPG frontend
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

100 lines
2.9 KiB

/*
* Yorkie - A task-oriented GnuPG frontend
* Copyright (C) 2020 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <errno.h>
#include <locale.h>
#include <glib.h>
#include <gtk/gtk.h>
#include "error.h"
#include "encrypt.h"
#include "gpgutil.h"
static gboolean
check_non_option_args(GOptionContext *parser, GOptionGroup *group, gpointer data, GError **error)
{
char **args = *((char ***)data);
size_t n = 0;
if ( args ) {
while ( *args++ )
n += 1;
}
if ( n != 1 )
g_set_error(error, YKI_ERROR, YKI_ERROR_INVALID_COMMAND, n == 0 ?
"Missing FILE argument" : "Too many arguments");
return n == 1;
}
int
main(int argc, char **argv)
{
gpgme_ctx_t gpgme;
GError *error = NULL;
gboolean encrypt;
gchar **files;
GOptionEntry options[] = {
{ "encrypt", 'e', 0, G_OPTION_ARG_NONE, &encrypt, "Encrypt the file", NULL },
{ "decrypt", 'd', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &encrypt, "Decrypt the file", NULL },
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &files, NULL, "FILE" },
{ NULL }
};
GOptionContext *parser;
GOptionGroup *main_options;
encrypt = TRUE;
files = NULL;
parser = g_option_context_new("- Perform cryptographic operation on specified file");
main_options = g_option_group_new(NULL, NULL, NULL, (gpointer) &files, NULL);
g_option_group_add_entries(main_options, options);
g_option_group_set_parse_hooks(main_options, NULL, check_non_option_args);
g_option_context_set_main_group(parser, main_options);
g_option_context_add_group(parser, gtk_get_option_group(TRUE));
g_option_context_parse(parser, &argc, &argv, &error);
if ( ! error )
gpgme = gpgutil_initialize(&error);
if ( ! error ) {
if ( encrypt )
yki_encrypt(gpgme, files[0], &error);
else
g_set_error(&error, YKI_ERROR, YKI_ERROR_NOT_IMPLEMENTED, "Cannot decrypt");
}
if ( error )
g_printerr("Error: %s: %s\n", yki_error_msg(error->code), error->message);
g_option_context_free(parser);
g_strfreev(files);
gpgme_release(gpgme);
return error ? EXIT_FAILURE : EXIT_SUCCESS;
}