Java tutorial
// // Copyright (c) 2014 Mamesoft // Released under the MIT license // http://opensource.org/licenses/mit-license.php // package jp.mamesoft.mailsocketchat; import java.io.IOException; import java.util.Date; import java.util.HashMap; import java.util.Properties; import java.util.Timer; import java.util.TimerTask; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.mail.Folder; import javax.mail.FolderClosedException; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Store; import javax.mail.Transport; import javax.mail.event.MessageCountAdapter; import javax.mail.event.MessageCountEvent; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import org.json.JSONObject; import com.sun.mail.imap.IMAPFolder; public class Mail extends Thread { String hashtag; TimerTask repeatthread = new Repeat(); Timer repeat = new Timer(); public void run() { while (true) { try { System.out.println("????"); Properties props = System.getProperties(); // Get a Session object Session session = Session.getInstance(props, null); // session.setDebug(true); // Get a Store object Store store = session.getStore("imaps"); // Connect store.connect("imap.gmail.com", 993, Mailsocketchat.mail_user, Mailsocketchat.mail_pass); System.out.println("??????"); // Open a Folder Folder folder = store.getFolder("INBOX"); if (folder == null || !folder.exists()) { System.out.println("IMAP??????"); System.exit(1); } folder.open(Folder.READ_WRITE); // Add messageCountListener to listen for new messages folder.addMessageCountListener(new MessageCountAdapter() { public void messagesAdded(MessageCountEvent ev) { Message[] msgs = ev.getMessages(); // Just dump out the new messages for (int i = 0; i < msgs.length; i++) { try { System.out.println("?????"); InternetAddress addrfrom = (InternetAddress) msgs[i].getFrom()[0]; String subject = msgs[i].getSubject(); if (subject == null) { subject = ""; } Pattern hashtag_p = Pattern.compile("#(.+)"); Matcher hashtag_m = hashtag_p.matcher(subject); if (subject.equals("#")) { hashtag = null; } else if (hashtag_m.find()) { hashtag = hashtag_m.group(1); } String comment = msgs[i].getContent().toString().replaceAll("\r\n", " "); comment = comment.replaceAll("\n", " "); comment = comment.replaceAll("\r", " "); JSONObject data = new JSONObject(); data.put("comment", comment); if (hashtag != null) { data.put("channel", hashtag); } if (!comment.equals("") && !comment.equals(" ") && !comment.equals(" ")) { Mailsocketchat.socket.emit("say", data); System.out.println("????"); } // if (subject.equals("push") || subject.equals("Push") || subject.equals("")) { Send(addrfrom.getAddress(), 2); Mailsocketchat.push = true; Mailsocketchat.repeat = false; Mailsocketchat.address = addrfrom.getAddress(); repeatthread.cancel(); repeatthread = null; System.out.println("?????"); } else if (subject.equals("fetch") || subject.equals("Fetch") || subject.equals("?")) { Send(addrfrom.getAddress(), 3); Mailsocketchat.push = false; Mailsocketchat.repeat = false; repeatthread.cancel(); repeatthread = null; System.out.println("??????"); } else if (subject.equals("repeat") || subject.equals("Repeat") || subject.equals("")) { Send(addrfrom.getAddress(), 7); Mailsocketchat.push = false; Mailsocketchat.repeat = true; Mailsocketchat.address = addrfrom.getAddress(); if (repeatthread == null) { repeatthread = new Repeat(); repeat = new Timer(); } repeat.schedule(repeatthread, 0, 30 * 1000); System.out.println("?????"); } else if (subject.equals("list") || subject.equals("List") || subject.equals("")) { Send(addrfrom.getAddress(), 4); } else if (subject.equals("help") || subject.equals("Help") || subject.equals("")) { Send(addrfrom.getAddress(), 5); } else { if (!Mailsocketchat.push && !Mailsocketchat.repeat) { Send(addrfrom.getAddress(), 0); } else if (comment.equals("") || comment.equals(" ") || comment.equals(" ")) { Send(addrfrom.getAddress(), 5); } } } catch (IOException ioex) { System.out.println( "??????????"); } catch (MessagingException mex) { System.out.println( "??????????"); } } } }); // Check mail once in "freq" MILLIseconds int freq = 1000; //?? boolean supportsIdle = false; try { if (folder instanceof IMAPFolder) { IMAPFolder f = (IMAPFolder) folder; f.idle(); supportsIdle = true; } } catch (FolderClosedException fex) { throw fex; } catch (MessagingException mex) { supportsIdle = false; } for (;;) { if (supportsIdle && folder instanceof IMAPFolder) { IMAPFolder f = (IMAPFolder) folder; f.idle(); } else { Thread.sleep(freq); // sleep for freq milliseconds // This is to force the IMAP server to send us // EXISTS notifications. folder.getMessageCount(); } } } catch (Exception ex) { System.out.println("??????????"); } } } public static void Send(String to, int mode) { //mode 0 = ?, 1 = ?, 2 = ?, 3 = ??, 4 = , 5 = System.out.println("???"); String from = Mailsocketchat.mail_user; String host = "smtp.gmail.com"; String port = "465"; String text = ""; String subject = ""; if (mode == 0) { text = logprint(text); subject = " - MailSocketChat"; if (Mailsocketchat.newver) { text = text + "\n\n??????????\n"; } } if (mode == 1) { if (Mailsocketchat.subjectname) { subject = Mailsocketchat.logs.get(0).get("name"); } else { subject = "?? - MailSocketChat"; } text = logprint(text); } if (mode == 8) { text = logprint(text); subject = "?? - MailSocketChat"; } if (mode == 2) { if (!Mailsocketchat.push) { subject = "????? - MailSocketChat"; text = "???????????????????\n\n??????\n"; text = logprint(text); } else { subject = "??? - MailSocketChat"; text = "?????????????????\n(???????????)\n\n"; } if (Mailsocketchat.newver) { text = text + "\n\n??????????\n"; } } if (mode == 3) { if (!Mailsocketchat.push && !Mailsocketchat.repeat) { subject = "???? - MailSocketChat"; text = "????\n\n??????\n"; text = logprint(text); } else { subject = "?????? - MailSocketChat"; text = "?????????????????\n\n"; if (Mailsocketchat.repeat) { text = text + "??????\n"; text = logprint(text); } } if (Mailsocketchat.newver) { text = text + "\n\n??????????\n"; } } if (mode == 7) { if (!Mailsocketchat.repeat) { subject = "????? - MailSocketChat"; text = "?????30????????????\n\n"; if (!Mailsocketchat.push) { text = text + "??????\n"; text = logprint(text); } } else { subject = "??? - MailSocketChat"; text = "???\n\n"; } if (Mailsocketchat.newver) { text = text + "\n\n??????????\n"; } } if (mode == 4) { subject = " - MailSocketChat"; int userint = Mailsocketchat.users.size(); int romint = Mailsocketchat.roms.size(); text = ": " + userint + " ROM: " + romint + "\n\n\n"; for (Integer id : Mailsocketchat.users.keySet()) { HashMap<String, String> data = Mailsocketchat.users.get(id); text = text + data.get("name") + "\n"; text = text + " (" + data.get("ip") + ")\n"; } text = text + "\n\nROM\n"; for (Integer id : Mailsocketchat.roms.keySet()) { HashMap<String, String> data = Mailsocketchat.roms.get(id); text = text + data.get("ip") + "\n"; } if (Mailsocketchat.newver) { text = text + "\n\n??????????\n"; } } if (mode == 5) { subject = " - MailSocketChat"; if (Mailsocketchat.push) { text = "??: \n\n"; } else if (Mailsocketchat.repeat) { text = "??: \n\n"; } else { text = "??: ?\n\n"; } text = text + "?\n"; text = text + "?(fetch): ?????\n"; text = text + "(push): ????\n"; text = text + "(repeat): ????\n"; text = text + "(list): ???\n"; text = text + "#: ?????\n"; text = text + "#hoge: ??hoge????\n"; text = text + "(help): ?????\n\n"; text = text + "\nMailSocketChat Ver." + Mailsocketchat.version + "\n"; if (Mailsocketchat.newver) { text = text + "??????????\n"; } } if (mode == 6) { subject = "????????? - MailSocketChat"; text = text + "MailSocketChat??????????????????\n"; } // create some properties and get the default Session Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.transport.protocol", "smtps"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.socketFactory.fallback", "false"); props.put("mail.smtp.host", host); props.put("mail.smtp.port", port); Session sendsession = Session.getInstance(props, new PlainAuthenticator(Mailsocketchat.mail_user, Mailsocketchat.mail_pass)); try { // create a message MimeMessage msg = new MimeMessage(sendsession); msg.setFrom(new InternetAddress(from)); InternetAddress[] sendaddress = { new InternetAddress(to) }; msg.setRecipients(Message.RecipientType.TO, sendaddress); msg.setSubject(subject); msg.setSentDate(new Date()); // If the desired charset is known, you can use // setText(text, charset) msg.setText(text); Transport.send(msg); System.out.println("?????"); } catch (MessagingException mex) { System.out.println("??????"); } } static String logprint(String str) { for (int i = Mailsocketchat.logs.size() - 1; i >= 0; i--) { HashMap<String, String> log = Mailsocketchat.logs.get(i); if (Mailsocketchat.subjectname || !Mailsocketchat.push) { str = str + log.get("name") + "> "; } str = str + log.get("comment"); if (log.containsKey("channel")) { str = str + log.get("channel"); } if (Mailsocketchat.logformat.equals("all")) { str = str + " (" + log.get("ip") + "; " + log.get("time") + ")"; } if (Mailsocketchat.logformat.equals("normal")) { str = str + " (" + log.get("ip"); if (!Mailsocketchat.push) { str = str + "; " + log.get("simpletime"); } str = str + ")"; } if (Mailsocketchat.logformat.equals("simple") && !Mailsocketchat.push) { str = str + " (" + log.get("simpletime") + ")"; } str = str + "\n"; if (!Mailsocketchat.push) { str = str + "\n"; } } Mailsocketchat.logs.clear(); return str; } }