/* * Poller.java * * Created on February 27, 2006, 4:08 PM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package com.persistent.mailtopage; /** * * @author kiran_thakkar */ import com.ecyrd.jspwiki.SearchResult; import com.ecyrd.jspwiki.WikiContext; import com.ecyrd.jspwiki.WikiEngine; import com.ecyrd.jspwiki.WikiException; import com.ecyrd.jspwiki.WikiPage; import com.ecyrd.jspwiki.auth.NoSuchPrincipalException; import com.ecyrd.jspwiki.auth.WikiPrincipal; import com.ecyrd.jspwiki.auth.permissions.PagePermission; import com.ecyrd.jspwiki.auth.user.UserProfile; import com.ecyrd.jspwiki.providers.ProviderException; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.security.Principal; import java.util.Collection; import java.util.Date; import java.util.Iterator; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Address; import javax.mail.Authenticator; import javax.mail.BodyPart; import javax.mail.Folder; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.NoSuchProviderException; import javax.mail.Part; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Store; import javax.mail.Transport; import javax.mail.event.MessageCountEvent; import javax.mail.event.MessageCountListener; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class Poller implements Runnable{ private boolean stay = true; private String host; private String user; private String password; private String protocol; private String propertiesFile; private WikiEngine engine; private Thread t; private int timeInterval; public String getHost(){ return this.host; } public String getUser(){ return this.user; } public Poller(String host,String user,String password,String protocol,String propertiesFile,int timeInterval){ this.host = host; this.user = user; this.password = password; this.protocol = protocol; this.timeInterval = timeInterval; this.propertiesFile = propertiesFile; try{ File file = new File(this.propertiesFile); FileInputStream inStream = new FileInputStream(file); Properties p = new Properties(); p.load(inStream); engine = new WikiEngine(p); } catch(FileNotFoundException e){ e.printStackTrace(); } catch(IOException e){ e.printStackTrace(); } catch(WikiException e){ e.printStackTrace(); } this.t = new Thread(this); this.t.start(); } public void run(){ try{ Properties props = System.getProperties(); props.setProperty("mail.store.protocol", this.protocol); Session session = Session.getInstance(props); Store store = session.getStore(); store.connect(host,user,password); Folder inbox = store.getFolder("INBOX"); if (inbox == null || !inbox.exists()) { System.out.println("Invalid folder"); System.exit(1); } inbox.open(Folder.READ_ONLY); System.out.println("Initial inbox opened: " + new Date().toString()); inbox.addMessageCountListener(new MessageCountListener() { public void messagesAdded(MessageCountEvent ev) { Message[] msgs = ev.getMessages(); if(msgs.length == 1) { try { Message message = msgs[0]; String subj = message.getSubject(); InternetAddress[] adds = (InternetAddress[]) message.getFrom(); System.out.println("Message received"); System.out.println("From: "+ adds[0].getPersonal() + "Subject:" + subj); if(subj!=null){ if(subj.toLowerCase().indexOf("jspwiki")>-1){ System.out.println("Important Mail"); String command = subj.substring(subj.toLowerCase().indexOf("jspwiki")+"jspwiki".length() + 1).trim(); String pageName = null; System.out.println("command is: " + command); if(command.toLowerCase().indexOf("get")>-1){ System.out.println("Get command is called" + command); pageName = command.substring(command.toLowerCase().indexOf("get")+3).trim(); System.out.println("Page Name is: " + pageName); WikiPage page = engine.getPage(pageName); if(page!=null){ UserProfile profile = null; try{ profile = engine.getUserDatabase().findByEmail(adds[0].getAddress()); } catch(NoSuchPrincipalException e){ sendMessage(adds[0].getAddress(), "Permission denied", "You are not authorized wiki user"); } PagePermission permission = new PagePermission(page, "view"); Principal p[] = engine.getPage(pageName).getAcl().findPrincipals(permission); boolean send = true; System.out.println("User is: " + profile.getLoginName()); for(int i=0;i-1){ System.out.println("Set command is called" + command); pageName = command.substring(command.toLowerCase().indexOf("set")+3).trim(); System.out.println("Page Name is: " + pageName); WikiPage page = engine.getPage(pageName); if(page!=null){ UserProfile profile = null; try{ profile = engine.getUserDatabase().findByEmail(adds[0].getAddress()); } catch(NoSuchPrincipalException e){ sendMessage(adds[0].getAddress(), "Permission denied", "You are not authorized wiki user"); } PagePermission permission = new PagePermission(page, "edit"); Principal p[] = engine.getPage(pageName).getAcl().findPrincipals(permission); boolean send = true; System.out.println("User is: " + profile.getLoginName()); for(int i=0;i-1){ System.out.println("Find command is called" + command); pageName = command.substring(command.toLowerCase().indexOf("find")+3).trim(); System.out.println("Page Name is: " + pageName); String searchResult = ""; Collection resultCollection = null; try{ resultCollection = engine.getSearchManager().findPages(pageName); } catch(ProviderException e){ e.printStackTrace(); } catch(IOException e){ e.printStackTrace(); } Iterator iterator = resultCollection.iterator(); while(iterator.hasNext()){ SearchResult result = (SearchResult)iterator.next(); searchResult += "Page: " + result.getPage().getName() + " with score: " + result.getScore() + "\n\r"; } sendMessage(adds[0].getAddress(), "Result of your search is in the mail", searchResult); } else{ System.out.println("Error mail has been sent"); sendMessage(adds[0].getAddress(),"Bad Command or message", "There are only three commands Get, Set and Find so use any of those commands"); } } } } catch(MessagingException ex) { ex.printStackTrace(); stay = false; } } else { System.out.println("Recieved " + msgs.length + " email messages."); } } public void messagesRemoved(MessageCountEvent ev) { System.out.println("A mail has been removed"); } }); while(stay) { if(!inbox.isOpen()){ inbox.open(Folder.READ_ONLY); System.out.println("Folder opened again" + new Date().toString()); } System.out.println("Again poll has done"); inbox.getMessageCount(); try { Thread.sleep(this.timeInterval); } catch(InterruptedException ex) {} } inbox.close(true); } catch(Exception e){ e.printStackTrace(); } } public void sendAttach(String to,String pageName,String subject){ try{ Properties props = System.getProperties(); if(props!=null) System.out.println("Properties object created "); else System.out.println("problem with properties"); props.put("mail.smtp.host",this.host); props.put("mail.smtp.auth","true"); MailAuthenticator auth = new MailAuthenticator(this.user,this.password); Session session = Session.getInstance(props,auth); MimeMessage message = new MimeMessage(session); //System.out.println("Message object created"); Address toAddress = new InternetAddress(to); Address fromAddress= new InternetAddress(this.user); //System.out.println("addresses generated"); message.setFrom(fromAddress); message.addRecipient(Message.RecipientType.TO,toAddress); BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText("Wiki Page that you requested is in the attachment."); message.setSubject("Re: " + subject); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart(); FileDataSource source = new FileDataSource(engine.getWikiProperties().getProperty("jspwiki.fileSystemProvider.pageDir") + pageName + ".txt"); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(pageName + ".txt"); multipart.addBodyPart(messageBodyPart); message.setContent(multipart); //System.out.println("Message composed"); Transport.send(message); //System.out.println("Message sent"); } catch(Exception e) { System.out.println("exception is:"+e); } } public void updatePage(String to,String pageName,Message message){ try{ System.out.println("UpdatePage is called"); Multipart mp = (Multipart)message.getContent(); int n = mp.getCount(); System.out.println("Parts are: " + n); String modifiedText = ""; for (int i=0;i