Search in sources :

Example 11 with AdminTask

use of web.AdminTask in project common by zenlunatics.

the class MailLists method checkForMail.

// --------------------------------------------------------------------------
@AdminTask
public void checkForMail() {
    if (m_site.getBaseFilePath().toString().startsWith("/Users"))
        return;
    if (m_read_and_send)
        readAndSend();
    else {
        if (m_lists != null && m_lists.size() == 0)
            return;
        DBConnection db = new DBConnection(m_site);
        if (m_lists == null)
            m_lists = db.readRows(new Select("name,username,host,password,id").from("mail_lists").where("active"));
        try {
            for (String[] list : m_lists) if (list[2] != null && list[3] != null)
                fetchAndSend(list[0], accountName(list[1], Integer.parseInt(list[4])), list[2], list[3], db);
        } catch (Exception e) {
            System.out.println("fetchAndSendAll");
            m_site.log(e);
        }
        db.close();
    }
}
Also used : DBConnection(db.DBConnection) Select(db.Select) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SQLException(java.sql.SQLException) AuthenticationFailedException(javax.mail.AuthenticationFailedException) MailConnectException(com.sun.mail.util.MailConnectException) IOException(java.io.IOException) AdminTask(web.AdminTask)

Example 12 with AdminTask

use of web.AdminTask in project common by zenlunatics.

the class MailLists method sendDigests.

// --------------------------------------------------------------------------
@AdminTask({ "date" })
public void sendDigests(LocalDate date, Site site, DBConnection db) {
    Session session = getSession();
    List<String> mail_lists = db.readValues(new Select("mail_lists_id").distinct().from("mail_lists_digest"));
    for (String list_id : mail_lists) {
        List<String> emails = db.readValues(new Select("email").from("people JOIN mail_lists_digest ON (mail_lists_digest.people_id=people.id)").where("mail_lists_id=" + list_id));
        InternetAddress[] recipients = new InternetAddress[emails.size()];
        for (int i = 0; i < emails.size(); i++) try {
            recipients[i] = new InternetAddress(emails.get(i));
        } catch (AddressException e) {
            e.printStackTrace();
        }
        MailList mail_list = new MailList(Integer.parseInt(list_id), site, db);
        MimeMessage message = mail_list.buildDigest(date, session, site, db);
        if (message != null)
            send(mail_list.getName(), message, recipients);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) AddressException(javax.mail.internet.AddressException) Select(db.Select) Session(javax.mail.Session) AdminTask(web.AdminTask)

Example 13 with AdminTask

use of web.AdminTask in project common by zenlunatics.

the class MailLists method setupList.

// --------------------------------------------------------------------------
@AdminTask
public static void setupList(String name, Site site) {
    File receive_script = new File(site.getBaseFilePath().append("inbox").append(name + ".sh").toString());
    if (!receive_script.exists())
        try {
            FileWriter fw = new FileWriter(receive_script);
            fw.write("cd ");
            fw.write(site.getBaseFilePath().toString());
            fw.write(";/usr/bin/java ReceiveMail ");
            fw.write(name);
            fw.close();
            receive_script.setExecutable(true);
        } catch (IOException e) {
        }
    new File(site.getBaseFilePath().append("inbox").append(name).toString()).mkdirs();
    String base_file_path = site.getBaseFilePath().toString();
    if (base_file_path.endsWith("/ROOT"))
        base_file_path = base_file_path.substring(0, base_file_path.length() - 5);
    site.runScript("new_email", base_file_path.substring(base_file_path.lastIndexOf('/') + 1), name, site.getDomain());
}
Also used : FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File) AdminTask(web.AdminTask)

Example 14 with AdminTask

use of web.AdminTask in project common by zenlunatics.

the class MailLists method fetchAndSend.

