1 25 26 package org.snipsnap.util.mail; 27 28 import org.radeox.util.logging.Logger; 29 import org.snipsnap.app.Application; 30 import org.snipsnap.app.ApplicationManager; 31 import org.snipsnap.config.Configuration; 32 import org.snipsnap.config.ConfigurationManager; 33 import org.snipsnap.container.Components; 34 import org.snipsnap.snip.Blog; 35 import org.snipsnap.snip.BlogKit; 36 import org.snipsnap.snip.SnipSpaceFactory; 37 import org.snipsnap.snip.Snip; 38 import org.snipsnap.user.User; 39 import org.snipsnap.user.UserManagerFactory; 40 import org.snipsnap.net.FileUploadServlet; 41 42 import javax.mail.Address ; 43 import javax.mail.Flags ; 44 import javax.mail.Folder ; 45 import javax.mail.Message ; 46 import javax.mail.MessagingException ; 47 import javax.mail.Multipart ; 48 import javax.mail.Part ; 49 import javax.mail.Session ; 50 import javax.mail.Store ; 51 import java.io.File ; 52 import java.io.FileOutputStream ; 53 import java.io.IOException ; 54 import java.io.InputStream ; 55 import java.io.StringWriter ; 56 import java.io.Writer ; 57 import java.util.HashMap ; 58 import java.util.Iterator ; 59 import java.util.Map ; 60 import java.util.Properties ; 61 import java.util.Timer ; 62 import java.util.TimerTask ; 63 import java.util.Date ; 64 import java.util.Calendar ; 65 66 73 74 public class PostDaemon extends TimerTask { 75 private final static int INTERVAl = 5; 76 77 private static PostDaemon postDaemon; 78 79 public static synchronized PostDaemon getInstance() { 80 if (null == postDaemon) { 81 postDaemon = new PostDaemon(); 82 } 83 return postDaemon; 84 } 85 86 private Timer pop3Timer; 87 private Map pop3Tasks = new HashMap (); 88 89 92 protected PostDaemon() { 93 pop3Timer = new Timer (); 94 pop3Timer.schedule(this, INTERVAl * 60 * 1000, INTERVAl * 60 * 1000); 95 } 96 97 100 public void run() { 101 ConfigurationManager configManager = ConfigurationManager.getInstance(); 102 Iterator oids = configManager.getOids(); 103 Logger.debug("PostDaemon: checking for mail-to-blog posts ..."); 104 105 106 while (oids.hasNext()) { 107 String appOid = (String ) oids.next(); 108 Configuration config = configManager.getConfiguration(appOid); 109 110 111 int minutes = -1; 112 try { 113 minutes = Integer.parseInt(config.getMailPop3Interval()); 114 } catch (NumberFormatException e) { 115 Logger.warn("PostDaemon: interval is not a number: " + config.getMailPop3Interval()); 116 } 117 String pop3Host = config.getMailPop3Host(); 118 String pop3User = config.getMailPop3User(); 119 String pop3Pass = config.getMailPop3Password(); 120 String mailPass = config.getMailBlogPassword(); 121 122 Date date = new Date (); 123 Long executionTime = (Long ) pop3Tasks.get(appOid); 124 if (minutes >= 5 && null != pop3Host && null != pop3User && null != mailPass) { 125 if (null == executionTime || executionTime.longValue() <= date.getTime()) { 126 Logger.debug("PostDaemon: processing "+pop3Host+":"+pop3User); 127 Application.get().storeObject(Application.OID, appOid); 129 Application.get().setConfiguration(config); 130 131 process(pop3Host, pop3User, pop3Pass, mailPass); 132 133 pop3Tasks.put(appOid, new Long (date.getTime() + (minutes * 60 * 1000))); 135 } 136 } 137 } 138 } 139 140 private void process(String pop3Host, String pop3User, String pop3Pass, String mailPass) { 141 try { 142 Properties props = new Properties (); 144 145 Session session = Session.getDefaultInstance(props, null); 147 148 Store store = session.getStore("pop3"); 150 store.connect(pop3Host, pop3User, pop3Pass); 151 152 Folder folder = store.getFolder("INBOX"); 154 folder.open(Folder.READ_WRITE); 155 156 Message message[] = folder.getMessages(); 158 159 String name = BlogKit.getPostName(); 160 161 for (int i = 0, n = message.length; i < n; i++) { 162 if(message[i].getFlags().contains(Flags.Flag.DELETED)) { 163 continue; 164 } 165 166 StringWriter writer = new StringWriter (); 167 168 Logger.debug(i + ": " + message[i].getFrom()[0] 169 + "\t" + message[i].getSubject()); 170 Logger.debug(message[i].getContentType()); 171 172 Address sender = message[i].getFrom()[0]; 173 String title = message[i].getSubject(); 174 if (title != null && title.startsWith(mailPass)) { 175 title = title.substring(mailPass.length()).trim(); 178 179 try { 180 String contentType = message[i].getContentType(); 181 if (contentType.startsWith("text/plain")) { 182 writer.write((String ) message[i].getContent()); 183 } else if (contentType.startsWith("image/")) { 184 processImage(writer, message[i], name); 185 } else if (contentType.startsWith("multipart/")) { 186 processMultipart(writer, (Multipart ) message[i].getContent(), name); 188 } 189 190 Application app = Application.get(); 192 ApplicationManager appManager = (ApplicationManager) Components.getComponent(ApplicationManager.class); 193 String appOid = appManager.getApplication("/"); 194 app.storeObject(Application.OID, appOid); 195 String user = app.getConfiguration().getAdminLogin(); 196 Logger.debug(user); 197 Logger.debug((String ) app.getObject(Application.OID)); 198 User admin = UserManagerFactory.getInstance().load(user); 199 Logger.debug(admin.toString()); 200 app.setUser(admin); 201 Blog blog = SnipSpaceFactory.getInstance().getBlog(); 202 Logger.debug("PostDaemon: posting '"+title); 203 204 Snip blogSnip = blog.post(writer.getBuffer().toString(), title); 205 206 } catch (Exception e) { 207 Logger.warn("PostDaemon Error:", e); 208 } finally { 209 message[i].setFlag(Flags.Flag.DELETED, true); 212 } 213 } else { 214 Logger.warn("PostDaemon: wrong mail blog password: "+title); 215 } 216 } 217 folder.close(true); 219 store.close(); 220 } catch (Exception e) { 221 Logger.warn("PostDaemon Error", e); 222 } 223 224 } 225 226 public void processImage(Writer writer, Part part, String name) throws MessagingException , IOException { 227 writer.write("{image:"); 228 writer.write(FileUploadServlet.getCanonicalFileName(part.getFileName())); 229 storeImage(part, name); 230 writer.write("}"); 231 } 232 233 public void processMultipart(Writer writer, Multipart mp, String name) throws MessagingException { 234 for (int j = 0; j < mp.getCount(); j++) { 235 try { 236 Part part = mp.getBodyPart(j); 237 Logger.debug("Disposition=" + part.getDisposition()); 238 String contentType = part.getContentType(); 239 Logger.debug("content-type=" + contentType); 240 Logger.debug("Object=" + part); 241 if (contentType.startsWith("text/plain")) { 242 writer.write((String ) part.getContent()); 243 } else if (contentType.startsWith("image/")) { 244 processImage(writer, part, name); 245 } else if (contentType.startsWith("multipart/")) { 246 processMultipart(writer, (Multipart ) part.getContent(), name); 247 } 248 } catch (Exception e) { 249 Logger.warn("PostDaemon: Error reading message", e); 250 } 251 } 252 } 253 254 public void storeImage(Part part, String name) { 255 try { 256 if (part != null && part.getFileName() != null) { 257 Configuration config = Application.get().getConfiguration(); 258 File imageDir = new File (config.getFilePath(), "images"); 259 File file = new File (imageDir, "image-" + name + "-" + part.getFileName()); 260 Logger.debug("Uploading '" + part.getFileName() + "' to '" + file.getAbsolutePath() + "'"); 261 FileOutputStream out = new FileOutputStream (file); 262 InputStream in = part.getInputStream(); 263 byte[] buf = new byte[4096]; 264 int length = 0; 265 while ((length = in.read(buf)) != -1) { 266 out.write(buf, 0, length); 267 } 268 out.close(); 269 in.close(); 270 } else { 271 Logger.warn("PostDaemon: Error processing mail"); 272 } 273 } catch (IOException e) { 274 Logger.warn("PostDaemon: Error processing mail", e); 275 } catch (MessagingException e) { 276 Logger.warn("PostDaemon: Error processing mail", e); 277 } 278 279 } 280 281 } 282 | Popular Tags |