KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jsmtpd > plugins > deliveryServices > MaildirWriter


1 /*
2  *
3  * Jsmtpd, Java SMTP daemon
4  * Copyright (C) 2005-2006 Jean-Francois POUX, jf.poux@laposte.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */

21
22 package org.jsmtpd.plugins.deliveryServices;
23
24 import java.io.File JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.RandomAccessFile JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.jsmtpd.config.ReadConfig;
33 import org.jsmtpd.core.common.PluginInitException;
34 import org.jsmtpd.core.common.delivery.FatalDeliveryException;
35 import org.jsmtpd.core.common.delivery.IDeliveryService;
36 import org.jsmtpd.core.common.delivery.TemporaryDeliveryException;
37 import org.jsmtpd.core.mail.Email;
38 import org.jsmtpd.core.mail.Rcpt;
39 import org.jsmtpd.tools.ByteArrayTool;
40 import org.jsmtpd.tools.rights.IChown;
41 import org.jsmtpd.tools.rights.RightException;
42 import org.jsmtpd.tools.rights.UnixChown;
43 /**
44  * This has been tested with solid-pop3d, under linux.
45  * There is nothing in java to change the owner of a file (as far as I know ;),
46  * but some (many) pop/imap daemons working with mdir requires permissions to be set.
47  * The current impl to change right invokes a system command (fork process ...), it's not
48  * really beautifull, you have to run Jsmtpd as root, but it works.
49  *
50  * Don't use in production/stressed environment, behavior should not be good.
51  *
52  * @author Jean-Francois POUX
53  *
54  */

