- further interfacing MailClient
[MUMail.cvs.git] / mumail / MUMail.java
blobb3a0269e482c0cee391feed529791ef87498c006
1 /*
2 * Copyright (C) 1998-2001 Mark Tuempfel and Uli Luckas
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * To reach the Authors you can send E-Mail to:
20 * Mark Tuempfel <marktop@cs.tu-berlin.de>
21 * Uli Luckas <luckas@cs.tu-brelin.de>
25 package mumail;
27 import java.awt.*;
28 import java.net.*;
29 import java.awt.event.*;
30 import java.beans.*;
31 import java.applet.Applet;
32 import java.io.IOException;
33 import mumail.gui.*;
34 import mumail.net.*;
35 import musoft.utils.*;
37 public class MUMail extends Applet implements ActionListener, PropertyChangeListener {
40 public static final int HiVersion = 1;
41 public static final int MedVersion = 4;
42 public static final int LowVersion = 0;
43 public static final String ID = "MUMail " + HiVersion + "." + MedVersion + "." + LowVersion;
45 public static final String LOGIN_PANEL = "LoginPanel";
46 public static final String SPLASH_PANEL = "SplashPanel";
47 public static final String SENDMAIL_PANEL = "SendMailPanel";
48 public static final String MAIL_PANEL = "MailPanel";
49 public static final String MESSAGE_PANEL = "MessagePanel";
51 public static final String Homepage = "http://www.cs.tu-berlin.de/~marktop/MUMail";
53 // configurable options
54 private static String ServerHost = "";
55 private static String ServerPort = "";
56 private static String SmtpHost = "";
57 private static String SmtpPort = "";
58 private static String User = "";
59 private static String MailDomain = "";
60 private static String Password = "";
61 public static boolean novice = false;
62 public static boolean showFontChoice = false;
63 public static boolean authorizedSend = false;
64 public static int headerListRows = 5;
65 public static boolean protocolSelect = false;
67 Timer timer;
68 LoginPanel lp;
69 MailPanel mp;
70 MessagePanel messp;
71 SplashPanel splash;
72 SendMailPanel smp;
73 MailQueue mq;
74 SmtpClient sm;
75 boolean inSplash = false;
76 String[] argv;
78 MailClientFactory factory;
80 public static boolean applet = true;
82 public static void main(String[] argv) {
84 MUMail app = new MUMail();
85 app.argv = argv;
87 app.applet = false;
88 app.init();
90 Frame f = new Frame ("MUMail");
91 f.addWindowListener(new WindowAdapter() {
92 public void windowClosing(WindowEvent event) {
93 System.exit(0);
95 });
97 Insets i = f.getInsets();
98 f.setSize(app.getPreferredSize().width+i.right+i.left, app.getPreferredSize().height+i.bottom+i.top);
99 f.setBackground(Color.lightGray);
100 f.add(app);
101 f.show();
103 app.start();
107 public void init() {
108 //System.setErr(System.out);
109 System.out.println(getAppletInfo());
110 setLayout(new CardLayout(0,0));
112 // init splash Panel
113 Image image;
114 String name = "MUMail.gif";
115 timer = new Timer(5000, new TimerAdapter(){
116 public void timerAlarm() {
117 switchFromSplashToLogin();
120 if(applet){
121 image = getImage(getCodeBase(), name);
123 else{
124 image = java.awt.Toolkit.getDefaultToolkit().getImage(name);
126 splash = new SplashPanel(image, timer);
127 //((CardLayout)getLayout()).addLayoutComponent(splash, SPLASH_PANEL);
128 add(SPLASH_PANEL, splash);
129 splash.addMouseListener(new MouseAdapter(){
130 public void mouseClicked(MouseEvent e){
131 if(timer!=null){
132 timer.cancel();
134 switchFromSplashToLogin();
137 showPanel(SPLASH_PANEL);
138 inSplash = true;
140 // init textfields in login panel
141 if(applet){
142 initApplet();
144 else{
145 initApplication();
148 // init login panel
149 factory = new MailClientFactory("mumail.net.Pop3Client:mumail.net.Imap4Client");
150 lp = new LoginPanel(ServerHost, ServerPort, SmtpHost, SmtpPort, User, Password, this, factory);
151 add(LOGIN_PANEL, lp);
155 public void switchFromSplashToLogin(){
156 if(inSplash){
157 inSplash = false;
158 boolean allParametersThere = ! (lp.getPassword().equals("") || lp.getUser().equals("") || lp.getServerHost().equals("") || lp.getSmtpHost().equals("") || lp.getServerPort().equals("") || lp.getSmtpPort().equals(""));
159 if(allParametersThere){
160 Connect();
162 else{
163 showPanel(LOGIN_PANEL);
165 timer = null;
169 public synchronized void stop() {
170 mq.Close();
173 private String getParam(String Param, String Default){
174 String dummy = getParameter(Param);
175 if(dummy==null){
176 dummy = Default;
178 return dummy;
181 public void initApplet(){
182 ServerHost = (getParam("ServerHost", getCodeBase().getHost()));
183 SmtpHost = (getParam("SmtpHost", ServerHost));
184 ServerPort = (getParam("ServerPort", "110"));
185 SmtpPort = (getParam("SmtpPort", "25"));
186 User = (getParam("User", ""));
187 MailDomain = (getParam("MailDomain", ""));
188 Password = (getParam("Password", ""));
189 showFontChoice = Boolean.valueOf(getParam("showFontChoice", "false")).booleanValue();
190 novice = Boolean.valueOf(getParam("novice", "false")).booleanValue();
191 protocolSelect = Boolean.valueOf(getParam("protocolSelect", "false")).booleanValue();
192 authorizedSend = Boolean.valueOf(getParam("authorizedSend", "false")).booleanValue();
193 try{
194 headerListRows = Integer.parseInt(getParam("headerListRows", "5"));
195 } catch (NumberFormatException e){
197 if(headerListRows<2 || headerListRows>20){
198 headerListRows = 5;
202 public void initApplication(){
203 ServerHost = ("mail");
204 SmtpHost = ("mail");
205 ServerPort = ("110");
206 SmtpPort = ("25");
207 User = (System.getProperty("user.name"));
208 Password = ("");
209 for (int i = 0; i < argv.length; i++) {
210 if (argv[i].toLowerCase().equals("-pophost") ||
211 argv[i].toLowerCase().equals("-serverhost")) {
212 ServerHost = (argv[++i]);
213 } else if (argv[i].toLowerCase().equals("-smtphost")) {
214 SmtpHost = (argv[++i]);
215 } else if (argv[i].toLowerCase().equals("-popport") ||
216 argv[i].toLowerCase().equals("-serverport")) {
217 ServerPort = (argv[++i]);
218 } else if (argv[i].toLowerCase().equals("-smtpport")) {
219 SmtpPort = (argv[++i]);
220 } else if (argv[i].toLowerCase().equals("-user")) {
221 User = (argv[++i]);
222 } else if (argv[i].toLowerCase().equals("-maildomain")) {
223 MailDomain = (argv[++i]);
224 } else if (argv[i].toLowerCase().equals("-password")) {
225 Password = (argv[++i]);
226 } else if (argv[i].toLowerCase().equals("-showfontchoice")) {
227 showFontChoice = true;
228 } else if (argv[i].toLowerCase().equals("-authorizedsend")) {
229 authorizedSend = true;
230 } else if (argv[i].toLowerCase().equals("-novice")) {
231 novice = true;
232 } else if (argv[i].toLowerCase().equals("-headerlistrows")) {
233 try{
234 headerListRows = Integer.parseInt(argv[++i]);
235 } catch(NumberFormatException e){
237 if(headerListRows<2 || headerListRows>20){
238 headerListRows = 5;
240 } else if (argv[i].toLowerCase().equals("-protocolselect")) {
241 protocolSelect = true;
242 } else {
243 System.err.println("usage: java mumail.MUMail [-smtphost <SMTP Server>] |");
244 System.err.println(" [-serverhost <Mail Server>] |");
245 System.err.println(" [-smtpport <SMTP Port>] |");
246 System.err.println(" [-serverport <Mail Port>] |");
247 System.err.println(" [-user <User Name>] |");
248 System.err.println(" [-password <Password>] |");
249 System.err.println(" [-from <my address>] |");
250 System.err.println(" [-maildomain <domain part of email adress>] |");
251 System.err.println(" [-headerListRows <number>] |");
252 System.err.println(" [-showFontChoice] |");
253 System.err.println(" [-authorizedSend] |");
254 System.err.println(" [-novice]");
255 System.err.println(" [-protocolSelect]");
256 System.exit(1);
261 public void showPanel(String s){
262 //System.out.println("showPanel("+s+")");
263 ((CardLayout)getLayout()).show(this, s);
266 public void showLastPanel(){
267 ((CardLayout)getLayout()).previous(this);
270 public void newMessagePanel(String s){
271 messp = new MessagePanel(s, this);
272 add(MESSAGE_PANEL, messp);
273 showPanel(MESSAGE_PANEL);
276 /*public void newMailPanel() throws IOException {
277 mp = new MailPanel(mq, this);
278 add(MAIL_PANEL,mp);
279 showPanel(MAIL_PANEL);
280 mp.DisplayHeaders();
283 public void newSendMailPanel(boolean reply){
285 String Domain;
286 String User = lp.getUser();
288 if (MailDomain.equals("")) {
289 Domain = lp.getServerHost();
290 int i = Domain.indexOf(".");
291 if (i == -1) {
292 if (Domain.equals("") || Domain.equals("localhost")) {
293 Domain = "";
294 } else {
295 Domain = "@" + Domain;
297 } else {
298 Domain = "@" + Domain.substring(i + 1);
300 }else {
301 Domain = "@" + MailDomain;
303 User = User + Domain;
305 if(reply){
306 smp = new SendMailPanel(mp.mo, User, this);
308 else{
309 smp = new SendMailPanel(User, this);
311 //CardLayout)getLayout()).addLayoutComponent(smp, SENDMAIL_PANEL);
312 add(SENDMAIL_PANEL, smp);
313 showPanel(SENDMAIL_PANEL);
316 public void Connect(){
317 setCursor(new Cursor(Cursor.WAIT_CURSOR));
318 try {
319 MailClient mc = factory.getMailClientByProtocolName(lp.getProtocol());
320 System.err.println(mc);
321 mq = new MailQueue(mc, lp.getServerHost(), java.lang.Integer.parseInt(lp.getServerPort()), lp.getUser(), lp.getPassword());
322 mq.addPropertyChangeListener(this);
323 //newMailPanle();
324 mq.Open();
325 mp = new MailPanel(mq, this);
326 if(applet){
327 showStatus("connected to " + lp.getServerHost() + " as " + lp.getUser());
329 add(MAIL_PANEL,mp);
330 showPanel(MAIL_PANEL);
331 mp.DisplayHeaders();
332 } catch (java.lang.Exception e2) {
333 //System.err.println(e2);
334 //e2.printStackTrace();
335 newMessagePanel(e2.getMessage());
337 setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
340 public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
341 if (propertyChangeEvent.getSource() == mq) {
342 if (propertyChangeEvent.getPropertyName() == mq.pn_Connected) {
347 public void actionPerformed(ActionEvent e) {
348 // confirm message panel
349 if (e.getActionCommand().equals("OK")){
350 showLastPanel();
351 remove(messp);
354 // from login panel
355 if ((e.getActionCommand().equals("Connect"))||(e.getSource()==lp.PasswordTF)){
356 Connect();
359 // from mail panel
360 if (e.getActionCommand().equals("Connection")) {
361 if (mq.isConnected()) {
362 mq.Close();
363 } else {
364 try{
365 mq.Open();
366 } catch(IOException exep) {
367 exep.printStackTrace();
372 // from mail panel
373 if (e.getActionCommand().equals("Close")) {
374 setCursor(new Cursor(Cursor.WAIT_CURSOR));
375 mq.Close();
376 remove(mp);
377 mp = null;
378 if(applet){
379 showStatus("MUMail login");
381 lp.setPassword("");
382 showPanel(LOGIN_PANEL);
383 setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
386 // from mail panel
387 if (e.getActionCommand().equals("Reply")){
388 newSendMailPanel(true);
391 // from mail panel AND!!! login panel
392 if (e.getActionCommand().equals("Send")){
393 newSendMailPanel(false);
396 // from sendmailpanel, means send mail now
397 if (e.getActionCommand().equals("SendMail")){
398 try {
399 sm = new SmtpClient(lp.getSmtpHost(), java.lang.Integer.parseInt(lp.getSmtpPort()));
400 sm.submitMail(smp.From.getText(), smp.To.getText(), smp.Subject.getText(), smp.Text.getText(), smp.getEncoding());
401 showLastPanel();
402 remove(smp);
403 smp = null;
404 } catch (java.lang.Exception exep) {
405 //remove(smp);
406 newMessagePanel(exep.getMessage());
407 //System.err.println(exep);
408 //exep.printStackTrace();
412 // from sendmailpanel
413 if (e.getActionCommand().equals("Cancel")){
414 showLastPanel();
415 remove(smp);
416 smp = null;
419 // from loginpanel
420 if (e.getActionCommand().equals(mumail.gui.LoginPanel.homepage)){
421 try{
422 getAppletContext().showDocument(new URL(Homepage));
424 catch (MalformedURLException excep){
428 // from loginpanel
429 if (e.getActionCommand().equals("Exit")){
430 System.exit(0);
433 // from mailpanel
434 if (e.getActionCommand().equals("Exit2")) {
435 setCursor(new Cursor(Cursor.WAIT_CURSOR));
436 mq.Close();
437 setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
438 System.exit(0);
442 public String[][] getParameterInfo() {
443 String[][] info = {
444 {"ServerHost", "String", ""},
445 {"SmtpHost", "String", ""},
446 {"ServerPort", "String", ""},
447 {"SmtpPort", "String", ""},
448 {"User", "String", ""},
449 {"Password", "String", ""},
450 {"MailDomain", "String", ""},
451 {"showFontChoice", "booelan", ""},
452 {"novice", "boolean", ""},
453 {"authorizedSend", "boolean", ""},
454 {"headerListRows", "Integer", ""},
455 {"protocolSelect", "boolean", ""},
457 return info;
460 public String getAppletInfo() {
461 return ID + " Copyright 1997 - 2001 by Uli Luckas <luckas@cs.tu-berlin.de>\n" +
462 " Mark T\u00fcmpfel <marktop@cs.tu-berlin.de>\n" +
463 "Homepage: "+Homepage+"\n"+
464 "MUMail is free software under the terms of GNU General Public License\n" +
465 "and comes with ABSOLUTELY NO WARRANTY.\n";
468 public Dimension getPreferredSize(){
469 return new Dimension(600,440);