File: POP.st

package info (click to toggle)
gnu-smalltalk 2.1.8-2.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 17,484 kB
  • ctags: 8,274
  • sloc: ansic: 53,661; sh: 17,366; perl: 4,319; awk: 1,319; yacc: 1,023; makefile: 1,010; sed: 258; lex: 249
file content (316 lines) | stat: -rw-r--r-- 8,616 bytes parent folder | download
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
306
307
308
309
310
311
312
313
314
315
316
"======================================================================
|
|   POP3 protocol support
|
|
 ======================================================================"


"======================================================================
|
| Based on code copyright (c) Kazuki Yasumatsu, and in the public domain
| Copyright (c) 2002 Free Software Foundation, Inc.
| Adapted by Paolo Bonzini.
|
| This file is part of the GNU Smalltalk class library.
|
| The GNU Smalltalk class library is free software; you can redistribute it
| and/or modify it under the terms of the GNU Lesser General Public License
| as published by the Free Software Foundation; either version 2.1, or (at
| your option) any later version.
|
| The GNU Smalltalk class library is distributed in the hope that it will be
| useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
| General Public License for more details.
|
| You should have received a copy of the GNU Lesser General Public License
| along with the GNU Smalltalk class library; see the file COPYING.LIB.
| If not, write to the Free Software Foundation, 59 Temple Place - Suite
| 330, Boston, MA 02111-1307, USA.
|
 ======================================================================"


Namespace current: NetClients.POP!

NetResponse subclass:  #POPResponse
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'NetClients-POP3'!

POPResponse comment: 
'
Copyright (c) Kazuki Yasumatsu, 1995. All rights reserved.
'!

NetClient subclass:  #POPClient
	instanceVariableNames: 'loggedInUser'
	classVariableNames: ''
	poolDictionaries: 'MIME '
	category: 'NetClients-POP3'!

POPClient comment: 
'
Copyright (c) Kazuki Yasumatsu, 1995. All rights reserved.
'!

NetProtocolInterpreter subclass:  #POPProtocolInterpreter
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: 'MIME '
	category: 'NetClients-POP3'!

POPProtocolInterpreter comment: 
'
Copyright (c) Kazuki Yasumatsu, 1995. All rights reserved.
'!

!POPResponse methodsFor: 'printing'!

printStatusOn: aStream
    status notNil
    	ifTrue: [status = 1
    		ifTrue:	[aStream nextPutAll: '+OK ']
    		ifFalse: [aStream nextPutAll: '-ERR ']].
    statusMessage notNil
    	ifTrue: [aStream nextPutAll: statusMessage].! !

!POPResponse methodsFor: 'private'!

parseStatusLine: aClient
    "Returned string is: '+OK ok message' or '-ERR error message'"
    | stream |
    stream := aClient nextLine readStream.
    "status = 1 (OK), status = 0 (ERR)"
    stream next = $+ ifTrue: [status := 1] ifFalse: [status := 0].
    stream skipTo: Character space.
    stream skipSeparators.
    statusMessage := stream upToEnd! !


!POPClient class methodsFor: 'examples'!

example2Host: host username: username password: password

    [self exampleHost: host username: username password: password]
	on: LoginIncorrectError
    	do: [:ex | 'Login incorrect' printNl. ex return]!

exampleHost: host username: username password: password
    | client |
    client := POPClient connectToHost: host.
    [client
    	username: username
    	password: password.

    client login.
    Transcript showCr: 'New messages: ', client newMessagesCount printString.
    Transcript showCr: 'bytes ', client newMessagesSize printString.
    Transcript showCr: 'ids ', client newMessagesIds printString.
    Transcript showCr: 'sizes ', client newMessages printString.

    client
    	getNewMailMessages: [:m | m inspect]
    	delete: false
    ] ensure: [client close] ! !

!POPClient methodsFor: 'accessing'!

login
    loggedInUser = self user ifTrue: [ ^self ].
    loggedInUser isNil ifFalse: [ self logout ].
    self connect.
    self clientPI popUser: self username.
    self clientPI popPassword: self password.
    loggedInUser := self user!

logout
    self clientPI popQuit!

newMessagesCount
    ^self clientPI popStatus key!

newMessagesSize
    ^self clientPI popStatus value!

newMessagesIds
    ^self clientPI popList keys asSortedCollection asArray!

newMessages
    ^self clientPI popList!

sizeAt: id
    ^self clientPI popList: id!

headersAt: id
    ^self clientPI popTop: id lines: 1!

at: id
    ^self clientPI popRetrieve: id!

