wmmoonclock: add autogen.
[dockapps.git] / wmget / request.c
blobdc79332bf6a81f2476796d1d3d230b1143942285
1 /*
2 wmget - A background download manager as a Window Maker dock app
3 Copyright (c) 2001-2003 Aaron Trickey <aaron@amtrickey.net>
5 Permission is hereby granted, free of charge, to any person
6 obtaining a copy of this software and associated documentation files
7 (the "Software"), to deal in the Software without restriction,
8 including without limitation the rights to use, copy, modify, merge,
9 publish, distribute, sublicense, and/or sell copies of the Software,
10 and to permit persons to whom the Software is furnished to do so,
11 subject to the following conditions:
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 ********************************************************************
25 request.c - Client code for submitting download requests
27 Invoked whenever wmget is run in "request" mode; submits a job to
28 the server dockapp.
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <getopt.h>
37 #include "wmget.h"
40 static char *enquote_strdup (const char *string)
42 int len = strlen (string);
43 char *newstr;
44 const char *src;
45 char *dest;
47 for (src = string; *src; src += strcspn (src, ")\\")) {
48 ++len;
51 dest = newstr = malloc (len + 1);
53 for (src = string; *src; ++src) {
54 switch (*src) {
55 case ')':
56 case '\\':
57 *dest++ = '\\';
60 *dest++ = *src;
63 *dest = '\0';
65 return newstr;
69 int add_arg_s (char *line, const char *argname, const char *argval)
71 char *qargval;
73 if (strlen (line) + strlen (argname) + strlen (argval) + 3
74 > MAXCMDLEN) {
75 error ("Too much data to send to server!");
76 return 1;
79 qargval = enquote_strdup (argval);
81 line += strlen (line);
82 sprintf (line, " %s(%s)", argname, qargval);
84 free (qargval);
86 return 0;
90 int add_arg_i (char *line, const char *argname, int argval)
92 if (strlen (line) + strlen (argname) + 10
93 > MAXCMDLEN) {
94 error ("Too much data to send to server!");
95 return 1;
98 line += strlen (line);
99 sprintf (line, " %s(%d)", argname, argval);
101 return 0;
105 int request (int argc, char **argv)
107 char line[MAXCMDLEN + 1];
108 char *word_break;
109 FILE *fp;
110 Request req;
112 config_request (argc, argv, &req);
114 if (!req.source_url) {
115 error ("Missing source URL!");
116 usage ();
117 return 1;
120 strcpy (line, CMD_GET);
121 if (add_arg_s (line, ARG_GET_SOURCE_URL, req.source_url))
122 return 1;
124 if (req.display)
125 if (add_arg_s (line, ARG_GET_DISPLAY, req.display))
126 return 1;
128 if (req.save_to)
129 if (add_arg_s (line, ARG_GET_SAVE_TO, req.save_to))
130 return 1;
132 if (req.overwrite != -1)
133 if (add_arg_i (line, ARG_GET_OVERWRITE, req.overwrite))
134 return 1;
136 if (req.continue_from != -1)
137 if (add_arg_i (line, ARG_GET_CONTINUE_FROM, req.continue_from))
138 return 1;
140 if (req.proxy)
141 if (add_arg_s (line, ARG_GET_PROXY, req.proxy))
142 return 1;
144 if (req.follow != -1)
145 if (add_arg_i (line, ARG_GET_FOLLOW, req.follow))
146 return 1;
148 if (req.user_agent)
149 if (add_arg_s (line, ARG_GET_UA, req.user_agent))
150 return 1;
152 if (req.use_ascii != -1)
153 if (add_arg_i (line, ARG_GET_USE_ASCII, req.use_ascii))
154 return 1;
156 if (req.referer)
157 if (add_arg_s (line, ARG_GET_REFERER, req.referer))
158 return 1;
160 if (req.include != -1)
161 if (add_arg_i (line, ARG_GET_INCLUDE, req.include))
162 return 1;
164 if (req.interface)
165 if (add_arg_s (line, ARG_GET_INTERFACE, req.interface))
166 return 1;
168 if (req.proxy_auth)
169 if (add_arg_s (line, ARG_GET_PROXY_AUTH, req.proxy_auth))
170 return 1;
172 if (req.auth)
173 if (add_arg_s (line, ARG_GET_AUTH, req.auth))
174 return 1;
176 debug ("Command line is '%s'", line);
178 strcat (line, "\n");
180 if (!(fp = iq_client_connect ()))
181 return 1;
183 if (fputs (line, fp) == EOF) {
184 error_sys ("Failed to send command to server (fputs)");
185 fclose (fp);
186 return 1;
189 if (!fgets (line, sizeof line - 1, fp)) {
190 error ("Server did not respond to command!");
191 fclose (fp);
192 return 1;
195 /* Extract the first word and compare. */
196 word_break = line + strcspn (line, " \t\r\n");
198 while (*word_break && isspace (*word_break))
199 *word_break++ = 0;
201 if (strcasecmp (line, RESPONSE_JOB_ACCEPTED)) {
202 error ("%s", word_break);
203 fclose (fp);
204 return 1;
207 fclose (fp);
209 /* Since we got a RESPONSE_JOB_ACCEPTED, present the user with the
210 * job ID for future cancellation.
212 printf ("Job ID is %s\n", word_break);
214 return 0;