Created
February 2, 2011 04:51
-
-
Save timburks/807255 to your computer and use it in GitHub Desktop.
Sending and receiving mail with Pantomime.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/nush | |
# pantomime-pop3.nu | |
(load "Pantomime") | |
(class PantomimePOP3Client is NSObject | |
(ivar (id) pop3 (id) user (BOOL) running) | |
(ivar-accessors) | |
(- (void) run is | |
(set @pop3 ((CWPOP3Store alloc) initWithName:(@user server:) port:(@user port:))) | |
(@pop3 setDelegate:self) | |
(@pop3 connectInBackgroundAndNotify) | |
(set @running YES) | |
(while @running | |
((NSRunLoop currentRunLoop) runUntilDate:(NSDate dateWithTimeIntervalSinceNow:0.01)))) | |
(- (void) authenticationCompleted:(id) theNotification is | |
(puts "Authentication completed! Checking for messages.") | |
((@pop3 defaultFolder) prefetch)) | |
(- (void) authenticationFailed:(id) theNotification is | |
(puts "Authentication failed! Closing the connection.") | |
(@pop3 close) | |
(set @running NO)) | |
(- (void) connectionEstablished:(id) theNotification is | |
((@pop3 connection) startSSL)) | |
(- (void) connectionTerminated:(id) theNotification is | |
(puts "Connection closed.") | |
(set @running NO)) | |
(- (void) folderPrefetchCompleted:(id) theNotification is | |
(set count ((@pop3 defaultFolder) count)) | |
(puts "There are #{count} messages on the server.") | |
(if (> count 0) | |
(then (puts "Prefetching and initializing.") | |
(((@pop3 defaultFolder) allMessages) each: | |
(do (message) | |
(message setInitialized:YES)))) | |
(else (puts "Closing the connection.") | |
(@pop3 close) | |
(set @running NO)))) | |
(- (void) messagePrefetchCompleted:(id) theNotification is | |
(set message ((theNotification userInfo) Message:)) | |
(puts "Got the message: #{(message subject)}") | |
(puts (NSString stringWithData:(message rawSource) encoding:NSUTF8StringEncoding)) | |
(@pop3 close)) | |
(- (void) serviceInitialized:(id) theNotification is | |
;(puts "SSL Handshake complete") | |
;(puts ((@pop3 supportedMechanisms) description)) | |
(@pop3 authenticate:(@user username:) password:(@user password:) mechanism:"none"))) | |
(set user (dict username:(((NSProcessInfo processInfo) environment) objectForKey:"BOT_MAIL_USERNAME") | |
password:(((NSProcessInfo processInfo) environment) objectForKey:"BOT_MAIL_PASSWORD") | |
server:"pop.gmail.com" | |
port:995)) | |
(set client (PantomimePOP3Client new)) | |
(client setUser:user) | |
(client run) | |
(puts "done") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/nush | |
# pantomime-smtp.nu | |
(load "Pantomime") | |
(class PantomimeSMTPClient is NSObject | |
(ivar (id) smtp (id) user (BOOL) running) | |
(ivar-accessors) | |
(- (void) sendMessage:(id) message is | |
(set cwmessage ((CWMessage alloc) init)) | |
(cwmessage setSubject:(message subject:)) | |
(cwmessage setFrom:((CWInternetAddress alloc) initWithString:(@user username:))) | |
((message recipients:) each: | |
(do (recipient) | |
(set address ((CWInternetAddress alloc) initWithString:recipient)) | |
(address setType:1) | |
(cwmessage addRecipient:address))) | |
(cwmessage setContentType:"text/plain") | |
(cwmessage setContent:((message text:) dataUsingEncoding:NSUTF8StringEncoding)) | |
(set @smtp ((CWSMTP alloc) initWithName:(@user server:) port:(@user port:))) | |
(@smtp setDelegate:self) | |
(@smtp setMessage:cwmessage) | |
(set @running YES) | |
(@smtp connectInBackgroundAndNotify)) | |
(- (void) authenticationCompleted:(id) notification is | |
(@smtp sendMessage)) | |
(- (void) authenticationFailed:(id) notification is | |
(puts "authentication failed") | |
(@smtp close)) | |
(- (void) connectionEstablished:(id) notification is | |
((@smtp connection) startSSL)) | |
(- (void) connectionTerminated:(id) notification is | |
(set @running NO)) | |
(- (void) messageSent:(id) notificaiton is | |
(puts "message sent") | |
(set @running NO)) | |
(- (void) serviceInitialized:(id) notification is | |
;(puts ((@smtp supportedMechanisms) description)) | |
(@smtp authenticate:(@user username:) password:(@user password:) mechanism:"LOGIN")) | |
(- (void) synchronouslySendMessage:(id) message is | |
(self sendMessage:message) | |
(while @running | |
((NSRunLoop currentRunLoop) runUntilDate:(NSDate dateWithTimeIntervalSinceNow:0.01))))) | |
(set user (dict username:(((NSProcessInfo processInfo) environment) objectForKey:"BOT_MAIL_USERNAME") | |
password:(((NSProcessInfo processInfo) environment) objectForKey:"BOT_MAIL_PASSWORD") | |
server:"smtp.gmail.com" | |
port:465)) | |
(set client (PantomimeSMTPClient new)) | |
(client setUser:user) | |
(10 times: | |
(do (i) | |
(puts i) | |
(set message (dict recipients:(array (user username:)) | |
subject:"Test Message #{i}" | |
text:"Hello #{i}")) | |
(client synchronouslySendMessage:message))) | |
(puts "done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment