]> WPIA git - gigi.git/blob - src/org/cacert/gigi/EmailAddress.java
ADD: simple pinger daemon.
[gigi.git] / src / org / cacert / gigi / EmailAddress.java
1 package org.cacert.gigi;
2
3 import java.io.IOException;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7
8 import org.cacert.gigi.database.DatabaseConnection;
9 import org.cacert.gigi.email.EmailProvider;
10 import org.cacert.gigi.email.MailProbe;
11 import org.cacert.gigi.localisation.Language;
12 import org.cacert.gigi.util.RandomToken;
13
14 public class EmailAddress {
15
16     private String address;
17
18     private int id;
19
20     private User owner;
21
22     private String hash = null;
23
24     private EmailAddress(int id) throws SQLException {
25         PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, email, hash FROM `emails` WHERE id=? AND deleted=0");
26         ps.setInt(1, id);
27
28         ResultSet rs = ps.executeQuery();
29         if ( !rs.next()) {
30             throw new IllegalArgumentException("Invalid email id " + id);
31         }
32         this.id = id;
33         owner = User.getById(rs.getInt(1));
34         address = rs.getString(2);
35         hash = rs.getString(3);
36         rs.close();
37     }
38
39     public EmailAddress(String address, User owner) {
40         if ( !EmailProvider.MAIL.matcher(address).matches()) {
41             throw new IllegalArgumentException("Invalid email.");
42         }
43         this.address = address;
44         this.owner = owner;
45         this.hash = RandomToken.generateToken(16);
46     }
47
48     public void insert(Language l) {
49         if (id != 0) {
50             throw new IllegalStateException("already inserted.");
51         }
52         try {
53             PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `emails` SET memid=?, hash=?, email=?");
54             ps.setInt(1, owner.getId());
55             ps.setString(2, hash);
56             ps.setString(3, address);
57             ps.execute();
58             id = DatabaseConnection.lastInsertId(ps);
59             MailProbe.sendMailProbe(l, "email", id, hash, address);
60         } catch (SQLException e) {
61             e.printStackTrace();
62         } catch (IOException e) {
63             e.printStackTrace();
64         }
65     }
66
67     public int getId() {
68         return id;
69     }
70
71     public String getAddress() {
72         return address;
73     }
74
75     public synchronized void verify(String hash) throws GigiApiException {
76         if (this.hash.equals(hash)) {
77
78             try {
79                 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `emails` SET hash='' WHERE id=?");
80                 ps.setInt(1, id);
81                 ps.execute();
82                 hash = "";
83
84                 // Verify user with that primary email
85                 PreparedStatement ps2 = DatabaseConnection.getInstance().prepare("update `users` set `verified`='1' where `id`=? and `email`=? and `verified`='0'");
86                 ps2.setInt(1, owner.getId());
87                 ps2.setString(2, address);
88                 ps2.execute();
89                 this.hash = "";
90             } catch (SQLException e) {
91                 throw new GigiApiException(e);
92             }
93
94         } else {
95             throw new GigiApiException("Email verification hash is invalid.");
96         }
97     }
98
99     public static EmailAddress getById(int id) throws IllegalArgumentException {
100         // TODO cache
101         try {
102             EmailAddress e = new EmailAddress(id);
103             return e;
104         } catch (SQLException e) {
105             throw new IllegalArgumentException(e);
106         }
107     }
108
109     public boolean isVerified() {
110         return hash.isEmpty();
111     }
112 }