initial vanilla import of susimail 0.13 (no build script yet)

This commit is contained in:
jrandom
2005-02-17 20:08:53 +00:00
committed by zzz
parent dca09d96b3
commit 40f4b47b87
32 changed files with 5899 additions and 0 deletions

2
apps/susimail/readme.txt Normal file
View File

@ -0,0 +1,2 @@
The src/ dir contains susimail 0.13 retrieved from http://susi.i2p/ on 2005/02/17
The contents are released under GPL. Please see http://susi.i2p/ for more info

BIN
apps/susimail/src/3down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 415 B

BIN
apps/susimail/src/3up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 406 B

340
apps/susimail/src/LICENSE Normal file
View File

@ -0,0 +1,340 @@
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Library General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program 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 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Library General
Public License instead of this License.

View File

@ -0,0 +1,20 @@
susimail.encodings=i2p.susi.webmail.encoding.HeaderLine;i2p.susi.webmail.encoding.QuotedPrintable;i2p.susi.webmail.encoding.Base64;i2p.susi.webmail.encoding.SevenBit;i2p.susi.webmail.encoding.EightBit;i2p.susi.webmail.encoding.HTML
susimail.host=localhost
susimail.ports.fixed=true
susimail.ports.pop3=7660
susimail.ports.smtp=7659
susimail.fast.start=true
susimail.sender.fixed=true
susimail.sender.domain=mail.i2p
susimail.pager.pagesize=10
susimail.composer.cols=80
susimail.composer.rows=15
susimail.composer.bcc.to.self=true
susimail.date.format=MM/dd/yyyy HH:mm:ss

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
<web-app>
<display-name>susimail</display-name>
<servlet>
<servlet-name>SusiMail</servlet-name>
<servlet-class>i2p.susi.webmail.WebMail</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SusiMail</servlet-name>
<url-pattern>/susimail</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>

View File

@ -0,0 +1,10 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="refresh" content="0;url=susimail" />
<title>Susimail</title>
</head>
<body>
<a href="susimail">Enter</a>
</body>
</html>

View File

@ -0,0 +1,43 @@
/*
* Created on Nov 4, 2004
*
* This file is part of susimail project, see http://susi.i2p/
*
* Copyright (C) 2004-2005 <susi23@mail.i2p>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Revision: 1.2 $
*/
package i2p.susi.debug;
/**
* @author susi23
*/
public class Debug {
public static final int ERROR = 1;
public static final int DEBUG = 2;
private static int level = ERROR;
public static void setLevel( int newLevel )
{
level = newLevel;
}
public static void debug( int msgLevel, String msg )
{
if( msgLevel <= level )
System.err.println( msg );
}
}

View File

@ -0,0 +1,128 @@
/*
* Created on Nov 15, 2004
*
* This file is part of susimail project, see http://susi.i2p/
*
* Copyright (C) 2004-2005 <susi23@mail.i2p>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Revision: 1.4 $
*/
package i2p.susi.util;
import i2p.susi.debug.Debug;
import java.io.FileInputStream;
import java.util.Properties;
/**
* @author susi
*/
public class Config {
private static Properties properties = null, config = null;
private static String configPrefix = null;
/**
*
* @param name
* @return
*/
public static String getProperty( String name )
{
if( configPrefix != null )
name = configPrefix + name;
String result = null;
if( properties == null ) {
reloadConfiguration();
}
result = System.getProperty( name );
if( result != null )
return result;
result = config.getProperty( name );
if( result != null )
return result;
result = properties.getProperty( name );
return result;
}
/**
*
*
*/
public static void reloadConfiguration()
{
properties = new Properties();
config = new Properties();
try {
properties.load( Config.class.getResourceAsStream( "/susimail.properties" ) );
} catch (Exception e) {
Debug.debug( Debug.DEBUG, "Could not open WEB-INF/classes/susimail.properties (possibly in jar), reason: " + e.getMessage() );
}
try {
config.load( new FileInputStream( "susimail.config" ) );
} catch (Exception e) {
Debug.debug( Debug.DEBUG, "Could not open susimail.config, reason: " + e.getMessage() );
}
}
/**
*
* @param name
* @param defaultValue
* @return
*/
public static String getProperty( String name, String defaultValue )
{
String result = getProperty( name );
return result != null ? result : defaultValue;
}
/**
*
* @param name
* @param defaultValue
* @return
*/
public static int getProperty( String name, int defaultValue )
{
int result = defaultValue;
String str = getProperty( name );
if( str != null ) {
try {
result = Integer.parseInt( str );
}
catch( NumberFormatException nfe ) {
result = defaultValue;
}
}
return result;
}
/**
*
* @param prefix
*/
public static void setPrefix( String prefix )
{
configPrefix = prefix.endsWith( "." ) ? prefix : prefix + ".";
}
}

View File

@ -0,0 +1,452 @@
/*
* Created on Nov 23, 2004
*
* This file is part of susimail project, see http://susi.i2p/
*
* Copyright (C) 2004-2005 <susi23@mail.i2p>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Revision: 1.2 $
*/
package i2p.susi.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Hashtable;
import java.util.Iterator;
/**
* Folder object manages a array Object[] to support
* paging and sorting.
*
* You create a folder object, set the contents with setElements(),
* add Comparators with addSorter(), choose one with sortBy() and
* and then fetch the content of the current page with
* currentPageIterator().
*
* @author susi
*/
public class Folder {
public static final String PAGESIZE = "pager.pagesize";
public static final int DEFAULT_PAGESIZE = 10;
public static final boolean DOWN = false;
public static final boolean UP = true;
private int pages, pageSize, currentPage;
private Object[] unsortedElements, elements;
private Hashtable sorter;
private boolean sortingDirection;
Comparator currentSorter;
public Folder()
{
pages = 1;
pageSize = 0;
currentPage = 1;
unsortedElements = null;
sorter = new Hashtable();
sortingDirection = UP;
currentSorter = null;
}
/**
* Returns the current page.
*
* @return Returns the current page.
*/
public int getCurrentPage() {
return currentPage;
}
/**
* Sets the current page to the given parameter.
*
* @param currentPage The current page to set.
*/
public void setCurrentPage(int currentPage) {
if( currentPage >= 1 && currentPage <= pages )
this.currentPage = currentPage;
}
/**
* Returns the size of the folder.
*
* @return Returns the size of the folder.
*/
public int getSize() {
return elements != null ? elements.length : 0;
}
/**
* Returns the number of pages in the folder.
* @return Returns the number of pages.
*/
public int getPages() {
return pages;
}
/**
* Returns page size. If no page size has been set, it returns property @link PAGESIZE.
* If no property is set @link DEFAULT_PAGESIZE is returned.
*
* @return Returns the pageSize.
*/
public int getPageSize() {
return pageSize > 0 ? pageSize : Config.getProperty( PAGESIZE, DEFAULT_PAGESIZE );
}
/**
* Set page size.
*
* @param pageSize The page size to set.
*/
public void setPageSize(int pageSize) {
if( pageSize > 0 )
this.pageSize = pageSize;
update();
}
/**
* Creates a copy of an array by copying its elements.
*
* @param source Array to copy.
* @return Copy of source.
*/
private Object[] copyArray( Object[] source )
{
Object[] destination = new Object[source.length];
for( int i = 0; i < source.length; i++ )
destination[i] = source[i];
return destination;
}
/**
* Recalculates variables.
*/
private void update() {
if( elements != null ) {
pages = elements.length / getPageSize();
if( pages * getPageSize() < elements.length )
pages++;
if( currentPage > pages )
currentPage = pages;
}
else {
pages = 1;
currentPage = 1;
}
}
/**
* Sorts the elements according the order given by @link addSorter()
* and @link sortBy().
*/
private void sort()
{
if( currentSorter != null ) {
elements = copyArray( unsortedElements );
Arrays.sort( elements, currentSorter );
}
else {
elements = unsortedElements;
}
}
/**
* Set the array of objects the folder should manage.
*
* @param elements Array of Objects.
*/
public void setElements( Object[] elements )
{
this.unsortedElements = elements;
if( currentSorter != null )
sort();
else
this.elements = elements;
update();
}
/**
* Returns an iterator containing the elements on the current page.
* @return Iterator containing the elements on the current page.
*/
public Iterator currentPageIterator()
{
ArrayList list = new ArrayList();
if( elements != null ) {
int pageSize = getPageSize();
int offset = ( currentPage - 1 ) * pageSize;
int step = 1;
if( sortingDirection == DOWN ) {
offset = elements.length - offset - 1;
step = -1;
}
for( int i = 0; i < pageSize && offset >= 0 && offset < elements.length; i++ ) {
list.add( elements[offset] );
offset += step;
}
}
return list.iterator();
}
/**
* Turns folder to next page.
*/
public void nextPage()
{
currentPage++;
if( currentPage > pages )
currentPage = pages;
}
/**
* Turns folder to previous page.
*/
public void previousPage()
{
currentPage--;
if( currentPage < 1 )
currentPage = 1;
}
/**
* Sets folder to display first page.
*/
public void firstPage()
{
currentPage = 1;
}
/**
* Sets folder to display last page.
*/
public void lastPage()
{
currentPage = pages;
}
/**
* Adds a new sorter to the folder. You can sort the folder by
* calling sortBy() and choose the given id there.
*
* @param id ID to identify the Comparator with @link sortBy()
* @param sorter a Comparator to sort the Array given by @link setElements()
*/
public void addSorter( String id, Comparator sorter )
{
this.sorter.put( id, sorter );
}
/**
* Activates sorting by the choosen Comparator. The id must
* match the one, which the Comparator has been stored in the
* folder with @link addSorter().
*
* @param id ID to identify the Comparator stored with @link addSorter()
*/
public void sortBy( String id )
{
currentSorter = (Comparator)sorter.get( id );
sort();
}
/**
* Returns the element on the current page on the given position.
*
* @param Position of the element on the current page.
* @return Element on the current page on the given position.
*/
public Object getElementAtPosXonCurrentPage( int x )
{
Object result = null;
if( elements != null ) {
int pageSize = getPageSize();
int offset = ( currentPage - 1 ) * pageSize;
int step = 1;
if( sortingDirection == DOWN ) {
offset = elements.length - offset - 1;
step = -1;
}
offset += x * step;
if( offset >= 0 && offset < elements.length )
result = elements[offset];
}
return result;
}
/**
* Sets the sorting direction of the folder.
*
* @param direction @link UP or @link DOWN
*/
public void setSortingDirection( boolean direction )
{
sortingDirection = direction;
}
/**
* Returns the first element of the sorted folder.
*
* @return First element.
*/
public Object getFirstElement()
{
/*
* sorting direction is taken into account from getElement
*/
return elements == null ? null : getElement( 0 );
}
/**
* Returns the last element of the sorted folder.
*
* @return Last element.
*/
public Object getLastElement()
{
/*
* sorting direction is taken into account from getElement
*/
return elements == null ? null : getElement( elements.length - 1 );
}
/**
* Gets index of an element in the array regardless of sorting direction.
*
* @param element
* @return
*/
private int getIndexOf( Object element )
{
if( elements != null ) {
for( int i = 0; i < elements.length; i++ )
if( elements[i].equals( element ) )
return i;
}
return -1;
}
/**
* Retrieves the next element in the sorted array.
* Sorting direction is taken into account.
*
* @param element
* @return
*/
public Object getNextElement( Object element )
{
Object result = null;
int i = getIndexOf( element );
if( i != -1 && elements != null ) {
i += sortingDirection == UP ? 1 : -1;
if( i >= 0 && i < elements.length )
result = elements[i];
}
return result;
}
/**
* Retrieves the previous element in the sorted array.
* Sorting direction is taken into account.
*
* @param element
* @return
*/
public Object getPreviousElement( Object element )
{
Object result = null;
int i = getIndexOf( element );
if( i != -1 && elements != null ) {
i += sortingDirection == DOWN ? 1 : -1;
if( i >= 0 && i < elements.length )
result = elements[i];
}
return result;
}
/**
* Retrieves element at index i. Depends on sorting direction.
*
* @param i
* @return
*/
private Object getElement( int i )
{
Object result = null;
if( elements != null ) {
if( sortingDirection == DOWN )
i = elements.length - i - 1;
result = elements[i];
}
return result;
}
/**
* Returns true, if folder shows points to the last page.
*
* @return
*/
public boolean isLastPage()
{
return currentPage == pages;
}
/**
* Returns true, if folder shows points to the first page.
*
* @return
*/
public boolean isFirstPage()
{
return currentPage == 1;
}
/**
* Returns true, if elements.equals( lastElementOfTheSortedArray ).
* The sorting direction influences which element is taken for comparison.
*
* @param element
* @return
*/
public boolean isLastElement( Object element )
{
if( elements == null )
return false;
return elements[ sortingDirection == DOWN ? 0 : elements.length - 1 ].equals( element );
}
/**
* Returns true, if elements.equals( firstElementOfTheSortedArray ).
* The sorting direction influences which element is taken for comparison.
*
* @param element
* @return
*/
public boolean isFirstElement( Object element )
{
if( elements == null )
return false;
return elements[ sortingDirection == UP ? 0 : elements.length - 1 ].equals( element );
}
}

