1//
2// This code is public domain. Do whatever you want with it.
3//
4// This test application shows how to check for new mail on
5// a POP3 server. If there are any messages on the server,
6// it downloads the first ones and dumps it on stdout.
7//
8// *** PLEASE READ Pantomime/Documentation/README ***
9//
10// Author: Ludovic Marcotte <ludovic@Sophos.ca>
11//
12
13#import <AppKit/AppKit.h>
14
15
16//
17// You can safely either #include or #import Pantomime headers.
18// Include/import Pantomime.h to have access to every class
19// or include/import them individually.
20//
21#import <Pantomime/Pantomime.h>
22
23//
24// Modify those defines to reflect your environment.
25//
26
27#define SERVER_NAME  @"pop.gmail.com"
28#define SERVER_PORT  995
29#define USE_SSL      NO
30#define USERNAME     @"german@xelalug.org"
31#define PASSWORD     @"cranfork725"
32#define MECHANISM    @"none"  // use "none" for normal POP3 authentication
33
34
35//
36// Our class interface.
37//
38@interface SimplePOP3 : NSObject
39{
40  @private
41    CWPOP3Store *_pop3;
42}
43@end
44
45
46//
47// Our class implementation.
48//
49@implementation SimplePOP3
50
51- (void) applicationDidFinishLaunching: (NSNotification *) theNotification
52{
53  // We initialize our POP3Store instance
54  _pop3 = [[CWPOP3Store alloc] initWithName: SERVER_NAME  port: SERVER_PORT];
55  [_pop3 setDelegate: self];
56
57  // We connect to the server _in background_. That means, this call
58  // is non-blocking and methods will be invoked on the delegate
59  // (or notifications will be posted) for further dialog with
60  // the remote POP3server.
61  NSLog(@"Connecting to the %@ server...", SERVER_NAME);
62  [_pop3 connectInBackgroundAndNotify];
63}
64
65
66//
67// This method is automatically called once the POP3 authentication
68// has completed. If it has failed, -authenticationFailed: will
69// be invoked.
70//
71- (void) authenticationCompleted: (NSNotification *) theNotification
72{
73  NSLog(@"Authentication completed! Checking for messages..");
74  [[_pop3 defaultFolder] prefetch];
75}
76
77
78//
79// This method is automatically called once the POP3 authentication
80// has failed. If it has succeeded, -authenticationCompleted: will
81// be invoked.
82//
83- (void) authenticationFailed: (NSNotification *) theNotification
84{
85  NSLog(@"Authentication failed! Closing the connection...");
86  [_pop3 close];
87}
88
89
90//
91// This method is automatically called when the connection to
92// the POP3 server was established.
93//
94- (void) connectionEstablished: (NSNotification *) theNotification
95{
96  NSLog(@"Connected!");
97
98  if (USE_SSL)
99    {
100      NSLog(@"Now starting SSL...");
101      [(CWTCPConnection *)[_pop3 connection] startSSL];
102    }
103}
104
105
106//
107// This method is automatically called when the connection to
108// the POP3 server was terminated avec invoking -close on the
109// POP3Store instance.
110//
111- (void) connectionTerminated: (NSNotification *) theNotification
112{
113  NSLog(@"Connection closed.");
114  RELEASE(_pop3);
115  [NSApp terminate: self];
116}
117
118
119//
120// This method is automatically invoked when the folder information
121// was fully prefetched from the POP3 server. Once it has been
122// prefetched, one can prefetch specific messages.
123//
124- (void) folderPrefetchCompleted: (NSNotification *) theNotification
125{
126  int count;
127
128  count = [(CWPOP3Folder *)[_pop3 defaultFolder] count];
129
130  NSLog(@"There are %d messages on the server.", count);
131
132  if (count > 0)
133    {
134      NSLog(@"Prefetching and initializing the first one...");
135      [[[[_pop3 defaultFolder] allMessages] objectAtIndex: 0] setInitialized: YES];
136    }
137  else
138    {
139      NSLog(@"Closing the connection...");
140      [_pop3 close];
141    }
142}
143
144
145//
146// This method is automatically invoked when a message was
147// fully prefetched from the POP3 server.
148//
149- (void) messagePrefetchCompleted: (NSNotification *) theNotification
150{
151  CWMessage *aMessage;
152
153  aMessage = [[theNotification userInfo] objectForKey: @"Message"];
154
155  NSLog(@"Got the message! The subject is: %@", [aMessage subject]);
156  NSLog(@"The full content is:\n\n------------------------\n%s------------------------", [[aMessage rawSource] cString]);
157
158  NSLog(@"Closing the connection...");
159  [_pop3 close];
160}
161
162
163//
164// This method is automatically invoked once the POP3Store service
165// is fully initialized.
166//
167- (void) serviceInitialized: (NSNotification *) theNotification
168{
169  if (USE_SSL)
170    {
171      NSLog(@"SSL handshaking completed.");
172    }
173
174  NSLog(@"Available authentication mechanisms: %@", [_pop3 supportedMechanisms]);
175  [_pop3 authenticate: USERNAME  password: PASSWORD  mechanism: MECHANISM];
176}
177
178@end
179
180
181//
182// Main entry point for the test application.
183//
184int main(int argc, const char *argv[], char *env[])
185{
186  NSAutoreleasePool *pool;
187  SimplePOP3 *o;
188
189  pool = [[NSAutoreleasePool alloc] init];
190  o = [[SimplePOP3 alloc] init];
191
192  [NSApplication sharedApplication];
193  [NSApp setDelegate: o];
194  [NSApp run];
195  RELEASE(o);
196  RELEASE(pool);
197
198  return 0;
199}
200