1 package org.cacert.gigi.dbObjects;
3 import java.io.IOException;
5 import java.util.LinkedList;
6 import java.util.Locale;
8 import org.cacert.gigi.GigiApiException;
9 import org.cacert.gigi.database.GigiPreparedStatement;
10 import org.cacert.gigi.database.GigiResultSet;
11 import org.cacert.gigi.email.EmailProvider;
12 import org.cacert.gigi.email.MailProbe;
13 import org.cacert.gigi.localisation.Language;
14 import org.cacert.gigi.output.template.SprintfCommand;
15 import org.cacert.gigi.util.RandomToken;
17 public class EmailAddress implements IdCachable, Verifyable {
19 public static final int REPING_MINIMUM_DELAY = 5 * 60 * 1000;
21 private String address;
27 private EmailAddress(int id) {
28 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid`, `email` FROM `emails` WHERE `id`=? AND `deleted` IS NULL")) {
31 GigiResultSet rs = ps.executeQuery();
33 throw new IllegalArgumentException("Invalid email id " + id);
36 owner = User.getById(rs.getInt(1));
37 address = rs.getString(2);
41 public EmailAddress(User owner, String address, Locale mailLocale) throws GigiApiException {
42 address = address.toLowerCase();
43 if ( !EmailProvider.isValidMailAddress(address)) {
44 throw new IllegalArgumentException("Invalid email.");
46 this.address = address;
48 insert(Language.getInstance(mailLocale));
51 private void insert(Language l) throws GigiApiException {
53 synchronized (EmailAddress.class) {
55 throw new IllegalStateException("already inserted.");
57 try (GigiPreparedStatement psCheck = new GigiPreparedStatement("SELECT 1 FROM `emails` WHERE email=? AND deleted is NULL")) {
58 psCheck.setString(1, address);
59 GigiResultSet res = psCheck.executeQuery();
61 throw new GigiApiException("The email address is already known to the system.");
64 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `emails` SET memid=?, email=?")) {
65 ps.setInt(1, owner.getId());
66 ps.setString(2, address);
68 id = ps.lastInsertId();
73 } catch (IOException e) {
78 private void ping(Language l) throws IOException {
79 String hash = RandomToken.generateToken(16);
80 try (GigiPreparedStatement statmt = new GigiPreparedStatement("INSERT INTO `emailPinglog` SET `when`=NOW(), `email`=?, `result`='', `uid`=?, `type`='active', `status`='open'::`pingState`, `challenge`=?")) {
81 statmt.setString(1, address);
82 statmt.setInt(2, owner.getId());
83 statmt.setString(3, hash);
87 MailProbe.sendMailProbe(l, "email", id, hash, address);
94 public String getAddress() {
98 public synchronized void verify(String hash) throws GigiApiException {
99 try (GigiPreparedStatement stmt = new GigiPreparedStatement("UPDATE `emailPinglog` SET `status`='success'::`pingState` WHERE `email`=? AND `uid`=? AND `type`='active' AND `challenge`=? AND `status`='open'::`pingState`")) {
100 stmt.setString(1, address);
101 stmt.setInt(2, owner.getId());
102 stmt.setString(3, hash);
103 if ( !stmt.executeMaybeUpdate()) {
104 throw new IllegalArgumentException("Given token could not be found to complete the verification process (Domain Ping).");
107 // Verify user with that primary email
108 try (GigiPreparedStatement ps2 = new GigiPreparedStatement("update `users` set `verified`='1' where `id`=? and `email`=? and `verified`='0'")) {
109 ps2.setInt(1, owner.getId());
110 ps2.setString(2, address);
115 public boolean isVerified() {
116 try (GigiPreparedStatement statmt = new GigiPreparedStatement("SELECT 1 FROM `emailPinglog` WHERE `email`=? AND `uid`=? AND `type`='active' AND `status`='success'")) {
117 statmt.setString(1, address);
118 statmt.setInt(2, owner.getId());
119 GigiResultSet e = statmt.executeQuery();
124 public Date getLastPing(boolean onlySuccess) {
126 try (GigiPreparedStatement statmt = new GigiPreparedStatement("SELECT MAX(`when`) FROM `emailPinglog` WHERE `email`=? AND `uid`=? AND `type`='active'" + (onlySuccess ? " AND `status`='success'" : ""))) {
127 statmt.setString(1, address);
128 statmt.setInt(2, owner.getId());
129 GigiResultSet e = statmt.executeQuery();
133 lastExecution = e.getTimestamp(1);
135 return lastExecution;
138 public synchronized void requestReping(Language l) throws IOException, GigiApiException {
139 Date lastExecution = getLastPing(false);
141 if (lastExecution != null && lastExecution.getTime() + REPING_MINIMUM_DELAY >= System.currentTimeMillis()) {
142 throw new GigiApiException(SprintfCommand.createSimple("Reping is only allowed after {0} minutes, yours end at {1}.", REPING_MINIMUM_DELAY / 60 / 1000, new Date(lastExecution.getTime() + REPING_MINIMUM_DELAY)));
148 private static ObjectCache<EmailAddress> myCache = new ObjectCache<>();
150 public static synchronized EmailAddress getById(int id) throws IllegalArgumentException {
151 EmailAddress em = myCache.get(id);
153 myCache.put(em = new EmailAddress(id));
158 public User getOwner() {
162 public static EmailAddress[] findByAllEmail(String mail) {
163 LinkedList<EmailAddress> results = new LinkedList<EmailAddress>();
164 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `emails`.`id` FROM `emails` INNER JOIN `users` ON `users`.`id` = `emails`.`memid` INNER JOIN `certOwners` ON `certOwners`.`id` = `users`.`id` WHERE `emails`.`email` LIKE ? AND `emails`.`deleted` IS NULL AND `certOwners`.`deleted` IS NULL ORDER BY `users`.`id`, `emails`.`email` LIMIT 100")) {
165 ps.setString(1, mail);
166 GigiResultSet rs = ps.executeQuery();
168 results.add(EmailAddress.getById(rs.getInt(1)));
170 return results.toArray(new EmailAddress[results.size()]);