// --------------------------------------------------------------------------
@AdminTask({ "list", "account name", "host", "password" })
public synchronized void fetchAndSend(String list, String account_name, String host, String password, DBConnection db) throws MessagingException {
    Session session = Session.getInstance(new Properties(), null);
    Folder folder = null;
    Store store = null;
    try {
        store = session.getStore(m_store_protocol);
        try {
            store.connect(host, account_name, password);
        } catch (AuthenticationFailedException e) {
            if (e.toString().indexOf("temporarily unavailable") == -1)
                try {
                    TimeUnit.SECONDS.sleep(15);
                    store.connect(host, account_name, password);
                } catch (AuthenticationFailedException e1) {
                    if (e1.toString().indexOf("temporarily unavailable") == -1)
                        sendError(list, "fetchAndSend", "store.connect", null, e1);
                    return;
                } catch (InterruptedException e1) {
                    sendError(list, "fetchAndSend", "store.connect", null, e1);
                    return;
                }
            else
                return;
        } catch (MailConnectException e) {
            m_site.log(e);
            if (host.equals("172.21.0.1") || host.equals("sonoracohousing.com"))
                return;
        }
        if (store.isConnected()) {
            folder = store.getFolder("INBOX");
            try {
                folder.open(Folder.READ_WRITE);
            } catch (MessagingException e) {
                if (e.toString().indexOf("* BYE") != -1)
                    return;
                throw e;
            }
            Message[] messages = folder.getMessages();
            long now = new Date().getTime();
            for (Message message : messages) {
                if (message.getHeader("X-ignore") != null || message.getHeader("auto-submitted") != null || message.getHeader("Auto-Submitted") != null || message.getHeader("X-Autoreply") != null || message.getHeader("X-Autorespond") != null) {
                    message.setFlag(Flags.Flag.DELETED, true);
                    continue;
                }
                Date received_date = message.getReceivedDate();
                long minutes = (now - received_date.getTime()) / 60000;
                if (minutes > 30)
                    sendError(list, "fetchAndSend", "message " + minutes + " minutes old", null, null);
                Address[] recipients = message.getRecipients(RecipientType.TO);
                if (recipients != null) {
                    String to = recipients[0].toString();
                    if (to == null)
                        System.out.println("to is null");
                    else if (to.indexOf('+') != -1)
                        message.setFlag(Flags.Flag.DELETED, true);
                }
                if (handleMessage(list, message, true, db))
                    message.setFlag(Flags.Flag.DELETED, true);
            }
        }
    } catch (MessagingException e) {
        if (e.toString().indexOf("Connection dropped by server?") == -1 && e.toString().indexOf("BYE JavaMail") == -1)
            m_site.log(e);
    // sendError(list, "fetchAndSend", null, e);
    } finally {
        try {
            if (folder != null && folder.isOpen())
                folder.close(true);
        } catch (Exception e) {
            sendError(list, "fetchAndSend", "closing folder", null, e);
        }
        store.close();
    }
}
Also used : Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) AuthenticationFailedException(javax.mail.AuthenticationFailedException) MessagingException(javax.mail.MessagingException) Store(javax.mail.Store) MailConnectException(com.sun.mail.util.MailConnectException) Properties(java.util.Properties) Folder(javax.mail.Folder) Date(java.util.Date) LocalDate(java.time.LocalDate) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SQLException(java.sql.SQLException) AuthenticationFailedException(javax.mail.AuthenticationFailedException) MailConnectException(com.sun.mail.util.MailConnectException) IOException(java.io.IOException) Session(javax.mail.Session) AdminTask(web.AdminTask)

Example 15 with AdminTask

use of web.AdminTask in project common by zenlunatics.

the class EventProvider method writeICS.

// --------------------------------------------------------------------------
@AdminTask
public synchronized void writeICS(Request request) throws IOException {
    ICalendar ical = new ICalendar();
    String string = request.site.getSettings().getString("time zone");
    TimeZone time_zone = TimeZone.getTimeZone(string);
    TimezoneAssignment tza = TimezoneAssignment.download(time_zone, false);
    ical.getTimezoneInfo().setDefaultTimezone(tza);
    ArrayList<Event> events = new ArrayList<Event>();
    addAllEvents(events, request);
    for (Event event : events) {
        VEvent vevent = event.newVEvent(request);
        if (vevent != null)
            ical.addEvent(vevent);
    }
    FilePathStringBuilder fpsb = request.site.getBaseFilePath().append("calendars");
    new File(fpsb.toString()).mkdirs();
    String file_path = fpsb.append(m_name).toString();
    Biweekly.write(ical).go(new File(file_path));
    new File(file_path).renameTo(new File(file_path + ".ics"));
}
Also used : VEvent(biweekly.component.VEvent) TimeZone(java.util.TimeZone) TimezoneAssignment(biweekly.io.TimezoneAssignment) ArrayList(java.util.ArrayList) VEvent(biweekly.component.VEvent) ICalendar(biweekly.ICalendar) FilePathStringBuilder(web.FilePathStringBuilder) File(java.io.File) AdminTask(web.AdminTask)

Aggregations

AdminTask (web.AdminTask)16 IOException (java.io.IOException)9 File (java.io.File)7 SQLException (java.sql.SQLException)7 Select (db.Select)6 ResultSet (java.sql.ResultSet)5 Session (javax.mail.Session)4 FilePathStringBuilder (web.FilePathStringBuilder)4 LocalDate (java.time.LocalDate)3 Properties (java.util.Properties)3 MessagingException (javax.mail.MessagingException)3 AddressException (javax.mail.internet.AddressException)3 MimeMessage (javax.mail.internet.MimeMessage)3 ICalendar (biweekly.ICalendar)2 VEvent (biweekly.component.VEvent)2 MailConnectException (com.sun.mail.util.MailConnectException)2 DBConnection (db.DBConnection)2 NameValuePairs (db.NameValuePairs)2 FileNotFoundException (java.io.FileNotFoundException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2