diff --git a/components/net/sf/briar/plugins/email/GmailPlugin.java b/components/net/sf/briar/plugins/email/GmailPlugin.java index 311273e6727f48a5f70e1754264e00889c867aeb..4f8cd5df49647d72c2238f5b7240f1dcd7d9f60b 100644 --- a/components/net/sf/briar/plugins/email/GmailPlugin.java +++ b/components/net/sf/briar/plugins/email/GmailPlugin.java @@ -36,49 +36,44 @@ import net.sf.briar.util.StringUtils; public class GmailPlugin implements SimplexPlugin { - public static final byte[] TRANSPORT_ID = - StringUtils.fromHexString("57ead1961d2120bbbbe8256ff9ce6ae2" + public static final byte[] TRANSPORT_ID = StringUtils + .fromHexString("57ead1961d2120bbbbe8256ff9ce6ae2" + "ef5535e44330c04cedcbafac4d756f0c" + "e8dd928ed1d7a9e7b89fd62210aa30bf"); - + private static final TransportId ID = new TransportId(TRANSPORT_ID); private final Executor pluginExecutor; private final SimplexPluginCallback callback; -// private static GmailTransportConnectionReader reader; -// private static GmailTransportConnectionWriter writer; - - + // private static GmailTransportConnectionReader reader; + // private static GmailTransportConnectionWriter writer; + public GmailPlugin(Executor pluginExecutor, SimplexPluginCallback callback) { this.pluginExecutor = pluginExecutor; this.callback = callback; } - - + public TransportId getId() { return ID; } public void start() throws IOException { - pluginExecutor.execute(new Runnable() { public void run() { connectIMAP(); - } + } }); } protected void checkUnreadEmails(Folder inbox) { - try { - FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false); - Message msg[] = inbox.search(ft); -// System.out.println("Unread Messages: "+ msg.length); - - for(final Message message:msg) { - try { - callback.readerCreated(new SimplexTransportReader() { - - public InputStream getInputStream() throws IOException { + FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false); + Message msg[] = inbox.search(ft); + // System.out.println("Unread Messages: "+ msg.length); + for (final Message message : msg) { + try { + callback.readerCreated(new SimplexTransportReader() { + + public InputStream getInputStream() throws IOException { try { return message.getInputStream(); } catch (MessagingException e) { @@ -95,125 +90,117 @@ public class GmailPlugin implements SimplexPlugin { } catch (MessagingException e) { e.printStackTrace(); } - }}); - - System.out.println("DATE: " + message.getSentDate().toString()); - System.out.println("FROM: " + message.getFrom()[0].toString()); - System.out.println("SUBJECT: " + message.getSubject().toString()); - System.out.println("CONTENT: " + message.getContent().toString()); - System.out.println("================================================="); - - - } catch (Exception e) { - System.out.println("No Information"); - } - } - + } + }); + + // This part for testing purposes only + System.out.println("DATE: " + + message.getSentDate().toString()); + System.out.println("FROM: " + + message.getFrom()[0].toString()); + System.out.println("SUBJECT: " + + message.getSubject().toString()); + System.out.println("CONTENT: " + + message.getContent().toString()); + System.out + .println("================================================="); + + } catch (Exception e) { + System.out.println("No Information"); + } + } + } catch (MessagingException e) { - System.out.println(e.toString()); - } - } - + System.out.println(e.toString()); + } + } - protected void connectIMAP() { + protected void connectIMAP() { Properties props = System.getProperties(); - props.setProperty("mail.store.protocol", "imaps"); - final ArrayList<String> userPass = getAuthenticationDetails(callback.getConfig()); - if (userPass != null) - { - try { - - Session session = Session.getDefaultInstance(props, null); - Store store = session.getStore("imaps"); - - synchronized(this) - { - store.connect("imap.gmail.com", userPass.get(0), userPass.get(1)); - } - - Folder inbox = store.getFolder("Inbox"); - inbox.open(Folder.READ_ONLY); - - checkUnreadEmails(inbox); - - } - catch (NoSuchProviderException e) { - System.out.println(e.toString()); - System.exit(1); - } catch (MessagingException e) { - System.out.println(e.toString()); - System.exit(2); - } - } + props.setProperty("mail.store.protocol", "imaps"); + final ArrayList<String> userPass = getAuthenticationDetails(callback + .getConfig()); + if (userPass != null) { + try { + Session session = Session.getDefaultInstance(props, null); + Store store = session.getStore("imaps"); + synchronized (this) { + store.connect("imap.gmail.com", userPass.get(0), + userPass.get(1)); + } + + Folder inbox = store.getFolder("Inbox"); + inbox.open(Folder.READ_ONLY); + checkUnreadEmails(inbox); + } catch (NoSuchProviderException e) { + System.out.println(e.toString()); + System.exit(1); + } catch (MessagingException e) { + System.out.println(e.toString()); + System.exit(2); + } + } } - protected boolean connectSMTP(ContactId cid) - { + protected boolean connectSMTP(ContactId cid) { boolean sent = false; - if(discoverContactEmail(cid) != null) - { + if (discoverContactEmail(cid) != null) { Properties prop = new Properties(); prop.put("mail.smtp.host", "smtp.gmail.com"); prop.put("mail.smtp.socketFactory.port", "465"); - prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); + prop.put("mail.smtp.socketFactory.class", + "javax.net.ssl.SSLSocketFactory"); prop.put("mail.smtp.auth", "true"); prop.put("mail.smtp.port", "465"); - - final ArrayList<String> userPass = getAuthenticationDetails(callback.getConfig()); - - if (userPass != null) - { - Session session; - - synchronized(this) - { - session = Session.getDefaultInstance(prop, new javax.mail.Authenticator() { - protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication(userPass.get(0),userPass.get(1)); - } - }); - } - -// SimplexTransportWriter writer = createWriter(cid); - - sent = sendMessage(session,cid); + + final ArrayList<String> userPass = getAuthenticationDetails(callback + .getConfig()); + + if (userPass != null) { + Session session; + synchronized (this) { + session = Session.getInstance(prop, + new javax.mail.Authenticator() { + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(userPass + .get(0), userPass.get(1)); + } + }); + } + // SimplexTransportWriter writer = createWriter(cid); + sent = sendMessage(session, cid); } - } + } return sent; } - - private synchronized boolean sendMessage(Session session, ContactId cid) - { + private synchronized boolean sendMessage(Session session, ContactId cid) { boolean sent = false; - try { Message message = new MimeMessage(session); TransportProperties props = callback.getLocalProperties(); String userEmail = props.get("email"); - + message.setFrom(new InternetAddress(userEmail)); - message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(discoverContactEmail(cid))); - + message.setRecipients(Message.RecipientType.TO, + InternetAddress.parse(discoverContactEmail(cid))); message.setSubject("Test Subject"); - message.setText("Test content"); - + message.setText("Test content"); + Transport.send(message); - + sent = true; return sent; - } catch (MessagingException e) { return sent; } - - } + } public void stop() throws IOException { - synchronized(this) { - //close open connections - } + synchronized (this) { + // close open connections + } } public boolean shouldPoll() { @@ -224,8 +211,8 @@ public class GmailPlugin implements SimplexPlugin { return 0; } - public void poll(Collection<ContactId> connected) throws UnsupportedOperationException{ - + public void poll(Collection<ContactId> connected) + throws UnsupportedOperationException { } public boolean supportsInvitations() { @@ -233,73 +220,67 @@ public class GmailPlugin implements SimplexPlugin { } /* - * Gets the user's authentication details - * ArrayList.get(0) = username, ArrayList.get(1) = password, - * or null if either value is null. + * Gets the user's authentication details ArrayList.get(0) = username, + * ArrayList.get(1) = password, or null if either value is null. */ - private ArrayList<String> getAuthenticationDetails(TransportConfig config) - { + private ArrayList<String> getAuthenticationDetails(TransportConfig config) { try { ArrayList<String> usernamePass = new ArrayList<String>(); - usernamePass.add(0, config.get("username")); + usernamePass.add(0, config.get("username")); usernamePass.add(1, config.get("password")); - - if(usernamePass.get(0)!=null && usernamePass.get(1)!=null) + if (usernamePass.get(0) != null && usernamePass.get(1) != null) return usernamePass; else - return null; - } catch(Exception e) { + return null; + } catch (Exception e) { return null; } - } - + /* * looks up the contact's email address given the contactID + * * @param ContactId + * * @return String email */ - private String discoverContactEmail(ContactId cid) - { + private String discoverContactEmail(ContactId cid) { try { - Map<ContactId, TransportProperties> remote = callback.getRemoteProperties(); - TransportProperties tp = remote.get(cid); + Map<ContactId, TransportProperties> remote = callback + .getRemoteProperties(); + TransportProperties tp = remote.get(cid); String address = tp.get("email"); return address; - } catch(Exception e) { + } catch (Exception e) { return null; } } - + public SimplexTransportReader createReader(ContactId c) { - return null; } public SimplexTransportWriter createWriter(ContactId c) { - return null; } - public SimplexTransportWriter sendInvitation(PseudoRandom r, long timeout) throws UnsupportedOperationException { - + public SimplexTransportWriter sendInvitation(PseudoRandom r, long timeout) + throws UnsupportedOperationException { return null; } - public SimplexTransportReader acceptInvitation(PseudoRandom r, long timeout) throws UnsupportedOperationException { - + public SimplexTransportReader acceptInvitation(PseudoRandom r, long timeout) + throws UnsupportedOperationException { return null; } public SimplexTransportWriter sendInvitationResponse(PseudoRandom r, - long timeout) throws UnsupportedOperationException{ - + long timeout) throws UnsupportedOperationException { return null; } public SimplexTransportReader acceptInvitationResponse(PseudoRandom r, - long timeout) throws UnsupportedOperationException{ - + long timeout) throws UnsupportedOperationException { return null; } diff --git a/components/net/sf/briar/plugins/email/GmailTransportConnectionReader.java b/components/net/sf/briar/plugins/email/GmailTransportConnectionReader.java index 6689e8d218722fbd5dba613ad7edd533c8c521a7..954678525f240bf23139346a2234e9783f9d133e 100644 --- a/components/net/sf/briar/plugins/email/GmailTransportConnectionReader.java +++ b/components/net/sf/briar/plugins/email/GmailTransportConnectionReader.java @@ -12,7 +12,6 @@ import net.sf.briar.api.plugins.simplex.SimplexTransportReader; public class GmailTransportConnectionReader implements SimplexTransportReader{ private static final Logger LOG = Logger.getLogger(GmailTransportConnectionReader.class.getName()); - private final StreamConnection stream; GmailTransportConnectionReader(StreamConnection stream) { @@ -20,7 +19,6 @@ public class GmailTransportConnectionReader implements SimplexTransportReader{ } public InputStream getInputStream() throws IOException { - return stream.openInputStream(); } diff --git a/components/net/sf/briar/plugins/email/GmailTransportConnectionWriter.java b/components/net/sf/briar/plugins/email/GmailTransportConnectionWriter.java index 2fa6bf430fecab39719015f060e5aa129b7b0418..a215bd550edacec4acbf6d7c5b6cf956bf077c41 100644 --- a/components/net/sf/briar/plugins/email/GmailTransportConnectionWriter.java +++ b/components/net/sf/briar/plugins/email/GmailTransportConnectionWriter.java @@ -12,7 +12,6 @@ import net.sf.briar.api.plugins.simplex.SimplexTransportWriter; public class GmailTransportConnectionWriter implements SimplexTransportWriter { private static final Logger LOG = Logger.getLogger(GmailTransportConnectionWriter.class.getName()); - private final StreamConnection stream; private final long capacity = 25 * 1000 * 1000; diff --git a/test/net/sf/briar/plugins/email/GmailPluginTester.java b/test/net/sf/briar/plugins/email/GmailPluginTester.java index b11e9d200aeef73092620186f8ed7ee2c6b3bbf4..a2952966a1a4fa62883b0a4571a0e820cd5ea0e0 100644 --- a/test/net/sf/briar/plugins/email/GmailPluginTester.java +++ b/test/net/sf/briar/plugins/email/GmailPluginTester.java @@ -37,7 +37,6 @@ Map<ContactId,TransportProperties> map = new HashMap<ContactId, TransportPropert @Before public void setup() { - local = new TransportProperties(); local.put("email",System.getenv("USER_GMAIL_ADDRESS")); @@ -48,7 +47,6 @@ Map<ContactId,TransportProperties> map = new HashMap<ContactId, TransportPropert props1 = new TransportProperties(); props1.put("email", System.getenv("CONTACT1_EMAIL")); test1 = new ContactId(12); - map.put(test1, props1); assertEquals(1,map.size()); @@ -67,14 +65,13 @@ Map<ContactId,TransportProperties> map = new HashMap<ContactId, TransportPropert } public void setLocalProperties(TransportProperties p) { - + local = p; } public void setConfig(TransportConfig c) { config = c; } - - + public Map<ContactId, TransportProperties> getRemoteProperties() { return map; } @@ -87,19 +84,13 @@ Map<ContactId,TransportProperties> map = new HashMap<ContactId, TransportPropert return config; } - public void writerCreated(ContactId c, SimplexTransportWriter w) { - - } + public void writerCreated(ContactId c, SimplexTransportWriter w) {} - public void readerCreated(SimplexTransportReader r) { - - } + public void readerCreated(SimplexTransportReader r) {} }; callback.setLocalProperties(local); callback.setConfig(config); - - } @Test @@ -107,7 +98,6 @@ Map<ContactId,TransportProperties> map = new HashMap<ContactId, TransportPropert { GmailPluginFactory plugin = new GmailPluginFactory(); plugin.createPlugin(Executors.newSingleThreadExecutor(), callback); - } @Test