WebMailSession.java
上传用户:huihesys
上传日期:2007-01-04
资源大小:3877k
文件大小:52k
- /* CVS ID: $Id: WebMailSession.java,v 1.5 2000/04/18 13:12:47 wastl Exp $ */
- package net.wastl.webmail.server;
- import java.net.*;
- import java.io.*;
- import java.util.*;
- import java.text.*;
- import javax.mail.*;
- import javax.mail.event.*;
- import javax.mail.internet.*;
- import net.wastl.webmail.misc.*;
- import net.wastl.webmail.xml.*;
- import net.wastl.webmail.ui.html.JavaScriptFilter;
- import net.wastl.webmail.ui.html.HTMLifier;
- import net.wastl.webmail.ui.html.Fancyfier;
- import net.wastl.webmail.server.http.HTTPRequestHeader;
- import org.w3c.dom.*;
- /*
- * WebMailSession.java
- *
- * Created: Thu Feb 4 12:59:30 1999
- *
- * Copyright (C) 1999-2000 Sebastian Schaffert
- *
- * 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.
- */
- /**
- * A user session for WebMail.
- * Contains the state of the actual user (loads it from disk).
- * Has a unique session-ID.
- *
- *
- * @author Sebastian Schaffert
- * @version $Revision: 1.5 $
- */
- public class WebMailSession implements HTTPSession {
-
- /** When has the session been last accessed? */
- private long last_access;
- /** The session-ID for this session */
- private String session_code;
- /** Parent WebMailServer */
- private WebMailServer parent;
- /** State of the current users configuration */
- private XMLUserData user;
- private XMLUserModel model;
- /** Connections to Mailboxes */
- private Hashtable connections;
-
- /** Connections to hosts */
- private Hashtable stores;
- /** javax.mail Mailsession */
- private Session mailsession;
-
- private InetAddress remote;
-
- /* Files attached to messages will be stored here. We will have to take care of
- possible memory problems! */
- private Hashtable mime_parts_decoded;
-
- private boolean sent;
-
- private String remote_agent;
- private String remote_accepts;
-
- private int attachments_size=0;
-
- private String last_login;
-
- /** Save the login password. It will be used for the second try password if
- * opening a folder fails.
- */
- private String login_password;
-
- private Object sess=null;
- private Hashtable folders;
- protected Vector need_expunge_folders;
- protected boolean is_logged_out=false;
-
- public WebMailSession(WebMailServer parent, Object parm, HTTPRequestHeader h) throws InvalidPasswordException {
- try {
- Class srvltreq=Class.forName("javax.servlet.http.HttpServletRequest");
- if(srvltreq.isInstance(parm)) {
- javax.servlet.http.HttpServletRequest req=(javax.servlet.http.HttpServletRequest)parm;
- this.sess=req.getSession(false);
- session_code=((javax.servlet.http.HttpSession)sess).getId();
-
- try {
- remote=InetAddress.getByName(req.getRemoteHost());
- } catch(UnknownHostException e) {
- try {
- remote=InetAddress.getByName(req.getRemoteAddr());
- } catch(Exception ex) {
- try {
- remote=InetAddress.getByName("localhost");
- } catch(Exception ex2) {}
- }
- }
- } else {
- throw new Exception("Servlet class found but not running as servlet");
- }
- } catch(Throwable t) {
- this.remote=(InetAddress)parm;
- session_code=Helper.calcSessionCode(remote,h);
- }
- doInit(parent,h);
-
- }
-
- protected void doInit(WebMailServer parent, HTTPRequestHeader h) throws InvalidPasswordException {
- last_access=System.currentTimeMillis();
- this.parent=parent;
- remote_agent=h.getHeader("User-Agent").replace('n',' ');
- remote_accepts=h.getHeader("Accept").replace('n',' ');
- parent.getStorage().log(Storage.LOG_INFO,"WebMail: New Session ("+session_code+")");
- user=WebMailServer.getStorage().getUserData(h.getContent("login"),h.getContent("vdom"),h.getContent("password"));
- last_login=user.getLastLogin();
- user.login();
- login_password=h.getContent("password");
- model=parent.getStorage().createXMLUserModel(user);
- connections=new Hashtable();
- stores=new Hashtable();
- folders=new Hashtable();
- mailsession=Session.getDefaultInstance(System.getProperties(),null);
- setEnv();
- }
- public XMLUserModel getUserModel() {
- return model;
- }
- public Document getModel() {
- return model.getRoot();
- }
-
- /**
- * Calculate session-ID for a session.
- *
- * @param a Adress of the remote host
- * @param h Requestheader of the remote user agent
- * @returns Session-ID
- */
- public String calcCode(InetAddress a, HTTPRequestHeader h) {
- if(sess==null) {
- return Helper.calcSessionCode(a,h);
- } else {
- try {
- Class srvltreq=Class.forName("javax.servlet.http.HttpSession");
- if(srvltreq.isInstance(sess)) {
- return ((javax.servlet.http.HttpSession)sess).getId();
- } else {
- return "error";
- }
- } catch(Throwable t) {
- return "error";
- }
- }
- }
-
- /**
- * Login to this session.
- * Establishes connections to a user磗 Mailhosts
- *
- * @param h RequestHeader with content from Login-POST operation.
- */
- public void login(HTTPRequestHeader h) throws InvalidPasswordException {
- //user.login(h.getContent("password"));
- setLastAccess();
- setEnv();
- connectAll();
- }
-
-
- /**
- * Return a locale-specific string resource
- */
- public String getStringResource(String key) {
- return parent.getStorage().getStringResource(key,user.getPreferredLocale());
- }
-
- /**
- * Create a Message List.
- * Fetches a list of headers in folder foldername for part list_part.
- * The messagelist will be stored in the "MESSAGES" environment.
- *
- * @param foldername folder for which a message list should be built
- * @param list_part part of list to display (1 = last xx messages, 2 = total-2*xx - total-xx messages)
- */
- public void createMessageList(String folderhash,int list_part)
- throws NoSuchFolderException {
- long time_start=System.currentTimeMillis();
- TimeZone tz=TimeZone.getDefault();
- DateFormat df=DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.SHORT, user.getPreferredLocale());
- df.setTimeZone(tz);
- try {
-
- Folder folder=getFolder(folderhash);
- Element xml_folder=model.getFolder(folderhash);
- Element xml_current=model.setCurrentFolder(folderhash);
- Element xml_messagelist=model.getMessageList(xml_folder);
- if(folder == null) {
- throw new NoSuchFolderException(folderhash);
- }
-
- long fetch_start=System.currentTimeMillis();
-
- if(!folder.isOpen()) {
- folder.open(Folder.READ_ONLY);
- } else {
- folder.close(false);
- folder.open(Folder.READ_ONLY);
- }
-
- /* Calculate first and last message to show */
- int total_messages=folder.getMessageCount();
- int new_messages=folder.getNewMessageCount();
- int show_msgs=user.getMaxShowMessages();
-
- xml_messagelist.setAttribute("total",total_messages+"");
- xml_messagelist.setAttribute("new",new_messages+"");
- // System.err.println("Total: "+total_messages);
- /* Handle small messagelists correctly */
- if(total_messages < show_msgs) {
- show_msgs = total_messages;
- }
- /* Don't accept list-parts smaller than 1 */
- if(list_part < 1) {
- list_part=1;
- }
- for(int k=0;k<list_part;k++) {
- total_messages-=show_msgs;
- }
- /* Handle beginning of message list */
- if(total_messages<0) {
- total_messages=0;
- }
- int first=total_messages+1;
- int last=total_messages+show_msgs;
- /* Set environment variable */
- setEnv();
- xml_current.setAttribute("first_msg",first+"");
- xml_current.setAttribute("last_msg",last+"");
- xml_current.setAttribute("list_part",list_part+"");
- /* Fetch headers */
- FetchProfile fp=new FetchProfile();
- fp.add(FetchProfile.Item.ENVELOPE);
- fp.add(FetchProfile.Item.FLAGS);
- fp.add(FetchProfile.Item.CONTENT_INFO);
- // System.err.println("Last: "+last+", first: "+first);
- Message[] msgs=folder.getMessages(first,last);
- //System.err.println(msgs.length + " messages fetching...");
- folder.fetch(msgs,fp);
- long fetch_stop=System.currentTimeMillis();
-
- Hashtable header=new Hashtable(15);
-
- Flags.Flag[] sf;
- String from,to,cc,bcc,replyto,subject;
- String messageid;
- for(int i=msgs.length-1; i>=0; i--) {
- StringTokenizer tok=new StringTokenizer(((MimeMessage)msgs[i]).getMessageID(),"<>");
- messageid=tok.nextToken();
- XMLMessage xml_message=model.getMessage(xml_folder,msgs[i].getMessageNumber()+"",
- messageid);
-
- /* Addresses */
- from="";replyto="";to="";cc="";bcc="";
- try {
- from=MimeUtility.decodeText(Helper.joinAddress(msgs[i].getFrom()));
- replyto=MimeUtility.decodeText(Helper.joinAddress(msgs[i].getReplyTo()));
- to=MimeUtility.decodeText(Helper.joinAddress(msgs[i].getRecipients(Message.RecipientType.TO)));
- cc=MimeUtility.decodeText(Helper.joinAddress(msgs[i].getRecipients(Message.RecipientType.CC)));
- bcc=MimeUtility.decodeText(Helper.joinAddress(msgs[i].getRecipients(Message.RecipientType.BCC)));
- } catch(UnsupportedEncodingException e) {
- parent.getStorage().log(Storage.LOG_WARN,"Unsupported Encoding: "+e.getMessage());
- }
- if(from=="") from=getStringResource("unknown sender");
- if(to == "") to = getStringResource("unknown recipient");
-
- /* Flags */
- sf = msgs[i].getFlags().getSystemFlags();
- String basepath=parent.getBasePath();
-
- for(int j=0;j<sf.length;j++) {
- if(sf[j]== Flags.Flag.RECENT) xml_message.setAttribute("recent","true");
- if(sf[j]== Flags.Flag.SEEN) xml_message.setAttribute("seen","true");
- if(sf[j]== Flags.Flag.DELETED) xml_message.setAttribute("deleted","true");
- if(sf[j]== Flags.Flag.ANSWERED) xml_message.setAttribute("answered","true");
- if(sf[j]== Flags.Flag.DRAFT) xml_message.setAttribute("draft","true");
- if(sf[j]== Flags.Flag.FLAGGED) xml_message.setAttribute("flagged","true");
- if(sf[j]== Flags.Flag.USER) xml_message.setAttribute("user","true");
- }
- if(msgs[i] instanceof MimeMessage &&
- ((MimeMessage) msgs[i]).getContentType().toUpperCase().startsWith("MULTIPART/")) {
- xml_message.setAttribute("attachment","true");
- }
-
- if(msgs[i] instanceof MimeMessage) {
- int size=((MimeMessage) msgs[i]).getSize();
- size/=1024;
- xml_message.setAttribute("size",size+" kB");
- }
-
- /* Subject */
- subject="";
- if(msgs[i].getSubject() != null) {
- try {
- subject=MimeUtility.decodeText(msgs[i].getSubject());
- } catch(UnsupportedEncodingException ex) {
- parent.getStorage().log(Storage.LOG_WARN,"Unsupported Encoding: "+ex.getMessage());
- }
- }
- if(subject == null || subject.equals("")) {
- subject=getStringResource("no subject");
- }
-
- /* Set all of what we found into the DOM */
- xml_message.setHeader("FROM",from);
- try {
- xml_message.setHeader("SUBJECT",MimeUtility.decodeText(subject));
- } catch(UnsupportedEncodingException e) {
- parent.getStorage().log(Storage.LOG_WARN,
- "Unsupported Encoding: "+e.getMessage());
- }
- xml_message.setHeader("TO",to);
- xml_message.setHeader("CC",cc);
- xml_message.setHeader("BCC",bcc);
- xml_message.setHeader("REPLY-TO",replyto);
-
- /* Date */
- Date d=msgs[i].getSentDate();
- String ds="";
- if(d!=null) {
- ds=df.format(d);
- }
- xml_message.setHeader("DATE",ds);
- }
- long time_stop=System.currentTimeMillis();
-
- // try {
- // XMLCommon.writeXML(model.getRoot(),new FileOutputStream("/tmp/wmdebug"),"");
- // } catch(IOException ex) {}
- parent.getStorage().log(Storage.LOG_DEBUG,"Construction of message list took "+(time_stop-time_start)+" ms. Time for IMAP transfer was "+(fetch_stop-fetch_start)+" ms.");
- folder.close(false);
- } catch(NullPointerException e) {
- e.printStackTrace();
- throw new NoSuchFolderException(folderhash);
- } catch(MessagingException ex) {
- ex.printStackTrace();
- }
- }
-
-
- public static final int GETMESSAGE_MODE_STANDARD=0;
- public static final int GETMESSAGE_MODE_REPLY=1;
- public static final int GETMESSAGE_MODE_FORWARD=2;
- public void getMessage(String folderhash, int msgnum) throws NoSuchFolderException {
- getMessage(folderhash,msgnum,GETMESSAGE_MODE_STANDARD);
- }
- /**
- * Fetch a message from a folder.
- * Will put the messages parameters in the sessions environment
- *
- * @param foldername Name of the folder were the message should be fetched from
- * @param msgnum Number of the message to fetch
- * @param mode there are three different modes: standard, reply and forward. reply and forward will enter the message
- * into the current work element of the user and set some additional flags on the message if the user
- * has enabled this option.
- * @see GETMESSAGE_MODE_STANDARD
- * @see GETMESSAGE_MODE_REPLY
- * @see GETMESSAGE_MODE_FORWARD
- */
- public void getMessage(String folderhash, int msgnum, int mode) throws NoSuchFolderException {
- // security reasons:
- // attachments=null;
-
- try {
- TimeZone tz=TimeZone.getDefault();
- DateFormat df=DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.SHORT, user.getPreferredLocale());
- df.setTimeZone(tz);
- Folder folder=getFolder(folderhash);
- Element xml_folder=model.getFolder(folderhash);
-
- if(folder==null) {
- throw new NoSuchFolderException("No such folder: "+folderhash);
- }
-
- if(folder.isOpen() && folder.getMode()==Folder.READ_WRITE) {
- folder.close(false);
- folder.open(Folder.READ_ONLY);
- } else if(!folder.isOpen()) {
- folder.open(Folder.READ_ONLY);
- }
-
- MimeMessage m=(MimeMessage)folder.getMessage(msgnum);
- StringTokenizer tok=new StringTokenizer(m.getMessageID(),"<>");
- String messageid=tok.nextToken();
- Element xml_current=model.setCurrentMessage(messageid);
- XMLMessage xml_message=model.getMessage(xml_folder,m.getMessageNumber()+"",
- messageid);
- /* Check whether we already cached this message (not only headers but complete)*/
- boolean cached=xml_message.messageCompletelyCached();
- /* If we cached the message, we don't need to fetch it again */
- if(!cached) {
- //Element xml_header=model.getHeader(xml_message);
-
- try {
- String from=MimeUtility.decodeText(Helper.joinAddress(m.getFrom()));
- String replyto=MimeUtility.decodeText(Helper.joinAddress(m.getReplyTo()));
- String to=MimeUtility.decodeText(Helper.joinAddress(m.getRecipients(Message.RecipientType.TO)));
- String cc=MimeUtility.decodeText(Helper.joinAddress(m.getRecipients(Message.RecipientType.CC)));
- String bcc=MimeUtility.decodeText(Helper.joinAddress(m.getRecipients(Message.RecipientType.BCC)));
- Date date_orig=m.getSentDate();
- String date=getStringResource("no date");
- if(date_orig!=null) {
- date=df.format(date_orig);
- }
- String subject="";
- if(m.getSubject() != null) {
- subject=MimeUtility.decodeText(m.getSubject());
- }
- if(subject == null || subject.equals("")) {
- subject=getStringResource("no subject");
- }
-
- try {
- Flags.Flag[] sf = m.getFlags().getSystemFlags();
- for(int j=0;j<sf.length;j++) {
- if(sf[j]== Flags.Flag.RECENT) xml_message.setAttribute("recent","true");
- if(sf[j]== Flags.Flag.SEEN) xml_message.setAttribute("seen","true");
- if(sf[j]== Flags.Flag.DELETED) xml_message.setAttribute("deleted","true");
- if(sf[j]== Flags.Flag.ANSWERED) xml_message.setAttribute("answered","true");
- if(sf[j]== Flags.Flag.DRAFT) xml_message.setAttribute("draft","true");
- if(sf[j]== Flags.Flag.FLAGGED) xml_message.setAttribute("flagged","true");
- if(sf[j]== Flags.Flag.USER) xml_message.setAttribute("user","true");
- }
- } catch(NullPointerException ex) {}
- if(m.getContentType().toUpperCase().startsWith("MULTIPART/")) {
- xml_message.setAttribute("attachment","true");
- }
-
- int size=m.getSize();
- size/=1024;
- xml_message.setAttribute("size",size+" kB");
-
- /* Set all of what we found into the DOM */
- xml_message.setHeader("FROM",from);
- xml_message.setHeader("SUBJECT",Fancyfier.apply(subject));
- xml_message.setHeader("TO",to);
- xml_message.setHeader("CC",cc);
- xml_message.setHeader("BCC",bcc);
- xml_message.setHeader("REPLY-TO",replyto);
- xml_message.setHeader("DATE",date);
-
- /* Decode MIME contents recursively */
- xml_message.removeAllParts();
- parseMIMEContent(m,xml_message,messageid);
-
- } catch(UnsupportedEncodingException e) {
- parent.getStorage().log(Storage.LOG_WARN,"Unsupported Encoding in parseMIMEContent: "+e.getMessage());
- System.err.println("Unsupported Encoding in parseMIMEContent: "+e.getMessage());
- }
- }
- /* Set seen flag (Maybe make that threaded to improve performance) */
- if(user.wantsSetFlags()) {
- if(folder.isOpen() && folder.getMode()==Folder.READ_ONLY) {
- folder.close(false);
- folder.open(Folder.READ_WRITE);
- } else if(!folder.isOpen()) {
- folder.open(Folder.READ_WRITE);
- }
- folder.setFlags(msgnum,msgnum,new Flags(Flags.Flag.SEEN),true);
- folder.setFlags(msgnum,msgnum,new Flags(Flags.Flag.RECENT), false);
- if((mode & GETMESSAGE_MODE_REPLY) == GETMESSAGE_MODE_REPLY) {
- folder.setFlags(msgnum,msgnum,new Flags(Flags.Flag.ANSWERED),true);
- }
- }
- folder.close(false);
- XMLMessage work=null;
- if((mode & GETMESSAGE_MODE_REPLY) == GETMESSAGE_MODE_REPLY ||
- (mode & GETMESSAGE_MODE_FORWARD) == GETMESSAGE_MODE_FORWARD) {
- System.err.println("Setting work message!");
- work=model.setWorkMessage(xml_message);
-
- String newmsgid=WebMailServer.generateMessageID(user.getUserName());
-
- if(work != null && (mode & GETMESSAGE_MODE_REPLY) == GETMESSAGE_MODE_REPLY) {
- String from=work.getHeader("FROM");
- work.setHeader("FROM",user.getEmail());
- work.setHeader("TO",from);
- work.prepareReply(getStringResource("reply subject prefix"),
- getStringResource("reply subject postfix"),
- getStringResource("reply message prefix"),
- getStringResource("reply message postfix"));
- } else if(work != null && (mode & GETMESSAGE_MODE_FORWARD) == GETMESSAGE_MODE_FORWARD) {
- String from=work.getHeader("FROM");
- work.setHeader("FROM",user.getEmail());
- work.setHeader("TO","");
- work.setHeader("CC","");
- work.prepareForward(getStringResource("forward subject prefix"),
- getStringResource("forward subject postfix"),
- getStringResource("forward message prefix"),
- getStringResource("forward message postfix"));
- /* Copy all references to MIME parts to the new message id */
- Enumeration attids=getMimeParts(work.getAttribute("msgid"));
- while(attids.hasMoreElements()) {
- String key=(String)attids.nextElement();
- StringTokenizer tok2=new StringTokenizer(key,"/");
- tok2.nextToken();
- String newkey=tok2.nextToken();
- mime_parts_decoded.put(newmsgid+"/"+newkey,mime_parts_decoded.get(key));
- }
- }
- /* Clear the msgnr and msgid fields at last */
- work.setAttribute("msgnr","0");
- work.setAttribute("msgid",newmsgid);
- prepareCompose();
- }
- } catch(MessagingException ex) {
- ex.printStackTrace();
- }
- }
-
- /**
- Use depth-first search to go through MIME-Parts recursively.
-
- @param p Part to begin with
- */
- protected void parseMIMEContent(Part p, XMLMessagePart parent_part, String msgid) throws MessagingException {
- StringBuffer content=new StringBuffer(1000);
- XMLMessagePart xml_part;
- try {
- if(p.getContentType().toUpperCase().startsWith("TEXT") ||
- p.getContentType().toUpperCase().startsWith("MESSAGE")) {
- /* The part is a standard message part in some incarnation of text (html or plain).
- We should decode it and take care of some extra issues like recognize quoted parts,
- filter JavaScript parts and replace smileys with smiley-icons if the user has
- set wantsFancy() */
- if(p.getContentType().toUpperCase().startsWith("TEXT/HTML")) {
- xml_part=parent_part.createPart("html");
- } else {
- xml_part=parent_part.createPart("text");
- }
- BufferedReader in;
- if(p instanceof MimeBodyPart) {
- int size=p.getSize();
- MimeBodyPart mpb=(MimeBodyPart)p;
- InputStream is=mpb.getInputStream();
-
- /* Workaround for Java or Javamail Bug */
- is=new BufferedInputStream(is);
- ByteStore ba=ByteStore.getBinaryFromIS(is,size);
- in=new BufferedReader(new InputStreamReader(new ByteArrayInputStream(ba.getBytes())));
- /* End of workaround */
- size=is.available();
-
- } else {
- in=new BufferedReader(new InputStreamReader(p.getInputStream()));
- }
-
- String token="";
- int quote_level=0, old_quotelevel=0;
- boolean javascript_mode=false;
- /* Read in the message part line by line */
- while((token=in.readLine()) != null) {
- /* First decode all language and MIME dependant stuff */
- try {
- token=MimeUtility.decodeText(token);
- } catch(UnsupportedEncodingException e) {
- parent.getStorage().log(Storage.LOG_WARN,"Unsupported Encoding in parseMIMEContent: "+e.getMessage());
- }
-
- /* Here we figure out which quote level this line has, simply by counting how many
- ">" are in front of the line, ignoring all whitespaces. */
- int current_quotelevel=Helper.getQuoteLevel(token);
-
- /* When we are in a different quote level than the last line, we append all we got
- so far to the part with the old quotelevel and begin with a clean String buffer */
- if(current_quotelevel != old_quotelevel) {
- xml_part.addContent(content.toString(),old_quotelevel);
- old_quotelevel = current_quotelevel;
- content=new StringBuffer(1000);
- }
- if(user.wantsBreakLines()) {
- Enumeration enum=Helper.breakLine(token,user.getMaxLineLength(),current_quotelevel);
-
- while(enum.hasMoreElements()) {
- String s=(String)enum.nextElement();
- if(user.wantsShowFancy()) {
- content.append(Fancyfier.apply(s)).append("n");
- } else {
- content.append(s).append("n");
- }
- }
- } else {
- if(user.wantsShowFancy()) {
- content.append(Fancyfier.apply(token)).append("n");
- } else {
- content.append(token).append("n");
- }
- }
- }
- xml_part.addContent(content.toString(),old_quotelevel);
- content=new StringBuffer(1000);
- } else if(p.getContentType().toUpperCase().startsWith("MULTIPART/ALTERNATIVE")) {
- /* This is a multipart/alternative part. That means that we should pick one of
- the formats and display it for this part. Our current precedence list is
- to choose HTML first and then to choose plain text. */
- MimeMultipart m=(MimeMultipart)p.getContent();
- String[] preferred={"TEXT/PLAIN","TEXT"};
- boolean found=false;
- int alt=0;
- // Walk though our preferred list of encodings. If we have found a fitting part,
- // decode it and replace it for the parent (this is what we really want with an
- // alternative!)
- findalt: while(!found && alt < preferred.length) {
- for(int i=0;i<m.getCount();i++) {
- Part p2=m.getBodyPart(i);
- if(p2.getContentType().toUpperCase().startsWith(preferred[alt])) {
- parseMIMEContent(p2,parent_part,msgid);
- found=true;
- break findalt;
- }
- }
- alt++;
- }
- if(!found) {
- // If we didn't find one of our preferred encodings, choose the first one
- // simply pass the parent part because replacement is what we really want with
- // an alternative.
- parseMIMEContent(m.getBodyPart(0),parent_part,msgid);
- }
- } else if(p.getContentType().toUpperCase().startsWith("MULTIPART/")) {
- /* This is a standard multipart message. We should recursively walk thorugh all of
- the parts and decode them, appending as children to the current part */
- xml_part=parent_part.createPart("multi");
- MimeMultipart m=(MimeMultipart)p.getContent();
- for(int i=0;i<m.getCount();i++) {
- parseMIMEContent(m.getBodyPart(i),xml_part,msgid);
- }
- } else {
- /* Else treat the part as a binary part that the user should either download or
- get displayed immediately in case of an image */
- InputStream in=null;
- String type="";
- if(p.getContentType().toUpperCase().startsWith("IMAGE/JPG") ||
- p.getContentType().toUpperCase().startsWith("IMAGE/JPEG")) {
- type="jpg";
- xml_part=parent_part.createPart("image");
- } else if(p.getContentType().toUpperCase().startsWith("IMAGE/GIF")) {
- type="gif";
- xml_part=parent_part.createPart("image");
- } else if(p.getContentType().toUpperCase().startsWith("IMAGE/PNG")) {
- type="png";
- xml_part=parent_part.createPart("image");
- } else {
- xml_part=parent_part.createPart("binary");
- }
- int size=p.getSize();
- if(p instanceof MimeBodyPart) {
- MimeBodyPart mpb=(MimeBodyPart)p;
- // System.err.println("MIME Body part (image), Encoding: "+mpb.getEncoding());
- InputStream is=mpb.getInputStream();
-
- /* Workaround for Java or Javamail Bug */
- in=new BufferedInputStream(is);
- ByteStore ba=ByteStore.getBinaryFromIS(in,size);
- in=new ByteArrayInputStream(ba.getBytes());
- /* End of workaround */
- size=in.available();
-
- } else {
- System.err.println("*** No MIME Body part!! ***");
- in=p.getInputStream();
- }
-
- ByteStore data=ByteStore.getBinaryFromIS(in,size);
- if(mime_parts_decoded==null) {
- mime_parts_decoded=new Hashtable();
- }
- String name=p.getFileName();
- if(name == null || name.equals("")) {
- name="unknown."+type;
- }
- // Eliminate space characters. Should do some more things in the future
- name=name.replace(' ','_');
- data.setContentType(p.getContentType());
- data.setContentEncoding("BINARY");
- mime_parts_decoded.put(msgid+"/"+name,data);
- xml_part.setAttribute("filename",name);
- xml_part.setAttribute("size",size+"");
- String description=p.getDescription()==null?"":p.getDescription();
- xml_part.setAttribute("description",description);
- StringTokenizer tok=new StringTokenizer(p.getContentType(),";");
- xml_part.setAttribute("content-type",tok.nextToken().toLowerCase());
- }
- } catch(java.io.IOException ex) {
- ex.printStackTrace();
- }
- }
-
- public ByteStore getMIMEPart(String msgid,String name) {
- if(mime_parts_decoded != null) {
- return (ByteStore)mime_parts_decoded.get(msgid+"/"+name);
- } else {
- return null;
- }
- }
- public Enumeration getMimeParts(String msgid) {
- if(mime_parts_decoded == null) {
- mime_parts_decoded=new Hashtable();
- }
- Enumeration enum=mime_parts_decoded.keys();
- Vector v=new Vector();
- while(enum.hasMoreElements()) {
- String key=(String)enum.nextElement();
- if(key.startsWith(msgid)) {
- v.addElement(key);
- }
- }
- return v.elements();
- }
-
- public void clearWork() {
- clearAttachments();
- model.clearWork();
- }
- public void prepareCompose() {
- model.getWorkMessage().getFirstMessageTextPart().addContent("n--n",0);
- model.getWorkMessage().getFirstMessageTextPart().addContent(user.getSignature(),0);
- }
- public void clearAttachments() {
- attachments_size=0;
-
- XMLMessage xml_message=model.getWorkMessage();
- String msgid=xml_message.getAttribute("msgid");
- Enumeration enum=getMimeParts(msgid);
- attachments_size=0;
- while(enum.hasMoreElements()) {
- mime_parts_decoded.remove((String)enum.nextElement());
- }
- }
-
- public Hashtable getAttachments() {
- Hashtable hash=new Hashtable();
- XMLMessage xml_message=model.getWorkMessage();
- String msgid=xml_message.getAttribute("msgid");
- Enumeration enum=getMimeParts(msgid);
- while(enum.hasMoreElements()) {
- String key=(String)enum.nextElement();
- String filename=key.substring(msgid.length()+1);
- hash.put(filename,mime_parts_decoded.get(key));
- }
-
- return hash;
- }
-
- public ByteStore getAttachment(String key) {
- XMLMessage xml_message=model.getWorkMessage();
- String msgid=xml_message.getAttribute("msgid");
-
- return getMIMEPart(msgid,key);
- }
-
- public void addWorkAttachment(String name, ByteStore bs, String description) throws WebMailException {
- XMLMessage xml_message=model.getWorkMessage();
- XMLMessagePart xml_multipart=xml_message.getFirstMessageMultiPart();
- String msgid=xml_message.getAttribute("msgid");
- bs.setDescription(description);
- Enumeration enum=getMimeParts(msgid);
- attachments_size=0;
- while(enum.hasMoreElements()) {
- ByteStore b=(ByteStore)mime_parts_decoded.get((String)enum.nextElement());
- attachments_size+=b.getSize();
- }
- int max_size=0;
- try {
- max_size=Integer.parseInt( parent.getStorage().getConfig("MAX ATTACH SIZE"));
- } catch(NumberFormatException e) {
- parent.getStorage().log(Storage.LOG_WARN,"Invalid setting for parameter "MAX ATTACH SIZE". Must be a number!");
- }
-
- if(attachments_size+bs.getSize() > max_size) {
- throw new WebMailException("Attachments are too big. The sum of the sizes may not exceed "+max_size+" bytes.");
- } else {
- mime_parts_decoded.put(msgid+"/"+name,bs);
- attachments_size+=bs.getSize();
- XMLMessagePart xml_part=xml_multipart.createPart("binary");
-
- xml_part.setAttribute("filename",name);
- xml_part.setAttribute("size",bs.getSize()+"");
- xml_part.setAttribute("description",description);
- xml_part.setAttribute("content-type",bs.getContentType().toLowerCase());
- }
- setEnv();
- //XMLCommon.debugXML(model.getRoot());
- }
- public void removeWorkAttachment(String name) {
- XMLMessage xml_message=model.getWorkMessage();
- XMLMessagePart xml_multipart=xml_message.getFirstMessageMultiPart();
- String msgid=xml_message.getAttribute("msgid");
- mime_parts_decoded.remove(msgid+"/"+name);
- Enumeration enum=getMimeParts(msgid);
- attachments_size=0;
- while(enum.hasMoreElements()) {
- ByteStore b=(ByteStore)mime_parts_decoded.get((String)enum.nextElement());
- attachments_size+=b.getSize();
- }
-
- enum=xml_multipart.getParts();
- XMLMessagePart oldpart=null;
- while(enum.hasMoreElements()) {
- XMLMessagePart tmp=(XMLMessagePart)enum.nextElement();
- if(tmp.getAttribute("filename") != null &&
- tmp.getAttribute("filename").equals(name)) {
- oldpart=tmp;
- break;
- }
- }
- if(oldpart != null) {
- xml_multipart.removePart(oldpart);
- }
- setEnv();
- //XMLCommon.debugXML(model.getRoot());
- }
-
- /**
- * Store a message in the environment for further processing.
- */
- public void storeMessage(HTTPRequestHeader head) {
- XMLMessage xml_message=model.getWorkMessage();
- XMLMessagePart xml_textpart=xml_message.getFirstMessageTextPart();
-
- /* Store the already typed message if necessary/possible */
- if(head.isContentSet("BODY")) {
- StringBuffer content=new StringBuffer();
- // If the user enabled "break line", then do it!
- if(user.wantsBreakLines()) {
- StringTokenizer tok=new StringTokenizer(head.getContent("BODY"),"n");
- while(tok.hasMoreTokens()) {
- String line=tok.nextToken();
- Enumeration enum=Helper.breakLine(line,user.getMaxLineLength(),
- Helper.getQuoteLevel(line));
- while(enum.hasMoreElements()) {
- content.append((String)enum.nextElement()).append('n');
- }
- }
- } else {
- content.append(head.getContent("BODY"));
- }
- xml_textpart.removeAllContent();
- xml_textpart.addContent(content.toString(),0);
- }
- if(head.isContentSet("TO")) {
- xml_message.setHeader("TO",head.getContent("TO"));
- }
- if(head.isContentSet("CC")) {
- xml_message.setHeader("CC",head.getContent("CC"));
- }
- if(head.isContentSet("BCC")) {
- xml_message.setHeader("BCC",head.getContent("BCC"));
- }
- if(head.isContentSet("REPLY-TO")) {
- xml_message.setHeader("REPLY-TO",head.getContent("REPLY-TO"));
- }
- if(head.isContentSet("SUBJECT")) {
- xml_message.setHeader("SUBJECT",head.getContent("SUBJECT"));
- }
- setEnv();
- }
-
- /**
- * Connect to all Mailhosts
- * @deprecated Should use refreshFolderInformation now.
- */
- public void connectAll() {
- refreshFolderInformation();
- }
- /**
- Get a childfolder of a rootfolder for a specified hash value
- */
- public Folder getChildFolder(Folder root, String folderhash) {
- return getFolder(folderhash);
- }
- public Folder getFolder(String folderhash) {
- return (Folder)folders.get(folderhash);
- }
- /**
- * Construct the folder subtree for the given folder and append it to xml_parent.
- */
- protected int getFolderTree(Folder folder, Element xml_parent) {
- int depth=1;
- String id=Integer.toHexString(folder.hashCode());
- // If possible, use the MD5-Sum for the folder ID because it is persistant over sessions
- try {
- MD5 md5=new MD5(folder.getURLName());
- id=md5.asHex();
- } catch(MessagingException ex) {
- }
-
- boolean holds_folders=false,holds_messages=false;
- Element xml_folder;
- try {
- holds_folders=(folder.getType() & Folder.HOLDS_FOLDERS) == Folder.HOLDS_FOLDERS;
- holds_messages=(folder.getType() & Folder.HOLDS_MESSAGES) == Folder.HOLDS_MESSAGES;
- xml_folder=model.createFolder(id,folder.getName(),holds_folders,holds_messages);
- } catch(MessagingException ex) {
- xml_folder=model.createFolder(id,folder.getName(),holds_folders,holds_messages);
- xml_folder.setAttribute("error",ex.getMessage());
- }
- folders.put(id,folder);
- try {
- /* This folder can contain messages */
- if(holds_messages) {
- Element messagelist=model.createMessageList();
- int total_messages=folder.getMessageCount();
- int new_messages=folder.getNewMessageCount();
- if((total_messages == -1 || new_messages == -1) && !folder.isOpen()) {
- folder.open(Folder.READ_ONLY);
- total_messages=folder.getMessageCount();
- new_messages=folder.getNewMessageCount();
- }
- folder.close(false);
- messagelist.setAttribute("total",total_messages+"");
- messagelist.setAttribute("new",new_messages+"");
- xml_folder.appendChild(messagelist);
- }
- } catch(MessagingException ex) {
- xml_folder.setAttribute("error",ex.getMessage());
- }
- try {
- /* There are subfolders, get them! */
- if(holds_folders) {
- Folder[] subfolders=folder.list();
- int max_tree_depth=0;
- for(int i=0;i<subfolders.length;i++) {
- int tree_depth=getFolderTree(subfolders[i],xml_folder);
- if(tree_depth>max_tree_depth) {
- max_tree_depth=tree_depth;
- }
- }
- depth+=max_tree_depth;
- }
- } catch(MessagingException ex) {
- xml_folder.setAttribute("error",ex.getMessage());
- }
- xml_parent.appendChild(xml_folder);
- return depth;
- }
- /**
- * Refresh Information about folders.
- * Tries to connect folders that are not yet connected.
- */
- public void refreshFolderInformation() {
- setEnv();
- folders=new Hashtable();
- Folder cur_folder=null;
- String cur_mh_id="";
- Enumeration mailhosts=user.mailHosts();
- int max_depth=0;
- while(mailhosts.hasMoreElements()) {
- cur_mh_id=(String)mailhosts.nextElement();
- try {
- MailHostData mhd=user.getMailHost(cur_mh_id);
- URLName url=new URLName(mhd.getHostURL());
- Element mailhost=model.createMailhost(mhd.getName(),mhd.getID(),url.toString());
- cur_folder=getRootFolder(cur_mh_id);
-
- int depth=0;
- // Here we try to determine the remote IMAP or POP host.
-
- // Washington University
- if(cur_folder.getFolder("~"+mhd.getLogin()+"/mail").exists()) {
- // Washington University stores user mailboxes as
- // ~user/mail/...
- depth=getFolderTree(cur_folder.getFolder("INBOX"),mailhost);
- if(depth>max_depth) {
- max_depth=depth;
- }
- depth=getFolderTree(cur_folder.getFolder("~"+mhd.getLogin()+"/mail"),mailhost);
- }
- // Cyrus & Co have their folders beneath the INBOX
- else if(cur_folder.getFolder("INBOX").exists()) {
- depth=getFolderTree(cur_folder.getFolder("INBOX"),mailhost);
- }
- if(depth>max_depth) {
- max_depth=depth;
- }
- model.addMailhost(mailhost);
- } catch(MessagingException ex) {
- URLName url=new URLName(((MailHostData)user.getMailHost(cur_mh_id)).getHostURL());
- parent.getStorage().log(Storage.LOG_WARN,"Error connecting to mailhost ("+url.toString()+"): "+ex.getMessage());
-
- }
- }
- model.setStateVar("max folder depth",(1+max_depth)+"");
- }
- public void refreshFolderInformation(String folderhash) {
- Folder folder=getFolder(folderhash);
- Element xml_folder=model.getFolder(folderhash);
-
- if(xml_folder.getAttribute("holds_messages").toLowerCase().equals("true")) {
- try {
- Element messagelist=model.createMessageList();
-
- int total_messages=folder.getMessageCount();
- int new_messages=folder.getNewMessageCount();
-
- if((total_messages == -1 || new_messages == -1) && !folder.isOpen()) {
- folder.open(Folder.READ_ONLY);
- total_messages=folder.getMessageCount();
- new_messages=folder.getNewMessageCount();
- }
- folder.close(false);
-
- messagelist.setAttribute("total",total_messages+"");
- messagelist.setAttribute("new",new_messages+"");
- model.removeMessageList(xml_folder);
- xml_folder.appendChild(messagelist);
-
- } catch(MessagingException ex) {
- xml_folder.setAttribute("error",ex.getMessage());
- }
- }
-
- }
-
- /**
- Disconnect from all Mailhosts
- */
- public void disconnectAll() {
- Enumeration e=user.mailHosts();
- while(e.hasMoreElements()) {
- String name=(String)e.nextElement();
- disconnect(name);
- }
- e=stores.keys();
- while(e.hasMoreElements()) {
- String name=(String)e.nextElement();
- Store st=(Store)stores.get(name);
- try {
- st.close();
- parent.getStorage().log(Storage.LOG_INFO,"Mail: Connection to "+st.toString()+" closed.");
- } catch(MessagingException ex) {
- parent.getStorage().log(Storage.LOG_WARN,"Mail: Failed to close connection to "+st.toString()+". Reason: "+ex.getMessage());
- }
- stores.remove(name);
- }
- }
-
-
- public Folder getRootFolder(String name) throws MessagingException {
- if(connections != null && connections.containsKey(name)) {
- return (Folder)connections.get(name);
- } else {
- return connect(name);
- }
- }
-
- protected Store connectStore(String host,String protocol,String login, String password) throws MessagingException {
- /* Check whether the domain of this user allows to connect to the host */
- WebMailVirtualDomain vdom=parent.getStorage().getVirtualDomain(user.getDomain());
- if(!vdom.isAllowedHost(host)) {
- throw new MessagingException("You are not allowed to connect to this host");
- }
- /* Check if this host is already connected. Use connection if true, create a new one if false. */
- Store st=(Store)stores.get(host+"-"+protocol);
- if(st==null) {
- st=mailsession.getStore(protocol);
- stores.put(host+"-"+protocol,st);
- }
- /* Maybe this is a new store or this store has been disconnected. Reconnect if this is the case. */
- if(!st.isConnected()) {
- try {
- st.connect(host,login,password);
- parent.getStorage().log(Storage.LOG_INFO,"Mail: Connection to "+st.toString()+".");
- } catch(AuthenticationFailedException ex) {
- /* If login fails, try the login_password */
- if(!login_password.equals(password) &&
- parent.getStorage().getConfig("FOLDER TRY LOGIN PASSWORD").toUpperCase().equals("YES")) {
- st.connect(host,login,login_password);
- parent.getStorage().log(Storage.LOG_INFO,"Mail: Connection to "+st.toString()+", second attempt with login password succeeded.");
- } else {
- throw ex;
- }
- }
- }
- return st;
- }
-
- /**
- Connect to mailhost "name"
- */
- public Folder connect(String name) throws MessagingException {
- MailHostData m=user.getMailHost(name);
- URLName url=new URLName(m.getHostURL());
- Store st=connectStore(url.getHost(),url.getProtocol(),m.getLogin(),m.getPassword());
- //System.err.println("Default folder: "+st.getDefaultFolder().toString());
-
- Folder f=st.getDefaultFolder();
- connections.put(name,f);
- parent.getStorage().log(Storage.LOG_INFO,"Mail: Folder "+f.toString()+" opened at store "+st.toString()+".");
- return f;
- }
-
-
- /**
- Disconnect from mailhost "name"
- */
- public void disconnect(String name) {
- try {
- Folder f=(Folder)connections.get(name);
- if(f != null && f.isOpen()) {
- f.close(true);
- Store st=((Folder)connections.get(name)).getStore();
- //st.close();
- parent.getStorage().log(Storage.LOG_INFO,"Mail: Disconnected from folder "+f.toString()+" at store "+st.toString()+".");
- } else {
- parent.getStorage().log(Storage.LOG_WARN,"Mail: Folder "+name+" was null???.");
- }
- } catch(MessagingException ex) {
- // Should not happen
- ex.printStackTrace();
- } catch(NullPointerException ex) {
- // This happens when deleting a folder with an error
- ex.printStackTrace();
- } finally {
- connections.remove(name);
- }
- }
-
- /**
- Terminate this session.
- */
- public void logout() {
- if(!is_logged_out) {
- is_logged_out=true;
- expungeFolders();
- disconnectAll();
- saveData();
- parent.getStorage().log(Storage.LOG_INFO,"WebMail: Session "+getSessionCode()+" logout.");
- // Make sure the session is invalidated
- if(sess != null) {
- try {
- Class srvltreq=Class.forName("javax.servlet.http.HttpSession");
- if(srvltreq.isInstance(sess)) {
- ((javax.servlet.http.HttpSession)sess).invalidate();
- }
- } catch(Throwable t) {
- }
- }
- if(parent.getSession(getSessionCode()) != null) {
- parent.removeSession(this);
- }
- } else {
- System.err.println("WARNING: Session was already logged out. Ignoring logout request.");
- }
- }
-
- public boolean isLoggedOut() {
- return is_logged_out;
- }
- public String getSessionCode() {
- return session_code;
- }
-
- public long getLastAccess() {
- return last_access;
- }
-
- public void setLastAccess() {
- last_access=System.currentTimeMillis();
- }
-
- public void timeoutOccured() {
- parent.getStorage().log(Storage.LOG_WARN,"WebMail: Session "+getSessionCode()+" timeout.");
- logout();
- }
-
- public long getTimeout() {
- long i=600000;
- try {
- i=Long.parseLong(parent.getStorage().getConfig("session timeout"));
- } catch(NumberFormatException ex) {
- ex.printStackTrace();
- }
- return i;
- }
-
- public Locale getLocale() {
- return user.getPreferredLocale();
- }
-
- public void saveData() {
- parent.getStorage().saveUserData(user.getUserName(),user.getDomain());
- }
-
-
-
- protected static int[] getSelectedMessages(HTTPRequestHeader head, int max) {
- // System.err.print(" - select messages...");
-
- Enumeration e=head.getContent().keys();
- int _msgs[]=new int[max];
- int j=0;
-
- while(e.hasMoreElements()) {
- String s=(String)e.nextElement();
- if(s.startsWith("CH") && head.getContent(s).equals("on")) {
- try {
- _msgs[j]=Integer.parseInt(s.substring(3));
- // System.err.print(_msgs[j]+" ");
- j++;
- } catch(NumberFormatException ex) {
- ex.printStackTrace();
- }
- }
- }
- //System.err.println();
-
- int msgs[]=new int[j];
- for(int i=0;i<j;i++) {
- msgs[i]=_msgs[i];
- }
- return msgs;
- }
-
- public void expungeFolders() {
- if(need_expunge_folders != null) {
- Enumeration enum=need_expunge_folders.elements();
- while(enum.hasMoreElements()) {
- String hash=(String)enum.nextElement();
- //System.err.println("Expunging folder: "+hash);
- if(user.wantsSetFlags()) {
- Folder f=getFolder(hash);
- try {
- //System.err.println("URL: "+f.getURLName());
- if(f.isOpen()) {
- //System.err.println("Folder is already open ... ");
- f.close(false);
- }
- f.open(Folder.READ_WRITE);
- f.expunge();
- f.close(true);
- } catch(MessagingException ex) {
- // XXXX
- ex.printStackTrace();
- }
- }
- }
- }
- }
-
- /**
- Change the Flags of the messages the user selected.
-
- */
- public void setFlags(String folderhash, HTTPRequestHeader head) throws MessagingException {
-
- if(head.isContentSet("copymovemsgs") && head.getContent("COPYMOVE").equals("COPY")) {
- copyMoveMessage(folderhash,head.getContent("TO"),head,false);
- } else if(head.isContentSet("copymovemsgs") && head.getContent("COPYMOVE").equals("MOVE")) {
- copyMoveMessage(folderhash,head.getContent("TO"),head,true);
- } else if(head.isContentSet("flagmsgs")) {
-
- System.err.println("setting message flags");
- Folder folder=getFolder(folderhash);
-
-
- //System.err.println("Processing Request Header...");
-
- /* Get selected messages */
- int msgs[]=getSelectedMessages(head,folder.getMessageCount());
-
- //System.err.println(" - get flags...");
-
- /* Get selected flags */
- Flags fl=new Flags(Flags.Flag.USER);
- if(head.getContent("MESSAGE FLAG").equals("DELETED")) {
- fl=new Flags(Flags.Flag.DELETED);
- if(need_expunge_folders == null) {
- need_expunge_folders=new Vector();
- }
- need_expunge_folders.addElement(folderhash);
- } else if(head.getContent("MESSAGE FLAG").equals("SEEN")) {
- fl=new Flags(Flags.Flag.SEEN);
- } else if(head.getContent("MESSAGE FLAG").equals("RECENT")) {
- fl=new Flags(Flags.Flag.RECENT);
- } else if(head.getContent("MESSAGE FLAG").equals("ANSWERED")) {
- fl=new Flags(Flags.Flag.ANSWERED);
- } else if(head.getContent("MESSAGE FLAG").equals("DRAFT")) {
- fl=new Flags(Flags.Flag.DRAFT);
- }
-
- boolean value=true;
- if(head.getContent("MARK").equals("UNMARK")) {
- value=false;
- }
-
- //System.err.println("Done!");
- //System.err.println("Setting flags...");
-
- if(user.wantsSetFlags()) {
- if(folder.isOpen() && folder.getMode()==Folder.READ_ONLY) {
- folder.close(false);
- folder.open(Folder.READ_WRITE);
- } else if(!folder.isOpen()) {
- folder.open(Folder.READ_WRITE);
- }
- folder.setFlags(msgs,fl,value);
- if(user.getBoolVar("autoexpunge")) {
- folder.close(true);
- if(need_expunge_folders != null) {
- need_expunge_folders.removeElement(folderhash);
- }
- } else {
- folder.close(false);
- }
- }
- refreshFolderInformation(folderhash);
-
- }
- }
-
- /**
- * Copy or move the selected messages from folder fromfolder to folder tofolder.
- */
- public void copyMoveMessage(String fromfolder, String tofolder, HTTPRequestHeader head, boolean move) throws MessagingException {
- Folder from=getFolder(fromfolder);
- Folder to=getFolder(tofolder);
- if(user.wantsSetFlags()) {
- if(from.isOpen() && from.getMode() == Folder.READ_ONLY) {
- from.close(false);
- from.open(Folder.READ_WRITE);
- } else if(!from.isOpen()) {
- from.open(Folder.READ_WRITE);
- }
- if(to.isOpen() && to.getMode() == Folder.READ_ONLY) {
- to.close(false);
- to.open(Folder.READ_WRITE);
- } else if(!to.isOpen()) {
- to.open(Folder.READ_WRITE);
- }
- } else {
- if(!from.isOpen()) {
- from.open(Folder.READ_ONLY);
- }
- if(to.isOpen() && to.getMode() == Folder.READ_ONLY) {
- to.close(false);
- to.open(Folder.READ_WRITE);
- } else if(!to.isOpen()) {
- to.open(Folder.READ_WRITE);
- }
- }
- int m[]=getSelectedMessages(head,from.getMessageCount());
- Message msgs[]=from.getMessages(m);
- from.copyMessages(msgs,to);
- if(move && user.wantsSetFlags()) {
- from.setFlags(m,new Flags(Flags.Flag.DELETED),true);
- if(user.getBoolVar("autoexpunge")) {
- from.close(true);
- to.close(true);
- } else {
- if(need_expunge_folders == null) {
- need_expunge_folders=new Vector();
- }
- need_expunge_folders.addElement(fromfolder);
- from.close(false);
- to.close(false);
- }
- } else {
- from.close(false);
- if(user.getBoolVar("autoexpunge")) {
- to.close(true);
- } else {
- to.close(false);
- }
- }
- refreshFolderInformation(fromfolder);
- refreshFolderInformation(tofolder);
- }
-
-
- public void changeSetup(HTTPRequestHeader head) throws WebMailException {
- Enumeration contentkeys=head.getContentKeys();
- user.resetBoolVars();
- while(contentkeys.hasMoreElements()) {
- String key=((String)contentkeys.nextElement()).toLowerCase();
- if(key.startsWith("intvar")) {
- try {
- long value=Long.parseLong(head.getContent(key));
- user.setIntVar(key.substring(7),value);
- } catch(NumberFormatException ex) {
- System.err.println("Warning: Remote provided illegal intvar in request header: n("+key+","+head.getContent(key)+")");
- }
- } else if(key.startsWith("boolvar")) {
- boolean value=head.getContent(key).toUpperCase().equals("ON");
- user.setBoolVar(key.substring(8),value);
- }
- }
- user.setSignature(head.getContent("SIGNATURE"));
- user.setFullName(head.getContent("FULLNAME"));
- user.setEmail(head.getContent("EMAIL"));
- if(!head.getContent("PASSWORD").equals("")) {
- net.wastl.webmail.server.Authenticator auth=parent.getStorage().getAuthenticator();
- if(auth.canChangePassword()) {
- auth.changePassword(user,head.getContent("PASSWORD"),head.getContent("VERIFY"));
- } else {
- throw new InvalidDataException(getStringResource("EX NO CHANGE PASSWORD"));
- }
- }
- user.setPreferredLocale(head.getContent("LANGUAGE"));
- if(head.isContentSet("SENTFOLDER")) {
- System.err.println("SENTFOLDER="+head.getContent("SENTFOLDER"));
- user.setSentFolder(head.getContent("SENTFOLDER"));
- }
- refreshFolderInformation();
- setEnv();
- model.update();
- }
-
-
-
- public void addMailbox(String name, String protocol, String host, String login, String password) {
- disconnectAll();
- String host_url=protocol+"://"+host;
- user.addMailHost(name,
- host_url,
- login,
- password);
- model.update();
- connectAll();
- refreshFolderInformation();
- }
-
-
- public void removeMailbox(String id) {
- disconnectAll();
- user.removeMailHost(id);
- model.update();
- connectAll();
- }
-
- public void setAddToFolder(String id) {
- model.setStateVar("add to folder",id);
- }
- public void addFolder(String toid, String name, boolean holds_messages, boolean holds_folders)
- throws MessagingException {
-
- Folder parent=getFolder(toid);
- Folder folder=parent.getFolder(name);
- if(!folder.exists()) {
- int type=0;
- if(holds_messages) {
- type+=Folder.HOLDS_MESSAGES;
- }
- if(holds_folders) {
- type+=Folder.HOLDS_FOLDERS;
- }
- folder.create(type);
- }
- refreshFolderInformation();
- }
-
- public void removeFolder(String id, boolean recurse) throws MessagingException {
- Folder folder=getFolder(id);
- folder.close(false);
- folder.delete(recurse);
- refreshFolderInformation();
- }
- public String getEnv(String key) {
- return "";
- }
-
- public void setEnv(String key, String value) {
- }
-
- public void setEnv() {
- // This will soon replace "ENV":
- model.setStateVar("base uri",parent.getBasePath());
- model.setStateVar("img base uri",parent.getImageBasePath());
- model.setStateVar("webmail version",parent.getVersion());
- model.setStateVar("operating system",System.getProperty("os.name")+" "+
- System.getProperty("os.version")+"/"+System.getProperty("os.arch"));
- model.setStateVar("java virtual machine",System.getProperty("java.vendor")+" "+
- System.getProperty("java.vm.name")+" "+System.getProperty("java.version"));
- model.setStateVar("last login",user.getLastLogin());
- model.setStateVar("first login",user.getFirstLogin());
- model.setStateVar("session id",session_code);
- model.setStateVar("date",formatDate(System.currentTimeMillis()));
- model.setStateVar("max attach size",parent.getStorage().getConfig("MAX ATTACH SIZE"));
- model.setStateVar("current attach size",""+attachments_size);
- // Add all languages to the state
- model.removeAllStateVars("language");
- String lang=parent.getConfig("languages");
- StringTokenizer tok=new StringTokenizer(lang," ");
- while(tok.hasMoreTokens()) {
- String t=tok.nextToken();
- model.addStateVar("language",t);
- }
- model.removeAllStateVars("protocol");
- Provider[] stores=parent.getStoreProviders();
- for(int i=0; i<stores.length; i++) {
- model.addStateVar("protocol",stores[i].getProtocol());
- }
- }
-
-
- public UserData getUser() {
- return user;
- }
-
- public String getUserName() {
- return user.getLogin();
- }
-
- public InetAddress getRemoteAddress() {
- return remote;
- }
-
- public Hashtable getActiveConnections() {
- return connections;
- }
-
- public void setSent(boolean b) {
- sent=b;
- }
-
- public boolean isSent() {
- return sent;
- }
-
- private String formatDate(long date) {
- TimeZone tz=TimeZone.getDefault();
- DateFormat df=DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.DEFAULT, getLocale());
- df.setTimeZone(tz);
- String now=df.format(new Date(date));
- return now;
- }
-
-
- public void handleTransportException(SendFailedException e) {
- model.setStateVar("send status",e.getNextException().getMessage());
- model.setStateVar("valid sent addresses",Helper.joinAddress(e.getValidSentAddresses()));
- model.setStateVar("valid unsent addresses",Helper.joinAddress(e.getValidUnsentAddresses()));
- model.setStateVar("invalid addresses",Helper.joinAddress(e.getInvalidAddresses()));
- sent=true;
- }
-
-
-
- } // WebMailSession