aboutsummaryrefslogtreecommitdiffstats
path: root/previousAttempts/java/imapList.java
blob: fd2ef1d4f92c8b360719fd88d85e4b3a78312b32 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.io.File;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Properties;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;

import org.ini4j.IniFile;

/** testing class for javax.mail functions
 * @author matej
 *
 */
public class imapList {
	private static Archives archiveList = new Archives();
	private static GregorianCalendar threeMonthAgo =
		new GregorianCalendar();
	
	private static void archiveFolder(Folder fld) throws MessagingException {
		fld.open(Folder.READ_WRITE);
		
		for (Message msg : fld.getMessages()) {
			GregorianCalendar msgDate = Archives.getMessageDate(msg);
			System.out.println(msgDate);
			if (msgDate.before(threeMonthAgo)) {
				archiveList.add(msg);
			}			
		}
		
		fld.close(false);
	}
	
	/**
	 * main() method setting up internal properties and then running
	 * remaining classes and methods.
	 * @param args
	 * @throws BackingStoreException 
	 * @throws MessagingException 
	 */
	public static void main(String[] args) throws BackingStoreException, MessagingException {
		Store store = null;
		String folderName = "";
		
		Properties props = System.getProperties();
		String homeFolder = props.getProperty("user.home"); 
		props.setProperty("javax.net.ssl.keyStore",
				homeFolder+"/.keystore");
		props.setProperty("javax.net.ssl.trustStore",
				homeFolder+"/.keystore");

		String iniFileName = homeFolder+"/.bugzillarc";
        Preferences prefs = new IniFile(new File(iniFileName));
		String login = prefs.node("localhost").get("user",
				props.getProperty("user.name"));
		String password = prefs.node("localhost").get("password", null);
		String hostname = prefs.node("localhost").get("host", "localhost");
		int port = prefs.node("localhost").getInt("port", 993);
		
		threeMonthAgo = (GregorianCalendar) Calendar.getInstance();
		threeMonthAgo.set(Calendar.MONTH,
				threeMonthAgo.get(Calendar.MONTH)-3);

		Session session = Session.getInstance(props,null);
		//session.setDebug(true);
		try {
			store = session.getStore("imaps");
		} catch (NoSuchProviderException e) {
			System.out.println(e.getMessage());
			System.exit(1);
		}
		try {
			store.connect(hostname, port, login, password);
		} catch (MessagingException e) {
			System.out.print(e.getMessage());
			System.exit(1);
		}
		Folder folder = store.getDefaultFolder();
		Folder inboxfolder = folder.getFolder("INBOX");
		Folder[] folderList = inboxfolder.list("*");
		for (Folder fld : folderList) {
			folderName = fld.getFullName();
			if (!folderName.startsWith("INBOX/Archiv")) {
				archiveFolder(fld);
			}
		}
		store.close();
	}
}