mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-22 15:49:53 +01:00
Fixed bug "Access to default session denied" and reformatted code
This commit is contained in:
@@ -36,49 +36,44 @@ import net.sf.briar.util.StringUtils;
|
|||||||
|
|
||||||
public class GmailPlugin implements SimplexPlugin {
|
public class GmailPlugin implements SimplexPlugin {
|
||||||
|
|
||||||
public static final byte[] TRANSPORT_ID =
|
public static final byte[] TRANSPORT_ID = StringUtils
|
||||||
StringUtils.fromHexString("57ead1961d2120bbbbe8256ff9ce6ae2"
|
.fromHexString("57ead1961d2120bbbbe8256ff9ce6ae2"
|
||||||
+ "ef5535e44330c04cedcbafac4d756f0c"
|
+ "ef5535e44330c04cedcbafac4d756f0c"
|
||||||
+ "e8dd928ed1d7a9e7b89fd62210aa30bf");
|
+ "e8dd928ed1d7a9e7b89fd62210aa30bf");
|
||||||
|
|
||||||
private static final TransportId ID = new TransportId(TRANSPORT_ID);
|
private static final TransportId ID = new TransportId(TRANSPORT_ID);
|
||||||
private final Executor pluginExecutor;
|
private final Executor pluginExecutor;
|
||||||
private final SimplexPluginCallback callback;
|
private final SimplexPluginCallback callback;
|
||||||
// private static GmailTransportConnectionReader reader;
|
// private static GmailTransportConnectionReader reader;
|
||||||
// private static GmailTransportConnectionWriter writer;
|
// private static GmailTransportConnectionWriter writer;
|
||||||
|
|
||||||
|
|
||||||
public GmailPlugin(Executor pluginExecutor, SimplexPluginCallback callback) {
|
public GmailPlugin(Executor pluginExecutor, SimplexPluginCallback callback) {
|
||||||
this.pluginExecutor = pluginExecutor;
|
this.pluginExecutor = pluginExecutor;
|
||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public TransportId getId() {
|
public TransportId getId() {
|
||||||
return ID;
|
return ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() throws IOException {
|
public void start() throws IOException {
|
||||||
|
|
||||||
pluginExecutor.execute(new Runnable() {
|
pluginExecutor.execute(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
connectIMAP();
|
connectIMAP();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void checkUnreadEmails(Folder inbox) {
|
protected void checkUnreadEmails(Folder inbox) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
|
FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
|
||||||
Message msg[] = inbox.search(ft);
|
Message msg[] = inbox.search(ft);
|
||||||
// System.out.println("Unread Messages: "+ msg.length);
|
// System.out.println("Unread Messages: "+ msg.length);
|
||||||
|
for (final Message message : msg) {
|
||||||
|
try {
|
||||||
|
callback.readerCreated(new SimplexTransportReader() {
|
||||||
|
|
||||||
for(final Message message:msg) {
|
public InputStream getInputStream() throws IOException {
|
||||||
try {
|
|
||||||
callback.readerCreated(new SimplexTransportReader() {
|
|
||||||
|
|
||||||
public InputStream getInputStream() throws IOException {
|
|
||||||
try {
|
try {
|
||||||
return message.getInputStream();
|
return message.getInputStream();
|
||||||
} catch (MessagingException e) {
|
} catch (MessagingException e) {
|
||||||
@@ -95,106 +90,100 @@ public class GmailPlugin implements SimplexPlugin {
|
|||||||
} catch (MessagingException e) {
|
} catch (MessagingException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}});
|
}
|
||||||
|
});
|
||||||
|
|
||||||
System.out.println("DATE: " + message.getSentDate().toString());
|
// This part for testing purposes only
|
||||||
System.out.println("FROM: " + message.getFrom()[0].toString());
|
System.out.println("DATE: "
|
||||||
System.out.println("SUBJECT: " + message.getSubject().toString());
|
+ message.getSentDate().toString());
|
||||||
System.out.println("CONTENT: " + message.getContent().toString());
|
System.out.println("FROM: "
|
||||||
System.out.println("=================================================");
|
+ message.getFrom()[0].toString());
|
||||||
|
System.out.println("SUBJECT: "
|
||||||
|
+ message.getSubject().toString());
|
||||||
|
System.out.println("CONTENT: "
|
||||||
|
+ message.getContent().toString());
|
||||||
|
System.out
|
||||||
|
.println("=================================================");
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
} catch (Exception e) {
|
System.out.println("No Information");
|
||||||
System.out.println("No Information");
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
} catch (MessagingException e) {
|
} catch (MessagingException e) {
|
||||||
System.out.println(e.toString());
|
System.out.println(e.toString());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean connectSMTP(ContactId cid)
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean connectSMTP(ContactId cid) {
|
||||||
boolean sent = false;
|
boolean sent = false;
|
||||||
if(discoverContactEmail(cid) != null)
|
if (discoverContactEmail(cid) != null) {
|
||||||
{
|
|
||||||
Properties prop = new Properties();
|
Properties prop = new Properties();
|
||||||
prop.put("mail.smtp.host", "smtp.gmail.com");
|
prop.put("mail.smtp.host", "smtp.gmail.com");
|
||||||
prop.put("mail.smtp.socketFactory.port", "465");
|
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.auth", "true");
|
||||||
prop.put("mail.smtp.port", "465");
|
prop.put("mail.smtp.port", "465");
|
||||||
|
|
||||||
final ArrayList<String> userPass = getAuthenticationDetails(callback.getConfig());
|
final ArrayList<String> userPass = getAuthenticationDetails(callback
|
||||||
|
.getConfig());
|
||||||
|
|
||||||
if (userPass != null)
|
if (userPass != null) {
|
||||||
{
|
|
||||||
Session session;
|
Session session;
|
||||||
|
synchronized (this) {
|
||||||
synchronized(this)
|
session = Session.getInstance(prop,
|
||||||
{
|
new javax.mail.Authenticator() {
|
||||||
session = Session.getDefaultInstance(prop, new javax.mail.Authenticator() {
|
protected PasswordAuthentication getPasswordAuthentication() {
|
||||||
protected PasswordAuthentication getPasswordAuthentication() {
|
return new PasswordAuthentication(userPass
|
||||||
return new PasswordAuthentication(userPass.get(0),userPass.get(1));
|
.get(0), userPass.get(1));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
// SimplexTransportWriter writer = createWriter(cid);
|
||||||
// SimplexTransportWriter writer = createWriter(cid);
|
sent = sendMessage(session, cid);
|
||||||
|
|
||||||
sent = sendMessage(session,cid);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sent;
|
return sent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private synchronized boolean sendMessage(Session session, ContactId cid) {
|
||||||
private synchronized boolean sendMessage(Session session, ContactId cid)
|
|
||||||
{
|
|
||||||
boolean sent = false;
|
boolean sent = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Message message = new MimeMessage(session);
|
Message message = new MimeMessage(session);
|
||||||
TransportProperties props = callback.getLocalProperties();
|
TransportProperties props = callback.getLocalProperties();
|
||||||
String userEmail = props.get("email");
|
String userEmail = props.get("email");
|
||||||
|
|
||||||
message.setFrom(new InternetAddress(userEmail));
|
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.setSubject("Test Subject");
|
||||||
message.setText("Test content");
|
message.setText("Test content");
|
||||||
|
|
||||||
@@ -202,18 +191,16 @@ public class GmailPlugin implements SimplexPlugin {
|
|||||||
|
|
||||||
sent = true;
|
sent = true;
|
||||||
return sent;
|
return sent;
|
||||||
|
|
||||||
} catch (MessagingException e) {
|
} catch (MessagingException e) {
|
||||||
return sent;
|
return sent;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void stop() throws IOException {
|
public void stop() throws IOException {
|
||||||
synchronized(this) {
|
synchronized (this) {
|
||||||
//close open connections
|
// close open connections
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean shouldPoll() {
|
public boolean shouldPoll() {
|
||||||
@@ -224,8 +211,8 @@ public class GmailPlugin implements SimplexPlugin {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void poll(Collection<ContactId> connected) throws UnsupportedOperationException{
|
public void poll(Collection<ContactId> connected)
|
||||||
|
throws UnsupportedOperationException {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean supportsInvitations() {
|
public boolean supportsInvitations() {
|
||||||
@@ -233,73 +220,67 @@ public class GmailPlugin implements SimplexPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Gets the user's authentication details
|
* Gets the user's authentication details ArrayList.get(0) = username,
|
||||||
* ArrayList.get(0) = username, ArrayList.get(1) = password,
|
* ArrayList.get(1) = password, or null if either value is null.
|
||||||
* or null if either value is null.
|
|
||||||
*/
|
*/
|
||||||
private ArrayList<String> getAuthenticationDetails(TransportConfig config)
|
private ArrayList<String> getAuthenticationDetails(TransportConfig config) {
|
||||||
{
|
|
||||||
try {
|
try {
|
||||||
ArrayList<String> usernamePass = new ArrayList<String>();
|
ArrayList<String> usernamePass = new ArrayList<String>();
|
||||||
usernamePass.add(0, config.get("username"));
|
usernamePass.add(0, config.get("username"));
|
||||||
usernamePass.add(1, config.get("password"));
|
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;
|
return usernamePass;
|
||||||
else
|
else
|
||||||
return null;
|
return null;
|
||||||
} catch(Exception e) {
|
} catch (Exception e) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* looks up the contact's email address given the contactID
|
* looks up the contact's email address given the contactID
|
||||||
|
*
|
||||||
* @param ContactId
|
* @param ContactId
|
||||||
|
*
|
||||||
* @return String email
|
* @return String email
|
||||||
*/
|
*/
|
||||||
private String discoverContactEmail(ContactId cid)
|
private String discoverContactEmail(ContactId cid) {
|
||||||
{
|
|
||||||
try {
|
try {
|
||||||
Map<ContactId, TransportProperties> remote = callback.getRemoteProperties();
|
Map<ContactId, TransportProperties> remote = callback
|
||||||
|
.getRemoteProperties();
|
||||||
TransportProperties tp = remote.get(cid);
|
TransportProperties tp = remote.get(cid);
|
||||||
String address = tp.get("email");
|
String address = tp.get("email");
|
||||||
return address;
|
return address;
|
||||||
} catch(Exception e) {
|
} catch (Exception e) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimplexTransportReader createReader(ContactId c) {
|
public SimplexTransportReader createReader(ContactId c) {
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimplexTransportWriter createWriter(ContactId c) {
|
public SimplexTransportWriter createWriter(ContactId c) {
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimplexTransportWriter sendInvitation(PseudoRandom r, long timeout) throws UnsupportedOperationException {
|
public SimplexTransportWriter sendInvitation(PseudoRandom r, long timeout)
|
||||||
|
throws UnsupportedOperationException {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimplexTransportReader acceptInvitation(PseudoRandom r, long timeout) throws UnsupportedOperationException {
|
public SimplexTransportReader acceptInvitation(PseudoRandom r, long timeout)
|
||||||
|
throws UnsupportedOperationException {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimplexTransportWriter sendInvitationResponse(PseudoRandom r,
|
public SimplexTransportWriter sendInvitationResponse(PseudoRandom r,
|
||||||
long timeout) throws UnsupportedOperationException{
|
long timeout) throws UnsupportedOperationException {
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimplexTransportReader acceptInvitationResponse(PseudoRandom r,
|
public SimplexTransportReader acceptInvitationResponse(PseudoRandom r,
|
||||||
long timeout) throws UnsupportedOperationException{
|
long timeout) throws UnsupportedOperationException {
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import net.sf.briar.api.plugins.simplex.SimplexTransportReader;
|
|||||||
public class GmailTransportConnectionReader implements SimplexTransportReader{
|
public class GmailTransportConnectionReader implements SimplexTransportReader{
|
||||||
|
|
||||||
private static final Logger LOG = Logger.getLogger(GmailTransportConnectionReader.class.getName());
|
private static final Logger LOG = Logger.getLogger(GmailTransportConnectionReader.class.getName());
|
||||||
|
|
||||||
private final StreamConnection stream;
|
private final StreamConnection stream;
|
||||||
|
|
||||||
GmailTransportConnectionReader(StreamConnection stream) {
|
GmailTransportConnectionReader(StreamConnection stream) {
|
||||||
@@ -20,7 +19,6 @@ public class GmailTransportConnectionReader implements SimplexTransportReader{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public InputStream getInputStream() throws IOException {
|
public InputStream getInputStream() throws IOException {
|
||||||
|
|
||||||
return stream.openInputStream();
|
return stream.openInputStream();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import net.sf.briar.api.plugins.simplex.SimplexTransportWriter;
|
|||||||
public class GmailTransportConnectionWriter implements SimplexTransportWriter {
|
public class GmailTransportConnectionWriter implements SimplexTransportWriter {
|
||||||
|
|
||||||
private static final Logger LOG = Logger.getLogger(GmailTransportConnectionWriter.class.getName());
|
private static final Logger LOG = Logger.getLogger(GmailTransportConnectionWriter.class.getName());
|
||||||
|
|
||||||
private final StreamConnection stream;
|
private final StreamConnection stream;
|
||||||
private final long capacity = 25 * 1000 * 1000;
|
private final long capacity = 25 * 1000 * 1000;
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,6 @@ Map<ContactId,TransportProperties> map = new HashMap<ContactId, TransportPropert
|
|||||||
@Before
|
@Before
|
||||||
public void setup()
|
public void setup()
|
||||||
{
|
{
|
||||||
|
|
||||||
local = new TransportProperties();
|
local = new TransportProperties();
|
||||||
local.put("email",System.getenv("USER_GMAIL_ADDRESS"));
|
local.put("email",System.getenv("USER_GMAIL_ADDRESS"));
|
||||||
|
|
||||||
@@ -48,7 +47,6 @@ Map<ContactId,TransportProperties> map = new HashMap<ContactId, TransportPropert
|
|||||||
props1 = new TransportProperties();
|
props1 = new TransportProperties();
|
||||||
props1.put("email", System.getenv("CONTACT1_EMAIL"));
|
props1.put("email", System.getenv("CONTACT1_EMAIL"));
|
||||||
test1 = new ContactId(12);
|
test1 = new ContactId(12);
|
||||||
|
|
||||||
map.put(test1, props1);
|
map.put(test1, props1);
|
||||||
assertEquals(1,map.size());
|
assertEquals(1,map.size());
|
||||||
|
|
||||||
@@ -67,14 +65,13 @@ Map<ContactId,TransportProperties> map = new HashMap<ContactId, TransportPropert
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setLocalProperties(TransportProperties p) {
|
public void setLocalProperties(TransportProperties p) {
|
||||||
|
local = p;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setConfig(TransportConfig c) {
|
public void setConfig(TransportConfig c) {
|
||||||
config = c;
|
config = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Map<ContactId, TransportProperties> getRemoteProperties() {
|
public Map<ContactId, TransportProperties> getRemoteProperties() {
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
@@ -87,19 +84,13 @@ Map<ContactId,TransportProperties> map = new HashMap<ContactId, TransportPropert
|
|||||||
return config;
|
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.setLocalProperties(local);
|
||||||
callback.setConfig(config);
|
callback.setConfig(config);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -107,7 +98,6 @@ Map<ContactId,TransportProperties> map = new HashMap<ContactId, TransportPropert
|
|||||||
{
|
{
|
||||||
GmailPluginFactory plugin = new GmailPluginFactory();
|
GmailPluginFactory plugin = new GmailPluginFactory();
|
||||||
plugin.createPlugin(Executors.newSingleThreadExecutor(), callback);
|
plugin.createPlugin(Executors.newSingleThreadExecutor(), callback);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user