-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb64r13.c
219 lines (175 loc) · 5.71 KB
/
b64r13.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* Base64-Rot13 encoder/decoder for Pidgin
* Copyright (C) 2015 tPenguinLTG
*
* 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 PURPLE_PLUGINS
# define PURPLE_PLUGINS
#endif
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <glib.h>
#include <string.h>
#include <version.h>
#include <plugin.h>
#include <conversation.h>
#include <cmds.h>
/* static PurplePlugin *purple_b64r13 = NULL; */
static PurpleCmdId b64r13_cmd_id, b64r13d_cmd_id;
/**
* Encodes the given string using the Rot13 algorithm.
* @param s the string to encode
* @return the string encoded with Rot13
*/
static gchar*
rot13(gchar *s)
{
int i=0;
for (i=0; s[i]!='\0'; i++) {
/* if lowercase */
if ('a' <= s[i] && s[i] <= 'z') {
s[i]=(s[i]-'a'+13) % 26 + 'a';
}
/* else if uppercase */
else if ('A' <= s[i] && s[i] <= 'Z') {
s[i]=(s[i]-'A'+13) % 26 + 'A';
}
/* else not a letter; do nothing */
}/* end for */
return s;
}/* end rot13(gchar*) */
#if 0
/**
* Encodes the given string using Base64, then Rot13.
* @param s the string to encode
* @return the encoded string
*/
static gchar*
encode_b64r13(gchar *s)
{
/* TODO */
return s;
}/* end encode_b64r13(gchar*) */
/**
* Decodes the given Base64-Rot13 encoded string.
* @param the string to decode
* @return the decoded string
*/
static gchar*
decode_b64r13(gchar *s)
{
/* TODO */
return s;
}/* end decode_b64r13(gchar*) */
#endif
/**
* Callback function for b64r13.
*/
static PurpleCmdRet
b64r13_cb(PurpleConversation *conv, const gchar *cmd, const gchar **args, gchar **error, void *data)
{
guchar *encoded_str = rot13(g_base64_encode(args[0], strlen(args[0])));
/* output to conversation */
if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM) {
purple_conv_im_send(PURPLE_CONV_IM(conv), encoded_str);
/* output original message as system message */
purple_conv_im_write(PURPLE_CONV_IM(conv), NULL,
args[0], PURPLE_MESSAGE_SYSTEM, time(NULL));
} else if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT) {
purple_conv_chat_send(PURPLE_CONV_CHAT(conv), encoded_str);
/* output original message as system message */
purple_conv_chat_write(PURPLE_CONV_CHAT(conv), NULL,
args[0], PURPLE_MESSAGE_SYSTEM, time(NULL));
}/* end if */
g_free(encoded_str);
return PURPLE_CMD_RET_OK;
}/* end b64r13d_cmd_id */
/**
* Callback function for b64r13d
*/
static PurpleCmdRet
b64r13_decode_cb(PurpleConversation *conv, const gchar *cmd, gchar **args, gchar **error, void *data)
{
gsize decoded_length=0;
gchar *decoded_raw = g_base64_decode(rot13(args[0]), &decoded_length);
char decoded_str[decoded_length+1];
strncpy(decoded_str, decoded_raw, decoded_length);
time_t _time=time(NULL);
if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM) {
purple_conv_im_write(PURPLE_CONV_IM(conv), NULL,
decoded_str, PURPLE_MESSAGE_NO_LOG | PURPLE_MESSAGE_SYSTEM, _time);
} else if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT) {
purple_conv_chat_write(PURPLE_CONV_CHAT(conv), NULL,
decoded_str, PURPLE_MESSAGE_NO_LOG | PURPLE_MESSAGE_SYSTEM, _time);
}/* end if */
g_free(decoded_raw);
return PURPLE_CMD_RET_OK;
}/* end b64r13_decode_cb */
static gboolean
plugin_load(PurplePlugin *plugin)
{
b64r13_cmd_id = purple_cmd_register("b64r13", "s", PURPLE_CMD_P_DEFAULT,
PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
b64r13_cb,
"b64r13 <message>: Send a message encoded with Base64 and Rot13.",
NULL);
b64r13d_cmd_id = purple_cmd_register("b64r13d", "s", PURPLE_CMD_P_DEFAULT,
PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
b64r13_decode_cb,
"b64r13d <encoded message>: Decode a message encoded with Base64 and Rot13.",
NULL);
return TRUE;
}/* end plugin_load(PurplePlugin*) */
static gboolean
plugin_unload(PurplePlugin *plugin)
{
purple_cmd_unregister(b64r13_cmd_id);
return TRUE;
}/* end plugin_load(PurplePlugin*) */
static PurplePluginInfo info = {
PURPLE_PLUGIN_MAGIC,
PURPLE_MAJOR_VERSION,
PURPLE_MINOR_VERSION,
PURPLE_PLUGIN_STANDARD,
NULL,
0,
NULL,
PURPLE_PRIORITY_DEFAULT,
"core-tpenguinltg-b64r13",
"Base64-Rot13 Encode",
"0.2.0-beta",
"Encodes message using Base64 and Rot13.",
"Encodes message using Base64, then encodes the result using Rot13."
" Adds the /b64r13 command for encoding and /b64r13d command for decoding.",
"tPenguinLTG <[email protected]>",
"https://github.com/tpenguinltg/purple-b64r13",
plugin_load,
plugin_unload,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
static void
init_plugin(PurplePlugin *plugin)
{
}/* end init_plugin(PurplePlugin*) */
PURPLE_INIT_PLUGIN(b64r13, init_plugin, info)