home *** CD-ROM | disk | FTP | other *** search
- package de.trantor.mail;
-
- import java.util.Calendar;
- import java.util.Random;
- import java.util.TimeZone;
- import java.util.Vector;
-
- public class Message {
- private Vector lines = new Vector();
- private int headerSize = 0;
-
- public Message() {
- this.lines.addElement("");
- }
-
- public Message(String from, String to, String subject) {
- this.lines.addElement("");
- if (from != null) {
- this.addHeaderLine("From: " + from);
- String s = getMachineAddress(from);
- this.addHeaderLine("Message-ID: <" + getRandomString() + "." + s + ">");
- } else {
- this.addHeaderLine("Message-ID: <" + getRandomString() + ">");
- }
-
- if (to != null) {
- this.addHeaderLine("To: " + to);
- }
-
- if (subject != null) {
- this.addHeaderLine("Subject: " + subject);
- }
-
- this.addHeaderLine("Date: " + getCanonicalDate(Calendar.getInstance(), TimeZone.getDefault()));
- }
-
- public String getHeaderLine(int index) throws ArrayIndexOutOfBoundsException {
- if (index >= 0 && index < this.headerSize) {
- return (String)this.lines.elementAt(index);
- } else {
- throw new ArrayIndexOutOfBoundsException(index);
- }
- }
-
- public String getHeaderName(int index) throws ArrayIndexOutOfBoundsException {
- return getStringName(this.getHeaderLine(index));
- }
-
- public String getHeaderValue(int index) throws ArrayIndexOutOfBoundsException {
- return getStringValue(this.getHeaderLine(index));
- }
-
- public int getHeaderIndex(String name, int startIndex) {
- String lowerName = name.toLowerCase();
-
- for(int i = startIndex; i < this.headerSize; ++i) {
- String s = this.getHeaderName(i);
- if (s != null && s.toLowerCase().equals(lowerName)) {
- return i;
- }
- }
-
- return -1;
- }
-
- public int getHeaderIndex(String name) {
- return this.getHeaderIndex(name, 0);
- }
-
- public String getHeaderValue(String name) {
- return this.getHeaderValue(name, (String)null);
- }
-
- public String getHeaderValue(String name, String def) {
- int i = this.getHeaderIndex(name);
- return i == -1 ? def : this.getHeaderValue(i);
- }
-
- public String[] getAllHeaderValues(String name) {
- Vector lines = new Vector();
-
- for(int i = this.getHeaderIndex(name); i != -1; i = this.getHeaderIndex(name, i + 1)) {
- lines.addElement(this.getHeaderValue(i));
- }
-
- String[] result = new String[lines.size()];
-
- for(int j = 0; j < lines.size(); ++j) {
- result[j] = (String)lines.elementAt(j);
- }
-
- return result;
- }
-
- public void setHeaderLine(int index, String line) {
- if (index >= 0 && index < this.headerSize) {
- this.lines.setElementAt(line, index);
- } else {
- throw new ArrayIndexOutOfBoundsException(index);
- }
- }
-
- public int getHeaderLineCount() {
- return this.headerSize;
- }
-
- public int addHeaderLine(String line) {
- this.lines.insertElementAt(line, this.headerSize);
- return this.headerSize++;
- }
-
- public void insertHeaderLine(int index, String line) {
- if (index >= 0 && index <= this.headerSize) {
- this.lines.insertElementAt(line, index);
- ++this.headerSize;
- } else {
- throw new ArrayIndexOutOfBoundsException(index);
- }
- }
-
- public void removeHeaderLine(int index) {
- if (index >= 0 && index < this.headerSize) {
- this.lines.removeElementAt(index);
- --this.headerSize;
- } else {
- throw new ArrayIndexOutOfBoundsException(index);
- }
- }
-
- public void setHeaderValue(String name, String value) {
- int i = this.getHeaderIndex(name);
- if (i == -1) {
- this.addHeaderLine(name + ": " + value);
- } else {
- this.setHeaderLine(i, name + ": " + value);
- }
-
- }
-
- public String getBodyLine(int index) {
- if (index >= 0 && index < this.getBodyLineCount()) {
- return (String)this.lines.elementAt(this.headerSize + index + 1);
- } else {
- throw new ArrayIndexOutOfBoundsException(index);
- }
- }
-
- public void setBodyLine(int index, String line) {
- if (index >= 0 && index < this.getBodyLineCount()) {
- this.lines.setElementAt(line, this.headerSize + index + 1);
- } else {
- throw new ArrayIndexOutOfBoundsException(index);
- }
- }
-
- public int getBodyLineCount() {
- return this.lines.size() - this.headerSize - 1;
- }
-
- public int addBodyLine(String line) {
- this.lines.addElement(line);
- return this.getBodyLineCount() - 1;
- }
-
- public void insertBodyLine(int index, String line) {
- if (index >= 0 && index <= this.getBodyLineCount()) {
- this.lines.insertElementAt(line, this.headerSize + index + 1);
- } else {
- throw new ArrayIndexOutOfBoundsException(index);
- }
- }
-
- public void removeBodyLine(int index) {
- if (index >= 0 && index < this.getBodyLineCount()) {
- this.lines.removeElementAt(this.headerSize + index + 1);
- } else {
- throw new ArrayIndexOutOfBoundsException(index);
- }
- }
-
- public static String getMachineAddress(String address) {
- int p = address.indexOf(60);
- int q = address.indexOf(62, p + 1);
- if (p != -1 && q != -1) {
- return address.substring(p + 1, q);
- } else {
- p = address.indexOf(40);
- q = address.indexOf(41, p + 1);
- if (p != -1 && q != -1) {
- return address.substring(0, p).trim();
- } else {
- p = address.indexOf(34);
- q = address.indexOf(34, p + 1);
- return p != -1 && q != -1 ? address.substring(0, p).trim() : address;
- }
- }
- }
-
- public static String getDisplayAddress(String address) {
- int p = address.indexOf(34);
- int q = address.indexOf(34, p + 1);
- if (p != -1 && q != -1) {
- return address.substring(p + 1, q);
- } else {
- p = address.indexOf(40);
- q = address.indexOf(41, p + 1);
- if (p != -1 && q != -1) {
- return address.substring(p + 1, q);
- } else {
- p = address.indexOf(60);
- q = address.indexOf(62, p + 1);
- return p != -1 && q != -1 ? address.substring(0, p).trim() : "";
- }
- }
- }
-
- public static String getCanonicalAddress(String address) {
- int p = address.indexOf(60);
- int q = address.indexOf(62, p + 1);
- if (p != -1 && q != -1) {
- return address.substring(p, q + 1);
- } else {
- p = address.indexOf(40);
- q = address.indexOf(41, p + 1);
- if (p != -1 && q != -1) {
- return "<" + address.substring(0, p) + ">";
- } else {
- p = address.indexOf(34);
- q = address.indexOf(34, p + 1);
- return p != -1 && q != -1 ? "<" + address.substring(0, p) + ">" : "<" + address + ">";
- }
- }
- }
-
- private static String intToStr(int value, int length) {
- String result = Integer.toString(value);
- result = "0000".substring(result.length(), length) + result;
- return result;
- }
-
- public static String getCanonicalDate(Calendar calendar, TimeZone timezone) {
- String monthNames = "JanFebMarAprMayJunJulAugSepOctNovDec";
- String dayNames = "SunMonTueWedThuFriSatSun";
- int year = calendar.get(1);
- int month = calendar.get(2);
- int day = calendar.get(5);
- int weekday = calendar.get(7) - 1;
- int hour = calendar.get(11);
- int minute = calendar.get(12);
- int second = calendar.get(13);
- String result = dayNames.substring(3 * weekday, 3 * weekday + 3) + ", " + intToStr(day, 2) + " " + monthNames.substring(3 * month, 3 * month + 3) + " " + intToStr(year, 4) + " " + intToStr(hour, 2) + ":" + intToStr(minute, 2) + ":" + intToStr(second, 2);
- if (timezone != null) {
- int offset = timezone.getRawOffset() / 1000;
- if (timezone.useDaylightTime()) {
- offset += 3600;
- }
-
- String name;
- if (offset >= 0) {
- name = " GMT+";
- } else {
- name = " GMT-";
- offset = -offset;
- }
-
- result = result + name + intToStr(offset / 3600, 2) + intToStr(offset % 3600, 2);
- }
-
- return result;
- }
-
- public static String[] getStringElements(String s) {
- Vector temp = new Vector();
- int len = s.length();
-
- int q;
- for(int p = 0; p < len; p = q + 1) {
- q = p;
-
- for(boolean quote = false; q < len && (s.charAt(q) != ';' || quote); ++q) {
- if (s.charAt(q) == '"') {
- quote = !quote;
- }
- }
-
- temp.addElement(s.substring(p, q).trim());
- }
-
- String[] result = new String[temp.size()];
-
- for(int i = 0; i < temp.size(); ++i) {
- result[i] = (String)temp.elementAt(i);
- }
-
- return result;
- }
-
- public static String getStringName(String s) {
- int p = s.indexOf(58);
- return p == -1 ? null : s.substring(0, p);
- }
-
- public static String getStringValue(String s) {
- int p = s.indexOf(58);
- String value;
- if (p == -1) {
- value = s;
- } else {
- value = s.substring(p + 1);
- }
-
- value = value.trim();
- if (value.length() > 1 && value.charAt(0) == '"' && value.charAt(value.length() - 1) == '"') {
- value = value.substring(1, value.length() - 1);
- }
-
- return value;
- }
-
- public static String getRandomString() {
- return Long.toString(System.currentTimeMillis(), 36) + "." + Integer.toString(Math.abs((new Random()).nextInt()), 36);
- }
-
- Vector getLines() {
- return this.lines;
- }
- }
-