View File

@ -0,0 +1,60 @@
/*
* Created on Nov 12, 2004
*
* This file is part of susimail project, see http://susi.i2p/
*
* Copyright (C) 2004-2005 <susi23@mail.i2p>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Revision: 1.2 $
*/
package i2p.susi.util;
/**
* @author susi
*/
public class HexTable {
public static String[] table = null;
static {
table = new String[256];
for( int i = 0; i < 256; i++ ) {
String str = intToHex( i );
if( str.length() == 1 )
str = "0" + str;
table[i] = "=" + str;
}
}
private static String intToHex( int b )
{
if( b == 0 )
return "0";
else {
String str = "";
while( b > 0 ) {
byte c = (byte)(b % 16);
if( c < 10 )
c += '0';
else
c += 'A' - 10;
str = "" + (char)c + str;
b = (byte)(b / 16);
}
return str;
}
}
}

View File

@ -0,0 +1,38 @@
/*
* Created on Nov 15, 2004
*
* This file is part of susimail project, see http://susi.i2p/
*
* Copyright (C) 2004-2005 <susi23@mail.i2p>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Revision: 1.2 $
*/
package i2p.susi.util;
/**
* @author susi
*/
public class ReadBuffer {
public byte content[];
public int length, offset;
public String toString()
{
return content != null ? new String( content, offset, length ) : "";
}
}

View File

@ -0,0 +1,97 @@
/*
* Created on 01.12.2004
*
* This file is part of susimail project, see http://susi.i2p/
*
* Copyright (C) 2004-2005 <susi23@mail.i2p>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Revision: 1.4 $
*/
package i2p.susi.webmail;
import i2p.susi.util.ReadBuffer;
/**
* @author user
*/
public class Attachment {
private String fileName, contentType, transferEncoding;
private ReadBuffer buffer;
private String data;
/**
* @return Returns the fileName.
*/
public String getFileName() {
return fileName;
}
/**
* @param fileName The fileName to set.
*/
public void setFileName(String fileName) {
this.fileName = fileName;
}
/**
* @return Returns the buffer.
*/
public ReadBuffer getBuffer() {
return buffer;
}
/**
* @param buffer The buffer to set.
*/
public void setBuffer(ReadBuffer buffer) {
this.buffer = buffer;
}
/**
* @return
*/
public String getTransferEncoding() {
// TODO Auto-generated method stub
return transferEncoding;
}
/**
* @return
*/
public String getContentType() {
// TODO Auto-generated method stub
return contentType;
}
/**
* @param contentType The contentType to set.
*/
public void setContentType(String contentType) {
this.contentType = contentType;
}
/**
* @param transferEncoding The transferEncoding to set.
*/
public void setTransferEncoding(String transferEncoding) {
this.transferEncoding = transferEncoding;
}
/**
* @param string
*/
public void setData(String data ) {
this.data = data;
}
/**
* @return Returns the data.
*/
public String getData() {
return data;
}
}

View File

@ -0,0 +1,240 @@
/*
* Created on Nov 9, 2004
*
* This file is part of susimail project, see http://susi.i2p/
*
* Copyright (C) 2004-2005 <susi23@mail.i2p>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Revision: 1.5 $
*/
package i2p.susi.webmail;
import i2p.susi.debug.Debug;
import i2p.susi.util.Config;
import i2p.susi.util.ReadBuffer;
import i2p.susi.webmail.encoding.Encoding;
import i2p.susi.webmail.encoding.EncodingFactory;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Locale;
/**
* data structure to hold a single message, mostly used with folder view and sorting
*
* @author susi
*/
public class Mail {
public static final String DATEFORMAT = "date.format";
public static final String unknown = "unknown";
public int id, size;
public String sender, reply, subject, dateString,
formattedSender, formattedSubject, formattedDate,
shortSender, shortSubject, quotedDate, uidl;
public Date date;
public ReadBuffer header, body;
public MailPart part;
Object[] to, cc;
public String error;
public boolean markForDeletion;
public boolean deleted;
public Mail() {
id = 0;
size = 0;
formattedSender = unknown;
formattedSubject = unknown;
formattedDate = unknown;
shortSender = unknown;
shortSubject = unknown;
quotedDate = unknown;
error = "";
}
/**
*
* @param address
* @return
*/
public static boolean validateAddress( String address )
{
if( address == null || address.length() == 0 )
return false;
address = address.trim();
if( address.indexOf( "\n" ) != -1 ||
address.indexOf( "\r" ) != -1 )
return false;
String[] tokens = address.split( "[ \t]+" );
int addresses = 0;
for( int i = 0; i < tokens.length; i++ ) {
if( tokens[i].matches( "^[^@< \t]+@[^> \t]+$" ) ||
tokens[i].matches( "^<[^@< \t]+@[^> \t]+>$" ) )
addresses++;
}
return addresses == 1;
}
/**
* @param address
* @return
*/
public static String getAddress(String address )
{
String[] tokens = address.split( "[ \t]+" );
for( int i = 0; i < tokens.length; i++ ) {
if( tokens[i].matches( "^[^@< \t]+@[^> \t]+$" ) )
return "<" + tokens[i] + ">";
if( tokens[i].matches( "^<[^@< \t]+@[^> \t]+>$" ) )
return tokens[i];
}
return null;
}
public static boolean getRecipientsFromList( ArrayList recipients, String text, boolean ok )
{
if( text != null && text.length() > 0 ) {
String[] ccs = text.split( "," );
for( int i = 0; i < ccs.length; i++ ) {
String recipient = ccs[i].trim();
if( validateAddress( recipient ) ) {
String str = getAddress( recipient );
if( str != null && str.length() > 0 ) {
recipients.add( str );
}
else {
ok = false;
}
}
else {
ok = false;
}
}
}
return ok;
}
public static void appendRecipients( StringBuffer buf, ArrayList recipients, String prefix )
{
for( Iterator it = recipients.iterator(); it.hasNext(); ) {
buf.append( prefix );
prefix ="\t";
buf.append( (String)it.next() );
buf.append( "\r\n" );
}
}
public void parseHeaders()
{
DateFormat dateFormatter = new SimpleDateFormat( Config.getProperty( DATEFORMAT, "mm/dd/yyyy HH:mm:ss" ) );
DateFormat mailDateFormatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH );
error = "";
if( header != null ) {
boolean ok = true;
Encoding html = EncodingFactory.getEncoding( "HTML" );
if( html == null ) {
error += "HTML encoder not found.<br>";
ok = false;
}
Encoding hl = EncodingFactory.getEncoding( "HEADERLINE" );
if( hl == null ) {
error += "Header line encoder not found.<br>";
ok = false;
}
if( ok ) {
try {
ReadBuffer decoded = hl.decode( header );
BufferedReader reader = new BufferedReader( new InputStreamReader( new ByteArrayInputStream( decoded.content, decoded.offset, decoded.length ), "ISO-8859-1" ) );
String line;
while( ( line = reader.readLine() ) != null ) {
if( line.length() == 0 )
break;
if( line.startsWith( "From:" ) ) {
sender = line.substring( 5 ).trim();
formattedSender = getAddress( sender );
shortSender = formattedSender.trim();
if( shortSender.length() > 40 ) {
shortSender = shortSender.substring( 0, 37 ).trim() + "...";
}
shortSender = html.encode( shortSender );
}
else if( line.startsWith( "Date:" ) ) {
dateString = line.substring( 5 ).trim();
try {
date = mailDateFormatter.parse( dateString );
formattedDate = dateFormatter.format( date );
quotedDate = html.encode( dateString );
}
catch (ParseException e) {
date = null;
e.printStackTrace();
}
}
else if( line.startsWith( "Subject:" ) ) {
subject = line.substring( 8 ).trim();
formattedSubject = subject;
shortSubject = formattedSubject;
if( formattedSubject.length() > 60 )
shortSubject = formattedSubject.substring( 0, 57 ).trim() + "...";
shortSubject = html.encode( shortSubject );
}
else if( line.toLowerCase().startsWith( "Reply-To:" ) ) {
reply = Mail.getAddress( line.substring( 9 ).trim() );
}
else if( line.startsWith( "To:" ) ) {
ArrayList list = new ArrayList();
Mail.getRecipientsFromList( list, line.substring( 3 ).trim(), true );
to = list.toArray();
}
else if( line.startsWith( "Cc:" ) ) {
ArrayList list = new ArrayList();
Mail.getRecipientsFromList( list, line.substring( 3 ).trim(), true );
cc = list.toArray();
}
}
}
catch( Exception e ) {
error += "Error parsing mail header: " + e.getClass().getName() + "<br>";
}
}
}
}
}

