2008-03-03 08:42:30 -07:00
|
|
|
type t = string option * string * string list * string option
|
2008-02-28 22:00:24 -07:00
|
|
|
|
2008-03-02 21:30:37 -07:00
|
|
|
let create sender name args text =
|
2008-03-03 08:42:30 -07:00
|
|
|
(sender, name, args, text)
|
2008-02-28 22:00:24 -07:00
|
|
|
|
2008-03-02 21:30:37 -07:00
|
|
|
let anon = create None
|
|
|
|
|
2008-03-03 08:42:30 -07:00
|
|
|
let as_string (sender, name, args, text) =
|
2008-02-28 22:00:24 -07:00
|
|
|
let ret = Buffer.create 120 in
|
2008-03-03 08:42:30 -07:00
|
|
|
(match sender with
|
2008-02-28 22:00:24 -07:00
|
|
|
| None ->
|
|
|
|
()
|
|
|
|
| Some s ->
|
|
|
|
Buffer.add_char ret ':';
|
|
|
|
Buffer.add_string ret s;
|
|
|
|
Buffer.add_char ret ' ');
|
2008-03-03 08:42:30 -07:00
|
|
|
Buffer.add_string ret name;
|
|
|
|
(match args with
|
2008-02-28 22:00:24 -07:00
|
|
|
| [] ->
|
|
|
|
()
|
|
|
|
| l ->
|
|
|
|
Buffer.add_char ret ' ';
|
|
|
|
Buffer.add_string ret (String.concat " " l));
|
2008-03-03 08:42:30 -07:00
|
|
|
(match text with
|
2008-02-28 22:00:24 -07:00
|
|
|
| None ->
|
|
|
|
()
|
|
|
|
| Some txt ->
|
|
|
|
Buffer.add_string ret " :";
|
|
|
|
Buffer.add_string ret txt);
|
|
|
|
Buffer.contents ret
|
|
|
|
|
|
|
|
let extract_word s =
|
|
|
|
try
|
|
|
|
let pos = String.index s ' ' in
|
|
|
|
(Str.string_before s pos, Str.string_after s (pos + 1))
|
|
|
|
with Not_found ->
|
|
|
|
(s, "")
|
|
|
|
|
|
|
|
let rec from_string line =
|
|
|
|
(* Very simple. Pull out words until you get one starting with ":".
|
|
|
|
The very first word might start with ":", that doesn't count
|
|
|
|
because it's the sender.. *)
|
|
|
|
let rec loop sender acc line =
|
|
|
|
let c = (if (line = "") then None else (Some line.[0])) in
|
|
|
|
match (c, acc) with
|
2008-03-03 08:42:30 -07:00
|
|
|
| (None, name :: args) ->
|
2008-02-28 22:00:24 -07:00
|
|
|
(* End of line, no text part *)
|
2008-03-06 16:11:57 -07:00
|
|
|
create sender (String.uppercase name) args None
|
2008-02-28 22:00:24 -07:00
|
|
|
| (None, []) ->
|
|
|
|
(* End of line, no text part, no args, no command *)
|
|
|
|
raise (Failure "No command, eh?")
|
|
|
|
| (Some ':', []) ->
|
|
|
|
(* First word, starts with ':' *)
|
|
|
|
let (word, rest) = extract_word line in
|
|
|
|
loop (Some (Str.string_after word 1)) acc rest
|
2008-03-03 08:42:30 -07:00
|
|
|
| (Some ':', name :: args) ->
|
2008-02-28 22:00:24 -07:00
|
|
|
(* Not first word, starts with ':' *)
|
2008-03-06 16:11:57 -07:00
|
|
|
create sender (String.uppercase name) args (Some (Str.string_after line 1))
|
2008-02-28 22:00:24 -07:00
|
|
|
| (Some _, _) ->
|
|
|
|
(* Argument *)
|
|
|
|
let (word, rest) = extract_word line in
|
|
|
|
loop sender (acc @ [word]) rest
|
|
|
|
in
|
|
|
|
loop None [] line
|
|
|
|
|
|
|
|
|
2008-03-03 08:42:30 -07:00
|
|
|
let as_tuple cmd = cmd
|
2008-02-28 22:00:24 -07:00
|
|
|
|
2008-03-03 08:42:30 -07:00
|
|
|
let sender (sender, name, args, text) = sender
|
|
|
|
let name (sender, name, args, text) = name
|
|
|
|
let args (sender, name, args, text) = args
|
|
|
|
let text (sender, name, args, text) = text
|