summary refs log tree commit diff stats
path: root/src/common/dbus/dbus-plugin.c
blob: bb9fb5e15d085cff3a0b5c8c1357021fd543dc63 (plain) (blame)
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/*
 * ssl.c v0.0.3
 * Copyright (C) 2000  --  DaP <profeta@freemail.c3.hu>
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include "inet.h"				  /* make it first to avoid macro redefinitions */
#include <openssl/ssl.h>		  /* SSL_() */
#include <openssl/err.h>		  /* ERR_() */
#include <time.h>					  /* asctime() */
#include <string.h>				  /* strncpy() */
#include "ssl.h"					  /* struct cert_info */
#include "../../config.h"		  /* HAVE_SNPRINTF */

#ifndef HAVE_SNPRINTF
#define snprintf g_snprintf
#endif

/* globals */
static struct chiper_info chiper_info;		/* static buffer for _SSL_get_cipher_info() */
static char err_buf[256];			/* generic error buffer */


/* +++++ Internal functions +++++ */

static void
__SSL_fill_err_buf (char *funcname)
{
	int err;
	char buf[256];


	err = ERR_get_error ();
	ERR_error_string (err, buf);
	snprintf (err_buf, sizeof (err_buf), "%s: %s (%d)\n", funcname, buf, err);
}


static void
__SSL_critical_error (char *funcname)
{
	__SSL_fill_err_buf (funcname);
	fprintf (stderr, "%s\n", err_buf);

	exit (1);
}

/* +++++ SSL functions +++++ */

SSL_CTX *
_SSL_context_init (void (*info_cb_func), int server)
{
	SSL_CTX *ctx;
#ifdef WIN32
	int i, r;
#endif

	SSLeay_add_ssl_algorithms ();
	SSL_load_error_strings ();
	ctx = SSL_CTX_new (server ? SSLv3_server_method() : SSLv3_client_method ());

	SSL_CTX_set_session_cache_mode (ctx, SSL_SESS_CACHE_BOTH);
	SSL_CTX_set_timeout (ctx, 300);

	/* used in SSL_connect(), SSL_accept() */
	SSL_CTX_set_info_callback (ctx, info_cb_func);

#ifdef WIN32
	/* under win32, OpenSSL needs to be seeded with some randomness */
	for (i = 0; i < 128; i++)
	{
		r = rand ();
		RAND_seed ((unsigned char *)&r, sizeof (r));
	}
#endif

	return(ctx);
}

static void
ASN1_TIME_snprintf (char *buf, int buf_len, ASN1_TIME * tm)
{
	char *expires = NULL;
	BIO *inMem = BIO_new (BIO_s_mem ());

	ASN1_TIME_print (inMem, tm);
	BIO_get_mem_data (inMem, &expires);
	buf[0] = 0;
	if (expires != NULL)
	{
		memset (buf, 0, buf_len);
		strncpy (buf, expires, 24);
	}
	BIO_free (inMem);
}


static void
broke_oneline (char *oneline, char *parray[])
{
	char *pt, *ppt;
	int i;


	i = 0;
	ppt = pt = oneline + 1;
	while ((pt = strchr (pt, '/')))
	{
		*pt = 0;
		parray[i++] = ppt;
		ppt = ++pt;
	}
	parray[i++] = ppt;
	parray[i] = NULL;
}


