2 * An example plugin that demonstrates exporting of a cipher object
3 * type to be used in another plugin.
5 * This plugin only provides the CaesarCipher type. See caesarcipher_consumer
8 * Copyright (C) 2013, Ankit Vani <a@nevitus.org>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 /* When writing a third-party plugin, do not include libpurple's internal.h
27 * included below. This file is for internal libpurple use only. We're including
28 * it here for our own convenience. */
31 /* This file defines PURPLE_PLUGINS and includes all the libpurple headers */
34 #include "caesarcipher.h"
36 #define CAESAR_CIPHER_GET_PRIVATE(obj) \
37 (G_TYPE_INSTANCE_GET_PRIVATE((obj), CAESAR_TYPE_CIPHER, CaesarCipherPrivate))
41 } CaesarCipherPrivate
;
49 /******************************************************************************
51 *****************************************************************************/
53 caesar_shift(const guchar input
[], size_t in_len
, guchar output
[], gint8 offset
)
57 for (i
= 0; i
< in_len
; ++i
) {
58 if (input
[i
] >= 'a' && input
[i
] <= 'z')
59 output
[i
] = (((input
[i
] - 'a') + offset
+ 26) % 26) + 'a';
60 else if (input
[i
] >= 'A' && input
[i
] <= 'Z')
61 output
[i
] = (((input
[i
] - 'A') + offset
+ 26) % 26) + 'A';
70 caesar_cipher_set_offset(PurpleCipher
*cipher
, gint8 offset
)
72 CaesarCipherPrivate
*priv
= CAESAR_CIPHER_GET_PRIVATE(cipher
);
74 priv
->offset
= offset
% 26;
78 caesar_cipher_encrypt(PurpleCipher
*cipher
, const guchar input
[], size_t in_len
,
79 guchar output
[], size_t out_size
)
81 CaesarCipherPrivate
*priv
= CAESAR_CIPHER_GET_PRIVATE(cipher
);
83 g_return_val_if_fail(out_size
> in_len
, -1);
85 caesar_shift(input
, in_len
, output
, priv
->offset
);
91 caesar_cipher_decrypt(PurpleCipher
*cipher
, const guchar input
[], size_t in_len
,
92 guchar output
[], size_t out_size
)
94 CaesarCipherPrivate
*priv
= CAESAR_CIPHER_GET_PRIVATE(cipher
);
96 g_return_val_if_fail(out_size
> in_len
, -1);
98 caesar_shift(input
, in_len
, output
, -priv
->offset
);
104 caesar_cipher_set_key(PurpleCipher
*cipher
, const guchar
*key
, size_t len
)
106 caesar_cipher_set_offset(cipher
, len
);
109 /******************************************************************************
111 *****************************************************************************/
113 caesar_cipher_set_property(GObject
*obj
, guint param_id
, const GValue
*value
,
116 PurpleCipher
*cipher
= PURPLE_CIPHER(obj
);
120 caesar_cipher_set_offset(cipher
, g_value_get_schar(value
));
123 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj
, param_id
, pspec
);
128 /* initialize the cipher object. used in PURPLE_DEFINE_TYPE. */
130 caesar_cipher_init(CaesarCipher
*cipher
)
132 /* classic caesar cipher uses a shift of 3 */
133 CAESAR_CIPHER_GET_PRIVATE(cipher
)->offset
= 3;
136 /* initialize the cipher class. used in PURPLE_DEFINE_TYPE. */
138 caesar_cipher_class_init(PurpleCipherClass
*klass
)
140 GObjectClass
*obj_class
= G_OBJECT_CLASS(klass
);
142 obj_class
->set_property
= caesar_cipher_set_property
;
143 g_type_class_add_private(obj_class
, sizeof(CaesarCipherPrivate
));
145 klass
->encrypt
= caesar_cipher_encrypt
;
146 klass
->decrypt
= caesar_cipher_decrypt
;
147 klass
->set_key
= caesar_cipher_set_key
;
149 g_object_class_install_property(obj_class
, PROP_OFFSET
,
150 g_param_spec_char("offset", "offset",
151 "The offset by which to shift alphabets",
158 * define the CaesarCipher type. this function defines
159 * caesar_cipher_get_type() and caesar_cipher_register_type().
161 PURPLE_DEFINE_TYPE(CaesarCipher
, caesar_cipher
, PURPLE_TYPE_CIPHER
);
163 G_MODULE_EXPORT PurpleCipher
*
164 caesar_cipher_new(void)
166 return g_object_new(CAESAR_TYPE_CIPHER
, NULL
);
169 /******************************************************************************
171 *****************************************************************************/
172 static PurplePluginInfo
*
173 plugin_query(GError
**error
)
175 const gchar
* const authors
[] = {
176 "Ankit Vani <a@nevitus.org>",
180 return purple_plugin_info_new(
181 "id", "core-caesarcipher",
182 "name", N_("Caesar Cipher Provider Example"),
183 "version", DISPLAY_VERSION
,
184 "category", N_("Example"),
185 "summary", N_("An example plugin that demonstrates exporting of a "
187 "description", N_("An example plugin that demonstrates exporting of "
188 "a cipher object type to be used in another "
191 "website", PURPLE_WEBSITE
,
192 "abi-version", PURPLE_ABI_VERSION
,
198 plugin_load(PurplePlugin
*plugin
, GError
**error
)
200 /* register the CaesarCipher type in the type system */
201 caesar_cipher_register_type(plugin
);
207 plugin_unload(PurplePlugin
*plugin
, GError
**error
)
212 PURPLE_PLUGIN_INIT(caesarcipher
, plugin_query
, plugin_load
, plugin_unload
);