summaryrefslogtreecommitdiff
path: root/tools/Purger.java
blob: 1751c8de38b1fdea5e29b9bf7d4cad0946b17143 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * Purger (c) 2006 Eugenio Favalli
 * License: GPL, v2 or later
 */
 
import java.io.*; 
import java.text.*;
import java.util.*;

 public class Purger {

	public static void main(String[] args) {
		if (args.length != 2) {
			System.out.println(
				"Usage: java Purger <folder> <date>\n" +
				" - folder: is the path to account.txt and athena.txt files.\n" +
				" - date: accounts created before this date will be purged (dd/mm/yy).");
			return;
		}
		
		int accounts = 0;
		int characters = 0;
		int deletedCharacters = 0;
		Vector activeAccounts = new Vector();
		
		File oldAccount = new File(args[0] + "account.txt");
		File oldAthena = new File(args[0] + "athena.txt");
		File newAccount = new File(args[0] + "account.txt.new");
		File newAthena = new File(args[0] + "athena.txt.new");
		
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");
		Date purgeDate;
		try {
			purgeDate = dateFormat.parse(args[1]);
		}
		catch (ParseException e) {
     			System.out.println("ERROR: Wrong date format.");
			return;
		}
		
		String line;
		try {
			FileInputStream fin =  new FileInputStream(oldAccount);
			BufferedReader input = new BufferedReader(
					new InputStreamReader(fin));
			FileOutputStream fout = new FileOutputStream(newAccount);
            PrintStream output = new PrintStream(fout);
			
			while ((line = input.readLine()) != null) {
				boolean copy = false;
				String[] fields = line.split("\t");
				// Check if we're reading a comment or the last line
				if (line.substring(0, 2).equals("//") || fields[1].charAt(0) == '%') {
					copy = true;
				}
				else {
					// Server accounts should not be purged
					if (!fields[4].equals("S")) {
						accounts++;
						dateFormat = new SimpleDateFormat("yyyy-MM-dd");						
						try {
							Date date = dateFormat.parse(fields[3]);
							if (date.after(purgeDate)) {
								activeAccounts.add(fields[0]);
								copy = true;
							}
						}
						catch (ParseException e) {
							System.out.println(
								"ERROR: Wrong date format in account.txt. ("
								+ accounts + ": " + line + ")");
							//return;
						}
						catch (Exception e) {
							e.printStackTrace();
							return;
						}
					}
					else {
						copy = true;
					}					
				}
				if (copy) {
					try {
						output.println(line);
					}
					catch (Exception e) {
						System.err.println("ERROR: Unable to write file.");
					}
				}
			}
		}
		catch (FileNotFoundException e ) {
			System.out.println(
				"ERROR: file " + oldAccount.getAbsolutePath() + " not found.");
			return;
		}
		catch (Exception e) {
			System.out.println("ERROR: unable to process account.txt");
			e.printStackTrace();
			return;
		}
		
		input.close();
		output.close();
		
		try {
			FileInputStream fin =  new FileInputStream(oldAthena);
			BufferedReader input = new BufferedReader(
					new InputStreamReader(fin));
			FileOutputStream fout = new FileOutputStream(newAthena);
            PrintStream output = new PrintStream(fout);
			
			while ((line = input.readLine()) != null) {
				boolean copy = false;
				String[] fields = line.split("\t");
				// Check if we're reading a comment or the last line
				if (line.substring(0, 2).equals("//")
						|| fields[1].charAt(0) == '%') {
					copy = true;
				}
				else {
					characters++;
					String id = fields[1].substring(0, fields[1].indexOf(','));
					if (activeAccounts.contains(id)) {
						copy = true;
					}
					else {
						deletedCharacters++;
					}
				}
				if (copy) {
					output.println(line);
				}
			}
		}
		catch (FileNotFoundException e ) {
			System.out.println(
				"ERROR: file " + oldAthena.getAbsolutePath() + " not found.");
			return;
		}
		catch (Exception e) {
			System.out.println("ERROR: unable to process athena.txt");
			e.printStackTrace();
			return;
		}
		
		input.close();
		output.close();
		
		System.out.println(
			"Removed " + (accounts - activeAccounts.size()) + "/" +
			accounts + " accounts.");
		System.out.println(
			"Removed " + deletedCharacters + "/"
			+ characters + " characters.");
	}
	
}