1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package gnu.hylafax.util;
36
37
38 import gnu.getopt.Getopt;
39 import gnu.hylafax.ClientProtocol;
40 import gnu.hylafax.HylaFAXClient;
41 import gnu.hylafax.Job;
42 import gnu.hylafax.Pagesize;
43
44 import java.awt.Dimension;
45 import java.io.BufferedReader;
46 import java.io.FileInputStream;
47 import java.io.InputStreamReader;
48 import java.io.PrintStream;
49 import java.util.Vector;
50
51 import org.apache.commons.logging.Log;
52 import org.apache.commons.logging.LogFactory;
53
54 /***
55 * This class implements most of the sendfax program as supplied with the
56 * HylaFAX distribution. Not all options/features of the HylaFAX sendfax command
57 * are supported.
58 * <P>
59 * <UL>
60 * Specifically,
61 * <LI>only one Job with one destination can be queued per execution
62 * <LI>only HylaFAX <I>server</I> native file formats can be part of a job.
63 * i.e. no client-side document conversions are performed
64 * <LI>no built-in faxcover support
65 * </UL>
66 * The following command line options are supported.
67 * <P>
68 *
69 * <PRE>
70 *
71 * -h <host> specifiy server hostname -u <user> user to login to the server with
72 * -v verbose mode -d <number> specify a destination FAX <number> -f <sender>
73 * user <sender> as the identity of the FAX sender -k <time> kill the job if it
74 * doesn't complete after the indicated <time> -t <tries> make no more than
75 * <tries> attempts to deliver the FAX -T <dials> maximum number of <dials> to
76 * attempt for each job -D enable delivery notification -R enable delivery and
77 * retry notification -N disable delivery and retry notification -P <pri> assign
78 * the <pri> priority to the job (default: 127) -l use low resolution (98
79 * lines/inch) -m use medium resolution (196 lines/inch) -s <size> specify the
80 * symbolic page <size> (legal, us-let, a3, a4, etc.)
81 *
82 * </PRE>
83 *
84 * <P>
85 * Refer to the sendfax man page (from the HylaFAX distribution) for more
86 * information.
87 * <P>
88 * This program depends on the gnu.getopt package for command line parsing.
89 * gnu.getopt (java-getopt) can be found at <a
90 * href="http://www.urbanophile.com/arenn/">http://www.urbanophile.com/arenn/</a>
91 */
92 public class SendFax {
93
94 private final static Log log = LogFactory.getLog(SendFax.class);
95
96 public static void main(String arguments[]) {
97
98 String user = "fax";
99 String host = "localhost";
100 String destination = null;
101 String from = user;
102 String killtime = "000259";
103 int maxdials = 12;
104 int maxtries = 3;
105 int priority = 127;
106 String notifyaddr = user;
107 int resolution = 98;
108 Dimension pagesize;
109 String notify = "none";
110 String pagechop = "default";
111 int chopthreshold = 3;
112 Vector documents = new Vector();
113 boolean verbose = false;
114 boolean from_is_set = false;
115
116 pagesize = Pagesize.LETTER;
117
118 Getopt g = new Getopt("SendFax", arguments, "k:t:T:DRNP:d:vf:h:lms:u:");
119 char opt;
120 while ((short) (opt = (char) g.getopt()) != -1) {
121 switch (opt) {
122 case 'd':
123
124 destination = g.getOptarg();
125 break;
126 case 'f':
127
128 from_is_set = true;
129 from = g.getOptarg();
130 break;
131 case 'k':
132
133 killtime = g.getOptarg();
134 break;
135 case 'h':
136 host = g.getOptarg();
137 break;
138 case 'l':
139
140 resolution = Job.RESOLUTION_LOW;
141 break;
142 case 'm':
143
144 resolution = Job.RESOLUTION_MEDIUM;
145 break;
146 case 't':
147 maxtries = Integer.parseInt(g.getOptarg());
148 break;
149 case 'T':
150 maxdials = Integer.parseInt(g.getOptarg());
151 break;
152 case 'D':
153 notify = Job.NOTIFY_DONE;
154 break;
155 case 'R':
156 notify = Job.NOTIFY_REQUEUE;
157 break;
158 case 'N':
159 notify = Job.NOTIFY_NONE;
160 break;
161 case 'P':
162 priority = Integer.parseInt(g.getOptarg());
163 break;
164 case 's':
165 pagesize = Pagesize.getPagesize(g.getOptarg());
166 if (pagesize == null) {
167
168 System.err.println("'" + g.getOptarg()
169 + "' is not a valid pagesize value");
170 usage(System.err);
171 System.exit(-1);
172 }
173 break;
174 case 'u':
175 user = g.getOptarg();
176 break;
177 case 'v':
178
179 verbose = true;
180 break;
181 case '?':
182 usage(System.err);
183 System.exit(-1);
184 break;
185 default:
186 usage(System.err);
187 System.exit(-1);
188
189 break;
190 }
191 }
192
193
194 if (!from_is_set) {
195 from = user;
196 }
197
198
199 if (destination == null) {
200
201 usage(System.err);
202 System.exit(-1);
203 }
204
205
206 int i, count = 0;
207 for (i = g.getOptind(); i < arguments.length; i++) {
208 count++;
209 }
210 if (count < 1) {
211
212 usage(System.err);
213 System.exit(-1);
214 }
215
216
217
218 HylaFAXClient c = new HylaFAXClient();
219 try {
220 c.open(host);
221
222 if (c.user(user)) {
223
224 System.out.print("Password:");
225 BufferedReader input = new BufferedReader(
226 new InputStreamReader(System.in));
227 String password = input.readLine();
228 c.pass(password);
229 }
230 c.noop();
231 c.tzone(ClientProtocol.TZONE_LOCAL);
232
233
234 for (i = g.getOptind(); i < arguments.length; i++) {
235 FileInputStream file = new FileInputStream(arguments[i]);
236 String remote_filename = c.putTemporary(file);
237 documents.addElement(remote_filename);
238 }
239
240 Job job = c.createJob();
241
242
243 job.setFromUser(from);
244 job.setNotifyAddress(from);
245 job.setKilltime(killtime);
246 job.setMaximumDials(maxdials);
247 job.setMaximumTries(maxtries);
248 job.setPriority(priority);
249 job.setDialstring(destination);
250 job.setVerticalResolution(resolution);
251 job.setPageDimension(pagesize);
252 job.setNotifyType(notify);
253 job.setChopThreshold(chopthreshold);
254
255
256 for (i = 0; i < documents.size(); i++) {
257 String document = (String) documents.elementAt(i);
258 job.addDocument(document);
259 }
260
261 c.submit(job);
262
263 } catch (Exception e) {
264 log.error(e.getMessage(), e);
265 } finally {
266
267 try {
268 c.quit();
269 } catch (Exception e) {
270
271 log.error(e.getMessage(), e);
272 }
273 }
274 }
275
276 /***
277 * print out usage information on this program
278 *
279 * @param out
280 * where to print the usage info
281 */
282 public static void usage(PrintStream out) {
283 out.println("usage:\n\tSendFax <options> file1 ...\n");
284 out
285 .println("where <options> can be:\n"
286 + "\t-h <host> specifiy server hostname\n"
287 + "\t-u <user> user to login to the server with\n"
288 + "\t-v verbose mode\n"
289 + "\t-d <number> specify a destination FAX <number>\n"
290 + "\t-f <sender> user <sender> as the identity of the FAX sender\n"
291 + "\t-k <time> kill the job if it doesn't complete after the\n"
292 + "\t indicated <time> (default: \"000259\", 2 hours, 59 minutes)\n"
293 + "\t-t <tries> make no more than <tries> attempts to deliver the FAX\n"
294 + "\t-T <dials> maximum number of <dials> to attempt for each job\n"
295 + "\t-D enable delivery notification\n"
296 + "\t-R enable delivery and retry notification\n"
297 + "\t-N disable delivery and retry notification\n"
298 + "\t-P <pri> assign the <pri> priority to the job (default: 127)\n"
299 + "\t-l use low resolution (98 lines/inch)\n"
300 + "\t-m use medium resolution (196 lines/inch)\n"
301 + "\t-s <size> specify the symbolic page <size>\n"
302 + "\t (legal, us-let, a3, a4, etc.)\n"
303 + "\nFiles queued must be formats that the HylaFAX server can\n"
304 + " handle natively (PS, TIFF, etc.) as no client-side\n"
305 + " conversions are performed.");
306 }
307
308 }
309
310