1 package org.cacert.gigi.pages;
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.lang.reflect.Field;
7 import java.util.Calendar;
8 import java.util.GregorianCalendar;
9 import java.util.HashMap;
10 import java.util.Iterator;
11 import java.util.LinkedList;
12 import java.util.Locale;
14 import java.util.Properties;
16 import javax.servlet.http.HttpServletRequest;
17 import javax.servlet.http.HttpServletResponse;
19 import org.cacert.gigi.GigiApiException;
20 import org.cacert.gigi.database.DatabaseConnection;
21 import org.cacert.gigi.database.GigiPreparedStatement;
22 import org.cacert.gigi.dbObjects.EmailAddress;
23 import org.cacert.gigi.dbObjects.Group;
24 import org.cacert.gigi.dbObjects.Name;
25 import org.cacert.gigi.dbObjects.User;
26 import org.cacert.gigi.email.EmailProvider;
27 import org.cacert.gigi.localisation.Language;
28 import org.cacert.gigi.output.template.IterableDataset;
29 import org.cacert.gigi.output.template.Template;
30 import org.cacert.gigi.util.Notary;
32 public class Manager extends Page {
34 public static final String PATH = "/manager";
39 super("Test Manager");
41 f = EmailAddress.class.getDeclaredField("hash");
42 f.setAccessible(true);
43 } catch (ReflectiveOperationException e) {
48 public User[] getAssurers() {
49 if (assurers != null) {
52 assurers = new User[10];
54 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?");
55 for (int i = 0; i < assurers.length; i++) {
56 String mail = "test-assurer" + i + "@example.com";
57 User u = User.getByEmail(mail);
59 System.out.println("Creating assurer");
61 u = User.getByEmail(mail);
63 ps.setInt(1, u.getId());
64 ps.setInt(2, u.getId());
66 ps.setString(4, "Manager init code");
67 ps.setString(5, "1990-01-01");
73 } catch (ReflectiveOperationException | GigiApiException e) {
79 private void passCATS(User u) {
80 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO cats_passed SET user_id=?, variant_id=3");
81 ps.setInt(1, u.getId());
85 private static Manager instance;
87 Template t = new Template(Manager.class.getResource("ManagerMails.templ"));
89 HashMap<String, LinkedList<String>> emails = new HashMap<>();
91 public static Manager getInstance() {
92 if (instance == null) {
93 instance = new Manager();
98 public static class MailFetcher extends EmailProvider {
100 public MailFetcher(Properties p) {}
103 public String checkEmailServer(int forUid, String address) throws IOException {
108 public synchronized void sendmail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
109 HashMap<String, LinkedList<String>> mails = Manager.getInstance().emails;
110 LinkedList<String> hismails = mails.get(to);
111 if (hismails == null) {
112 mails.put(to, hismails = new LinkedList<>());
114 hismails.addFirst(subject + "\n" + message);
119 public void batchCreateUsers(String mailPrefix, String domain, int amount, PrintWriter out) {
123 out.print("100 at most, please.");
126 for (int i = 0; i < amount; i++) {
127 String email = mailPrefix + i + "@" + domain;
130 } catch (ReflectiveOperationException e) {
131 out.println("failed");
133 } catch (GigiApiException e) {
134 out.println("failed: " + e.getMessage());
139 private void createUser(String email) throws GigiApiException, IllegalAccessException {
141 u.setName(new Name("Först", "Läst", "Müddle", "Süffix"));
143 Calendar gc = GregorianCalendar.getInstance();
145 u.setDoB(new Date(gc.getTime().getTime()));
146 u.setPreferredLocale(Locale.ENGLISH);
147 u.insert("xvXV12°§");
148 EmailAddress ea = new EmailAddress(u, email);
149 ea.insert(Language.getInstance(Locale.ENGLISH));
150 String hash = (String) f.get(ea);
158 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
159 if (req.getParameter("create") != null) {
160 batchCreateUsers(req.getParameter("prefix"), req.getParameter("suffix"), Integer.parseInt(req.getParameter("amount")), resp.getWriter());
161 resp.getWriter().println("User batch created.");
162 } else if (req.getParameter("addpriv") != null || req.getParameter("delpriv") != null) {
163 User u = User.getByEmail(req.getParameter("email"));
165 resp.getWriter().println("User not found.");
168 if (req.getParameter("addpriv") != null) {
169 u.grantGroup(u, Group.getByString(req.getParameter("priv")));
170 resp.getWriter().println("Privilege granted");
172 u.revokeGroup(u, Group.getByString(req.getParameter("priv")));
173 resp.getWriter().println("Privilege revoked");
175 } else if (req.getParameter("fetch") != null) {
176 String mail = req.getParameter("femail");
177 fetchMails(req, resp, mail);
178 } else if (req.getParameter("cats") != null) {
179 String mail = req.getParameter("catsEmail");
180 User byEmail = User.getByEmail(mail);
181 if (byEmail == null) {
182 resp.getWriter().println("User not found.");
186 resp.getWriter().println("User has been passed CATS");
187 } else if (req.getParameter("assure") != null) {
188 String mail = req.getParameter("assureEmail");
189 User byEmail = User.getByEmail(mail);
190 if (byEmail == null) {
191 resp.getWriter().println("User not found.");
195 for (int i = 0; i < getAssurers().length; i++) {
196 Notary.assure(getAssurers()[i], byEmail, byEmail.getName(), byEmail.getDoB(), 10, "Testmanager Assure up code", "2014-11-06");
198 } catch (GigiApiException e) {
201 resp.getWriter().println("User has been assured.");
202 } else if (req.getParameter("addEmail") != null) {
203 User u = User.getByEmail(req.getParameter("addEmailEmail"));
204 EmailAddress ea = new EmailAddress(u, req.getParameter("addEmailNew"));
206 ea.insert(Language.getInstance(Locale.ENGLISH));
207 String hash = (String) f.get(ea);
209 resp.getWriter().println("Email added and verified");
210 } catch (IllegalArgumentException e) {
212 resp.getWriter().println("An internal error occured.");
213 } catch (IllegalAccessException e) {
215 resp.getWriter().println("An internal error occured.");
216 } catch (GigiApiException e) {
217 e.format(resp.getWriter(), Language.getInstance(Locale.ENGLISH));
223 private void fetchMails(HttpServletRequest req, HttpServletResponse resp, String mail) throws IOException {
224 final LinkedList<String> mails = emails.get(mail);
225 HashMap<String, Object> vars = new HashMap<>();
226 vars.put("mail", mail);
228 vars.put("mails", new IterableDataset() {
230 Iterator<String> s = mails.iterator();
233 public boolean next(Language l, Map<String, Object> vars) {
237 vars.put("body", s.next().replaceAll("(https?://\\S+)", "<a href=\"$1\">$1</a>"));
242 t.output(resp.getWriter(), getLanguage(req), vars);
244 resp.getWriter().println("No mails");
249 private Template form = new Template(Manager.class.getResource("Manager.templ"));
252 public boolean needsLogin() {
257 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
259 String pi = req.getPathInfo().substring(PATH.length());
260 if (pi.length() > 1 && pi.startsWith("/fetch-")) {
261 String mail = pi.substring(pi.indexOf('-', 2) + 1);
262 fetchMails(req, resp, mail);
266 form.output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());