1 17 18 package org.apache.james.imapserver.commands; 19 20 import org.apache.james.core.MimeMessageInputStreamSource; 21 import org.apache.james.core.MimeMessageWrapper; 22 import org.apache.james.imapserver.AccessControlException; 23 import org.apache.james.imapserver.ACLMailbox; 24 import org.apache.james.imapserver.FileMailbox; 25 import org.apache.james.imapserver.ImapRequest; 26 import org.apache.james.imapserver.ImapSession; 27 import org.apache.james.imapserver.ImapSessionState; 28 import org.apache.james.imapserver.Flags; 29 import org.apache.james.imapserver.AuthorizationException; 30 import java.util.StringTokenizer ; 31 import java.util.List ; 32 import java.util.Date ; 33 import java.util.List ; 34 import java.util.ArrayList ; 35 import java.util.Iterator ; 36 import java.io.BufferedReader ; 37 import java.io.ByteArrayInputStream ; 38 import java.io.ByteArrayOutputStream ; 39 import java.io.IOException ; 40 import javax.mail.internet.MimeMessage ; 41 import javax.mail.MessagingException ; 42 import com.sun.mail.iap.Literal; 43 44 49 class AppendCommand extends AuthenticatedSelectedStateCommand { 50 public AppendCommand(){ 51 System.out.println("APPEND STARTED"); 52 this.commandName = "APPEND"; 53 this.getArgs().add( new AstringArgument( "mailbox" ) ); 54 this.getArgs().add( new AstringArgument( "flags" ) ); 55 this.getArgs().add( new AstringArgument( "date" ) ); 56 this.getArgs().add( new AstringArgument( "message" ) ); 57 } 58 59 public boolean process( ImapRequest request, ImapSession session ) { 60 StringTokenizer tokens = request.getCommandLine(); 61 List argValues = new ArrayList (); 62 63 boolean fetchNextTogether = false; 64 String tokensave = ""; 65 while(tokens.hasMoreTokens()) { 66 String token = tokens.nextToken(); 67 System.out.println("FOUND TOKEN: "+token); 68 if (token.startsWith("(") || token.startsWith("\"")) { 69 token = token.substring(1); 71 if (token.endsWith(")") || token.endsWith("\"")) { 72 token = token.substring(0,token.length()-1); 73 argValues.add(token); 74 System.out.println("ADDED1 TOKEN: "+token); 75 }else{ 76 fetchNextTogether = true; 77 tokensave = tokensave+token; 78 } 79 }else if(token.endsWith(")") || token.endsWith("\"")) { 80 token = token.substring(0,token.length()-1); 81 argValues.add(tokensave+" "+token); 82 System.out.println("ADDED2 TOKEN: "+tokensave+" "+token); 83 tokensave = ""; 84 fetchNextTogether = false; 85 }else if(fetchNextTogether) { 86 tokensave = tokensave+" "+token; 87 }else{ 88 argValues.add(token); 89 System.out.println("ADDED3 TOKEN: "+token); 90 } 91 } 92 return doProcess( request, session, argValues ); 93 } 94 95 protected boolean doProcess( ImapRequest request, ImapSession session, List argValues ) { 96 String command = this.getCommand(); 97 ByteArrayOutputStream byteout = new ByteArrayOutputStream (); 98 java.io.PrintWriter out = session.getOut(); 99 BufferedReader bre = session.getIn(); 100 101 String folder = (String ) argValues.get( 0 ); 102 String ms = ""; 103 String flags = ""; 104 String date = ""; 105 106 String arg1 = ""; 107 String arg2 = ""; 108 String arg3 = ""; 109 try { 110 arg1 = (String ) argValues.get(1); 111 arg2 = (String ) argValues.get(2); 112 arg3 = (String ) argValues.get(3); 113 }catch(Exception e) {} 114 115 if (arg1.startsWith("{")) { 116 ms = arg1; 118 }else if (arg1.startsWith("\\")) { 119 flags = arg1; 121 if (arg3.startsWith("{")) { 122 date = arg2; 123 ms = arg3; 124 }else{ 125 ms = arg2; 126 } 127 }else if (arg2.startsWith("\\")) { 128 flags = arg2; 130 ms = arg3; 131 } 132 System.out.println("APPEND: ms is "+ms); 133 System.out.println("APPEND: flags is "+flags); 134 System.out.println("APPEND: date is "+date); 135 136 try{ 137 long messagelen = Long.parseLong(ms.substring(1,ms.length()-2)); 138 long messageleft = messagelen; 139 140 session.setCanParseCommand(false); 141 out.println("+ go ahead"); 142 143 while(messageleft > 0) { 144 int buffer = 200; 145 if (messageleft<200) buffer = (int) messageleft; 146 char[] cbuf = new char[buffer] ; 147 bre.read(cbuf); 148 byteout.write(String.valueOf(cbuf).getBytes()); 149 messageleft = messageleft - buffer; 150 } 151 session.setCanParseCommand(true); 152 }catch(Exception e){ 153 System.out.println("Error occured parsing input"); 154 e.printStackTrace(); 155 } 156 157 158 FileMailbox mailbox = (FileMailbox)getMailbox( session, folder, command ); 159 160 if ( mailbox == null ) { 161 session.noResponse( command, "Must specify a mailbox." ); 162 return true; 163 } else { 164 session.setCurrentMailbox( mailbox ); 165 } 166 try { if ( !session.getCurrentMailbox().hasInsertRights( session.getCurrentUser() ) ) { 168 session.noResponse( command, "Insert access not granted." ); 169 return true; 170 } 171 172 MimeMessageInputStreamSource source; 173 try { 174 source = 175 new MimeMessageInputStreamSource("Mail" + System.currentTimeMillis() + "-" + mailbox.getNextUID(), 176 new ByteArrayInputStream (byteout.toByteArray())); 177 } catch (MessagingException me) { 178 me.printStackTrace(); 179 return false; 180 } 181 MimeMessageWrapper msg = new MimeMessageWrapper(source); 182 183 try{ 184 msg.setHeader("Received", date); 185 msg.saveChanges(); 186 }catch (MessagingException me){ 187 me.printStackTrace(); 188 } 189 mailbox.store( (MimeMessage )msg, session.getCurrentUser() ); 190 191 session.okResponse( "append completed" ); 192 return true; 193 } catch ( AuthorizationException aze ) { 194 System.out.println("AUTHERROR"); 195 session.noResponse( command, "append error: can't append to that mailbox, error in flags or date/time or message text." ); 196 session.logAZE( aze ); 197 return true; 198 } catch ( AccessControlException ace ) { 199 System.out.println("ACCESSERROR"); 200 session.noResponse( command, "[TRYCREATE " + folder + "] No such mailbox." ); 201 session.logACE( ace ); 202 return true; 203 } 204 } 205 } 206 207 | Popular Tags |