[Pkg-privacy-commits] [irssi-plugin-otr] 93/267: Refactor otr_key.c to key.c
Ximin Luo
infinity0 at moszumanska.debian.org
Sat Aug 22 12:41:32 UTC 2015
This is an automated email from the git hooks/post-receive script.
infinity0 pushed a commit to branch debian
in repository irssi-plugin-otr.
commit a392dc84e4327d3b94e9464821d9ea35039052ea
Author: David Goulet <dgoulet at ev0ke.net>
Date: Fri Nov 2 19:04:54 2012 -0400
Refactor otr_key.c to key.c
Change name space, standardize code and fix callsite.
Signed-off-by: David Goulet <dgoulet at ev0ke.net>
---
src/Makefile.am | 2 +-
src/io_util.c | 6 +-
src/{otr_key.c => key.c} | 212 +++++++++++++++++++++++------------------------
src/key.h | 34 ++++++++
src/otr-ops.c | 4 +-
src/otr.c | 6 +-
src/otr.h | 15 ----
7 files changed, 148 insertions(+), 131 deletions(-)
diff --git a/src/Makefile.am b/src/Makefile.am
index 617f202..dd5fec6 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -15,5 +15,5 @@ INCLUDES = -I$(top_srcdir)/src $(IRSSI_INCLUDE)
lib_LTLIBRARIES = libotr.la
-libotr_la_SOURCES = otr_key.c otr.c otr-ops.c io_set.c io_util.c \
+libotr_la_SOURCES = key.c key.h otr.c otr-ops.c io_set.c io_util.c \
otr.h module.c irssi_otr.h otr-formats.c otr-formats.h
diff --git a/src/io_util.c b/src/io_util.c
index 4d1c71b..9e9627c 100644
--- a/src/io_util.c
+++ b/src/io_util.c
@@ -17,7 +17,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,USA
*/
-#include "otr.h"
+#include "key.h"
char *otr_status_txt[] = {
"FINISHED",
@@ -165,9 +165,9 @@ void cmd_genkey(IOUSTATE *ioustate, IRC_CTX *ircctx, int argc, char *argv[],
{
if (argc) {
if (strcmp(argv[0], "abort") == 0)
- keygen_abort(ioustate, FALSE);
+ key_generation_abort(ioustate, FALSE);
else if (strchr(argv[0], '@'))
- keygen_run(ioustate, argv[0]);
+ key_generation_run(ioustate, argv[0]);
else
otr_noticest(TXT_KG_NEEDACC);
} else {
diff --git a/src/otr_key.c b/src/key.c
similarity index 64%
rename from src/otr_key.c
rename to src/key.c
index 1082bc5..5caf431 100644
--- a/src/otr_key.c
+++ b/src/key.c
@@ -19,17 +19,15 @@
#define _GNU_SOURCE
-#include "otr.h"
-
#include <libgen.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/poll.h>
#include <signal.h>
-typedef enum { KEYGEN_NO, KEYGEN_RUNNING } keygen_status_t;
+#include "key.h"
-struct {
+static struct {
keygen_status_t status;
char *accountname;
char *protocol;
@@ -41,55 +39,58 @@ struct {
IOUSTATE *ioustate;
} kg_st = { .status = KEYGEN_NO };
-void keygen_childwatch(GPid pid, gint status, gpointer data)
+
+static void keygen_childwatch(GPid pid, gint status, gpointer data)
{
+ int ret;
struct pollfd pfd = {
.fd = g_io_channel_unix_get_fd(kg_st.ch[0]),
.events = POLLIN
};
- int ret;
/* nothing to do if keygen_complete has already been called */
- if (data)
- return;
+ if (data) {
+ goto end;
+ }
kg_st.pid = 0;
ret = poll(&pfd, 1, 0);
-
- /* data is there, let's wait for keygen_complete to be called */
- if (ret == 1)
+ if (ret == 1) {
+ /* data is there, let's wait for keygen_complete to be called */
return;
-
- /* no data, report error and reset kg_st */
- if (ret == 0) {
+ } else if (ret == 0) {
+ /* no data, report error and reset kg_st */
if (WIFSIGNALED(status)) {
char sigstr[16];
- sprintf(sigstr,
+ ret = snprintf(sigstr, sizeof(sigstr),
#ifndef HAVE_STRSIGNAL
- "%d", WTERMSIG(status));
+ "%d", WTERMSIG(status)
#else
- "%s", strsignal(WTERMSIG(status)));
+ "%s", strsignal(WTERMSIG(status))
#endif
- otr_noticest(TXT_KG_EXITSIG,
- kg_st.accountname,
- sigstr);
- }else
+ );
+ otr_noticest(TXT_KG_EXITSIG, kg_st.accountname, sigstr);
+ } else {
otr_noticest(TXT_KG_EXITED, kg_st.accountname);
- } else if (ret == -1)
- otr_noticest(TXT_KG_POLLERR, kg_st.accountname,
- strerror(errno));
+ }
+ } else if (ret < 0) {
+ otr_noticest(TXT_KG_POLLERR, kg_st.accountname, strerror(errno));
+ }
- keygen_abort(kg_st.ioustate, FALSE);
+ key_generation_abort(kg_st.ioustate, FALSE);
+
+end:
+ return;
}
/*
* Installed as g_io_watch and called when the key generation
* process finishs.
*/
-gboolean keygen_complete(GIOChannel *source, GIOCondition condition,
- gpointer data)
+static gboolean keygen_complete(GIOChannel *source, GIOCondition condition,
+ gpointer data)
{
gcry_error_t err;
const char *clconfdir = get_client_config_dir();
@@ -104,23 +105,20 @@ gboolean keygen_complete(GIOChannel *source, GIOCondition condition,
g_io_channel_unref(kg_st.ch[0]);
g_io_channel_unref(kg_st.ch[1]);
- if (err)
- otr_noticest(TXT_KG_FAILED,
- kg_st.accountname,
- gcry_strerror(err),
- gcry_strsource(err));
- else {
+ if (err) {
+ otr_noticest(TXT_KG_FAILED, kg_st.accountname, gcry_strerror(err),
+ gcry_strsource(err));
+ } else {
/* reload keys */
- otr_noticest(TXT_KG_COMPLETED,
- kg_st.accountname,
- time(NULL) - kg_st.started);
+ otr_noticest(TXT_KG_COMPLETED, kg_st.accountname,
+ time(NULL) - kg_st.started);
rename(tmpfilename, filename);
//otrl_privkey_forget_all(otr_state); <-- done by lib
key_load(kg_st.ioustate);
}
g_source_remove(kg_st.cwid);
- kg_st.cwid = g_child_watch_add(kg_st.pid, keygen_childwatch, (void*)1);
+ kg_st.cwid = g_child_watch_add(kg_st.pid, keygen_childwatch, (void*) 1);
kg_st.status = KEYGEN_NO;
g_free(kg_st.accountname);
@@ -132,45 +130,44 @@ gboolean keygen_complete(GIOChannel *source, GIOCondition condition,
}
/*
- * Run key generation in a seperate process (takes ages).
- * The other process will rewrite the key file, we shouldn't
- * change anything till it's done and we've reloaded the keys.
+ * Run key generation in a seperate process (takes ages). The other process
+ * will rewrite the key file, we shouldn't change anything till it's done and
+ * we've reloaded the keys.
*/
-void keygen_run(IOUSTATE *ioustate, const char *accname)
+void key_generation_run(IOUSTATE *ioustate, const char *accname)
{
gcry_error_t err;
int ret;
int fds[2];
- char *filename = g_strconcat(
- get_client_config_dir(), TMPKEYFILE,
- NULL), *filenamedup = g_strdup(
- filename);
+ char *filename = g_strconcat(get_client_config_dir(), TMPKEYFILE, NULL);
+ char *filenamedup = g_strdup(filename);
char *dir = dirname(filenamedup);
if (kg_st.status != KEYGEN_NO) {
- if (strcmp(accname, kg_st.accountname) != 0)
- otr_noticest(TXT_KG_ABORTED_DUP,
- accname, kg_st.accountname);
- return;
+ if (strcmp(accname, kg_st.accountname) != 0) {
+ otr_noticest(TXT_KG_ABORTED_DUP, accname, kg_st.accountname);
+ }
+ goto end;
}
if (!g_file_test(dir, G_FILE_TEST_EXISTS)) {
if (g_mkdir(dir, S_IRWXU)) {
- otr_noticest(TXT_KG_ABORTED_DIR,
- accname, dir, strerror(errno));
+ otr_noticest(TXT_KG_ABORTED_DIR, accname, dir, strerror(errno));
g_free(dir);
g_free(filename);
- return;
- } else
+ goto end;
+ } else {
otr_noticest(TXT_KG_MKDIR, dir);
+ }
}
+
g_free(filenamedup);
- if (pipe(fds) != 0) {
- otr_noticest(TXT_KG_PIPE,
- accname, strerror(errno));
+ ret = pipe(fds);
+ if (ret < 0) {
+ otr_noticest(TXT_KG_PIPE, accname, strerror(errno));
g_free(filename);
- return;
+ goto end;
}
kg_st.ch[0] = g_io_channel_unix_new(fds[0]);
@@ -184,45 +181,46 @@ void keygen_run(IOUSTATE *ioustate, const char *accname)
if ((ret = fork())) {
g_free(filename);
if (ret == -1) {
- otr_noticest(TXT_KG_FORK,
- accname, strerror(errno));
- return;
+ otr_noticest(TXT_KG_FORK, accname, strerror(errno));
+ goto end;
}
kg_st.status = KEYGEN_RUNNING;
kg_st.pid = ret;
- otr_noticest(TXT_KG_INITIATED,
- accname);
+ otr_noticest(TXT_KG_INITIATED, accname);
kg_st.cpid = g_io_add_watch(kg_st.ch[0], G_IO_IN,
- (GIOFunc)keygen_complete, NULL);
- kg_st.cwid = g_child_watch_add(kg_st.pid, keygen_childwatch,
- NULL);
-
+ (GIOFunc) keygen_complete, NULL);
+ kg_st.cwid = g_child_watch_add(kg_st.pid, keygen_childwatch, NULL);
kg_st.started = time(NULL);
- return;
+ goto end;
}
/* child */
err = otrl_privkey_generate(ioustate->otr_state, filename, accname,
- PROTOCOLID);
- write(fds[1], &err, sizeof(err));
+ PROTOCOLID);
+ (void) write(fds[1], &err, sizeof(err));
g_free(filename);
- _exit(0);
+
+ exit(EXIT_SUCCESS);
+
+end:
+ return;
}
/*
* Abort ongoing key generation.
*/
-void keygen_abort(IOUSTATE *ioustate, int ignoreidle)
+void key_generation_abort(IOUSTATE *ioustate, int ignoreidle)
{
if (kg_st.status != KEYGEN_RUNNING) {
- if (!ignoreidle)
+ if (!ignoreidle) {
otr_noticest(TXT_KG_NOABORT);
- return;
+ }
+ goto end;
}
otr_noticest(TXT_KG_ABORT, kg_st.accountname);
@@ -233,10 +231,13 @@ void keygen_abort(IOUSTATE *ioustate, int ignoreidle)
if (kg_st.pid != 0) {
kill(kg_st.pid, SIGTERM);
- g_child_watch_add(kg_st.pid, keygen_childwatch, (void*)1);
+ g_child_watch_add(kg_st.pid, keygen_childwatch, (void *) 1);
}
kg_st.status = KEYGEN_NO;
+
+end:
+ return;
}
/*
@@ -248,14 +249,13 @@ void otr_writefps(IOUSTATE *ioustate)
char *filename = g_strconcat(get_client_config_dir(), FPSFILE, NULL);
err = otrl_privkey_write_fingerprints(ioustate->otr_state, filename);
-
if (err == GPG_ERR_NO_ERROR) {
otr_noticest(TXT_FP_SAVED);
} else {
- otr_noticest(TXT_FP_SAVE_ERROR,
- gcry_strerror(err),
- gcry_strsource(err));
+ otr_noticest(TXT_FP_SAVE_ERROR, gcry_strerror(err),
+ gcry_strsource(err));
}
+
g_free(filename);
}
@@ -266,18 +266,16 @@ void otr_writefps(IOUSTATE *ioustate)
void otr_writeinstags(IOUSTATE *ioustate)
{
gcry_error_t err;
- char *filename = g_strconcat(
- get_client_config_dir(), INSTAGFILE, NULL);
+ char *filename = g_strconcat(get_client_config_dir(), INSTAGFILE, NULL);
err = otrl_instag_write(ioustate->otr_state, filename);
-
if (err == GPG_ERR_NO_ERROR) {
otr_noticest(TXT_INSTAG_SAVED);
} else {
- otr_noticest(TXT_INSTAG_SAVE_ERROR,
- gcry_strerror(err),
- gcry_strsource(err));
+ otr_noticest(TXT_INSTAG_SAVE_ERROR, gcry_strerror(err),
+ gcry_strsource(err));
}
+
g_free(filename);
}
#endif
@@ -292,47 +290,46 @@ void key_load(IOUSTATE *ioustate)
if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
otr_noticest(TXT_KEY_NOT_FOUND);
- return;
+ goto end;
}
err = otrl_privkey_read(ioustate->otr_state, filename);
-
if (err == GPG_ERR_NO_ERROR) {
otr_noticest(TXT_KEY_LOADED);
} else {
- otr_noticest(TXT_KEY_LOAD_ERROR,
- gcry_strerror(err),
- gcry_strsource(err));
+ otr_noticest(TXT_KEY_LOAD_ERROR, gcry_strerror(err),
+ gcry_strsource(err));
}
+
+end:
g_free(filename);
+ return;
}
/*
* Load fingerprints.
*/
-void fps_load(IOUSTATE *ioustate)
+void key_load_fingerprints(IOUSTATE *ioustate)
{
gcry_error_t err;
char *filename = g_strconcat(get_client_config_dir(), FPSFILE, NULL);
-
if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
otr_noticest(TXT_FP_NOT_FOUND);
- return;
+ goto end;
}
- err =
- otrl_privkey_read_fingerprints(ioustate->otr_state, filename,
- NULL,
- NULL);
-
+ err = otrl_privkey_read_fingerprints(ioustate->otr_state, filename, NULL,
+ NULL);
if (err == GPG_ERR_NO_ERROR) {
otr_noticest(TXT_FP_LOADED);
} else {
- otr_noticest(TXT_FP_LOAD_ERROR,
- gcry_strerror(err),
- gcry_strsource(err));
+ otr_noticest(TXT_FP_LOAD_ERROR, gcry_strerror(err),
+ gcry_strsource(err));
}
+
+end:
g_free(filename);
+ return;
}
#ifndef LIBOTR3
@@ -342,23 +339,24 @@ void fps_load(IOUSTATE *ioustate)
void instag_load(IOUSTATE *ioustate)
{
gcry_error_t err;
- char *filename = g_strconcat(
- get_client_config_dir(), INSTAGFILE, NULL);
+ char *filename = g_strconcat(get_client_config_dir(), INSTAGFILE, NULL);
if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
otr_noticest(TXT_INSTAG_NOT_FOUND);
- return;
+ g_free(filename);
+ goto end;
}
err = otrl_instag_read(ioustate->otr_state, filename);
-
if (err == GPG_ERR_NO_ERROR) {
otr_noticest(TXT_INSTAG_LOADED);
} else {
- otr_noticest(TXT_INSTAG_LOAD_ERROR,
- gcry_strerror(err),
- gcry_strsource(err));
+ otr_noticest(TXT_INSTAG_LOAD_ERROR, gcry_strerror(err),
+ gcry_strsource(err));
}
+
+end:
g_free(filename);
+ return;
}
#endif
diff --git a/src/key.h b/src/key.h
new file mode 100644
index 0000000..7afcfef
--- /dev/null
+++ b/src/key.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2012 - David Goulet <dgoulet at ev0ke.net>
+ *
+ * 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
+ */
+
+#ifndef IRSSI_OTR_KEY_H
+#define IRSSI_OTR_KEY_H
+
+#include "otr.h"
+
+typedef enum { KEYGEN_NO, KEYGEN_RUNNING } keygen_status_t;
+
+void key_generation_abort(IOUSTATE *ioustate, int ignoreidle);
+void key_generation_run(IOUSTATE *ioustate, const char *accname);
+void key_load(IOUSTATE *ioustate);
+void key_load_fingerprints(IOUSTATE *ioustate);
+void otr_writefps(IOUSTATE *ioustate);
+void otr_writeinstags(IOUSTATE *ioustate);
+void instag_load(IOUSTATE *ioustate);
+
+#endif /* IRSSI_OTR_KEY_H */
diff --git a/src/otr-ops.c b/src/otr-ops.c
index 6b09f68..0f29721 100644
--- a/src/otr-ops.c
+++ b/src/otr-ops.c
@@ -17,7 +17,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,USA
*/
-#include "otr.h"
+#include "key.h"
OtrlPolicy IO_DEFAULT_OTR_POLICY =
OTRL_POLICY_MANUAL | OTRL_POLICY_WHITESPACE_START_AKE;
@@ -89,7 +89,7 @@ static void ops_create_privkey(void *opdata, const char *accountname,
{
IRC_CTX *ircctx __attribute__((unused)) = opdata;
- keygen_run(IRCCTX_IO_US(ircctx), accountname);
+ key_generation_run(IRCCTX_IO_US(ircctx), accountname);
}
/*
diff --git a/src/otr.c b/src/otr.c
index 5a684a3..3751200 100644
--- a/src/otr.c
+++ b/src/otr.c
@@ -19,7 +19,7 @@
* Franklin Street, Fifth Floor, Boston, MA 02110-1301,USA
*/
-#include "otr.h"
+#include "key.h"
#include <gcrypt.h>
@@ -41,14 +41,14 @@ IOUSTATE *otr_init_user(char *user)
instag_load(ioustate);
#endif
key_load(ioustate);
- fps_load(ioustate);
+ key_load_fingerprints(ioustate);
return ioustate;
}
void otr_deinit_user(IOUSTATE *ioustate)
{
- keygen_abort(ioustate, TRUE);
+ key_generation_abort(ioustate, TRUE);
if (ioustate->otr_state) {
otr_writefps(ioustate);
diff --git a/src/otr.h b/src/otr.h
index b08e04b..5b7f179 100644
--- a/src/otr.h
+++ b/src/otr.h
@@ -213,21 +213,6 @@ void otr_abort_auth(ConnContext *co, IRC_CTX *ircctx, const char *nick);
struct ctxlist_ *otr_contexts(IOUSTATE *ioustate);
void otr_finishall(IOUSTATE *ioustate);
-
-/* key/fingerprint stuff */
-
-void keygen_run(IOUSTATE *ioustate, const char *accname);
-void keygen_abort(IOUSTATE *ioustate, int ignoreidle);
-void key_load(IOUSTATE *ioustate);
-void fps_load(IOUSTATE *ioustate);
-void otr_writefps(IOUSTATE *ioustate);
-
-#ifndef LIBOTR3
-/* instance tags */
-void instag_load(IOUSTATE *ioustate);
-void otr_writeinstags(IOUSTATE *ioustate);
-#endif
-
int extract_nick(char *nick, char *line);
struct _cmds {
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-privacy/packages/irssi-plugin-otr.git
More information about the Pkg-privacy-commits
mailing list