Source file Config.ml

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
open Prelude

type irc_log =
  [ `None
  | `Chan of Lwt_io.output_channel
  | `Custom of (string -> unit Lwt.t)
  ]

type t = {
  server : string;
  port : int;
  username : string;
  realname : string;
  nick : string;
  tls: bool;
  tls_cert : Ssl.certificate option;
  channel : string;
  state_file : string;
  irc_log: irc_log; (* log IRC events *)
  log_level: Logs.level;
  prefix: string; (** prefix for commands *)
}

let default = {
  server = "irc.freenode.net";
  port = 7000;
  username = "calculon";
  realname = "calculon";
  nick = "calculon";
  tls = true;
  tls_cert = None;
  channel = "#ocaml";
  state_file = "state.json";
  log_level=Logs.Warning;
  irc_log = `None;
  prefix = "!";
}

let parse ?(extra_args=[]) conf args =
  let custom_nick = ref None in
  let custom_chan = ref None in
  let custom_server = ref None in
  let custom_state = ref None in
  let custom_port = ref conf.port in
  let custom_tls = ref None in
  let prefix = ref default.prefix in
  let log_lvl = ref None in
  let options = Arg.align @@ extra_args @
      [ "--nick", Arg.String (fun s -> custom_nick := Some s),
        " custom nickname (default: " ^ default.nick ^ ")"
      ; "--chan", Arg.String (fun s -> custom_chan := Some s),
        " channel to join (default: " ^ default.channel ^ ")"
      ; "--port", Arg.Set_int custom_port, " port of the server"
      ; "--server", Arg.String (fun s -> custom_server := Some s),
        " server to join (default: " ^ default.server ^ ")"
      ; "--state", Arg.String (fun s -> custom_state := Some s),
        " file containing factoids (default: " ^ default.state_file ^ ")"
      ; "--tls", Arg.Unit (fun () -> custom_tls := Some true), " enable TLS"
      ; "--no-tls", Arg.Unit (fun () -> custom_tls := Some false), " disable TLS"
      ; "--debug", Arg.Unit (fun() ->log_lvl := Some Logs.Debug), " print debug messages (on stderr)"
      ; "--prefix", Arg.Set_string prefix, " set prefix for commands (default \"!\")";
      ]
  in
  let log_level = !log_lvl |? conf.log_level in
  Arg.parse_argv args options ignore "parse options";
  Logs.set_level ~all:true (Some log_level);
  { conf with
    nick = !custom_nick |? conf.nick;
    channel = !custom_chan |? conf.channel;
    server = !custom_server |? conf.server;
    tls = !custom_tls |? conf.tls;
    port = !custom_port;
    state_file = !custom_state |? conf.state_file;
    log_level;
    irc_log=`None;
    prefix = !prefix;
  }

let of_argv ?extra_args () =
  try parse ?extra_args default Sys.argv
  with
    | Arg.Bad msg -> print_endline msg; exit 1
    | Arg.Help msg -> print_endline msg; exit 0