/
xmpp_uri.c
354 lines (290 loc) · 11.4 KB
/
xmpp_uri.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* Copyright 2013 James Ravn
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#define PURPLE_PLUGINS
#include <assert.h>
#include <glib.h>
#include <string.h>
#include "account.h"
#include "conversation.h"
#include "core.h"
#include "debug.h"
#include "eventloop.h"
#include "gtkconv.h"
#include "gtkplugin.h"
#include "gtkimhtml.h"
#include "plugin.h"
#include "version.h"
#define PLUGIN_ID "gtk-jravn-xmppuri"
#define PLUGIN_NAME "XMPP URI Handler"
#define PLUGIN_STATIC_NAME "xmpp_uri"
#define PLUGIN_VERSION "0.3-dev"
#define PLUGIN_SUMMARY "Handler for inline XMPP URI links (XEP-0147)."
#define PLUGIN_DESCRIPTION "This plugin supports clicking of XMPP URI links. "\
"The URI scheme is defined by XEP-0147. "\
"Links take the form of "\
"'xmpp:user@domain.com?query;param=value'. The "\
"supported queries are:\n\n"\
" - ?message;body=hello\%20buddy\n"\
" - ?join;body=hello\%20chat\n"
#define PLUGIN_AUTHOR "James Ravn <james.ravn@gmail.com>"
#define PLUGIN_WEBSITE "https://github.com/jsravn/xmpp-uri"
static PurplePlugin *plugin_instance = NULL;
// Methods
static gint match_proto(const void *acct, const void *proto) {
const char *acct_proto = purple_account_get_protocol_name(acct);
return g_strcmp0(acct_proto, proto);
}
/**
* @Nullable if no active accounts
*/
static PurpleAccount *find_acct(GtkIMHtml *imhtml) {
const char *proto = gtk_imhtml_get_protocol_name(imhtml);
GList *accts = purple_accounts_get_all_active();
if (!accts) {
purple_debug_warning(PLUGIN_ID, "No active accounts found\n");
return NULL;
}
PurpleAccount *acct = NULL;
GList *acct_item = g_list_find_custom(accts, proto, match_proto);
if (!acct_item) {
purple_debug_warning(PLUGIN_ID, "No account for %s found, using first"\
"\n", proto);
acct = g_list_first(accts)->data;
} else {
acct = acct_item->data;
}
g_list_free(accts);
return acct;
}
/**
* Creates a hash table of param to value pairs. The returned
* hash table should be destroyed by the calling function.
*/
static GHashTable *find_params(const char *query) {
GHashTable *params_table = g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, g_free);
const char *start_of_params = strchr(query, ';');
if (!start_of_params) return params_table;
char **params = g_strsplit(start_of_params, ";", G_MAXINT);
for (char **next = params; *next != NULL; next++) {
char **key_value = g_strsplit(*next, "=", 2);
const char *key = key_value[0];
const char *value = (key ? key_value[1] : NULL);
if (key && value) {
g_hash_table_insert(params_table, g_uri_unescape_string(key, ""),
g_uri_unescape_string(value, ""));
}
g_strfreev(key_value);
}
g_strfreev(params);
return params_table;
}
static gboolean handle_message(PurpleAccount *acct, const char *jid,
GHashTable *params) {
const char *body = g_hash_table_lookup(params, "body");
PurpleConversation *conv = purple_conversation_new(PURPLE_CONV_TYPE_IM,
acct, jid);
purple_conversation_present(conv);
if (body) {
purple_conv_send_confirm(conv, body);
}
return TRUE;
}
typedef struct {
PurpleAccount *acct;
char *jid;
char *body;
guint tries;
} FocusContext;
static FocusContext *new_focus_context(PurpleAccount *acct, const char *jid,
const char *body) {
FocusContext *context = g_new0(FocusContext, 1);
context->acct = acct;
context->jid = g_strdup(jid);
context->body = g_strdup(body);
context->tries = 0;
return context;
}
static void free_focus_context(FocusContext *context) {
g_free(context->jid);
g_free(context->body);
g_free(context);
}
static gboolean focus_chat(gpointer data) {
FocusContext *context = data;
PurpleConversation *conv = purple_find_conversation_with_account(
PURPLE_CONV_TYPE_CHAT, context->jid, context->acct);
if (!conv) {
if (context->tries++ > 50) {
purple_debug_misc(PLUGIN_ID, "Unable to focus %s [%s]\n",
context->jid,
purple_account_get_username(context->acct));
free_focus_context(context);
return FALSE;
} else {
return TRUE;
}
}
purple_debug_misc(PLUGIN_ID, "Found %s [%s], raising\n",
context->jid, purple_account_get_username(context->acct));
// XXX: Can't use purple_conversation_present because it's ignored
// when not in a user event state.
PidginConversation *gtkconv = PIDGIN_CONVERSATION(conv);
pidgin_conv_window_switch_gtkconv(gtkconv->win, gtkconv);
if (context->body) {
purple_conv_send_confirm(conv, context->body);
}
free_focus_context(context);
return FALSE;
}
static gboolean handle_join(PurpleAccount *acct, const char *jid,
GHashTable *params) {
PurpleConnection *con = purple_account_get_connection(acct);
PurplePlugin *prpl = purple_find_prpl(purple_account_get_protocol_id(acct));
PurplePluginProtocolInfo *prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);
if (!(prpl_info->chat_info_defaults && prpl_info->join_chat)) {
purple_debug_warning(PLUGIN_ID, "%s doesn't support chats\n",
purple_account_get_username(acct));
return FALSE;
}
GHashTable *chat_params = prpl_info->chat_info_defaults(con, jid);
prpl_info->join_chat(con, chat_params);
// XXX: Since the chat window is created in a callback we have to
// focus it later in the event loop.
const char *body = g_hash_table_lookup(params, "body");
FocusContext *context = new_focus_context(acct, jid, body);
purple_timeout_add(20, focus_chat, context);
return TRUE;
}
static void append_key_value(gpointer key, gpointer value, gpointer s) {
g_string_append(s, "[");
g_string_append(s, key);
g_string_append(s, " -> ");
g_string_append(s, value);
g_string_append(s, "] ");
}
static char *to_string(GHashTable *params) {
GString *s = g_string_new("");
g_hash_table_foreach(params, append_key_value, s);
return g_string_free(s, FALSE);
}
static void log_action(PurpleAccount *acct, const char *jid,
const char *action, GHashTable *params) {
purple_debug_misc(PLUGIN_ID, "Account: %s\n",
purple_account_get_username(acct));
purple_debug_misc(PLUGIN_ID, "Jid: %s\n", jid);
purple_debug_misc(PLUGIN_ID, "Action: %s\n", action);
char *params_str = to_string(params);
purple_debug_misc(PLUGIN_ID, "Parameters: %s\n", params_str);
g_free(params_str);
}
static gboolean handle_action(PurpleAccount *acct, const char *jid,
const char *action, GHashTable *params) {
log_action(acct, jid, action, params);
if (g_str_equal(action, "?message")) {
return handle_message(acct, jid, params);
} else if (g_str_equal(action, "?join")) {
return handle_join(acct, jid, params);
} else {
purple_debug_warning(PLUGIN_ID, "Unrecognized action, doing nothing");
return FALSE;
}
}
static gboolean handle_uri(GtkIMHtml *imhtml, GtkIMHtmlLink *link) {
const char *url = gtk_imhtml_link_get_url(link);
purple_debug_misc(PLUGIN_ID, "XMPP URI clicked: '%s'\n", url);
if (!g_str_has_prefix(url, "xmpp:")) {
purple_debug_error(PLUGIN_ID, "bug: unexpected protocol");
return FALSE;
}
char *query = strchr(url, '?');
if (!query) {
purple_debug_warning(PLUGIN_ID, "No query, ignoring. Did you forget a "
"'?message' ?");
return FALSE;
}
PurpleAccount *acct = find_acct(imhtml);
if (!acct) {
return FALSE;
}
const char *start_of_jid = url + strlen("xmpp:");
char *jid = g_strndup(start_of_jid, query - start_of_jid);
const char *end_of_action = strchr(query, ';');
if (!end_of_action) end_of_action = strchr(query, '\0');
char *action = g_strndup(query, end_of_action - query);
GHashTable *params = find_params(query);
gboolean r = handle_action(acct, jid, action, params);
g_free(jid);
g_free(action);
g_hash_table_destroy(params);
return r;
}
static gboolean plugin_load(PurplePlugin *plugin) {
// XXX: The windows installer inadvertently takes over internal xmpp links if
// the xmpp registry handler is added. I'm pretty sure this is
// unintentional, as the registry handler is meant for external links only
// (e.g. click on xmpp in a browser window will cause pidgin to handle it).
// So to prevent an issue we clear any existing callback so this plugin
// can handle it.
gtk_imhtml_class_register_protocol("xmpp:", NULL, NULL);
gboolean r = gtk_imhtml_class_register_protocol("xmpp:", handle_uri, NULL);
if (r) {
purple_debug_info(PLUGIN_ID, "Loaded\n");
} else {
purple_debug_error(PLUGIN_ID, "bug: unable to register xmpp protocol\n");
}
return r;
}
static gboolean plugin_unload(PurplePlugin *plugin) {
gtk_imhtml_class_register_protocol("xmpp:", NULL, NULL);
purple_debug_info(PLUGIN_ID, "Unloaded\n");
return TRUE;
}
static PurplePluginInfo info = {
PURPLE_PLUGIN_MAGIC, /* Magic */
PURPLE_MAJOR_VERSION, /* Purple Major Version */
PURPLE_MINOR_VERSION, /* Purple Minor Version */
PURPLE_PLUGIN_STANDARD, /* plugin type */
PIDGIN_PLUGIN_TYPE, /* ui requirement */
0, /* flags */
NULL, /* dependencies */
PURPLE_PRIORITY_DEFAULT, /* priority */
PLUGIN_ID, /* plugin id */
PLUGIN_NAME, /* name */
PLUGIN_VERSION, /* version */
PLUGIN_SUMMARY, /* summary */
PLUGIN_DESCRIPTION, /* description */
PLUGIN_AUTHOR, /* author */
PLUGIN_WEBSITE, /* website */
plugin_load, /* load */
plugin_unload, /* unload */
NULL, /* destroy */
NULL, /* ui_info */
NULL, /* extra_info */
NULL, /* prefs_info */
NULL, /* actions */
/* padding */
NULL,
NULL,
NULL,
NULL
};
static void init_plugin(PurplePlugin *plugin)
{
plugin_instance = plugin;
}
PURPLE_INIT_PLUGIN(PLUGIN_STATIC_NAME, init_plugin, info)