[gaim-migrate @ 2949]
[pidgin-git.git] / plugins / raw.c
blob2efc383367aeb9cddb797b841f9de95844989b00
1 #define GAIM_PLUGINS
2 #include "gaim.h"
3 #include "prpl.h"
4 #ifdef MAX
5 #undef MAX
6 #undef MIN
7 #endif
8 #include "protocols/jabber/jabber.h"
10 static GtkWidget *window = NULL;
11 static GtkWidget *optmenu = NULL;
12 static struct gaim_connection *gc = NULL;
13 static GModule *me = NULL;
15 /* this is an evil hack.
16 * gc->proto_data for Jabber connections can be cast to a jconn *.
17 * gc->proto_data for MSN, TOC, and IRC connections can be cast to an int *.
20 char *name()
22 return "Raw";
25 char *description()
27 return "Lets you send raw XML to Jabber, or raw commands to MSN, IRC, and TOC."
28 " Not very useful except for debugging. Hit 'enter' in the entry to send."
29 " Watch the debug window.";
32 static int goodbye()
34 gaim_plugin_unload(me);
35 return FALSE;
38 static void send_it(GtkEntry *entry)
40 char *txt;
41 if (!gc) return;
42 txt = gtk_entry_get_text(entry);
43 switch (gc->protocol) {
44 case PROTO_TOC:
46 int *a = (int *)gc->proto_data;
47 unsigned short seqno = htons(a[1]++ & 0xffff);
48 unsigned short len = htons(strlen(txt) + 1);
49 write(*a, "*\002", 2);
50 write(*a, &seqno, 2);
51 write(*a, &len, 2);
52 write(*a, txt, ntohs(len));
53 debug_printf("TOC C: %s\n", txt);
55 break;
56 case PROTO_MSN:
57 write(*(int *)gc->proto_data, txt, strlen(txt));
58 write(*(int *)gc->proto_data, "\r\n", 2);
59 debug_printf("MSN C: %s\n", txt);
60 break;
61 case PROTO_IRC:
62 write(*(int *)gc->proto_data, txt, strlen(txt));
63 write(*(int *)gc->proto_data, "\r\n", 2);
64 debug_printf("IRC C: %s\n", txt);
65 break;
66 case PROTO_JABBER:
67 jab_send_raw(*(jconn *)gc->proto_data, txt);
68 break;
70 gtk_entry_set_text(entry, "");
73 static void set_gc(gpointer d, struct gaim_connection *c)
75 gc = c;
78 static void redo_optmenu(struct gaim_connection *arg, gpointer x)
80 GtkWidget *menu;
81 GSList *g = connections;
82 struct gaim_connection *c;
84 menu = gtk_menu_new();
85 gc = NULL;
87 while (g) {
88 char buf[256];
89 GtkWidget *opt;
90 c = (struct gaim_connection *)g->data;
91 g = g->next;
92 if (x && c == arg)
93 continue;
94 if (c->protocol != PROTO_TOC && c->protocol != PROTO_MSN &&
95 c->protocol != PROTO_IRC && c->protocol != PROTO_JABBER)
96 continue;
97 if (!gc)
98 gc = c;
99 g_snprintf(buf, sizeof buf, "%s (%s)", c->username, (*c->prpl->name)());
100 opt = gtk_menu_item_new_with_label(buf);
101 gtk_signal_connect(GTK_OBJECT(opt), "activate", GTK_SIGNAL_FUNC(set_gc), c);
102 gtk_widget_show(opt);
103 gtk_menu_append(GTK_MENU(menu), opt);
106 gtk_option_menu_remove_menu(GTK_OPTION_MENU(optmenu));
107 gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), menu);
108 gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), 0);
111 char *gaim_plugin_init(GModule *h)
113 GtkWidget *hbox;
114 GtkWidget *entry;
116 me = h;
118 gaim_signal_connect(h, event_signon, redo_optmenu, NULL);
119 gaim_signal_connect(h, event_signoff, redo_optmenu, me);
121 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
122 gtk_signal_connect(GTK_OBJECT(window), "delete_event", GTK_SIGNAL_FUNC(goodbye), NULL);
124 hbox = gtk_hbox_new(FALSE, 0);
125 gtk_container_add(GTK_CONTAINER(window), hbox);
127 optmenu = gtk_option_menu_new();
128 gtk_box_pack_start(GTK_BOX(hbox), optmenu, FALSE, FALSE, 5);
130 redo_optmenu(NULL, NULL);
132 entry = gtk_entry_new();
133 gtk_box_pack_start(GTK_BOX(hbox), entry, FALSE, FALSE, 5);
134 gtk_signal_connect(GTK_OBJECT(entry), "activate", GTK_SIGNAL_FUNC(send_it), NULL);
136 gtk_widget_show_all(window);
138 return NULL;
141 void gaim_plugin_remove()
143 if (window)
144 gtk_widget_destroy(window);
145 window = NULL;
146 me = NULL;