File: SimplePOP3.m

package info (click to toggle)
pantomime 1.3.0%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 2,332 kB
  • sloc: objc: 22,026; makefile: 11; sh: 4
file content (199 lines) | stat: -rw-r--r-- 4,809 bytes parent folder | download | duplicates (4)
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
//
// This code is public domain. Do whatever you want with it.
//
// This test application shows how to check for new mail on
// a POP3 server. If there are any messages on the server,
// it downloads the first ones and dumps it on stdout.
//
// *** PLEASE READ Pantomime/Documentation/README ***
//
// Author: Ludovic Marcotte <ludovic@Sophos.ca>
//

#import <AppKit/AppKit.h>


//
// You can safely either #include or #import Pantomime headers.
// Include/import Pantomime.h to have access to every class
// or include/import them individually.
//
#import <Pantomime/Pantomime.h>

//
// Modify those defines to reflect your environment.
//

#define SERVER_NAME  @"pop.gmail.com"
#define SERVER_PORT  995
#define USE_SSL      NO
#define USERNAME     @"german@xelalug.org"
#define PASSWORD     @"cranfork725"
#define MECHANISM    @"none"  // use "none" for normal POP3 authentication


//
// Our class interface.
//
@interface SimplePOP3 : NSObject
{
  @private
    CWPOP3Store *_pop3;
}
@end


//
// Our class implementation.
//
@implementation SimplePOP3

- (void) applicationDidFinishLaunching: (NSNotification *) theNotification
{
  // We initialize our POP3Store instance
  _pop3 = [[CWPOP3Store alloc] initWithName: SERVER_NAME  port: SERVER_PORT];
  [_pop3 setDelegate: self];

  // We connect to the server _in background_. That means, this call
  // is non-blocking and methods will be invoked on the delegate
  // (or notifications will be posted) for further dialog with
  // the remote POP3server.
  NSLog(@"Connecting to the %@ server...", SERVER_NAME);
  [_pop3 connectInBackgroundAndNotify];
}


//
// This method is automatically called once the POP3 authentication
// has completed. If it has failed, -authenticationFailed: will
// be invoked.
//
- (void) authenticationCompleted: (NSNotification *) theNotification
{
  NSLog(@"Authentication completed! Checking for messages..");
  [[_pop3 defaultFolder] prefetch];
}


//
// This method is automatically called once the POP3 authentication
// has failed. If it has succeeded, -authenticationCompleted: will
// be invoked.
//
- (void) authenticationFailed: (NSNotification *) theNotification
{
  NSLog(@"Authentication failed! Closing the connection...");
  [_pop3 close];
}


//
// This method is automatically called when the connection to
// the POP3 server was established.
//
- (void) connectionEstablished: (NSNotification *) theNotification
{
  NSLog(@"Connected!");

  if (USE_SSL)
    {
      NSLog(@"Now starting SSL...");
      [(CWTCPConnection *)[_pop3 connection] startSSL];
    }
}


//
// This method is automatically called when the connection to
// the POP3 server was terminated avec invoking -close on the
// POP3Store instance.
//
- (void) connectionTerminated: (NSNotification *) theNotification
{
  NSLog(@"Connection closed.");
  RELEASE(_pop3);
  [NSApp terminate: self];
}


//
// This method is automatically invoked when the folder information
// was fully prefetched from the POP3 server. Once it has been
// prefetched, one can prefetch specific messages.
//
- (void) folderPrefetchCompleted: (NSNotification *) theNotification
{
  int count;

  count = [(CWPOP3Folder *)[_pop3 defaultFolder] count];

  NSLog(@"There are %d messages on the server.", count);

  if (count > 0)
    {
      NSLog(@"Prefetching and initializing the first one...");
      [[[[_pop3 defaultFolder] allMessages] objectAtIndex: 0] setInitialized: YES];
    }
  else
    {
      NSLog(@"Closing the connection...");
      [_pop3 close];
    }
}


//
// This method is automatically invoked when a message was 
// fully prefetched from the POP3 server.
//
- (void) messagePrefetchCompleted: (NSNotification *) theNotification
{
  CWMessage *aMessage;

  aMessage = [[theNotification userInfo] objectForKey: @"Message"];

  NSLog(@"Got the message! The subject is: %@", [aMessage subject]);
  NSLog(@"The full content is:\n\n------------------------\n%s------------------------", [[aMessage rawSource] cString]);

  NSLog(@"Closing the connection...");
  [_pop3 close];
}


//
// This method is automatically invoked once the POP3Store service
// is fully initialized.
//
- (void) serviceInitialized: (NSNotification *) theNotification
{
  if (USE_SSL)
    {
      NSLog(@"SSL handshaking completed.");
    }

  NSLog(@"Available authentication mechanisms: %@", [_pop3 supportedMechanisms]);
  [_pop3 authenticate: USERNAME  password: PASSWORD  mechanism: MECHANISM];
}

@end


//
// Main entry point for the test application.
//
int main(int argc, const char *argv[], char *env[])
{
  NSAutoreleasePool *pool;
  SimplePOP3 *o;
  
  pool = [[NSAutoreleasePool alloc] init];
  o = [[SimplePOP3 alloc] init];
  
  [NSApplication sharedApplication];
  [NSApp setDelegate: o];
  [NSApp run];
  RELEASE(o);
  RELEASE(pool);
  
  return 0;
}