-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcitadel.go
305 lines (253 loc) · 7.67 KB
/
citadel.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Package bitbucket.org/gotamer/citadel is a Citadel Client library to access Citadel email and collaboration servers from Go using the Citadel Protocol.
// http://www.citadel.org
package citadel
import (
"crypto/tls"
"fmt"
"net"
"net/textproto"
"os"
"strconv"
"strings"
"time"
"bitbucket.org/gotamer/errors"
)
/************************************************
* Client ID's for CitadelGo
* 1. CitadelGo the library
* 2. CitadelSync https://bitbucket.org/gotamer/citadel/sync
************************************************/
var (
DEVELOPER_ID = 12 // Developer ID number (same as the server developer ID numbers in the INFO command – please obtain one if you are a new developer)
CLIENT_ID = 1 // Client ID number (which does not have to be globally unique - only unique within the domain of the developer number)
CLIENT_VERSION = 3 // Client version number the Version of your Client
CLIENT_NAME = "Lib" // Client IDString afree-form text string describing the client
)
const (
DS = "|"
DE = "000"
VIEW_BBS = "0" // Bulletin board view
VIEW_MAILBOX = "1" // Mailbox summary
VIEW_ADDRESSBOOK = "2" // Address book view
VIEW_CALENDAR = "3" // Calendar view
VIEW_TASKS = "4" // Tasks view
VIEW_NOTES = "5" // Notes view
VIEW_WIKI = "6" // Wiki view
CODE_DONE = 0
CODE_LISTING_FOLLOWS = 1 // The requested operation is progressing and is now delivering text. The client *must* now read lines of text until it receives the termination sequence (“000” on a line by itself).
CODE_OK = 2 // The requested operation succeeded.
CODE_MORE_DATA = 3 // The requested operation succeeded so far, but another command is required to complete it.
CODE_SEND_LISTING = 4 // The requested operation is progressing and is now expecting text. The client *must* now transmit zero or more lines of text followed by the termination sequence (“000” on a line by itself).
CODE_ERROR = 5 // The requested operation failed. The second and third digits of the error code and/or the error message following it describes why.
CODE_BINARY_FOLLOWS = 6 // After this line please read n bytes. (n follows after a blank)
CODE_SEND_BINARY = 7 // you now may send us n bytes binary data. (n follows after a blank)
CODE_START_CHAT_MODE = 8 // ok, we are in chat mode now. every line you send will be broadcasted.
CODE_ASYNC_MSG = 9 // there is a page waiting for you, please fetch it.
MESG_OK = 0
MESG_ASYNC_GEXP = 02
MESG_INTERNAL_ERROR = 10
MESG_TOO_BIG = 11
MESG_ILLEGAL_VALUE = 12
MESG_NOT_LOGGED_IN = 20
MESG_CMD_NOT_SUPPORTED = 30
MESG_SERVER_SHUTTING_DOWN = 31
MESG_PASSWORD_REQUIRED = 40
MESG_ALREADY_LOGGED_IN = 41
MESG_USERNAME_REQUIRED = 42
MESG_HIGHER_ACCESS_REQUIRED = 50
MESG_MAX_SESSIONS_EXCEEDED = 51
MESG_RESOURCE_BUSY = 52
MESG_RESOURCE_NOT_OPEN = 53
MESG_NOT_HERE = 60
MESG_INVALID_FLOOR_OPERATION = 61
MESG_NO_SUCH_USER = 70
MESG_FILE_NOT_FOUND = 71
MESG_ROOM_NOT_FOUND = 72
MESG_NO_SUCH_SYSTEM = 73
MESG_ALREADY_EXISTS = 74
MESG_MESSAGE_NOT_FOUND = 75
)
type Citadel struct {
tls *tls.Conn
Conn *textproto.Conn
Room room // Current room data
Floor floor // Current floor data
Code int // Citadel reponce CODE_XXXX
Mesg int // Citadel responce MESG_XXXX
Resp []string
Raw string // Raw responce from citadel
Error error
}
func New(addr string) (c *Citadel) {
c = new(Citadel)
c.Open(addr)
return
}
func (c *Citadel) Open(addr string) {
var err error
c.Conn, err = textproto.Dial("tcp", addr)
e.Check(err)
_, c.Error = c.Conn.ReadLine()
c.Check()
c.Iden()
}
// Timeout values for the Dial functions.
var (
NetTimeout = 30 * time.Second // Time to establish a TCP connection
ServerTimeout = 60 * time.Second // Time to receive greeting and capabilities
)
// Dial returns a new Client connected to a server at addr.
func Dial(addr string) (cit *Citadel, err error) {
addr = defaultPort(addr, "504")
cit = new(Citadel)
if cit.Conn, err = textproto.Dial("tcp", addr); err == nil {
_, cit.Error = cit.Conn.ReadLine()
cit.Check()
}
return
}
// defaultPort joins addr and port if addr contains just the host name or IP.
func defaultPort(addr, port string) string {
_, _, err := net.SplitHostPort(addr)
if err != nil {
addr = net.JoinHostPort(addr, port)
}
return addr
}
func (c *Citadel) Close() {
c.Request("QUIT")
err := c.Conn.Close()
e.Check(err)
}
func (c *Citadel) Iden() {
hostname, err := os.Hostname()
if err != nil {
hostname = "localhost"
}
var cmd = fmt.Sprintf("IDEN %v|%v|%v|CitGo %v|%v", DEVELOPER_ID, CLIENT_ID, CLIENT_VERSION, CLIENT_NAME, hostname)
c.Request(cmd)
}
// Makes a request and returns the 1st responce
func (c *Citadel) Request(cmd string) (ok bool) {
var err error
e.Info(cmd)
err = c.Conn.PrintfLine("%s", cmd)
ok = e.Check(err)
c.Raw, c.Error = c.Conn.ReadLine()
c.Check()
c.Raw = strings.Trim(c.Raw, " |")
c.Code, err = strconv.Atoi(c.Raw[0:1])
ok = e.Check(err)
if c.Code != 0 && len(c.Raw) > 2 {
c.Mesg, err = strconv.Atoi(c.Raw[1:3])
ok = e.Check(err)
}
if len(c.Raw) > 4 {
c.Resp = strings.Split(c.Raw[4:], DS)
e.Info("Resp: %v", c.Resp)
}
return
}
// This is for multi line responces.
// Used when receiving CODE_LISTING_FOLLOWS
func (c *Citadel) Responce() (r [][]string) {
var err error
var text string
for {
text, err = c.Conn.ReadLine()
if e.Check(err) {
if text == DE {
break
}
r = append(r, strings.Split(text, "|"))
}
}
return
}
func (c *Citadel) code() (ok bool) {
switch c.Code {
case CODE_LISTING_FOLLOWS:
c.Error = nil
ok = true
case CODE_OK:
c.Error = nil
ok = true
case CODE_MORE_DATA:
c.Error = nil
ok = true
case CODE_SEND_LISTING:
c.Error = nil
ok = true
case CODE_ERROR:
c.setError()
case CODE_BINARY_FOLLOWS:
c.Error = nil
ok = true
case CODE_SEND_BINARY:
c.Error = nil
ok = true
case CODE_START_CHAT_MODE:
c.Error = nil
ok = true
case CODE_ASYNC_MSG:
c.Error = nil
ok = true
default:
c.setError()
}
c.Check()
return
}
/*
switch c.Mesg {
case MESG_ALREADY_EXISTS:
log.Println("Mesg: ", c.Mesg)
case MESG_ALREADY_LOGGED_IN:
log.Println("Mesg: ", c.Mesg)
case MESG_ASYNC_GEXP:
log.Println("Mesg: ", c.Mesg)
case MESG_CMD_NOT_SUPPORTED:
log.Println("Mesg: ", c.Mesg)
case MESG_FILE_NOT_FOUND:
log.Println("Mesg: ", c.Mesg)
case MESG_HIGHER_ACCESS_REQUIRED:
log.Println("Mesg: ", c.Mesg)
case MESG_ILLEGAL_VALUE:
log.Println("Mesg: ", c.Mesg)
case MESG_INTERNAL_ERROR:
log.Println("Mesg: ", c.Mesg)
case MESG_INVALID_FLOOR_OPERATION:
log.Println("Mesg: ", c.Mesg)
case MESG_MAX_SESSIONS_EXCEEDED:
log.Println("Mesg: ", c.Mesg)
case MESG_MESSAGE_NOT_FOUND:
log.Println("Mesg: ", c.Mesg)
case MESG_NOT_HERE:
log.Println("Mesg: ", c.Mesg)
case MESG_NOT_LOGGED_IN:
log.Println("Mesg: ", c.Mesg)
case MESG_NO_SUCH_SYSTEM:
log.Println("Mesg: ", c.Mesg)
case MESG_NO_SUCH_USER:
log.Println("Mesg: ", c.Mesg)
case MESG_OK:
log.Println("Mesg: ", c.Mesg)
case MESG_PASSWORD_REQUIRED:
log.Println("Mesg: ", c.Mesg)
case MESG_RESOURCE_BUSY:
log.Println("Mesg: ", c.Mesg)
case MESG_RESOURCE_NOT_OPEN:
log.Println("Mesg: ", c.Mesg)
case MESG_ROOM_NOT_FOUND:
log.Println("Mesg: ", c.Mesg)
case MESG_SERVER_SHUTTING_DOWN:
log.Println("Mesg: ", c.Mesg)
case MESG_TOO_BIG:
log.Println("Mesg: ", c.Mesg)
case MESG_USERNAME_REQUIRED:
log.Println("Mesg: ", c.Mesg)
default:
log.Println("Unknown Message: ")
c.Error = fmt.Errorf("CIT CODE: %v CIT MESG: %v %s", c.Code, c.Mesg, c.Resp)
}
*/