--- /dev/null
+package org.cacert.gigi.output.template;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Reader;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.net.URL;
+import java.util.Collection;
+import java.util.Map;
+
+import org.cacert.gigi.email.SendMail;
+import org.cacert.gigi.localisation.Language;
+
+public class MailTemplate extends Template {
+
+ public static final String SUBJECT_TAG = "[SomeCA.org] ";
+
+ private TemplateBlock subjectBlock;
+
+ public MailTemplate(URL u) {
+ super(u);
+ }
+
+ public MailTemplate(Reader r) {
+ super(r);
+ }
+
+ @Override
+ protected ParseResult parse(Reader r) throws IOException {
+ StringBuilder strb = new StringBuilder();
+ int ct = 0;
+ int c;
+ while ((c = r.read()) > 0) {
+ if (c == '\n') {
+ ct++;
+ if (ct == 2) {
+ break;
+ }
+ } else {
+ ct = 0;
+ }
+ strb.append((char) c);
+ }
+ String[] lines = strb.toString().split("\n");
+ for (int i = 0; i < lines.length; i++) {
+ String[] lineParts = lines[i].split(": ", 2);
+ if (lineParts.length != 2) {
+ throw new IOException("Mail template header malformed.");
+ }
+ if (lineParts[0].equals("Subject")) {
+ subjectBlock = parseContent(new StringReader(lineParts[1])).getBlock(null);
+ }
+ }
+ if (subjectBlock == null) {
+ throw new IOException("Mail template without subject line.");
+ }
+ return parseContent(r);
+ }
+
+ public void sendMail(Language l, Map<String, Object> vars, String to) throws IOException {
+ tryReload();
+ vars.put(Outputable.OUT_KEY_PLAIN, true);
+
+ String body = runTemplate(this, l, vars);
+ String subject = runTemplate(subjectBlock, l, vars);
+
+ SendMail.getInstance().sendMail(to, SUBJECT_TAG + subject, body, "support@cacert.org", null, null, null, null, false);
+ }
+
+ private static String runTemplate(Outputable toRun, Language l, Map<String, Object> vars) {
+ StringWriter sw = new StringWriter();
+ PrintWriter pw = new PrintWriter(sw);
+ toRun.output(pw, l, vars);
+ pw.close();
+ return sw.toString();
+ }
+
+ @Override
+ public void addTranslations(Collection<String> s) {
+ subjectBlock.addTranslations(s);
+ super.addTranslations(s);
+ }
+
+}
*/
public class Template implements Outputable {
- private static class ParseResult {
+ protected static class ParseResult {
TemplateBlock block;
}
}
- private ParseResult parse(Reader r) throws IOException {
+ protected ParseResult parse(Reader r) throws IOException {
+ return parseContent(r);
+ }
+
+ protected ParseResult parseContent(Reader r) throws IOException {
LinkedList<String> splitted = new LinkedList<String>();
LinkedList<Translatable> commands = new LinkedList<Translatable>();
StringBuffer buf = new StringBuffer();
if (m.matches()) {
String type = m.group(1);
String variable = m.group(2);
- ParseResult body = parse(r);
+ ParseResult body = parseContent(r);
if (type.equals("if")) {
if ("else".equals(body.getEndType())) {
- commands.add(new IfStatement(variable, body.getBlock("else"), parse(r).getBlock("}")));
+ commands.add(new IfStatement(variable, body.getBlock("else"), parseContent(r).getBlock("}")));
} else {
commands.add(new IfStatement(variable, body.getBlock("}")));
}
@Override
public void output(PrintWriter out, Language l, Map<String, Object> vars) {
+ tryReload();
+ data.output(out, l, vars);
+ }
+
+ protected void tryReload() {
if (source != null && lastLoaded < source.lastModified()) {
try {
System.out.println("Reloading template.... " + source);
e.printStackTrace();
}
}
- data.output(out, l, vars);
}
protected static void outputVar(PrintWriter out, Language l, Map<String, Object> vars, String varname, boolean unescaped) {
--- /dev/null
+package org.cacert.gigi.template;
+
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.HashMap;
+import java.util.Locale;
+
+import org.cacert.gigi.localisation.Language;
+import org.cacert.gigi.output.template.MailTemplate;
+import org.cacert.gigi.testUtils.BusinessTest;
+import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail;
+import org.junit.Test;
+
+public class TestTemplateMail extends BusinessTest {
+
+ private static final String TEST_MAIL = "test@mail.com";
+
+ private void testExecuteMail(HashMap<String, Object> vars, String input) throws IOException {
+ MailTemplate t = new MailTemplate(new StringReader(input));
+ t.sendMail(Language.getInstance(Locale.ENGLISH), vars, TEST_MAIL);
+ }
+
+ HashMap<String, Object> vars = new HashMap<>();
+
+ @Test
+ public void testSimple() throws IOException {
+ vars.put("var", "val");
+ testExecuteMail(vars, "Subject: subj\n\n<?=$var?>l");
+ TestMail tm = getMailReceiver().receive();
+ assertEquals(MailTemplate.SUBJECT_TAG + "subj", tm.getSubject());
+ assertEquals("vall", tm.getMessage());
+ }
+
+ @Test
+ public void testVarNoEscape() throws IOException {
+ vars.put("var", "val\">");
+ vars.put("var2", "sl\">");
+ testExecuteMail(vars, "Subject: a<?=$var?>b\n\n<?=$var2?>l");
+ TestMail tm = getMailReceiver().receive();
+ assertEquals(MailTemplate.SUBJECT_TAG + "aval\">b", tm.getSubject());
+ assertEquals("sl\">l", tm.getMessage());
+
+ }
+
+ @Test
+ public void testTranslate() throws IOException {
+ testExecuteMail(vars, "Subject: a<?=_a<?>b\n\nc<?=_b\"?>l");
+ TestMail tm = getMailReceiver().receive();
+ assertEquals(MailTemplate.SUBJECT_TAG + "aa<b", tm.getSubject());
+ assertEquals("cb\"l", tm.getMessage());
+
+ }
+}