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

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