Permalink
Browse files

Moved geanypg to the geany-plugins/ directory

  • Loading branch information...
1 parent 066d45f commit c85a9b49211dab44d3b6925ae96e29d1b2d873f0 @alvesh88 alvesh88 committed Feb 28, 2011
View
@@ -0,0 +1 @@
+Hans Alves <alves(dot)h88(at)gmail(dot)com>
View
Oops, something went wrong.
View
@@ -0,0 +1 @@
+SUBDIRS=src
View
@@ -0,0 +1,20 @@
+AC_INIT([geanypg], [0.1], [alves.h88@gmail.com])
+AM_INIT_AUTOMAKE([-Wall -Werror foreign])
+AC_PROG_CC
+AC_DISABLE_STATIC
+AC_PROG_LIBTOOL
+
+# checking for gpgme
+AM_PATH_GPGME()
+
+# checking for Geany
+PKG_CHECK_MODULES(GEANY, [geany >= 0.20])
+AC_SUBST(GEANY_CFLAGS)
+AC_SUBST(GEANY_LIBS)
+
+libdir="`$PKG_CONFIG --variable=libdir geany`/geany"
+datadir="`$PKG_CONFIG --variable=datadir geany`/geany/plugins/${PACKAGE_NAME}"
+
+AC_CONFIG_HEADER([config.h])
+AC_CONFIG_FILES([Makefile src/Makefile])
+AC_OUTPUT
View
@@ -0,0 +1,5 @@
+lib_LTLIBRARIES=geanypg.la
+geanypg_la_SOURCES=aux.c encrypt_cb.c key_selection_dialog.c sign_cb.c verify_cb.c decrypt_cb.c geanypg.c pinentry.c verify_aux.c
+geanypg_la_LDFLAGS=-module -avoid-version
+geanypg_la_LIBADD=@GEANY_LIBS@ @GPGME_LIBS@
+AM_CFLAGS=@GEANY_CFLAGS@ @GPGME_CFLAGS@
View
@@ -0,0 +1,163 @@
+// aux.c
+//
+// Copyright 2011 Hans Alves <alves.h88@gmail.com>
+//
+// 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 2 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, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+// MA 02110-1301, USA.
+
+
+#include "geanypg.h"
+
+void geanypg_init_ed(encrypt_data * ed)
+{
+ ed->key_array = NULL;
+ ed->nkeys = 0;
+ ed->skey_array = NULL;
+ ed->nskeys = 0;
+}
+
+int geanypg_get_keys(encrypt_data * ed)
+{
+ gpgme_error_t err;
+ unsigned long size = SIZE;
+ //initialize index to 0
+ unsigned long index = 0;
+ //allocate array of size 1N
+ ed->key_array = (gpgme_key_t*) malloc(SIZE * sizeof(gpgme_key_t));
+ err = gpgme_op_keylist_start(ed->ctx, NULL, 0);
+ while (!err)
+ {
+ err = gpgme_op_keylist_next(ed->ctx, ed->key_array + index);
+ if (err)
+ break;
+ ++index;
+ if (index >= size)
+ {
+ size += SIZE;
+ ed->key_array = (gpgme_key_t*) realloc(ed->key_array, size * sizeof(gpgme_key_t));
+ }
+ }
+ ed->nkeys = index;
+ if (gpg_err_code(err) != GPG_ERR_EOF)
+ {
+ geanypg_show_err_msg(err);
+ return 0;
+ }
+ return 1;
+}
+
+int geanypg_get_secret_keys(encrypt_data * ed)
+{
+ gpgme_error_t err;
+ unsigned long size = SIZE;
+ //initialize index to 0
+ unsigned long index = 0;
+ //allocate array of size 1N
+ ed->skey_array = (gpgme_key_t*) malloc(SIZE * sizeof(gpgme_key_t));
+ err = gpgme_op_keylist_start(ed->ctx, NULL, 1);
+ while (!err)
+ {
+ err = gpgme_op_keylist_next(ed->ctx, ed->skey_array + index);
+ if (err)
+ break;
+ ++index;
+ if (index >= size)
+ {
+ size += SIZE;
+ ed->skey_array = (gpgme_key_t*) realloc(ed->skey_array, size * sizeof(gpgme_key_t));
+ }
+ }
+ ed->nskeys = index;
+ if (gpg_err_code(err) != GPG_ERR_EOF)
+ {
+ geanypg_show_err_msg(err);
+ return 0;
+ }
+ return 1;
+}
+
+void geanypg_release_keys(encrypt_data * ed)
+{
+ gpgme_key_t * ptr;
+ if (ed->key_array)
+ {
+ ptr = ed->key_array;
+ while (ptr < ed->key_array + ed->nkeys)
+ gpgme_key_unref(*(ptr++));
+ free(ed->key_array);
+ ed->key_array = NULL;
+ ed->nkeys = 0;
+ }
+ if (ed->skey_array)
+ {
+ ptr = ed->skey_array;
+ while (ptr < ed->skey_array + ed->nskeys)
+ gpgme_key_unref(*(ptr++));
+ free(ed->skey_array);
+ ed->skey_array = NULL;
+ ed->nskeys = 0;
+ }
+}
+
+
+void geanypg_load_buffer(gpgme_data_t * buffer)
+{
+ //gpgme_data_new_from_mem(buffer, text, size, 0);
+ GeanyDocument * doc = document_get_current();
+ //SCI_GETSELECTIONSTART-SCI_GETSELECTIONEND
+ char * data = NULL;
+ unsigned long sstart = scintilla_send_message(doc->editor->sci, SCI_GETSELECTIONSTART, 0, 0);
+ unsigned long send = scintilla_send_message(doc->editor->sci, SCI_GETSELECTIONEND, 0, 0);
+ unsigned long size = 0;
+ if (sstart - send)
+ {
+ size = scintilla_send_message(doc->editor->sci, SCI_GETSELTEXT, 0, 0);
+ data = (char *) malloc(size + 1);
+ scintilla_send_message(doc->editor->sci, SCI_GETSELTEXT, 0, (sptr_t)data);
+ gpgme_data_new_from_mem(buffer, data, size, 1);
+ }
+ else
+ {
+ size = scintilla_send_message(doc->editor->sci, SCI_GETLENGTH, 0, 0);
+ data = (char *) malloc(size + 1);
+ scintilla_send_message(doc->editor->sci, SCI_GETTEXT, (uptr_t)(size + 1), (sptr_t)data);
+ gpgme_data_new_from_mem(buffer, data, size, 1);
+ }
+ if (data) // if there is no text data may still be NULL
+ free(data);
+ gpgme_data_set_encoding(*buffer, GPGME_DATA_ENCODING_BINARY);
+}
+
+void geanypg_write_file(FILE * file)
+{
+ unsigned bufsize = 2048;
+ unsigned long size;
+ char buffer[bufsize];
+ GeanyDocument * doc = document_get_current();
+ if (abs(sci_get_selection_start(doc->editor->sci) - sci_get_selection_end(doc->editor->sci)))
+ { // replace selected text
+ // clear selection, cursor should be at the end or beginneng of the selection
+ scintilla_send_message(doc->editor->sci, SCI_REPLACESEL, 0, (sptr_t)"");
+ while ((size = fread(buffer, 1, bufsize, file)))
+ // add at the cursor
+ scintilla_send_message(doc->editor->sci, SCI_ADDTEXT, (uptr_t) size, (sptr_t) buffer);
+ }
+ else
+ { //replace complete document
+ scintilla_send_message(doc->editor->sci, SCI_CLEARALL, 0, 0);
+ while ((size = fread(buffer, 1, bufsize, file)))
+ scintilla_send_message(doc->editor->sci, SCI_APPENDTEXT, (uptr_t) size, (sptr_t) buffer);
+ }
+}
View
@@ -0,0 +1,76 @@
+// decrypt_cb.c
+//
+// Copyright 2011 Hans Alves <alves.h88@gmail.com>
+//
+// 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 2 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, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+// MA 02110-1301, USA.
+
+
+#include "geanypg.h"
+
+void geanypg_decrypt_verify(encrypt_data * ed)
+{
+ gpgme_data_t plain, cipher;
+ gpgme_error_t err;
+ FILE * tempfile;
+
+ tempfile = tmpfile();
+ if (!(tempfile))
+ {
+ fprintf(stderr, "GEANYPG: couldn't create tempfile: %s.\n", strerror(errno));
+ return ;
+ }
+ gpgme_data_new_from_stream(&plain, tempfile);
+
+ geanypg_load_buffer(&cipher);
+
+ err = gpgme_op_decrypt_verify(ed->ctx, cipher, plain);
+ if (gpgme_err_code(err) == GPG_ERR_NO_DATA) // no encription, but maybe signatures
+ {
+ // maybe reaload cipher
+ gpgme_data_release(cipher);
+ geanypg_load_buffer(&cipher);
+ rewind(tempfile);
+ err = gpgme_op_verify(ed->ctx, cipher, NULL, plain);
+ }
+ if (err != GPG_ERR_NO_ERROR)
+ geanypg_show_err_msg(err);
+ else
+ {
+ rewind(tempfile);
+ geanypg_write_file(tempfile);
+ geanypg_handle_signatures(ed);
+ }
+
+ fclose(tempfile);
+ // release buffers
+ gpgme_data_release(cipher);
+ gpgme_data_release(plain);
+}
+
+void geanypg_decrypt_cb(GtkMenuItem * menuitem, gpointer user_data)
+{
+ encrypt_data ed;
+ geanypg_init_ed(&ed);
+ gpgme_error_t err = gpgme_new(&ed.ctx);
+ if (err && geanypg_show_err_msg(err))
+ return;
+ gpgme_set_protocol(ed.ctx, GPGME_PROTOCOL_OpenPGP);
+ gpgme_set_passphrase_cb(ed.ctx, geanypg_passphrase_cb, NULL);
+ if (geanypg_get_keys(&ed) && geanypg_get_secret_keys(&ed))
+ geanypg_decrypt_verify(&ed);
+ geanypg_release_keys(&ed);
+ gpgme_release(ed.ctx);
+}
View
@@ -0,0 +1,83 @@
+// encrypt_cb.c
+//
+// Copyright 2011 Hans Alves <alves.h88@gmail.com>
+//
+// 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 2 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, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+// MA 02110-1301, USA.
+
+
+#include "geanypg.h"
+
+void geanypg_encrypt(encrypt_data * ed, gpgme_key_t * recp, int sign)
+{ // FACTORIZE
+ gpgme_data_t plain, cipher;
+ gpgme_error_t err;
+ FILE * tempfile;
+ tempfile = tmpfile();
+ if (!(tempfile))
+ {
+ fprintf(stderr, "GEANYPG: couldn't create tempfile: %s.\n", strerror(errno));
+ return ;
+ }
+ gpgme_data_new_from_stream(&cipher, tempfile);
+ gpgme_data_set_encoding(cipher, GPGME_DATA_ENCODING_ARMOR);
+
+ geanypg_load_buffer(&plain);
+
+ // do the actual encryption
+ if (sign)
+ err = gpgme_op_encrypt_sign(ed->ctx, recp, 0, plain, cipher);
+ else
+ err = gpgme_op_encrypt(ed->ctx, recp, 0, plain, cipher);
+ if (err != GPG_ERR_NO_ERROR && gpgme_err_code(err) != GPG_ERR_CANCELED)
+ geanypg_show_err_msg(err);
+ else
+ {
+ rewind(tempfile);
+ geanypg_write_file(tempfile);
+ }
+
+ fclose(tempfile);
+ // release buffers
+ gpgme_data_release(plain);
+ gpgme_data_release(cipher);
+}
+
+void geanypg_encrypt_cb(GtkMenuItem * menuitem, gpointer user_data)
+{
+ int sign;
+ encrypt_data ed;
+ geanypg_init_ed(&ed);
+ gpgme_error_t err = gpgme_new(&ed.ctx);
+ if (err && geanypg_show_err_msg(err))
+ return;
+ gpgme_set_armor(ed.ctx, 1);
+ gpgme_set_passphrase_cb(ed.ctx, geanypg_passphrase_cb, NULL);
+ if (geanypg_get_keys(&ed) && geanypg_get_secret_keys(&ed))
+ {
+ gpgme_key_t * recp = NULL;
+ if (geanypg_encrypt_selection_dialog(&ed, &recp, &sign))
+ {
+ if (*recp)
+ geanypg_encrypt(&ed, recp, sign);
+ else if (dialogs_show_question("No recipients were selected,\nuse symetric cipher?"))
+ geanypg_encrypt(&ed, NULL, sign);
+ }
+ if (recp)
+ free(recp);
+ }
+ geanypg_release_keys(&ed);
+ gpgme_release(ed.ctx);
+}
Oops, something went wrong.

0 comments on commit c85a9b4

Please sign in to comment.