001: /* CVS ID $Id: XMLUserData.java,v 1.1.1.1 2002/10/02 18:42:55 wastl Exp $ */
002: package net.wastl.webmail.xml;
003:
004: import net.wastl.webmail.config.*;
005: import net.wastl.webmail.server.*;
006: import net.wastl.webmail.misc.*;
007: import net.wastl.webmail.exceptions.*;
008:
009: import java.util.*;
010: import java.text.*;
011:
012: import org.w3c.dom.*;
013:
014: /**
015: * XMLUserData.java
016: *
017: * Created: Fri Mar 10 16:17:28 2000
018: *
019: * Copyright (C) 2000 Sebastian Schaffert
020: *
021: * This program is free software; you can redistribute it and/or
022: * modify it under the terms of the GNU General Public License
023: * as published by the Free Software Foundation; either version 2
024: * of the License, or (at your option) any later version.
025: *
026: * This program is distributed in the hope that it will be useful,
027: * but WITHOUT ANY WARRANTY; without even the implied warranty of
028: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
029: * GNU General Public License for more details.
030: *
031: * You should have received a copy of the GNU General Public License
032: * along with this program; if not, write to the Free Software
033: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
034: */
035:
036: /**
037: *
038: *
039: *
040: * @author Sebastian Schaffert
041: * @version
042: */
043: /* 9/25/2000 devink -- modified for challenge/response authentication */
044:
045: public class XMLUserData implements UserData {
046:
047: protected Document root;
048:
049: protected Element userdata;
050:
051: protected boolean debug;
052:
053: protected long login_time;
054: protected boolean logged_in;
055:
056: public XMLUserData(Document d) {
057: debug = WebMailServer.getDebug();
058: root = d;
059: userdata = root.getDocumentElement();
060: if (userdata == null) {
061: System.err.println("UserData was null ???");
062: userdata = root.createElement("USERDATA");
063: root.appendChild(userdata);
064: }
065: }
066:
067: public void init(String user, String domain, String password) {
068: setUserName(user);
069: setDomain(domain);
070: setFullName(user);
071: if (domain.equals("")) {
072: // This is a special case when the user already contains the domain
073: // e.g. QMail
074: setEmail(user);
075: } else {
076: setEmail(user + "@" + domain);
077: }
078: try {
079: setPassword(password, password);
080: } catch (InvalidPasswordException ex) {
081: }
082: // Modified by exce, start
083: /**
084: * Set user's locale to WebMailServer's default locale.
085: */
086: // setPreferredLocale(Locale.getDefault().toString());
087: setPreferredLocale(WebMailServer.getDefaultLocale().toString());
088: // Modified by exce, end
089: setTheme(WebMailServer.getDefaultTheme());
090: setIntVar("first login", System.currentTimeMillis());
091: setIntVar("last login", System.currentTimeMillis());
092: setIntVar("login count", 0);
093: setIntVar("max show messages", 20);
094: setIntVar("icon size", 48);
095: setBoolVar("break lines", true);
096: setIntVar("max line length", 79);
097: }
098:
099: public Document getRoot() {
100: return root;
101: }
102:
103: public Element getUserData() {
104: return userdata;
105: }
106:
107: public DocumentFragment getDocumentFragment() {
108: DocumentFragment df = root.createDocumentFragment();
109: df.appendChild(userdata);
110: return df;
111: }
112:
113: protected void ensureElement(String tag, String attribute,
114: String att_value) {
115: NodeList nl = userdata.getElementsByTagName(tag);
116: boolean flag = false;
117: for (int i = 0; i < nl.getLength(); i++) {
118: Element e = (Element) nl.item(i);
119: if (attribute == null) {
120: // No attribute required
121: flag = true;
122: break;
123: } else if (att_value == null) {
124: if (e.getAttributeNode(attribute) != null) {
125: // Attribute exists, value is not requested
126: flag = true;
127: break;
128: }
129: } else if (e.getAttribute(attribute).equals(att_value)) {
130: flag = true;
131: break;
132: }
133: }
134: if (!flag) {
135: Element elem = root.createElement(tag);
136: if (attribute != null) {
137: elem.setAttribute(attribute, att_value == null ? ""
138: : att_value);
139: }
140: userdata.appendChild(elem);
141: }
142: }
143:
144: public void login() {
145: // Increase login count and last login pointer
146: //setIntVar("last login",System.currentTimeMillis());
147: if (!logged_in) {
148: setIntVar("login count", getIntVar("login count") + 1);
149: login_time = System.currentTimeMillis();
150: logged_in = true;
151: } else {
152: System.err
153: .println("Err: Trying to log in a second time for user "
154: + getLogin());
155: }
156: }
157:
158: public void logout() {
159: if (logged_in) {
160: setIntVar("last login", login_time);
161: // Modified by exce, start
162: logged_in = false;
163: // Modified by exce, end
164: } else {
165: System.err
166: .println("Err: Logging out a user that wasn't logged in.");
167: }
168: }
169:
170: public void addMailHost(String name, String host, String login,
171: String password) {
172: // First, check whether a mailhost with this name already exists.
173: // Delete, if yes.
174: try {
175: //System.err.println("Adding mailhost "+name);
176: if (getMailHost(name) != null) {
177: removeMailHost(name);
178: }
179: Element mailhost = root.createElement("MAILHOST");
180: mailhost.setAttribute("name", name);
181: mailhost.setAttribute("id", Long.toHexString(Math.abs(name
182: .hashCode()))
183: + Long.toHexString(System.currentTimeMillis()));
184:
185: Element mh_login = root.createElement("MH_LOGIN");
186: XMLCommon.setElementTextValue(mh_login, login);
187: mailhost.appendChild(mh_login);
188:
189: Element mh_pass = root.createElement("MH_PASSWORD");
190: XMLCommon.setElementTextValue(mh_pass, Helper
191: .encryptTEA(password));
192: mailhost.appendChild(mh_pass);
193:
194: Element mh_uri = root.createElement("MH_URI");
195: XMLCommon.setElementTextValue(mh_uri, host);
196: mailhost.appendChild(mh_uri);
197:
198: userdata.appendChild(mailhost);
199: //System.err.println("Done mailhost "+name);
200: //XMLCommon.writeXML(root,System.err,"");
201: } catch (Exception ex) {
202: ex.printStackTrace();
203: }
204: }
205:
206: public void removeMailHost(String id) {
207: Element n = XMLCommon.getElementByAttribute(userdata,
208: "MAILHOST", "id", id);
209: if (n != null) {
210: userdata.removeChild(n);
211: }
212: }
213:
214: public MailHostData getMailHost(String id) {
215: final Element mailhost = XMLCommon.getElementByAttribute(
216: userdata, "MAILHOST", "id", id);
217: return new MailHostData() {
218:
219: public String getPassword() {
220: return Helper.decryptTEA(XMLCommon.getTagValue(
221: mailhost, "MH_PASSWORD"));
222: }
223:
224: public void setPassword(String s) {
225: XMLCommon.setTagValue(mailhost, "MH_PASSWORD", Helper
226: .encryptTEA(s));
227: }
228:
229: public String getLogin() {
230: return XMLCommon.getTagValue(mailhost, "MH_LOGIN");
231: }
232:
233: public String getName() {
234: return mailhost.getAttribute("name");
235: }
236:
237: public void setLogin(String s) {
238: XMLCommon.setTagValue(mailhost, "MH_LOGIN", s);
239: }
240:
241: public void setName(String s) {
242: mailhost.setAttribute("name", s);
243: }
244:
245: public String getHostURL() {
246: return XMLCommon.getTagValue(mailhost, "MH_URI");
247: }
248:
249: public void setHostURL(String s) {
250: XMLCommon.setTagValue(mailhost, "MH_URI", s);
251: }
252:
253: public String getID() {
254: return mailhost.getAttribute("id");
255: }
256: };
257: }
258:
259: public Enumeration mailHosts() {
260: final NodeList nl = userdata.getElementsByTagName("MAILHOST");
261: return new Enumeration() {
262: int i = 0;
263:
264: public boolean hasMoreElements() {
265: return i < nl.getLength();
266: }
267:
268: public Object nextElement() {
269: Element e = (Element) nl.item(i++);
270: return e.getAttribute("id");
271: }
272: };
273: }
274:
275: public int getMaxShowMessages() {
276: int retval = (int) getIntVarWrapper("max show messages");
277: return retval == 0 ? 20 : retval;
278: }
279:
280: public void setMaxShowMessages(int i) {
281: setIntVarWrapper("max show messages", i);
282: }
283:
284: /**
285: * As of WebMail 0.7.0 this is different from the username, because it
286: * consists of the username and the domain.
287: * @see getUserName()
288: */
289: public String getLogin() {
290: return getUserName() + "@" + getDomain();
291: }
292:
293: public String getFullName() {
294: return XMLCommon.getTagValue(userdata, "FULL_NAME");
295: }
296:
297: public void setFullName(String s) {
298: XMLCommon.setTagValue(userdata, "FULL_NAME", s);
299: }
300:
301: public String getSignature() {
302: return XMLCommon.getTagValue(userdata, "SIGNATURE");
303: }
304:
305: public void setSignature(String s) {
306: XMLCommon.setTagValue(userdata, "SIGNATURE", s, true);
307: }
308:
309: public String getEmail() {
310: return XMLCommon.getTagValue(userdata, "EMAIL");
311: }
312:
313: public void setEmail(String s) {
314: XMLCommon.setTagValue(userdata, "EMAIL", s);
315: }
316:
317: public Locale getPreferredLocale() {
318: String loc = XMLCommon.getTagValue(userdata, "LOCALE");
319: StringTokenizer t = new StringTokenizer(loc, "_");
320: String language = t.nextToken().toLowerCase();
321: String country = "";
322: if (t.hasMoreTokens()) {
323: country = t.nextToken().toUpperCase();
324: }
325: return new Locale(language, country);
326: }
327:
328: public void setPreferredLocale(String newloc) {
329: XMLCommon.setTagValue(userdata, "LOCALE", newloc);
330: }
331:
332: public String getTheme() {
333: String retval = XMLCommon.getTagValue(userdata, "THEME");
334: if (retval.equals("")) {
335: return WebMailServer.getDefaultTheme();
336: } else {
337: return retval;
338: }
339: }
340:
341: public void setTheme(String theme) {
342: XMLCommon.setTagValue(userdata, "THEME", theme);
343: }
344:
345: private String formatDate(long date) {
346: TimeZone tz = TimeZone.getDefault();
347: DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG,
348: DateFormat.DEFAULT, getPreferredLocale());
349: df.setTimeZone(tz);
350: String now = df.format(new Date(date));
351: return now;
352: }
353:
354: public String getFirstLogin() {
355: long date = getIntVarWrapper("first login");
356: return formatDate(date);
357: }
358:
359: public String getLastLogin() {
360: long date = getIntVarWrapper("last login");
361: return formatDate(date);
362: }
363:
364: public String getLoginCount() {
365: return getIntVarWrapper("login count") + "";
366: }
367:
368: public boolean checkPassword(String s) {
369: String password = XMLCommon.getTagValue(userdata, "PASSWORD");
370: if (password.startsWith(">")) {
371: password = password.substring(1);
372: }
373: return password.equals(Helper.crypt(password, s));
374: }
375:
376: public void setPassword(String newpasswd, String verify)
377: throws InvalidPasswordException {
378: if (newpasswd.equals(verify)) {
379: Random r = new Random();
380: // Generate the crypted password; avoid problems with XML parsing
381: String crypted = ">";
382: while (crypted.lastIndexOf(">") >= 0
383: || crypted.lastIndexOf("<") >= 0) {
384: // This has to be some integer between 46 and 127 for the Helper
385: // class
386: String seed = (char) (r.nextInt(80) + 46) + ""
387: + (char) (r.nextInt(80) + 46);
388: System.err.println("Seed: " + seed);
389: crypted = Helper.crypt(seed, newpasswd);
390: }
391: XMLCommon.setTagValue(userdata, "PASSWORD", crypted);
392: } else {
393: throw new InvalidPasswordException(
394: "The passwords did not match!");
395: }
396: }
397:
398: public void setPasswordData(String data) {
399: XMLCommon.setTagValue(userdata, "PASSDATA", data);
400: }
401:
402: public String getPasswordData() {
403: return XMLCommon.getTagValue(userdata, "PASSDATA");
404: }
405:
406: public int getMaxLineLength() {
407: int retval = (int) getIntVarWrapper("max line length");
408: return retval == 0 ? 79 : retval;
409: }
410:
411: public void setMaxLineLength(int i) {
412: setIntVarWrapper("max line length", i);
413: }
414:
415: public boolean wantsBreakLines() {
416: return getBoolVarWrapper("break lines");
417: }
418:
419: public void setBreakLines(boolean b) {
420: setBoolVarWrapper("break lines", b);
421: }
422:
423: public boolean wantsShowImages() {
424: return getBoolVarWrapper("show images");
425: }
426:
427: public void setShowImages(boolean b) {
428: setBoolVarWrapper("show images", b);
429: }
430:
431: public boolean wantsShowFancy() {
432: return getBoolVarWrapper("show fancy");
433: }
434:
435: public void setShowFancy(boolean b) {
436: setBoolVarWrapper("show fancy", b);
437: }
438:
439: public boolean wantsSetFlags() {
440: return getBoolVarWrapper("set message flags");
441: }
442:
443: public void setSetFlags(boolean b) {
444: setBoolVarWrapper("set message flags", b);
445: }
446:
447: public void setSaveSent(boolean b) {
448: setBoolVarWrapper("save sent messages", b);
449: }
450:
451: public boolean wantsSaveSent() {
452: return getBoolVarWrapper("save sent messages");
453: }
454:
455: public String getSentFolder() {
456: return XMLCommon.getTagValue(userdata, "SENT_FOLDER");
457: }
458:
459: public void setSentFolder(String s) {
460: XMLCommon.setTagValue(userdata, "SENT_FOLDER", s);
461: }
462:
463: public String getDomain() {
464: return XMLCommon.getTagValue(userdata, "USER_DOMAIN");
465: }
466:
467: public void setDomain(String s) {
468: XMLCommon.setTagValue(userdata, "USER_DOMAIN", s);
469: }
470:
471: /**
472: * Return the username without the domain (in contrast to getLogin()).
473: * @see getLogin()
474: */
475: public String getUserName() {
476: return XMLCommon.getTagValue(userdata, "LOGIN");
477: }
478:
479: public void setUserName(String s) {
480: XMLCommon.setTagValue(userdata, "LOGIN", s);
481: }
482:
483: public void setIntVar(String var, long value) {
484: setIntVarWrapper(var, value);
485: }
486:
487: public long getIntVar(String var) {
488: return getIntVarWrapper(var);
489: }
490:
491: public void setBoolVar(String var, boolean value) {
492: setBoolVarWrapper(var, value);
493: }
494:
495: public boolean getBoolVar(String var) {
496: return getBoolVarWrapper(var);
497: }
498:
499: /**
500: * Wrapper method for setting all bool vars
501: */
502: protected void setIntVarWrapper(String var, long value) {
503: ensureElement("INTVAR", "name", var);
504: Element e = XMLCommon.getElementByAttribute(userdata, "INTVAR",
505: "name", var);
506: e.setAttribute("value", value + "");
507: if (debug)
508: System.err.println("XMLUserData (" + getUserName() + "@"
509: + getDomain() + "): Setting '" + var + "' to '"
510: + value + "'");
511: }
512:
513: protected long getIntVarWrapper(String var) {
514: ensureElement("INTVAR", "name", var);
515: Element e = XMLCommon.getElementByAttribute(userdata, "INTVAR",
516: "name", var);
517: long r = 0;
518: try {
519: r = Long.parseLong(e.getAttribute("value"));
520: } catch (NumberFormatException ex) {
521: System.err
522: .println("Warning: Not a valid number in '" + var
523: + "' for user " + getUserName() + "@"
524: + getDomain());
525: }
526: return r;
527: }
528:
529: /**
530: * Wrapper method for setting all bool vars
531: */
532: protected void setBoolVarWrapper(String var, boolean value) {
533: ensureElement("BOOLVAR", "name", var);
534: Element e = XMLCommon.getElementByAttribute(userdata,
535: "BOOLVAR", "name", var);
536: e.setAttribute("value", value ? "yes" : "no");
537: if (debug)
538: System.err.println("XMLUserData (" + getUserName() + "@"
539: + getDomain() + "): Setting '" + var + "' to '"
540: + value + "'");
541: }
542:
543: protected boolean getBoolVarWrapper(String var) {
544: ensureElement("BOOLVAR", "name", var);
545: Element e = XMLCommon.getElementByAttribute(userdata,
546: "BOOLVAR", "name", var);
547: return (e.getAttribute("value").toUpperCase().equals("YES") || e
548: .getAttribute("value").toUpperCase().equals("TRUE"));
549: }
550:
551: /**
552: * Set all boolvars to "false".
553: *
554: */
555: public void resetBoolVars() {
556: NodeList nl = root.getElementsByTagName("BOOLVAR");
557: for (int i = 0; i < nl.getLength(); i++) {
558: Element elem = (Element) nl.item(i);
559: elem.setAttribute("value", "no");
560: }
561: }
562:
563: } // XMLUserData
|