1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2012 Jameson Rollins
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see http://www.gnu.org/licenses/ .
18 * Authors: Jameson Rollins <jrollins@finestructure.net>
21 #include "notmuch-client.h"
23 /* Create a GPG context (GMime 2.6) */
24 static notmuch_crypto_context_t *
25 create_gpg_context (notmuch_crypto_t *crypto)
27 notmuch_crypto_context_t *gpgctx;
30 return crypto->gpgctx;
32 /* TODO: GMimePasswordRequestFunc */
33 gpgctx = g_mime_gpg_context_new (NULL, crypto->gpgpath ? crypto->gpgpath : "gpg");
35 fprintf (stderr, "Failed to construct gpg context.\n");
38 crypto->gpgctx = gpgctx;
40 g_mime_gpg_context_set_use_agent ((GMimeGpgContext *) gpgctx, TRUE);
41 g_mime_gpg_context_set_always_trust ((GMimeGpgContext *) gpgctx, FALSE);
48 notmuch_crypto_context_t *(*get_context) (notmuch_crypto_t *crypto);
51 .protocol = "application/pgp-signature",
52 .get_context = create_gpg_context,
55 .protocol = "application/pgp-encrypted",
56 .get_context = create_gpg_context,
60 /* for the specified protocol return the context pointer (initializing
62 notmuch_crypto_context_t *
63 notmuch_crypto_get_context (notmuch_crypto_t *crypto, const char *protocol)
65 notmuch_crypto_context_t *cryptoctx = NULL;
69 fprintf (stderr, "Cryptographic protocol is empty.\n");
73 /* As per RFC 1847 section 2.1: "the [protocol] value token is
74 * comprised of the type and sub-type tokens of the Content-Type".
75 * As per RFC 1521 section 2: "Content-Type values, subtypes, and
76 * parameter names as defined in this document are
77 * case-insensitive." Thus, we use strcasecmp for the protocol.
79 for (i = 0; i < ARRAY_SIZE (protocols); i++) {
80 if (strcasecmp (protocol, protocols[i].protocol) == 0)
81 return protocols[i].get_context (crypto);
84 fprintf (stderr, "Unknown or unsupported cryptographic protocol %s.\n",
91 notmuch_crypto_cleanup (notmuch_crypto_t *crypto)
94 g_object_unref (crypto->gpgctx);
95 crypto->gpgctx = NULL;