]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/EmailAddress.java
Merge changes I5927fb2c,I13853b13
[gigi.git] / src / org / cacert / gigi / dbObjects / EmailAddress.java
1 package org.cacert.gigi.dbObjects;
2
3 import java.io.IOException;
4 import java.util.Arrays;
5 import java.util.Date;
6 import java.util.HashMap;
7 import java.util.Locale;
8 import java.util.Map;
9
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;
19
20 public class EmailAddress implements IdCachable, Verifyable {
21
22     public static final int REPING_MINIMUM_DELAY = 5 * 60 * 1000;
23
24     private String address;
25
26     private int id;
27
28     private User owner;
29
30     private EmailAddress(int id) {
31         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid`, `email` FROM `emails` WHERE `id`=? AND `deleted` IS NULL")) {
32             ps.setInt(1, id);
33
34             GigiResultSet rs = ps.executeQuery();
35             if ( !rs.next()) {
36                 throw new IllegalArgumentException("Invalid email id " + id);
37             }
38             this.id = id;
39             owner = User.getById(rs.getInt(1));
40             address = rs.getString(2);
41         }
42     }
43
44     public EmailAddress(User owner, String address, Locale mailLocale) throws GigiApiException {
45         if ( !EmailProvider.MAIL.matcher(address).matches()) {
46             throw new IllegalArgumentException("Invalid email.");
47         }
48         this.address = address;
49         this.owner = owner;
50         insert(Language.getInstance(mailLocale));
51     }
52
53     private void insert(Language l) throws GigiApiException {
54         try {
55             synchronized (EmailAddress.class) {
56                 if (id != 0) {
57                     throw new IllegalStateException("already inserted.");
58                 }
59                 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=?")) {
60                     ps.setInt(1, owner.getId());
61                     ps.setString(2, address);
62                     psCheck.setString(1, address);
63                     GigiResultSet res = psCheck.executeQuery();
64                     if (res.next()) {
65                         throw new GigiApiException("The email address is already known to the system.");
66                     }
67                     ps.execute();
68                     id = ps.lastInsertId();
69                 }
70                 myCache.put(this);
71             }
72             ping(l);
73         } catch (IOException e) {
74             e.printStackTrace();
75         }
76     }
77
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);
84             statmt.execute();
85         }
86
87         MailProbe.sendMailProbe(l, "email", id, hash, address);
88     }
89
90     public int getId() {
91         return id;
92     }
93
94     public String getAddress() {
95         return address;
96     }
97
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`=?")) {
100             stmt.setString(1, address);
101             stmt.setInt(2, owner.getId());
102             stmt.setString(3, hash);
103             stmt.executeUpdate();
104         }
105         // Verify user with that primary email
106         try (GigiPreparedStatement ps2 = new GigiPreparedStatement("update `users` set `verified`='1' where `id`=? and `email`=? and `verified`='0'")) {
107             ps2.setInt(1, owner.getId());
108             ps2.setString(2, address);
109             ps2.execute();
110         }
111     }
112
113     public boolean isVerified() {
114         try (GigiPreparedStatement statmt = new GigiPreparedStatement("SELECT 1 FROM `emailPinglog` WHERE `email`=? AND `uid`=? AND `type`='active' AND `status`='success'")) {
115             statmt.setString(1, address);
116             statmt.setInt(2, owner.getId());
117             GigiResultSet e = statmt.executeQuery();
118             return e.next();
119         }
120     }
121
122     public Date getLastPing(boolean onlySuccess) {
123         Date lastExecution;
124         try (GigiPreparedStatement statmt = new GigiPreparedStatement("SELECT MAX(`when`) FROM `emailPinglog` WHERE `email`=? AND `uid`=? AND `type`='active'" + (onlySuccess ? " AND `status`='success'" : ""))) {
125             statmt.setString(1, address);
126             statmt.setInt(2, owner.getId());
127             GigiResultSet e = statmt.executeQuery();
128             if ( !e.next()) {
129                 return null;
130             }
131             lastExecution = e.getTimestamp(1);
132         }
133         return lastExecution;
134     }
135
136     public synchronized void requestReping(Language l) throws IOException, GigiApiException {
137         Date lastExecution = getLastPing(false);
138
139         if (lastExecution != null && lastExecution.getTime() + REPING_MINIMUM_DELAY >= System.currentTimeMillis()) {
140             Map<String, Object> data = new HashMap<String, Object>();
141             data.put("data", new Date(lastExecution.getTime() + REPING_MINIMUM_DELAY));
142             throw new GigiApiException(new Scope(new SprintfCommand("Reping is only allowed after 5 minutes, yours end at {0}.", Arrays.asList("${data}")), data));
143         }
144         ping(l);
145         return;
146     }
147
148     private static ObjectCache<EmailAddress> myCache = new ObjectCache<>();
149
150     public static synchronized EmailAddress getById(int id) throws IllegalArgumentException {
151         EmailAddress em = myCache.get(id);
152         if (em == null) {
153             myCache.put(em = new EmailAddress(id));
154         }
155         return em;
156     }
157 }