Search in sources :

Example 31 with I2PAppContext

use of net.i2p.I2PAppContext in project i2p.i2p by i2p.

the class WebMail method sendMail.

/**
 * @param sessionObject
 * @param request
 * @return success
 */
private static boolean sendMail(SessionObject sessionObject, RequestWrapper request) {
    boolean ok = true;
    String from = request.getParameter(NEW_FROM);
    String to = request.getParameter(NEW_TO);
    String cc = request.getParameter(NEW_CC);
    String bcc = request.getParameter(NEW_BCC);
    String subject = request.getParameter(NEW_SUBJECT, _t("no subject"));
    String text = request.getParameter(NEW_TEXT, "");
    boolean fixed = Boolean.parseBoolean(Config.getProperty(CONFIG_SENDER_FIXED, "true"));
    if (fixed) {
        String domain = Config.getProperty(CONFIG_SENDER_DOMAIN, "mail.i2p");
        from = "<" + sessionObject.user + "@" + domain + ">";
    }
    ArrayList<String> toList = new ArrayList<String>();
    ArrayList<String> ccList = new ArrayList<String>();
    ArrayList<String> bccList = new ArrayList<String>();
    ArrayList<String> recipients = new ArrayList<String>();
    String sender = null;
    if (from == null || !Mail.validateAddress(from)) {
        ok = false;
        sessionObject.error += _t("Found no valid sender address.") + '\n';
    } else {
        sender = Mail.getAddress(from);
        if (sender == null || sender.length() == 0) {
            ok = false;
            sessionObject.error += _t("Found no valid address in \\''{0}\\''.", quoteHTML(from)) + '\n';
        }
    }
    ok = Mail.getRecipientsFromList(toList, to, ok);
    ok = Mail.getRecipientsFromList(ccList, cc, ok);
    ok = Mail.getRecipientsFromList(bccList, bcc, ok);
    recipients.addAll(toList);
    recipients.addAll(ccList);
    recipients.addAll(bccList);
    String bccToSelf = request.getParameter(NEW_BCC_TO_SELF);
    boolean toSelf = "1".equals(bccToSelf);
    // save preference in session
    sessionObject.bccToSelf = toSelf;
    if (toSelf)
        recipients.add(sender);
    if (toList.isEmpty()) {
        ok = false;
        sessionObject.error += _t("No recipients found.") + '\n';
    }
    Encoding qp = EncodingFactory.getEncoding("quoted-printable");
    Encoding hl = EncodingFactory.getEncoding("HEADERLINE");
    if (qp == null) {
        ok = false;
        // can't happen, don't translate
        sessionObject.error += "Internal error: Quoted printable encoder not available.";
    }
    if (hl == null) {
        ok = false;
        // can't happen, don't translate
        sessionObject.error += "Internal error: Header line encoder not available.";
    }
    long total = text.length();
    boolean multipart = sessionObject.attachments != null && !sessionObject.attachments.isEmpty();
    if (multipart) {
        for (Attachment a : sessionObject.attachments) {
            total += a.getSize();
        }
    }
    if (total > SMTPClient.BINARY_MAX_SIZE) {
        ok = false;
        sessionObject.error += _t("Email is too large, max is {0}", DataHelper.formatSize2(SMTPClient.BINARY_MAX_SIZE, false) + 'B') + '\n';
    }
    if (ok) {
        StringBuilder body = new StringBuilder(1024);
        I2PAppContext ctx = I2PAppContext.getGlobalContext();
        body.append("Date: " + RFC822Date.to822Date(ctx.clock().now()) + "\r\n");
        // todo include real names, and headerline encode them
        body.append("From: " + from + "\r\n");
        Mail.appendRecipients(body, toList, "To: ");
        Mail.appendRecipients(body, ccList, "Cc: ");
        try {
            body.append(hl.encode("Subject: " + subject.trim()));
        } catch (EncodingException e) {
            ok = false;
            sessionObject.error += e.getMessage();
        }
        String boundary = "_=" + ctx.random().nextLong();
        if (multipart) {
            body.append("MIME-Version: 1.0\r\nContent-type: multipart/mixed; boundary=\"" + boundary + "\"\r\n\r\n");
        } else {
            body.append("MIME-Version: 1.0\r\nContent-type: text/plain; charset=\"utf-8\"\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n");
        }
        try {
            // TODO pass the text separately to SMTP and let it pick the encoding
            if (multipart)
                body.append("--" + boundary + "\r\nContent-type: text/plain; charset=\"utf-8\"\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n");
            body.append(qp.encode(text));
        } catch (EncodingException e) {
            ok = false;
            sessionObject.error += e.getMessage();
        }
        // set to the StringBuilder so SMTP can replace() in place
        sessionObject.sentMail = body;
        if (ok) {
            SMTPClient relay = new SMTPClient();
            if (relay.sendMail(sessionObject.host, sessionObject.smtpPort, sessionObject.user, sessionObject.pass, sender, recipients.toArray(new String[recipients.size()]), sessionObject.sentMail, sessionObject.attachments, boundary)) {
                sessionObject.info += _t("Mail sent.");
                sessionObject.sentMail = null;
                sessionObject.clearAttachments();
            } else {
                ok = false;
                sessionObject.error += relay.error;
            }
        }
    }
    return ok;
}
Also used : SMTPClient(i2p.susi.webmail.smtp.SMTPClient) I2PAppContext(net.i2p.I2PAppContext) UnsupportedEncodingException(java.io.UnsupportedEncodingException) EncodingException(i2p.susi.webmail.encoding.EncodingException) ArrayList(java.util.ArrayList) Encoding(i2p.susi.webmail.encoding.Encoding)

