[tuxdroid-svn] r4869 - software_suite_v3/software/plugin/plugin-email/trunk/plugin-email/src/net/ka
Status: Beta
Brought to you by:
ks156
|
From: jerome <c2m...@c2...> - 2009-06-23 14:20:09
|
Author: jerome Date: 2009-06-23 14:19:06 +0200 (Tue, 23 Jun 2009) New Revision: 4869 Modified: software_suite_v3/software/plugin/plugin-email/trunk/plugin-email/src/net/karmaLab/tuxDroid/plugins/MailPlugin.java Log: * Make use of the new source from R?\195?\169mi. Modified: software_suite_v3/software/plugin/plugin-email/trunk/plugin-email/src/net/karmaLab/tuxDroid/plugins/MailPlugin.java =================================================================== --- software_suite_v3/software/plugin/plugin-email/trunk/plugin-email/src/net/karmaLab/tuxDroid/plugins/MailPlugin.java 2009-06-23 11:37:18 UTC (rev 4868) +++ software_suite_v3/software/plugin/plugin-email/trunk/plugin-email/src/net/karmaLab/tuxDroid/plugins/MailPlugin.java 2009-06-23 12:19:06 UTC (rev 4869) @@ -1,367 +1,390 @@ -/* This file is part of "TuxDroid Plugin Mail". - * Copyright 2009, kysoh - * Author : Yoran Brault - * eMail : software@_bad_karma-lab.net (remove _bad_ before sending an email) - * Site : http://www.kysoh.com/ - * - * "TuxDroid Plugin 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 Plugin 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 Plugin 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 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> { - /** - * @param args - * @throws Exception - */ - public static void main(String[] args) throws Exception { - new MailPlugin().boot(args, new Configuration()); - } - - /** - * - * @param readOne - * @throws MessagingException - * @throws IOException - * @throws ClassNotFoundException - */ - public void mailBox(boolean readOne) throws MessagingException, IOException, ClassNotFoundException { - this.configuration().setPort(-1); - String sessionId = configuration().getHost() + "_" + configuration().getPort() + "_" + configuration().getUser(); - State stateAlert = readState(State.class, sessionId + "ALERT"); - State stateRun = readState(State.class, sessionId + "RUN"); - - Security.setProperty("ssl.SocketFactory.provider", "net.karmaLab.tuxDroid.gadgets.DummySSLSocketFactory"); - - /* Connect to the mail box */ - System.setProperty("mail.mime.decodetext.strict", "false"); - Properties props = new Properties(); - Session session = Session.getDefaultInstance(props, null); - Store store = session.getStore(configuration().getProtocol().toString()); - int port = configuration().getPort(); - if (port == -1) { - switch (configuration().getProtocol()) { - case imap: - port = 143; - break; - case pop3: - port = 110; - break; - case imaps: - port = 993; - break; - case pop3s: - port = 995; - break; - } - } - try { - store.connect(configuration().getHost(), port, configuration().getUser(), configuration().getPassword()); - } - // Authentification exception (login / password / protocol / etc) - catch (javax.mail.AuthenticationFailedException e) - { - throwMessage("Sorry, there was an error when connecting to the mail server. Please check your email configuration."); - } - // Connection failed exception (timeout / network unreachable) - catch (javax.mail.MessagingException e) - { - throwMessage("Sorry, I could not connect to the mail server. Please check your internet connection or try again later."); - } - - - Folder folder = store.getFolder(configuration().getFolder()); - folder.open(Folder.READ_ONLY); - Message messages[] = folder.getMessages(); - - /* Check if you have a new message */ - boolean newMessageAlert = false; - boolean newMessageRun = false; - for (int i = 0; i < messages.length; i++) { - if (!messages[i].getFlags().contains(Flag.SEEN)) { - if (!stateAlert.getLastMessages().contains(messages[i].getSubject())) { - newMessageAlert = true; - break; - } - } - } - - for (int i = 0; i < messages.length; i++) { - if (!messages[i].getFlags().contains(Flag.SEEN)) { - if (!stateRun.getLastMessages().contains(messages[i].getSubject())) { - newMessageRun = true; - break; - } - } - } - - /* If (ALERT MODE) */ - if (readOne) { - /* If new message */ - if (newMessageAlert) { - - Vector<Boolean> ver = new Vector<Boolean>(); - boolean notify = false; - - //Check for allowed messages. - for(int i = 0; i < messages.length; i++) - { - String subject = messages[i].getSubject(); - String mail = ((InternetAddress)messages[i].getFrom()[0]).getAddress(); - boolean mFilter = this.filterSender(mail); - boolean sFilter = this.filterSubject(subject); - - if(mFilter) - { - ver.add(new Boolean(false)); - } - - else if(sFilter) - { - ver.add(new Boolean(false)); - } - - stateAlert.getLastMessages().add(messages[i].getSubject()); - } - - for(Boolean bool : ver) - { - if(!bool.booleanValue()) - notify = true; - } - - if(notify) - { - throwMessage("You have a new message."); - } - } - } - /* If (RUN MODE) */ - else { - boolean notifyNoNewMail = true; - /* If new message */ - if (newMessageRun) { - int mailCounter = 0; - - /* For all unread mails */ - for (int i = 0; i < messages.length; i++) { - /* Get the subject and the sender of the current mail */ - String subject = messages[i].getSubject(); - String sender = ((InternetAddress) messages[i].getFrom()[0]).getPersonal(); - String mail = ((InternetAddress)messages[i].getFrom()[0]).getAddress(); - - if (sender == null) { - sender = ((InternetAddress) messages[i].getFrom()[0]).getAddress(); - } - - /* Filtering the mail address */ - if (sender.contains("<")) { - sender = sender.substring(0, sender.indexOf("<")); - } - - /* Filtering the mail subject */ - subject = subject.replace("*", " "); - subject = subject.replace("_", " "); - subject = subject.replace("{", " "); - subject = subject.replace("}", " "); - subject = subject.replace("[", " "); - subject = subject.replace("]", " "); - subject = subject.replace("/", " "); - subject = subject.replace("\\", " "); - subject = subject.replace(" ", " "); - - /* If the mail is marked as not seen */ - if (!messages[i].getFlags().contains(Flag.SEEN)) { - boolean mailAlreadyRead = false; - - if(this.configuration().isSenderFiltered()) - { - //Set sender filter. - mailAlreadyRead = filterSender(mail); - } - - else if(this.configuration().isSubjectFiltered()) - { - //Set subject filter. - mailAlreadyRead = filterSubject(subject); - } - - else - { - /* Check if the mail is not already read */ - for (int j = 0; j < stateRun.getLastMessages().size(); j++) { - if (subject.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) { - //TODO set filters. - throwMessage("Mail sent by {0}", sender); - throwMessage("The message subject is : {0}", subject); - } - - /* Referencing the current mail */ - stateRun.getLastMessages().add(messages[i].getSubject()); - - } - } - } - if(notifyNoNewMail) - { - throwMessage("No new mail."); - } - } - /* No new message */ - else { - throwMessage("No new mail."); - } - } - - /* Save the current referenced mails */ - writeState(stateAlert, sessionId + "ALERT"); - writeState(stateRun, sessionId + "RUN"); - } - - - /** - * Return true if the sender is not allowed. - * @param mail - * @return - */ - private boolean filterSender(String mail) - { - Vector<Boolean> ver = new Vector<Boolean>(); - for(String allowedSender : this.configuration().getSenderFilter()) - { - if(!mail.equals(allowedSender)) - { - ver.add(new Boolean(false)); - } - } - - for(Boolean bool : ver) - { - if(!bool.booleanValue()) - { - return true; - } - } - - return false; - } - - - /** - * Return true if subject is not allowed. - * @param subject - * @return - */ - private boolean filterSubject(String subject) - { - Vector<Boolean> ver = new Vector<Boolean>(); - for(String allowedContent : this.configuration().getSubjectFilter()) - { - if(!subject.contains(allowedContent)) - { - ver.add(new Boolean(false)); - } - } - - for(Boolean bool : ver) - { - if(!bool.booleanValue()) - { - return true; - } - } - - return false; - } - - /** - * @throws MessagingException - * @throws IOException - * @throws ClassNotFoundException - */ - public void check() throws MessagingException, IOException, ClassNotFoundException { - mailBox(true); - } - - /** - * - * @throws MessagingException - * @throws IOException - * @throws ClassNotFoundException - */ - public void run() throws MessagingException, IOException, ClassNotFoundException { - mailBox(false); - } - - /** - * - */ - @Override - public void start() throws Exception { - - if (getCommand().equals("check")) { - check(); - return; - } - else if (getCommand().equals("run")) { - run(); - return; - } - } - - @Override - protected void onPluginEvent(String arg0, String[] arg1) throws Throwable - { - ; - } - - @Override - protected void onPluginStop() throws Throwable - { - ; - } - -} +/* This file is part of "TuxDroid Gadget Mail". + * Copyright 2008, kysoh + * Author : Yoran Brault + * eMail : software@_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 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> +{ + /** + * @param args + * @throws Exception + */ + public static void main(String[] args) throws Exception + { + new MailPlugin().boot(args, new Configuration()); + } + + /** + * + */ + @Override + public void start() throws Exception + { + if (getCommand().equals("run")) + { + run(); + } + else if (getCommand().equals("check")) + { + check(); + } + else + { + run(); + } + } + + + /** + * @throws MessagingException + * @throws IOException + * @throws ClassNotFoundException + */ + public void check() throws MessagingException, IOException, ClassNotFoundException + { + Message messages[] = connectToMailBox(); + if (messages == null) + { + throwResult(false); + return; + } + String sessionId = configuration().getHost() + "_" + configuration().getPort() + "_" + configuration().getUser(); + State stateAlert = readState(State.class, sessionId + "ALERT"); + /* Check if you have a new message */ + boolean newMessageAlert = false; + for (int i = 0; i < messages.length; i++) + { + if (!messages[i].getFlags().contains(Flag.SEEN)) + { + if (!stateAlert.getLastMessages().contains(messages[i].getSubject())) + { + newMessageAlert = true; + break; + } + } + } + /* If new message */ + if (newMessageAlert) + { + Vector<Boolean> ver = new Vector<Boolean>(); + boolean notify = false; + //Check for allowed messages. + for(int i = 0; i < messages.length; i++) + { + String subject = messages[i].getSubject(); + String mail = ((InternetAddress)messages[i].getFrom()[0]).getAddress(); + boolean mFilter = this.filterSender(mail); + boolean sFilter = this.filterSubject(subject); + if(mFilter) + { + ver.add(new Boolean(false)); + } + else if(sFilter) + { + ver.add(new Boolean(false)); + } + stateAlert.getLastMessages().add(messages[i].getSubject()); + } + for(Boolean bool : ver) + { + if(!bool.booleanValue()) + { + notify = true; + break; + } + } + if(notify) + { + throwResult(true); + throwMessage("You have a new message."); + } + 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 = configuration().getHost() + "_" + configuration().getPort() + "_" + configuration().getUser(); + State stateRun = readState(State.class, sessionId + "RUN"); + /* Check if you have a new message */ + boolean newMessageRun = false; + for (int i = 0; i < messages.length; i++) + { + if (!messages[i].getFlags().contains(Flag.SEEN)) + { + if (!stateRun.getLastMessages().contains(messages[i].getSubject())) + { + newMessageRun = true; + break; + } + } + } + boolean notifyNoNewMail = true; + /* If new message */ + if (newMessageRun) + { + int mailCounter = 0; + + /* For all unread mails */ + for (int i = 0; i < messages.length; i++) + { + /* Get the subject and the sender of the current mail */ + String subject = messages[i].getSubject(); + String sender = ((InternetAddress) messages[i].getFrom()[0]).getPersonal(); + String mail = ((InternetAddress)messages[i].getFrom()[0]).getAddress(); + if (sender == null) + { + sender = ((InternetAddress) messages[i].getFrom()[0]).getAddress(); + } + /* Filtering the mail address */ + if (sender.contains("<")) + { + sender = sender.substring(0, sender.indexOf("<")); + } + /* Filtering the mail subject */ + subject = subject.replace("*", " "); + subject = subject.replace("_", " "); + subject = subject.replace("{", " "); + subject = subject.replace("}", " "); + subject = subject.replace("[", " "); + subject = subject.replace("]", " "); + subject = subject.replace("/", " "); + subject = subject.replace("\\", " "); + subject = subject.replace(" ", " "); + /* If the mail is marked as not seen */ + if (!messages[i].getFlags().contains(Flag.SEEN)) + { + boolean mailAlreadyRead = false; + if(this.configuration().isSenderFiltered()) + { + //Set sender filter. + mailAlreadyRead = filterSender(mail); + } + else if(this.configuration().isSubjectFiltered()) + { + //Set subject filter. + mailAlreadyRead = filterSubject(subject); + } + else + { + /* Check if the mail is not already read */ + for (int j = 0; j < stateRun.getLastMessages().size(); j++) + { + if (subject.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("Mail sent by {0}", sender); + throwMessage("The message subject is : {0}", subject); + } + /* Referencing the current mail */ + stateRun.getLastMessages().add(messages[i].getSubject()); + + } + } + } + if(notifyNoNewMail) + { + throwMessage("No new mail."); + } + } + /* No new message */ + else + { + throwMessage("No new mail."); + } + /* Save the current referenced mails */ + writeState(stateRun, sessionId + "RUN"); + } + + /** + * + * @return + * @throws MessagingException + * @throws IOException + * @throws ClassNotFoundException + */ + private Message[] connectToMailBox() throws MessagingException, IOException, ClassNotFoundException + { + this.configuration().setPort(-1); + Security.setProperty("ssl.SocketFactory.provider", "net.karmaLab.tuxDroid.gadgets.DummySSLSocketFactory"); + /* Connect to the mail box */ + System.setProperty("mail.mime.decodetext.strict", "false"); + Properties props = new Properties(); + Session session = Session.getDefaultInstance(props, null); + Store store = session.getStore(configuration().getProtocol().toString()); + int port = configuration().getPort(); + if (port == -1) + { + switch (configuration().getProtocol()) + { + case imap: + port = 143; + break; + case pop3: + port = 110; + break; + case imaps: + port = 993; + break; + case pop3s: + port = 995; + break; + } + } + try + { + store.connect(configuration().getHost(), port, configuration().getUser(), configuration().getPassword()); + } + // Authentification exception (login / password / protocol / etc) + catch (javax.mail.AuthenticationFailedException e) + { + if (!getCommand().equals("check")) + { + 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; + } + Folder folder = store.getFolder(configuration().getFolder()); + folder.open(Folder.READ_ONLY); + Message messages[] = folder.getMessages(); + return messages; + } + + /** + * Return true if the sender is not allowed. + * @param mail + * @return + */ + private boolean filterSender(String mail) + { + Vector<Boolean> ver = new Vector<Boolean>(); + for(String allowedSender : this.configuration().getSenderFilter()) + { + if(!mail.equals(allowedSender)) + { + ver.add(new Boolean(false)); + } + } + for(Boolean bool : ver) + { + if(!bool.booleanValue()) + { + return true; + } + } + return false; + } + + /** + * Return true if subject is not allowed. + * @param subject + * @return + */ + private boolean filterSubject(String subject) + { + Vector<Boolean> ver = new Vector<Boolean>(); + for(String allowedContent : this.configuration().getSubjectFilter()) + { + if(!subject.contains(allowedContent)) + { + ver.add(new Boolean(false)); + } + } + for(Boolean bool : ver) + { + if(!bool.booleanValue()) + { + return true; + } + } + return false; + } + + @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 + ; + } +} \ No newline at end of file |