Mercurial > pidgin.yaz
annotate src/gaim-remote.c @ 10003:33431f91e4a8
[gaim-migrate @ 10920]
(21:42:10) JBSilvest: see away msg :)
(21:42:29) Luke: awesome
(21:42:47) JBSilvest: it could probably use a glance at by a second pair of
eyes, but it works
(21:43:02) JBSilvest: oh, i should do the obligatory thing, shouldn't i?
(21:43:15) JBSilvest: [^H]
(21:43:31) JBSilvest: Hello World
(21:43:53) JBSilvest: [john@tux-laptop gaim]$ gaim-remote send -t lschiere
-f JBSilvest -p prpl-oscar -m "Hello World"
(21:44:27) JBSilvest: i shain't think there are any memleak risks or other
baddies, but i won't profess to being a highly skilled C programmer
(21:45:06) Luke: so is it ready?
(21:45:16) JBSilvest: more or less
(21:45:26) JBSilvest: could use more strings/docs, but it definitely works
(21:47:51) JBSilvest: someone will no doubt bitch & moan about 9999-char
limit, but until someone can recommend a better way || i redo the whole
sys., that'll be it
(21:49:13) JBSilvest: *and* thinking about things, there is blessedly no
risk of remote buffer overflow or any such crap :-D
(21:49:21) JBSilvest: it has to be run by the user :)
(21:49:25) Luke: okay, so this patch does what now? it lets you send and
not just put text in the input widget and what else?
(21:49:40) Luke: or the web browser
(21:49:42) JBSilvest: it lets you send...period :)
(21:49:52) JBSilvest: from the command line/scripts
(21:49:56) Luke: for instance if you made gaim-remote your handler for aim:
uris
(21:50:02) JBSilvest: no
(21:50:06) JBSilvest: that's not what this does
(21:50:12) JBSilvest: it doesn't parse aim:uris
(21:50:16) Luke: ah
(21:50:17) JBSilvest: it's for sending messages themselves
committer: Tailor Script <tailor@pidgin.im>
author | Luke Schierer <lschiere@pidgin.im> |
---|---|
date | Fri, 10 Sep 2004 01:51:02 +0000 |
parents | 8793c7127c80 |
children | c892b6dbc341 |
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 " add Add buddy to buddy list\n" | |
3559 | 36 " remove Remove buddy from list\n" |
4242 | 37 " -q, --quiet Send message without showing a conversation\n" |
3480 | 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; | |
219 char temp[10003]; /*TODO: Future implementation should send packets instead */ | |
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 | |
228 /*Format is as follows: | |
229 *Each string has a 4 character 'header' containing the length of the string | |
230 *The strings are: To, From, Protocol name, Message | |
231 *Following the message is the quiet flag, expressed in a single int (0/1) | |
232 *Because the header is 4 characters long, there is a 9999 char limit on any | |
233 *given string, though none of these strings should be exceeding this. | |
234 *-JBS | |
235 */ | |
236 | |
237 if(opts.to && *opts.to && opts.from && *opts.from && opts.protocol && *opts.protocol && opts.message && *opts.message && (strlen(opts.to) <10000) && (strlen(opts.from) <10000) && (strlen(opts.protocol) <20) && (strlen(opts.message) <10000) ){ | |
238 sprintf(temp, "%04d%s", strlen(opts.to), opts.to); | |
239 gaim_remote_packet_append_string(p, temp); | |
240 sprintf(temp, "%04d%s", strlen(opts.from), opts.from); | |
241 gaim_remote_packet_append_string(p, temp); | |
242 sprintf(temp, "%04d%s", strlen(opts.protocol), opts.protocol); | |
243 gaim_remote_packet_append_string(p, temp); | |
244 sprintf(temp, "%04d%s", strlen(opts.message), opts.message); | |
245 gaim_remote_packet_append_string(p, temp); | |
246 sprintf(temp, "%d", 0);/*quiet flag - off for now*/ | |
247 gaim_remote_packet_append_string(p, temp); | |
248 | |
249 gaim_remote_session_send_packet (fd, p); | |
250 close(fd); | |
251 gaim_remote_packet_free(p); | |
252 return 0; | |
253 }else{ | |
254 message(_("Insufficient arguments (-t, -f, -p, & -m are all required) or arguments greater than 9999 chars\n"), 2); | |
255 close(fd); | |
256 gaim_remote_packet_free(p); | |
257 return 1; | |
258 } | |
259 | |
260 } | |
261 | |
9752 | 262 static void |
263 show_longhelp( char *name, char *command) | |
9608 | 264 { |
9752 | 265 if (!strcmp(command, "uri")) { |
7724 | 266 message(_("\n" |
267 "Using AIM: URIs:\n" | |
8152 | 268 "Sending an IM to a screen name:\n" |
7724 | 269 " gaim-remote uri 'aim:goim?screenname=Penguin&message=hello+world'\n" |
8152 | 270 "In this case, 'Penguin' is the screen name we wish to IM, and 'hello world'\n" |
7724 | 271 "is the message to be sent. '+' must be used in place of spaces.\n" |
272 "Please note the quoting used above - if you run this from a shell the '&'\n" | |
273 "needs to be escaped, or the command will stop at that point.\n" | |
8152 | 274 "Also,the following will just open a conversation window to a screen name,\n" |
7724 | 275 "with no message:\n" |
276 " gaim-remote uri 'aim:goim?screenname=Penguin'\n\n" | |
277 "Joining a chat:\n" | |
278 " gaim-remote uri 'aim:gochat?roomname=PenguinLounge'\n" | |
279 "...joins the 'PenguinLounge' chat room.\n\n" | |
280 "Adding a buddy to your buddy list:\n" | |
281 " gaim-remote uri 'aim:addbuddy?screenname=Penguin'\n" | |
282 "...prompts you to add 'Penguin' to your buddy list.\n"), 1); | |
5116 | 283 } |
9695 | 284 |
285 else if (!strcmp(command, "quit")) { | |
7724 | 286 message(_("\nClose running copy of Gaim\n"), 1); |
5116 | 287 } |
9695 | 288 |
289 else if (!strcmp(command, "away")) { | |
290 message(_("\nMark all accounts as \"away\" with the default message.\n"), 1); | |
291 } | |
292 | |
293 else if (!strcmp(command, "back")) { | |
294 message(_("\nSet all accounts as not away.\n"), 1); | |
295 } | |
296 | |
10003 | 297 else if (!strcmp(command, "send")) { |
298 message(_("\nSend instant message\n"), 1); | |
299 } | |
300 | |
5116 | 301 else { |
302 show_remote_usage(name); | |
303 } | |
4242 | 304 } |
305 | |
9752 | 306 int main(int argc, char *argv[]) |
3480 | 307 { |
5116 | 308 #ifdef ENABLE_NLS |
309 setlocale (LC_ALL, ""); | |
310 bindtextdomain(PACKAGE, LOCALEDIR); | |
311 bind_textdomain_codeset(PACKAGE, "UTF-8"); | |
312 textdomain(PACKAGE); | |
313 #endif | |
314 | |
3480 | 315 if (get_options(argc, argv)) { |
316 show_remote_usage(argv[0]); | |
317 return 0; | |
318 } | |
9674 | 319 |
3480 | 320 if (!strcmp(opts.command, "uri")) { |
9674 | 321 if (opts.help) |
322 show_longhelp(argv[0], "uri"); | |
323 else | |
9752 | 324 return send_command_uri(); |
9674 | 325 } |
326 | |
10003 | 327 else if (!strcmp(opts.command, "send")) { |
328 if (opts.help) | |
329 show_longhelp(argv[0], "send"); | |
330 else | |
331 return send_command_send(); | |
332 } | |
333 | |
9674 | 334 else if (!strcmp(opts.command, "away")) { |
335 if (opts.help) | |
336 show_longhelp(argv[0], "away"); | |
337 else | |
9752 | 338 return send_generic_command(CUI_TYPE_USER, CUI_USER_AWAY); |
9674 | 339 } |
340 | |
341 else if (!strcmp(opts.command, "back")) { | |
342 if (opts.help) | |
343 show_longhelp(argv[0], "back"); | |
344 else | |
9752 | 345 return send_generic_command(CUI_TYPE_USER, CUI_USER_BACK); |
9674 | 346 } |
347 | |
348 else if (!strcmp(opts.command, "quit")) { | |
349 if (opts.help) | |
350 show_longhelp(argv[0], "quit"); | |
351 else | |
9752 | 352 return send_generic_command(CUI_TYPE_META, CUI_META_QUIT); |
9674 | 353 } |
354 | |
355 else { | |
3480 | 356 show_remote_usage(argv[0]); |
357 return 1; | |
358 } | |
359 | |
360 return 0; | |
361 } |