Add .gitignore.
[MUMail.cvs.git] / mumail / MUMail.java
blob81050473618ab37acb73716ac942c59f74fedbb7
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 = 2;
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.musoft.de/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 MailAddress = "";
61 private static String Password = "";
62 public static boolean novice = false;
63 public static boolean showFontChoice = false;
64 public static boolean authorizedSend = false;
65 public static int headerListRows = 5;
66 public static boolean protocolSelect = false;
68 Timer timer;
69 LoginPanel lp;
70 MailPanel mp;
71 MessagePanel messp;
72 SplashPanel splash;
73 SendMailPanel smp;
74 MailQueue mq;
75 SmtpClient sm;
76 boolean inSplash = false;
77 String[] argv;
79 MailClientFactory factory;
81 public static boolean applet = true;
83 public static void main(String[] argv) {
85 MUMail app = new MUMail();
86 app.argv = argv;
88 app.applet = false;
89 app.init();
91 Frame f = new Frame ("MUMail");
92 f.addWindowListener(new WindowAdapter() {
93 public void windowClosing(WindowEvent event) {
94 System.exit(0);
96 });
98 Insets i = f.getInsets();
99 f.setSize(app.getPreferredSize().width+i.right+i.left, app.getPreferredSize().height+i.bottom+i.top);
100 f.setBackground(Color.lightGray);
101 f.add(app);
102 f.show();
104 app.start();
108 public void init() {
109 //System.setErr(System.out);
110 System.out.println(getAppletInfo());
111 setLayout(new CardLayout(0,0));
113 // init splash Panel
114 Image image;
115 String name = "MUMail.gif";
116 timer = new Timer(5000, new TimerAdapter(){
117 public void timerAlarm() {
118 switchFromSplashToLogin();
121 if(applet){
122 image = getImage(getCodeBase(), name);
124 else{
125 image = java.awt.Toolkit.getDefaultToolkit().getImage(name);
127 splash = new SplashPanel(image, timer);
128 //((CardLayout)getLayout()).addLayoutComponent(splash, SPLASH_PANEL);
129 add(SPLASH_PANEL, splash);
130 splash.addMouseListener(new MouseAdapter(){
131 public void mouseClicked(MouseEvent e){
132 if(timer!=null){
133 timer.cancel();
135 switchFromSplashToLogin();
138 showPanel(SPLASH_PANEL);
139 inSplash = true;
141 // init textfields in login panel
142 if(applet){
143 initApplet();
145 else{
146 initApplication();
149 // init login panel
150 factory = new MailClientFactory("mumail.net.Pop3Client:mumail.net.Imap4Client");
151 lp = new LoginPanel(ServerHost, ServerPort, SmtpHost, SmtpPort, User, Password, this, factory);
152 add(LOGIN_PANEL, lp);
156 public void switchFromSplashToLogin(){
157 if(inSplash){
158 inSplash = false;
159 boolean allParametersThere = ! (lp.getPassword().equals("") || lp.getUser().equals("") || lp.getServerHost().equals("") || lp.getSmtpHost().equals("") || lp.getServerPort().equals("") || lp.getSmtpPort().equals(""));
160 if(allParametersThere){
161 Connect();
163 else{
164 showPanel(LOGIN_PANEL);
166 timer = null;
170 public synchronized void stop() {
171 if (mq != null) {
172 mq.Close();
176 private String getParam(String Param, String Default){
177 String dummy = getParameter(Param);
178 if(dummy==null){
179 dummy = Default;
181 return dummy;
184 public void initApplet(){
185 ServerHost = (getParam("ServerHost", getCodeBase().getHost()));
186 SmtpHost = (getParam("SmtpHost", ServerHost));
187 ServerPort = (getParam("ServerPort", "110"));
188 SmtpPort = (getParam("SmtpPort", "25"));
189 User = (getParam("User", ""));
190 MailDomain = (getParam("MailDomain", ""));
191 MailAddress = (getParam("MailAddress", ""));
192 Password = (getParam("Password", ""));
193 showFontChoice = Boolean.valueOf(getParam("showFontChoice", "false")).booleanValue();
194 novice = Boolean.valueOf(getParam("novice", "false")).booleanValue();
195 protocolSelect = Boolean.valueOf(getParam("protocolSelect", "false")).booleanValue();
196 authorizedSend = Boolean.valueOf(getParam("authorizedSend", "false")).booleanValue();
197 try{
198 headerListRows = Integer.parseInt(getParam("headerListRows", "5"));
199 } catch (NumberFormatException e){
201 if(headerListRows<2 || headerListRows>20){
202 headerListRows = 5;
206 public void initApplication(){
207 ServerHost = ("mail");
208 SmtpHost = ("mail");
209 ServerPort = ("110");
210 SmtpPort = ("25");
211 User = (System.getProperty("user.name"));
212 Password = ("");
213 for (int i = 0; i < argv.length; i++) {
214 if (argv[i].toLowerCase().equals("-pophost") ||
215 argv[i].toLowerCase().equals("-serverhost")) {
216 ServerHost = (argv[++i]);
217 } else if (argv[i].toLowerCase().equals("-smtphost")) {
218 SmtpHost = (argv[++i]);
219 } else if (argv[i].toLowerCase().equals("-popport") ||
220 argv[i].toLowerCase().equals("-serverport")) {
221 ServerPort = (argv[++i]);
222 } else if (argv[i].toLowerCase().equals("-smtpport")) {
223 SmtpPort = (argv[++i]);
224 } else if (argv[i].toLowerCase().equals("-user")) {
225 User = (argv[++i]);
226 } else if (argv[i].toLowerCase().equals("-maildomain")) {
227 MailDomain = (argv[++i]);
228 } else if (argv[i].toLowerCase().equals("-mailaddress")) {
229 MailAddress = (argv[++i]);
230 } else if (argv[i].toLowerCase().equals("-password")) {
231 Password = (argv[++i]);
232 } else if (argv[i].toLowerCase().equals("-showfontchoice")) {
233 showFontChoice = true;
234 } else if (argv[i].toLowerCase().equals("-authorizedsend")) {
235 authorizedSend = true;
236 } else if (argv[i].toLowerCase().equals("-novice")) {
237 novice = true;
238 } else if (argv[i].toLowerCase().equals("-headerlistrows")) {
239 try{
240 headerListRows = Integer.parseInt(argv[++i]);
241 } catch(NumberFormatException e){
243 if(headerListRows<2 || headerListRows>20){
244 headerListRows = 5;
246 } else if (argv[i].toLowerCase().equals("-protocolselect")) {
247 protocolSelect = true;
248 } else {
249 System.err.println("usage: java mumail.MUMail [-smtphost <SMTP Server>] |");
250 System.err.println(" [-serverhost <Mail Server>] |");
251 System.err.println(" [-smtpport <SMTP Port>] |");
252 System.err.println(" [-serverport <Mail Port>] |");
253 System.err.println(" [-user <User Name>] |");
254 System.err.println(" [-password <Password>] |");
255 System.err.println(" [-from <my address>] |");
256 System.err.println(" [-maildomain <domain part of email adress>] |");
257 System.err.println(" [-headerListRows <number>] |");
258 System.err.println(" [-showFontChoice] |");
259 System.err.println(" [-authorizedSend] |");
260 System.err.println(" [-novice]");
261 System.err.println(" [-protocolSelect]");
262 System.exit(1);
267 public void showPanel(String s){
268 //System.out.println("showPanel("+s+")");
269 ((CardLayout)getLayout()).show(this, s);
272 public void showLastPanel(){
273 ((CardLayout)getLayout()).previous(this);
276 public void newMessagePanel(String s){
277 messp = new MessagePanel(s, this);
278 add(MESSAGE_PANEL, messp);
279 showPanel(MESSAGE_PANEL);
282 /*public void newMailPanel() throws IOException {
283 mp = new MailPanel(mq, this);
284 add(MAIL_PANEL,mp);
285 showPanel(MAIL_PANEL);
286 mp.DisplayHeaders();
289 public void newSendMailPanel(boolean reply){
291 String Domain;
292 String User = lp.getUser();
294 if(MailAddress.equals("")){
295 if (MailDomain.equals("")) {
296 Domain = lp.getServerHost();
297 int i = Domain.indexOf(".");
298 if (i == -1) {
299 if (Domain.equals("") || Domain.equals("localhost")) {
300 Domain = "";
301 } else {
302 Domain = "@" + Domain;
304 } else {
305 Domain = "@" + Domain.substring(i + 1);
307 }else {
308 Domain = "@" + MailDomain;
310 User = User + Domain;
312 else{
313 User = MailAddress;
316 if(reply){
317 smp = new SendMailPanel(mp.mo, User, this);
319 else{
320 smp = new SendMailPanel(User, this);
322 //CardLayout)getLayout()).addLayoutComponent(smp, SENDMAIL_PANEL);
323 add(SENDMAIL_PANEL, smp);
324 showPanel(SENDMAIL_PANEL);
327 public void Connect(){
328 setCursor(new Cursor(Cursor.WAIT_CURSOR));
329 try {
330 MailClient mc = factory.getMailClientByProtocolName(lp.getProtocol());
331 System.err.println(mc);
332 mq = new MailQueue(mc, lp.getServerHost(), java.lang.Integer.parseInt(lp.getServerPort()), lp.getUser(), lp.getPassword());
333 mq.addPropertyChangeListener(this);
334 //newMailPanle();
335 mq.Open();
336 mp = new MailPanel(mq, this);
337 if(applet){
338 showStatus("connected to " + lp.getServerHost() + " as " + lp.getUser());
340 add(MAIL_PANEL,mp);
341 showPanel(MAIL_PANEL);
342 mp.DisplayHeaders();
343 } catch (java.lang.Exception e2) {
344 //System.err.println(e2);
345 //e2.printStackTrace();
346 newMessagePanel(e2.getMessage());
348 setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
351 public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
352 if (propertyChangeEvent.getSource() == mq) {
353 if (propertyChangeEvent.getPropertyName().equals(mq.pn_Connected)) {
358 public void actionPerformed(ActionEvent e) {
359 // confirm message panel
360 if (e.getActionCommand().equals("OK")){
361 showLastPanel();
362 remove(messp);
365 // from login panel
366 if ((e.getActionCommand().equals("Connect"))||(e.getSource()==lp.PasswordTF)){
367 Connect();
370 // from mail panel
371 if (e.getActionCommand().equals("Connection")) {
372 if (mq.isConnected()) {
373 mq.Close();
374 } else {
375 try{
376 mq.Open();
377 } catch(IOException exep) {
378 exep.printStackTrace();
383 // from mail panel
384 if (e.getActionCommand().equals("Close")) {
385 setCursor(new Cursor(Cursor.WAIT_CURSOR));
386 mq.Close();
387 remove(mp);
388 mp = null;
389 if(applet){
390 showStatus("MUMail login");
392 lp.setPassword("");
393 showPanel(LOGIN_PANEL);
394 setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
397 // from mail panel
398 if (e.getActionCommand().equals("Reply")){
399 newSendMailPanel(true);
402 // from mail panel AND!!! login panel
403 if (e.getActionCommand().equals("Send")){
404 newSendMailPanel(false);
407 // from sendmailpanel, means send mail now
408 if (e.getActionCommand().equals("SendMail")){
409 try {
410 sm = new SmtpClient(lp.getSmtpHost(), java.lang.Integer.parseInt(lp.getSmtpPort()));
411 sm.submitMail(smp.From.getText(), smp.To.getText(), smp.Subject.getText(), smp.Text.getText(), smp.getEncoding());
412 showLastPanel();
413 remove(smp);
414 smp = null;
415 } catch (java.lang.Exception exep) {
416 //remove(smp);
417 newMessagePanel(exep.getMessage());
418 //System.err.println(exep);
419 //exep.printStackTrace();
423 // from sendmailpanel
424 if (e.getActionCommand().equals("Cancel")){
425 showLastPanel();
426 remove(smp);
427 smp = null;
430 // from loginpanel
431 if (e.getActionCommand().equals(mumail.gui.LoginPanel.homepage)){
432 try{
433 getAppletContext().showDocument(new URL(Homepage));
435 catch (MalformedURLException excep){
439 // from loginpanel
440 if (e.getActionCommand().equals("Exit")){
441 System.exit(0);
444 // from mailpanel
445 if (e.getActionCommand().equals("Exit2")) {
446 setCursor(new Cursor(Cursor.WAIT_CURSOR));
447 mq.Close();
448 setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
449 System.exit(0);
453 public String[][] getParameterInfo() {
454 String[][] info = {
455 {"ServerHost", "String", ""},
456 {"SmtpHost", "String", ""},
457 {"ServerPort", "String", ""},
458 {"SmtpPort", "String", ""},
459 {"User", "String", ""},
460 {"Password", "String", ""},
461 {"MailDomain", "String", ""},
462 {"showFontChoice", "booelan", ""},
463 {"novice", "boolean", ""},
464 {"authorizedSend", "boolean", ""},
465 {"headerListRows", "Integer", ""},
466 {"protocolSelect", "boolean", ""},
468 return info;
471 public String getAppletInfo() {
472 return ID + " Copyright 1997 - 2001 by Uli Luckas <luckas@cs.tu-berlin.de>\n" +
473 " Mark T\u00fcmpfel <marktop@cs.tu-berlin.de>\n" +
474 "Homepage: "+Homepage+"\n"+
475 "MUMail is free software under the terms of GNU General Public License\n" +
476 "and comes with ABSOLUTELY NO WARRANTY.\n";
479 public Dimension getPreferredSize(){
480 return new Dimension(600,440);