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