getNewMailHeaders: messageBlock delete: delete
    | count entity |
    self login.
    count := self clientPI popStatus key.
    count = 0 ifFalse:
    	[1 to: count do: [:i |
    		entity := self clientPI popTop: i lines: 1.
    		messageBlock value: entity ].
    	delete
    		ifTrue: [1 to: count do: [:i | self clientPI popDelete: i]]
    ]!

getNewMailMessages: messageBlock delete: delete
    | count entity |
    self login.
    count := self clientPI popStatus key.
    count = 0 ifFalse:
    	[1 to: count do: [:i |
    		entity := self clientPI popRetrieve: i.
    		messageBlock value: entity ].
    	delete
    		ifTrue: [1 to: count do: [:i | self clientPI popDelete: i]]
    ]!

getNewMailStreams: streamBlock delete: delete
    | count |
    self connectIfClosed.
    self clientPI popUser: self username.
    self clientPI popPassword: self password.
    count := self clientPI popStatus.
    count = 0 ifFalse:
    	[1 to: count do: [:i |
    		self clientPI popRetrieve: i into: streamBlock value.
    		].
    	delete
    		ifTrue: [1 to: count do: [:i | self clientPI popDelete: i]]
    ]! !

!POPClient methodsFor: 'private'!

protocolInterpreter
    ^POPProtocolInterpreter! !

!POPProtocolInterpreter methodsFor: 'pop protocol'!

connect
    super connect.
    self checkResponse!

popDelete: anInteger
    self nextPutAll: ('DELE ', anInteger printString); cr.
    self checkResponse!

popList
    | stream dictionary assoc |
    self nextPutAll: 'LIST'; cr.
    self checkResponse.

    dictionary := LookupTable new.
    stream := ReadWriteStream on: (String new: 100).
    self receiveMessageUntilPeriodInto: stream.
    stream reset.

    [
	assoc := self parseSizeDataFrom: stream nextLine readStream.
        assoc key > 0
    ] whileTrue: [
	dictionary add: assoc
    ].
    ^dictionary
!

popList: anInteger
    | stream response |
    self nextPutAll: ('LIST ', anInteger printString); cr.
    response := self getResponse.
    self checkResponse: response.
    response statusMessage == nil ifTrue: [^0].
    stream := response statusMessage readStream.
    ^(self parseSizeDataFrom: stream) value!

popPassword: password
    | response |
    self nextPutAll: ('PASS ', password); cr.
    response := self getResponse.
    self
    	checkResponse: response
    	ifError: [self loginIncorrectError: response statusMessage]!

popQuit
    self nextPutAll: 'QUIT'; cr.
    self checkResponse!

popRetrieve: anInteger
    self nextPutAll: ('RETR ', anInteger printString); cr.
    self checkResponse.
    ^MIME.MimeEntity readFromClient: self connectionStream!

popRetrieve: anInteger into: aStream
    self nextPutAll: ('RETR ', anInteger printString); cr.
    self checkResponse.
    self receiveMessageUntilPeriodInto: aStream!

popStatus
    "Check status and return a number of messages."
    | response stream |
    self nextPutAll: 'STAT'; cr.
    response := self getResponse.
    self checkResponse: response.
    response statusMessage == nil ifTrue: [^0 -> 0].
    stream := response statusMessage readStream.
    ^self parseSizeDataFrom: stream!

popTop: anInteger lines: linesInteger
    self nextPutAll: ('TOP ', anInteger printString);
	 nextPutAll: (' ', linesInteger printString);
         cr.

    self checkResponse.
    ^MIME.MimeEntity readFromClient: self connectionStream!

popTop: anInteger lines: linesInteger into: aStream
    self nextPutAll: ('TOP ', anInteger printString);
	 nextPutAll: (' ', linesInteger printString);
         cr.

    self checkResponse.
    self receiveMessageUntilPeriodInto: aStream!

popUser: user
    self nextPutAll: ('USER ', user); cr.
    self checkResponse! !

!POPProtocolInterpreter methodsFor: 'private'!

checkResponse: response
    ^self
    	checkResponse: response
    	ifError: [self protocolError: response statusMessage]!

checkResponse: response ifError: errorBlock
    | status |
    status := response status.
    status = 1 "OK"	ifTrue: [^self].
    ^errorBlock value!

parseSizeDataFrom: stream
    | count size |
    stream skipSeparators.
    count := Integer readFrom: stream.
    stream skipSeparators.
    size := Integer readFrom: stream.
    ^count -> size! !

!POPProtocolInterpreter class methodsFor: 'private-attributes'!

defaultPortNumber
    ^110!

defaultResponseClass
    ^POPResponse! !


Namespace current: Smalltalk!