/*
    FIXME: Master-Key, Extensions, CA bits
	    (openssl x509 -text -in servcert.pem)
*/
int
_SSL_get_cert_info (struct cert_info *cert_info, SSL * ssl)
{
	X509 *peer_cert;
	EVP_PKEY *peer_pkey;
	/* EVP_PKEY *ca_pkey; */
	/* EVP_PKEY *tmp_pkey; */
	char notBefore[64];
	char notAfter[64];
	int alg;
	int sign_alg;


	if (!(peer_cert = SSL_get_peer_certificate (ssl)))
		return (1);				  /* FATAL? */

	X509_NAME_oneline (X509_get_subject_name (peer_cert), cert_info->subject,
							 sizeof (cert_info->subject));
	X509_NAME_oneline (X509_get_issuer_name (peer_cert), cert_info->issuer,
							 sizeof (cert_info->issuer));
	broke_oneline (cert_info->subject, cert_info->subject_word);
	broke_oneline (cert_info->issuer, cert_info->issuer_word);

	alg = OBJ_obj2nid (peer_cert->cert_info->key->algor->algorithm);
	sign_alg = OBJ_obj2nid (peer_cert->sig_alg->algorithm);
	ASN1_TIME_snprintf (notBefore, sizeof (notBefore),
							  X509_get_notBefore (peer_cert));
	ASN1_TIME_snprintf (notAfter, sizeof (notAfter),
							  X509_get_notAfter (peer_cert));

	peer_pkey = X509_get_pubkey (peer_cert);

	strncpy (cert_info->algorithm,
				(alg == NID_undef) ? "Unknown" : OBJ_nid2ln (alg),
				sizeof (cert_info->algorithm));
	cert_info->algorithm_bits = EVP_PKEY_bits (peer_pkey);
	strncpy (cert_info->sign_algorithm,
				(sign_alg == NID_undef) ? "Unknown" : OBJ_nid2ln (sign_alg),
				sizeof (cert_info->sign_algorithm));
	/* EVP_PKEY_bits(ca_pkey)); */
	cert_info->sign_algorithm_bits = 0;
	strncpy (cert_info->notbefore, notBefore, sizeof (cert_info->notbefore));
	strncpy (cert_info->notafter, notAfter, sizeof (cert_info->notafter));

	EVP_PKEY_free (peer_pkey);

	/* SSL_SESSION_print_fp(stdout, SSL_get_session(ssl)); */
/*
	if (ssl->session->sess_cert->peer_rsa_tmp) {
		tmp_pkey = EVP_PKEY_new();
		EVP_PKEY_assign_RSA(tmp_pkey, ssl->session->sess_cert->peer_rsa_tmp);
		cert_info->rsa_tmp_bits = EVP_PKEY_bits (tmp_pkey);
		EVP_PKEY_free(tmp_pkey);
	} else
		fprintf(stderr, "REMOTE SIDE DOESN'T PROVIDES ->peer_rsa_tmp\n");
*/
	cert_info->rsa_tmp_bits = 0;

	X509_free (peer_cert);

	return (0);
}


struct chiper_info *
_SSL_get_cipher_info (SSL * ssl)
{
	SSL_CIPHER *c;


	c = SSL_get_current_cipher (ssl);
	strncpy (chiper_info.version, SSL_CIPHER_get_version (c),
				sizeof (chiper_info.version));
	strncpy (chiper_info.chiper, SSL_CIPHER_get_name (c),
				sizeof (chiper_info.chiper));
	SSL_CIPHER_get_bits (c, &chiper_info.chiper_bits);

	return (&chiper_info);
}


int
_SSL_send (SSL * ssl, char *buf, int len)
{
	int num;


	num = SSL_write (ssl, buf, len);

	switch (SSL_get_error (ssl, num))
	{
	case SSL_ERROR_SSL:			  /* setup errno! */
		/* ??? */
		__SSL_fill_err_buf ("SSL_write");
		fprintf (stderr, "%s\n", err_buf);
		break;
	case SSL_ERROR_SYSCALL:
		/* ??? */
		perror ("SSL_write/write");
		break;
	case SSL_ERROR_ZERO_RETURN:
		/* fprintf(stderr, "SSL closed on write\n"); */
		break;
	}

	return (num);
}


int
_SSL_recv (SSL * ssl, char *buf, int len)
{
	int num;


	num = SSL_read (ssl, buf, len);

	switch (SSL_get_error (ssl, num))
	{
	case SSL_ERROR_SSL:
		/* ??? */
		__SSL_fill_err_buf ("SSL_read");
		fprintf (stderr, "%s\n", err_buf);
		break;
	case SSL_ERROR_SYSCALL:
		/* ??? */
		if (!would_block ())
			perror ("SSL_read/read");
		break;
	case SSL_ERROR_ZERO_RETURN:
		/* fprintf(stdeerr, "SSL closed on read\n"); */
		break;
	}

	return (num);
}


SSL *
_SSL_socket (SSL_CTX *ctx, int sd)
{
	SSL *ssl;


	if (!(ssl = SSL_new (ctx)))
		/* FATAL */
		__SSL_critical_error ("SSL_new");

	SSL_set_fd (ssl, sd);
	if (ctx->method == SSLv3_client_method())
		SSL_set_connect_state (ssl);
	else
	        SSL_set_accept_state(ssl);

	return (ssl);
}


char *
_SSL_set_verify (SSL_CTX *ctx, void *verify_callback, char *cacert)
{
	if (!SSL_CTX_set_default_verify_paths (ctx))
	{
		__SSL_fill_err_buf ("SSL_CTX_set_default_verify_paths");
		return (err_buf);
	}
/*
	if (cacert)
	{
		if (!SSL_CTX_load_verify_locations (ctx, cacert, NULL))
		{
			__SSL_fill_err_buf ("SSL_CTX_load_verify_locations");
			return (err_buf);
		}
	}
*/
	SSL_CTX_set_verify (ctx, SSL_VERIFY_PEER, verify_callback);

	return (NULL);
}


void
_SSL_close (SSL * ssl)
{
	SSL_set_shutdown (ssl, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
	SSL_free (ssl);
	ERR_remove_state (0);		  /* free state buffer */
}
>*name, int pri, int return_value, guint *ret_id, GError **error); static gboolean remote_object_hook_print (RemoteObject *obj, const char *name, int pri, int return_value, guint *ret_id, GError **error); static gboolean remote_object_unhook (RemoteObject *obj, guint id, GError **error); static gboolean remote_object_list_get (RemoteObject *obj, const char *name, guint *ret_id, GError **error); static gboolean remote_object_list_next (RemoteObject *obj, guint id, gboolean *ret, GError **error); static gboolean remote_object_list_str (RemoteObject *obj, guint id, const char *name, char **ret_str, GError **error); static gboolean remote_object_list_int (RemoteObject *obj, guint id, const char *name, int *ret_int, GError **error); static gboolean remote_object_list_time (RemoteObject *obj, guint id, const char *name, guint64 *ret_time, GError **error); static gboolean remote_object_list_fields (RemoteObject *obj, const char *name, char ***ret, GError **error); static gboolean remote_object_list_free (RemoteObject *obj, guint id, GError **error); static gboolean remote_object_emit_print (RemoteObject *obj, const char *event_name, const char *args[], gboolean *ret, GError **error); static gboolean remote_object_nickcmp (RemoteObject *obj, const char *nick1, const char *nick2, int *ret, GError **error); static gboolean remote_object_strip (RemoteObject *obj, const char *str, int len, int flag, char **ret_str, GError **error); static gboolean remote_object_send_modes (RemoteObject *obj, const char *targets[], int modes_per_line, char sign, char mode, GError **error); #include "remote-object-glue.h" #include "../marshal.h" /* Useful functions */ static char** build_list (char *word[]); static guint context_list_find_id (hexchat_context *context); static hexchat_context* context_list_find_context (guint id); /* Remote Object */ static void hook_info_destroy (gpointer data) { HookInfo *info = (HookInfo*)data; if (info == NULL) { return; } hexchat_unhook (ph, info->hook); g_free (info); } static void list_info_destroy (gpointer data) { hexchat_list_free (ph, (hexchat_list*)data); } static void remote_object_finalize (GObject *obj) { RemoteObject *self = (RemoteObject*)obj; g_hash_table_destroy (self->lists); g_hash_table_destroy (self->hooks); g_free (self->dbus_path); g_free (self->filename); hexchat_plugingui_remove (ph, self->handle); G_OBJECT_CLASS (remote_object_parent_class)->finalize (obj); } static void remote_object_init (RemoteObject *obj) { obj->hooks = g_hash_table_new_full (g_int_hash, g_int_equal, NULL, hook_info_destroy); obj->lists = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, list_info_destroy); obj->dbus_path = NULL; obj->filename = NULL; obj->last_hook_id = 0; obj->last_list_id = 0; obj->context = hexchat_get_context (ph); } static void remote_object_class_init (RemoteObjectClass *klass) { GObjectClass *gobject_class = G_OBJECT_CLASS (klass); gobject_class->finalize = remote_object_finalize; signals[SERVER_SIGNAL] = g_signal_new ("server_signal", G_OBJECT_CLASS_TYPE (klass), G_SIGNAL_RUN_LAST, 0, NULL, NULL, _hexchat_marshal_VOID__POINTER_POINTER_UINT_UINT, G_TYPE_NONE, 4, G_TYPE_STRV, G_TYPE_STRV, G_TYPE_UINT, G_TYPE_UINT); signals[COMMAND_SIGNAL] = g_signal_new ("command_signal", G_OBJECT_CLASS_TYPE (klass), G_SIGNAL_RUN_LAST, 0, NULL, NULL, _hexchat_marshal_VOID__POINTER_POINTER_UINT_UINT, G_TYPE_NONE, 4, G_TYPE_STRV, G_TYPE_STRV, G_TYPE_UINT, G_TYPE_UINT); signals[PRINT_SIGNAL] = g_signal_new ("print_signal", G_OBJECT_CLASS_TYPE (klass), G_SIGNAL_RUN_LAST, 0, NULL, NULL, _hexchat_marshal_VOID__POINTER_POINTER_UINT_UINT, G_TYPE_NONE, 3, G_TYPE_STRV, G_TYPE_UINT, G_TYPE_UINT); signals[UNLOAD_SIGNAL] = g_signal_new ("unload_signal", G_OBJECT_CLASS_TYPE (klass), G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0); } /* Implementation of services */ static gboolean remote_object_connect (RemoteObject *obj, const char *filename, const char *name, const char *desc, const char *version, DBusGMethodInvocation *context) { static guint count = 0; char *sender, *path; RemoteObject *remote_object; sender = dbus_g_method_get_sender (context); remote_object = g_hash_table_lookup (clients, sender); if (remote_object != NULL) { dbus_g_method_return (context, remote_object->dbus_path); g_free (sender); return TRUE; } path = g_build_filename (DBUS_OBJECT_PATH, count++, NULL); remote_object = g_object_new (REMOTE_TYPE_OBJECT, NULL); remote_object->dbus_path = path; remote_object->filename = g_path_get_basename (filename); remote_object->handle = hexchat_plugingui_add (ph, remote_object->filename, name, desc, version, NULL); dbus_g_connection_register_g_object (connection, path, G_OBJECT (remote_object)); g_hash_table_insert (clients, sender, remote_object); dbus_g_method_return (context, path); return TRUE; } static gboolean remote_object_disconnect (RemoteObject *obj, DBusGMethodInvocation *context) { char *sender; sender = dbus_g_method_get_sender (context); g_hash_table_remove (clients, sender); g_free (sender); dbus_g_method_return (context); return TRUE; } static gboolean remote_object_command (RemoteObject *obj, const char *command, GError **error) { if (hexchat_set_context (ph, obj->context)) { hexchat_command (ph, command); } return TRUE; } static gboolean remote_object_print (RemoteObject *obj, const char *text, GError **error) { if (hexchat_set_context (ph, obj->context)) { hexchat_print (ph, text); } return TRUE; } static gboolean remote_object_find_context (RemoteObject *obj, const char *server, const char *channel, guint *ret_id, GError **error) { hexchat_context *context; if (*server == '\0') { server = NULL; } if (*channel == '\0') { channel = NULL; } context = hexchat_find_context (ph, server, channel); *ret_id = context_list_find_id (context); return TRUE; } static gboolean remote_object_get_context (RemoteObject *obj, guint *ret_id, GError **error) { *ret_id = context_list_find_id (obj->context); return TRUE; } static gboolean remote_object_set_context (RemoteObject *obj, guint id, gboolean *ret, GError **error) { hexchat_context *context; context = context_list_find_context (id); if (context == NULL) { *ret = FALSE; return TRUE; } obj->context = context; *ret = TRUE; return TRUE; } static gboolean remote_object_get_info (RemoteObject *obj, const char *id, char **ret_info, GError **error) { /* win_ptr is a GtkWindow* casted to char* and will crash * D-Bus if we send it as a string */ if (!hexchat_set_context (ph, obj->context) || g_str_equal (id, "win_ptr")) { *ret_info = NULL; return TRUE; } *ret_info = g_strdup (hexchat_get_info (ph, id)); return TRUE; } static gboolean remote_object_get_prefs (RemoteObject *obj, const char *name, int *ret_type, char **ret_str, int *ret_int, GError **error) { const char *str; if (!hexchat_set_context (ph, obj->context)) { *ret_type = 0; return TRUE; } *ret_type = hexchat_get_prefs (ph, name, &str, ret_int); *ret_str = g_strdup (str); return TRUE; } static int server_hook_cb (char *word[], char *word_eol[], void *userdata) { HookInfo *info = (HookInfo*)userdata; char **arg1; char **arg2; arg1 = build_list (word + 1); arg2 = build_list (word_eol + 1); info->obj->context = hexchat_get_context (ph); g_signal_emit (info->obj, signals[SERVER_SIGNAL], 0, arg1, arg2, info->id, context_list_find_id (info->obj->context)); g_strfreev (arg1); g_strfreev (arg2); return info->return_value; } static int command_hook_cb (char *word[], char *word_eol[], void *userdata) { HookInfo *info = (HookInfo*)userdata; char **arg1; char **arg2; arg1 = build_list (word + 1); arg2 = build_list (word_eol + 1); info->obj->context = hexchat_get_context (ph); g_signal_emit (info->obj, signals[COMMAND_SIGNAL], 0, arg1, arg2, info->id, context_list_find_id (info->obj->context)); g_strfreev (arg1); g_strfreev (arg2); return info->return_value; } static int print_hook_cb (char *word[], void *userdata) { HookInfo *info = (HookInfo*)userdata; char **arg1; arg1 = build_list (word + 1); info->obj->context = hexchat_get_context (ph); g_signal_emit (info->obj, signals[PRINT_SIGNAL], 0, arg1, info->id, context_list_find_id (info->obj->context)); g_strfreev (arg1); return info->return_value; } static gboolean remote_object_hook_command (RemoteObject *obj, const char *name, int priority, const char *help_text, int return_value, guint *ret_id, GError **error) { HookInfo *info; info = g_new0 (HookInfo, 1); info->obj = obj; info->return_value = return_value; info->id = ++obj->last_hook_id; info->hook = hexchat_hook_command (ph, name, priority, command_hook_cb, help_text, info); g_hash_table_insert (obj->hooks, &info->id, info); *ret_id = info->id; return TRUE; } static gboolean remote_object_hook_server (RemoteObject *obj, const char *name, int priority, int return_value, guint *ret_id, GError **error) { HookInfo *info; info = g_new0 (HookInfo, 1); info->obj = obj; info->return_value = return_value; info->id = ++obj->last_hook_id; info->hook = hexchat_hook_server (ph, name, priority, server_hook_cb, info); g_hash_table_insert (obj->hooks, &info->id, info); *ret_id = info->id; return TRUE; } static gboolean remote_object_hook_print (RemoteObject *obj, const char *name, int priority, int return_value, guint *ret_id, GError **error) { HookInfo *info; info = g_new0 (HookInfo, 1); info->obj = obj; info->return_value = return_value; info->id = ++obj->last_hook_id; info->hook = hexchat_hook_print (ph, name, priority, print_hook_cb, info); g_hash_table_insert (obj->hooks, &info->id, info); *ret_id = info->id; return TRUE; } static gboolean remote_object_unhook (RemoteObject *obj, guint id, GError **error) { g_hash_table_remove (obj->hooks, &id); return TRUE; } static gboolean remote_object_list_get (RemoteObject *obj, const char *name, guint *ret_id, GError **error) { hexchat_list *xlist; guint *id; if (!hexchat_set_context (ph, obj->context)) { *ret_id = 0; return TRUE; } xlist = hexchat_list_get (ph, name); if (xlist == NULL) { *ret_id = 0; return TRUE; } id = g_new (guint, 1); *id = ++obj->last_list_id; *ret_id = *id; g_hash_table_insert (obj->lists, id, xlist); return TRUE; } static gboolean remote_object_list_next (RemoteObject *obj, guint id, gboolean *ret, GError **error) { hexchat_list *xlist; xlist = g_hash_table_lookup (obj->lists, &id); if (xlist == NULL) { *ret = FALSE; return TRUE; } *ret = hexchat_list_next (ph, xlist); return TRUE; } static gboolean remote_object_list_str (RemoteObject *obj, guint id, const char *name, char **ret_str, GError **error) { hexchat_list *xlist; xlist = g_hash_table_lookup (obj->lists, &id); if (xlist == NULL && !hexchat_set_context (ph, obj->context)) { *ret_str = NULL; return TRUE; } if (g_str_equal (name, "context")) { *ret_str = NULL; return TRUE; } *ret_str = g_strdup (hexchat_list_str (ph, xlist, name)); return TRUE; } static gboolean remote_object_list_int (RemoteObject *obj, guint id, const char *name, int *ret_int, GError **error) { hexchat_list *xlist; xlist = g_hash_table_lookup (obj->lists, &id); if (xlist == NULL && !hexchat_set_context (ph, obj->context)) { *ret_int = -1; return TRUE; } if (g_str_equal (name, "context")) { hexchat_context *context; context = (hexchat_context*)hexchat_list_str (ph, xlist, name); *ret_int = context_list_find_id (context); } else { *ret_int = hexchat_list_int (ph, xlist, name); } return TRUE; } static gboolean remote_object_list_time (RemoteObject *obj, guint id, const char *name, guint64 *ret_time, GError **error) { hexchat_list *xlist; xlist = g_hash_table_lookup (obj->lists, &id); if (xlist == NULL) { *ret_time = (guint64) -1; return TRUE; } *ret_time = hexchat_list_time (ph, xlist, name); return TRUE; } static gboolean remote_object_list_fields (RemoteObject *obj, const char *name, char ***ret, GError **error) { *ret = g_strdupv ((char**)hexchat_list_fields (ph, name)); if (*ret == NULL) { *ret = g_new0 (char*, 1); } return TRUE; } static gboolean remote_object_list_free (RemoteObject *obj, guint id, GError **error) { g_hash_table_remove (obj->lists, &id); return TRUE; } static gboolean remote_object_emit_print (RemoteObject *obj, const char *event_name, const char *args[], gboolean *ret, GError **error) { const char *argv[4] = {NULL, NULL, NULL, NULL}; int i; for (i = 0; i < 4 && args[i] != NULL; i++) { argv[i] = args[i]; } *ret = hexchat_set_context (ph, obj->context); if (*ret) { *ret = hexchat_emit_print (ph, event_name, argv[0], argv[1], argv[2], argv[3]); } return TRUE; } static gboolean remote_object_nickcmp (RemoteObject *obj, const char *nick1, const char *nick2, int *ret, GError **error) { hexchat_set_context (ph, obj->context); *ret = hexchat_nickcmp (ph, nick1, nick2); return TRUE; } static gboolean remote_object_strip (RemoteObject *obj, const char *str, int len, int flag, char **ret_str, GError **error) { *ret_str = hexchat_strip (ph, str, len, flag); return TRUE; } static gboolean remote_object_send_modes (RemoteObject *obj, const char *targets[], int modes_per_line, char sign, char mode, GError **error) { if (hexchat_set_context (ph, obj->context)) { hexchat_send_modes (ph, targets, g_strv_length ((char**)targets), modes_per_line, sign, mode); } return TRUE; } /* DBUS stuffs */ static void name_owner_changed (DBusGProxy *driver_proxy, const char *name, const char *old_owner, const char *new_owner, void *user_data) { if (*new_owner == '\0') { /* this name has vanished */ g_hash_table_remove (clients, name); } } static gboolean init_dbus (void) { DBusGProxy *proxy; RemoteObject *remote; guint request_name_result; GError *error = NULL; dbus_g_object_type_install_info (REMOTE_TYPE_OBJECT, &dbus_glib_remote_object_object_info); connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error); if (connection == NULL) { hexchat_printf (ph, _("Couldn't connect to session bus: %s\n"), error->message); g_error_free (error); return FALSE; } proxy = dbus_g_proxy_new_for_name (connection, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS); if (!dbus_g_proxy_call (proxy, "RequestName", &error, G_TYPE_STRING, DBUS_SERVICE, G_TYPE_UINT, DBUS_NAME_FLAG_ALLOW_REPLACEMENT, G_TYPE_INVALID, G_TYPE_UINT, &request_name_result, G_TYPE_INVALID)) { hexchat_printf (ph, _("Failed to acquire %s: %s\n"), DBUS_SERVICE, error->message); g_error_free (error); return FALSE; } dbus_g_proxy_add_signal (proxy, "NameOwnerChanged", G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID); dbus_g_proxy_connect_signal (proxy, "NameOwnerChanged", G_CALLBACK (name_owner_changed), NULL, NULL); remote = g_object_new (REMOTE_TYPE_OBJECT, NULL); dbus_g_connection_register_g_object (connection, DBUS_OBJECT_PATH"/Remote", G_OBJECT (remote)); return TRUE; } /* hexchat_plugin stuffs */ static char** build_list (char *word[]) { guint i; guint num = 0; char **result; if (word == NULL) { return NULL; } while (word[num] && word[num][0]) { num++; } result = g_new0 (char*, num + 1); for (i = 0; i < num; i++) { result[i] = g_strdup (word[i]); } return result; } static guint context_list_find_id (hexchat_context *context) { GList *l = NULL; for (l = contexts; l != NULL; l = l->next) { if (((ContextInfo*)l->data)->context == context) { return ((ContextInfo*)l->data)->id; } } return 0; } static hexchat_context* context_list_find_context (guint id) { GList *l = NULL; for (l = contexts; l != NULL; l = l->next) { if (((ContextInfo*)l->data)->id == id) { return ((ContextInfo*)l->data)->context; } } return NULL; } static int open_context_cb (char *word[], void *userdata) { ContextInfo *info; info = g_new0 (ContextInfo, 1); info->id = ++last_context_id; info->context = hexchat_get_context (ph); contexts = g_list_prepend (contexts, info); return HEXCHAT_EAT_NONE; } static int close_context_cb (char *word[], void *userdata) { GList *l; hexchat_context *context = hexchat_get_context (ph); for (l = contexts; l != NULL; l = l->next) { if (((ContextInfo*)l->data)->context == context) { g_free (l->data); contexts = g_list_delete_link (contexts, l); break; } } return HEXCHAT_EAT_NONE; } static gboolean clients_find_filename_foreach (gpointer key, gpointer value, gpointer user_data) { RemoteObject *obj = REMOTE_OBJECT (value); return g_str_equal (obj->filename, (char*)user_data); } static int unload_plugin_cb (char *word[], char *word_eol[], void *userdata) { RemoteObject *obj; obj = g_hash_table_find (clients, clients_find_filename_foreach, word[2]); if (obj != NULL) { g_signal_emit (obj, signals[UNLOAD_SIGNAL], 0); return HEXCHAT_EAT_ALL; } return HEXCHAT_EAT_NONE; } int dbus_plugin_init (hexchat_plugin *plugin_handle, char **plugin_name, char **plugin_desc, char **plugin_version, char *arg) { ph = plugin_handle; *plugin_name = PNAME; *plugin_desc = PDESC; *plugin_version = PVERSION; if (init_dbus()) { /*hexchat_printf (ph, _("%s loaded successfully!\n"), PNAME);*/ clients = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref); hexchat_hook_print (ph, "Open Context", HEXCHAT_PRI_NORM, open_context_cb, NULL); hexchat_hook_print (ph, "Close Context", HEXCHAT_PRI_NORM, close_context_cb, NULL); hexchat_hook_command (ph, "unload", HEXCHAT_PRI_HIGHEST, unload_plugin_cb, NULL, NULL); } return TRUE; }