1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.plugin.opennap.net;
21
22 import java.io.BufferedReader;
23 import java.io.FileReader;
24 import java.io.IOException;
25
26 import org.xnap.util.PortRange;
27 import org.xnap.util.QuotedStringTokenizer;
28
29 /***
30 * Creates {@link OpenNapServer} objects from a plain text file.
31 */
32 public class OpenNapServerFileReader implements OpenNapServerReader {
33
34
35
36 private BufferedReader in = null;
37 private String filename;
38
39
40
41 public OpenNapServerFileReader(String filename)
42 {
43 this.filename = filename;
44 }
45
46
47
48 public void close()
49 {
50 try {
51 if (in != null) {
52 in.close();
53 }
54 }
55 catch (IOException e) {
56 }
57 }
58
59 public void open() throws IOException
60 {
61 in = new BufferedReader(new FileReader(filename));
62 }
63
64 public OpenNapServer read() throws IOException
65 {
66 String line;
67
68 while ((line = in.readLine()) != null) {
69 try {
70 QuotedStringTokenizer t
71 = new QuotedStringTokenizer(line, " :");
72
73 if (t.countTokens() < 2) {
74 continue;
75 }
76
77 String ip = t.nextToken();
78 int port = Integer.parseInt(t.nextToken());
79 if (port < PortRange.MIN_PORT || port > PortRange.MAX_PORT) {
80 throw new NumberFormatException();
81 }
82 String network = (t.countTokens() > 0) ? t.nextToken() : "";
83
84 OpenNapServer s = new OpenNapServer(ip, port, network);
85
86 if (t.countTokens() >= 3) {
87 String token = t.nextToken();
88 s.setNick((token.length() > 0) ? token : null);
89 token = t.nextToken();
90 s.setPassword((token.length() > 0) ? token : null);
91 token = t.nextToken();
92 s.setEmail((token.length() > 0) ? token : null);
93 }
94
95 if (t.countTokens() >= 1) {
96 s.setRedirector(t.nextToken().equals("true"));
97 }
98
99 if (t.countTokens() >= 1) {
100 s.setAutoJoinChannels(t.nextToken());
101 }
102
103 if (t.countTokens() >= 1) {
104 s.setLastConnect(Long.parseLong(t.nextToken()));
105 }
106
107 if (t.countTokens() >= 1) {
108 s.setAutoConnect(t.nextToken().equals("true"));
109
110 }
111
112 return s;
113 }
114 catch (NumberFormatException e) {
115 }
116 catch (IllegalArgumentException e) {
117 }
118 }
119
120 return null;
121 }
122
123 }