From 6ecfedba3761465501eea1e1ce237850d5f9a6a8 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Sat, 19 Apr 2014 16:57:56 +0000
Subject: [PATCH] SusiMail:    - Increase max size of mails that are fetched in
 full,      previous limit was so small it never happened.    - Move page nav
 to top of folder view, hide if only one page    - Put message nav buttons on
 second line    - Refuse to send mail with no "to"    - Reduce default page
 size as it slows startup    - Remove unnecessary casts    - Right justify
 some input fields

---
 .../src/src/i2p/susi/webmail/Mail.java        |   6 +-
 .../src/src/i2p/susi/webmail/MailCache.java   |   7 +-
 .../src/src/i2p/susi/webmail/WebMail.java     | 103 ++++++++++--------
 .../src/i2p/susi/webmail/smtp/SMTPClient.java |  12 +-
 apps/susimail/src/susimail.properties         |   2 +-
 5 files changed, 78 insertions(+), 52 deletions(-)

diff --git a/apps/susimail/src/src/i2p/susi/webmail/Mail.java b/apps/susimail/src/src/i2p/susi/webmail/Mail.java
index b73fd134ec..aec5cd2879 100644
--- a/apps/susimail/src/src/i2p/susi/webmail/Mail.java
+++ b/apps/susimail/src/src/i2p/susi/webmail/Mail.java
@@ -63,7 +63,7 @@ public class Mail {
 	public Date date;
 	public ReadBuffer header, body;
 	public MailPart part;
-	Object[] to, cc;
+	String[] to, cc;
 
 	public String error;
 
@@ -235,12 +235,12 @@ public class Mail {
 						else if( line.startsWith( "To:" ) ) {
 							ArrayList<String> list = new ArrayList<String>();
 							getRecipientsFromList( list, line.substring( 3 ).trim(), true );
-							to = list.toArray();
+							to = list.toArray(new String[list.size()]);
 						}
 						else if( line.startsWith( "Cc:" ) ) {
 							ArrayList<String> list = new ArrayList<String>();
 							getRecipientsFromList( list, line.substring( 3 ).trim(), true );
-							cc = list.toArray();
+							cc = list.toArray(new String[list.size()]);
 						}
 					}
 				}
