View Javadoc

1   // SendFax.java - gnu.hylafax implementation of the sendfax utility
2   // $Id: SendFax.java,v 1.8 2007/02/21 00:07:49 sjardine Exp $
3   //
4   // - basically gives an example for queuing a FAX job
5   //
6   // Copyright 2000, 2001, Joe Phillips <jaiger@innovationsw.com>
7   // Copyright 2001, Innovation Software Group, LLC - http://www.innovationsw.com
8   //
9   // This library is free software; you can redistribute it and/or
10  // modify it under the terms of the GNU Library General Public
11  // License as published by the Free Software Foundation; either
12  // version 2 of the License, or (at your option) any later version.
13  //
14  // This library is distributed in the hope that it will be useful,
15  // but WITHOUT ANY WARRANTY; without even the implied warranty of
16  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  // Library General Public License for more details.
18  //
19  // You should have received a copy of the GNU Library General Public
20  // License along with this library; if not, write to the Free
21  // Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  //
23  //
24  // for information on the HylaFAX FAX server see
25  //  http://www.hylafax.org/
26  //
27  // KNOWN ISSUES:
28  // - The password dialog is echoed to the screen, beware
29  // - not all symbolic pagesizes are supported yet (see Job.pagesizes)
30  // - can only queue a single job to the server per execution
31  //
32  // TODO make this class more flexible so it can be used by other programs rather than only called from the command line don't echo the password to the screen
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"; // -u
99  		String host = "localhost"; // -h
100 		String destination = null; // -d
101 		String from = user; // -f
102 		String killtime = "000259"; // -k
103 		int maxdials = 12; // -T
104 		int maxtries = 3; // -t
105 		int priority = 127; // -P
106 		String notifyaddr = user; // -f
107 		int resolution = 98; // -l, -m
108 		Dimension pagesize; // -s
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; // default pagesize is US 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 				// destination
124 				destination = g.getOptarg();
125 				break;
126 			case 'f':
127 				// from address
128 				from_is_set = true;
129 				from = g.getOptarg();
130 				break;
131 			case 'k':
132 				// killtime
133 				killtime = g.getOptarg();
134 				break;
135 			case 'h':
136 				host = g.getOptarg();
137 				break;
138 			case 'l':
139 				// low-res
140 				resolution = Job.RESOLUTION_LOW;
141 				break;
142 			case 'm':
143 				// medium-res
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 					// no good
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 				// verbose mode
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 				// error
189 				break;
190 			}
191 		}// while processing options
192 
193 		// validate some parameters
194 		if (!from_is_set) {
195 			from = user;
196 		}
197 
198 		// there should be a destination
199 		if (destination == null) {
200 			// destination is required
201 			usage(System.err);
202 			System.exit(-1);
203 		}
204 
205 		// make sure there is at least one file
206 		int i, count = 0;
207 		for (i = g.getOptind(); i < arguments.length; i++) {
208 			count++;
209 		}
210 		if (count < 1) {
211 			// at least one document is required
212 			usage(System.err);
213 			System.exit(-1);
214 		}
215 
216 		// get down to business, send the FAX already
217 
218 		HylaFAXClient c = new HylaFAXClient();
219 		try {
220 			c.open(host);
221 
222 			if (c.user(user)) {
223 				// need password
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(); // for the heck of it
231 			c.tzone(ClientProtocol.TZONE_LOCAL);
232 
233 			// schlep files up to server
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(); // start a new job
241 
242 			// set job properties
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 			// add documents to the job
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); // submit the job to the scheduler
262 
263 		} catch (Exception e) {
264 			log.error(e.getMessage(), e);
265 		} finally {
266 			// disconnect from the server
267 			try {
268 				c.quit();
269 			} catch (Exception e) {
270 				// quit failed, not much we can do now
271 				log.error(e.getMessage(), e);
272 			}
273 		}
274 	}// main
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 	}// usage
307 
308 }// end of SendFax
309 
310 // end of file