From 2490c3ed202a912438d34594c01811f7532430a4 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 22 Mar 2015 00:12:14 +0000 Subject: Added pgp key list command --- .travis.yml | 2 +- Makefile.am | 9 ++++++ configure.ac | 13 ++++++++ install-all.sh | 2 +- src/command/command.c | 18 +++++++++-- src/command/commands.c | 28 +++++++++++++++++ src/command/commands.h | 1 + src/main.c | 6 ++++ src/pgp/gpg.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/pgp/gpg.h | 41 +++++++++++++++++++++++++ src/profanity.c | 6 ++++ tests/pgp/stub_gpg.c | 7 +++++ 12 files changed, 210 insertions(+), 4 deletions(-) create mode 100644 src/pgp/gpg.c create mode 100644 src/pgp/gpg.h create mode 100644 tests/pgp/stub_gpg.c diff --git a/.travis.yml b/.travis.yml index 534440d0..6efa5d82 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: c install: - sudo apt-get update - - sudo apt-get -y install libssl-dev libexpat1-dev libncursesw5-dev libglib2.0-dev libnotify-dev libcurl3-dev libxss-dev libotr2-dev + - sudo apt-get -y install libssl-dev libexpat1-dev libncursesw5-dev libglib2.0-dev libnotify-dev libcurl3-dev libxss-dev libotr2-dev libgpgme11-dev - git clone git://github.com/strophe/libstrophe.git - cd libstrophe - mkdir m4 diff --git a/Makefile.am b/Makefile.am index cd708a13..e14010a7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -44,6 +44,7 @@ tests_sources = \ src/xmpp/xmpp.h src/xmpp/form.c \ src/ui/ui.h \ src/otr/otr.h \ + src/pgp/gpg.h \ src/command/command.h src/command/command.c \ src/command/commands.h src/command/commands.c \ src/tools/parser.c \ @@ -63,6 +64,7 @@ tests_sources = \ src/server_events.c src/server_events.h \ tests/xmpp/stub_xmpp.c \ tests/ui/stub_ui.c \ + tests/pgp/stub_gpg.c \ tests/log/stub_log.c \ tests/config/stub_accounts.c \ tests/helpers.c tests/helpers.h \ @@ -95,6 +97,9 @@ main_source = src/main.c git_include = src/gitversion.h +pgp_sources = \ + src/pgp/gpg.h src/pgp/gpg.c + otr3_sources = \ src/otr/otrlib.h src/otr/otrlibv3.c src/otr/otr.h src/otr/otr.c @@ -110,6 +115,10 @@ script_sources = bootstrap.sh configure-debug install-all.sh man_sources = docs/profanity.1 +if BUILD_PGP +core_sources += $(pgp_sources) +endif + if BUILD_OTR tests_sources += $(otr_test_sources) if BUILD_OTR3 diff --git a/configure.ac b/configure.ac index 0653ab68..96b0e3ba 100644 --- a/configure.ac +++ b/configure.ac @@ -42,6 +42,8 @@ AC_ARG_ENABLE([notifications], [AS_HELP_STRING([--enable-notifications], [enable desktop notifications])]) AC_ARG_ENABLE([otr], [AS_HELP_STRING([--enable-otr], [enable otr encryption])]) +AC_ARG_ENABLE([pgp], + [AS_HELP_STRING([--enable-pgp], [enable pgp])]) AC_ARG_WITH([libxml2], [AS_HELP_STRING([--with-libxml2], [link with libxml2 instead of expat])]) AC_ARG_WITH([xscreensaver], @@ -183,6 +185,17 @@ elif test "x$with_xscreensaver" = x; then [AC_MSG_NOTICE([libX11 not found, falling back to profanity auto-away])]) fi +AM_CONDITIONAL([BUILD_PGP], [false]) +if test "x$enable_pgp" = xyes; then + AC_CHECK_LIB([gpgme], [main], + [AM_CONDITIONAL([BUILD_PGP], [true]) LIBS="-lgpgme $LIBS" AC_DEFINE([HAVE_LIBGPGME], [1], [Have libgpgme])], + [AC_MSG_ERROR([libgpgme is required for profanity])]) +elif test "x$enable_pgp" = x; then + AC_CHECK_LIB([gpgme], [main], + [AM_CONDITIONAL([BUILD_PGP], [true]) LIBS="-lgpgme $LIBS" AC_DEFINE([HAVE_LIBGPGME], [1], [Have libgpgme])], + [AC_MSG_NOTICE([libgpgme not found, pgp support not included.])]) +fi + AM_CONDITIONAL([BUILD_OTR], [false]) AM_CONDITIONAL([BUILD_OTR3], [false]) AM_CONDITIONAL([BUILD_OTR4], [false]) diff --git a/install-all.sh b/install-all.sh index a054e166..fc704bbc 100755 --- a/install-all.sh +++ b/install-all.sh @@ -24,7 +24,7 @@ debian_prepare() echo echo Profanity installer... installing dependencies echo - sudo apt-get -y install git automake autoconf libssl-dev libexpat1-dev libncursesw5-dev libglib2.0-dev libnotify-dev libcurl3-dev libxss-dev libotr5-dev libreadline-dev libtool + sudo apt-get -y install git automake autoconf libssl-dev libexpat1-dev libncursesw5-dev libglib2.0-dev libnotify-dev libcurl3-dev libxss-dev libotr5-dev libreadline-dev libtool libgpgme11-dev } diff --git a/src/command/command.c b/src/command/command.c index e165254e..a8eb362b 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -848,6 +848,14 @@ static struct cmd_t command_defs[] = "Send chat state notifications during chat sessions.", NULL } } }, + { "/pgp", + cmd_pgp, parse_args, 1, 1, NULL, + { "/pgp keys", "Open PGP.", + { "/pgp keys", + "---------", + "Open PGP.", + NULL } } }, + { "/otr", cmd_otr, parse_args, 1, 3, NULL, { "/otr command [args..]", "Off The Record encryption commands.", @@ -1202,6 +1210,7 @@ static Autocomplete time_statusbar_ac; static Autocomplete resource_ac; static Autocomplete inpblock_ac; static Autocomplete receipts_ac; +static Autocomplete pgp_ac; /* * Initialise command autocompleter and history @@ -1563,6 +1572,9 @@ cmd_init(void) receipts_ac = autocomplete_new(); autocomplete_add(receipts_ac, "send"); autocomplete_add(receipts_ac, "request"); + + pgp_ac = autocomplete_new(); + autocomplete_add(pgp_ac, "keys"); } void @@ -1621,6 +1633,7 @@ cmd_uninit(void) autocomplete_free(resource_ac); autocomplete_free(inpblock_ac); autocomplete_free(receipts_ac); + autocomplete_free(pgp_ac); } gboolean @@ -1788,6 +1801,7 @@ cmd_reset_autocomplete() autocomplete_reset(resource_ac); autocomplete_reset(inpblock_ac); autocomplete_reset(receipts_ac); + autocomplete_reset(pgp_ac); if (ui_current_win_type() == WIN_CHAT) { ProfChatWin *chatwin = wins_get_current_chat(); @@ -1971,8 +1985,8 @@ _cmd_complete_parameters(const char * const input) } } - gchar *cmds[] = { "/help", "/prefs", "/disco", "/close", "/wins", "/subject", "/room" }; - Autocomplete completers[] = { help_ac, prefs_ac, disco_ac, close_ac, wins_ac, subject_ac, room_ac }; + gchar *cmds[] = { "/help", "/prefs", "/disco", "/close", "/wins", "/subject", "/room", "/pgp" }; + Autocomplete completers[] = { help_ac, prefs_ac, disco_ac, close_ac, wins_ac, subject_ac, room_ac, pgp_ac }; for (i = 0; i < ARRAY_SIZE(cmds); i++) { result = autocomplete_param_with_ac(input, cmds[i], completers[i], TRUE); diff --git a/src/command/commands.c b/src/command/commands.c index 86285a46..bc1e162b 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -56,6 +56,9 @@ #ifdef HAVE_LIBOTR #include "otr/otr.h" #endif +#ifdef HAVE_LIBGPGME +#include "pgp/gpg.h" +#endif #include "profanity.h" #include "tools/autocomplete.h" #include "tools/parser.h" @@ -4063,6 +4066,31 @@ cmd_xa(gchar **args, struct cmd_help_t help) return TRUE; } +gboolean +cmd_pgp(gchar **args, struct cmd_help_t help) +{ +#ifdef HAVE_LIBGPGME + if (g_strcmp0(args[0], "keys") == 0) { + GSList *keys = p_gpg_list_keys(); + if (keys) { + while (keys) { + cons_debug("Key: %s", keys->data); + keys = g_slist_next(keys); + } + } else { + cons_debug("No keys found"); + } + g_slist_free_full(keys, (GDestroyNotify)free); + } + + return TRUE; +#else + cons_show("This version of Profanity has not been built with PGP support enabled"); + return TRUE; +#endif + +} + gboolean cmd_otr(gchar **args, struct cmd_help_t help) { diff --git a/src/command/commands.h b/src/command/commands.h index 7b7e7c93..9fe645e3 100644 --- a/src/command/commands.h +++ b/src/command/commands.h @@ -103,6 +103,7 @@ gboolean cmd_nick(gchar **args, struct cmd_help_t help); gboolean cmd_notify(gchar **args, struct cmd_help_t help); gboolean cmd_online(gchar **args, struct cmd_help_t help); gboolean cmd_otr(gchar **args, struct cmd_help_t help); +gboolean cmd_pgp(gchar **args, struct cmd_help_t help); gboolean cmd_outtype(gchar **args, struct cmd_help_t help); gboolean cmd_prefs(gchar **args, struct cmd_help_t help); gboolean cmd_priority(gchar **args, struct cmd_help_t help); diff --git a/src/main.c b/src/main.c index 3bb7eeb6..ea8f0cea 100644 --- a/src/main.c +++ b/src/main.c @@ -121,6 +121,12 @@ main(int argc, char **argv) g_print("OTR support: Disabled\n"); #endif +#ifdef HAVE_LIBGPGME + g_print("PGP support: Enabled\n"); +#else + g_print("PGP support: Disabled\n"); +#endif + return 0; } diff --git a/src/pgp/gpg.c b/src/pgp/gpg.c new file mode 100644 index 00000000..e27a0859 --- /dev/null +++ b/src/pgp/gpg.c @@ -0,0 +1,81 @@ +/* + * gpg.c + * + * Copyright (C) 2012 - 2015 James Booth + * + * This file is part of Profanity. + * + * Profanity 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. + * + * Profanity 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 Profanity. If not, see . + * + * 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. + * + */ + +#include +#include + +#include + +#include "log.h" + +void +p_gpg_init(void) +{ + char *version = gpgme_check_version (NULL); + log_debug("GPG: Found gpgme version: %s",version); + gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL)); +} + +GSList * +p_gpg_list_keys(void) +{ + gpgme_error_t error; + gpgme_ctx_t ctx; + gpgme_key_t key; + GSList *result = NULL; + + error = gpgme_new(&ctx); + if (error) { + log_error("GPG: Could not list keys: %s %s", gpgme_strsource(error), gpgme_strerror(error)); + return NULL; + } + + error = gpgme_op_keylist_start(ctx, NULL, 1); + if (error == GPG_ERR_NO_ERROR) { + while (!error) { + error = gpgme_op_keylist_next(ctx, &key); + if (error) { + break; + } + result = g_slist_append(result, strdup(key->uids->uid)); + gpgme_key_release(key); + } + } else { + log_error("GPG: Could not list keys: %s %s", gpgme_strsource(error), gpgme_strerror(error)); + } + + gpgme_release(ctx); + + return result; +} diff --git a/src/pgp/gpg.h b/src/pgp/gpg.h new file mode 100644 index 00000000..3dc256dc --- /dev/null +++ b/src/pgp/gpg.h @@ -0,0 +1,41 @@ +/* + * gpg.h + * + * Copyright (C) 2012 - 2015 James Booth + * + * This file is part of Profanity. + * + * Profanity 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. + * + * Profanity 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 Profanity. If not, see . + * + * 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. + * + */ + +#ifndef GPG_H +#define GPG_H + +void p_gpg_init(void); +GSList* p_gpg_list_keys(void); + +#endif diff --git a/src/profanity.c b/src/profanity.c index 6a2966dd..e54aa7a2 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -59,6 +59,9 @@ #ifdef HAVE_LIBOTR #include "otr/otr.h" #endif +#ifdef HAVE_LIBGPGME +#include "pgp/gpg.h" +#endif #include "resource.h" #include "xmpp/xmpp.h" #include "ui/ui.h" @@ -240,6 +243,9 @@ _init(const int disable_tls, char *log_level) muc_init(); #ifdef HAVE_LIBOTR otr_init(); +#endif +#ifdef HAVE_LIBGPGME + p_gpg_init(); #endif atexit(_shutdown); ui_input_nonblocking(TRUE); diff --git a/tests/pgp/stub_gpg.c b/tests/pgp/stub_gpg.c new file mode 100644 index 00000000..4ee03129 --- /dev/null +++ b/tests/pgp/stub_gpg.c @@ -0,0 +1,7 @@ +#include + +void p_gpg_init(void) {} +GSList* p_gpg_list_keys(void) +{ + return NULL; +} -- cgit 1.4.1-2-gfad0