1 package org.cacert.gigi.dbObjects;
3 import java.io.IOException;
4 import java.util.Arrays;
6 import java.util.HashMap;
7 import java.util.Locale;
10 import org.cacert.gigi.GigiApiException;
11 import org.cacert.gigi.database.GigiPreparedStatement;
12 import org.cacert.gigi.database.GigiResultSet;
13 import org.cacert.gigi.email.EmailProvider;
14 import org.cacert.gigi.email.MailProbe;
15 import org.cacert.gigi.localisation.Language;
16 import org.cacert.gigi.output.template.Scope;
17 import org.cacert.gigi.output.template.SprintfCommand;
18 import org.cacert.gigi.util.RandomToken;
20 public class EmailAddress implements IdCachable, Verifyable {
22 public static final int REPING_MINIMUM_DELAY = 5 * 60 * 1000;
24 private String address;
30 private EmailAddress(int id) {
31 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid`, `email` FROM `emails` WHERE `id`=? AND `deleted` IS NULL")) {
34 GigiResultSet rs = ps.executeQuery();
36 throw new IllegalArgumentException("Invalid email id " + id);
39 owner = User.getById(rs.getInt(1));
40 address = rs.getString(2);
44 public EmailAddress(User owner, String address, Locale mailLocale) throws GigiApiException {
45 address = address.toLowerCase();
46 if ( !EmailProvider.MAIL.matcher(address).matches()) {
47 throw new IllegalArgumentException("Invalid email.");
49 this.address = address;
51 insert(Language.getInstance(mailLocale));
54 private void insert(Language l) throws GigiApiException {
56 synchronized (EmailAddress.class) {
58 throw new IllegalStateException("already inserted.");
60 try (GigiPreparedStatement psCheck = new GigiPreparedStatement("SELECT 1 FROM `emails` WHERE email=? AND deleted is NULL"); GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `emails` SET memid=?, email=?")) {
61 ps.setInt(1, owner.getId());
62 ps.setString(2, address);
63 psCheck.setString(1, address);
64 GigiResultSet res = psCheck.executeQuery();
66 throw new GigiApiException("The email address is already known to the system.");
69 id = ps.lastInsertId();
74 } catch (IOException e) {
79 private void ping(Language l) throws IOException {
80 String hash = RandomToken.generateToken(16);
81 try (GigiPreparedStatement statmt = new GigiPreparedStatement("INSERT INTO `emailPinglog` SET `when`=NOW(), `email`=?, `result`='', `uid`=?, `type`='active', `status`='open'::`pingState`, `challenge`=?")) {
82 statmt.setString(1, address);
83 statmt.setInt(2, owner.getId());
84 statmt.setString(3, hash);
88 MailProbe.sendMailProbe(l, "email", id, hash, address);
95 public String getAddress() {
99 public synchronized void verify(String hash) throws GigiApiException {
100 try (GigiPreparedStatement stmt = new GigiPreparedStatement("UPDATE `emailPinglog` SET `status`='success'::`pingState` WHERE `email`=? AND `uid`=? AND `type`='active' AND `challenge`=?")) {
101 stmt.setString(1, address);
102 stmt.setInt(2, owner.getId());
103 stmt.setString(3, hash);
104 stmt.executeUpdate();
106 // Verify user with that primary email
107 try (GigiPreparedStatement ps2 = new GigiPreparedStatement("update `users` set `verified`='1' where `id`=? and `email`=? and `verified`='0'")) {
108 ps2.setInt(1, owner.getId());
109 ps2.setString(2, address);
114 public boolean isVerified() {
115 try (GigiPreparedStatement statmt = new GigiPreparedStatement("SELECT 1 FROM `emailPinglog` WHERE `email`=? AND `uid`=? AND `type`='active' AND `status`='success'")) {
116 statmt.setString(1, address);
117 statmt.setInt(2, owner.getId());
118 GigiResultSet e = statmt.executeQuery();
123 public Date getLastPing(boolean onlySuccess) {
125 try (GigiPreparedStatement statmt = new GigiPreparedStatement("SELECT MAX(`when`) FROM `emailPinglog` WHERE `email`=? AND `uid`=? AND `type`='active'" + (onlySuccess ? " AND `status`='success'" : ""))) {
126 statmt.setString(1, address);
127 statmt.setInt(2, owner.getId());
128 GigiResultSet e = statmt.executeQuery();
132 lastExecution = e.getTimestamp(1);
134 return lastExecution;
137 public synchronized void requestReping(Language l) throws IOException, GigiApiException {
138 Date lastExecution = getLastPing(false);
140 if (lastExecution != null && lastExecution.getTime() + REPING_MINIMUM_DELAY >= System.currentTimeMillis()) {
141 Map<String, Object> data = new HashMap<String, Object>();
142 data.put("data", new Date(lastExecution.getTime() + REPING_MINIMUM_DELAY));
143 throw new GigiApiException(new Scope(new SprintfCommand("Reping is only allowed after 5 minutes, yours end at {0}.", Arrays.asList("${data}")), data));
149 private static ObjectCache<EmailAddress> myCache = new ObjectCache<>();
151 public static synchronized EmailAddress getById(int id) throws IllegalArgumentException {
152 EmailAddress em = myCache.get(id);
154 myCache.put(em = new EmailAddress(id));