55 public class MaildirWriter implements IDeliveryService {
56     private String JavaDoc basePath="/var/mail";
57     private String JavaDoc postPath="";
58     private String JavaDoc host = ReadConfig.getInstance().getLocalDomain();
59     private Log log = LogFactory.getLog(MaildirWriter.class);
60     private IChown chown = new UnixChown();
61     private boolean tryChown=false;
62     private boolean crlf=false;
63     
64     public void setCrlf(boolean crlf) {
65         this.crlf = crlf;
66     }
67
68     public void setChown(IChown chown) {
69         this.chown = chown;
70     }
71
72     public void doDelivery(Email in, List JavaDoc<Rcpt> rcpts) {
73         log.debug("Starting batch");
74         for (Rcpt rcpt : rcpts) {
75             try {
76                 deliver(in,rcpt);
77                 rcpt.setDelivered(Rcpt.STATUS_DELIVERED);
78                 log.debug("mail delivered to "+rcpt.getEmailAddress().toString());
79             } catch (FatalDeliveryException e) {
80                 rcpt.setDelivered(Rcpt.STATUS_ERROR_FATAL);
81                 log.debug("fatal delivery error for "+rcpt.getEmailAddress().toString());
82             } catch (TemporaryDeliveryException e) {
83                 rcpt.setDelivered(Rcpt.STATUS_ERROR_NOT_FATAL);
84                 log.debug("temporary delivery error for "+rcpt.getEmailAddress().toString());
85             } catch (RightException e) {
86                 log.debug("Fatal delivery error for rcpt (right error)"+rcpt.getEmailAddress().toString());
87                 rcpt.setDelivered(Rcpt.STATUS_ERROR_FATAL);
88             }
89         }
90         log.debug("ending batch");
91     }
92
93     public String JavaDoc getPluginName() {
94         return "Maildir Writer for Jsmtpd";
95     }
96
97     public void initPlugin() throws PluginInitException {
98         File JavaDoc fl = new File JavaDoc(basePath);
99         fl.mkdir();
100     }
101
102     public void shutdownPlugin() {
103     }
104     
105     private void deliver (Email in, Rcpt rcpt) throws TemporaryDeliveryException,FatalDeliveryException, RightException {
106         String JavaDoc user = rcpt.getEmailAddress().getUser().toLowerCase(); // This assumes that a rcpt rewriter plugin exists for handling aliases, and rewrites to system uids.
107

108         // Make sure dirs exists
109
String JavaDoc mpath = basePath+"/"+rcpt.getEmailAddress().getUser()+"/"+postPath+"/";
110         File JavaDoc fl = new File JavaDoc (mpath);
111         // if no exists mkdirs + rchown
112
if (!fl.exists()) {
113             fl.mkdirs();
114             if (tryChown)
115                 chown.recursiveChown(fl.toString(),user);
116         }
117         File JavaDoc tmp = new File JavaDoc (mpath+"/tmp/");
118         if (!tmp.exists()) {
119             tmp.mkdirs();
120             if (tryChown)
121                 chown.recursiveChown(tmp.toString(),user);
122         }
123         File JavaDoc cur = new File JavaDoc (mpath+"/cur/");
124         if (!cur.exists()) {
125             cur.mkdirs();
126             if (tryChown)
127                 chown.recursiveChown(cur.toString(),user);
128         }
129         File JavaDoc newF = new File JavaDoc(mpath+"/new/");
130         if (!newF.exists()) {
131             newF.mkdirs();
132             if (tryChown)
133                 chown.chown(newF.toString(),user);
134         }
135         
136         // Write to tmp directory
137
String JavaDoc fileName = makeUid();
138         File JavaDoc target = new File JavaDoc (tmp.getAbsolutePath()+"/"+fileName);
139         RandomAccessFile JavaDoc raf;
140             try {
141                 raf = new RandomAccessFile JavaDoc (target,"rw");
142                 raf.seek(0);
143                 //DataStreamParser dsp = new DataStreamParser(512, 512);
144
//dsp.appendString("From " + in.getFrom().toString() + " " + DateUtil.currentMailboxDate());
145
//raf.write(ByteArrayTool.replaceBytes(dsp.getData(), ByteArrayTool.CRLF, ByteArrayTool.LF));
146
if (crlf) {
147                     raf.write(ByteArrayTool.replaceBytes(in.getDataAsByte(),ByteArrayTool.LF,ByteArrayTool.CRLF));
148                     byte[] lf = new byte[2];
149                     lf[0] = 13;
150                     lf[1] = 10;
151                     raf.write(lf);
152                 }
153                 else {
154                     raf.write(ByteArrayTool.replaceBytes(in.getDataAsByte(),ByteArrayTool.CRLF,ByteArrayTool.LF));
155                     byte[] lf = new byte[1];
156                     lf[0] = 10;
157                     raf.write(lf);
158                 }
159                 
160                 raf.close();
161             } catch (FileNotFoundException JavaDoc e) {
162                 log.error("File not found: "+e);
163                 throw new TemporaryDeliveryException(e);
164             } catch (IOException JavaDoc e) {
165                 log.error("Io error ",e);
166                 throw new TemporaryDeliveryException (e);
167             }
168
169         if (tryChown)
170             chown.chown(target.toString(),user);
171         
172         // Commit = mv
173
File JavaDoc commit = new File JavaDoc (cur.getAbsolutePath()+"/"+fileName);
174         target.renameTo(commit);
175     }
176     
177     /**
178      * See spec of D. J. Bernstein and what we can do in Java
179      * @return
180      */

181     private String JavaDoc makeUid () {
182         return ""+System.currentTimeMillis()+"."+Thread.currentThread().getId()+"."+host;
183     }
184
185     /**
186      * The path is made of basePath/user/postPath/
187      * @param basePath maildir storage prefix
188      */

189     public void setBasePath(String JavaDoc basePath) {
190         this.basePath = basePath;
191     }
192     
193     /**
194      * The path is made of basePath/user/postPath/
195      * @param postPath path suffix.
196      */

197     public void setPostPath(String JavaDoc postPath) {
198         this.postPath = postPath;
199     }
200
201     public void setTryChown(boolean tryChown) {
202         this.tryChown = tryChown;
203     }
204     
205 }
206
Popular Tags