-
Notifications
You must be signed in to change notification settings - Fork 4
/
server_response.go
87 lines (74 loc) · 2.08 KB
/
server_response.go
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
// Copyright 2014 The imapsrv Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the imapsrv.LICENSE file.
package imap
import (
"bufio"
)
// An IMAP serverResponse
type serverResponse struct {
// The tag of the command that this is the serverResponse for
tag string
// The machine readable condition
condition string
// A human readable message
message string
// Untagged output lines
untagged []string
// Should the connection be closed after the serverResponse has been sent?
closeConnection bool
}
// Create a serverResponse
func createResponse(tag string, condition string, message string) *serverResponse {
return &serverResponse{
tag: tag,
condition: condition,
message: message,
untagged: make([]string, 0, 4),
}
}
// Create a OK serverResponse
func ok(tag string, message string) *serverResponse {
return createResponse(tag, "OK", message)
}
// Create an BAD serverResponse
func bad(tag string, message string) *serverResponse {
return createResponse(tag, "BAD", message)
}
// Create a NO serverResponse
func no(tag string, message string) *serverResponse {
return createResponse(tag, "NO", message)
}
// Write an untagged fatal serverResponse
func fatalResponse(w *bufio.Writer, err error) {
resp := createResponse("*", "BYE", err.Error())
resp.closeConnection = true
resp.write(w)
}
// Add an untagged line to a serverResponse
func (r *serverResponse) extra(line string) *serverResponse {
r.untagged = append(r.untagged, line)
return r
}
// Mark that a serverResponse should close the connection
func (r *serverResponse) shouldClose() *serverResponse {
r.closeConnection = true
return r
}
// Write a serverResponse to the given writer
func (r *serverResponse) write(w *bufio.Writer) error {
// Write untagged lines
for _, line := range r.untagged {
_, err := w.WriteString("* " + line + "\r\n")
if err != nil {
return err
}
}
_, err := w.WriteString(r.tag + " " + r.condition + " " + r.message + "\r\n")
if err != nil {
return err
}
// Flush the serverResponse
w.Flush()
return nil
}