Added various log messages to mailvisad.
[mailvisa2.git] / src / mailvisa.ml
blobf440acd23d886b7185a2d68fd8b6f039402fc8a5
1 (* Mailvisa ispatcher binary *)
3 open Unix
5 let usage = "USAGE mailvisa <command> [options]"
6 let help = "Valid commands are:
8 add Add messages to database
9 calculate Recalculate scores
10 check Check whether a message is spam
11 remove Remove messages from database
12 start Start daemon
13 view View scores
15 Use mailvisa <command> -h to list options for that command"
17 (** Execs the program designated by path. The program is
18 passed the concatenation of path and args as its arguments.
19 I.e. the program name is passed as the zeroth argument, as
20 is conventional in Unix.
22 let exec_program path args =
23 execvp path (Array.append [| path |] args)
25 (** Execs a mailvisa program with the given arguments. If
26 callerpath is an explicit path, its dirname is prefixed
27 to program. Otherwise, program is sought it the
28 system path.
30 let exec_mailvisa_program callername program args =
31 try
32 if (Filename.dirname callername) = "." && (Filename.is_implicit callername) then
33 exec_program program args
34 else
35 exec_program (Filename.concat (Filename.dirname callername) program) args
36 with error ->
37 (match error with
38 | Unix_error(errno, _, _) ->
39 prerr_endline ("Error running program " ^ program ^ ": " ^ (error_message errno))
40 | _ ->
41 prerr_endline ("Error running program " ^ program));
42 exit 1
44 let _ =
45 (* Abort and print usage message if no command was given *)
46 if (Array.length Sys.argv) < 2 then begin
47 prerr_endline usage;
48 exit 0x80
49 end;
50 (* Extract command and arguments *)
51 let progname = Sys.argv.(0) in
52 let command = Sys.argv.(1) in
53 let args = (let length = Array.length Sys.argv in
54 if length > 2
55 then Array.sub Sys.argv 2 (length - 2)
56 else [||])
58 match command with
59 | "add" -> exec_mailvisa_program progname "mailvisa-add-messages" args
60 | "calculate" -> exec_mailvisa_program progname "mailvisa-calculate-scores" args
61 | "check" -> exec_mailvisa_program progname "mailvisa-check" args
62 | "help" -> print_endline help
63 | "remove" -> exec_mailvisa_program progname "mailvisa-remove-messages" args
64 | "start" -> exec_mailvisa_program progname "mailvisad" args
65 | "view" -> exec_mailvisa_program progname "mailvisa-view-scores" args
66 | _ ->
67 prerr_endline usage;
68 prerr_endline help;
69 exit 0x80