Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support basic IMAP/IMAPS retrieval of all and unread messages

Yikes IMAP w/ Gmail is slow...
  • Loading branch information...
commit 9308a3fd6c2f0fe9715601ce88f1e04e7dc2ad56 1 parent 8fe1567
@carsongross authored
View
11 scratch.gsp
@@ -11,6 +11,17 @@ Server.Base = new Server() {
:Password = ""
}
+print( "all --------" )
+
+Server.Base.folder( "Scala" ).AllMessages.each( \ e -> print( e.From ) )
+
+print( "unread --------" )
+
+Server.Base.folder( "Scala" ).UnreadMessages.each( \ e -> print( e.From ) )
+
+
+if(true) { return }
+
// The given block will be fired when new messages arrive in the Inbox
// API demo only, doesn't currently work!
Server.Base.Inbox.follow( \ e ->{
View
14 src/romail/Email.gs
@@ -8,6 +8,7 @@ uses java.io.File
uses java.util.ArrayList
uses java.util.List
uses java.lang.StringBuilder
+uses javax.mail.Message
class Email {
@@ -19,7 +20,18 @@ class Email {
var _subject : String as Subject = ""
var _text : String as Text = ""
var _html : String as HTML = ""
- var _server : Server as Server = Server.Base
+ var _server : Server as Server = Server.Base
+
+ construct(){}
+
+ internal construct(m : Message) {
+ _from = m.From?.first() as InternetAddress
+ _to = m.getRecipients( Message.RecipientType.TO )?.whereTypeIs( InternetAddress )?.toList()
+ _cc = m.getRecipients( Message.RecipientType.CC )?.whereTypeIs( InternetAddress )?.toList()
+ _bcc = m.getRecipients( Message.RecipientType.BCC )?.whereTypeIs( InternetAddress )?.toList()
+ Subject = m.Subject
+ Text = m.Content?.toString() // obviously wrong!
+ }
property set From(s : String) {
_from = parseEmail(s)
View
14 src/romail/EmailFolder.gs
@@ -3,13 +3,25 @@ package romail
uses java.util.List
class EmailFolder {
+
+ var _server : Server
+ var _name : String as Name
+
+ construct( server : Server, folderName : String ) {
+ _server = server
+ _name = folderName
+ }
function follow(callback : block(Email)) {
}
property get AllMessages() : List<Email> {
- return null
+ return _server.getAllMessages( this )
+ }
+
+ property get UnreadMessages() : List<Email> {
+ return _server.getUnreadMessages( this )
}
function deleteAllMessages() {
View
43 src/romail/Server.gs
@@ -11,6 +11,10 @@ uses java.util.Properties
uses java.lang.IllegalStateException
uses java.lang.ThreadLocal
uses gw.util.Stack
+uses javax.mail.Folder
+uses javax.mail.FolderClosedException
+uses javax.mail.search.FlagTerm
+uses javax.mail.Flags
public class Server implements IReentrant {
@@ -95,7 +99,7 @@ public class Server implements IReentrant {
}
function folder(name : String) : EmailFolder {
- return null
+ return new EmailFolder(this, name)
}
property get Inbox() : EmailFolder {
@@ -110,6 +114,41 @@ public class Server implements IReentrant {
getStack().pop()
}
+ internal function getAllMessages( emailFolder : EmailFolder ) : List<Email> {
+ var session = getSession()
+ // Get the store
+ using(var store = session.getStore(_fetchProtocol.Val)) {
+ store.connect(FetchServer, UserName, Password)
+ // Get folder
+ var f = store.getFolder(emailFolder.Name)
+ f.open(Folder.READ_ONLY)
+ try {
+ return f.Messages.toList().map( \ m -> new Email(m) )
+ } finally {
+ // Close folder
+ f.close(false)
+ }
+ }
+ }
+
+ internal function getUnreadMessages( emailFolder : EmailFolder ) : List<Email> {
+ var session = getSession()
+ // Get the store
+ using(var store = session.getStore(_fetchProtocol.Val)) {
+ store.connect(FetchServer, UserName, Password)
+ // Get folder
+ var f = store.getFolder(emailFolder.Name)
+ f.open(Folder.READ_ONLY)
+ try {
+ var ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false)
+ return f.search( ft ).toList().map( \ m -> new Email(m) )
+ } finally {
+ // Close folder
+ f.close(false)
+ }
+ }
+ }
+
function connect() : IReentrant {
return new IReentrant() {
override function enter() {
@@ -141,7 +180,7 @@ public class Server implements IReentrant {
public enum FetchProtocol {
POP("pop3"),
IMAP("imap"),
- IMAPS("imap")
+ IMAPS("imaps")
var _code : String as readonly Val
private construct( c : String) {
_code = c
Please sign in to comment.
Something went wrong with that request. Please try again.