[r1009]: plugins / dbus / xchat-remote.c Maximize Restore History

Download this file

xchat-remote.c    172 lines (157 with data), 6.1 kB

  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
/* xchat-remote - program for remote access xchat using DBUS
* Copyright (C) 2005 Claessens Xavier
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Claessens Xavier
* xclaesse@gmail.com
*/
#include <config.h>
#include <dbus/dbus-glib.h>
#include <unistd.h>
#include <glib/gi18n.h>
#define DBUS_SERVICE "org.xchat.service"
#define DBUS_OBJECT "/org/xchat/RemoteObject"
#define DBUS_INTERFACE "org.xchat.interface"
static DBusGProxy *remote_object = NULL;
static gchar *opt_open_url = NULL;
static gchar *opt_command = NULL;
static gchar *opt_print = NULL;
static gchar *opt_channel = NULL;
static gchar *opt_server = NULL;
static gchar *opt_info = NULL;
static gchar *opt_prefs = NULL;
static GOptionEntry entries[] = {
{"url", 'u', 0, G_OPTION_ARG_STRING, &opt_open_url, N_("Open an irc:// url"), N_("irc://server:port/channel")},
{"command", 'c', 0, G_OPTION_ARG_STRING, &opt_command, N_("Execute a xchat command"), N_("\"Command to execute\"")},
{"print", 'p', 0, G_OPTION_ARG_STRING, &opt_print, N_("Prints some text to the current tab/window"), N_("\"Text to print\"")},
{"channel", 'h', 0, G_OPTION_ARG_STRING, &opt_channel, N_("Change the context to the channel"), N_("channel")},
{"server", 's', 0, G_OPTION_ARG_STRING, &opt_server, N_("Change the context to the server"), N_("server")},
{"info", 'i', 0, G_OPTION_ARG_STRING, &opt_info, N_("Get some informations from xchat"), N_("id")},
{"prefs", 'r', 0, G_OPTION_ARG_STRING, &opt_prefs, N_("Get settings from xchat"), N_("name")},
{ NULL }
};
static void
write_error (gchar *message, GError *err)
{
if (!err)
return;
g_printerr ("%s : %s\n", message, err->message);
g_error_free (err);
}
int
main (int argc, char **argv)
{
DBusGConnection *bus = NULL;
GError *error = NULL;
GOptionContext *context = NULL;
#ifdef ENABLE_NLS
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
#endif
context = g_option_context_new (NULL);
g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
g_option_context_parse (context, &argc, &argv, &error);
if (error)
{
g_printerr (_("xchat-remote: %s\n"
"Try `xchat-remote --help' for more information\n"),
error->message);
return 1;
}
g_type_init ();
bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
if (!bus)
{
write_error (_("Couldn't connect to session bus"), error);
return 1;
}
remote_object = dbus_g_proxy_new_for_name (bus,
DBUS_SERVICE,
DBUS_OBJECT,
DBUS_INTERFACE);
if (opt_open_url)
{
gchar *command;
command = g_strdup_printf ("url %s", opt_open_url);
if (!dbus_g_proxy_call (remote_object, "command", &error,
G_TYPE_STRING, command, G_TYPE_INVALID,
G_TYPE_INVALID))
{
if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_SERVICE_UNKNOWN)
{
/* No xchat running... */
if (fork() == 0)
{
gchar *url = g_strdup_printf ("--url=%s", opt_open_url);
execl (PREFIX"/bin/"PACKAGE_NAME, PACKAGE_NAME, "-a", url, NULL);
}
} else
write_error (_("Failed to complete command"), error);
}
g_free (command);
}
if (opt_channel || opt_server)
if (!dbus_g_proxy_call (remote_object, "SetContext", &error,
G_TYPE_STRING, opt_server,
G_TYPE_STRING, opt_channel, G_TYPE_INVALID,
G_TYPE_INVALID))
write_error (_("Failed to complete SetContext"), error);
if (opt_command)
if (!dbus_g_proxy_call (remote_object, "command", &error,
G_TYPE_STRING, opt_command, G_TYPE_INVALID,
G_TYPE_INVALID))
write_error (_("Failed to complete command"), error);
if (opt_print)
if (!dbus_g_proxy_call (remote_object, "print", &error,
G_TYPE_STRING, opt_print, G_TYPE_INVALID,
G_TYPE_INVALID))
write_error (_("Failed to complete print"), error);
if (opt_info)
{
gchar *info;
if (!dbus_g_proxy_call (remote_object, "GetInfo", &error,
G_TYPE_STRING, opt_info, G_TYPE_INVALID,
G_TYPE_STRING, &info, G_TYPE_INVALID))
write_error (_("Failed to complete GetInfo"), error);
else
g_print ("%s = %s\n", opt_info, info);
}
if (opt_prefs)
{
gchar *str;
int type, i;
if (!dbus_g_proxy_call (remote_object, "GetPrefs", &error,
G_TYPE_STRING, opt_prefs, G_TYPE_INVALID,
G_TYPE_INT, &type,
G_TYPE_STRING, &str,
G_TYPE_INT, &i, G_TYPE_INVALID))
write_error (_("Failed to complete GetPrefs"), error);
else
{
if (type == 0)
g_print (_("%s doesn't exist\n"), opt_prefs);
else if (type == 1)
g_print ("%s = %s\n", opt_prefs, str);
else if (type == 2)
g_print ("%s = %d\n", opt_prefs, i);
else
g_print ("%s = %s\n", opt_prefs, i ? "TRUE" : "FALSE");
}
}
g_object_unref (G_OBJECT (remote_object));
return 0;
}