Migrate certificates, icons, logs to XDG dirs
[pidgin-git.git] / pidgin / plugins / raw.c
blobbf8824ab967bf4fea4fe50778aee70aeefc02d27
1 /*
2 * Purple - Send raw data across the connections of some protocols.
4 * Pidgin is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
24 #include "internal.h"
25 #include "pidgin.h"
27 #include "conversation.h"
28 #include "debug.h"
29 #include "protocol.h"
30 #include "version.h"
32 #include "gtk3compat.h"
33 #include "gtkplugin.h"
34 #include "gtkutils.h"
36 #include "protocols/jabber/jabber.h"
37 #include "protocols/msn/session.h"
39 #ifdef MAX
40 # undef MAX
41 # undef MIN
42 #endif
44 #define RAW_PLUGIN_ID "gtk-raw"
46 static GtkWidget *window = NULL;
47 static PurpleAccount *account = NULL;
48 static PurplePlugin *my_plugin = NULL;
50 static int
51 window_closed_cb()
53 purple_plugin_unload(my_plugin, NULL);
55 return FALSE;
58 static void
59 text_sent_cb(GtkEntry *entry)
61 const char *txt;
62 PurpleConnection *gc;
63 const char *protocol_id;
65 if (account == NULL)
66 return;
68 gc = purple_account_get_connection(account);
70 txt = gtk_entry_get_text(entry);
72 protocol_id = purple_account_get_protocol_id(account);
74 purple_debug_misc("raw", "protocol_id = %s\n", protocol_id);
76 if (strcmp(protocol_id, "prpl-toc") == 0) {
77 int *a = (int *)purple_connection_get_protocol_data(gc);
78 unsigned short seqno = htons(a[1]++ & 0xffff);
79 unsigned short len = htons(strlen(txt) + 1);
80 write(*a, "*\002", 2);
81 write(*a, &seqno, 2);
82 write(*a, &len, 2);
83 write(*a, txt, ntohs(len));
84 purple_debug(PURPLE_DEBUG_MISC, "raw", "TOC C: %s\n", txt);
86 } else if (strcmp(protocol_id, "prpl-msn") == 0) {
87 MsnSession *session = purple_connection_get_protocol_data(gc);
88 char buf[strlen(txt) + 3];
90 g_snprintf(buf, sizeof(buf), "%s\r\n", txt);
91 msn_servconn_write(session->notification->servconn, buf, strlen(buf));
93 } else if (strcmp(protocol_id, "prpl-irc") == 0) {
94 write(*(int *)purple_connection_get_protocol_data(gc), txt, strlen(txt));
95 write(*(int *)purple_connection_get_protocol_data(gc), "\r\n", 2);
96 purple_debug(PURPLE_DEBUG_MISC, "raw", "IRC C: %s\n", txt);
98 } else if (strcmp(protocol_id, "prpl-jabber") == 0) {
99 jabber_send_raw((JabberStream *)purple_connection_get_protocol_data(gc), txt, -1);
101 } else {
102 purple_debug_error("raw", "Unknown protocol ID %s\n", protocol_id);
105 gtk_entry_set_text(entry, "");
108 static void
109 account_changed_cb(GtkWidget *dropdown, PurpleAccount *new_account,
110 void *user_data)
112 account = new_account;
115 static PidginPluginInfo *
116 plugin_query(GError **error)
118 const gchar * const authors[] = {
119 "Eric Warmenhoven <eric@warmenhoven.org>",
120 NULL
123 return pidgin_plugin_info_new(
124 "id", RAW_PLUGIN_ID,
125 "name", N_("Raw"),
126 "version", DISPLAY_VERSION,
127 "category", N_("Protocol utility"),
128 "summary", N_("Lets you send raw input to text-based protocols."),
129 "description", N_("Lets you send raw input to text-based protocols "
130 "(XMPP, MSN, IRC, TOC). Hit 'Enter' in the entry "
131 "box to send. Watch the debug window."),
132 "authors", authors,
133 "website", PURPLE_WEBSITE,
134 "abi-version", PURPLE_ABI_VERSION,
135 NULL
139 static gboolean
140 plugin_load(PurplePlugin *plugin, GError **error)
142 GtkWidget *hbox;
143 GtkWidget *entry;
144 GtkWidget *dropdown;
146 my_plugin = plugin;
148 /* Setup the window. */
149 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
150 gtk_container_set_border_width(GTK_CONTAINER(window), 6);
152 g_signal_connect(G_OBJECT(window), "delete_event",
153 G_CALLBACK(window_closed_cb), NULL);
155 /* Main hbox */
156 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
157 gtk_container_add(GTK_CONTAINER(window), hbox);
159 /* Account drop-down menu. */
160 dropdown = pidgin_account_option_menu_new(NULL, FALSE,
161 G_CALLBACK(account_changed_cb), NULL, NULL);
163 if (purple_connections_get_all())
164 account = (PurpleAccount *)purple_connections_get_all()->data;
166 gtk_box_pack_start(GTK_BOX(hbox), dropdown, FALSE, FALSE, 0);
168 /* Entry box */
169 entry = gtk_entry_new();
170 gtk_box_pack_start(GTK_BOX(hbox), entry, FALSE, FALSE, 0);
172 g_signal_connect(G_OBJECT(entry), "activate",
173 G_CALLBACK(text_sent_cb), NULL);
175 gtk_widget_show_all(window);
177 return TRUE;
180 static gboolean
181 plugin_unload(PurplePlugin *plugin, GError **error)
183 if (window)
184 gtk_widget_destroy(window);
186 window = NULL;
187 my_plugin = NULL;
189 return TRUE;
192 PURPLE_PLUGIN_INIT(raw, plugin_query, plugin_load, plugin_unload);