Project:
Wakanda
Code Location:
git://github.com/Wakanda/waf-mail.gitmaster
/
Outline
-
>
Fn
ThreeDigitReplyScope()
- V codeRegExp
- Fn ThreeDigitReplyException()
- V ThreeDigitReplyException
-
Fn
ThreeDigitReply()
- V errorCode
- V code
- V lines
- V isLinePending
- Fn this.readData(data)
- Fn this.isOk()
- Fn this.getErrorCode()
- Fn this.getCode()
- Fn this.getLines()
- Fn this.isPositiveCompletion()
- Fn this.isPositiveIntermediate()
- Fn this.isTransientNegativeCompletion()
- Fn this.isPermanentNegativeCompletion()
- Fn this.getNumberLines()
threeDigitReply.js
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
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
/* * This file is part of Wakanda software, licensed by 4D under * (i) the GNU General Public License version 3 (GNU GPL v3), or * (ii) the Affero General Public License version 3 (AGPL v3) or * (iii) a commercial license. * This file remains the exclusive property of 4D and/or its licensors * and is protected by national and international legislations. * In any event, Licensee's compliance with the terms and conditions * of the applicable license constitutes a prerequisite to any use of this file. * Except as otherwise expressly stated in the applicable license, * such license does not include any other license or rights on this file, * 4D's and/or its licensors' trademarks and/or other proprietary rights. * Consequently, no title, copyright or other proprietary rights * other than those specified in the applicable license is granted. */ // "Three-digit" reply used by SMTP and FTP protocols. // // References: // // http://www.ietf.org/rfc/rfc5321.txt (SMTP, section 4.2) // http://www.ietf.org/rfc/rfc959.txt (FTP, section 4.2) // // Note: // // Low-level library, to be used with SMTPClient and FTPClient. // // Usage: // // Data from socket is feed to readData() which will return an error code indicating if the reply has been read // completely (the number of line(s) read, a positive number), if more data is expected (zero), or if an error // occured (a negative number, see error constants) while reading reply. // // Then use getCode() to retrieve code (a string) of the reply. And getLines() to retrieve the array of line(s) // making up the reply. The line(s) are unprocessed, code(s) starting them are left. End user should call isOk() // to make sure reply has been read properly. If not, getCode(), getLines(), etc will throw an exception. function ThreeDigitReplyScope () { var codeRegExp = /^([2-5][0-5]\d)(-| )/; var ThreeDigitReplyException = function () { // Thrown if reply has not been properly read and trying retrieve content (getLines()) of reply. } function ThreeDigitReply () { this.OK = 0; // Reply has been successfully read. this.IN_PROGRESS = -1; // Currently reading reply. this.NO_DATA = -2; // Empty data is forbidden. this.INVALID_CODE = -3; // Lines must start by a valid code. this.CODES_DONT_MATCH = -4; // Codes in a reply must be same. this.EARLY_LAST_LINE = -5; // Last line indicator is not at last line of reply. var errorCode = this.IN_PROGRESS; // Read status. var code = null; // Three-digit reply code; var lines = new Array(); // All lines of the reply. Textual data starts at fourth character. var isLinePending = true; // Middle of a line?. // Read reply data, return: // // * a negative error constant if erroneous; // * zero if ongoing; // * or the number of line(s) read if reply is complete and ok. this.readData = function (data) { var array = data.split('\r\n'); if (!array.length) return errorCode = this.NO_DATA; var i, j; if (isLinePending) { if (!lines.length) lines[0] = array[0]; else { i = lines.length - 1; lines[i] = lines[i].concat(array[0]); } if (array.length == 1) return 0; // No '\r\n' yet, line is not complete yet. else { isLinePending = false; i = 1; } } else i = 0; j = lines.length; if ((code = lines[j - 1].match(codeRegExp)) == null) return errorCode = this.INVALID_CODE; else code = code[0].substring(0, 3); for ( ; i < array.length - 1; i++, j++) { lines[j] = array[i]; // Check for correctness. All codes must be the same, and last line indicator must be at last line! var matched; if ((matched = lines[j].match(codeRegExp)) == null) return errorCode = this.INVALID_CODE; if (code != matched[0].substring(0, 3)) return errorCode = this.CODE_DONT_MATCH; if (matched[0].charAt(3) == ' ' && i != array.length - 2) return errorCode = this.EARLY_LAST_LINE; // Premature last line. } if (array.length > 1 && array[i] != '') { lines[j] = array[i]; isLinePending = true; return 0; } if (lines[j - 1].charAt(3) == ' ') { // Reply is complete. code = lines[j - 1].substring(0, 3); errorCode = this.OK; return j; } else return 0; } // Return true if the reply has been successfully read. this.isOk = function () { return errorCode == this.OK; } // Return error code, which can pinpoint a reading error, if any. this.getErrorCode = function () { return errorCode; } // Return three-digit code. this.getCode = function () { if (errorCode != this.OK) throw new ThreeDigitReplyException(); else return code; } // Return reply line(s) as an Array of String. this.getLines = function () { if (errorCode != this.OK) throw new ThreeDigitReplyException(); else return lines; } // Successful completion of command. this.isPositiveCompletion = function () { if (errorCode != this.OK) throw new ThreeDigitReplyException(); else return code.charAt(0) == 2; } // Command accepted, but needs further data for completion. this.isPositiveIntermediate = function () { if (errorCode != this.OK) throw new ThreeDigitReplyException(); else return code.charAt(0) == 3; } // Command failed, but error condition is transient. Should try again. this.isTransientNegativeCompletion = function () { if (errorCode != this.OK) throw new ThreeDigitReplyException(); else return code.charAt(0) == 4; } // Erroneous command. this.isPermanentNegativeCompletion = function () { if (errorCode != this.OK) throw new ThreeDigitReplyException(); else return code.charAt(0) == 5; } // Return number of reply line(s). this.getNumberLines = function () { if (errorCode != this.OK) throw new ThreeDigitReplyException(); else return lines.length; } } return ThreeDigitReply; } exports.ThreeDigitReply = ThreeDigitReplyScope();
