Mercurial > pidgin
annotate src/gaim-remote.c @ 10007:267a2b69e36e
[gaim-migrate @ 10924]
Better i18nability and more uniform code?
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Fri, 10 Sep 2004 04:30:37 +0000 |
parents | c892b6dbc341 |
children | 6cd2b467e303 |
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'}, | |
46 {"quiet", no_argument, NULL, 'q'}, | |
47 {"help", no_argument, NULL, 'h'}, | |
48 {0,0,0,0} | |
49 }; | |
50 | |
51 struct remoteopts { | |
52 char *command; | |
53 char *uri; | |
54 gboolean help, quiet; | |
10003 | 55 char *message, *to, *from, *protocol; |
56 /*int protocol;*/ | |
3480 | 57 }; |
9752 | 58 struct remoteopts opts; |
3480 | 59 |
9752 | 60 /* |
61 * Prints a message to the terminal/shell/console. | |
62 * We try to convert "text" from UTF-8 to the user's locale. | |
63 * If that fails then UTF-8 is used as a fallback. | |
64 * | |
65 * If channel is 1, the message is printed to stdout. | |
66 * if channel is 2, the message is printed to stderr. | |
67 */ | |
68 static void | |
69 message(char *text, int channel) | |
70 { | |
71 char *text_conv = NULL,*text_output; | |
72 GError *error = NULL; | |
73 | |
74 text_conv = g_locale_from_utf8(text, -1, NULL, NULL, &error); | |
75 | |
76 if (text_conv == NULL) { | |
77 g_warning("%s\n", error->message); | |
78 g_error_free(error); | |
79 } | |
80 | |
81 text_output = (text_conv ? text_conv : text); | |
3480 | 82 |
9752 | 83 switch (channel) { |
84 case 1: | |
85 puts(text_output); | |
86 break; | |
87 case 2: | |
88 fputs(text_output, stderr); | |
89 break; | |
90 default: | |
91 break; | |
92 } | |
93 | |
94 if (text_conv) | |
95 g_free(text_conv); | |
96 } | |
97 | |
98 static void | |
99 show_remote_usage(const char *name) | |
100 { | |
101 char *text = NULL; | |
102 | |
103 text = g_strdup_printf(_("Usage: %s command [OPTIONS] [URI]\n\n" | |
104 " COMMANDS:\n" | |
105 " uri Handle AIM: URI\n" | |
106 " away Popup the away dialog with the default message\n" | |
107 " back Remove the away dialog\n" | |
10003 | 108 " send Send message\n" |
9752 | 109 " quit Close running copy of Gaim\n\n" |
110 " OPTIONS:\n" | |
10003 | 111 " -m, --message=MESG Message to send or show in conversation window\n" |
112 " -t, --to=SCREENNAME Select a target for command\n" | |
113 " -p, --protocol=PROTO Specify protocol to use\n" | |
114 " -f, --from=SCREENNAME Specify screen name to use\n" | |
9752 | 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)); |
10003 | 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': | |
10003 | 140 opts.protocol = optarg; |
3480 | 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 | |
10003 | 215 static int |
216 send_command_send() { | |
217 int fd = 0; | |
218 GaimRemotePacket *p = NULL; | |
10005 | 219 char temp[10003]; /* TODO: Future implementation should send packets instead */ |
10003 | 220 |
221 fd = gaim_remote_session_connect(0); | |
222 if (fd < 0) { | |
223 message(_("Gaim not running (on session 0)\nIs the \"Remote Control\" plugin loaded?\n"), 2); | |
224 return 1; | |
225 } | |
226 p = gaim_remote_packet_new(CUI_TYPE_REMOTE, CUI_REMOTE_SEND); | |
227 | |
10005 | 228 /* |
229 * Format is as follows: | |
230 * Each string has a 4 character 'header' containing the length of the string | |
231 * The strings are: To, From, Protocol name, Message | |
232 * Following the message is the quiet flag, expressed in a single int (0/1) | |
233 * Because the header is 4 characters long, there is a 9999 char limit on any | |
234 * given string, though none of these strings should be exceeding this. | |
235 * -JBS | |
10003 | 236 */ |
237 | |
10005 | 238 if (opts.to && *opts.to && opts.from && *opts.from && |
239 opts.protocol && *opts.protocol && opts.message && *opts.message && | |
240 (strlen(opts.to) < 10000) && (strlen(opts.from) < 10000) && | |
241 (strlen(opts.protocol) < 20) && (strlen(opts.message) < 10000) ) | |
242 { | |
10003 | 243 sprintf(temp, "%04d%s", strlen(opts.to), opts.to); |
244 gaim_remote_packet_append_string(p, temp); | |
245 sprintf(temp, "%04d%s", strlen(opts.from), opts.from); | |
246 gaim_remote_packet_append_string(p, temp); | |
247 sprintf(temp, "%04d%s", strlen(opts.protocol), opts.protocol); | |
248 gaim_remote_packet_append_string(p, temp); | |
249 sprintf(temp, "%04d%s", strlen(opts.message), opts.message); | |
250 gaim_remote_packet_append_string(p, temp); | |
10005 | 251 sprintf(temp, "%d", 0); /* quiet flag - off for now */ |
10003 | 252 gaim_remote_packet_append_string(p, temp); |
253 | |
254 gaim_remote_session_send_packet (fd, p); | |
255 close(fd); | |
256 gaim_remote_packet_free(p); | |
257 return 0; | |
10005 | 258 } else { |
10003 | 259 message(_("Insufficient arguments (-t, -f, -p, & -m are all required) or arguments greater than 9999 chars\n"), 2); |
260 close(fd); | |
261 gaim_remote_packet_free(p); | |
262 return 1; | |
263 } | |
264 } | |
265 | |
9752 | 266 static void |
267 show_longhelp( char *name, char *command) | |
9608 | 268 { |
9752 | 269 if (!strcmp(command, "uri")) { |
7724 | 270 message(_("\n" |
271 "Using AIM: URIs:\n" | |
8152 | 272 "Sending an IM to a screen name:\n" |
7724 | 273 " gaim-remote uri 'aim:goim?screenname=Penguin&message=hello+world'\n" |
8152 | 274 "In this case, 'Penguin' is the screen name we wish to IM, and 'hello world'\n" |
7724 | 275 "is the message to be sent. '+' must be used in place of spaces.\n" |
276 "Please note the quoting used above - if you run this from a shell the '&'\n" | |
277 "needs to be escaped, or the command will stop at that point.\n" | |
8152 | 278 "Also,the following will just open a conversation window to a screen name,\n" |
7724 | 279 "with no message:\n" |
280 " gaim-remote uri 'aim:goim?screenname=Penguin'\n\n" | |
281 "Joining a chat:\n" | |
282 " gaim-remote uri 'aim:gochat?roomname=PenguinLounge'\n" | |
283 "...joins the 'PenguinLounge' chat room.\n\n" | |
284 "Adding a buddy to your buddy list:\n" | |
285 " gaim-remote uri 'aim:addbuddy?screenname=Penguin'\n" | |
286 "...prompts you to add 'Penguin' to your buddy list.\n"), 1); | |
5116 | 287 } |
9695 | 288 |
289 else if (!strcmp(command, "quit")) { | |
7724 | 290 message(_("\nClose running copy of Gaim\n"), 1); |
5116 | 291 } |
9695 | 292 |
293 else if (!strcmp(command, "away")) { | |
294 message(_("\nMark all accounts as \"away\" with the default message.\n"), 1); | |
295 } | |
296 | |
297 else if (!strcmp(command, "back")) { | |
298 message(_("\nSet all accounts as not away.\n"), 1); | |
299 } | |
300 | |
10003 | 301 else if (!strcmp(command, "send")) { |
302 message(_("\nSend instant message\n"), 1); | |
303 } | |
304 | |
5116 | 305 else { |
306 show_remote_usage(name); | |
307 } | |
4242 | 308 } |
309 | |
9752 | 310 int main(int argc, char *argv[]) |
3480 | 311 { |
5116 | 312 #ifdef ENABLE_NLS |
313 setlocale (LC_ALL, ""); | |
314 bindtextdomain(PACKAGE, LOCALEDIR); | |
315 bind_textdomain_codeset(PACKAGE, "UTF-8"); | |
316 textdomain(PACKAGE); | |
317 #endif | |
318 | |
3480 | 319 if (get_options(argc, argv)) { |
320 show_remote_usage(argv[0]); | |
321 return 0; | |
322 } | |
9674 | 323 |
3480 | 324 if (!strcmp(opts.command, "uri")) { |
9674 | 325 if (opts.help) |
326 show_longhelp(argv[0], "uri"); | |
327 else | |
9752 | 328 return send_command_uri(); |
9674 | 329 } |
330 | |
10003 | 331 else if (!strcmp(opts.command, "send")) { |
332 if (opts.help) | |
333 show_longhelp(argv[0], "send"); | |
334 else | |
335 return send_command_send(); | |
336 } | |
337 | |
9674 | 338 else if (!strcmp(opts.command, "away")) { |
339 if (opts.help) | |
340 show_longhelp(argv[0], "away"); | |
341 else | |
9752 | 342 return send_generic_command(CUI_TYPE_USER, CUI_USER_AWAY); |
9674 | 343 } |
344 | |
345 else if (!strcmp(opts.command, "back")) { | |
346 if (opts.help) | |
347 show_longhelp(argv[0], "back"); | |
348 else | |
9752 | 349 return send_generic_command(CUI_TYPE_USER, CUI_USER_BACK); |
9674 | 350 } |
351 | |
352 else if (!strcmp(opts.command, "quit")) { | |
353 if (opts.help) | |
354 show_longhelp(argv[0], "quit"); | |
355 else | |
9752 | 356 return send_generic_command(CUI_TYPE_META, CUI_META_QUIT); |
9674 | 357 } |
358 | |
359 else { | |
3480 | 360 show_remote_usage(argv[0]); |
361 return 1; | |
362 } | |
363 | |
364 return 0; | |
365 } |