Mercurial > pidgin.yaz
annotate src/gaim-remote.c @ 10196:760e690a5f30
[gaim-migrate @ 11313]
A few steps closer to being able to set an oscar away message.
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Tue, 16 Nov 2004 23:29:53 +0000 |
parents | 1a91e814e9d8 |
children | b4b9dabdd7c7 |
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 |
10005 | 30 /* To be implemented: |
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 " add Add buddy to buddy list\n" | |
36 " remove Remove buddy from list\n" | |
37 " -q, --quiet Send message without showing a conversation\n" | |
38 " window\n" | |
4242 | 39 */ |
3480 | 40 |
41 static struct option longopts[] = { | |
42 {"message", required_argument, NULL, 'm'}, | |
43 {"to", required_argument, NULL, 't'}, | |
44 {"protocol",required_argument, NULL, 'p'}, | |
45 {"from", required_argument, NULL, 'f'}, | |
10086 | 46 {"session", required_argument, NULL, 's'}, |
3480 | 47 {"quiet", no_argument, NULL, 'q'}, |
48 {"help", no_argument, NULL, 'h'}, | |
49 {0,0,0,0} | |
50 }; | |
51 | |
52 struct remoteopts { | |
53 char *command; | |
54 char *uri; | |
55 gboolean help, quiet; | |
10086 | 56 char *message, *to, *from, *protocol, *session; |
10003 | 57 /*int protocol;*/ |
3480 | 58 }; |
9752 | 59 struct remoteopts opts; |
3480 | 60 |
9752 | 61 /* |
62 * Prints a message to the terminal/shell/console. | |
63 * We try to convert "text" from UTF-8 to the user's locale. | |
64 * If that fails then UTF-8 is used as a fallback. | |
65 * | |
66 * If channel is 1, the message is printed to stdout. | |
67 * if channel is 2, the message is printed to stderr. | |
68 */ | |
69 static void | |
70 message(char *text, int channel) | |
71 { | |
72 char *text_conv = NULL,*text_output; | |
73 GError *error = NULL; | |
74 | |
75 text_conv = g_locale_from_utf8(text, -1, NULL, NULL, &error); | |
76 | |
77 if (text_conv == NULL) { | |
78 g_warning("%s\n", error->message); | |
79 g_error_free(error); | |
80 } | |
81 | |
82 text_output = (text_conv ? text_conv : text); | |
3480 | 83 |
9752 | 84 switch (channel) { |
85 case 1: | |
86 puts(text_output); | |
87 break; | |
88 case 2: | |
89 fputs(text_output, stderr); | |
90 break; | |
91 default: | |
92 break; | |
93 } | |
94 | |
95 if (text_conv) | |
96 g_free(text_conv); | |
97 } | |
98 | |
99 static void | |
100 show_remote_usage(const char *name) | |
101 { | |
102 char *text = NULL; | |
103 | |
104 text = g_strdup_printf(_("Usage: %s command [OPTIONS] [URI]\n\n" | |
105 " COMMANDS:\n" | |
106 " uri Handle AIM: URI\n" | |
107 " away Popup the away dialog with the default message\n" | |
108 " back Remove the away dialog\n" | |
10003 | 109 " send Send message\n" |
9752 | 110 " quit Close running copy of Gaim\n\n" |
111 " OPTIONS:\n" | |
10003 | 112 " -m, --message=MESG Message to send or show in conversation window\n" |
113 " -t, --to=SCREENNAME Select a target for command\n" | |
114 " -p, --protocol=PROTO Specify protocol to use\n" | |
115 " -f, --from=SCREENNAME Specify screen name to use\n" | |
10086 | 116 " -s, --session=SESSION Specify which Gaim session to use\n" |
9752 | 117 " -h, --help [command] Show help for command\n"), name); |
118 | |
119 message(text, 1); | |
120 g_free(text); | |
121 | |
122 return; | |
123 } | |
124 | |
125 int | |
126 get_options(int argc, char *argv[]) | |
3480 | 127 { |
128 int i; | |
9752 | 129 |
3480 | 130 memset(&opts, 0, sizeof(opts)); |
10003 | 131 /*opts.protocol = -1;*/ |
9752 | 132 |
10086 | 133 while ((i=getopt_long(argc, argv, "m:t:p:f:s:qh", longopts, NULL)) != -1) { |
3480 | 134 switch (i) { |
135 case 'm': | |
136 opts.message = optarg; | |
137 break; | |
138 case 't': | |
139 opts.to = optarg; | |
140 break; | |
141 case 'p': | |
10003 | 142 opts.protocol = optarg; |
3480 | 143 break; |
144 case 'f': | |
145 opts.from = optarg; | |
146 break; | |
10086 | 147 case 's': |
148 opts.session = optarg; | |
149 break; | |
3480 | 150 case 'q': |
151 opts.quiet = TRUE; | |
152 break; | |
153 case 'h': | |
154 opts.help = TRUE; | |
155 break; | |
156 } | |
157 } | |
10086 | 158 |
3480 | 159 /* We must have non getopt'ed argument-- the command */ |
160 if (optind < argc) | |
161 opts.command = g_strdup(argv[optind++]); | |
162 else | |
163 return 1; | |
164 | |
9674 | 165 if (opts.help) |
6643 | 166 return 0; |
167 | |
3480 | 168 /* And we can have another argument--the URI. */ |
6643 | 169 /* but only if we're using the uri command. */ |
170 if (!strcmp(opts.command, "uri")) { | |
9674 | 171 if (argc-optind == 1) |
3480 | 172 opts.uri = g_strdup(argv[optind++]); |
173 else | |
174 return 1; | |
9752 | 175 } else if (optind == argc) |
6643 | 176 return 0; |
177 else | |
178 return 1; | |
179 | |
10086 | 180 return 0; |
181 } | |
182 | |
183 static int | |
184 open_session() { | |
185 int fd = 0, session = 0; | |
186 char *msg; | |
187 | |
188 if (opts.session != NULL) | |
189 session = atoi(opts.session); | |
190 | |
191 fd = gaim_remote_session_connect(session); | |
192 if (fd < 0) { | |
193 msg = g_strdup_printf(_("Gaim not running (on session %d)\nIs the \"Remote Control\" plugin loaded?\n"), session); | |
194 message(msg, 2); | |
195 g_free(msg); | |
196 return -1; | |
197 } | |
198 | |
199 return fd; | |
3480 | 200 } |
201 | |
9752 | 202 static int |
203 send_generic_command(guchar type, guchar subtype) { | |
3480 | 204 int fd = 0; |
5859
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
205 GaimRemotePacket *p = NULL; |
9752 | 206 |
10086 | 207 fd = open_session(); |
9752 | 208 if (fd < 0) { |
209 return 1; | |
210 } | |
211 p = gaim_remote_packet_new(type, subtype); | |
212 gaim_remote_session_send_packet(fd, p); | |
213 close(fd); | |
214 gaim_remote_packet_free(p); | |
215 | |
216 return 0; | |
217 } | |
218 | |
219 static int | |
220 send_command_uri() { | |
221 int fd = 0; | |
222 GaimRemotePacket *p = NULL; | |
223 | |
10086 | 224 fd = open_session(); |
9752 | 225 if (fd < 0) { |
3480 | 226 return 1; |
227 } | |
5859
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
228 p = gaim_remote_packet_new(CUI_TYPE_REMOTE, CUI_REMOTE_URI); |
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
229 gaim_remote_packet_append_string(p, opts.uri); |
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
230 gaim_remote_session_send_packet(fd, p); |
3480 | 231 close(fd); |
5859
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
232 gaim_remote_packet_free(p); |
3480 | 233 |
3559 | 234 return 0; |
235 } | |
236 | |
10003 | 237 static int |
238 send_command_send() { | |
239 int fd = 0; | |
240 GaimRemotePacket *p = NULL; | |
10005 | 241 char temp[10003]; /* TODO: Future implementation should send packets instead */ |
10003 | 242 |
10086 | 243 fd = open_session(); |
10003 | 244 if (fd < 0) { |
245 return 1; | |
246 } | |
247 p = gaim_remote_packet_new(CUI_TYPE_REMOTE, CUI_REMOTE_SEND); | |
248 | |
10005 | 249 /* |
250 * Format is as follows: | |
251 * Each string has a 4 character 'header' containing the length of the string | |
252 * The strings are: To, From, Protocol name, Message | |
253 * Following the message is the quiet flag, expressed in a single int (0/1) | |
254 * Because the header is 4 characters long, there is a 9999 char limit on any | |
255 * given string, though none of these strings should be exceeding this. | |
256 * -JBS | |
10003 | 257 */ |
258 | |
10005 | 259 if (opts.to && *opts.to && opts.from && *opts.from && |
260 opts.protocol && *opts.protocol && opts.message && *opts.message && | |
261 (strlen(opts.to) < 10000) && (strlen(opts.from) < 10000) && | |
262 (strlen(opts.protocol) < 20) && (strlen(opts.message) < 10000) ) | |
263 { | |
10112 | 264 sprintf(temp, "%04zd%s", strlen(opts.to), opts.to); |
10003 | 265 gaim_remote_packet_append_string(p, temp); |
10112 | 266 sprintf(temp, "%04zd%s", strlen(opts.from), opts.from); |
10003 | 267 gaim_remote_packet_append_string(p, temp); |
10112 | 268 sprintf(temp, "%04zd%s", strlen(opts.protocol), opts.protocol); |
10003 | 269 gaim_remote_packet_append_string(p, temp); |
10112 | 270 sprintf(temp, "%04zd%s", strlen(opts.message), opts.message); |
10003 | 271 gaim_remote_packet_append_string(p, temp); |
10005 | 272 sprintf(temp, "%d", 0); /* quiet flag - off for now */ |
10003 | 273 gaim_remote_packet_append_string(p, temp); |
274 | |
275 gaim_remote_session_send_packet (fd, p); | |
276 close(fd); | |
277 gaim_remote_packet_free(p); | |
278 return 0; | |
10005 | 279 } else { |
10003 | 280 message(_("Insufficient arguments (-t, -f, -p, & -m are all required) or arguments greater than 9999 chars\n"), 2); |
281 close(fd); | |
282 gaim_remote_packet_free(p); | |
283 return 1; | |
284 } | |
285 } | |
286 | |
9752 | 287 static void |
288 show_longhelp( char *name, char *command) | |
9608 | 289 { |
9752 | 290 if (!strcmp(command, "uri")) { |
7724 | 291 message(_("\n" |
292 "Using AIM: URIs:\n" | |
8152 | 293 "Sending an IM to a screen name:\n" |
7724 | 294 " gaim-remote uri 'aim:goim?screenname=Penguin&message=hello+world'\n" |
8152 | 295 "In this case, 'Penguin' is the screen name we wish to IM, and 'hello world'\n" |
7724 | 296 "is the message to be sent. '+' must be used in place of spaces.\n" |
297 "Please note the quoting used above - if you run this from a shell the '&'\n" | |
298 "needs to be escaped, or the command will stop at that point.\n" | |
8152 | 299 "Also,the following will just open a conversation window to a screen name,\n" |
7724 | 300 "with no message:\n" |
301 " gaim-remote uri 'aim:goim?screenname=Penguin'\n\n" | |
302 "Joining a chat:\n" | |
303 " gaim-remote uri 'aim:gochat?roomname=PenguinLounge'\n" | |
304 "...joins the 'PenguinLounge' chat room.\n\n" | |
305 "Adding a buddy to your buddy list:\n" | |
306 " gaim-remote uri 'aim:addbuddy?screenname=Penguin'\n" | |
307 "...prompts you to add 'Penguin' to your buddy list.\n"), 1); | |
5116 | 308 } |
9695 | 309 |
310 else if (!strcmp(command, "quit")) { | |
7724 | 311 message(_("\nClose running copy of Gaim\n"), 1); |
5116 | 312 } |
9695 | 313 |
314 else if (!strcmp(command, "away")) { | |
315 message(_("\nMark all accounts as \"away\" with the default message.\n"), 1); | |
316 } | |
317 | |
318 else if (!strcmp(command, "back")) { | |
319 message(_("\nSet all accounts as not away.\n"), 1); | |
320 } | |
321 | |
10003 | 322 else if (!strcmp(command, "send")) { |
323 message(_("\nSend instant message\n"), 1); | |
324 } | |
325 | |
5116 | 326 else { |
327 show_remote_usage(name); | |
328 } | |
4242 | 329 } |
330 | |
9752 | 331 int main(int argc, char *argv[]) |
3480 | 332 { |
5116 | 333 #ifdef ENABLE_NLS |
334 setlocale (LC_ALL, ""); | |
335 bindtextdomain(PACKAGE, LOCALEDIR); | |
336 bind_textdomain_codeset(PACKAGE, "UTF-8"); | |
337 textdomain(PACKAGE); | |
338 #endif | |
339 | |
3480 | 340 if (get_options(argc, argv)) { |
341 show_remote_usage(argv[0]); | |
342 return 0; | |
343 } | |
9674 | 344 |
3480 | 345 if (!strcmp(opts.command, "uri")) { |
9674 | 346 if (opts.help) |
347 show_longhelp(argv[0], "uri"); | |
348 else | |
9752 | 349 return send_command_uri(); |
9674 | 350 } |
351 | |
10003 | 352 else if (!strcmp(opts.command, "send")) { |
353 if (opts.help) | |
354 show_longhelp(argv[0], "send"); | |
355 else | |
356 return send_command_send(); | |
357 } | |
358 | |
9674 | 359 else if (!strcmp(opts.command, "away")) { |
360 if (opts.help) | |
361 show_longhelp(argv[0], "away"); | |
362 else | |
9752 | 363 return send_generic_command(CUI_TYPE_USER, CUI_USER_AWAY); |
9674 | 364 } |
365 | |
366 else if (!strcmp(opts.command, "back")) { | |
367 if (opts.help) | |
368 show_longhelp(argv[0], "back"); | |
369 else | |
9752 | 370 return send_generic_command(CUI_TYPE_USER, CUI_USER_BACK); |
9674 | 371 } |
372 | |
373 else if (!strcmp(opts.command, "quit")) { | |
374 if (opts.help) | |
375 show_longhelp(argv[0], "quit"); | |
376 else | |
9752 | 377 return send_generic_command(CUI_TYPE_META, CUI_META_QUIT); |
9674 | 378 } |
379 | |
380 else { | |
3480 | 381 show_remote_usage(argv[0]); |
382 return 1; | |
383 } | |
384 | |
385 return 0; | |
386 } |