Subversion Repositories HomeAutomation

Rev

Go to most recent revision | Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
15 arune 1
/*
2
 * File created by arune
3
 * at 2005-jan-09
4
 */
5
package Macbeth.Modules.modMailcheck;
6
 
7
import Macbeth.Utilities.UByte;
8
import Macbeth.Net.TcpClient;
9
 
10
import java.io.*;
11
 
12
/**
13
 *
14
 *
15
 *
16
 * @author arune
17
 */
18
public class AccountObject {
19
 
20
    public String server;
21
    public int port;
22
    public String accountname;
23
    public String password;
24
 
25
    private String lastMessageID = null;
26
 
27
 
28
    /**
29
     * Creates a new instance of AccountObject.
30
     * @param server The servername for this account.
31
     * @param port .
32
     * @param accountname .
33
     * @param password .
34
     */
35
    public AccountObject(String server, int  port, String accountname, String password) {
36
        this.server = server;
37
        this.port = port;
38
        this.accountname = accountname;
39
        this.password = password;
40
    }
41
 
42
     /**
43
     *
44
     * @throws Exception whenever something goes wrong
45
     */
46
    public boolean hasNewMessages() throws Exception {
47
        TcpClient tcpClient = new TcpClient(port, server);
48
        String reply;
49
        try {
50
            tcpClient.openConnection();
51
        } catch (Exception e) {
52
            throw (new Exception("POP3: Cannot connect to POP3-server at '"  + server + ":" + port + "' ! " +
53
                    "Exception was: '" + e + "'"));
54
        }
55
 
56
        reply = tcpClient.getRow();
57
        //System.out.println(reply);
58
        if (!reply.substring(0, 3).equalsIgnoreCase("+OK")) {
59
            throw (new Exception("POP3(connect): " + reply));
60
        }
61
 
62
        tcpClient.sendString("USER " + accountname);
63
        reply = tcpClient.getRow();
64
        //System.out.println(reply);
65
        if (!reply.substring(0, 3).equalsIgnoreCase("+OK")) {
66
            throw (new Exception("POP3(user): " + reply));
67
        }
68
 
69
        tcpClient.sendString("PASS " + password);
70
        reply = tcpClient.getRow();
71
        //System.out.println(reply);
72
        if (!reply.substring(0, 3).equalsIgnoreCase("+OK")) {
73
            throw (new Exception("POP3(pass): " + reply));
74
        }
75
 
76
        tcpClient.sendString("STAT");
77
        reply = tcpClient.getRow();
78
        //System.out.println(reply);
79
        if (!reply.substring(0, 3).equalsIgnoreCase("+OK")) {
80
            throw (new Exception("POP3(stat): " + reply));
81
        }
82
        String[] replyTokens = reply.split(" ");
83
        int nrOfMess;
84
        if (replyTokens.length == 3) {
85
            try {
86
                nrOfMess = Integer.parseInt(replyTokens[1]);
87
            } catch (Exception e) {
88
                throw (new Exception("POP3(stat): " + reply));
89
            }
90
        }
91
        else {
92
            throw (new Exception("POP3(stat): " + reply));
93
        }
94
        //kolla om antalet är 0
95
        if (nrOfMess == 0) {
96
            lastMessageID = "";
97
            return false;
98
        }
99
 
100
        tcpClient.sendString("UIDL " + nrOfMess);
101
        reply = tcpClient.getRow();
102
        //System.out.println(reply);
103
        if (!reply.substring(0, 3).equalsIgnoreCase("+OK")) {
104
            throw (new Exception("POP3(uidl): " + reply));
105
        }
106
        replyTokens = reply.split(" ");
107
        String messID;
108
        if (replyTokens.length == 3) {
109
            messID = replyTokens[2];
110
        }
111
        else {
112
            throw (new Exception("POP3(uidl): " + reply));
113
        }
114
 
115
        boolean returnmess = false;
116
        if (lastMessageID == null) {
117
            lastMessageID = messID;
118
            returnmess = false;
119
        } else if (lastMessageID.equals("")) {
120
            //there is new messages on server BUT there were non last check
121
            lastMessageID = messID;
122
            returnmess = true;
123
        } else if (!lastMessageID.equals(messID)) {
124
            //sista meddelandet är inte samma längre
125
            lastMessageID = messID;
126
            returnmess = true;
127
        }
128
        tcpClient.sendString("QUIT");
129
        //System.out.println(tcpClient.getRow());
130
        tcpClient.closeConnection();
131
        return returnmess;
132
 
133
    }
134
    /**
135
    *
136
    * @throws Exception whenever something goes wrong
137
    */
138
    public int nrOfMessages() throws Exception {
139
        TcpClient tcpClient = new TcpClient(port, server);
140
        String reply;
141
        try {
142
            tcpClient.openConnection();
143
        } catch (Exception e) {
144
            throw (new Exception("POP3: Cannot connect to POP3-server at '"  + server + ":" + port + "' ! " +
145
                    "Exception was: '" + e + "'"));
146
        }
147
        reply = tcpClient.getRow();
148
        //System.out.println(reply);
149
        if (!reply.substring(0, 3).equalsIgnoreCase("+OK")) {
150
            throw (new Exception("POP3(connect): " + reply));
151
        }
152
 
153
        tcpClient.sendString("USER " + accountname);
154
        reply = tcpClient.getRow();
155
        //System.out.println(reply);
156
        if (!reply.substring(0, 3).equalsIgnoreCase("+OK")) {
157
            throw (new Exception("POP3(user): " + reply));
158
        }
159
 
160
        tcpClient.sendString("PASS " + password);
161
        reply = tcpClient.getRow();
162
        //System.out.println(reply);
163
        if (!reply.substring(0, 3).equalsIgnoreCase("+OK")) {
164
            throw (new Exception("POP3(pass): " + reply));
165
        }
166
 
167
        tcpClient.sendString("STAT");
168
        reply = tcpClient.getRow();
169
        //System.out.println(reply);
170
        //System.out.println("'" + reply.substring(0, 3) + "'");
171
        if (!reply.substring(0, 3).equalsIgnoreCase("+OK")) {
172
            throw (new Exception("POP3(stat1, " + accountname + " " + server + "): " + reply));
173
        }
174
        String[] replyTokens = reply.split(" ");
175
        int nrOfMess;
176
        if (replyTokens.length == 3) {
177
            try {
178
                nrOfMess = Integer.parseInt(replyTokens[1]);
179
            } catch (Exception e) {
180
                throw (new Exception("POP3(stat2, " + accountname + " " + server + "): " + reply));
181
            }
182
        }
183
        else {
184
            throw (new Exception("POP3(stat3, " + accountname + " " + server + "): " + reply));
185
        }
186
        //kolla om antalet är 0
187
        if (nrOfMess == 0) {
188
            lastMessageID = "";
189
            return 0;
190
        }
191
 
192
        tcpClient.sendString("UIDL");
193
        reply = tcpClient.getRow();
194
        //System.out.println(reply);
195
        if (!reply.substring(0, 3).equalsIgnoreCase("+OK")) {
196
            throw (new Exception("POP3(uidl): " + reply));
197
        }
198
 
199
        int nrOfNewMess = 0;
200
        for (int i = 1; i <= nrOfMess; i++) {
201
            reply = tcpClient.getRow();
202
 
203
            replyTokens = reply.split(" ");
204
            String messID;
205
            if (replyTokens.length == 2) {
206
                messID = replyTokens[1];
207
            }
208
            else {
209
                throw (new Exception("POP3(uidl): " + reply));
210
            }
211
 
212
            if (lastMessageID == null) {
213
                if (i == nrOfMess) {
214
                    lastMessageID = messID;
215
                    nrOfNewMess = 0;
216
                }
217
            } else if (lastMessageID.equals("")) {
218
                //there is new messages on server BUT there were non last check
219
                if (i == nrOfMess) {
220
                    lastMessageID = messID;
221
                    nrOfNewMess = nrOfMess;
222
                }
223
            } else if (lastMessageID.equals(messID)) {
224
                //found the old message
225
                nrOfNewMess = nrOfMess - i;
226
            } else if (i == nrOfMess) {
227
                //if all messages have been searched
228
                lastMessageID = messID;
229
                if (nrOfNewMess == 0) {
230
                    //and the old mess was not found
231
                    nrOfNewMess = nrOfMess;
232
                }
233
            }
234
        }
235
        tcpClient.sendString("QUIT");
236
        //System.out.println(tcpClient.getRow());
237
        tcpClient.closeConnection();
238
        return nrOfNewMess;
239
    }
240
 
241
}