/Users/lyon/j4p/src/net/mail/CheckMail.java

1    package net.mail; 
2     
3    import ip.gui.frames.ClosableFrame; 
4     
5    import java.applet.Applet; 
6    import java.awt.*; 
7    import java.awt.event.ActionEvent; 
8    import java.awt.event.ActionListener; 
9    import java.io.BufferedReader; 
10   import java.io.IOException; 
11   import java.io.InputStreamReader; 
12   import java.io.PrintWriter; 
13   import java.net.InetAddress; 
14   import java.net.Socket; 
15   import java.util.StringTokenizer; 
16    
17    
18   public class CheckMail extends Applet { 
19    
20       private Socket socket; 
21       private String serverHostName = null; 
22       private PrintWriter ps; 
23       private InetAddress rina, lina; 
24       private int popPort = 110; 
25       private boolean standalone = false; 
26    
27       private CheckMailPanel form; 
28    
29    
30       /** 
31        * Initialize the applet. 
32        */ 
33       public void init() { 
34           form = new CheckMailPanel(this); 
35           add(form); 
36           form.setSize(300, 300); 
37           setBackground(Color.blue); 
38           if (serverHostName == null) serverHostName = getCodeBase().getHost(); 
39           System.out.println("ServerHostName=" + serverHostName); 
40       } 
41    
42    
43       /** 
44        * Show status text to the user. 
45        */ 
46       public void showStatus(String s) { 
47    
48           System.out.println(s); 
49           if (standalone) return; 
50           super.showStatus(s); 
51       } 
52    
53       private BufferedReader buf_reader; 
54    
55       /** 
56        * Perform check for e-mail. 
57        */ 
58       public void checkForMail() throws IOException, NumberFormatException, Exception { 
59    
60           showStatus("Checking for mail on " + serverHostName); 
61    
62           socket = new Socket(serverHostName, popPort);    //  open a connection 
63           String rs; 
64    
65           try { 
66               rs = sendALine(); 
67    
68           } catch (Exception ex) { 
69               socket.close(); 
70               throw ex; 
71           } 
72    
73           // Close connection 
74           socket.close(); 
75    
76           // Parse result 
77           int r = 0; 
78           System.out.println("rs=" + rs); 
79           int i = rs.indexOf(" messages"); 
80           int locationOfOk = rs.indexOf("OK "); 
81           System.out.println("found OK at:"+locationOfOk); 
82    
83           if (i > 0) 
84               r = Integer.parseInt(rs.substring(4, i)); 
85           else { 
86    
87               StringTokenizer st = new StringTokenizer(rs); 
88               System.out.println("number of tokens="+st.countTokens()); 
89               st.nextToken(); 
90               r = Integer.parseInt(st.nextToken()); 
91           } 
92    
93           // Update result field 
94           form.getResultField().setText(Integer.toString(r)); 
95       } 
96    
97       private String sendALine() throws IOException { 
98           String returnString; 
99           rina = socket.getInetAddress(); 
100          lina = rina.getLocalHost(); 
101   
102          // 
103          //  Conversion of the PrintStream due to deprication. 
104          //     ps = new PrintStream(socket.getOutputStream()); 
105          // 
106          //  Print values and objects to a Writer. 
107          // 
108          ps = new PrintWriter( 
109                  socket.getOutputStream()); 
110   
111          // 
112          //  Conversion of the DataInputStream due to deprecation. 
113          //    dis  = new DataInputStream(socket.getInputStream()); 
114          // 
115          //  Use BufferedReader instead of DataInputStream. 
116          // 
117          buf_reader = new BufferedReader( 
118                  new InputStreamReader(socket.getInputStream())); 
119   
120          // 
121          //  Check for messages 
122          // 
123          sendline("USER " + form.getId()); 
124          returnString = sendline("PASS " + form.getPswd()); 
125          //if (returnString.charAt(0) != '+') throw new Exception("Incorrect password"); 
126          returnString = sendline("STAT"); 
127          return returnString; 
128      } 
129   
130      /** 
131       * Send a line of data to the server, and return the handshake. 
132       */ 
133      String sendline(String data) throws IOException { 
134   
135          System.out.println("sendline out:" + data); 
136          ps.println(data); 
137          ps.flush(); 
138   
139          // 
140          //  Conversion of the DataInputStream due to deprication. 
141          // 
142          // String s = dis.readLine(); 
143          // 
144          String s = buf_reader.readLine(); 
145          System.out.println("sendline in:" + s); 
146          return s; 
147      } 
148   
149   
150      /** 
151       * Main routine, for running as a standalone application. 
152       */ 
153      public static void main(String args[]) { 
154   
155          CheckMail ap = new CheckMail(); 
156          ap.serverHostName = "localhost"; 
157          ap.standalone = true; 
158   
159          ClosableFrame fr = new ClosableFrame("CheckMail"); 
160          ap.init(); 
161          fr.add("Center", ap); 
162          fr.setSize(300, 300); 
163          fr.setVisible(true); 
164          ap.start(); 
165      } 
166   
167      public Socket getSocket() { 
168          return socket; 
169      } 
170   
171      public void setSocket(Socket socket) { 
172          this.socket = socket; 
173      } 
174   
175      public String getServerHostName() { 
176          return serverHostName; 
177      } 
178   
179      public void setServerHostName(String serverHostName) { 
180          this.serverHostName = serverHostName; 
181      } 
182   
183      public PrintWriter getPs() { 
184          return ps; 
185      } 
186   
187      public void setPs(PrintWriter ps) { 
188          this.ps = ps; 
189      } 
190   
191      public InetAddress getRina() { 
192          return rina; 
193      } 
194   
195      public void setRina(InetAddress rina) { 
196          this.rina = rina; 
197      } 
198   
199      public InetAddress getLina() { 
200          return lina; 
201      } 
202   
203      public void setLina(InetAddress lina) { 
204          this.lina = lina; 
205      } 
206   
207      public int getPopPort() { 
208          return popPort; 
209      } 
210   
211      public void setPopPort(int popPort) { 
212          this.popPort = popPort; 
213      } 
214   
215      public boolean isStandalone() { 
216          return standalone; 
217      } 
218   
219      public void setStandalone(boolean standalone) { 
220          this.standalone = standalone; 
221      } 
222   
223      public CheckMailPanel getForm() { 
224          return form; 
225      } 
226   
227      public void setForm(CheckMailPanel form) { 
228          this.form = form; 
229      } 
230   
231      public BufferedReader getBuf_reader() { 
232          return buf_reader; 
233      } 
234   
235      public void setBuf_reader(BufferedReader buf_reader) { 
236          this.buf_reader = buf_reader; 
237      } 
238  } 
239   
240   
241  /** 
242   * Form for obtaining e-mail user id and password from the user. 
243   */ 
244  class CheckMailPanel 
245          extends Panel implements ActionListener { 
246   
247      private CheckMail applet; 
248   
249      // The form's elements... 
250      private Label idLabel; 
251      private TextField idField; 
252      private Label pswdLabel; 
253      private TextField pswdField; 
254      private Button button; 
255      private Label resultLabel; 
256      private TextField resultField; 
257   
258      /** 
259       * The constructor. 
260       */ 
261      public CheckMailPanel(CheckMail ap) { 
262   
263          applet = ap; 
264          setBackground(Color.blue); 
265          setLayout(new GridLayout(7, 1)); 
266   
267          // Instantiate all the elements, and add them to the form... 
268          add(button = new Button("Check For Mail")); 
269          button.addActionListener(this); 
270          add(idLabel = new Label("Id:")); 
271          add(idField = new TextField(20)); 
272          add(pswdLabel = new Label("Password:")); 
273          add(pswdField = new TextField(20)); 
274          pswdField.setEchoChar('*'); 
275          add(resultLabel = new Label("Messages Waiting:")); 
276          add(resultField = new TextField(6)); 
277          resultField.setEditable(false); 
278   
279          // Set the size of the form 
280          setSize(250, 250); 
281      } 
282   
283   
284      /** 
285       * Return the value of the ID field in the form. 
286       */ 
287      public String getId() { 
288          return idField.getText(); 
289      } 
290   
291   
292      /** 
293       * Return the value of the password field in the form. 
294       */ 
295      public String getPswd() { 
296          return pswdField.getText(); 
297      } 
298   
299   
300      // 
301      //  Updated to the new Event model. 
302      // 
303      public void actionPerformed(ActionEvent e) { 
304   
305          if (e.getSource() == button) { 
306   
307              try { 
308                  try { 
309                      applet.checkForMail(); 
310                  } catch (Exception e1) { 
311                      e1.printStackTrace();  //To change body of catch statement use Options | File Templates. 
312                  } 
313              } catch (Exception ex) { 
314                  applet.showStatus("Error; unable to check for messages:\n  " + ex.toString()); 
315                  return; 
316              } 
317              applet.showStatus("Completed."); 
318          } 
319      } 
320   
321      public CheckMail getApplet() { 
322          return applet; 
323      } 
324   
325      public void setApplet(CheckMail applet) { 
326          this.applet = applet; 
327      } 
328   
329      public Label getIdLabel() { 
330          return idLabel; 
331      } 
332   
333      public void setIdLabel(Label idLabel) { 
334          this.idLabel = idLabel; 
335      } 
336   
337      public TextField getIdField() { 
338          return idField; 
339      } 
340   
341      public void setIdField(TextField idField) { 
342          this.idField = idField; 
343      } 
344   
345      public Label getPswdLabel() { 
346          return pswdLabel; 
347      } 
348   
349      public void setPswdLabel(Label pswdLabel) { 
350          this.pswdLabel = pswdLabel; 
351      } 
352   
353      public TextField getPswdField() { 
354          return pswdField; 
355      } 
356   
357      public void setPswdField(TextField pswdField) { 
358          this.pswdField = pswdField; 
359      } 
360   
361      public Button getButton() { 
362          return button; 
363      } 
364   
365      public void setButton(Button button) { 
366          this.button = button; 
367      } 
368   
369      public Label getResultLabel() { 
370          return resultLabel; 
371      } 
372   
373      public void setResultLabel(Label resultLabel) { 
374          this.resultLabel = resultLabel; 
375      } 
376   
377      public TextField getResultField() { 
378          return resultField; 
379      } 
380   
381      public void setResultField(TextField resultField) { 
382          this.resultField = resultField; 
383      } 
384  } 
385