Mercurial > pidgin.yaz
annotate src/gaim-remote.c @ 9776:1e5ef71c9583
[gaim-migrate @ 10644]
A patch from Daniel Atallah that should fix sf bug 1008489:
"Windows Messenger BOT Crashes Gaim"
Someone MSN-savvy should check this to make sure it's a valid fix.
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Thu, 19 Aug 2004 01:13:58 +0000 |
parents | 8793c7127c80 |
children | 33431f91e4a8 |
rev | line source |
---|---|
3480 | 1 /* |
2 * gaim-remote | |
3 * | |
8046 | 4 * Gaim 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. | |
3480 | 7 * |
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. | |
12 * | |
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. | |
17 * | |
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 * | |
22 */ | |
23 | |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5859
diff
changeset
|
24 #include "internal.h" |
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5859
diff
changeset
|
25 |
3533 | 26 #include <getopt.h> |
5859
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
27 |
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
28 #include <gaim-remote/remote.h> |
3480 | 29 |
4242 | 30 /*To be implemented: |
3480 | 31 " info Show information about connected accounts\n" |
32 " list Print buddy list\n" | |
33 " ison Show presence state of your buddy\n" | |
34 " convo Open a new conversation window\n" | |
35 " send Send message\n" | |
36 " add Add buddy to buddy list\n" | |
3559 | 37 " remove Remove buddy from list\n" |
3480 | 38 " -m, --message=MESG Message to send or show in conversation window\n" |
39 " -t, --to=SCREENNAME Select a target for command\n" | |
40 " -p, --protocol=PROTO Specify protocol to use\n" | |
8154 | 41 " -f, --from=SCREENNAME Specify screen name to use\n" |
4242 | 42 " -q, --quiet Send message without showing a conversation\n" |
3480 | 43 " window\n" |
4242 | 44 */ |
3480 | 45 |
46 static struct option longopts[] = { | |
47 {"message", required_argument, NULL, 'm'}, | |
48 {"to", required_argument, NULL, 't'}, | |
49 {"protocol",required_argument, NULL, 'p'}, | |
50 {"from", required_argument, NULL, 'f'}, | |
51 {"quiet", no_argument, NULL, 'q'}, | |
52 {"help", no_argument, NULL, 'h'}, | |
53 {0,0,0,0} | |
54 }; | |
55 | |
56 struct remoteopts { | |
57 char *command; | |
58 char *uri; | |
59 gboolean help, quiet; | |
60 char *message, *to, *from; | |
61 int protocol; | |
62 }; | |
9752 | 63 struct remoteopts opts; |
3480 | 64 |
9752 | 65 /* |
66 * Prints a message to the terminal/shell/console. | |
67 * We try to convert "text" from UTF-8 to the user's locale. | |
68 * If that fails then UTF-8 is used as a fallback. | |
69 * | |
70 * If channel is 1, the message is printed to stdout. | |
71 * if channel is 2, the message is printed to stderr. | |
72 */ | |
73 static void | |
74 message(char *text, int channel) | |
75 { | |
76 char *text_conv = NULL,*text_output; | |
77 GError *error = NULL; | |
78 | |
79 text_conv = g_locale_from_utf8(text, -1, NULL, NULL, &error); | |
80 | |
81 if (text_conv == NULL) { | |
82 g_warning("%s\n", error->message); | |
83 g_error_free(error); | |
84 } | |
85 | |
86 text_output = (text_conv ? text_conv : text); | |
3480 | 87 |
9752 | 88 switch (channel) { |
89 case 1: | |
90 puts(text_output); | |
91 break; | |
92 case 2: | |
93 fputs(text_output, stderr); | |
94 break; | |
95 default: | |
96 break; | |
97 } | |
98 | |
99 if (text_conv) | |
100 g_free(text_conv); | |
101 } | |
102 | |
103 static void | |
104 show_remote_usage(const char *name) | |
105 { | |
106 char *text = NULL; | |
107 | |
108 text = g_strdup_printf(_("Usage: %s command [OPTIONS] [URI]\n\n" | |
109 " COMMANDS:\n" | |
110 " uri Handle AIM: URI\n" | |
111 " away Popup the away dialog with the default message\n" | |
112 " back Remove the away dialog\n" | |
113 " quit Close running copy of Gaim\n\n" | |
114 " OPTIONS:\n" | |
115 " -h, --help [command] Show help for command\n"), name); | |
116 | |
117 message(text, 1); | |
118 g_free(text); | |
119 | |
120 return; | |
121 } | |
122 | |
123 int | |
124 get_options(int argc, char *argv[]) | |
3480 | 125 { |
126 int i; | |
9752 | 127 |
3480 | 128 memset(&opts, 0, sizeof(opts)); |
129 opts.protocol = -1; | |
9752 | 130 |
3480 | 131 while ((i=getopt_long(argc, argv, "m:t:p:f:qh", longopts, NULL)) != -1) { |
132 switch (i) { | |
133 case 'm': | |
134 opts.message = optarg; | |
135 break; | |
136 case 't': | |
137 opts.to = optarg; | |
138 break; | |
139 case 'p': | |
140 /* Do stuff here. */ | |
141 break; | |
142 case 'f': | |
143 opts.from = optarg; | |
144 break; | |
145 case 'q': | |
146 opts.quiet = TRUE; | |
147 break; | |
148 case 'h': | |
149 opts.help = TRUE; | |
150 break; | |
151 } | |
152 } | |
153 | |
154 /* We must have non getopt'ed argument-- the command */ | |
155 if (optind < argc) | |
156 opts.command = g_strdup(argv[optind++]); | |
157 else | |
158 return 1; | |
159 | |
9674 | 160 if (opts.help) |
6643 | 161 return 0; |
162 | |
3480 | 163 /* And we can have another argument--the URI. */ |
6643 | 164 /* but only if we're using the uri command. */ |
165 if (!strcmp(opts.command, "uri")) { | |
9674 | 166 if (argc-optind == 1) |
3480 | 167 opts.uri = g_strdup(argv[optind++]); |
168 else | |
169 return 1; | |
9752 | 170 } else if (optind == argc) |
6643 | 171 return 0; |
172 else | |
173 return 1; | |
174 | |
175 return 0; | |
3480 | 176 } |
177 | |
9752 | 178 static int |
179 send_generic_command(guchar type, guchar subtype) { | |
3480 | 180 int fd = 0; |
5859
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
181 GaimRemotePacket *p = NULL; |
9752 | 182 |
5859
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
183 fd = gaim_remote_session_connect(0); |
9752 | 184 if (fd < 0) { |
185 message(_("Gaim not running (on session 0)\nIs the \"Remote Control\" plugin loaded?\n"), 2); | |
186 return 1; | |
187 } | |
188 p = gaim_remote_packet_new(type, subtype); | |
189 gaim_remote_session_send_packet(fd, p); | |
190 close(fd); | |
191 gaim_remote_packet_free(p); | |
192 | |
193 return 0; | |
194 } | |
195 | |
196 static int | |
197 send_command_uri() { | |
198 int fd = 0; | |
199 GaimRemotePacket *p = NULL; | |
200 | |
201 fd = gaim_remote_session_connect(0); | |
202 if (fd < 0) { | |
203 message(_("Gaim not running (on session 0)\nIs the \"Remote Control\" plugin loaded?\n"), 2); | |
3480 | 204 return 1; |
205 } | |
5859
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
206 p = gaim_remote_packet_new(CUI_TYPE_REMOTE, CUI_REMOTE_URI); |
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
207 gaim_remote_packet_append_string(p, opts.uri); |
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
208 gaim_remote_session_send_packet(fd, p); |
3480 | 209 close(fd); |
5859
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
210 gaim_remote_packet_free(p); |
3480 | 211 |
3559 | 212 return 0; |
213 } | |
214 | |
9752 | 215 static void |
216 show_longhelp( char *name, char *command) | |
9608 | 217 { |
9752 | 218 if (!strcmp(command, "uri")) { |
7724 | 219 message(_("\n" |
220 "Using AIM: URIs:\n" | |
8152 | 221 "Sending an IM to a screen name:\n" |
7724 | 222 " gaim-remote uri 'aim:goim?screenname=Penguin&message=hello+world'\n" |
8152 | 223 "In this case, 'Penguin' is the screen name we wish to IM, and 'hello world'\n" |
7724 | 224 "is the message to be sent. '+' must be used in place of spaces.\n" |
225 "Please note the quoting used above - if you run this from a shell the '&'\n" | |
226 "needs to be escaped, or the command will stop at that point.\n" | |
8152 | 227 "Also,the following will just open a conversation window to a screen name,\n" |
7724 | 228 "with no message:\n" |
229 " gaim-remote uri 'aim:goim?screenname=Penguin'\n\n" | |
230 "Joining a chat:\n" | |
231 " gaim-remote uri 'aim:gochat?roomname=PenguinLounge'\n" | |
232 "...joins the 'PenguinLounge' chat room.\n\n" | |
233 "Adding a buddy to your buddy list:\n" | |
234 " gaim-remote uri 'aim:addbuddy?screenname=Penguin'\n" | |
235 "...prompts you to add 'Penguin' to your buddy list.\n"), 1); | |
5116 | 236 } |
9695 | 237 |
238 else if (!strcmp(command, "quit")) { | |
7724 | 239 message(_("\nClose running copy of Gaim\n"), 1); |
5116 | 240 } |
9695 | 241 |
242 else if (!strcmp(command, "away")) { | |
243 message(_("\nMark all accounts as \"away\" with the default message.\n"), 1); | |
244 } | |
245 | |
246 else if (!strcmp(command, "back")) { | |
247 message(_("\nSet all accounts as not away.\n"), 1); | |
248 } | |
249 | |
5116 | 250 else { |
251 show_remote_usage(name); | |
252 } | |
4242 | 253 } |
254 | |
9752 | 255 int main(int argc, char *argv[]) |
3480 | 256 { |
5116 | 257 #ifdef ENABLE_NLS |
258 setlocale (LC_ALL, ""); | |
259 bindtextdomain(PACKAGE, LOCALEDIR); | |
260 bind_textdomain_codeset(PACKAGE, "UTF-8"); | |
261 textdomain(PACKAGE); | |
262 #endif | |
263 | |
3480 | 264 if (get_options(argc, argv)) { |
265 show_remote_usage(argv[0]); | |
266 return 0; | |
267 } | |
9674 | 268 |
3480 | 269 if (!strcmp(opts.command, "uri")) { |
9674 | 270 if (opts.help) |
271 show_longhelp(argv[0], "uri"); | |
272 else | |
9752 | 273 return send_command_uri(); |
9674 | 274 } |
275 | |
276 else if (!strcmp(opts.command, "away")) { | |
277 if (opts.help) | |
278 show_longhelp(argv[0], "away"); | |
279 else | |
9752 | 280 return send_generic_command(CUI_TYPE_USER, CUI_USER_AWAY); |
9674 | 281 } |
282 | |
283 else if (!strcmp(opts.command, "back")) { | |
284 if (opts.help) | |
285 show_longhelp(argv[0], "back"); | |
286 else | |
9752 | 287 return send_generic_command(CUI_TYPE_USER, CUI_USER_BACK); |
9674 | 288 } |
289 | |
290 else if (!strcmp(opts.command, "quit")) { | |
291 if (opts.help) | |
292 show_longhelp(argv[0], "quit"); | |
293 else | |
9752 | 294 return send_generic_command(CUI_TYPE_META, CUI_META_QUIT); |
9674 | 295 } |
296 | |
297 else { | |
3480 | 298 show_remote_usage(argv[0]); |
299 return 1; | |
300 } | |
301 | |
302 return 0; | |
303 } |