diff --git a/apps/susimail/src/src/i2p/susi/webmail/MailCache.java b/apps/susimail/src/src/i2p/susi/webmail/MailCache.java
index f61a1698f8..6a8482c202 100644
--- a/apps/susimail/src/src/i2p/susi/webmail/MailCache.java
+++ b/apps/susimail/src/src/i2p/susi/webmail/MailCache.java
@@ -38,6 +38,11 @@ public class MailCache {
 	private final POP3MailBox mailbox;
 	private final Hashtable<String, Mail> mails;
 	
+	/** Includes header, headers are generally 1KB to 1.5 KB,
+	 *  and bodies will compress well.
+         */
+	private static final int FETCH_ALL_SIZE = 3072;
+
 	MailCache( POP3MailBox mailbox ) {
 		this.mailbox = mailbox;
 		mails = new Hashtable<String, Mail>();
@@ -70,7 +75,7 @@ public class MailCache {
 				mail.uidl = uidl;
 				mail.size = mailbox.getSize( uidl );
 			}
-			if( mail.size < 1024 )
+			if( mail.size <= FETCH_ALL_SIZE)
 				headerOnly = false;
 			
 			boolean parseHeaders = mail.header == null;
diff --git a/apps/susimail/src/src/i2p/susi/webmail/WebMail.java b/apps/susimail/src/src/i2p/susi/webmail/WebMail.java
index 478e24126f..ab7ca30255 100644
--- a/apps/susimail/src/src/i2p/susi/webmail/WebMail.java
+++ b/apps/susimail/src/src/i2p/susi/webmail/WebMail.java
@@ -693,12 +693,13 @@ public class WebMail extends HttpServlet
 		 *  compose dialog
 		 */
 		if( sessionObject.state == STATE_NEW ) {
-			if( buttonPressed( request, CANCEL ) )
+			if (buttonPressed( request, CANCEL ) ||
+			    buttonPressed( request, LIST )) {      // LIST button not shown but we could be lost
 				sessionObject.state = STATE_LIST;
-		
-			else if( buttonPressed( request, SEND ) )
+			} else if( buttonPressed( request, SEND ) ) {
 				if( sendMail( sessionObject, request ) )
 					sessionObject.state = STATE_LIST;
+			}
 		}
 		/*
 		 * message dialog
@@ -740,7 +741,7 @@ public class WebMail extends HttpServlet
 				String uidl = null;
 				if( sessionObject.state == STATE_LIST ) {
 					int pos = getCheckedMessage( request );
-					uidl = (String)sessionObject.folder.getElementAtPosXonCurrentPage( pos );
+					uidl = sessionObject.folder.getElementAtPosXonCurrentPage( pos );
 				}
 				else {
 					uidl = sessionObject.showUIDL;
@@ -784,14 +785,14 @@ public class WebMail extends HttpServlet
 							if( mail.to != null ) {
 								for( int i = 0; i < mail.to.length; i++ ) {
 									buf.append( pad );
-									buf.append( (String)mail.to[i] );
+									buf.append(mail.to[i]);
 									pad = ", ";
 								}
 							}
 							if( mail.cc != null ) {
 								for( int i = 0; i < mail.cc.length; i++ ) {
 									buf.append( pad );
-									buf.append( (String)mail.cc[i] );
+									buf.append(mail.cc[i]);
 									pad = ", ";
 								}
 							}
@@ -817,7 +818,7 @@ public class WebMail extends HttpServlet
 								String pad = "To: ";
 								for( int i = 0; i < mail.to.length; i++ ) {
 									pw.println( pad );
-									pw.println( (String)mail.to[i] );
+									pw.println(mail.to[i]);
 									pad = "    ";
 								}
 							}
@@ -825,7 +826,7 @@ public class WebMail extends HttpServlet
 								String pad = "Cc: ";
 								for( int i = 0; i < mail.cc.length; i++ ) {
 									pw.println( pad );
-									pw.println( (String)mail.cc[i] );
+									pw.println(mail.cc[i]);
 									pad = "    ";
 								}
 							}
@@ -859,7 +860,7 @@ public class WebMail extends HttpServlet
 					int id = Integer.parseInt( show );
 					
 					if( id >= 0 && id < sessionObject.folder.getPageSize() ) {
-						String uidl = (String)sessionObject.folder.getElementAtPosXonCurrentPage( id );
+						String uidl = sessionObject.folder.getElementAtPosXonCurrentPage( id );
 						if( uidl != null ) {
 							sessionObject.state = STATE_SHOW;
 							sessionObject.showUIDL = uidl;
@@ -994,12 +995,12 @@ public class WebMail extends HttpServlet
 	private static void processMessageButtons(SessionObject sessionObject, RequestWrapper request)
 	{
 		if( buttonPressed( request, PREV ) ) {
-			String uidl = (String)sessionObject.folder.getPreviousElement( sessionObject.showUIDL );
+			String uidl = sessionObject.folder.getPreviousElement( sessionObject.showUIDL );
 			if( uidl != null )
 				sessionObject.showUIDL = uidl;
 		}
 		if( buttonPressed( request, NEXT ) ) {
-			String uidl = (String)sessionObject.folder.getNextElement( sessionObject.showUIDL );
+			String uidl = sessionObject.folder.getNextElement( sessionObject.showUIDL );
 			if( uidl != null )
 				sessionObject.showUIDL = uidl;
 		}
@@ -1010,12 +1011,12 @@ public class WebMail extends HttpServlet
 			/*
 			 * first find the next message
 			 */
-			String nextUIDL = (String)sessionObject.folder.getNextElement( sessionObject.showUIDL );
+			String nextUIDL = sessionObject.folder.getNextElement( sessionObject.showUIDL );
 			if( nextUIDL == null ) {
 				/*
 				 * nothing found? then look for the previous one
 				 */
-				nextUIDL = (String)sessionObject.folder.getPreviousElement( sessionObject.showUIDL );
+				nextUIDL = sessionObject.folder.getPreviousElement( sessionObject.showUIDL );
 				if( nextUIDL == null )
 					/*
 					 * still nothing found? then this was the last message, so go back to the folder
@@ -1117,7 +1118,7 @@ public class WebMail extends HttpServlet
 						String number = parameter.substring( 5 );
 						try {
 							int n = Integer.parseInt( number );
-							String uidl = (String)sessionObject.folder.getElementAtPosXonCurrentPage( n );
+							String uidl = sessionObject.folder.getElementAtPosXonCurrentPage( n );
 							if( uidl != null ) {
 								Mail mail = sessionObject.mailCache.getMail( uidl, MailCache.FETCH_HEADER );
 								if( mail != null ) {
@@ -1499,7 +1500,7 @@ public class WebMail extends HttpServlet
 		if( bccToSelf != null && bccToSelf.compareTo( "1" ) == 0 )
 			recipients.add( sender );
 		
-		if( recipients.isEmpty() ) {
+		if( toList.isEmpty() ) {
 			ok = false;
 			sessionObject.error += _("No recipients found.") + "<br>";
 		}
@@ -1508,12 +1509,12 @@ public class WebMail extends HttpServlet
 		
 		if( qp == null ) {
 			ok = false;
-			sessionObject.error += _("Quoted printable encoder not available.");
+			sessionObject.error += _("Internal error") + ": Quoted printable encoder not available.";
 		}
 		
 		if( hl == null ) {
 			ok = false;
-			sessionObject.error += _("Header line encoder not available.");
+			sessionObject.error += _("Internal error") + ": Header line encoder not available.";
 		}
 
 		if( ok ) {
@@ -1666,15 +1667,15 @@ public class WebMail extends HttpServlet
 		
 		out.println( "<table cellspacing=\"0\" cellpadding=\"5\">\n" +
 			// current postman hq length limits 16/12, new postman version 32/32
-			"<tr><td align=\"right\" width=\"30%\">" + _("User") + "</td><td width=\"40%\" align=\"left\"><input type=\"text\" size=\"32\" name=\"" + USER + "\" value=\"" + ( RELEASE ? "" : "test") + "\"> @mail.i2p</td></tr>\n" +
-			"<tr><td align=\"right\" width=\"30%\">" + _("Password") + "</td><td width=\"40%\" align=\"left\"><input type=\"password\" size=\"32\" name=\"pass\" value=\"" + ( RELEASE ? "" : "test") + "\"></td></tr>\n");
+			"<tr><td align=\"right\" width=\"30%\">" + _("User") + "</td><td width=\"40%\" align=\"left\"><input type=\"text\" size=\"32\" name=\"" + USER + "\" value=\"" + "\"> @mail.i2p</td></tr>\n" +
+			"<tr><td align=\"right\" width=\"30%\">" + _("Password") + "</td><td width=\"40%\" align=\"left\"><input type=\"password\" size=\"32\" name=\"pass\" value=\"" + "\"></td></tr>\n");
 		// which is better?
 		//if (!fixed) {
 		if (true) {
 		    out.println(
 			"<tr><td align=\"right\" width=\"30%\">" + _("Host") + "</td><td width=\"40%\" align=\"left\"><input type=\"text\" size=\"32\" name=\"" + HOST +"\" value=\"" + host + "\"" + ( fixed ? " disabled" : "" ) + "></td></tr>\n" +
-			"<tr><td align=\"right\" width=\"30%\">" + _("POP3-Port") + "</td><td width=\"40%\" align=\"left\"><input type=\"text\" size=\"5\" name=\"" + POP3 +"\" value=\"" + pop3 + "\"" + ( fixed ? " disabled" : "" ) + "></td></tr>\n" +
-			"<tr><td align=\"right\" width=\"30%\">" + _("SMTP-Port") + "</td><td width=\"40%\" align=\"left\"><input type=\"text\" size=\"5\" name=\"" + SMTP +"\" value=\"" + smtp + "\"" + ( fixed ? " disabled" : "" ) + "></td></tr>\n");
+			"<tr><td align=\"right\" width=\"30%\">" + _("POP3 Port") + "</td><td width=\"40%\" align=\"left\"><input type=\"text\" style=\"text-align: right;\" size=\"5\" name=\"" + POP3 +"\" value=\"" + pop3 + "\"" + ( fixed ? " disabled" : "" ) + "></td></tr>\n" +
+			"<tr><td align=\"right\" width=\"30%\">" + _("SMTP Port") + "</td><td width=\"40%\" align=\"left\"><input type=\"text\" style=\"text-align: right;\" size=\"5\" name=\"" + SMTP +"\" value=\"" + smtp + "\"" + ( fixed ? " disabled" : "" ) + "></td></tr>\n");
 		}
 		out.println(
 			"<tr><td></td><td align=\"left\">" + button( LOGIN, _("Login") ) + " <input class=\"cancel\" type=\"reset\" value=\"" + _("Reset") + "\"></td></tr>\n" +
@@ -1694,14 +1695,33 @@ public class WebMail extends HttpServlet
 			out.println( "<p class=\"error\">" + _("Really delete the marked messages?") + " " + button( REALLYDELETE, _("Yes, really delete them!") ) + "</p>" );
 		}
 		out.println( button( NEW, _("New") ) + spacer +
-			button( REPLY, _("Reply") ) +
-			button( REPLYALL, _("Reply All") ) +
-			button( FORWARD, _("Forward") ) + spacer +
+			// In theory, these are valid and will apply to the first checked message,
+			// but that's not obvious and did it work?
+			//button( REPLY, _("Reply") ) +
+			//button( REPLYALL, _("Reply All") ) +
+			//button( FORWARD, _("Forward") ) + spacer +
 			button( DELETE, _("Delete") ) + spacer +
 			button( REFRESH, _("Check Mail") ) + spacer);
 		if (Config.hasConfigFile())
 			out.println(button( RELOAD, _("Reload Config") ) + spacer);
-		out.println(button( LOGOUT, _("Logout") ) + "<table id=\"mailbox\" cellspacing=\"0\" cellpadding=\"5\">\n" +
+		out.println(button( LOGOUT, _("Logout") ));
+
+		if (sessionObject.folder.getPages() > 1) {
+			out.println(
+				"<br>" +
+				( sessionObject.folder.isFirstPage() ?
+									button2( FIRSTPAGE, _("First") ) + button2( PREVPAGE, _("Previous") ) :
+									button( FIRSTPAGE, _("First") ) + button( PREVPAGE, _("Previous") ) ) +
+				" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
+				_("Page {0} of {1}", sessionObject.folder.getCurrentPage(), sessionObject.folder.getPages()) +
+				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " +
+				( sessionObject.folder.isLastPage() ? 
+									button2( NEXTPAGE, _("Next") ) + button2( LASTPAGE, _("Last") ) :
+									button( NEXTPAGE, _("Next") ) + button( LASTPAGE, _("Last") ) )
+				);
+		}
+
+		out.println("<table id=\"mailbox\" cellspacing=\"0\" cellpadding=\"5\">\n" +
 			"<tr><td colspan=\"8\"><hr></td></tr>\n<tr>" +
 			thSpacer + "<th>" + sortHeader( SORT_SENDER, _("Sender"), sessionObject.imgPath ) + "</th>" +
 			thSpacer + "<th>" + sortHeader( SORT_SUBJECT, _("Subject"), sessionObject.imgPath ) + "</th>" +
@@ -1717,7 +1737,7 @@ public class WebMail extends HttpServlet
 			String link = "<a href=\"" + myself + "?" + SHOW + "=" + i + "\">";
 			
 			boolean idChecked = false;
-			String checkId = sessionObject.pageChanged ? null : (String)request.getParameter( "check" + i );
+			String checkId = sessionObject.pageChanged ? null : request.getParameter( "check" + i );
 			
 			if( checkId != null && checkId.compareTo( "1" ) == 0 )
 				idChecked = true;
@@ -1729,12 +1749,12 @@ public class WebMail extends HttpServlet
 			if( sessionObject.clear )
 				idChecked = false;
 
-			Debug.debug( Debug.DEBUG, "check" + i + ": checkId=" + checkId + ", idChecked=" + idChecked + ", pageChanged=" + sessionObject.pageChanged +
-					", markAll=" + sessionObject.markAll +
-					", invert=" + sessionObject.invert +
-					", clear=" + sessionObject.clear );
+			//Debug.debug( Debug.DEBUG, "check" + i + ": checkId=" + checkId + ", idChecked=" + idChecked + ", pageChanged=" + sessionObject.pageChanged +
+			//		", markAll=" + sessionObject.markAll +
+			//		", invert=" + sessionObject.invert +
+			//		", clear=" + sessionObject.clear );
 			out.println( "<tr class=\"list" + bg + "\"><td><input type=\"checkbox\" class=\"optbox\" name=\"check" + i + "\" value=\"1\"" + 
-					( idChecked ? "checked" : "" ) + ">" + ( RELEASE ? "" : "" + i ) + "</td><td>" +
+					( idChecked ? "checked" : "" ) + ">" + "</td><td>" +
 					link + mail.shortSender + "</a></td><td>&nbsp;</td><td>" + link + mail.shortSubject + "</a></td><td>&nbsp;</td><td>" +
 					 mail.localFormattedDate + "</td><td>&nbsp;</td><td>" + ngettext("1 Byte", "{0} Bytes", mail.size) + "</td></tr>" );
 			bg = 1 - bg;
@@ -1744,17 +1764,9 @@ public class WebMail extends HttpServlet
 				button( MARKALL, _("Mark All") ) +
 				button( INVERT, _("Invert Selection") ) +
 				button( CLEAR, _("Clear") ) +
-				"<br>" +
-				( sessionObject.folder.isFirstPage() ?
-										 button2( FIRSTPAGE, _("First") ) + button2( PREVPAGE, _("Previous") ) :
- 										 button( FIRSTPAGE, _("First") ) + button( PREVPAGE, _("Previous") ) ) +
-				" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + _("Page {0} of {1}", sessionObject.folder.getCurrentPage(), sessionObject.folder.getPages()) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " +
-				( sessionObject.folder.isLastPage() ? 
-														   button2( NEXTPAGE, _("Next") ) + button2( LASTPAGE, _("Last") ) :
-														   button( NEXTPAGE, _("Next") ) + button( LASTPAGE, _("Last") ) ) +
-														   
-				"<br>" +
-				_("Pagesize:") + "&nbsp;<input type=\"text\" name=\"" + PAGESIZE + "\" size=\"4\" value=\"" +  sessionObject.folder.getPageSize() + "\">" +
+				"<br>");
+		out.println(
+				_("Page Size:") + "&nbsp;<input type=\"text\" style=\"text-align: right;\" name=\"" + PAGESIZE + "\" size=\"4\" value=\"" +  sessionObject.folder.getPageSize() + "\">" +
 				button( SETPAGESIZE, _("Set") ) );
 	}
 	/**
@@ -1783,9 +1795,10 @@ public class WebMail extends HttpServlet
 			button( REPLYALL, _("Reply All") ) +
 			button( FORWARD, _("Forward") ) + spacer +
 			button( DELETE, _("Delete") ) + spacer +
-			( sessionObject.folder.isFirstElement( sessionObject.showUIDL ) ? button2( PREV, _("Previous") ) : button( PREV, _("Previous") ) ) +
-			( sessionObject.folder.isLastElement( sessionObject.showUIDL ) ? button2( NEXT, _("Next") ) : button( NEXT, _("Next") ) ) + spacer +
-			button( LIST, _("Back to Folder") ) + spacer);
+			"<br>" +
+			( sessionObject.folder.isFirstElement( sessionObject.showUIDL ) ? button2( PREV, _("Previous") ) : button( PREV, _("Previous") ) ) + spacer +
+			button( LIST, _("Back to Folder") ) + spacer +
+			( sessionObject.folder.isLastElement( sessionObject.showUIDL ) ? button2( NEXT, _("Next") ) : button( NEXT, _("Next") ) ));
 		//if (Config.hasConfigFile())
 		//	out.println(button( RELOAD, _("Reload Config") ) + spacer);
 		//out.println(button( LOGOUT, _("Logout") ) );
diff --git a/apps/susimail/src/src/i2p/susi/webmail/smtp/SMTPClient.java b/apps/susimail/src/src/i2p/susi/webmail/smtp/SMTPClient.java
index 6d63ff854c..0a2bd70e5a 100644
--- a/apps/susimail/src/src/i2p/susi/webmail/smtp/SMTPClient.java
+++ b/apps/susimail/src/src/i2p/susi/webmail/smtp/SMTPClient.java
@@ -56,9 +56,13 @@ public class SMTPClient {
 		lastResponse = "";
 	}
 	
-	public int sendCmd( String cmd )
+	/**
+	 *  @param cmd may be null
+	 *  @return result code or 0 for failure
+	 */
+	private int sendCmd( String cmd )
 	{
-		Debug.debug( Debug.DEBUG, "sendCmd(" + cmd +")" );
+		Debug.debug( Debug.DEBUG, "SMTP sendCmd(" + cmd +")" );
 		
 		if( socket == null )
 			return 0;
@@ -110,6 +114,10 @@ public class SMTPClient {
 		}
 		return result;
 	}
+
+	/**
+	 *  @return success
+	 */
 	public boolean sendMail( String host, int port, String user, String pass, String sender, Object[] recipients, String body )
 	{
 		boolean mailSent = false;
diff --git a/apps/susimail/src/susimail.properties b/apps/susimail/src/susimail.properties
index 68070226b9..6cd44eb4fb 100644
--- a/apps/susimail/src/susimail.properties
+++ b/apps/susimail/src/susimail.properties
@@ -11,7 +11,7 @@ susimail.fast.start=true
 susimail.sender.fixed=true
 susimail.sender.domain=mail.i2p
 
-susimail.pager.pagesize=40
+susimail.pager.pagesize=20
 
 susimail.composer.cols=80
 susimail.composer.rows=15
-- 
GitLab