View File

@ -0,0 +1,103 @@
/*
* Created on Nov 23, 2004
*
* This file is part of susimail project, see http://susi.i2p/
*
* Copyright (C) 2004-2005 <susi23@mail.i2p>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Revision: 1.3 $
*/
package i2p.susi.webmail;
import java.util.Hashtable;
import i2p.susi.debug.Debug;
import i2p.susi.webmail.pop3.POP3MailBox;
/**
* @author user
*/
public class MailCache {
public static final boolean FETCH_HEADER = true;
public static final boolean FETCH_ALL = false;
private POP3MailBox mailbox;
private String error;
private Hashtable mails;
private Object synchronizer;
MailCache( POP3MailBox mailbox ) {
this.mailbox = mailbox;
mails = new Hashtable();
synchronizer = new Object();
}
/**
* Fetch any needed data from pop3 server.
*
* @param id message id to get
* @param headerOnly fetch only header lines?
* @return
*/
public Mail getMail( String uidl, boolean headerOnly ) {
Mail mail = null, newMail = null;
if( mailbox != null ) {
/*
* synchronize update to hashtable
*/
synchronized( synchronizer ) {
mail = (Mail)mails.get( uidl );
if( mail == null ) {
newMail = new Mail();
mails.put( uidl, newMail );
}
}
if( mail == null ) {
mail = newMail;
mail.uidl = uidl;
mail.size = mailbox.getSize( uidl );
}
if( mail.size < 1024 )
headerOnly = false;
boolean parseHeaders = mail.header == null;
if( headerOnly ) {
if( mail.header == null )
mail.header = mailbox.getHeader( uidl );
}
else {
if( mail.body == null ) {
mail.body = mailbox.getBody( uidl );
if( mail.body != null ) {
mail.header = mail.body;
MailPart.parse( mail );
}
}
}
if( parseHeaders && mail.header != null )
mail.parseHeaders();
}
if( mail != null && mail.deleted )
mail = null;
return mail;
}
}

View File

@ -0,0 +1,260 @@
/*
* Created on 07.11.2004
*
* This file is part of susimail project, see http://susi.i2p/
*
* Copyright (C) 2004-2005 <susi23@mail.i2p>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Revision: 1.4 $
*/
package i2p.susi.webmail;
import i2p.susi.util.ReadBuffer;
import i2p.susi.webmail.encoding.EncodingFactory;
import java.util.ArrayList;
/**
* @author susi23
*/
public class MailPart {
public String headerLines[], type, boundary, encoding, name,
filename, description, disposition, charset, version;
public int beginBody, begin, end;
public ArrayList parts = null;
public boolean multipart = false, message = false;
public ReadBuffer buffer = null;
public void parse( ReadBuffer readBuffer )
{
parse( readBuffer, readBuffer.offset, readBuffer.length );
}
public void parse( ReadBuffer readBuffer, int offset, int length )
{
begin = offset;
end = offset + length;
buffer = readBuffer;
if( parts == null )
parts = new ArrayList();
else
parts.clear();
/*
* parse header lines
*/
beginBody = end;
for( int i = begin; i < end - 4; i++ )
if( buffer.content[i] == '\r' &&
buffer.content[i+1] == '\n' &&
buffer.content[i+2] == '\r' &&
buffer.content[i+3] == '\n' ) {
beginBody = i + 2;
break;
}
ReadBuffer decodedHeaders = null;
try {
decodedHeaders = EncodingFactory.getEncoding( "HEADERLINE" ).decode( buffer.content, begin, beginBody - begin );
}
catch (Exception e) {
e.printStackTrace();
}
headerLines = new String( decodedHeaders.content, decodedHeaders.offset, decodedHeaders.length ).split( "\r\n" );
for( int i = 0; i < headerLines.length; i++ )
{
if( headerLines[i].toLowerCase().startsWith( "content-transfer-encoding: " ) ) {
encoding = getFirstAttribute( headerLines[i] ).toLowerCase();
}
else if( headerLines[i].toLowerCase().startsWith( "content-disposition: " ) ) {
disposition = getFirstAttribute( headerLines[i] ).toLowerCase();
String str;
str = getHeaderLineAttribute( headerLines[i], "filename" );
if( str != null )
name = str;
}
else if( headerLines[i].toLowerCase().startsWith( "content-type: " ) ) {
type = getFirstAttribute( headerLines[i] ).toLowerCase();
/*
* extract boundary, name and charset from content type
*/
String str;
str = getHeaderLineAttribute( headerLines[i], "boundary" );
if( str != null )
boundary = str;
if( type != null && type.startsWith( "multipart" ) && boundary != null )
multipart = true;
if( type != null && type.startsWith( "message" ) )
message = true;
str = getHeaderLineAttribute( headerLines[i], "name" );
if( str != null )
name = str;
str = getHeaderLineAttribute( headerLines[i], "charset" );
if( str != null )
charset = str.toUpperCase();
}
else if( headerLines[i].toLowerCase().startsWith( "content-description: " ) ) {
description = getFirstAttribute( headerLines[i] );
}
else if( headerLines[i].toLowerCase().startsWith( "mime-version: " ) ) {
version = getFirstAttribute( headerLines[i] );
}
}
/*
* parse body
*/
int beginLastPart = -1;
if( multipart ) {
byte boundaryArray[] = boundary.getBytes();
for( int i = beginBody; i < end - 4; i++ ) {
if( buffer.content[i] == '\r' &&
buffer.content[i+1] == '\n' &&
buffer.content[i+2] == '-' &&
buffer.content[i+3] == '-' ) {
/*
* begin of possible boundary line
*/
int j = 0;
for( ; j < boundaryArray.length && i + 4 + j < end; j++ )
if( buffer.content[ i + 4 + j ] != boundaryArray[j] )
break;
if( j == boundaryArray.length ) {
int k = i + 4 + j;
if( k < end - 2 &&
buffer.content[k] == '-' &&
buffer.content[k+1] == '-' )
k += 2;
if( k < end - 2 &&
buffer.content[k] == '\r' &&
buffer.content[k+1] == '\n' ) {
k += 2;
int endLastPart = i + 2;
if( beginLastPart != -1 ) {
MailPart newPart = new MailPart();
newPart.parse( buffer, beginLastPart, endLastPart - beginLastPart );
parts.add( newPart );
}
beginLastPart = k;
}
i = k;
}
}
}
}
else if( message ) {
MailPart newPart = new MailPart();
newPart.parse( buffer, beginBody, end );
parts.add( newPart );
}
}
public static String getFirstAttribute( String line )
{
String result = null;
int i = line.indexOf( ": " );
if( i != - 1 ) {
int j = line.indexOf( ";", i + 2 );
if( j == -1 )
result = line.substring( i + 2 );
else
result = line.substring( i + 2, j );
result = result.trim();
}
return result;
}
public static String getHeaderLineAttribute( String line, String attributeName )
{
String result = null;
int h = 0;
int l = attributeName.length();
while( true ) {
int i = line.indexOf( attributeName, h );
// System.err.println( "i=" + i );
if( i == -1 )
break;
h = i + l;
int j = line.indexOf( "=", i + l );
// System.err.println( "j=" + j );
if( j != -1 ) {
int k = line.indexOf( "\"", j + 1 );
int m = line.indexOf( ";", j + 1 );
// System.err.println( "k=" + k );
if( k != -1 && ( m == -1 || k < m ) ) {
/*
* we found a " before a possible ;
*
* now we look for the 2nd (not quoted) "
*/
m = -1;
int k2 = k + 1;
while( true ) {
m = line.indexOf( "\"", k2 );
// System.err.println( "m=" + m + " '" + line.substring( m ) + "'" );
if( m == -1 ) {
break;
}
else {
/*
* found one
*/
if( line.charAt( m - 1 ) != '\\' ) {
/*
* its not quoted, so it is the one we look for
*/
result = line.substring( k + 1, m );
break;
}
else {
/*
* this is quoted, so we extract the quote and continue the search
*/
line = line.substring( 0, m - 1 ) + line.substring( m );
// System.err.println( "quoting found, line='" + line + "'" );
k2 = m;
}
}
}
}
else if( m != -1 ) {
/*
* no " found, but a ;
*/
result = line.substring( j + 1, m ).trim();
}
else {
/*
* no " found and no ;
*/
result = line.substring( j + 1 );
}
}
}
return result;
}
/**
* @param mail
*/
public static void parse(Mail mail) {
// TODO Auto-generated method stub
}
}

