Mercurial > pidgin.yaz
comparison src/gaim-remote.c @ 9752:8793c7127c80
[gaim-migrate @ 10619]
Clean up gaim-remote some and give it a better error message
when the Remote Control plugin isn't loaded
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Sun, 15 Aug 2004 16:17:29 +0000 |
parents | 89110d3665df |
children | 33431f91e4a8 |
comparison
equal
deleted
inserted
replaced
9751:94c643a77913 | 9752:8793c7127c80 |
---|---|
24 #include "internal.h" | 24 #include "internal.h" |
25 | 25 |
26 #include <getopt.h> | 26 #include <getopt.h> |
27 | 27 |
28 #include <gaim-remote/remote.h> | 28 #include <gaim-remote/remote.h> |
29 | |
30 /* writes a message 'text' to screen | |
31 * message tries to convert 'text' from utf-8 to user's locale and | |
32 * uses the original message 'text' as a fallback | |
33 * | |
34 * if channel is 1, the message is printed to stdout | |
35 * if channel is 2, the message is printed to stderr | |
36 */ | |
37 void message(char *text,int channel) | |
38 { | |
39 char *text_conv=NULL,*text_output; | |
40 GError *error=NULL; | |
41 | |
42 text_conv=g_locale_from_utf8(text,-1,NULL,NULL,&error); | |
43 | |
44 if(!text_conv) { | |
45 g_warning("%s\n", error->message); | |
46 g_error_free(error); | |
47 } | |
48 | |
49 text_output=(text_conv ? text_conv : text); | |
50 | |
51 switch (channel) { | |
52 case 1: puts(text_output); break; | |
53 case 2: fputs(text_output, stderr); break; | |
54 default: break; | |
55 } | |
56 | |
57 if(text_conv) | |
58 g_free(text_conv); | |
59 } | |
60 | |
61 void show_remote_usage(char *name) | |
62 { | |
63 char *text = NULL; | |
64 | |
65 text = g_strdup_printf(_("Usage: %s command [OPTIONS] [URI]\n\n" | |
66 " COMMANDS:\n" | |
67 " uri Handle AIM: URI\n" | |
68 " away Popup the away dialog with the default message\n" | |
69 " back Remove the away dialog\n" | |
70 " quit Close running copy of Gaim\n\n" | |
71 " OPTIONS:\n" | |
72 " -h, --help [command] Show help for command\n"), name); | |
73 | |
74 message(text, 1); | |
75 g_free(text); | |
76 | |
77 return; | |
78 } | |
79 | 29 |
80 /*To be implemented: | 30 /*To be implemented: |
81 " info Show information about connected accounts\n" | 31 " info Show information about connected accounts\n" |
82 " list Print buddy list\n" | 32 " list Print buddy list\n" |
83 " ison Show presence state of your buddy\n" | 33 " ison Show presence state of your buddy\n" |
108 char *uri; | 58 char *uri; |
109 gboolean help, quiet; | 59 gboolean help, quiet; |
110 char *message, *to, *from; | 60 char *message, *to, *from; |
111 int protocol; | 61 int protocol; |
112 }; | 62 }; |
113 | |
114 | |
115 struct remoteopts opts; | 63 struct remoteopts opts; |
116 int get_options(int argc, char *argv[]) | 64 |
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); | |
87 | |
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[]) | |
117 { | 125 { |
118 int i; | 126 int i; |
127 | |
119 memset(&opts, 0, sizeof(opts)); | 128 memset(&opts, 0, sizeof(opts)); |
120 opts.protocol = -1; | 129 opts.protocol = -1; |
130 | |
121 while ((i=getopt_long(argc, argv, "m:t:p:f:qh", longopts, NULL)) != -1) { | 131 while ((i=getopt_long(argc, argv, "m:t:p:f:qh", longopts, NULL)) != -1) { |
122 switch (i) { | 132 switch (i) { |
123 case 'm': | 133 case 'm': |
124 opts.message = optarg; | 134 opts.message = optarg; |
125 break; | 135 break; |
155 if (!strcmp(opts.command, "uri")) { | 165 if (!strcmp(opts.command, "uri")) { |
156 if (argc-optind == 1) | 166 if (argc-optind == 1) |
157 opts.uri = g_strdup(argv[optind++]); | 167 opts.uri = g_strdup(argv[optind++]); |
158 else | 168 else |
159 return 1; | 169 return 1; |
160 } | 170 } else if (optind == argc) |
161 else if (optind == argc) | |
162 return 0; | 171 return 0; |
163 else | 172 else |
164 return 1; | 173 return 1; |
165 | 174 |
166 return 0; | 175 return 0; |
167 } | 176 } |
168 | 177 |
169 int command_uri() { | 178 static int |
179 send_generic_command(guchar type, guchar subtype) { | |
170 int fd = 0; | 180 int fd = 0; |
171 GaimRemotePacket *p = NULL; | 181 GaimRemotePacket *p = NULL; |
182 | |
172 fd = gaim_remote_session_connect(0); | 183 fd = gaim_remote_session_connect(0); |
173 if (fd<0) { | 184 if (fd < 0) { |
174 message(_("Gaim not running (on session 0)\n"),2); | 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); | |
175 return 1; | 204 return 1; |
176 } | 205 } |
177 p = gaim_remote_packet_new(CUI_TYPE_REMOTE, CUI_REMOTE_URI); | 206 p = gaim_remote_packet_new(CUI_TYPE_REMOTE, CUI_REMOTE_URI); |
178 gaim_remote_packet_append_string(p, opts.uri); | 207 gaim_remote_packet_append_string(p, opts.uri); |
179 gaim_remote_session_send_packet(fd, p); | 208 gaim_remote_session_send_packet(fd, p); |
180 close(fd); | 209 close(fd); |
181 gaim_remote_packet_free(p); | 210 gaim_remote_packet_free(p); |
211 | |
182 return 0; | 212 return 0; |
183 } | 213 } |
184 | 214 |
185 int command_quit() { | 215 static void |
186 int fd = 0; | 216 show_longhelp( char *name, char *command) |
187 GaimRemotePacket *p = NULL; | 217 { |
188 fd = gaim_remote_session_connect(0); | 218 if (!strcmp(command, "uri")) { |
189 if (fd<0) { | |
190 message(_("Gaim not running (on session 0)\n"),2); | |
191 return 1; | |
192 } | |
193 p = gaim_remote_packet_new(CUI_TYPE_META, CUI_META_QUIT); | |
194 gaim_remote_session_send_packet(fd, p); | |
195 close(fd); | |
196 gaim_remote_packet_free(p); | |
197 return 0; | |
198 } | |
199 | |
200 int command_away() | |
201 { | |
202 int fd = 0; | |
203 GaimRemotePacket *p = NULL; | |
204 fd = gaim_remote_session_connect(0); | |
205 if (fd<0) { | |
206 message(_("Gaim not running (on session 0)\n"),2); | |
207 return 1; | |
208 } | |
209 p = gaim_remote_packet_new(CUI_TYPE_USER, CUI_USER_AWAY); | |
210 gaim_remote_session_send_packet(fd, p); | |
211 close(fd); | |
212 gaim_remote_packet_free(p); | |
213 return 0; | |
214 } | |
215 | |
216 int command_back() | |
217 { | |
218 int fd = 0; | |
219 GaimRemotePacket *p = NULL; | |
220 fd = gaim_remote_session_connect(0); | |
221 if (fd<0) { | |
222 message(_("Gaim not running (on session 0)\n"),2); | |
223 return 1; | |
224 } | |
225 p = gaim_remote_packet_new(CUI_TYPE_USER, CUI_USER_BACK); | |
226 gaim_remote_session_send_packet(fd, p); | |
227 close(fd); | |
228 gaim_remote_packet_free(p); | |
229 return 0; | |
230 } | |
231 | |
232 | |
233 void show_longhelp( char *name, char *command) | |
234 { | |
235 if(!strcmp(command, "uri")) { | |
236 message(_("\n" | 219 message(_("\n" |
237 "Using AIM: URIs:\n" | 220 "Using AIM: URIs:\n" |
238 "Sending an IM to a screen name:\n" | 221 "Sending an IM to a screen name:\n" |
239 " gaim-remote uri 'aim:goim?screenname=Penguin&message=hello+world'\n" | 222 " gaim-remote uri 'aim:goim?screenname=Penguin&message=hello+world'\n" |
240 "In this case, 'Penguin' is the screen name we wish to IM, and 'hello world'\n" | 223 "In this case, 'Penguin' is the screen name we wish to IM, and 'hello world'\n" |
267 else { | 250 else { |
268 show_remote_usage(name); | 251 show_remote_usage(name); |
269 } | 252 } |
270 } | 253 } |
271 | 254 |
272 int main (int argc, char *argv[]) | 255 int main(int argc, char *argv[]) |
273 { | 256 { |
274 | |
275 #ifdef ENABLE_NLS | 257 #ifdef ENABLE_NLS |
276 setlocale (LC_ALL, ""); | 258 setlocale (LC_ALL, ""); |
277 bindtextdomain(PACKAGE, LOCALEDIR); | 259 bindtextdomain(PACKAGE, LOCALEDIR); |
278 bind_textdomain_codeset(PACKAGE, "UTF-8"); | 260 bind_textdomain_codeset(PACKAGE, "UTF-8"); |
279 textdomain(PACKAGE); | 261 textdomain(PACKAGE); |
282 if (get_options(argc, argv)) { | 264 if (get_options(argc, argv)) { |
283 show_remote_usage(argv[0]); | 265 show_remote_usage(argv[0]); |
284 return 0; | 266 return 0; |
285 } | 267 } |
286 | 268 |
287 | |
288 if (!strcmp(opts.command, "uri")) { | 269 if (!strcmp(opts.command, "uri")) { |
289 if (opts.help) | 270 if (opts.help) |
290 show_longhelp(argv[0], "uri"); | 271 show_longhelp(argv[0], "uri"); |
291 else | 272 else |
292 return command_uri(); | 273 return send_command_uri(); |
293 } | 274 } |
294 | 275 |
295 else if (!strcmp(opts.command, "away")) { | 276 else if (!strcmp(opts.command, "away")) { |
296 if (opts.help) | 277 if (opts.help) |
297 show_longhelp(argv[0], "away"); | 278 show_longhelp(argv[0], "away"); |
298 else | 279 else |
299 return command_away(); | 280 return send_generic_command(CUI_TYPE_USER, CUI_USER_AWAY); |
300 } | 281 } |
301 | 282 |
302 else if (!strcmp(opts.command, "back")) { | 283 else if (!strcmp(opts.command, "back")) { |
303 if (opts.help) | 284 if (opts.help) |
304 show_longhelp(argv[0], "back"); | 285 show_longhelp(argv[0], "back"); |
305 else | 286 else |
306 return command_back(); | 287 return send_generic_command(CUI_TYPE_USER, CUI_USER_BACK); |
307 } | 288 } |
308 | 289 |
309 else if (!strcmp(opts.command, "quit")) { | 290 else if (!strcmp(opts.command, "quit")) { |
310 if (opts.help) | 291 if (opts.help) |
311 show_longhelp(argv[0], "quit"); | 292 show_longhelp(argv[0], "quit"); |
312 else | 293 else |
313 return command_quit(); | 294 return send_generic_command(CUI_TYPE_META, CUI_META_QUIT); |
314 } | 295 } |
315 | 296 |
316 else { | 297 else { |
317 show_remote_usage(argv[0]); | 298 show_remote_usage(argv[0]); |
318 return 1; | 299 return 1; |