Author: ks156
Date: 2009-10-15 08:44:22 +0200 (Thu, 15 Oct 2009)
New Revision: 5668
Modified:
software_suite_v3/software/plugin/plugin-gmail/branches/random_sentences/plugin-gmail/src/net/karmaLab/tuxDroid/plugins/MailPlugin.java
Log:
* Set fileformat to unix
* Reindent
* Retab
* Removed trailing spaces
Modified:
software_suite_v3/software/plugin/plugin-gmail/branches/random_sentences/plugin-gmail/src/net/karmaLab/tuxDroid/plugins/MailPlugin.java
===================================================================
---
software_suite_v3/software/plugin/plugin-gmail/branches/random_sentences/plugin-gmail/src/net/karmaLab/tuxDroid/plugins/MailPlugin.java
2009-10-15 06:42:25 UTC (rev 5667)
+++
software_suite_v3/software/plugin/plugin-gmail/branches/random_sentences/plugin-gmail/src/net/karmaLab/tuxDroid/plugins/MailPlugin.java
2009-10-15 06:44:22 UTC (rev 5668)
@@ -1,389 +1,390 @@
-/* This file is part of "TuxDroid Gadget Mail".
- * Copyright 2008, kysoh
- * Author : Yoran Brault
- * eMail : softw...@_bad_karma-lab.net (remove _bad_ before sending an
email)
- * Site : http://www.kysoh.com/
- *
- * "TuxDroid Gadget Mail" is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * "TuxDroid Gadget Mail" is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public
- * License along with "TuxDroid Gadget Mail"; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-
-package net.karmaLab.tuxDroid.plugins;
-
-import java.io.IOException;
-import java.security.Security;
-import java.util.Properties;
-import java.util.Vector;
-import java.util.Random;
-
-import javax.mail.Folder;
-import javax.mail.Message;
-import javax.mail.MessagingException;
-import javax.mail.Session;
-import javax.mail.Store;
-import javax.mail.Flags.Flag;
-import javax.mail.internet.InternetAddress;
-
-import com.kysoh.tuxdroid.plugin.framework.plugin.SimplePlugin;
-
-/**
- * @author User
- */
-public class MailPlugin extends SimplePlugin<Configuration>
-{
- String[] newMailSentences = {
- "You have a new message.",
- "A new message has arrived !",
- "It appears you have received a new email.",
- "A new message ? I wonder who could be thinking about you.",
- "It would appear you have received new messages."
- };
-
- String[] mailSenderSentences = {
- "Mail sent by {0}",
- "The message was sent by {0}",
- "The sender of the message is {0}"
- };
-
- String[] mailSubjectSentences = {
- "The message subject is : {0}",
- "The subject of the message is : {0}"
- };
-
- String[] noNewMailSentences = {
- "No new mail.",
- "I'm sorry, nobody sent you any messages.",
- "No messages. Nobody seems to be thinking about you.",
- "Your mailbox is empty."
- };
-
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception
- {
- new MailPlugin().boot(args, new Configuration());
- }
-
- /**
- *
- */
- @Override
- public void start()
- {
- try
- {
- if (getCommand().equals("run"))
- {
- run();
- }
- else if (getCommand().equals("check"))
- {
- check();
- }
- else
- {
- run();
- }
- }
- catch (Exception e) {}
- }
-
-
- /**
- * @throws MessagingException
- * @throws IOException
- * @throws ClassNotFoundException
- */
- public void check() throws MessagingException, IOException,
ClassNotFoundException, Exception
- {
- Message messages[] = connectToMailBox();
-
- if (messages == null)
- {
- throwResult(false);
- return;
- }
- String sessionId = "pop.gmail.com_" + configuration().getUser();
- State stateAlert = readState(State.class, sessionId + "ALERT");
- /* Is first check ? */
- boolean firstCheck;
- if (stateAlert.getLastMessages().size() == 0)
- {
- firstCheck = true;
- stateAlert.getLastMessages().add("FIRST_CHECK_HACK");
- }
- else
- {
- firstCheck = false;
- }
- /* Check if you have a new message */
- boolean newMessageAlert = false;
- for (int i = messages.length - 1; i >= 0; i--)
- {
- if (!messages[i].getFlags().contains(Flag.SEEN))
- {
- String subject = messages[i].getSubject();
- if (subject != null)
- {
- if (!stateAlert.getLastMessages().contains(subject))
- {
- newMessageAlert = true;
- stateAlert.getLastMessages().add(subject);
- }
- }
- }
- }
- if((newMessageAlert) && (!firstCheck))
- {
- throwResult(true);
- throwMessage(this.pickSentence(newMailSentences));
- }
- else
- {
- throwResult(false);
- }
- /* Save the current referenced mails */
- writeState(stateAlert, sessionId + "ALERT");
- }
-
- /**
- *
- * @throws MessagingException
- * @throws IOException
- * @throws ClassNotFoundException
- */
- public void run() throws MessagingException, IOException,
ClassNotFoundException
- {
- Message messages[] = connectToMailBox();
- if (messages == null)
- {
- return;
- }
- String sessionId = "pop.gmail.com_" + configuration().getUser();
- State stateRun = readState(State.class, sessionId + "RUN");
- /* Check for new message */
- boolean newMessageRun = false;
- for (int i = messages.length - 1; i >= 0; i--)
- {
- if (!messages[i].getFlags().contains(Flag.SEEN))
- {
- if
(!stateRun.getLastMessages().contains(messages[i].getSubject()))
- {
- newMessageRun = true;
- break;
- }
- }
- }
- /* If new message found */
- if (newMessageRun)
- {
- int mailCounter = 0;
- boolean notifyNoNewMail = true;
- /* For all unread mails */
- for (int i = messages.length - 1; i >= 0; i--)
- {
- String subject;
- String filteredSubject;
- String sender;
- String senderPersonal;
- String senderAddress;
- boolean seen;
- boolean mailAlreadyRead;
-
- try
- {
- /* Get current mail data */
- subject = messages[i].getSubject();
- senderPersonal = ((InternetAddress)
messages[i].getFrom()[0]).getPersonal();
- senderAddress =
((InternetAddress)messages[i].getFrom()[0]).getAddress();
- seen = messages[i].getFlags().contains(Flag.SEEN);
- /* Pass if mail is already seen */
- if (seen)
- {
- continue;
- }
- /* Pass if subject is empty */
- if (subject == null)
- {
- continue;
- }
- /* Filtering the mail address */
- if (senderPersonal == null)
- {
- sender = senderAddress;
- }
- else
- {
- sender = senderPersonal;
- }
- if (sender.contains("<"))
- {
- sender = sender.substring(0, sender.indexOf("<"));
- }
- /* Filtering the mail subject */
- filteredSubject = subject.replace("*", " ");
- filteredSubject = filteredSubject.replace("_", " ");
- filteredSubject = filteredSubject.replace("{", " ");
- filteredSubject = filteredSubject.replace("}", " ");
- filteredSubject = filteredSubject.replace("[", " ");
- filteredSubject = filteredSubject.replace("]", " ");
- filteredSubject = filteredSubject.replace("/", " ");
- filteredSubject = filteredSubject.replace("\\", " ");
- filteredSubject = filteredSubject.replace(" ", " ");
- mailAlreadyRead = false;
- /* Check if the mail has already be read */
- for (int j = 0; j < stateRun.getLastMessages().size(); j++)
- {
- if
(filteredSubject.equals(stateRun.getLastMessages().get(j)))
- {
- mailAlreadyRead = true;
- break;
- }
- }
- /* If the mail is not already read */
- if (!mailAlreadyRead)
- {
- notifyNoNewMail = false;
- /* Read max 5 mails */
- mailCounter++;
- if (mailCounter <= 5)
- {
-
throwMessage(this.pickSentence(mailSenderSentences), sender);
-
throwMessage(this.pickSentence(mailSubjectSentences), filteredSubject);
- }
- /* Referencing the current mail */
- stateRun.getLastMessages().add(filteredSubject);
- }
- }
- catch (Exception e)
- {
- /* Prevent unhandled errors */
- ;
- }
- }
- if(notifyNoNewMail)
- {
-
throwMessage(this.pickSentence(noNewMailSentences));
- }
- }
- /* No new message */
- else
- {
- throwMessage(this.pickSentence(noNewMailSentences));
- }
- /* Save the current referenced mails */
- writeState(stateRun, sessionId + "RUN");
- }
-
- /**
- * This function return a sentences from a defined list
- */
- private String pickSentence(String[] list) {
- Random rand = new Random();
- return list[rand.nextInt(list.length)];
- }
-
- /**
- *
- * @return
- * @throws MessagingException
- * @throws IOException
- * @throws ClassNotFoundException
- */
- private Message[] connectToMailBox() throws MessagingException,
IOException, ClassNotFoundException
- {
- String user;
- String host;
- String password;
- Session session;
- Properties props;
- int port;
- String protocol;
- Store store;
- Folder folder;
- Message messages[];
-
- /* Get user data */
- user = configuration().getUser();
- password = configuration().getPassword();
- host = "pop.gmail.com";
- /* Set ssl socket */
- Security.setProperty("ssl.SocketFactory.provider",
"net.karmaLab.tuxDroid.plugins.DummySSLSocketFactory");
- /* Set text decoder */
- System.setProperty("mail.mime.decodetext.strict", "false");
- /* Configure seesion */
- props = new Properties();
- session = Session.getDefaultInstance(props, null);
- /* Find server port */
- port = 995;
- /* Get mail protocol */
- protocol = "pop3s";
- /* Create mail store */
- store = session.getStore(protocol);
- /* Connect to the mailbox */
- try
- {
- store.connect(host, port, user, password);
- }
- /* Authentification exception (login / password / protocol /
etc) */
- catch (javax.mail.AuthenticationFailedException e)
- {
- if (!getCommand().equals("check"))
- {
- if (e.toString().contains("[SYS/PERM]"))
- {
-
BareBonesBrowserLaunch.openURL("http://mail.google.com/mail/?#settings/fwdandpop");
- throwMessage("You must activate the pop3 service in your
Mail account");
- }
- else
- {
- throwMessage("Sorry, there was an error when connecting to
the mail server. Please check your email configuration.");
- }
- }
- return null;
- }
- /* Connection failed exception (timeout / network unreachable)
*/
- catch (javax.mail.MessagingException e)
- {
- if (!getCommand().equals("check"))
- {
- throwMessage("Sorry, I could not connect to the mail server.
Please check your internet connection or try again later.");
- }
- return null;
- }
- /* Connect and open inbox folder */
- folder = store.getFolder("INBOX");
- folder.open(Folder.READ_ONLY);
- /* Get messages */
- messages = folder.getMessages();
- return messages;
- }
-
- @Override
- protected void onPluginEvent(String arg0, String[] arg1) throws
Throwable
- {
- // TODO Auto-generated method stub
- ;
- }
-
- @Override
- protected void onPluginStop() throws Throwable
- {
- // TODO Auto-generated method stub
- ;
- }
-}
+/* This file is part of "TuxDroid Gadget Mail".
+ * Copyright 2008, kysoh
+ * Author : Yoran Brault
+ * eMail : softw...@_bad_karma-lab.net (remove _bad_ before sending an
+ * email)
+ * Site : http://www.kysoh.com/
+ *
+ * "TuxDroid Gadget Mail" is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * "TuxDroid Gadget Mail" is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with "TuxDroid Gadget Mail"; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package net.karmaLab.tuxDroid.plugins;
+
+import java.io.IOException;
+import java.security.Security;
+import java.util.Properties;
+import java.util.Vector;
+import java.util.Random;
+
+import javax.mail.Folder;
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.Store;
+import javax.mail.Flags.Flag;
+import javax.mail.internet.InternetAddress;
+
+import com.kysoh.tuxdroid.plugin.framework.plugin.SimplePlugin;
+
+/**
+ * @author User
+ */
+public class MailPlugin extends SimplePlugin<Configuration>
+{
+ String[] newMailSentences = {
+ "You have a new message.",
+ "A new message has arrived !",
+ "It appears you have received a new email.",
+ "A new message ? I wonder who could be thinking about you.",
+ "It would appear you have received new messages."
+ };
+
+ String[] mailSenderSentences = {
+ "Mail sent by {0}",
+ "The message was sent by {0}",
+ "The sender of the message is {0}"
+ };
+
+ String[] mailSubjectSentences = {
+ "The message subject is : {0}",
+ "The subject of the message is : {0}"
+ };
+
+ String[] noNewMailSentences = {
+ "No new mail.",
+ "I'm sorry, nobody sent you any messages.",
+ "No messages. Nobody seems to be thinking about you.",
+ "Your mailbox is empty."
+ };
+
+ /**
+ * @param args
+ * @throws Exception
+ */
+ public static void main(String[] args) throws Exception
+ {
+ new MailPlugin().boot(args, new Configuration());
+ }
+
+ /**
+ *
+ */
+ @Override
+ public void start()
+ {
+ try
+ {
+ if (getCommand().equals("run"))
+ {
+ run();
+ }
+ else if (getCommand().equals("check"))
+ {
+ check();
+ }
+ else
+ {
+ run();
+ }
+ }
+ catch (Exception e) {}
+ }
+
+
+ /**
+ * @throws MessagingException
+ * @throws IOException
+ * @throws ClassNotFoundException
+ */
+ public void check() throws MessagingException, IOException,
ClassNotFoundException, Exception
+ {
+ Message messages[] = connectToMailBox();
+
+ if (messages == null)
+ {
+ throwResult(false);
+ return;
+ }
+ String sessionId = "pop.gmail.com_" + configuration().getUser();
+ State stateAlert = readState(State.class, sessionId + "ALERT");
+ /* Is first check ? */
+ boolean firstCheck;
+ if (stateAlert.getLastMessages().size() == 0)
+ {
+ firstCheck = true;
+ stateAlert.getLastMessages().add("FIRST_CHECK_HACK");
+ }
+ else
+ {
+ firstCheck = false;
+ }
+ /* Check if you have a new message */
+ boolean newMessageAlert = false;
+ for (int i = messages.length - 1; i >= 0; i--)
+ {
+ if (!messages[i].getFlags().contains(Flag.SEEN))
+ {
+ String subject = messages[i].getSubject();
+ if (subject != null)
+ {
+ if (!stateAlert.getLastMessages().contains(subject))
+ {
+ newMessageAlert = true;
+ stateAlert.getLastMessages().add(subject);
+ }
+ }
+ }
+ }
+ if((newMessageAlert) && (!firstCheck))
+ {
+ throwResult(true);
+ throwMessage(this.pickSentence(newMailSentences));
+ }
+ else
+ {
+ throwResult(false);
+ }
+ /* Save the current referenced mails */
+ writeState(stateAlert, sessionId + "ALERT");
+ }
+
+ /**
+ *
+ * @throws MessagingException
+ * @throws IOException
+ * @throws ClassNotFoundException
+ */
+ public void run() throws MessagingException, IOException,
ClassNotFoundException
+ {
+ Message messages[] = connectToMailBox();
+ if (messages == null)
+ {
+ return;
+ }
+ String sessionId = "pop.gmail.com_" + configuration().getUser();
+ State stateRun = readState(State.class, sessionId + "RUN");
+ /* Check for new message */
+ boolean newMessageRun = false;
+ for (int i = messages.length - 1; i >= 0; i--)
+ {
+ if (!messages[i].getFlags().contains(Flag.SEEN))
+ {
+ if
(!stateRun.getLastMessages().contains(messages[i].getSubject()))
+ {
+ newMessageRun = true;
+ break;
+ }
+ }
+ }
+ /* If new message found */
+ if (newMessageRun)
+ {
+ int mailCounter = 0;
+ boolean notifyNoNewMail = true;
+ /* For all unread mails */
+ for (int i = messages.length - 1; i >= 0; i--)
+ {
+ String subject;
+ String filteredSubject;
+ String sender;
+ String senderPersonal;
+ String senderAddress;
+ boolean seen;
+ boolean mailAlreadyRead;
+
+ try
+ {
+ /* Get current mail data */
+ subject = messages[i].getSubject();
+ senderPersonal = ((InternetAddress)
messages[i].getFrom()[0]).getPersonal();
+ senderAddress =
((InternetAddress)messages[i].getFrom()[0]).getAddress();
+ seen = messages[i].getFlags().contains(Flag.SEEN);
+ /* Pass if mail is already seen */
+ if (seen)
+ {
+ continue;
+ }
+ /* Pass if subject is empty */
+ if (subject == null)
+ {
+ continue;
+ }
+ /* Filtering the mail address */
+ if (senderPersonal == null)
+ {
+ sender = senderAddress;
+ }
+ else
+ {
+ sender = senderPersonal;
+ }
+ if (sender.contains("<"))
+ {
+ sender = sender.substring(0, sender.indexOf("<"));
+ }
+ /* Filtering the mail subject */
+ filteredSubject = subject.replace("*", " ");
+ filteredSubject = filteredSubject.replace("_", " ");
+ filteredSubject = filteredSubject.replace("{", " ");
+ filteredSubject = filteredSubject.replace("}", " ");
+ filteredSubject = filteredSubject.replace("[", " ");
+ filteredSubject = filteredSubject.replace("]", " ");
+ filteredSubject = filteredSubject.replace("/", " ");
+ filteredSubject = filteredSubject.replace("\\", " ");
+ filteredSubject = filteredSubject.replace(" ", " ");
+ mailAlreadyRead = false;
+ /* Check if the mail has already be read */
+ for (int j = 0; j < stateRun.getLastMessages().size(); j++)
+ {
+ if
(filteredSubject.equals(stateRun.getLastMessages().get(j)))
+ {
+ mailAlreadyRead = true;
+ break;
+ }
+ }
+ /* If the mail is not already read */
+ if (!mailAlreadyRead)
+ {
+ notifyNoNewMail = false;
+ /* Read max 5 mails */
+ mailCounter++;
+ if (mailCounter <= 5)
+ {
+
throwMessage(this.pickSentence(mailSenderSentences), sender);
+
throwMessage(this.pickSentence(mailSubjectSentences), filteredSubject);
+ }
+ /* Referencing the current mail */
+ stateRun.getLastMessages().add(filteredSubject);
+ }
+ }
+ catch (Exception e)
+ {
+ /* Prevent unhandled errors */
+ ;
+ }
+ }
+ if(notifyNoNewMail)
+ {
+ throwMessage(this.pickSentence(noNewMailSentences));
+ }
+ }
+ /* No new message */
+ else
+ {
+ throwMessage(this.pickSentence(noNewMailSentences));
+ }
+ /* Save the current referenced mails */
+ writeState(stateRun, sessionId + "RUN");
+ }
+
+ /**
+ * This function return a sentences from a defined list
+ */
+ private String pickSentence(String[] list) {
+ Random rand = new Random();
+ return list[rand.nextInt(list.length)];
+ }
+
+ /**
+ *
+ * @return
+ * @throws MessagingException
+ * @throws IOException
+ * @throws ClassNotFoundException
+ */
+ private Message[] connectToMailBox() throws MessagingException,
IOException, ClassNotFoundException
+ {
+ String user;
+ String host;
+ String password;
+ Session session;
+ Properties props;
+ int port;
+ String protocol;
+ Store store;
+ Folder folder;
+ Message messages[];
+
+ /* Get user data */
+ user = configuration().getUser();
+ password = configuration().getPassword();
+ host = "pop.gmail.com";
+ /* Set ssl socket */
+ Security.setProperty("ssl.SocketFactory.provider",
"net.karmaLab.tuxDroid.plugins.DummySSLSocketFactory");
+ /* Set text decoder */
+ System.setProperty("mail.mime.decodetext.strict", "false");
+ /* Configure seesion */
+ props = new Properties();
+ session = Session.getDefaultInstance(props, null);
+ /* Find server port */
+ port = 995;
+ /* Get mail protocol */
+ protocol = "pop3s";
+ /* Create mail store */
+ store = session.getStore(protocol);
+ /* Connect to the mailbox */
+ try
+ {
+ store.connect(host, port, user, password);
+ }
+ /* Authentification exception (login / password / protocol / etc) */
+ catch (javax.mail.AuthenticationFailedException e)
+ {
+ if (!getCommand().equals("check"))
+ {
+ if (e.toString().contains("[SYS/PERM]"))
+ {
+
BareBonesBrowserLaunch.openURL("http://mail.google.com/mail/?#settings/fwdandpop");
+ throwMessage("You must activate the pop3 service in your
Mail account");
+ }
+ else
+ {
+ throwMessage("Sorry, there was an error when connecting to
the mail server. Please check your email configuration.");
+ }
+ }
+ return null;
+ }
+ /* Connection failed exception (timeout / network unreachable) */
+ catch (javax.mail.MessagingException e)
+ {
+ if (!getCommand().equals("check"))
+ {
+ throwMessage("Sorry, I could not connect to the mail server.
Please check your internet connection or try again later.");
+ }
+ return null;
+ }
+ /* Connect and open inbox folder */
+ folder = store.getFolder("INBOX");
+ folder.open(Folder.READ_ONLY);
+ /* Get messages */
+ messages = folder.getMessages();
+ return messages;
+ }
+
+ @Override
+ protected void onPluginEvent(String arg0, String[] arg1) throws
Throwable
+ {
+ // TODO Auto-generated method stub
+ ;
+ }
+
+ @Override
+ protected void onPluginStop() throws Throwable
+ {
+ // TODO Auto-generated method stub
+ ;
+ }
+}
------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn