-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.ml
104 lines (89 loc) · 3.86 KB
/
response.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
(*
* Copyright (c) 2013-2014 Gregory Tsipenyuk <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)
open Core.Std
open Utils
open States
module StatusResponse : sig
type response_type = Ok|Bad|No|Preauth|Bye
val ok : ?tag:string -> ?code:responseCode option -> string -> string
val bad : ?tag:string -> ?code:responseCode option -> string -> string
val no : ?tag:string -> ?code:responseCode option -> string -> string
val preauth : ?code:responseCode option -> string -> string
val bye : ?code:responseCode option -> string -> string
val untagged : string -> string
val any : string -> string
val continue : ?text:string -> unit -> string
end = struct
type response_type = Ok|Bad|No|Preauth|Bye
(** use for utf8?? **)
let to_str x = x
(** have to change the brackets, review neew to build
* structures that then are printed, maybe sexp TBD
**)
let response_code code = match code with
| Some rc -> (match rc with
| RespCode_Alert -> "[ALERT"
| RespCode_Badcharset -> "[BADCHARSET"
| RespCode_Capability -> "[CAPABILITY"
| RespCode_Parse -> "[PARSE"
| RespCode_Permanentflags -> "[PERMANENTFLAGS"
| RespCode_Read_only -> "[READ-ONLY"
| RespCode_Read_write -> "[READ-WRITE"
| RespCode_Trycreate -> "[TRYCREATE"
| RespCode_Uidnext -> "[UIDNEXT"
| RespCode_Uidvalidity -> "[UIDVALIDITY"
| RespCode_Unseen -> "[UNSEEN")
| None -> ""
let get_rtype = function
| Ok -> "OK"
| Bad -> "BAD"
| No -> "NO"
| Preauth -> "PREAUTH"
| Bye -> "BYE"
let get_response ?(tag="*") ?(code=None) ~rtype text =
let l = [tag; get_rtype rtype; response_code code; text] in
let acc = List.fold l
~init:""
~f:(fun acc s -> if acc = "" then s else if s = "" then acc else acc ^ sp() ^ s) in
match code with
|None-> to_str acc
|Some _ -> to_str (acc ^ "]")
let ok ?(tag="*") ?(code=None) text = get_response ~tag ~code ~rtype:Ok text
let bad ?(tag="*") ?(code=None) text = get_response ~tag ~code ~rtype:Bad text
let no ?(tag="*") ?(code=None) text = get_response ~tag ~code ~rtype:No text
let preauth ?(code=None) text = get_response ~tag:"*" ~code ~rtype:Preauth text
let bye ?(code=None) text = get_response ~tag:"*" ~code ~rtype:Bye text
let untagged text = to_str ("*" ^ sp() ^ text)
let any text = to_str text
let continue ?text () =
let pl = "+" in
let str = (match text with
| None -> pl
| Some t -> pl ^ sp() ^ t) in
to_str str
end
let write_resp w ?(tag="*") resp =
match resp with
| Resp_Ok (code, s) -> send_wcrlf w (StatusResponse.ok ~tag ~code s)
| Resp_No (code, s) -> send_wcrlf w (StatusResponse.no ~tag ~code s)
| Resp_Bad (code, s) -> send_wcrlf w (StatusResponse.bad ~tag ~code s)
| Resp_Bye (code, s) -> send_wcrlf w (StatusResponse.bye ~code s)
| Resp_Preauth (code, s) -> send_wcrlf w (StatusResponse.preauth ~code s)
| Resp_Cont (text) -> send_wcrlf w (StatusResponse.continue ~text ())
| Resp_Untagged (text) -> send_wcrlf w (StatusResponse.untagged text)
| Resp_Any (text) -> send_wcrlf w (StatusResponse.any text)
let write_resp_untagged writer text =
write_resp writer (Resp_Untagged text)