Commit | Line | Data |
---|---|---|
08fe2e33 NR |
1 | package be.nikiroo.fanfix; |
2 | ||
3 | import java.io.File; | |
4 | import java.io.IOException; | |
5 | import java.net.MalformedURLException; | |
6 | import java.net.URL; | |
91b82a5c | 7 | import java.util.ArrayList; |
f569d249 | 8 | import java.util.List; |
08fe2e33 | 9 | |
0bb51c9c NR |
10 | import javax.net.ssl.SSLException; |
11 | ||
fb25273c | 12 | import be.nikiroo.fanfix.bundles.Config; |
08fe2e33 NR |
13 | import be.nikiroo.fanfix.bundles.StringId; |
14 | import be.nikiroo.fanfix.data.Chapter; | |
b6b65795 | 15 | import be.nikiroo.fanfix.data.MetaData; |
08fe2e33 | 16 | import be.nikiroo.fanfix.data.Story; |
ff05b828 NR |
17 | import be.nikiroo.fanfix.library.BasicLibrary; |
18 | import be.nikiroo.fanfix.library.CacheLibrary; | |
e42573a0 NR |
19 | import be.nikiroo.fanfix.library.LocalLibrary; |
20 | import be.nikiroo.fanfix.library.RemoteLibrary; | |
21 | import be.nikiroo.fanfix.library.RemoteLibraryServer; | |
f433d153 | 22 | import be.nikiroo.fanfix.library.WebLibraryServer; |
08fe2e33 NR |
23 | import be.nikiroo.fanfix.output.BasicOutput; |
24 | import be.nikiroo.fanfix.output.BasicOutput.OutputType; | |
3727aae2 | 25 | import be.nikiroo.fanfix.reader.BasicReader; |
9b75402f | 26 | import be.nikiroo.fanfix.reader.CliReader; |
91b82a5c | 27 | import be.nikiroo.fanfix.searchable.BasicSearchable; |
08fe2e33 | 28 | import be.nikiroo.fanfix.supported.BasicSupport; |
0ffa4754 | 29 | import be.nikiroo.fanfix.supported.SupportType; |
3b2b638f | 30 | import be.nikiroo.utils.Progress; |
39c3c689 | 31 | import be.nikiroo.utils.Version; |
bb0c30ac | 32 | import be.nikiroo.utils.VersionCheck; |
08fe2e33 NR |
33 | |
34 | /** | |
35 | * Main program entry point. | |
36 | * | |
37 | * @author niki | |
38 | */ | |
39 | public class Main { | |
d0114000 | 40 | private enum MainAction { |
9b75402f | 41 | IMPORT, EXPORT, CONVERT, READ, READ_URL, LIST, HELP, START, VERSION, SERVER, STOP_SERVER, REMOTE, SET_SOURCE, SET_TITLE, SET_AUTHOR, SEARCH, SEARCH_TAG |
d0114000 NR |
42 | } |
43 | ||
08fe2e33 NR |
44 | /** |
45 | * Main program entry point. | |
46 | * <p> | |
47 | * Known environment variables: | |
48 | * <ul> | |
d0114000 | 49 | * <li>NOUTF: if set to 1 or 'true', the program will prefer non-unicode |
08fe2e33 NR |
50 | * {@link String}s when possible</li> |
51 | * <li>CONFIG_DIR: a path where to look for the <tt>.properties</tt> files | |
edd46289 NR |
52 | * before taking the usual ones; they will also be saved/updated into this |
53 | * path when the program starts</li> | |
d0114000 NR |
54 | * <li>DEBUG: if set to 1 or 'true', the program will override the DEBUG_ERR |
55 | * configuration value with 'true'</li> | |
56 | * </ul> | |
57 | * <p> | |
58 | * <ul> | |
59 | * <li>--import [URL]: import into library</li> | |
60 | * <li>--export [id] [output_type] [target]: export story to target</li> | |
61 | * <li>--convert [URL] [output_type] [target] (+info): convert URL into | |
62 | * target</li> | |
63 | * <li>--read [id] ([chapter number]): read the given story from the library | |
64 | * </li> | |
333f0e7b | 65 | * <li>--read-url [URL] ([chapter number]): convert on the fly and read the |
d0114000 | 66 | * story, without saving it</li> |
8b153400 NR |
67 | * <li>--search: list the supported websites (where)</li> |
68 | * <li>--search [where] [keywords] (page [page]) (item [item]): search on | |
69 | * the supported website and display the given results page of stories it | |
70 | * found, or the story details if asked</li> | |
8141d7ac NR |
71 | * <li>--search-tag [where]: list all the tags supported by this website</li> |
72 | * <li>--search-tag [index 1]... (page [page]) (item [item]): search for the | |
73 | * given stories or subtags, tag by tag, and display information about a | |
74 | * specific page of results or about a specific item if requested</li> | |
333f0e7b | 75 | * <li>--list ([type]): list the stories present in the library</li> |
e10b51a2 NR |
76 | * <li>--set-source [id] [new source]: change the source of the given story</li> |
77 | * <li>--set-title [id] [new title]: change the title of the given story</li> | |
78 | * <li>--set-author [id] [new author]: change the author of the given story</li> | |
39c3c689 | 79 | * <li>--version: get the version of the program</li> |
fb25273c NR |
80 | * <li>--server: start the server mode (see config file for parameters)</li> |
81 | * <li>--stop-server: stop the running server on this port if any</li> | |
f433d153 | 82 | * <li>--remote [key] [host] [port]: use the given remote library</li> |
08fe2e33 NR |
83 | * </ul> |
84 | * | |
85 | * @param args | |
d0114000 | 86 | * see method description |
08fe2e33 NR |
87 | */ |
88 | public static void main(String[] args) { | |
9a553563 NR |
89 | new Main().start(args); |
90 | } | |
91 | ||
92 | /** | |
93 | * Start the default handling for the application. | |
94 | * <p> | |
95 | * If specific actions were asked (with correct parameters), they will be | |
96 | * forwarded to the different protected methods that you can override. | |
97 | * <p> | |
98 | * At the end of the method, {@link Main#exit(int)} will be called; by | |
99 | * default, it calls {@link System#exit(int)} if the status is not 0. | |
100 | * | |
101 | * @param args | |
102 | * the arguments received from the system | |
103 | */ | |
104 | public void start(String [] args) { | |
ee9b7083 NR |
105 | // Only one line, but very important: |
106 | Instance.init(); | |
107 | ||
d0114000 NR |
108 | String urlString = null; |
109 | String luid = null; | |
b0e88ebd | 110 | String sourceString = null; |
e10b51a2 NR |
111 | String titleString = null; |
112 | String authorString = null; | |
d0114000 NR |
113 | String chapString = null; |
114 | String target = null; | |
2070ced5 | 115 | String key = null; |
333f0e7b | 116 | MainAction action = MainAction.START; |
d0114000 | 117 | Boolean plusInfo = null; |
b0e88ebd NR |
118 | String host = null; |
119 | Integer port = null; | |
91b82a5c NR |
120 | SupportType searchOn = null; |
121 | String search = null; | |
8b153400 | 122 | List<Integer> tags = new ArrayList<Integer>(); |
91b82a5c NR |
123 | Integer page = null; |
124 | Integer item = null; | |
73ce17ef | 125 | |
d0114000 NR |
126 | boolean noMoreActions = false; |
127 | ||
128 | int exitCode = 0; | |
129 | for (int i = 0; exitCode == 0 && i < args.length; i++) { | |
59f1f1bf NR |
130 | if (args[i] == null) |
131 | continue; | |
132 | ||
d0114000 NR |
133 | // Action (--) handling: |
134 | if (!noMoreActions && args[i].startsWith("--")) { | |
135 | if (args[i].equals("--")) { | |
136 | noMoreActions = true; | |
137 | } else { | |
138 | try { | |
139 | action = MainAction.valueOf(args[i].substring(2) | |
140 | .toUpperCase().replace("-", "_")); | |
141 | } catch (Exception e) { | |
d66deb8d NR |
142 | Instance.getInstance().getTraceHandler() |
143 | .error(new IllegalArgumentException("Unknown action: " + args[i], e)); | |
d0114000 NR |
144 | exitCode = 255; |
145 | } | |
146 | } | |
08fe2e33 | 147 | |
d0114000 NR |
148 | continue; |
149 | } | |
150 | ||
151 | switch (action) { | |
152 | case IMPORT: | |
153 | if (urlString == null) { | |
154 | urlString = args[i]; | |
155 | } else { | |
156 | exitCode = 255; | |
157 | } | |
158 | break; | |
159 | case EXPORT: | |
160 | if (luid == null) { | |
161 | luid = args[i]; | |
b0e88ebd NR |
162 | } else if (sourceString == null) { |
163 | sourceString = args[i]; | |
d0114000 NR |
164 | } else if (target == null) { |
165 | target = args[i]; | |
166 | } else { | |
167 | exitCode = 255; | |
168 | } | |
169 | break; | |
170 | case CONVERT: | |
171 | if (urlString == null) { | |
172 | urlString = args[i]; | |
b0e88ebd NR |
173 | } else if (sourceString == null) { |
174 | sourceString = args[i]; | |
d0114000 NR |
175 | } else if (target == null) { |
176 | target = args[i]; | |
177 | } else if (plusInfo == null) { | |
178 | if ("+info".equals(args[i])) { | |
179 | plusInfo = true; | |
180 | } else { | |
181 | exitCode = 255; | |
182 | } | |
183 | } else { | |
184 | exitCode = 255; | |
08fe2e33 | 185 | } |
d0114000 NR |
186 | break; |
187 | case LIST: | |
b0e88ebd NR |
188 | if (sourceString == null) { |
189 | sourceString = args[i]; | |
d0114000 NR |
190 | } else { |
191 | exitCode = 255; | |
08fe2e33 | 192 | } |
d0114000 | 193 | break; |
e10b51a2 NR |
194 | case SET_SOURCE: |
195 | if (luid == null) { | |
196 | luid = args[i]; | |
197 | } else if (sourceString == null) { | |
198 | sourceString = args[i]; | |
199 | } else { | |
200 | exitCode = 255; | |
201 | } | |
202 | break; | |
203 | case SET_TITLE: | |
204 | if (luid == null) { | |
205 | luid = args[i]; | |
206 | } else if (sourceString == null) { | |
207 | titleString = args[i]; | |
208 | } else { | |
209 | exitCode = 255; | |
210 | } | |
211 | break; | |
212 | case SET_AUTHOR: | |
213 | if (luid == null) { | |
214 | luid = args[i]; | |
215 | } else if (sourceString == null) { | |
216 | authorString = args[i]; | |
217 | } else { | |
218 | exitCode = 255; | |
219 | } | |
220 | break; | |
d0114000 NR |
221 | case READ: |
222 | if (luid == null) { | |
223 | luid = args[i]; | |
224 | } else if (chapString == null) { | |
225 | chapString = args[i]; | |
226 | } else { | |
227 | exitCode = 255; | |
08fe2e33 | 228 | } |
d0114000 NR |
229 | break; |
230 | case READ_URL: | |
231 | if (urlString == null) { | |
232 | urlString = args[i]; | |
233 | } else if (chapString == null) { | |
234 | chapString = args[i]; | |
235 | } else { | |
236 | exitCode = 255; | |
08fe2e33 | 237 | } |
d0114000 | 238 | break; |
91b82a5c NR |
239 | case SEARCH: |
240 | if (searchOn == null) { | |
241 | searchOn = SupportType.valueOfAllOkUC(args[i]); | |
8b153400 | 242 | |
91b82a5c | 243 | if (searchOn == null) { |
d66deb8d | 244 | Instance.getInstance().getTraceHandler().error("Website not known: <" + args[i] + ">"); |
b31a0db0 NR |
245 | exitCode = 41; |
246 | break; | |
91b82a5c | 247 | } |
8b153400 | 248 | |
91b82a5c | 249 | if (BasicSearchable.getSearchable(searchOn) == null) { |
d66deb8d | 250 | Instance.getInstance().getTraceHandler().error("Website not supported: " + searchOn); |
b31a0db0 NR |
251 | exitCode = 42; |
252 | break; | |
91b82a5c NR |
253 | } |
254 | } else if (search == null) { | |
255 | search = args[i]; | |
8b153400 | 256 | } else if (page != null && page == -1) { |
91b82a5c NR |
257 | try { |
258 | page = Integer.parseInt(args[i]); | |
8b153400 NR |
259 | } catch (Exception e) { |
260 | page = -2; | |
91b82a5c | 261 | } |
8b153400 | 262 | } else if (item != null && item == -1) { |
91b82a5c NR |
263 | try { |
264 | item = Integer.parseInt(args[i]); | |
8b153400 NR |
265 | } catch (Exception e) { |
266 | item = -2; | |
267 | } | |
268 | } else if (page == null || item == null) { | |
269 | if (page == null && "page".equals(args[i])) { | |
270 | page = -1; | |
271 | } else if (item == null && "item".equals(args[i])) { | |
272 | item = -1; | |
273 | } else { | |
91b82a5c NR |
274 | exitCode = 255; |
275 | } | |
276 | } else { | |
277 | exitCode = 255; | |
278 | } | |
279 | break; | |
8141d7ac | 280 | case SEARCH_TAG: |
91b82a5c NR |
281 | if (searchOn == null) { |
282 | searchOn = SupportType.valueOfAllOkUC(args[i]); | |
8b153400 | 283 | |
91b82a5c | 284 | if (searchOn == null) { |
d66deb8d | 285 | Instance.getInstance().getTraceHandler().error("Website not known: <" + args[i] + ">"); |
91b82a5c NR |
286 | exitCode = 255; |
287 | } | |
8b153400 | 288 | |
91b82a5c | 289 | if (BasicSearchable.getSearchable(searchOn) == null) { |
d66deb8d | 290 | Instance.getInstance().getTraceHandler().error("Website not supported: " + searchOn); |
91b82a5c NR |
291 | exitCode = 255; |
292 | } | |
8b153400 NR |
293 | } else if (page == null && item == null) { |
294 | if ("page".equals(args[i])) { | |
295 | page = -1; | |
296 | } else if ("item".equals(args[i])) { | |
297 | item = -1; | |
298 | } else { | |
299 | try { | |
300 | int index = Integer.parseInt(args[i]); | |
301 | tags.add(index); | |
302 | } catch (NumberFormatException e) { | |
d66deb8d | 303 | Instance.getInstance().getTraceHandler().error("Invalid tag index: " + args[i]); |
8b153400 NR |
304 | exitCode = 255; |
305 | } | |
306 | } | |
307 | } else if (page != null && page == -1) { | |
308 | try { | |
309 | page = Integer.parseInt(args[i]); | |
310 | } catch (Exception e) { | |
311 | page = -2; | |
312 | } | |
313 | } else if (item != null && item == -1) { | |
314 | try { | |
315 | item = Integer.parseInt(args[i]); | |
316 | } catch (Exception e) { | |
317 | item = -2; | |
318 | } | |
319 | } else if (page == null || item == null) { | |
320 | if (page == null && "page".equals(args[i])) { | |
321 | page = -1; | |
322 | } else if (item == null && "item".equals(args[i])) { | |
323 | item = -1; | |
324 | } else { | |
325 | exitCode = 255; | |
326 | } | |
91b82a5c | 327 | } else { |
8b153400 | 328 | exitCode = 255; |
91b82a5c NR |
329 | } |
330 | break; | |
d0114000 NR |
331 | case HELP: |
332 | exitCode = 255; | |
333 | break; | |
333f0e7b NR |
334 | case START: |
335 | exitCode = 255; // not supposed to be selected by user | |
336 | break; | |
39c3c689 NR |
337 | case VERSION: |
338 | exitCode = 255; // no arguments for this option | |
b0e88ebd NR |
339 | break; |
340 | case SERVER: | |
fb25273c NR |
341 | exitCode = 255; // no arguments for this option |
342 | break; | |
5e848e6a | 343 | case STOP_SERVER: |
fb25273c | 344 | exitCode = 255; // no arguments for this option |
b0e88ebd NR |
345 | break; |
346 | case REMOTE: | |
2070ced5 NR |
347 | if (key == null) { |
348 | key = args[i]; | |
349 | } else if (host == null) { | |
b0e88ebd NR |
350 | host = args[i]; |
351 | } else if (port == null) { | |
352 | port = Integer.parseInt(args[i]); | |
ff05b828 | 353 | |
2070ced5 | 354 | BasicLibrary lib = new RemoteLibrary(key, host, port); |
9b75402f NR |
355 | lib = new CacheLibrary( |
356 | Instance.getInstance().getRemoteDir(host), lib, | |
d66deb8d | 357 | Instance.getInstance().getUiConfig()); |
ff05b828 | 358 | |
9b75402f | 359 | Instance.getInstance().setLibrary(lib); |
5e848e6a | 360 | |
b0e88ebd NR |
361 | action = MainAction.START; |
362 | } else { | |
363 | exitCode = 255; | |
364 | } | |
365 | break; | |
d0114000 NR |
366 | } |
367 | } | |
368 | ||
92fb0719 NR |
369 | final Progress mainProgress = new Progress(0, 80); |
370 | mainProgress.addProgressListener(new Progress.ProgressListener() { | |
371 | private int current = mainProgress.getMin(); | |
372 | ||
211f7ddb | 373 | @Override |
92fb0719 NR |
374 | public void progress(Progress progress, String name) { |
375 | int diff = progress.getProgress() - current; | |
376 | current += diff; | |
377 | ||
1822d603 NR |
378 | if (diff <= 0) |
379 | return; | |
380 | ||
92fb0719 NR |
381 | StringBuilder builder = new StringBuilder(); |
382 | for (int i = 0; i < diff; i++) { | |
383 | builder.append('.'); | |
384 | } | |
385 | ||
386 | System.err.print(builder.toString()); | |
387 | ||
388 | if (progress.isDone()) { | |
389 | System.err.println(""); | |
390 | } | |
391 | } | |
392 | }); | |
393 | Progress pg = new Progress(); | |
394 | mainProgress.addProgress(pg, mainProgress.getMax()); | |
395 | ||
9a553563 | 396 | VersionCheck updates = checkUpdates(); |
b42117f1 | 397 | |
b31a0db0 | 398 | if (exitCode == 0) { |
d0114000 NR |
399 | switch (action) { |
400 | case IMPORT: | |
bb0c30ac NR |
401 | if (updates != null) { |
402 | // we consider it read | |
403 | Instance.getInstance().setVersionChecked(); | |
404 | } | |
9a553563 NR |
405 | |
406 | try { | |
407 | exitCode = imprt(BasicReader.getUrl(urlString), pg); | |
408 | } catch (MalformedURLException e) { | |
409 | Instance.getInstance().getTraceHandler().error(e); | |
410 | exitCode = 1; | |
411 | } | |
412 | ||
d0114000 NR |
413 | break; |
414 | case EXPORT: | |
bb0c30ac NR |
415 | if (updates != null) { |
416 | // we consider it read | |
417 | Instance.getInstance().setVersionChecked(); | |
418 | } | |
9a553563 NR |
419 | |
420 | OutputType exportType = OutputType.valueOfNullOkUC(sourceString, null); | |
421 | if (exportType == null) { | |
422 | Instance.getInstance().getTraceHandler().error(new Exception(trans(StringId.OUTPUT_DESC, sourceString))); | |
423 | exitCode = 1; | |
424 | break; | |
425 | } | |
426 | ||
427 | exitCode = export(luid, exportType, target, pg); | |
428 | ||
d0114000 NR |
429 | break; |
430 | case CONVERT: | |
bb0c30ac NR |
431 | if (updates != null) { |
432 | // we consider it read | |
433 | Instance.getInstance().setVersionChecked(); | |
434 | } | |
9a553563 NR |
435 | |
436 | OutputType convertType = OutputType.valueOfAllOkUC(sourceString, null); | |
437 | if (convertType == null) { | |
438 | Instance.getInstance().getTraceHandler() | |
439 | .error(new IOException(trans(StringId.ERR_BAD_OUTPUT_TYPE, sourceString))); | |
440 | ||
441 | exitCode = 2; | |
442 | break; | |
443 | } | |
444 | ||
445 | exitCode = convert(urlString, convertType, target, | |
92fb0719 | 446 | plusInfo == null ? false : plusInfo, pg); |
9a553563 | 447 | |
d0114000 NR |
448 | break; |
449 | case LIST: | |
b0e88ebd | 450 | exitCode = list(sourceString); |
d0114000 | 451 | break; |
e10b51a2 NR |
452 | case SET_SOURCE: |
453 | try { | |
d66deb8d | 454 | Instance.getInstance().getLibrary().changeSource(luid, sourceString, pg); |
e10b51a2 | 455 | } catch (IOException e1) { |
d66deb8d | 456 | Instance.getInstance().getTraceHandler().error(e1); |
e10b51a2 NR |
457 | exitCode = 21; |
458 | } | |
459 | break; | |
460 | case SET_TITLE: | |
461 | try { | |
d66deb8d | 462 | Instance.getInstance().getLibrary().changeTitle(luid, titleString, pg); |
e10b51a2 | 463 | } catch (IOException e1) { |
d66deb8d | 464 | Instance.getInstance().getTraceHandler().error(e1); |
e10b51a2 NR |
465 | exitCode = 22; |
466 | } | |
467 | break; | |
468 | case SET_AUTHOR: | |
469 | try { | |
d66deb8d | 470 | Instance.getInstance().getLibrary().changeAuthor(luid, authorString, pg); |
e10b51a2 | 471 | } catch (IOException e1) { |
d66deb8d | 472 | Instance.getInstance().getTraceHandler().error(e1); |
e10b51a2 NR |
473 | exitCode = 23; |
474 | } | |
475 | break; | |
d0114000 | 476 | case READ: |
9b75402f NR |
477 | if (luid == null || luid.isEmpty()) { |
478 | syntax(false); | |
479 | exitCode = 255; | |
99ccbdf6 NR |
480 | break; |
481 | } | |
9b75402f NR |
482 | |
483 | try { | |
9a553563 NR |
484 | Integer chap = null; |
485 | if (chapString != null) { | |
486 | try { | |
487 | chap = Integer.parseInt(chapString); | |
488 | } catch (NumberFormatException e) { | |
489 | Instance.getInstance().getTraceHandler().error(new IOException( | |
490 | "Chapter number cannot be parsed: " + chapString, e)); | |
491 | exitCode = 2; | |
492 | break; | |
493 | } | |
494 | } | |
495 | ||
9b75402f | 496 | BasicLibrary lib = Instance.getInstance().getLibrary(); |
9a553563 | 497 | exitCode = read(lib.getStory(luid, null), chap); |
9b75402f NR |
498 | } catch (IOException e) { |
499 | Instance.getInstance().getTraceHandler() | |
500 | .error(new IOException("Failed to read book", e)); | |
501 | exitCode = 2; | |
502 | } | |
503 | ||
d0114000 NR |
504 | break; |
505 | case READ_URL: | |
9b75402f NR |
506 | if (urlString == null || urlString.isEmpty()) { |
507 | syntax(false); | |
508 | exitCode = 255; | |
99ccbdf6 NR |
509 | break; |
510 | } | |
9b75402f NR |
511 | |
512 | try { | |
9a553563 NR |
513 | Integer chap = null; |
514 | if (chapString != null) { | |
515 | try { | |
516 | chap = Integer.parseInt(chapString); | |
517 | } catch (NumberFormatException e) { | |
518 | Instance.getInstance().getTraceHandler().error(new IOException( | |
519 | "Chapter number cannot be parsed: " + chapString, e)); | |
520 | exitCode = 2; | |
521 | break; | |
522 | } | |
523 | } | |
524 | ||
9b75402f NR |
525 | BasicSupport support = BasicSupport |
526 | .getSupport(BasicReader.getUrl(urlString)); | |
527 | if (support == null) { | |
528 | Instance.getInstance().getTraceHandler() | |
529 | .error("URL not supported: " + urlString); | |
530 | exitCode = 2; | |
531 | break; | |
532 | } | |
533 | ||
9a553563 | 534 | exitCode = read(support.process(null), chap); |
9b75402f NR |
535 | } catch (IOException e) { |
536 | Instance.getInstance().getTraceHandler() | |
537 | .error(new IOException("Failed to read book", e)); | |
538 | exitCode = 2; | |
539 | } | |
540 | ||
91b82a5c NR |
541 | break; |
542 | case SEARCH: | |
8b153400 NR |
543 | page = page == null ? 1 : page; |
544 | if (page < 0) { | |
d66deb8d | 545 | Instance.getInstance().getTraceHandler().error("Incorrect page number"); |
91b82a5c NR |
546 | exitCode = 255; |
547 | break; | |
548 | } | |
8b153400 NR |
549 | |
550 | item = item == null ? 0 : item; | |
551 | if (item < 0) { | |
d66deb8d | 552 | Instance.getInstance().getTraceHandler().error("Incorrect item number"); |
8b153400 NR |
553 | exitCode = 255; |
554 | break; | |
91b82a5c | 555 | } |
8b153400 | 556 | |
9a553563 NR |
557 | if (searchOn == null) { |
558 | try { | |
559 | search(); | |
560 | } catch (IOException e) { | |
561 | Instance.getInstance().getTraceHandler().error(e); | |
562 | exitCode = 1; | |
8b153400 | 563 | } |
9a553563 NR |
564 | } else if (search != null) { |
565 | try { | |
566 | searchKeywords(searchOn, search, page, item); | |
567 | } catch (IOException e) { | |
568 | Instance.getInstance().getTraceHandler().error(e); | |
569 | exitCode = 20; | |
570 | } | |
571 | } else { | |
572 | exitCode = 255; | |
91b82a5c | 573 | } |
8b153400 | 574 | |
91b82a5c | 575 | break; |
8141d7ac | 576 | case SEARCH_TAG: |
91b82a5c NR |
577 | if (searchOn == null) { |
578 | exitCode = 255; | |
579 | break; | |
580 | } | |
91b82a5c | 581 | |
8b153400 NR |
582 | page = page == null ? 1 : page; |
583 | if (page < 0) { | |
d66deb8d | 584 | Instance.getInstance().getTraceHandler().error("Incorrect page number"); |
8b153400 NR |
585 | exitCode = 255; |
586 | break; | |
91b82a5c | 587 | } |
8b153400 NR |
588 | |
589 | item = item == null ? 0 : item; | |
590 | if (item < 0) { | |
d66deb8d | 591 | Instance.getInstance().getTraceHandler().error("Incorrect item number"); |
8b153400 NR |
592 | exitCode = 255; |
593 | break; | |
594 | } | |
595 | ||
91b82a5c | 596 | try { |
9a553563 NR |
597 | searchTags(searchOn, page, item, |
598 | tags.toArray(new Integer[] {})); | |
599 | } catch (IOException e) { | |
600 | Instance.getInstance().getTraceHandler().error(e); | |
91b82a5c | 601 | } |
8b153400 | 602 | |
d0114000 NR |
603 | break; |
604 | case HELP: | |
605 | syntax(true); | |
606 | exitCode = 0; | |
607 | break; | |
39c3c689 | 608 | case VERSION: |
bb0c30ac NR |
609 | if (updates != null) { |
610 | // we consider it read | |
611 | Instance.getInstance().setVersionChecked(); | |
612 | } | |
9a553563 | 613 | |
39c3c689 NR |
614 | System.out |
615 | .println(String.format("Fanfix version %s" | |
9fe3f177 NR |
616 | + "%nhttps://github.com/nikiroo/fanfix/" |
617 | + "%n\tWritten by Nikiroo", | |
39c3c689 NR |
618 | Version.getCurrentVersion())); |
619 | break; | |
333f0e7b | 620 | case START: |
0bb51c9c | 621 | try { |
9a553563 | 622 | start(); |
0bb51c9c | 623 | } catch (IOException e) { |
d66deb8d | 624 | Instance.getInstance().getTraceHandler().error(e); |
0bb51c9c NR |
625 | exitCode = 66; |
626 | } | |
b0e88ebd NR |
627 | break; |
628 | case SERVER: | |
b0e88ebd | 629 | try { |
f433d153 | 630 | startServer(); |
b0e88ebd | 631 | } catch (IOException e) { |
d66deb8d | 632 | Instance.getInstance().getTraceHandler().error(e); |
b0e88ebd | 633 | } |
9a553563 NR |
634 | |
635 | break; | |
5e848e6a | 636 | case STOP_SERVER: |
aad0e5ae NR |
637 | // Can be given via "--remote XX XX XX" |
638 | if (key == null) | |
d66deb8d | 639 | key = Instance.getInstance().getConfig().getString(Config.SERVER_KEY); |
aad0e5ae | 640 | if (port == null) |
d66deb8d | 641 | port = Instance.getInstance().getConfig().getInteger(Config.SERVER_PORT); |
aad0e5ae | 642 | |
5e848e6a | 643 | if (port == null) { |
aad0e5ae | 644 | System.err.println("No port given nor configured in the config file"); |
fb25273c | 645 | exitCode = 15; |
5e848e6a NR |
646 | break; |
647 | } | |
0bb51c9c | 648 | try { |
9a553563 | 649 | stopServer(key, host, port); |
0bb51c9c | 650 | } catch (SSLException e) { |
d66deb8d | 651 | Instance.getInstance().getTraceHandler().error( |
0bb51c9c NR |
652 | "Bad access key for remote library"); |
653 | exitCode = 43; | |
654 | } catch (IOException e) { | |
d66deb8d | 655 | Instance.getInstance().getTraceHandler().error(e); |
0bb51c9c NR |
656 | exitCode = 44; |
657 | } | |
5e848e6a | 658 | |
5e848e6a | 659 | break; |
b0e88ebd | 660 | case REMOTE: |
99ccbdf6 | 661 | exitCode = 255; // should not be reachable (REMOTE -> START) |
333f0e7b | 662 | break; |
08fe2e33 NR |
663 | } |
664 | } | |
665 | ||
350bc060 | 666 | try { |
d66deb8d | 667 | Instance.getInstance().getTempFiles().close(); |
350bc060 | 668 | } catch (IOException e) { |
9a553563 NR |
669 | Instance.getInstance().getTraceHandler().error(new IOException( |
670 | "Cannot dispose of the temporary files", e)); | |
2aac79c7 NR |
671 | } |
672 | ||
08fe2e33 | 673 | if (exitCode == 255) { |
d0114000 | 674 | syntax(false); |
08fe2e33 NR |
675 | } |
676 | ||
9a553563 NR |
677 | exit(exitCode); |
678 | } | |
679 | ||
680 | /** | |
681 | * A normal invocation of the program (without parameters or at least | |
682 | * without "action" parameters). | |
683 | * <p> | |
684 | * You will probably want to override that one if you offer a user | |
685 | * interface. | |
686 | * | |
687 | * @throws IOException | |
688 | * in case of I/O error | |
689 | */ | |
690 | protected void start() throws IOException { | |
691 | new CliReader().listBooks(null); | |
692 | } | |
693 | ||
694 | /** | |
98b95fb8 | 695 | * Will check if updates are available, synchronously. |
9a553563 NR |
696 | * <p> |
697 | * For this, it will simply forward the call to | |
698 | * {@link Main#checkUpdates(String)} with a value of "nikiroo/fanfix". | |
98b95fb8 NR |
699 | * <p> |
700 | * You may want to override it so you call the forward method with the right | |
701 | * parameters (or also if you want it to be asynchronous). | |
9a553563 NR |
702 | * |
703 | * @return the newer version information or NULL if nothing new | |
704 | */ | |
705 | protected VersionCheck checkUpdates() { | |
706 | return checkUpdates("nikiroo/fanfix"); | |
707 | } | |
708 | ||
709 | /** | |
710 | * Will check if updates are available on a specific GitHub project. | |
711 | * <p> | |
712 | * Will be called by {@link Main#checkUpdates()}, but if you override that | |
713 | * one you mall call it with another project. | |
714 | * | |
715 | * @param githubProject | |
716 | * the GitHub project, for instance "nikiroo/fanfix" | |
717 | * | |
718 | * @return the newer version information or NULL if nothing new | |
719 | */ | |
720 | protected VersionCheck checkUpdates(String githubProject) { | |
bb0c30ac NR |
721 | try { |
722 | VersionCheck updates = VersionCheck.check(githubProject, | |
723 | Instance.getInstance().getTrans().getLocale()); | |
724 | if (updates.isNewVersionAvailable()) { | |
725 | notifyUpdates(updates); | |
726 | return updates; | |
727 | } | |
728 | } catch (IOException e) { | |
729 | // Maybe no internet. Do not report any update. | |
9a553563 | 730 | } |
bb0c30ac | 731 | |
9a553563 | 732 | return null; |
08fe2e33 NR |
733 | } |
734 | ||
9a553563 NR |
735 | /** |
736 | * Notify the user about available updates. | |
737 | * <p> | |
738 | * Will only be called when a version is available. | |
739 | * <p> | |
bb0c30ac NR |
740 | * Note that you can call {@link Instance#setVersionChecked()} on it if the |
741 | * user has read the information (by default, it is marked read only on | |
742 | * certain other actions). | |
9a553563 NR |
743 | * |
744 | * @param updates | |
745 | * the new version information | |
746 | */ | |
747 | protected void notifyUpdates(VersionCheck updates) { | |
748 | // Sent to syserr so not to cause problem if one tries to capture a | |
749 | // story content in text mode | |
750 | System.err.println( | |
751 | "A new version of the program is available at https://github.com/nikiroo/fanfix/releases"); | |
752 | System.err.println(""); | |
753 | for (Version v : updates.getNewer()) { | |
754 | System.err.println("\tVersion " + v); | |
755 | System.err.println("\t-------------"); | |
756 | System.err.println(""); | |
757 | for (String it : updates.getChanges().get(v)) { | |
758 | System.err.println("\t- " + it); | |
759 | } | |
760 | System.err.println(""); | |
761 | } | |
762 | } | |
763 | ||
08fe2e33 | 764 | /** |
68e2c6d2 | 765 | * Import the given resource into the {@link LocalLibrary}. |
08fe2e33 | 766 | * |
9a553563 | 767 | * @param url |
08fe2e33 | 768 | * the resource to import |
92fb0719 NR |
769 | * @param pg |
770 | * the optional progress reporter | |
08fe2e33 NR |
771 | * |
772 | * @return the exit return code (0 = success) | |
773 | */ | |
9a553563 | 774 | protected static int imprt(URL url, Progress pg) { |
08fe2e33 | 775 | try { |
9a553563 | 776 | MetaData meta = Instance.getInstance().getLibrary().imprt(url, pg); |
d66deb8d | 777 | System.out.println(meta.getLuid() + ": \"" + meta.getTitle() + "\" imported."); |
08fe2e33 | 778 | } catch (IOException e) { |
d66deb8d | 779 | Instance.getInstance().getTraceHandler().error(e); |
08fe2e33 NR |
780 | return 1; |
781 | } | |
782 | ||
783 | return 0; | |
784 | } | |
785 | ||
786 | /** | |
68e2c6d2 NR |
787 | * Export the {@link Story} from the {@link LocalLibrary} to the given |
788 | * target. | |
08fe2e33 | 789 | * |
73ce17ef | 790 | * @param luid |
08fe2e33 | 791 | * the story LUID |
9a553563 | 792 | * @param type |
08fe2e33 NR |
793 | * the {@link OutputType} to use |
794 | * @param target | |
795 | * the target | |
92fb0719 NR |
796 | * @param pg |
797 | * the optional progress reporter | |
08fe2e33 NR |
798 | * |
799 | * @return the exit return code (0 = success) | |
800 | */ | |
9a553563 | 801 | protected static int export(String luid, OutputType type, String target, |
92fb0719 | 802 | Progress pg) { |
08fe2e33 | 803 | try { |
d66deb8d | 804 | Instance.getInstance().getLibrary().export(luid, type, target, pg); |
08fe2e33 | 805 | } catch (IOException e) { |
d66deb8d | 806 | Instance.getInstance().getTraceHandler().error(e); |
08fe2e33 NR |
807 | return 4; |
808 | } | |
809 | ||
810 | return 0; | |
811 | } | |
9a553563 | 812 | |
08fe2e33 | 813 | /** |
68e2c6d2 NR |
814 | * List the stories of the given source from the {@link LocalLibrary} |
815 | * (unless NULL is passed, in which case all stories will be listed). | |
08fe2e33 | 816 | * |
b0e88ebd NR |
817 | * @param source |
818 | * the source to list the known stories of, or NULL to list all | |
333f0e7b | 819 | * stories |
08fe2e33 NR |
820 | * |
821 | * @return the exit return code (0 = success) | |
822 | */ | |
9a553563 | 823 | protected int list(String source) { |
0bb51c9c | 824 | try { |
9b75402f | 825 | new CliReader().listBooks(source); |
0bb51c9c | 826 | } catch (IOException e) { |
d66deb8d | 827 | Instance.getInstance().getTraceHandler().error(e); |
0bb51c9c | 828 | return 66; |
f569d249 | 829 | } |
0bb51c9c | 830 | |
08fe2e33 NR |
831 | return 0; |
832 | } | |
833 | ||
834 | /** | |
350bc060 | 835 | * Start the current reader for this {@link Story}. |
08fe2e33 NR |
836 | * |
837 | * @param story | |
9b75402f | 838 | * the story to read |
9a553563 | 839 | * @param chap |
08fe2e33 NR |
840 | * which {@link Chapter} to read (starting at 1), or NULL to get |
841 | * the {@link Story} description | |
08fe2e33 NR |
842 | * |
843 | * @return the exit return code (0 = success) | |
844 | */ | |
9a553563 | 845 | protected int read(Story story, Integer chap) { |
9b75402f NR |
846 | if (story != null) { |
847 | try { | |
848 | if (chap == null) { | |
849 | new CliReader().listChapters(story); | |
850 | } else { | |
851 | new CliReader().printChapter(story, chap); | |
d0114000 | 852 | } |
9b75402f NR |
853 | } catch (IOException e) { |
854 | Instance.getInstance().getTraceHandler() | |
855 | .error(new IOException("Failed to read book", e)); | |
856 | return 2; | |
08fe2e33 | 857 | } |
9b75402f NR |
858 | } else { |
859 | Instance.getInstance().getTraceHandler() | |
9a553563 | 860 | .error("Cannot find book: " + story); |
9b75402f | 861 | return 2; |
08fe2e33 NR |
862 | } |
863 | ||
864 | return 0; | |
865 | } | |
866 | ||
867 | /** | |
868 | * Convert the {@link Story} into another format. | |
869 | * | |
d0114000 | 870 | * @param urlString |
08fe2e33 | 871 | * the source {@link Story} to convert |
9a553563 | 872 | * @param type |
08fe2e33 | 873 | * the {@link OutputType} to convert to |
d0114000 | 874 | * @param target |
08fe2e33 NR |
875 | * the target file |
876 | * @param infoCover | |
877 | * TRUE to also export the cover and info file, even if the given | |
878 | * {@link OutputType} does not usually save them | |
92fb0719 NR |
879 | * @param pg |
880 | * the optional progress reporter | |
08fe2e33 NR |
881 | * |
882 | * @return the exit return code (0 = success) | |
883 | */ | |
9a553563 | 884 | protected int convert(String urlString, OutputType type, |
92fb0719 | 885 | String target, boolean infoCover, Progress pg) { |
08fe2e33 NR |
886 | int exitCode = 0; |
887 | ||
d66deb8d | 888 | Instance.getInstance().getTraceHandler().trace("Convert: " + urlString); |
d0114000 | 889 | String sourceName = urlString; |
08fe2e33 | 890 | try { |
3b2b638f | 891 | URL source = BasicReader.getUrl(urlString); |
08fe2e33 | 892 | sourceName = source.toString(); |
3ddb5591 | 893 | if (sourceName.startsWith("file://")) { |
08fe2e33 NR |
894 | sourceName = sourceName.substring("file://".length()); |
895 | } | |
896 | ||
9a553563 NR |
897 | try { |
898 | BasicSupport support = BasicSupport.getSupport(source); | |
08fe2e33 | 899 | |
9a553563 NR |
900 | if (support != null) { |
901 | Instance.getInstance().getTraceHandler() | |
902 | .trace("Support found: " + support.getClass()); | |
903 | Progress pgIn = new Progress(); | |
904 | Progress pgOut = new Progress(); | |
905 | if (pg != null) { | |
906 | pg.setMax(2); | |
907 | pg.addProgress(pgIn, 1); | |
908 | pg.addProgress(pgOut, 1); | |
909 | } | |
08fe2e33 | 910 | |
9a553563 NR |
911 | Story story = support.process(pgIn); |
912 | try { | |
913 | target = new File(target).getAbsolutePath(); | |
914 | BasicOutput.getOutput(type, infoCover, infoCover) | |
915 | .process(story, target, pgOut); | |
916 | } catch (IOException e) { | |
d66deb8d | 917 | Instance.getInstance().getTraceHandler() |
9a553563 NR |
918 | .error(new IOException( |
919 | trans(StringId.ERR_SAVING, target), e)); | |
920 | exitCode = 5; | |
08fe2e33 | 921 | } |
9a553563 | 922 | } else { |
d66deb8d | 923 | Instance.getInstance().getTraceHandler() |
9a553563 NR |
924 | .error(new IOException( |
925 | trans(StringId.ERR_NOT_SUPPORTED, source))); | |
926 | ||
927 | exitCode = 4; | |
08fe2e33 | 928 | } |
9a553563 NR |
929 | } catch (IOException e) { |
930 | Instance.getInstance().getTraceHandler().error(new IOException( | |
931 | trans(StringId.ERR_LOADING, sourceName), e)); | |
932 | exitCode = 3; | |
08fe2e33 NR |
933 | } |
934 | } catch (MalformedURLException e) { | |
d66deb8d | 935 | Instance.getInstance().getTraceHandler().error(new IOException(trans(StringId.ERR_BAD_URL, sourceName), e)); |
08fe2e33 NR |
936 | exitCode = 1; |
937 | } | |
938 | ||
939 | return exitCode; | |
940 | } | |
941 | ||
08fe2e33 | 942 | /** |
d0114000 NR |
943 | * Display the correct syntax of the program to the user to stdout, or an |
944 | * error message if the syntax used was wrong on stderr. | |
945 | * | |
946 | * @param showHelp | |
947 | * TRUE to show the syntax help, FALSE to show "syntax error" | |
08fe2e33 | 948 | */ |
9a553563 | 949 | protected void syntax(boolean showHelp) { |
d0114000 NR |
950 | if (showHelp) { |
951 | StringBuilder builder = new StringBuilder(); | |
952 | for (SupportType type : SupportType.values()) { | |
953 | builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(), | |
954 | type.getDesc())); | |
955 | builder.append('\n'); | |
956 | } | |
08fe2e33 | 957 | |
d0114000 NR |
958 | String typesIn = builder.toString(); |
959 | builder.setLength(0); | |
08fe2e33 | 960 | |
d0114000 NR |
961 | for (OutputType type : OutputType.values()) { |
962 | builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(), | |
4d205683 | 963 | type.getDesc(true))); |
d0114000 NR |
964 | builder.append('\n'); |
965 | } | |
08fe2e33 | 966 | |
d0114000 | 967 | String typesOut = builder.toString(); |
08fe2e33 | 968 | |
d0114000 NR |
969 | System.out.println(trans(StringId.HELP_SYNTAX, typesIn, typesOut)); |
970 | } else { | |
971 | System.err.println(trans(StringId.ERR_SYNTAX)); | |
972 | } | |
973 | } | |
9a553563 NR |
974 | |
975 | /** | |
976 | * Starts a search operation (i.e., list the available web sites we can | |
977 | * search on). | |
978 | * | |
979 | * @throws IOException | |
980 | * in case of I/O errors | |
981 | */ | |
982 | protected void search() throws IOException { | |
983 | new CliReader().listSearchables(); | |
984 | } | |
985 | ||
986 | /** | |
987 | * Search for books by keywords on the given supported web site. | |
988 | * | |
989 | * @param searchOn | |
990 | * the web site to search on | |
991 | * @param search | |
992 | * the keyword to look for | |
993 | * @param page | |
994 | * the page of results to get, or 0 to inquire about the number | |
995 | * of pages | |
996 | * @param item | |
997 | * the index of the book we are interested by, or 0 to query | |
998 | * about how many books are in that page of results | |
999 | * | |
1000 | * @throws IOException | |
1001 | * in case of I/O error | |
1002 | */ | |
1003 | protected void searchKeywords(SupportType searchOn, String search, | |
1004 | int page, Integer item) throws IOException { | |
1005 | new CliReader().searchBooksByKeyword(searchOn, search, page, item); | |
1006 | } | |
1007 | ||
1008 | /** | |
1009 | * Search for books by tags on the given supported web site. | |
1010 | * | |
1011 | * @param searchOn | |
1012 | * the web site to search on | |
1013 | * @param page | |
1014 | * the page of results to get, or 0 to inquire about the number | |
1015 | * of pages | |
1016 | * @param item | |
1017 | * the index of the book we are interested by, or 0 to query | |
1018 | * about how many books are in that page of results | |
1019 | * @param tags | |
1020 | * the tags to look for | |
1021 | * | |
1022 | * @throws IOException | |
1023 | * in case of I/O error | |
1024 | */ | |
1025 | protected void searchTags(SupportType searchOn, Integer page, Integer item, | |
1026 | Integer[] tags) throws IOException { | |
1027 | new CliReader().searchBooksByTag(searchOn, page, item, tags); | |
1028 | } | |
1029 | ||
1030 | /** | |
1031 | * Start a Fanfix server. | |
1032 | * | |
9a553563 NR |
1033 | * @throws IOException |
1034 | * in case of I/O errors | |
1035 | * @throws SSLException | |
1036 | * when the key was not accepted | |
1037 | */ | |
f433d153 NR |
1038 | private void startServer() throws IOException { |
1039 | String mode = Instance.getInstance().getConfig() | |
1040 | .getString(Config.SERVER_MODE, "fanfix"); | |
1041 | if (mode.equals("fanfix")) { | |
1042 | RemoteLibraryServer server = new RemoteLibraryServer(); | |
1043 | server.setTraceHandler(Instance.getInstance().getTraceHandler()); | |
1044 | server.run(); | |
1045 | } else if (mode.equals("http")) { | |
1046 | WebLibraryServer server = new WebLibraryServer(false); | |
1047 | server.setTraceHandler(Instance.getInstance().getTraceHandler()); | |
1048 | server.run(); | |
1049 | } else if (mode.equals("https")) { | |
1050 | WebLibraryServer server = new WebLibraryServer(true); | |
1051 | server.setTraceHandler(Instance.getInstance().getTraceHandler()); | |
1052 | server.run(); | |
1053 | } else { | |
1054 | throw new IOException("Unknown server mode: " + mode); | |
1055 | } | |
9a553563 NR |
1056 | } |
1057 | ||
1058 | /** | |
1059 | * Stop a running Fanfix server. | |
1060 | * | |
1061 | * @param key | |
1062 | * the key to contact the Fanfix server | |
1063 | * @param host | |
1064 | * the host on which it runs (NULL means localhost) | |
1065 | * @param port | |
1066 | * the port on which it runs | |
1067 | * | |
1068 | * @throws IOException | |
1069 | * in case of I/O errors | |
1070 | * @throws SSLException | |
1071 | * when the key was not accepted | |
1072 | */ | |
1073 | private void stopServer( | |
1074 | String key, String host, Integer port) | |
1075 | throws IOException, SSLException { | |
1076 | new RemoteLibrary(key, host, port).exit(); | |
1077 | } | |
1078 | ||
1079 | /** | |
1080 | * We are done and ready to exit. | |
1081 | * <p> | |
1082 | * By default, it will call {@link System#exit(int)} if the status is not 0. | |
1083 | * | |
1084 | * @param status | |
1085 | * the exit status | |
1086 | */ | |
1087 | protected void exit(int status) { | |
1088 | if (status != 0) { | |
1089 | System.exit(status); | |
1090 | } | |
1091 | } | |
1092 | ||
1093 | /** | |
1094 | * Simple shortcut method to call {link Instance#getTrans()#getString()}. | |
1095 | * | |
1096 | * @param id | |
1097 | * the ID to translate | |
1098 | * | |
1099 | * @return the translated result | |
1100 | */ | |
1101 | static private String trans(StringId id, Object... params) { | |
1102 | return Instance.getInstance().getTrans().getString(id, params); | |
1103 | } | |
08fe2e33 | 1104 | } |