/soc/2013/ankitkv/gobjectification: d757fbddbe3d: Added example ...

Ankit Vani a at nevitus.org
Fri Sep 20 12:41:34 EDT 2013


Changeset: d757fbddbe3d3b32919c4bc92aadd8766f723a8e
Author:	 Ankit Vani <a at nevitus.org>
Date:	 2013-09-20 17:53 +0530
Branch:	 soc.2013.gobjectification.plugins
URL: https://hg.pidgin.im/soc/2013/ankitkv/gobjectification/rev/d757fbddbe3d

Description:

Added example plugins caesarcipher and caesarcipher_consumer

diffstat:

 libpurple/plugins/Makefile.am             |    8 +
 libpurple/plugins/caesarcipher.c          |  212 ++++++++++++++++++++++++++++++
 libpurple/plugins/caesarcipher.h          |   68 +++++++++
 libpurple/plugins/caesarcipher_consumer.c |   75 ++++++++++
 4 files changed, 363 insertions(+), 0 deletions(-)

diffs (truncated from 409 to 300 lines):

diff --git a/libpurple/plugins/Makefile.am b/libpurple/plugins/Makefile.am
--- a/libpurple/plugins/Makefile.am
+++ b/libpurple/plugins/Makefile.am
@@ -27,6 +27,8 @@ plugindir = $(libdir)/purple-$(PURPLE_MA
 
 autoaccept_la_LDFLAGS       = -module -avoid-version
 buddynote_la_LDFLAGS        = -module -avoid-version
+caesarcipher_la_LDFLAGS     = -module -avoid-version
+caesarcipher_consumer_la_LDFLAGS = -module -avoid-version
 ciphertest_la_LDFLAGS		= -module -avoid-version
 codeinline_la_LDFLAGS		= -module -avoid-version
 debug_example_la_LDFLAGS    = -module -avoid-version
@@ -62,6 +64,8 @@ plugin_LTLIBRARIES = \
 	$(DBUS_LTLIB)
 
 noinst_LTLIBRARIES = \
+	caesarcipher.la \
+	caesarcipher_consumer.la \
 	ciphertest.la \
 	codeinline.la \
 	debug_example.la \
@@ -74,6 +78,8 @@ noinst_LTLIBRARIES = \
 
 autoaccept_la_SOURCES       = autoaccept.c
 buddynote_la_SOURCES        = buddynote.c
+caesarcipher_la_SOURCES     = caesarcipher.c caesarcipher.h
+caesarcipher_consumer_la_SOURCES = caesarcipher_consumer.c
 ciphertest_la_SOURCES		= ciphertest.c
 codeinline_la_SOURCES		= codeinline.c
 debug_example_la_SOURCES = debug_example.c
@@ -93,6 +99,8 @@ statenotify_la_SOURCES      = statenotif
 
 autoaccept_la_LIBADD        = $(GLIB_LIBS)
 buddynote_la_LIBADD         = $(GLIB_LIBS)
+caesarcipher_la_LIBADD      = $(GLIB_LIBS)
+caesarcipher_consumer_la_LIBADD = $(GLIB_LIBS)
 ciphertest_la_LIBADD		= $(GLIB_LIBS)
 codeinline_la_LIBADD		= $(GLIB_LIBS)
 idle_la_LIBADD              = $(GLIB_LIBS)
diff --git a/libpurple/plugins/caesarcipher.c b/libpurple/plugins/caesarcipher.c
new file mode 100644
--- /dev/null
+++ b/libpurple/plugins/caesarcipher.c
@@ -0,0 +1,212 @@
+/*
+ * An example plugin that demonstrates exporting of a cipher object
+ * type to be used in another plugin.
+ *
+ * This plugin only provides the CaesarCipher type. See caesarcipher_consumer
+ * plugin for its use.
+ *
+ * Copyright (C) 2013, Ankit Vani <a at nevitus.org>
+ *
+ * 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
+ * 02111-1301, USA.
+ */
+
+#include "internal.h"
+#include "caesarcipher.h"
+
+#define CAESAR_CIPHER_GET_PRIVATE(obj) \
+	(G_TYPE_INSTANCE_GET_PRIVATE((obj), CAESAR_TYPE_CIPHER, CaesarCipherPrivate))
+
+typedef struct {
+	gint8 offset;
+} CaesarCipherPrivate;
+
+enum {
+	PROP_NONE,
+	PROP_OFFSET,
+	PROP_LAST,
+};
+
+/******************************************************************************
+ * Cipher stuff
+ *****************************************************************************/
+static void
+caesar_shift(const guchar input[], size_t in_len, guchar output[], gint8 offset)
+{
+	size_t i;
+
+	for (i = 0; i < in_len; ++i) {
+		if (input[i] >= 'a' && input[i] <= 'z')
+			output[i] = (((input[i] - 'a') + offset) % 26) + 'a';
+		else if (input[i] >= 'A' && input[i] <= 'Z')
+			output[i] = (((input[i] - 'A') + offset) % 26) + 'A';
+	}
+}
+
+static void
+caesar_cipher_set_offset(PurpleCipher *cipher, gint8 offset)
+{
+	CaesarCipherPrivate *priv = CAESAR_CIPHER_GET_PRIVATE(cipher);
+
+	priv->offset = offset % 26;
+}
+
+static ssize_t
+caesar_cipher_encrypt(PurpleCipher *cipher, const guchar input[], size_t in_len,
+		guchar output[], size_t out_size)
+{
+	CaesarCipherPrivate *priv = CAESAR_CIPHER_GET_PRIVATE(cipher);
+
+	g_return_val_if_fail(out_size >= in_len, -1);
+
+	caesar_shift(input, in_len, output, priv->offset);
+
+	return in_len;
+}
+
+static ssize_t
+caesar_cipher_decrypt(PurpleCipher *cipher, const guchar input[], size_t in_len,
+		guchar output[], size_t out_size)
+{
+	CaesarCipherPrivate *priv = CAESAR_CIPHER_GET_PRIVATE(cipher);
+
+	g_return_val_if_fail(out_size >= in_len, -1);
+
+	caesar_shift(input, in_len, output, -priv->offset);
+
+	return in_len;
+}
+
+static void
+caesar_cipher_set_key(PurpleCipher *cipher, const guchar *key, size_t len)
+{
+	caesar_cipher_set_offset(cipher, GPOINTER_TO_INT(key));
+}
+
+static const gchar*
+caesar_cipher_get_name(PurpleCipher *cipher)
+{
+	return "caesar";
+}
+
+/******************************************************************************
+ * Object stuff
+ *****************************************************************************/
+static void
+caesar_cipher_set_property(GObject *obj, guint param_id, const GValue *value,
+		GParamSpec *pspec)
+{
+	PurpleCipher *cipher = PURPLE_CIPHER(obj);
+
+	switch(param_id) {
+		case PROP_OFFSET:
+#if GLIB_CHECK_VERSION(2, 32, 0)
+			caesar_cipher_set_offset(cipher, g_value_get_schar(value));
+#else
+			caesar_cipher_set_offset(cipher, g_value_get_char(value));
+#endif
+			break;
+		default:
+			G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
+			break;
+	}
+}
+
+/* initialize the cipher object. used in PURPLE_DEFINE_TYPE. */
+static void
+caesar_cipher_init(CaesarCipher *cipher)
+{
+	/* classic caesar cipher uses a shift of 3 */
+	CAESAR_CIPHER_GET_PRIVATE(cipher)->offset = 3;
+}
+
+/* initialize the cipher class. used in PURPLE_DEFINE_TYPE. */
+static void
+caesar_cipher_class_init(PurpleCipherClass *klass)
+{
+	GObjectClass *obj_class = G_OBJECT_CLASS(klass);
+
+	obj_class->set_property = caesar_cipher_set_property;
+	g_type_class_add_private(obj_class, sizeof(CaesarCipherPrivate));
+
+	klass->encrypt  = caesar_cipher_encrypt;
+	klass->decrypt  = caesar_cipher_decrypt;
+	klass->set_key  = caesar_cipher_set_key;
+	klass->get_name = caesar_cipher_get_name;
+
+	g_object_class_install_property(obj_class, PROP_OFFSET,
+		g_param_spec_char("offset", "offset",
+			"The offset by which to shift alphabets",
+			-25, 25, 3,
+			G_PARAM_WRITABLE)
+	);
+}
+
+/*
+ * define the CaesarCipher type. this function defines
+ * caesar_cipher_get_type() and caesar_cipher_register_type().
+ */
+PURPLE_DEFINE_TYPE(CaesarCipher, caesar_cipher, PURPLE_TYPE_CIPHER);
+
+PurpleCipher *
+caesar_cipher_new(void)
+{
+	return g_object_new(CAESAR_TYPE_CIPHER, NULL);
+}
+
+/******************************************************************************
+ * Plugin stuff
+ *****************************************************************************/
+static PurplePluginInfo *
+plugin_query(GError **error)
+{
+	const gchar * const authors[] = {
+		"Ankit Vani <a at nevitus.org>",
+		NULL
+	};
+
+	return purple_plugin_info_new(
+		"id",           "core-caesarcipher",
+		"name",         N_("Caesar Cipher Provider Example"),
+		"version",      DISPLAY_VERSION,
+		"category",     N_("Example"),
+		"summary",      N_("An example plugin that demonstrates exporting of a "
+		                   "cipher type."),
+		"description",  N_("An example plugin that demonstrates exporting of "
+		                   "a cipher object type to be used in another "
+		                   "plugin."),
+		"authors",      authors,
+		"website",      PURPLE_WEBSITE,
+		"abi-version",  PURPLE_ABI_VERSION,
+		NULL
+	);
+}
+
+static gboolean
+plugin_load(PurplePlugin *plugin, GError **error)
+{
+	/* register the CaesarCipher type in the type system */
+	caesar_cipher_register_type(plugin);
+
+	return TRUE;
+}
+
+static gboolean
+plugin_unload(PurplePlugin *plugin, GError **error)
+{
+	return TRUE;
+}
+
+PURPLE_PLUGIN_INIT(caesarcipher, plugin_query, plugin_load, plugin_unload);
diff --git a/libpurple/plugins/caesarcipher.h b/libpurple/plugins/caesarcipher.h
new file mode 100644
--- /dev/null
+++ b/libpurple/plugins/caesarcipher.h
@@ -0,0 +1,68 @@
+/*
+ * An example plugin that demonstrates exporting of a cipher object
+ * type to be used in another plugin.
+ *
+ * Copyright (C) 2013, Ankit Vani <a at nevitus.org>
+ *
+ * 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
+ * 02111-1301, USA.
+ */
+
+#ifndef _CAESAR_CIPHER_H_
+#define _CAESAR_CIPHER_H_
+
+#include "cipher.h"
+#include "plugins.h"
+
+#define CAESAR_TYPE_CIPHER             (caesar_cipher_get_type())
+#define CAESAR_CIPHER(obj)             (G_TYPE_CHECK_INSTANCE_CAST((obj), CAESAR_TYPE_CIPHER, CaesarCipher))
+#define CAESAR_CIPHER_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST((klass), CAESAR_TYPE_CIPHER, CaesarCipherClass))
+#define CAESAR_IS_CIPHER(obj)          (G_TYPE_CHECK_INSTANCE_TYPE((obj), CAESAR_TYPE_CIPHER))
+#define CAESAR_IS_CIPHER_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE((klass), CAESAR_TYPE_CIPHER))
+#define CAESAR_CIPHER_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS((obj), CAESAR_TYPE_CIPHER, CaesarCipherClass))
+
+typedef struct _CaesarCipher CaesarCipher;
+typedef struct _CaesarCipherClass CaesarCipherClass;
+
+/*



More information about the Commits mailing list