]> WPIA git - gigi.git/blob - src/club/wpia/gigi/output/template/MailTemplate.java
upd: rename package name and all references to it
[gigi.git] / src / club / wpia / gigi / output / template / MailTemplate.java
1 package club.wpia.gigi.output.template;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.io.Reader;
6 import java.io.StringReader;
7 import java.io.StringWriter;
8 import java.net.URL;
9 import java.util.Collection;
10 import java.util.Map;
11
12 import club.wpia.gigi.email.SendMail;
13 import club.wpia.gigi.localisation.Language;
14
15 public class MailTemplate extends Template {
16
17     private static final Template FOOTER = new Template(MailTemplate.class.getResource("MailFooter.templ"));
18
19     public static final String SUBJECT_TAG = "[SomeCA] ";
20
21     private TemplateBlock subjectBlock;
22
23     public MailTemplate(URL u) {
24         super(u);
25     }
26
27     public MailTemplate(Reader r) {
28         super(r);
29     }
30
31     @Override
32     protected ParseResult parse(Reader r) throws IOException {
33         StringBuilder strb = new StringBuilder();
34         int ct = 0;
35         int c;
36         while ((c = r.read()) > 0) {
37             if (c == '\n') {
38                 ct++;
39                 if (ct == 2) {
40                     break;
41                 }
42             } else {
43                 ct = 0;
44             }
45             strb.append((char) c);
46         }
47         String[] lines = strb.toString().split("\n");
48         for (int i = 0; i < lines.length; i++) {
49             String[] lineParts = lines[i].split(": ", 2);
50             if (lineParts.length != 2) {
51                 throw new IOException("Mail template header malformed.");
52             }
53             if (lineParts[0].equals("Subject")) {
54                 subjectBlock = parseContent(new StringReader(lineParts[1])).getBlock(null);
55             }
56         }
57         if (subjectBlock == null) {
58             throw new IOException("Mail template without subject line.");
59         }
60         return parseContent(r);
61     }
62
63     public void sendMail(Language l, Map<String, Object> vars, String to) throws IOException {
64         tryReload();
65         vars.put(Outputable.OUT_KEY_PLAIN, true);
66
67         String body = runTemplate(this, l, vars);
68         body += runTemplate(FOOTER, l, vars);
69         String subject = runTemplate(subjectBlock, l, vars);
70
71         SendMail.getInstance().sendMail(to, SUBJECT_TAG + subject, body, null, null, null, null, false);
72     }
73
74     private static String runTemplate(Outputable toRun, Language l, Map<String, Object> vars) {
75         StringWriter sw = new StringWriter();
76         PrintWriter pw = new PrintWriter(sw);
77         toRun.output(pw, l, vars);
78         pw.close();
79         return sw.toString();
80     }
81
82     @Override
83     public void addTranslations(Collection<String> s) {
84         subjectBlock.addTranslations(s);
85         super.addTranslations(s);
86     }
87
88 }