Source code: plugins/NewsService/NewsService.java
1 /*
2 This file is part of DeXter - Java Internet Communication Solution
3 Copyright (c) 2002 Tobias Riemer
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 /*
21 * NewsService.java
22 *
23 * Created on 21. September 2002, 22:03
24 */
25
26 package plugins.NewsService;
27 import dexter.swingExtensions.*;
28 import dexter.core.*;
29 import dexter.property.*;
30 import java.util.Vector;
31 import javax.swing.tree.*;
32 import java.io.*;
33 import java.net.*;
34 import java.lang.*;
35 import java.util.*;
36
37 /**
38 *
39 * @author Tobias Riemer
40 */
41 public class NewsService extends dexter.core.DefaultService {
42
43 static boolean verbose = false;
44
45 Socket sock = null;
46 BufferedReader in = null;
47 PrintWriter out = null;
48
49 Vector nodes = new Vector();
50 DefaultMutableTreeNode node = null;
51
52 Property pServer = new Property("Server", Property.STRING_VALUE, "news.chello.at");
53 Property pPort = new Property("Port", Property.INT_VALUE, 119);
54 Property pLast = new Property("Numbers of Msg", Property.INT_VALUE, 3);
55 Property pGroups = new Property("Newsgroup", Property.VECTOR_VALUE);
56 Property pSleep = new Property("Sleep",Property.INT_VALUE, 30000);
57
58 Vector groups = new Vector();
59
60 /** Creates a new instance of NewsService */
61 public NewsService() {
62 super("NewsService");
63
64 setServiceName("News");
65 setIcon(new javax.swing.ImageIcon(getClass().getResource("/dexter/images/news.gif")));
66
67 root = new DefaultMutableTreeNode(this);
68 propertyFile.setFileName(Dexter.getInstance().getHomeDir() + "news");
69 PropertyGroup pg = new PropertyGroup("General");
70 propertyFile.addGroup(pg);
71 pg.addProperty(pServer);
72 pGroups.setDisplayType(Property.VECTOR_TEXTFIELD);
73 pg.addProperty(pPort);
74 pg.addProperty(pSleep);
75 pg.addProperty(pGroups);
76 pGroups.setVector(groups);
77
78 setToolTipText(getServer());
79 }
80
81 public void initRun() {
82 super.initRun();
83 fireNodeChanged(root);
84 }
85
86 public void doRun() {
87
88 try {
89 //System.out.println("size: " + groups.size());
90 for (int gi=0;gi<groups.size();gi++) {
91 sock = new Socket(getServer(), getPort());
92 System.out.println(getServer() + " " + getPort());
93 in = new BufferedReader(
94 new InputStreamReader(
95 sock.getInputStream()));
96 out = new PrintWriter(
97 sock.getOutputStream(), true);
98
99 recvline("200");
100
101 sendline("group " + (String) groups.get(gi));
102
103 if (nodes.size() > gi) {
104 node = (DefaultMutableTreeNode) nodes.get(gi);
105 node.removeAllChildren();
106 fireNodeChanged(node);
107 } else {
108 node = new DefaultMutableTreeNode(new DefaultEntry((String) groups.get(gi) ,null,""));
109 nodes.add(node);
110 fireNodeAdded(root,node);
111 System.out.println("add node " +(String) groups.get(gi));
112 }
113
114 fireNodeChanged(root);
115
116 String facts = recvline("211");
117 StringTokenizer art = new StringTokenizer(facts);
118 if (art.countTokens() < 4) {
119 System.err.println("Incorrectly formed 211 response.");
120 }
121 String code = art.nextToken();
122 String num = art.nextToken();
123 String first = art.nextToken();
124 String last = art.nextToken();
125
126 int l = Integer.decode(last).intValue();
127 System.out.println("l: " + l);
128
129 for (int i=l;i>(l-getLast());i--) {
130 System.out.println("i: " + i);
131 sendline("article " + i);
132 recvline("220");
133 boolean done = false;
134 while (!done) {
135
136 String line = in.readLine();
137 if (line.indexOf("Subject") >=0) {
138 fireNodeAdded(node, new DefaultMutableTreeNode(new DefaultEntry(line.substring(8),null,"")));
139 System.out.println("sub: " + line.substring(8));
140 }
141 if (line == null) {
142 done = true;
143 } else {
144 StringTokenizer st = new StringTokenizer(line);
145 if (st.countTokens() == 1) {
146 String dot = st.nextToken();
147 if (dot.equals(".")) {
148 done = true;
149 }
150 }
151 }
152 if (!done) {
153 //System.out.println(line);
154 }
155 }
156 System.out.println("done");
157 }
158
159 sendline("quit");
160 recvline("205");
161 in.close();
162 out.close();
163 sock.close();
164 }
165 } catch (UnknownHostException e) {
166 System.err.println("Don't know about host: " + getServer());
167 e.printStackTrace();
168 //} catch (IOException e) {
169 // System.err.println("Couldn't get I/O for the connection to: " +
170 // hostName);
171 // System.exit(1);
172 }
173 catch ( java.io.IOException e2) {
174 System.err.println("IO Error: " + getServer());
175 e2.printStackTrace();
176
177 } catch (NumberFormatException nfe) {
178 }
179
180
181
182 try {
183 thread.sleep(getSleep());
184 } catch(Exception ex) {}
185 }
186
187 void sendline(String s) {
188 if (verbose) {
189 //System.out.println(">>> " + s);
190 }
191 out.println(s);
192 }
193
194 public void finalizeRun() {
195 super.finalizeRun();
196 try {
197 in.close();
198 out.close();
199 sock.close();
200 } catch ( java.io.IOException e2) {
201 System.err.println("IO Error: " + getServer());
202 }
203 }
204
205 String recvline(String expected) throws IOException,
206 NoSuchElementException {
207 String line = in.readLine();
208 if (line == null) {
209 System.out.println("*** Unexpected end of file.");
210 }
211 if (verbose) {
212 //System.out.println("<<< " + line);
213 }
214 StringTokenizer st = new StringTokenizer(line);
215 if (!st.hasMoreTokens()) {
216 //System.out.println("*** Expected line with code " + expected +
217 //" but received an empty line.");
218 }
219 String received = st.nextToken();
220 if (!received.equals(expected)) {
221 //System.out.println("*** Expected code " + expected +
222 //" but received " + received + ".");
223 }
224 return line;
225 }
226
227 public String getServer() {
228 return pServer.stringValue();
229 }
230
231 public int getSleep() {
232 return pSleep.intValue();
233 }
234
235 public int getPort() {
236 return pPort.intValue();
237 }
238
239 public int getLast() {
240 return pLast.intValue();
241 }
242
243
244 }