Debian Codegraph @

Framework/Pantomime/CWService.h

raw symbols SCIP
1 /*
2 ** CWService.h
3 **
4 ** Copyright (c) 2001-2007 Ludovic Marcotte
5 **
6 ** Author: Ludovic Marcotte <[email protected]>
7 **
8 ** This library is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU Lesser General Public
10 ** License as published by the Free Software Foundation; either
11 ** version 2.1 of the License, or (at your option) any later version.
12 **
13 ** This library is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** Lesser General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #ifndef _Pantomime_H_CWService
23 #define _Pantomime_H_CWService
24
25 #import <Pantomime/CWConnection.h>
26 #import <Pantomime/CWConstants.h>
27
28 #import <Foundation/NSArray.h>
29 #import <Foundation/NSObject.h>
30 #import <Foundation/NSNotification.h>
31 #import <Foundation/NSRunLoop.h>
32 #import <Foundation/NSTimer.h>
33
34 #ifdef MACOSX
35 #import <Foundation/NSMapTable.h>
36 #include <CoreFoundation/CoreFoundation.h>
37 #endif
38
39 @class CWService;
40
41 /*!
42 @function split_lines
43 @discussion This function is used to split lines from <i>theMutableData</i> and
44 return the first one found immediately (by removing it first
45 from <i>theMutableData</i>). IMAP, POP3 and SMTP servers reponses are
46 always ended by CRLF.
47 @param theMutableData The data from which to split lines from.
48 @result A line as a NSData instance, nil if no line was splitted.
49 */
50 static inline NSData *split_lines(NSMutableData *theMutableData)
51 {
52 char *bytes, *end;
53 NSUInteger i, count;
54
55 end = bytes = (char *)[theMutableData mutableBytes];
56 count = [theMutableData length];
57
58 for (i = 0; i < count; i++)
59 {
60 if (*end == '\n' && *(end-1) == '\r')
61 {
62 NSData *aData;
63
64 aData = [NSData dataWithBytes: bytes length: (i-1)];
65 memmove(bytes,end+1,count-i-1);
66 [theMutableData setLength: count-i-1];
67 return aData;
68 }
69
70 end++;
71 }
72 return nil;
73 }
74
75 /*!
76 @const PantomimeAuthenticationCompleted
77 */
78 extern NSString* PantomimeAuthenticationCompleted;
79
80 /*!
81 @const PantomimeAuthenticationFailed
82 */
83 extern NSString* PantomimeAuthenticationFailed;
84
85 /*!
86 @const PantomimeConnectionEstablished
87 */
88 extern NSString* PantomimeConnectionEstablished;
89
90 /*!
91 @const PantomimeConnectionLost
92 */
93 extern NSString* PantomimeConnectionLost;
94
95 /*!
96 @const PantomimeConnectionTerminated
97 */
98 extern NSString* PantomimeConnectionTerminated;
99
100 /*!
101 @const PantomimeConnectionTimedOut
102 */
103 extern NSString* PantomimeConnectionTimedOut;
104
105 /*!
106 @const PantomimeRequestCancelled
107 */
108 extern NSString* PantomimeRequestCancelled;
109
110 /*!
111 @const PantomimeServiceInitialized
112 */
113 extern NSString* PantomimeServiceInitialized;
114
115 /*!
116 @const PantomimeServiceReconnected
117 */
118 extern NSString* PantomimeServiceReconnected;
119
120 /*!
121 @const PantomimeProtocolException
122 @description This exception can be raised if a major
123 protocol handling error occured in one
124 of the CWService subclasses. This would
125 mean that Pantomime has a bug.
126 */
127 extern NSString* PantomimeProtocolException;
128
129 /*!
130 @category NSObject (CWServiceClient)
131 @discussion This informal protocol defines methods that can implemented in
132 CWService's delegate (CWIMAPStore, CWPOP3Store or CWSMTP instance)
133 to control the behavior of the class or to obtain status information.
134 You can release/autorelease a CWService instance in -connectionTimedOut:,
135 -connectionLost: or -connectionTerminated (or the respective
136 notification handlers). You may NOT do it elsewhere.
137 */
138 @interface NSObject (CWServiceClient)
139
140 /*!
141 @method authenticationCompleted:
142 @discussion This method is automatically called on the delegate
143 when the authentication has sucessfully completed
144 on the underlying Service's instance.
145 A PantomimeAuthenticationCompleted notification is also posted.
146 The authentication mechanism that was used can be obtained
147 from the notification's userInfo using the "Mechanism" key.
148 @param theNotification The notification holding the information.
149 */
150 - (void) authenticationCompleted: (NSNotification *) theNotification;
151
152 /*!
153 @method authenticationFailed:
154 @discussion This method is automatically called on the delegate
155 when the authentication has failed on the underlying Service's instance.
156 A PantomimeAuthenticationCompleted notification is also posted.
157 The authentication mechanism that was used can be obtained
158 from the notification's userInfo using the "Mechanism" key.
159 @param theNotification The notification holding the information.
160 */
161 - (void) authenticationFailed: (NSNotification *) theNotification;
162
163 /*!
164 @method connectionEstablished:
165 @discussion Invoked once the connection has been established with the peer.
166 A PantomimeConnectionEstablished notification is also posted.
167 @param theNotification The notification holding the information.
168 */
169 - (void) connectionEstablished: (NSNotification *) theNotification;
170
171 /*!
172 @method connectionLost:
173 @discussion Invoked when the connection to the peer has been
174 lost without the "user's" intervention.
175 A PantomimeConnectionLost notification is also posted.
176 @param theNotification The notification holding the information.
177 */
178 - (void) connectionLost: (NSNotification *) theNotification;
179
180 /*!
181 @method connectionTerminated:
182 @discussion Invoked when the connection has been cleanly terminated
183 with the peer.
184 A PantomimeConnectionTerminated notification is also posted.
185 @param theNotification The notification holding the information.
186 */
187 - (void) connectionTerminated: (NSNotification *) theNotification;
188
189 /*!
190 @method connectionTimedOut:
191 @discussion Invoked when connecting to a peer on a non-blocking
192 fashion but the associated timeout has expired. It is also
193 invoked when we try to read bytes from the peer or when
194 trying send bytes to it. The method <i>isConnected</i> can
195 be invoked in order if we got this timeout when trying to
196 connect (the value will be set to "NO") or when we are
197 connected but can't read or write anymore.
198 A PantomimeConnectionTimedOut notification is also posted.
199 @param theNotification The notification holding the information.
200 */
201 - (void) connectionTimedOut: (NSNotification *) theNotification;
202
203 /*!
204 @method service: receivedData:
205 @discussion Invoked when bytes have been received by the underlying
206 CWService's connection. No notification is posted.
207 @param theService The CWService instance that generated network activity.
208 @param theData The received bytes.
209 */
210 - (void) service: (CWService *) theService receivedData: (NSData *) theData;
211
212 /*!
213 @method service: sentData:
214 @discussion Invoked when bytes have been sent using the underlying
215 CWService's connection. No notification is posted.
216 @param theService The CWService instance that generated network activity.
217 @param theData The sent bytes.
218 */
219 - (void) service: (CWService *) theService sentData: (NSData *) theData;
220
221 /*!
222 @method requestCancelled:
223 @discussion This method is automatically called after
224 a request has been cancelled. The connection
225 was automatically closed PRIOR to calling this delegate method.
226 A PantomimeRequestCancelled notification is also posted.
227 @param theNotification The notification holding the information.
228 */
229 - (void) requestCancelled: (NSNotification *) theNotification;
230
231 /*!
232 @method serviceInitialized:
233 @discussion This method is automatically invoked on the delegate
234 when the Service is fully initialized. This method
235 is invoked after -connectionEstablished: is called.
236 A PantomimeServiceInitialized notification is also posted.
237 @param theNotification The notification holding the information.
238 */
239 - (void) serviceInitialized: (NSNotification *) theNotification;
240
241 /*!
242 @method serviceReconnected:
243 @discussion When a service lost its connection, -connectionWasLost: is
244 called. Usually, -reconnect is called to re-establish the
245 connection with the remote host. Once it has completed,
246 -serviceReconnected: is invoked on the delegate.
247 A PantomimeServiceReconnected notification is also posted.
248 @param theNotification The notification holding the information.
249 */
250 - (void) serviceReconnected: (NSNotification *) theNotification;
251 @end
252
253
254 #ifdef MACOSX
255 typedef enum {ET_RDESC, ET_WDESC, ET_EDESC} RunLoopEventType;
256
257 /*!
258 @class CWService
259 @discussion This abstract class defines the basic behavior and implementation
260 of all Pantomime internet services such as SMTP, POP3 and IMAP.
261 You should never instantiate this class directly. You rather
262 need to instantiate the CWSMTP, CWPOP3Store or CWIMAPStore classes,
263 which fully implement the abstract methods found in this class.
264 */
265 @interface CWService : NSObject
266 #else
267 @interface CWService : NSObject <RunLoopEvents>
268 #endif
269 {
270 @protected
271 NSMutableArray *_supportedMechanisms;
272 NSMutableArray *_responsesFromServer;
273 NSMutableArray *_capabilities;
274 NSMutableArray *_runLoopModes;
275 NSMutableArray *_queue;
276 NSMutableData *_wbuf;
277 NSMutableData *_rbuf;
278 NSString *_mechanism;
279 NSString *_username;
280 NSString *_password;
281 NSString *_name;
282
283 #ifdef MACOSX
284 CFRunLoopSourceRef _runLoopSource;
285 CFSocketContext *_context;
286 CFSocketRef _socket;
287 #endif
288
289 unsigned int _connectionTimeout;
290 unsigned int _readTimeout;
291 unsigned int _writeTimeout;
292 unsigned int _lastCommand;
293 unsigned int _port;
294 BOOL _connected;
295 id _delegate;
296
297 id<CWConnection> _connection;
298 NSTimer * _timer;
299 int _counter;
300
301 struct {
302 NSMutableArray *previous_queue;
303 BOOL reconnecting;
304 BOOL opening_mailbox;
305 } _connection_state;
306 }
307
308 /*!
309 @method initWithName: port:
310 @discussion This is the designated initializer for the CWService class.
311 Once called, it'll open a connection to the server specified
312 by <i>theName</i> using the specified port (<i>thePort</i>).
313 @param theName The FQDN of the server.
314 @param thePort The server port to which we will connect.
315 @result An instance of a Service class, nil on error.
316 */
317 - (id) initWithName: (NSString *) theName
318 port: (unsigned int) thePort;
319
320 /*!
321 @method setDelegate:
322 @discussion This method is used to set the CWService's delegate.
323 The delegate will not be retained. The CWService class
324 (and its subclasses) will invoke methods on the delegate
325 based on actions performed.
326 @param theDelegate The delegate, which implements various callback methods.
327 */
328 - (void) setDelegate: (id) theDelegate;
329
330 /*!
331 @method delegate
332 @discussion This method is used to get the delegate of the CWService's instance.
333 @result The delegate, nil if none was previously set.
334 */
335 - (id) delegate;
336
337 /*!
338 @method name
339 @discussion This method is used to obtain the server name.
340 @result The server name.
341 */
342 - (NSString *) name;
343
344 /*!
345 @method setName:
346 @discussion This method is used to set the server name to which
347 we will eventually connect to.
348 @param theName The name of the server.
349 */
350 - (void) setName: (NSString *) theName;
351
352 /*!
353 @method port
354 @discussion This method is used to obtain the server port.
355 @result The server port.
356 */
357 - (unsigned int) port;
358
359 /*!
360 @method setPort:
361 @discussion This method is used to set the server port to which
362 we will eventually connect to.
363 @param theName The port of the server.
364 */
365 - (void) setPort: (unsigned int) thePort;
366
367 /*!
368 @method connection
369 @discussion This method is used to retrieve the associated connection
370 object for the service (usually a CWTCPConnection instance).
371 @result The associated connectio object.
372 */
373 - (id<CWConnection>) connection;
374
375 /*!
376 @method username
377 @discussion This method is used to get the username (if any) that will be
378 used to authenticate to the service.
379 @result The username.
380 */
381 - (NSString *) username;
382
383 /*!
384 @method setUsername:
385 @discussion This method is used to set the username that will be used
386 to authenticate to the service.
387 @param theUsername The username for authentication.
388 */
389 - (void) setUsername: (NSString *) theUsername;
390
391 /*!
392 @method supportedMechanisms
393 @discussion This method is used to return the supported SASL
394 authentication mecanisms by the receiver.
395 @result An array of NSString instances which indicates
396 what SASL mechanisms are supported.
397 */
398 - (NSArray *) supportedMechanisms;
399
400 /*!
401 @method isConnected
402 @discussion This method is used to verify if the receiver
403 is connected to the server.
404 @result YES if connected, NO otherwise.
405 */
406 - (BOOL) isConnected;
407
408 /*!
409 @method authenticate: password: mechanism:
410 @discussion This method is used to authentifcate the receiver
411 to the server. This method posts a PantomimeAuthenticationCompleted
412 (or calls the -authenticationCompleted: method on the delegate, if any)
413 if the authentication is sucessful. If not, it posts the
414 PantomimeAuthenticationFailed notification (or calls the
415 -authenticationFailed: method on the delegate, if any). This method
416 is fully asynchronous.
417 @param theUsername The username to use, overwriting -username: if any.
418 @param thePassword The password to use.
419 @param theMechanism The authentication mechanism to use.
420 */
421 - (void) authenticate: (NSString *) theUsername
422 password: (NSString *) thePassword
423 mechanism: (NSString *) theMechanism;
424
425 /*!
426 @method cancelRequest
427 @discussion This method will cancel any pending requests or communications
428 with the server and close the connection. It'll post a
429 PantomimeRequestCancelled once it has fully cancelled everything.
430 This method is fully asynchronous.
431 */
432 - (void) cancelRequest;
433
434 /*!
435 @method close
436 @discussion This method is used to close the connection to the server.
437 If the receiver is not in a connected state, it does nothing.
438 If it is, it posts a PantomimeConnectionTerminated notification
439 once it has completed and invokes -connectionTerminated: on the
440 delegate, if any.
441 */
442 - (void) close;
443
444 /*!
445 @method connect
446 @discussion This method is used to connect the receiver to the server.
447 It will block until the connection was succefully established
448 (or until it fails).
449 @result 0 on success, -1 on error.
450 */
451 - (int) connect;
452
453 /*!
454 @method connectInBackgroundAndNotify
455 @discussion This method is used connect the receiver to the server.
456 The call to this method is non-blocking. This method will
457 post a PantomimeConnectionEstablished notification once
458 the connection has been establish (and call -connectionEstablished:
459 on the delegate, if any). Otherwise, it will post a PantomimeConnectionTimedOut
460 notification (and call -connectionTimedOut: on the delegate, if any).
461 */
462 - (void) connectInBackgroundAndNotify;
463
464 /*!
465 @method noop
466 @discussion This method is used to generate some traffic on a server
467 so the connection doesn't idle and gets terminated by
468 the server. Subclasses of CWService need to implement this method.
469 */
470 - (void) noop;
471
472 /*!
473 @method receivedEvent: type: extra: forMode:
474 @discussion This method is automatically invoked when the receiver can
475 either read or write bytes to its underlying CWConnection
476 instance. Never call this method directly.
477 @param theData The file descriptor.
478 @param theType The type of event that occured.
479 @param theExtra Additional information.
480 @param theMode The runloop modes.
481 */
482 - (void) receivedEvent: (void *) theData
483 type: (RunLoopEventType) theType
484 extra: (void *) theExtra
485 forMode: (NSString *) theMode;
486
487 /*!
488 @method reconnect
489 @discussion Pending.
490 @result Pending.
491 */
492 - (int) reconnect;
493
494 /*!
495 @method updateRead
496 @discussion This method is invoked automatically when bytes are available
497 to be read. You should never have to invoke this method directly.
498 */
499 - (void) updateRead;
500
501 /*!
502 @method updateWrite
503 @discussion This method is invoked automatically when bytes are available
504 to be written. You should never have to invoke this method directly.
505 */
506 - (void) updateWrite;
507
508 /*!
509 @method writeData:
510 @discussion This method is used to buffer bytes to be written on the socket.
511 You should never have to invoke this method directly.
512 @param The bytes to buffer, as a NSData instance.
513 */
514 - (void) writeData: (NSData *) theData;
515
516 /*!
517 @method addRunLoopMode:
518 @discussion This method is used to add an additional mode that the run-loop
519 will use to listen for network events for reading and writing.
520 Note that this method does nothing on OS X since only the
521 kCFRunLoopCommonModes mode is used.
522 @param The additional mode. NSDefaultRunLoopMode is always present so there
523 is no need to add it.
524 */
525 - (void) addRunLoopMode: (NSString *) theMode;
526
527 /*!
528 @method connectionTimeout
529 @discussion This method is used to get the timeout used when
530 connecting to the host.
531 @result The connecton timeout.
532 */
533 - (unsigned int) connectionTimeout;
534
535 /*!
536 @method setConnectionTimeout:
537 @discussion This method is used to set the timeout used when
538 connecting to the host.
539 @param theConnectionTimeout The timeout to use.
540 */
541 - (void) setConnectionTimeout: (unsigned int) theConnectionTimeout;
542
543 /*!
544 @method readTimeout
545 @discussion This method is used to get the timeout used when
546 reading bytes from the socket.
547 @result The read timeout.
548 */
549 - (unsigned int) readTimeout;
550
551 /*!
552 @method setReadTimeout
553 @discussion This method is used to set the timeout used when
554 reading bytes from the socket.
555 @param The timeout to use.
556 */
557 - (void) setReadTimeout: (unsigned int) theReadTimeout;
558
559 /*!
560 @method writeTimeout
561 @discussion This method is used to get the timeout used when
562 writing bytes from the socket.
563 @result The write timeout.
564 */
565 - (unsigned int) writeTimeout;
566
567 /*!
568 @method setWriteTimeout
569 @discussion This method is used to set the timeout used when
570 writing bytes from the socket.
571 @param The timeout to use.
572 */
573 - (void) setWriteTimeout: (unsigned int) theWriteTimeout;
574
575 /*!
576 @method startTLS
577 @discussion This method is used to activate TLS over
578 a non-secure connection. This method can
579 be called in the -serviceInitialized:
580 delegate method. The latter will be invoked
581 again once TLS has been activated successfully.
582 */
583 - (void) startTLS;
584
585 /*!
586 @method lastCommand
587 @discussion This method is used to get the last command that
588 has been sent by CWService subclasses to the
589 remote server. To know which commands can be
590 sent, see the documentation of the associated
591 subclasses.
592 @result The last command sent, 0 otherwise.
593 */
594 - (unsigned int) lastCommand;
595
596
597 /*!
598 @method capabilities
599 @discussion This method is used to obtain the capabilities of the
600 associated service.
601 @result The capabilities, as an array of NSString instances.
602 */
603 - (NSArray *) capabilities;
604
605 @end
606
607 #endif // _Pantomime_H_CWService