Example 32 with I2PAppContext

use of net.i2p.I2PAppContext in project i2p.i2p by i2p.

the class WebMail method threadedStartup.

/**
 * Starts one thread to load the emails from disk,
 * and in parallel starts a second thread to connect to the POP3 server
 * (unless user clicked the 'read mail offline' at login).
 * Either could finish first, but unless the local disk cache is really big,
 * the loading will probably finish first.
 *
 * Once the POP3 connects, it waits for the disk loader to finish, and then
 * does the fetching of new emails.
 *
 * The user may view the local folder once the first (loader) thread is done.
 *
 * @since 0.9.34
 */
private static State threadedStartup(SessionObject sessionObject, boolean offline, State state, String host, int pop3PortNo, String user, String pass) {
    POP3MailBox mailbox = new POP3MailBox(host, pop3PortNo, user, pass);
    I2PAppContext ctx = I2PAppContext.getGlobalContext();
    MailCache mc;
    try {
        mc = new MailCache(ctx, mailbox, host, pop3PortNo, user, pass);
    } catch (IOException ioe) {
        Debug.debug(Debug.ERROR, "Error creating disk cache", ioe);
        sessionObject.error += ioe.toString() + '\n';
        return State.AUTH;
    }
    Folder<String> folder = new Folder<String>();
    // setElements() sorts, so configure the sorting first
    // sessionObject.folder.addSorter( SORT_ID, new IDSorter( sessionObject.mailCache ) );
    folder.addSorter(SORT_SENDER, new SenderSorter(mc));
    folder.addSorter(SORT_SUBJECT, new SubjectSorter(mc));
    folder.addSorter(SORT_DATE, new DateSorter(mc));
    folder.addSorter(SORT_SIZE, new SizeSorter(mc));
    // reverse sort, latest mail first
    // TODO get user defaults from config
    folder.setSortBy(SORT_DEFAULT, SORT_ORDER_DEFAULT);
    sessionObject.folder = folder;
    sessionObject.mailbox = mailbox;
    sessionObject.user = user;
    sessionObject.pass = pass;
    sessionObject.host = host;
    sessionObject.reallyDelete = false;
    // Thread the loading and the server connection.
    // Either could finish first.
    // With a mix of email (10KB median, 100KB average size),
    // about 20 emails per second per thread loaded.
    // thread 1: mc.loadFromDisk()
    sessionObject.mailCache = mc;
    sessionObject.isLoading = true;
    boolean ok = mc.loadFromDisk(new LoadWaiter(sessionObject));
    if (!ok)
        sessionObject.isLoading = false;
    // thread 2: mailbox.connectToServer()
    if (offline) {
        Debug.debug(Debug.DEBUG, "OFFLINE MODE");
    } else {
        sessionObject.isFetching = true;
        if (!mailbox.connectToServer(new ConnectWaiter(sessionObject))) {
            sessionObject.error += _t("Cannot connect") + '\n';
            sessionObject.isFetching = false;
        }
    }
    // wait a little while so we avoid the loading page if we can
    if (sessionObject.isLoading) {
        try {
            sessionObject.wait(5000);
        } catch (InterruptedException ie) {
            Debug.debug(Debug.DEBUG, "Interrupted waiting for load", ie);
        }
    }
    state = sessionObject.isLoading ? State.LOADING : State.LIST;
    return state;
}
Also used : I2PAppContext(net.i2p.I2PAppContext) IOException(java.io.IOException) Folder(i2p.susi.util.Folder) POP3MailBox(i2p.susi.webmail.pop3.POP3MailBox)

