reintroduced Headers.toString()
[org.ibex.mail.git] / src / org / ibex / mail / Headers.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.mail;
6 import static org.ibex.mail.MailException.*;
7 import org.ibex.crypto.*;
8 import org.ibex.util.*;
9 import org.ibex.mail.protocol.*;
10 import org.ibex.js.*;
11 import org.ibex.io.*;
12 import org.ibex.io.Fountain;
13 import java.util.*;
14 import java.net.*;
15 import java.io.*;
16
17 // FIXME: this is important: folded headers: can insert CRLF anywhere that whitespace appears (before the whitespace)
18 public class Headers extends JS.Immutable implements Fountain {
19     private final Hash head = new Hash();
20     private final Hash headModified = new Hash();
21     public        int lines;
22     public  final boolean mime;
23
24     private String raw;
25     private StringFountain fountain;
26
27     public String get(String s) {
28         String ret = (String)headModified.get(s.toLowerCase());
29         if (ret==null) ret = (String)head.get(s.toLowerCase());
30         return ret;
31     }
32     public void remove(String k) { put(k, null); }
33     public void put(String k, String v) {
34         Stream stream = getStream();
35         StringBuffer all = new StringBuffer();
36         int lines = 0;
37         String key = null;
38         for(String s = stream.readln(); s != null && !s.equals(""); s = stream.readln()) {
39             if (!Character.isSpace(s.charAt(0))) {
40                 if (s.indexOf(':') == -1) throw new Malformed("Header line does not contain colon: " + s);
41                 key = s.substring(0, s.indexOf(':')).toLowerCase();
42             }
43             if (key.toLowerCase().equals(k.toLowerCase())) {
44                 if (v != null) { all.append(k + ": " + v + "\r\n"); lines++; v = null; }
45                 continue;
46             }
47             all.append(s);
48             all.append("\r\n");
49             lines++;
50         }
51         if (v != null) {
52             lines++;
53             all.append(k + ": " + v + "\r\n");
54         }
55         all.append("\r\n");
56         this.raw = all.toString();
57         this.lines = lines;
58         this.fountain = new Fountain.StringFountain(this.raw);
59     }
60     public JS get(JS s) throws JSExn { return JSU.S(get(JSU.toString(s).toLowerCase())); }
61
62     public Stream getStream() { return fountain.getStream(); }
63     public int    getLength() { return fountain.getLength(); }
64     public int    getNumLines() { return fountain.getNumLines(); }
65     public Stream getStreamWithCRLF() { return new Stream(raw+"\r\n"); }
66     
67     // FIXME
68     public String getString() { return raw; }
69
70     public Headers(Stream stream) throws Malformed { this(stream, false); }
71     public Headers(Stream stream, boolean assumeMime) throws Malformed {
72         StringBuffer all = new StringBuffer();
73         String key = null;
74         int lines = 0;
75         for(String s = stream.readln(); s != null && !s.equals(""); s = stream.readln()) {
76             all.append(s);
77             all.append("\r\n");
78             lines++;
79             if (Character.isSpace(s.charAt(0))) {
80                 if (key == null) throw new Malformed("Message began with a blank line; no headers");
81                 head.put(key, head.get(key) + " " + s.trim());
82                 continue;
83             }
84             if (s.indexOf(':') == -1) throw new Malformed("Header line does not contain colon: " + s);
85             key = s.substring(0, s.indexOf(':')).toLowerCase();
86             for(int i=0; i<key.length(); i++)
87                 if (key.charAt(i) < 33 || key.charAt(i) > 126)
88                     throw new Malformed("Header key \""+key+"\" contains invalid character \"" + key.charAt(i) + "\"");
89             String val = s.substring(s.indexOf(':') + 1).trim();
90             if (get(key) != null) val = get(key) + " " + val; // just append it to the previous one;
91             head.put(key, val);
92         }
93         this.raw = all.toString();
94         this.fountain = new Fountain.StringFountain(this.raw);
95         this.lines = lines;
96         this.mime = assumeMime | (get("mime-version") != null && get("mime-version").trim().equals("1.0"));
97         /*
98           java.util.Enumeration e = head.keys();
99           while(e.hasNext()) {
100           String k = (String)e.next();
101           String v = (String)head.get(k);
102           if (mime) k = Encode.RFC2047.decode(k);
103           v = uncomment(v);
104           if (mime) v = Encode.RFC2047.decode(v);
105           head.put(k, v);
106           }
107         */
108     }
109
110     // Helpers //////////////////////////////////////////////////////////////////////////////
111
112     public static Stream skip(Stream stream) {
113         for(String s = stream.readln(); s!=null && s.length() > 0;) s = stream.readln();
114         return stream;
115     }
116
117     public static String uncomment(String val) {
118         boolean inquotes = false;
119         for(int i=0; i<val.length(); i++) {
120             if (val.charAt(i) == '\"') inquotes = !inquotes;
121             if (val.charAt(i) == '(' && !inquotes)
122                 val = val.substring(0, i) + val.substring(val.indexOf(i--, ')') + 1);
123         }
124         return val;
125     }
126 }