View File

@ -0,0 +1,214 @@
/*
* Created on Dec 8, 2004
*
* This file is part of susimail project, see http://susi.i2p/
*
* Copyright (C) 2004-2005 <susi23@mail.i2p>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Revision: 1.3 $
*/
package i2p.susi.webmail;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.mortbay.servlet.MultiPartRequest;
/**
* @author user
*/
public class RequestWrapper {
private HttpServletRequest httpRequest = null;
private MultiPartRequest multiPartRequest = null;
private Hashtable cache;
private Hashtable cachedParameterNames;
/**
* do not call
*/
private RequestWrapper()
{
}
/**
* @param httpRequest
*/
public RequestWrapper(HttpServletRequest httpRequest) {
cache = new Hashtable();
this.httpRequest = httpRequest;
String contentType = httpRequest.getContentType();
if( contentType != null && contentType.toLowerCase().startsWith( "multipart/form-data" ) ) {
try {
multiPartRequest = new MultiPartRequest( httpRequest );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* @param b
* @return
*/
public HttpSession getSession(boolean b) {
return httpRequest.getSession( b );
}
/**
* @param key
* @return
*/
public String getParameter(String name ) {
return getParameter( name, null );
}
/**
* @return
*/
public HttpSession getSession() {
return httpRequest.getSession();
}
/**
* @return
*/
public Enumeration getParameterNames() {
if( multiPartRequest != null ) {
if( cachedParameterNames == null ) {
cachedParameterNames = new Hashtable();
String[] partNames = multiPartRequest.getPartNames();
for( int i = 0; i < partNames.length; i++ )
cachedParameterNames.put( partNames[i], new Integer( i ) );
}
return cachedParameterNames.keys();
}
else
return httpRequest.getParameterNames();
}
/**
* @return
*/
public int getContentLength() {
return httpRequest.getContentLength();
}
/**
* @return
*/
public String getContentType() {
return httpRequest.getContentType();
}
/**
*
* @param partName
* @return
*/
public String getContentType( String partName )
{
String result = null;
if( multiPartRequest != null ) {
Hashtable params = multiPartRequest.getParams( partName );
for( Enumeration e = params.keys(); e.hasMoreElements(); ) {
String key = (String)e.nextElement();
if( key.toLowerCase().compareToIgnoreCase( "content-type") == 0 ) {
String value = (String)params.get( key );
int i = value.indexOf( ";" );
if( i != -1 )
result = value.substring( 0, i );
else
result = value;
}
}
}
return result;
}
/**
* @param string
* @return
*/
public Object getAttribute(String string) {
return httpRequest.getAttribute( string );
}
/**
* @param new_subject
* @param string
* @return
*/
public String getParameter( String name, String defaultValue )
{
String result = defaultValue;
if( multiPartRequest != null ) {
String str = (String)cache.get( name );
if( str != null ) {
result = str;
}
else {
String[] partNames = multiPartRequest.getPartNames();
for( int i = 0; i < partNames.length; i++ )
if( partNames[i].compareToIgnoreCase( name ) == 0 ) {
str = multiPartRequest.getString( partNames[i] );
if( str != null ) {
result = str;
cache.put( name, result );
break;
}
}
}
}
else {
String str = httpRequest.getParameter( name );
if( str != null )
result = str;
}
return result;
}
/**
* @param new_filename
* @return
*/
public String getFilename(String partName )
{
String result = null;
if( multiPartRequest != null ) {
String str = multiPartRequest.getFilename( partName );
if( str != null )
result = str;
}
return result;
}
/**
* @param new_filename
* @return
*/
public InputStream getInputStream(String partName )
{
InputStream result = null;
if( multiPartRequest != null ) {
result = multiPartRequest.getInputStream( partName );
}
return result;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,237 @@
/*
* Created on 09.11.2004
*
* This file is part of susimail project, see http://susi.i2p/
*
* Copyright (C) 2004-2005 <susi23@mail.i2p>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Revision: 1.6 $
*/
package i2p.susi.webmail.encoding;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import i2p.susi.util.ReadBuffer;
/**
* @author susi
*/
public class Base64 implements Encoding {
/* (non-Javadoc)
* @see i2p.susi23.util.Encoding#getName()
*/
public String getName() {
return "base64";
}
/**
* @param string
* @return
*/
public String encode( byte in[] ) throws EncodingException
{
try {
return encode( new ByteArrayInputStream( in ) );
}catch (IOException e) {
throw new EncodingException( e.getMessage() );
}
}
/* (non-Javadoc)
* @see i2p.susi23.util.Encoding#encode(java.lang.String)
*/
public String encode(String str) throws EncodingException {
try {
return encode( new StringBufferInputStream( str ) );
}catch (IOException e) {
throw new EncodingException( e.getMessage() );
}
}
/**
*
* @param in
* @return
*/
private String encode( InputStream in ) throws IOException, EncodingException
{
StringBuffer strBuf = new StringBuffer();
int buf[] = new int[3];
int out[] = new int[4];
int l = 0;
while( true ) {
int read = in.available();
if( read == 0 )
break;
int i = 0;
buf[0] = buf[1] = buf[2] = 0;
while( read > 0 && i < 3 ) {
buf[i] = in.read();
if( buf[i] < 0 || buf[i] > 255 )
throw new EncodingException( "Encoding supports only values 0..255 (" + buf[i] + ")" );
i++;
read--;
}
out[0] = encodeByte( ( buf[0] >> 2 ) & 63 );
out[1] = encodeByte( ( ( buf[0] & 3 ) << 4 ) | ( ( buf[1] >> 4 ) & 15 ) );
out[2] = encodeByte( ( ( buf[1] & 15 ) << 2 ) | ( ( buf[2] >> 6 ) & 3 ) );
out[3] = encodeByte( buf[2] & 63 );
strBuf.append( (char)out[0] );
strBuf.append( (char)out[1] );
if( i > 1 ) {
strBuf.append( (char)out[2] );
}
else
strBuf.append( "=" );
if( i > 2 )
strBuf.append( (char)out[3] );
else
strBuf.append( "=" );
i += 3;
l += 4;
if( l >= 76 ) {
strBuf.append( "\r\n" );
l -= 76;
}
}
return strBuf.toString();
}
/**
* @param b
* @return
*/
private static int encodeByte(int b) {
/*
0 A 17 R 34 i 51 z
1 B 18 S 35 j 52 0
2 C 19 T 36 k 53 1
3 D 20 U 37 l 54 2
4 E 21 V 38 m 55 3
5 F 22 W 39 n 56 4
6 G 23 X 40 o 57 5
7 H 24 Y 41 p 58 6
8 I 25 Z 42 q 59 7
9 J 26 a 43 r 60 8
10 K 27 b 44 s 61 9
11 L 28 c 45 t 62 +
12 M 29 d 46 u 63 /
13 N 30 e 47 v
14 O 31 f 48 w (pad) =
15 P 32 g 49 x
16 Q 33 h 50 y
*/
if( b < 26 )
b += 'A';
else if( b < 52 )
b += 'a' - 26;
else if( b < 62 )
b += '0' - 52;
else if( b == 62 )
b = '+';
else
b = '/';
return b;
}
private static byte decodeByte( byte b ) throws DecodingException {
byte a = b;
if( b >= 'A' && b <= 'Z' )
b -= 'A';
else if( b >= 'a' && b <= 'z' )
b = (byte) (b - 'a' + 26);
else if( b >= '0' && b <= '9' )
b = (byte) (b - '0' + 52);
else if( b == '+' )
b = 62;
else if( b == '/' )
b = 63;
else if( b == '=' )
b = 0;
else
throw new DecodingException( "Decoding base64 failed (invalid data)." );
// System.err.println( "decoded " + (char)a + " to " + b );
return b;
}
/**
* @param str
* @return
*/
public ReadBuffer decode(String text) throws DecodingException {
return text != null ? decode( text.getBytes() ) : null;
}
/* (non-Javadoc)
* @see i2p.susi23.util.Encoding#decode(byte[])
*/
public ReadBuffer decode(byte[] in) throws DecodingException {
return decode( in, 0, in.length );
}
/* (non-Javadoc)
* @see i2p.susi23.util.Encoding#decode(byte[], int, int)
*/
public ReadBuffer decode(byte[] in, int offset, int length) throws DecodingException {
byte out[] = new byte[length * 3 / 4 + 1 ];
int written = 0;
while( length > 0 ) {
if( in[offset] == '\r' || in[offset] == '\n' ||
in[offset] == ' ' || in[offset] == '\t' ) {
offset++;
length--;
continue;
}
if( length >= 4 ) {
// System.out.println( "decode: " + (char)in[offset] + (char)in[offset+1]+ (char)in[offset+2]+ (char)in[offset+3] );
byte b1 = decodeByte( in[offset++] );
byte b2 = decodeByte( in[offset++] );
out[written++] = (byte) (( b1 << 2 ) | ( ( b2 >> 4 ) & 3 ) );
byte b3 = in[offset++];
if( b3 != '=' ) {
b3 = decodeByte( b3 );
out[written++] = (byte)( ( ( b2 & 15 ) << 4 ) | ( ( b3 >> 2 ) & 15 ) );
}
byte b4 = in[offset++];
if( b4 != '=' ) {
b4 = decodeByte( b4 );
out[written++] = (byte)( ( ( b3 & 3 ) << 6 ) | b4 & 63 );
}
length -= 4;
}
else {
System.err.println( "" );
throw new DecodingException( "Decoding base64 failed (trailing garbage)." );
}
}
ReadBuffer readBuffer = new ReadBuffer();
readBuffer.content = out;
readBuffer.offset = 0;
readBuffer.length = written;
return readBuffer;
}
/* (non-Javadoc)
* @see i2p.susi23.util.Encoding#decode(i2p.susi23.util.ReadBuffer)
*/
public ReadBuffer decode(ReadBuffer in) throws DecodingException {
return decode( in.content, in.offset, in.length );
}
}

View File

@ -0,0 +1,36 @@
/*
* Created on Nov 15, 2004
*
* This file is part of susimail project, see http://susi.i2p/
*
* Copyright (C) 2004-2005 <susi23@mail.i2p>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Revision: 1.2 $
*/
package i2p.susi.webmail.encoding;
/**
* @author susi
*/
public class DecodingException extends Exception {
private static final long serialVersionUID = 1L;
DecodingException( String msg ) {
super( msg );
}
}

View File

@ -0,0 +1,89 @@
/*
* Created on Nov 16, 2004
*
* This file is part of susimail project, see http://susi.i2p/
*
* Copyright (C) 2004-2005 <susi23@mail.i2p>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Revision: 1.3 $
*/
package i2p.susi.webmail.encoding;
import i2p.susi.util.ReadBuffer;
/**
* @author susi
*/
public class EightBit implements Encoding {
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#getName()
*/
public String getName() {
return "8bit";
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#encode(byte[])
*/
public String encode(byte[] in) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#encode(java.lang.String)
*/
public String encode(String str) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(byte[])
*/
public ReadBuffer decode(byte[] in) throws DecodingException {
return decode( in, 0, in.length );
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(byte[], int, int)
*/
public ReadBuffer decode(byte[] in, int offset, int length)
throws DecodingException {
ReadBuffer readBuffer = new ReadBuffer();
readBuffer.content = in;
readBuffer.offset = offset;
readBuffer.length = length;
return readBuffer;
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(java.lang.String)
*/
public ReadBuffer decode(String str) throws DecodingException {
return decode( str.getBytes() );
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(i2p.susi.webmail.util.ReadBuffer)
*/
public ReadBuffer decode(ReadBuffer in) throws DecodingException {
return in;
}
}

View File

@ -0,0 +1,72 @@
/*
* Created on Nov 12, 2004
*
* This file is part of susimail project, see http://susi.i2p/
*
* Copyright (C) 2004-2005 <susi23@mail.i2p>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Revision: 1.3 $
*/
package i2p.susi.webmail.encoding;
import i2p.susi.util.ReadBuffer;
/**
* Interface to encode/decode content transfer encodings like quoted-printable, base64 etc.
*
* @author susi
*/
public interface Encoding {
public String getName();
/**
*
* @param in
* @return
*/
public String encode( byte in[] ) throws EncodingException;
/**
*
* @param str
* @return
*/
public String encode( String str ) throws EncodingException;
/**
*
* @param in
* @return
*/
public ReadBuffer decode( byte in[] ) throws DecodingException;
/**
*
* @param in
* @param size
* @return
*/
public ReadBuffer decode( byte in[], int offset, int length ) throws DecodingException;
/**
*
* @param str
* @return
*/
public ReadBuffer decode( String str ) throws DecodingException;
/**
*
* @param in
* @return
*/
public ReadBuffer decode( ReadBuffer in ) throws DecodingException;
}

View File

@ -0,0 +1,40 @@
/*
* Created on Nov 17, 2004
*
* This file is part of susimail project, see http://susi.i2p/
*
* Copyright (C) 2004-2005 <susi23@mail.i2p>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Revision: 1.2 $
*/
package i2p.susi.webmail.encoding;
/**
* @author susi
*/
public class EncodingException extends Exception {
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 1L;
EncodingException( String msg )
{
super( msg );
}
}

View File

@ -0,0 +1,80 @@
/*
* Created on Nov 12, 2004
*
* This file is part of susimail project, see http://susi.i2p/
*
* Copyright (C) 2004-2005 <susi23@mail.i2p>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Revision: 1.4 $
*/
package i2p.susi.webmail.encoding;
import i2p.susi.debug.Debug;
import i2p.susi.util.Config;
import java.util.Hashtable;
import java.util.Set;
/**
* Manager class to handle content transfer encodings.
* @author susi
*/
public class EncodingFactory {
public static String CONFIG_ENCODING = "encodings";
private static Hashtable encodings = null;
static {
encodings = new Hashtable();
String list = Config.getProperty( CONFIG_ENCODING );
if( list != null ) {
String[] classNames = list.split( ";" );
for( int i = 0; i < classNames.length; i++ ) {
try {
Class c = Class.forName( classNames[i] );
Encoding e = (Encoding)c.newInstance();
encodings.put( e.getName(), e );
Debug.debug( Debug.DEBUG, "Registered " + e.getClass().getName() );
}
catch (Exception e) {
Debug.debug( Debug.ERROR, "Error loading class '" + classNames[i] + "', reason: " + e.getClass().getName() );
}
}
}
}
/**
* Retrieve instance of an encoder for a supported encoding (or null).
*
* @param name name of encoding (e.g. quoted-printable)
*
* @return
*/
public static Encoding getEncoding( String name )
{
return name != null && name.length() > 0 ? (Encoding)encodings.get( name ) : null;
}
/**
* Returns list of available encodings;
*
* @return
*/
public static Set availableEncodings()
{
return encodings.keySet();
}
}

View File

@ -0,0 +1,89 @@
/*
* Created on Nov 23, 2004
*
* This file is part of susimail project, see http://susi.i2p/
*
* Copyright (C) 2004-2005 <susi23@mail.i2p>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Revision: 1.3 $
*/
package i2p.susi.webmail.encoding;
import i2p.susi.util.ReadBuffer;
/**
* @author user
*/
public class HTML implements Encoding {
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#getName()
*/
public String getName() {
return "HTML";
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#encode(byte[])
*/
public String encode(byte[] in) throws EncodingException {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#encode(java.lang.String)
*/
public String encode(String str) throws EncodingException
{
return str.replaceAll( "<", "&lt;" ).replaceAll( ">", "&gt;" ).replaceAll( "\r{0,1}\n", "<br>\r\n" );
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(byte[])
*/
public ReadBuffer decode(byte[] in) throws DecodingException {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(byte[], int, int)
*/
public ReadBuffer decode(byte[] in, int offset, int length)
throws DecodingException {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(java.lang.String)
*/
public ReadBuffer decode(String str) throws DecodingException {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(i2p.susi.webmail.util.ReadBuffer)
*/
public ReadBuffer decode(ReadBuffer in) throws DecodingException {
// TODO Auto-generated method stub
return null;
}
}

View File

@ -0,0 +1,303 @@
/*
* Created on Nov 12, 2004
*
* This file is part of susimail project, see http://susi.i2p/
*
* Copyright (C) 2004-2005 <susi23@mail.i2p>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Revision: 1.3 $
*/
package i2p.susi.webmail.encoding;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import i2p.susi.util.HexTable;
import i2p.susi.util.ReadBuffer;
/**
* @author susi
*/
public class HeaderLine implements Encoding {
public static final String NAME = "HEADERLINE";
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#getName()
*/
public String getName() {
return NAME;
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#encode(java.lang.String)
*/
public String encode(String text) throws EncodingException {
try {
return encode( new StringBufferInputStream( text ) );
} catch (IOException e) {
throw new EncodingException( "IOException occured." );
}
}
private static final int BUFSIZE = 2;
private String encode(InputStream in) throws IOException
{
StringBuffer out = new StringBuffer();
int l = 0, buffered = 0, tmp[] = new int[BUFSIZE];
boolean quoting = false;
boolean quote = false;
boolean linebreak = false;
String quotedSequence = null;
int rest = 0;
while( true ) {
rest = in.available();
while( rest > 0 && buffered < BUFSIZE ) {
tmp[buffered++] = in.read();
rest--;
}
if( rest == 0 && buffered == 0 )
break;
int c = tmp[0];
buffered--;
for( int j = 1; j < BUFSIZE; j++ )
tmp[j-1] = tmp[j];
quote = true;
if( c > 32 && c < 127 && c != 61 ) {
quote = false;
}
else if( ( c == 32 || c == 9 ) ) {
quote = false;
if( rest == 0 && buffered == 1 )
quote = true;
if( buffered > 0 && ( tmp[0] == '\r' || tmp[0] == '\n' ) )
quote = true;
}
else if( c == 13 && buffered > 0 && tmp[0] == 10 ) {
quote = false;
linebreak = true;
buffered--;
for( int j = 1; j < BUFSIZE; j++ )
tmp[j-1] = tmp[j];
}
if( quote ) {
if( ! quoting ) {
quotedSequence = "=?iso-8859-1?Q?";
quoting = true;
}
quotedSequence += HexTable.table[ c ];
}
else {
if( quoting ) {
quotedSequence += "?=";
int sl = quotedSequence.length();
if( l + sl > 76 ) {
/*
* wrap line
*/
out.append( "\r\n\t" );
l = 0;
}
out.append( quotedSequence );
l += sl;
quoting = false;
}
if( linebreak ) {
out.append( "\r\n" );
linebreak = false;
l = 0;
}
else {
if( l > 76 ) {
out.append( "\r\n\t" );
l = 0;
}
out.append( (char)c );
l++;
}
}
}
if( quoting ) {
quotedSequence += "?=";
int sl = quotedSequence.length();
if( l + sl > 76 ) {
/*
* wrap line
*/
out.append( "\r\n\t" );
l = 0;
}
out.append( quotedSequence );
}
return out.toString();
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(java.lang.String)
*/
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#encode(java.lang.String)
*/
public String encode( byte in[] ) throws EncodingException {
try {
return encode( new ByteArrayInputStream( in ) );
} catch (IOException e) {
throw new EncodingException( "IOException occured." );
}
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(java.lang.String)
*/
public ReadBuffer decode( byte in[] ) throws DecodingException {
return decode( in, 0, in.length );
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(java.lang.String)
*/
public ReadBuffer decode( byte in[], int offset, int length ) throws DecodingException {
byte[] out = new byte[length];
int written = 0;
int end = offset + length;
if( end > in.length )
throw new DecodingException( "Index out of bound." );
boolean linebreak = false;
boolean lastCharWasQuoted = false;
while( length-- > 0 ) {
byte c = in[offset++];
if( c == '=' ) {
if( length > 0 ) {
if( in[offset] == '?' ) {
// System.err.println( "=? found at " + ( offset -1 ) );
int f2 = offset + 1;
for( ; f2 < end && in[f2] != '?'; f2++ );
if( f2 < end ) {
/*
* 2nd question mark found
*/
// System.err.println( "2nd ? found at " + f2 );
int f3 = f2 + 1;
for( ; f3 < end && in[f3] != '?'; f3++ );
if( f3 < end ) {
/*
* 3rd question mark found
*/
// System.err.println( "3rd ? found at " + f3 );
int f4 = f3 + 1;
for( ; f4 < end && in[f4] != '?'; f4++ );
if( f4 < end - 1 && in[f4+1] == '=' ) {
/*
* 4th question mark found, we are complete, so lets start
*/
String enc = ( in[f2+1] == 'Q' || in[f2+1] == 'q' ) ? "quoted-printable" : ( ( in[f2+1] == 'B' || in[f2+1] == 'b' ) ? "base64" : null );
// System.err.println( "4th ? found at " + f4 + ", encoding=" + enc );
if( enc != null ) {
Encoding e = EncodingFactory.getEncoding( enc );
if( e != null ) {
// System.err.println( "encoder found" );
ReadBuffer tmp = null;
try {
// System.err.println( "decode(" + (f3 + 1) + "," + ( f4 - f3 - 1 ) + ")" );
tmp = e.decode( in, f3 + 1, f4 - f3 - 1 );
for( int j = 0; j < tmp.length; j++ ) {
byte d = tmp.content[ tmp.offset + j ];
out[written++] = ( d == '_' ? 32 : d );
}
int distance = f4 + 2 - offset;
offset += distance;
length -= distance;
lastCharWasQuoted = true;
continue;
}
catch (Exception e1) {
}
}
}
}
}
}
}
}
}
else if( c == '\r' ) {
if( length > 0 && in[offset] == '\n' ) {
/*
* delay linebreak in case of long line
*/
linebreak = true;
length--;
offset++;
continue;
}
}
if( linebreak ) {
linebreak = false;
if( c != ' ' && c != '\t' ) {
/*
* new line does not start with whitespace, so its not a new part of a
* long line
*/
out[written++] = '\r';
out[written++] = '\n';
}
else {
if( !lastCharWasQuoted )
out[written++] = ' ';
/*
* skip whitespace
*/
while( length > 0 && ( in[offset] == ' ' || in[offset] == '\t' ) ) {
offset++;
length--;
}
continue;
}
}
/*
* print out everything else literally
*/
out[written++] = c;
lastCharWasQuoted = false;
}
if( linebreak ) {
out[written++] = '\r';
out[written++] = '\n';
}
ReadBuffer readBuffer = new ReadBuffer();
readBuffer.content = out;
readBuffer.offset = 0;
readBuffer.length = written;
return readBuffer;
}
public ReadBuffer decode(String text) throws DecodingException {
return text != null ? decode( text.getBytes() ) : null;
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(i2p.susi.webmail.util.ReadBuffer)
*/
public ReadBuffer decode(ReadBuffer in) throws DecodingException {
return decode( in.content, in.offset, in.length );
}
public static void main( String[] args ) throws EncodingException {
String text = "Subject: test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test \r\n" +
"From: Sm<53>rebr<62>d <smoerebroed@mail.i2p>\r\n" +
"To: <20><><EFBFBD><EFBFBD> <lalala@mail.i2p>\r\n";
HeaderLine hl = new HeaderLine();
System.out.println( hl.encode( text ) );
System.out.println( hl.encode( "test <20><><EFBFBD>" ) );
}
}

View File

@ -0,0 +1,203 @@
/*
* Created on Nov 12, 2004
*
* This file is part of susimail project, see http://susi.i2p/
*
* Copyright (C) 2004-2005 <susi23@mail.i2p>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Revision: 1.3 $
*/
package i2p.susi.webmail.encoding;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import i2p.susi.util.HexTable;
import i2p.susi.util.ReadBuffer;
/**
* @author susi
*/
public class QuotedPrintable implements Encoding {
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#getName()
*/
public String getName() {
return "quoted-printable";
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#encode(java.lang.String)
*/
public String encode( byte in[] ) throws EncodingException {
try {
return encode( new ByteArrayInputStream( in ) );
}catch (IOException e) {
throw new EncodingException( "IOException occured." );
}
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#encode(java.lang.String)
*/
public String encode(String text) throws EncodingException {
try {
return encode( new StringBufferInputStream( text ) );
}catch (IOException e) {
throw new EncodingException( "IOException occured." );
}
}
/**
*
* @param in
* @return
*/
private static int BUFSIZE = 2;
private String encode( InputStream in ) throws EncodingException, IOException {
StringBuffer out = new StringBuffer();
int l = 0;
int read = 0, buffered = 0, tmp[] = new int[BUFSIZE];
while( true ) {
read = in.available();
while( read > 0 && buffered < BUFSIZE ) {
tmp[buffered++] = in.read();
read--;
}
if( read == 0 && buffered == 0 )
break;
int c = tmp[0];
buffered--;
for( int j = 1; j < BUFSIZE; j++ )
tmp[j-1] = tmp[j];
if( c > 32 && c < 127 && c != 61 ) {
out.append( (char)c );
l++;
}
else if( ( c == 32 || c == 9 ) ) {
if( buffered > 0 && ( tmp[0] == 10 || tmp[0] == 13 ) ) {
/*
* whitespace at end of line
*/
out.append( c == 32 ? "=20" : "=09" );
l += 3;
}
else {
out.append( (char)c );
l++;
}
}
else if( c == 13 && buffered > 0 && tmp[0] == 10 ) {
out.append( "\r\n" );
buffered--;
for( int j = 1; j < BUFSIZE; j++ )
tmp[j-1] = tmp[j];
l = 0;
}
else {
if( c < 0 || c > 255 ) {
throw new EncodingException( "Encoding supports only values of 0..255." );
}
out.append( HexTable.table[ c ] );
l += 3;
}
}
return out.toString();
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(java.lang.String)
*/
public ReadBuffer decode( byte in[] ) {
return decode( in, 0, in.length );
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(java.lang.String)
*/
public ReadBuffer decode(String text) {
return text != null ? decode( text.getBytes() ) : null;
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(byte[], int, int)
*/
public ReadBuffer decode(byte[] in, int offset, int length) {
byte[] out = new byte[length];
int written = 0;
while( length-- > 0 ) {
byte c = in[offset++];
if( c == '=' ) {
if( length >= 2 ) {
byte a = in[offset];
byte b = in[offset + 1];
if( ( ( a >= '0' && a <= '9' ) || ( a >= 'A' && a <= 'F' ) ) &&
( ( b >= '0' && b <= '9' ) || ( b >= 'A' && b <= 'F' ) ) ) {
/*
* decode sequence
*/
// System.err.println( "decoding 0x" + (char)a + "" + (char)b );
length -= 2 ;
offset += 2;
if( a >= '0' && a <= '9' )
a -= '0';
else if( a >= 'A' && a <= 'F' )
a = (byte) (a - 'A' + 10);
if( b >= '0' && b <= '9' )
b -= '0';
else if( b >= 'A' && b <= 'F' )
b = (byte) (b - 'A' + 10);
out[written++]=(byte) (a*16 + b);
continue;
}
else if( a == '\r' && b == '\n' ) {
/*
* softbreak, simply ignore it
*/
length -= 2;
offset += 2;
continue;
}
/*
* no correct encoded sequence found, ignore it and print it literally
*/
}
}
/*
* print out everything else literally
*/
out[written++] = c;
}
ReadBuffer readBuffer = new ReadBuffer();
readBuffer.content = out;
readBuffer.offset = 0;
readBuffer.length = written;
return readBuffer;
}
/* (non-Javadoc)
* @see i2p.susi.webmail.encoding.Encoding#decode(i2p.susi.webmail.util.ReadBuffer)
*/
public ReadBuffer decode(ReadBuffer in) {
return decode( in.content, in.offset, in.length );
}
}

View File

@ -0,0 +1,101 @@
/*
* Created on 15.11.2004
*
* This file is part of susimail project, see http://susi.i2p/
*
* Copyright (C) 2004-2005 <susi23@mail.i2p>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Revision: 1.3 $
*/
package i2p.susi.webmail.encoding;
import i2p.susi.util.ReadBuffer;
/**
* @author susi
*/
public class SevenBit implements Encoding {
/* (non-Javadoc)
* @see i2p.susi23.mail.encoding.Encoding#getName()
*/
public String getName() {
return "7bit";
}
/* (non-Javadoc)
* @see i2p.susi23.mail.encoding.Encoding#encode(byte[])
*/
public String encode(byte[] in) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see i2p.susi23.mail.encoding.Encoding#encode(java.lang.String)
*/
public String encode(String str) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc)
* @see i2p.susi23.mail.encoding.Encoding#decode(byte[])
*/
public ReadBuffer decode(byte[] in) throws DecodingException {
return decode( in, 0, in.length );
}
/* (non-Javadoc)
* @see i2p.susi23.mail.encoding.Encoding#decode(byte[], int, int)
*/
public ReadBuffer decode(byte[] in, int offset, int length)
throws DecodingException {
int backupLength = length;
int backupOffset = offset;
while( length-- > 0 ) {
byte b = in[offset++];
if( b > 32 && b < 127 )
continue;
if( b == ' ' || b == '\t' )
continue;
if( b == '\r' || b == '\n' )
continue;
throw new DecodingException( "No 8bit Data allowed (" + b + ")" );
}
ReadBuffer readBuffer = new ReadBuffer();
readBuffer.content = in;
readBuffer.offset = backupOffset;
readBuffer.length = backupLength;
return readBuffer;
}
/* (non-Javadoc)
* @see i2p.susi23.mail.encoding.Encoding#decode(java.lang.String)
*/
public ReadBuffer decode(String str) throws DecodingException {
return decode( str.getBytes() );
}
/* (non-Javadoc)
* @see i2p.susi23.mail.encoding.Encoding#decode(i2p.susi.webmail.util.ReadBuffer)
*/
public ReadBuffer decode(ReadBuffer in) throws DecodingException {
return decode( in.content, in.offset, in.length );
}
}

View File

@ -0,0 +1,725 @@
/*
* Created on Nov 4, 2004
*
* This file is part of susimail project, see http://susi.i2p/
*
* Copyright (C) 2004-2005 <susi23@mail.i2p>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Revision: 1.8 $
*/
package i2p.susi.webmail.pop3;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Hashtable;
import i2p.susi.debug.Debug;
import i2p.susi.util.*;
/**
* @author susi23
*/
public class POP3MailBox {
private static final int DEFAULT_BUFSIZE = 4096;
private String host = null, user = null, pass = null;
private String lastLine = "-ERR", lastError = null;
private int port = 0, mails = 0, read = 0;
private boolean connected = false;
private Hashtable headerList = null, bodyList = null, sizes = null, uidlToID = null;
private ArrayList uidlList = null;
private Socket socket = null;
private byte[] buffer = new byte[DEFAULT_BUFSIZE];
private Object synchronizer = null;
private Object[] uidls = null;
/**
* @param host
* @param port
* @param user
* @param pass
*/
public POP3MailBox(String host, int port, String user, String pass) {
Debug.debug(
Debug.DEBUG,
"Mailbox(" + host + "," + port + "," + user + ",password)");
this.host = host;
this.port = port;
this.user = user;
this.pass = pass;
headerList = new Hashtable();
bodyList = new Hashtable();
uidlList = new ArrayList();
uidlToID = new Hashtable();
sizes = new Hashtable();
synchronizer = new Object();
connect();
}
/**
*
* @param uidl
* @return
*/
public ReadBuffer getHeader( String uidl ) {
synchronized( synchronizer ) {
return getHeader( getIDfromUIDL( uidl ) );
}
}
/**
* retrieves header from pop3 server (with TOP command and RETR as fallback)
*
* @param id message id
* @return byte buffer containing data
*/
private ReadBuffer getHeader( int id ) {
synchronized( synchronizer ) {
Debug.debug(Debug.DEBUG, "getHeader(" + id + ")");
Integer idObj = new Integer(id);
ReadBuffer header = null;
if (id >= 1 && id <= mails) {
/*
* is data already cached?
*/
header = (ReadBuffer)headerList.get(idObj);
if (header == null) {
/*
* try 'TOP n 0' command
*/
header = sendCmdN("TOP " + id + " 0" );
}
if( header == null) {
/*
* try 'RETR n' command
*/
header = sendCmdN("RETR " + id );
}
if( header != null ) {
/*
* store result in hashtable
*/
headerList.put(idObj, header);
}
}
else {
lastError = "Message id out of range.";
}
return header;
}
}
/**
*
* @param uidl
* @return
*/
public ReadBuffer getBody( String uidl ) {
synchronized( synchronizer ) {
return getBody( getIDfromUIDL( uidl ) );
}
}
/**
* retrieve message body from pop3 server (via RETR command)
*
* @param id message id
* @return byte buffer containing data
*/
private ReadBuffer getBody(int id) {
synchronized( synchronizer ) {
Debug.debug(Debug.DEBUG, "getBody(" + id + ")");
Integer idObj = new Integer(id);
ReadBuffer body = null;
if (id >= 1 && id <= mails) {
body = (ReadBuffer)bodyList.get(idObj);
if( body == null ) {
body = sendCmdN( "RETR " + id );
if (body != null) {
bodyList.put(idObj, body);
}
else {
Debug.debug( Debug.DEBUG, "sendCmdN returned null" );
}
}
}
else {
lastError = "Message id out of range.";
}
return body;
}
}
/**
*
* @param uidl
* @return
*/
public boolean delete( String uidl )
{
Debug.debug(Debug.DEBUG, "delete(" + uidl + ")");
synchronized( synchronizer ) {
return delete( getIDfromUIDL( uidl ) );
}
}
/**
* delete message on pop3 server
*
* @param id message id
* @return true if successful
*/
private boolean delete(int id)
{
Debug.debug(Debug.DEBUG, "delete(" + id + ")");
boolean result = false;
synchronized( synchronizer ) {
try {
result = sendCmd1a( "DELE " + id );
}
catch (IOException e) {
}
}
return result;
}
/**
*
* @param uidl
* @return
*/
public int getSize( String uidl ) {
synchronized( synchronizer ) {
return getSize( getIDfromUIDL( uidl ) );
}
}
/**
* get size of a message (via LIST command)
*
* @param id message id
* @return message size in bytes
*/
private int getSize(int id) {
synchronized( synchronizer ) {
Debug.debug(Debug.DEBUG, "getSize(" + id + ")");
int result = 0;
/*
* find value in hashtable
*/
Integer resultObj = (Integer) sizes.get(new Integer(id));
if (resultObj != null)
result = resultObj.intValue();
return result;
}
}
/**
* check whether connection is still alive
*
* @return true or false
*/
public boolean isConnected() {
Debug.debug(Debug.DEBUG, "isConnected()");
if (socket == null
|| !socket.isConnected()
|| socket.isInputShutdown()
|| socket.isOutputShutdown()
|| socket.isClosed()) {
connected = false;
}
return connected;
}
/**
*
* @throws IOException
*/
private void updateUIDLs() throws IOException
{
synchronized (synchronizer) {
ReadBuffer readBuffer = null;
uidlToID.clear();
uidlList.clear();
uidls = null;
readBuffer = sendCmdNa( "UIDL", DEFAULT_BUFSIZE );
if( readBuffer != null ) {
String[] lines = new String( readBuffer.toString() ).split( "\r\n" );
for( int i = 0; i < lines.length; i++ ) {
int j = lines[i].indexOf( " " );
if( j != -1 ) {
try {
int n = Integer.parseInt( lines[i].substring( 0, j ) );
String uidl = lines[i].substring( j+1 );
uidlToID.put( uidl, new Integer( n ) );
uidlList.add( n-1, uidl );
}
catch( NumberFormatException nfe ) {
}
}
}
uidls = uidlList.toArray();
}
else {
System.err.println( "Error getting UIDL list from pop3 server.");
}
}
}
/**
*
* @throws IOException
*/
private void updateSizes() throws IOException {
/*
* try LIST
*/
sizes.clear();
ReadBuffer readBuffer = sendCmdNa("LIST", DEFAULT_BUFSIZE );
if(readBuffer != null) {
String[] lines = new String( readBuffer.content, 0, readBuffer.length ).split( "\r\n" );
if (lines != null) {
sizes = new Hashtable();
for (int i = 0; i < lines.length; i++) {
int j = lines[i].indexOf(" ");
if (j != -1) {
int key = Integer.parseInt(lines[i].substring(0, j));
int value = Integer.parseInt(lines[i].substring(j + 1));
sizes.put(new Integer(key), new Integer(value));
}
}
}
}
else {
System.err.println( "Error getting size LIST from pop3 server.");
}
}
/**
*
*
*/
public void refresh() {
synchronized( synchronizer ) {
close();
connect();
}
}
/**
*
*
*/
private void clear()
{
uidlList.clear();
uidlToID.clear();
sizes.clear();
uidls = null;
mails = 0;
}
/**
* connect to pop3 server, login with USER and PASS and try STAT then
*/
private void connect() {
Debug.debug(Debug.DEBUG, "connect()");
clear();
if (socket != null && socket.isConnected())
close();
try {
socket = new Socket(host, port);
} catch (UnknownHostException e) {
lastError = e.getMessage();
return;
} catch (IOException e) {
lastError = e.getMessage();
return;
}
if (socket != null) {
try {
if (sendCmd1a("")
&& sendCmd1a("USER " + user)
&& sendCmd1a("PASS " + pass)
&& sendCmd1a("STAT") ) {
int i = lastLine.indexOf(" ", 5);
mails =
Integer.parseInt(
i != -1
? lastLine.substring(4, i)
: lastLine.substring(4));
connected = true;
updateUIDLs();
updateSizes();
}
else {
lastError = new String( lastLine );
close();
}
}
catch (NumberFormatException e1) {
lastError = "Error getting number of messages: " + e1.getCause();
}
catch (IOException e1) {
lastError = "Error while opening mailbox: " + e1.getCause();
}
}
}
/**
* send command to pop3 server (and expect single line answer), if any
* exception occurs, reconnect and try again
*
* @param cmd command to send
* @return true if command was successful (+OK)
*/
private boolean sendCmd1(String cmd) {
boolean result = false;
synchronized (synchronizer) {
if (!isConnected())
connect();
try {
result = sendCmd1a(cmd);
}
catch (Exception e) {
Debug.debug(Debug.ERROR, "Exception: " + e.getCause());
Debug.debug(Debug.ERROR, "trying to reconnect" );
connect();
try {
result = sendCmd1a(cmd);
} catch (IOException e2) {
Debug.debug(Debug.ERROR, "reconnect failed: " + e2.getCause());
}
}
}
return result;
}
/**
* send command to pop3 server (and expect single line answer)
*
* @param cmd command to send
* @return true if command was successful (+OK)
* @throws IOException
*/
private boolean sendCmd1a(String cmd) throws IOException {
/*
* dont log password
*/
boolean result = false;
String msg = cmd;
if (msg.startsWith("PASS"))
msg = "PASS provided";
Debug.debug(Debug.DEBUG, "sendCmd1a(" + msg + ")");
cmd += "\r\n";
socket.getOutputStream().write(cmd.getBytes());
read = socket.getInputStream().read(buffer);
// Debug.debug(Debug.DEBUG, "sendCmd1a: read " + read + " bytes");
if (read > 0) {
lastLine = new String(buffer, 0, read);
// Debug.debug( Debug.DEBUG, "sendCmd1a: READBUFFER: '" + lastLine + "'" );
if (lastLine.startsWith("+OK")) {
result = true;
}
else {
lastError = lastLine;
}
}
return result;
}
/**
*
* @param cmd
* @return
*/
private ReadBuffer sendCmdN(String cmd )
{
return sendCmdN( cmd, DEFAULT_BUFSIZE );
}
/**
* @param cmd
* @return
*/
private ReadBuffer sendCmdN(String cmd, int bufSize )
{
ReadBuffer result = null;
synchronized (synchronizer) {
if (!isConnected())
connect();
try {
result = sendCmdNa(cmd, bufSize);
}
catch (IOException e) {
lastError = e.getMessage();
Debug.debug( Debug.DEBUG, "sendCmdNa throws IOException: " + lastError );
result = null;
}
if( result == null ) {
connect();
if (connected) {
try {
result = sendCmdNa(cmd, bufSize);
}
catch (IOException e2) {
lastError = e2.getMessage();
Debug.debug( Debug.DEBUG, "2nd sendCmdNa throws IOException: " + lastError );
result = null;
}
}
else {
Debug.debug( Debug.DEBUG, "not connected after reconnect" );
}
}
}
return result;
}
/**
*
* @param src
* @param newSize
* @return
*/
private byte[] resizeArray( byte src[], int newSize )
{
byte dest[] = new byte[newSize];
for( int i = 0; i < src.length; i++ )
dest[i] = src[i];
return dest;
}
/**
*
* @param src
* @param srcOffset
* @param len
* @param dest
* @param destOffset
*/
private void copy( byte[] src, int srcOffset, int len, byte[] dest, int destOffset )
{
while( len-- > 0 ) {
dest[destOffset++] = src[srcOffset++];
}
}
/**
* @param id
* @return @throws
* IOException
*/
private ReadBuffer sendCmdNa(String cmd, int bufSize ) throws IOException
{
Debug.debug(Debug.DEBUG, "sendCmdNa(" + cmd + ")");
ReadBuffer readBuffer = null;
long timeOut = 60000;
byte result[] = new byte[bufSize];
int written = 0;
if (sendCmd1a(cmd)) {
int offset = 0;
while( offset < read - 1 && buffer[offset] != '\r' && buffer[offset+1] != '\n' )
offset++;
offset += 2;
if( read - offset > result.length )
result = resizeArray( result, result.length + result.length );
if( read - offset > 0 )
copy( buffer, offset, read - offset, result, 0 );
written = read - offset;
boolean doRead = true;
// Debug.debug( Debug.DEBUG, "READBUFFER: '" + new String( result, 0, written ) + "'" );
if( written >= 5 && result[ written - 5 ] == '\r' &&
result[ written - 4 ] == '\n' &&
result[ written - 3 ] == '.' &&
result[ written - 2 ] == '\r' &&
result[ written - 1 ] == '\n' ) {
written -= 3;
doRead = false;
}
InputStream input = socket.getInputStream();
long startTime = System.currentTimeMillis();
while (doRead) {
int len = input.available();
if( len == 0 ) {
if( System.currentTimeMillis() - startTime > timeOut )
throw new IOException( "Timeout while waiting on server response." );
try {
Thread.sleep( 500 );
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
while (len > 0) {
read = socket.getInputStream().read(buffer, 0, len > buffer.length ? buffer.length : len );
// Debug.debug(Debug.DEBUG, "read " + read + " bytes");
if( written + read > result.length )
result = resizeArray( result, result.length + result.length );
copy( buffer, 0, read, result, written );
written += read;
// Debug.debug( Debug.DEBUG, "READBUFFER: '" + new String( result, 0, written ) + "'" );
if( result[ written - 5 ] == '\r' &&
result[ written - 4 ] == '\n' &&
result[ written - 3 ] == '.' &&
result[ written - 2 ] == '\r' &&
result[ written - 1 ] == '\n' ) {
written -= 3;
doRead = false;
}
len -= read;
}
}
}
readBuffer = new ReadBuffer();
readBuffer.content = result;
readBuffer.offset = 0;
readBuffer.length = written;
}
else {
Debug.debug( Debug.DEBUG, "sendCmd1a returned false" );
}
return readBuffer;
}
/**
* @return
*/
public int getNumMails() {
synchronized( synchronizer ) {
Debug.debug(Debug.DEBUG, "getNumMails()");
if (!isConnected())
connect();
return connected ? mails : 0;
}
}
/**
* @return
*/
public String lastError() {
Debug.debug(Debug.DEBUG, "lastError()");
return this.lastError;
}
/**
*
*
*/
public void close() {
synchronized( synchronizer ) {
Debug.debug(Debug.DEBUG, "close()");
if (socket != null && socket.isConnected()) {
try {
sendCmd1a("QUIT");
socket.close();
} catch (IOException e) {
System.err.println(
"Error while closing connection: " + e.getCause());
}
}
socket = null;
connected = false;
}
}
/**
* returns number of message with given UIDL
*
* @param uidl
* @return
*/
private int getIDfromUIDL( String uidl )
{
int result = -1;
Integer intObject = (Integer)uidlToID.get( uidl );
if( intObject != null ) {
result = intObject.intValue();
}
return result;
}
/**
*
* @param id
* @return
*/
public String getUIDLfromID( int id )
{
return (String)uidlList.get( id );
}
/**
*
* @return
*/
public Object[] getUIDLs()
{
return uidls;
}
/**
*
* @param args
*/
public static void main( String[] args )
{
Debug.setLevel( Debug.DEBUG );
POP3MailBox mailbox = new POP3MailBox( "localhost", 7660 , "test", "test");
ReadBuffer readBuffer = mailbox.sendCmdN( "LIST" );
System.out.println( "list='" + readBuffer + "'" );
}
/**
*
*/
public void performDelete()
{
close();
connect();
}
}

View File

@ -0,0 +1,176 @@
/*
* Created on Nov 9, 2004
*
* This file is part of susimail project, see http://susi.i2p/
*
* Copyright (C) 2004-2005 <susi23@mail.i2p>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Revision: 1.3 $
*/
package i2p.susi.webmail.smtp;
import i2p.susi.debug.Debug;
import i2p.susi.webmail.encoding.Encoding;
import i2p.susi.webmail.encoding.EncodingException;
import i2p.susi.webmail.encoding.EncodingFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
/**
* @author susi
*/
public class SMTPClient {
Socket socket;
byte buffer[];
public String error;
String lastResponse;
private static Encoding base64 = null;
static {
base64 = EncodingFactory.getEncoding( "base64" );
}
public SMTPClient()
{
socket = null;
buffer = new byte[10240];
error = "";
lastResponse = "";
}
public int sendCmd( String cmd )
{
Debug.debug( Debug.DEBUG, "sendCmd(" + cmd +")" );
if( socket == null )
return 0;
int result = 0;
lastResponse = "";
try {
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
if( cmd != null ) {
cmd += "\r\n";
out.write( cmd.getBytes() );
}
String str = "";
boolean doContinue = true;
while( doContinue ) {
if( in.available() > 0 ) {
int read = in.read( buffer );
str += new String( buffer, 0, read );
lastResponse += str;
while( true ) {
int i = str.indexOf( "\r\n" );
if( i == -1 )
break;
if( result == 0 ) {
try {
result = Integer.parseInt( str.substring( 0, 3 ) );
}
catch( NumberFormatException nfe ) {
result = 0;
doContinue = false;
break;
}
}
if( str.substring( 3, 4 ).compareTo( " " ) == 0 ) {
doContinue = false;
break;
}
str = str.substring( i + 2 );
}
}
}
}
catch (IOException e) {
error += "IOException occured.<br>";
result = 0;
}
return result;
}
public boolean sendMail( String host, int port, String user, String pass, String sender, Object[] recipients, String body )
{
boolean mailSent = false;
boolean ok = true;
try {
socket = new Socket( host, port );
}
catch (Exception e) {
error += "Cannot connect: " + e.getMessage() + "<br>";
ok = false;
}
try {
if( ok && sendCmd( null ) == 220 &&
sendCmd( "EHLO localhost" ) == 250 &&
sendCmd( "AUTH LOGIN" ) == 334 &&
sendCmd( base64.encode( user ) ) == 334 &&
sendCmd( base64.encode( pass ) ) == 235 &&
sendCmd( "MAIL FROM: " + sender ) == 250 ) {
for( int i = 0; i < recipients.length; i++ ) {
if( sendCmd( "RCPT TO: " + recipients[i] ) != 250 ) {
ok = false;
}
}
if( ok ) {
if( sendCmd( "DATA" ) == 354 ) {
if( body.indexOf( "\r\n.\r\n" ) != -1 )
body = body.replaceAll( "\r\n.\r\n", "\r\n..\r\n" );
body += "\r\n.\r\n";
try {
socket.getOutputStream().write( body.getBytes() );
if( sendCmd( null ) == 250 ) {
mailSent = true;
}
}
catch (Exception e) {
ok = false;
error += "Error while sending mail: " + e.getMessage() + "<br>";
}
}
}
}
} catch (EncodingException e) {
ok = false;
error += e.getMessage();
}
if( !mailSent && lastResponse.length() > 0 ) {
String[] lines = lastResponse.split( "\r\n" );
for( int i = 0; i < lines.length; i++ )
error += lines[i] + "<br>";
}
sendCmd( "QUIT" );
if( socket != null ) {
try {
socket.close();
}
catch (IOException e1) {
// ignore
}
}
return mailSent;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.4 KiB

View File

@ -0,0 +1,20 @@
susimail.encodings=i2p.susi.webmail.encoding.HeaderLine;i2p.susi.webmail.encoding.QuotedPrintable;i2p.susi.webmail.encoding.Base64;i2p.susi.webmail.encoding.SevenBit;i2p.susi.webmail.encoding.EightBit;i2p.susi.webmail.encoding.HTML
susimail.host=localhost
susimail.ports.fixed=true
susimail.ports.pop3=7660
susimail.ports.smtp=7659
susimail.fast.start=true
susimail.sender.fixed=true
susimail.sender.domain=mail.i2p
susimail.pager.pagesize=20
susimail.composer.cols=80
susimail.composer.rows=15
susimail.composer.bcc.to.self=true
susimail.date.format=MM/dd/yyyy HH:mm:ss