Improvements and refinements to web site
[zxing.git] / zxingorg / src / com / google / zxing / web / DecodeEmailTask.java
1 /*
2  * Copyright 2008 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.web;
18
19 import com.google.zxing.MultiFormatReader;
20 import com.google.zxing.Reader;
21 import com.google.zxing.ReaderException;
22 import com.google.zxing.Result;
23 import com.google.zxing.client.j2se.BufferedImageMonochromeBitmapSource;
24
25 import javax.imageio.ImageIO;
26 import javax.mail.Address;
27 import javax.mail.Authenticator;
28 import javax.mail.Flags;
29 import javax.mail.Folder;
30 import javax.mail.Message;
31 import javax.mail.MessagingException;
32 import javax.mail.Session;
33 import javax.mail.Store;
34 import javax.mail.Transport;
35 import javax.mail.internet.InternetAddress;
36 import javax.mail.internet.MimeBodyPart;
37 import javax.mail.internet.MimeMessage;
38 import javax.mail.internet.MimeMultipart;
39 import java.awt.image.BufferedImage;
40 import java.io.UnsupportedEncodingException;
41 import java.io.IOException;
42 import java.util.Properties;
43 import java.util.TimerTask;
44 import java.util.logging.Level;
45 import java.util.logging.Logger;
46
47 /**
48  * A {@link TimerTask} which repeatedly checks an e-mail account for messages with an attached
49  * image. When one is found it attempts to decode the image and replies with the decoded messages
50  * by e-mail.
51  *
52  * @author Sean Owen (srowen@google.com)
53  */
54 final class DecodeEmailTask extends TimerTask {
55
56   private static final Logger log = Logger.getLogger(DecodeEmailTask.class.getName());
57
58   private static final String SMTP_HOST = "smtp.gmail.com";
59   private static final String POP_HOST = "pop.gmail.com";
60   private static final String SMTP_PORT = "465";
61   private static final String POP_PORT = "995";
62   private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
63   private static final Properties sessionProperties = new Properties();
64   static {
65     sessionProperties.setProperty("mail.transport.protocol", "smtp");
66     sessionProperties.setProperty("mail.smtp.host", SMTP_HOST);
67     sessionProperties.setProperty("mail.smtp.auth", "true");
68     sessionProperties.setProperty("mail.smtp.port", SMTP_PORT);
69     sessionProperties.setProperty("mail.smtp.socketFactory.port", SMTP_PORT);
70     sessionProperties.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
71     sessionProperties.setProperty("mail.smtp.socketFactory.fallback", "false");
72     sessionProperties.setProperty("mail.smtp.quitwait", "false");
73     sessionProperties.setProperty("mail.pop3.host", POP_HOST);
74     sessionProperties.setProperty("mail.pop3.auth", "true");
75     sessionProperties.setProperty("mail.pop3.port", POP_PORT);
76     sessionProperties.setProperty("mail.pop3.socketFactory.port", POP_PORT);
77     sessionProperties.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
78     sessionProperties.setProperty("mail.pop3.socketFactory.fallback", "false");
79   }
80
81   private final Authenticator emailAuthenticator;
82   private final Address fromAddress;
83
84   DecodeEmailTask(String emailAddress, Authenticator emailAuthenticator) {
85     this.emailAuthenticator = emailAuthenticator;
86     try {
87       fromAddress = new InternetAddress(emailAddress, "ZXing By Email");
88     } catch (UnsupportedEncodingException uee) {
89       // Can't happen?
90       throw new RuntimeException(uee);
91     }
92   }
93
94   @Override
95   public void run() {
96     log.info("Checking email...");
97     Session session = Session.getInstance(sessionProperties, emailAuthenticator);
98     Store store = null;
99     Folder inbox = null;
100     try {
101       store = session.getStore("pop3");
102       store.connect();
103       inbox = store.getFolder("INBOX");
104       inbox.open(Folder.READ_WRITE);
105       int count = inbox.getMessageCount();
106       if (count > 0) {
107         log.info("Found " + count + " messages");
108       }
109       for (int i = 1; i <= count; i++) {
110         log.info("Processing message " + i);
111         Message message = inbox.getMessage(i);
112         processMessage(session, message);
113       }
114     } catch (Throwable t) {
115       log.log(Level.WARNING, "Unexpected error", t);
116     } finally {
117       closeResources(store, inbox);
118     }
119   }
120
121   private void processMessage(Session session, Message message) throws MessagingException, IOException {
122     Object content = message.getContent();
123     if (content instanceof MimeMultipart) {
124       MimeMultipart mimeContent = (MimeMultipart) content;
125       int numParts = mimeContent.getCount();
126       for (int j = 0; j < numParts; j++) {
127         MimeBodyPart part = (MimeBodyPart) mimeContent.getBodyPart(j);
128         processMessagePart(session, message, part);
129       }
130     }
131     message.setFlag(Flags.Flag.DELETED, true);
132   }
133
134   private void processMessagePart(Session session, Message message, MimeBodyPart part)
135       throws MessagingException, IOException {
136     String contentType = part.getContentType();
137     if (contentType.startsWith("image/")) {
138       BufferedImage image = ImageIO.read(part.getInputStream());
139       if (image != null) {
140         Reader reader = new MultiFormatReader();
141         Result result = null;
142         try {
143           result = reader.decode(new BufferedImageMonochromeBitmapSource(image), DecodeServlet.HINTS);
144         } catch (ReaderException re) {
145           log.info("Decoding FAILED");
146         }
147
148         Message reply = new MimeMessage(session);
149         Address sender = message.getFrom()[0];
150         reply.setRecipient(Message.RecipientType.TO, sender);
151         reply.setFrom(fromAddress);
152         if (result == null) {
153           reply.setSubject("Decode failed");
154           reply.setContent("Sorry, we could not decode that image.", "text/plain");
155         } else {
156           String text = result.getText();
157           reply.setSubject("Decode succeeded");
158           reply.setContent(text, "text/plain");
159         }
160         log.info("Sending reply");
161         Transport.send(reply);
162       }
163     }
164   }
165
166   private void closeResources(Store store, Folder inbox) {
167     try {
168       if (inbox != null) {
169         inbox.close(true);
170       }
171       if (store != null) {
172         store.close();
173       }
174     } catch (MessagingException me) {
175       // continue
176     }
177   }
178
179   public static void main(String[] args) {
180     String emailAddress = args[0];
181     String emailPassword = args[1];
182     Authenticator emailAuthenticator = new EmailAuthenticator(emailAddress, emailPassword);
183     new DecodeEmailTask(emailAddress, emailAuthenticator).run();
184   }
185
186 }