Example 33 with I2PAppContext

use of net.i2p.I2PAppContext in project i2p.i2p by i2p.

the class TestSwarm method main.

public static void main(String[] args) {
    if (args.length < 3) {
        System.err.println("Usage: TestSwarm samHost samPort myDestFile [peerDestFile ]*");
        return;
    }
    I2PAppContext ctx = new I2PAppContext();
    String[] files = new String[args.length - 3];
    System.arraycopy(args, 3, files, 0, files.length);
    TestSwarm swarm = new TestSwarm(ctx, args[0], args[1], args[2], files);
    swarm.startup();
}
Also used : I2PAppContext(net.i2p.I2PAppContext)

Example 34 with I2PAppContext

use of net.i2p.I2PAppContext in project i2p.i2p by i2p.

the class ConnectionOptions method error.

private static void error(String s) {
    I2PAppContext ctx = I2PAppContext.getGlobalContext();
    Log log = ctx.logManager().getLog(ConnectionOptions.class);
    log.error(s);
}
Also used : I2PAppContext(net.i2p.I2PAppContext) Log(net.i2p.util.Log)

Example 35 with I2PAppContext

use of net.i2p.I2PAppContext in project i2p.i2p by i2p.

the class PingIT method test.

@Test
public void test() throws Exception {
    I2PAppContext context = I2PAppContext.getGlobalContext();
    I2PSession session = createSession();
    ConnectionManager mgr = new ConnectionManager(context, session, new ConnectionOptions());
    for (int i = 0; i < 10; i++) {
        boolean ponged = mgr.ping(session.getMyDestination(), 2 * 1000);
        assertTrue(ponged);
    }
}
Also used : I2PAppContext(net.i2p.I2PAppContext) I2PSession(net.i2p.client.I2PSession) Test(org.junit.Test)

Aggregations

I2PAppContext (net.i2p.I2PAppContext)55 SessionKey (net.i2p.data.SessionKey)13 File (java.io.File)11 IOException (java.io.IOException)11 Properties (java.util.Properties)9 Test (org.junit.Test)7 Getopt (gnu.getopt.Getopt)5 ArrayList (java.util.ArrayList)5 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 InputStream (java.io.InputStream)3 DataFormatException (net.i2p.data.DataFormatException)3 Log (net.i2p.util.Log)3 SecureFileOutputStream (net.i2p.util.SecureFileOutputStream)3 Encoding (i2p.susi.webmail.encoding.Encoding)2 OutputStream (java.io.OutputStream)2 GeneralSecurityException (java.security.GeneralSecurityException)2 HashSet (java.util.HashSet)2 I2PSession (net.i2p.client.I2PSession)2 NamingService (net.i2p.client.naming.NamingService)2