Mercurial > pidgin.yaz
annotate src/gaim-remote.c @ 8888:8592f296db16
[gaim-migrate @ 9657]
Eh, why not?
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Thu, 06 May 2004 05:32:35 +0000 |
parents | 8b935eddeb10 |
children | d9640b38a65b |
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 |
7724 | 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 | |
4242 | 61 void show_remote_usage(char *name) |
3480 | 62 { |
7724 | 63 char *text=NULL; |
64 | |
65 text=g_strdup_printf(_("Usage: %s command [OPTIONS] [URI]\n\n" | |
4242 | 66 |
3480 | 67 " COMMANDS:\n" |
4242 | 68 " uri Handle AIM: URI\n" |
69 " quit Close running copy of Gaim\n\n" | |
70 | |
71 " OPTIONS:\n" | |
72 " -h, --help [commmand] Show help for command\n"), name); | |
7724 | 73 |
74 message(text,1); | |
75 g_free(text); | |
76 | |
4242 | 77 return; |
78 } | |
79 | |
80 /*To be implemented: | |
3480 | 81 " info Show information about connected accounts\n" |
82 " list Print buddy list\n" | |
83 " ison Show presence state of your buddy\n" | |
84 " convo Open a new conversation window\n" | |
85 " send Send message\n" | |
86 " add Add buddy to buddy list\n" | |
3559 | 87 " remove Remove buddy from list\n" |
3480 | 88 " -m, --message=MESG Message to send or show in conversation window\n" |
89 " -t, --to=SCREENNAME Select a target for command\n" | |
90 " -p, --protocol=PROTO Specify protocol to use\n" | |
8154 | 91 " -f, --from=SCREENNAME Specify screen name to use\n" |
4242 | 92 " -q, --quiet Send message without showing a conversation\n" |
3480 | 93 " window\n" |
4242 | 94 */ |
3480 | 95 |
96 static struct option longopts[] = { | |
97 {"message", required_argument, NULL, 'm'}, | |
98 {"to", required_argument, NULL, 't'}, | |
99 {"protocol",required_argument, NULL, 'p'}, | |
100 {"from", required_argument, NULL, 'f'}, | |
101 {"quiet", no_argument, NULL, 'q'}, | |
102 {"help", no_argument, NULL, 'h'}, | |
103 {0,0,0,0} | |
104 }; | |
105 | |
106 struct remoteopts { | |
107 char *command; | |
108 char *uri; | |
109 gboolean help, quiet; | |
110 char *message, *to, *from; | |
111 int protocol; | |
112 }; | |
113 | |
114 | |
115 struct remoteopts opts; | |
116 int get_options(int argc, char *argv[]) | |
117 { | |
118 int i; | |
119 memset(&opts, 0, sizeof(opts)); | |
120 opts.protocol = -1; | |
121 while ((i=getopt_long(argc, argv, "m:t:p:f:qh", longopts, NULL)) != -1) { | |
122 switch (i) { | |
123 case 'm': | |
124 opts.message = optarg; | |
125 break; | |
126 case 't': | |
127 opts.to = optarg; | |
128 break; | |
129 case 'p': | |
130 /* Do stuff here. */ | |
131 break; | |
132 case 'f': | |
133 opts.from = optarg; | |
134 break; | |
135 case 'q': | |
136 opts.quiet = TRUE; | |
137 break; | |
138 case 'h': | |
139 opts.help = TRUE; | |
140 break; | |
141 } | |
142 } | |
143 | |
144 /* We must have non getopt'ed argument-- the command */ | |
145 if (optind < argc) | |
146 opts.command = g_strdup(argv[optind++]); | |
147 else | |
148 return 1; | |
149 | |
6643 | 150 if(opts.help) |
151 return 0; | |
152 | |
3480 | 153 /* And we can have another argument--the URI. */ |
6643 | 154 /* but only if we're using the uri command. */ |
155 if (!strcmp(opts.command, "uri")) { | |
156 if(argc-optind==1) | |
3480 | 157 opts.uri = g_strdup(argv[optind++]); |
158 else | |
159 return 1; | |
160 } | |
6643 | 161 else if(optind==argc) |
162 return 0; | |
163 else | |
164 return 1; | |
165 | |
166 return 0; | |
3480 | 167 } |
168 | |
169 int command_uri() { | |
170 int fd = 0; | |
5859
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
171 GaimRemotePacket *p = NULL; |
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
172 fd = gaim_remote_session_connect(0); |
6643 | 173 if (fd<0) { |
7724 | 174 message(_("Gaim not running (on session 0)\n"),2); |
3480 | 175 return 1; |
176 } | |
5859
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
177 p = gaim_remote_packet_new(CUI_TYPE_REMOTE, CUI_REMOTE_URI); |
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
178 gaim_remote_packet_append_string(p, opts.uri); |
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
179 gaim_remote_session_send_packet(fd, p); |
3480 | 180 close(fd); |
5859
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
181 gaim_remote_packet_free(p); |
3480 | 182 return 0; |
183 } | |
184 | |
3559 | 185 int command_quit() { |
186 int fd = 0; | |
5859
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
187 GaimRemotePacket *p = NULL; |
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
188 fd = gaim_remote_session_connect(0); |
6643 | 189 if (fd<0) { |
7724 | 190 message(_("Gaim not running (on session 0)\n"),2); |
3559 | 191 return 1; |
192 } | |
5859
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
193 p = gaim_remote_packet_new(CUI_TYPE_META, CUI_META_QUIT); |
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
194 gaim_remote_session_send_packet(fd, p); |
3559 | 195 close(fd); |
5859
022786c7ab53
[gaim-migrate @ 6290]
Christian Hammond <chipx86@chipx86.com>
parents:
5124
diff
changeset
|
196 gaim_remote_packet_free(p); |
3559 | 197 return 0; |
198 } | |
199 | |
7724 | 200 void show_longhelp_uri( char *name, char *command) |
201 { | |
5116 | 202 if(!strcmp(command, "uri")) { |
7724 | 203 message(_("\n" |
204 "Using AIM: URIs:\n" | |
8152 | 205 "Sending an IM to a screen name:\n" |
7724 | 206 " gaim-remote uri 'aim:goim?screenname=Penguin&message=hello+world'\n" |
8152 | 207 "In this case, 'Penguin' is the screen name we wish to IM, and 'hello world'\n" |
7724 | 208 "is the message to be sent. '+' must be used in place of spaces.\n" |
209 "Please note the quoting used above - if you run this from a shell the '&'\n" | |
210 "needs to be escaped, or the command will stop at that point.\n" | |
8152 | 211 "Also,the following will just open a conversation window to a screen name,\n" |
7724 | 212 "with no message:\n" |
213 " gaim-remote uri 'aim:goim?screenname=Penguin'\n\n" | |
214 "Joining a chat:\n" | |
215 " gaim-remote uri 'aim:gochat?roomname=PenguinLounge'\n" | |
216 "...joins the 'PenguinLounge' chat room.\n\n" | |
217 "Adding a buddy to your buddy list:\n" | |
218 " gaim-remote uri 'aim:addbuddy?screenname=Penguin'\n" | |
219 "...prompts you to add 'Penguin' to your buddy list.\n"), 1); | |
5116 | 220 } |
221 else if(!strcmp(command, "quit")) { | |
7724 | 222 message(_("\nClose running copy of Gaim\n"), 1); |
5116 | 223 } |
224 else { | |
225 show_remote_usage(name); | |
226 } | |
4242 | 227 } |
228 | |
229 /* Work in progress - JBS | |
3867 | 230 int command_info(){ |
231 fprintf(stderr, "Info not yet implemented\n"); | |
232 return 1; | |
4242 | 233 }*/ |
3480 | 234 |
3559 | 235 int main (int argc, char *argv[]) |
3480 | 236 { |
237 | |
5116 | 238 #ifdef ENABLE_NLS |
239 setlocale (LC_ALL, ""); | |
240 bindtextdomain(PACKAGE, LOCALEDIR); | |
241 bind_textdomain_codeset(PACKAGE, "UTF-8"); | |
242 textdomain(PACKAGE); | |
243 #endif | |
244 | |
3480 | 245 if (get_options(argc, argv)) { |
246 show_remote_usage(argv[0]); | |
247 return 0; | |
248 } | |
249 | |
250 | |
251 if (!strcmp(opts.command, "uri")) { | |
4242 | 252 if(opts.help){ |
5116 | 253 show_longhelp_uri(argv[0], "uri"); |
4242 | 254 }else{ |
255 return command_uri(); | |
256 } | |
257 /* } else if (!strcmp(opts.command, "info")) { | |
258 return command_info();*/ | |
3559 | 259 } else if (!strcmp(opts.command, "quit")) { |
5116 | 260 if(opts.help){ |
261 show_longhelp_uri(argv[0], "quit"); | |
262 }else{ | |
263 return command_quit(); | |
264 } | |
3480 | 265 } else { |
266 show_remote_usage(argv[0]); | |
267 return 1; | |
268 } | |
269 | |
270 return 0; | |
271 } |