-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathEmailTracker.java
More file actions
230 lines (204 loc) · 8.17 KB
/
EmailTracker.java
File metadata and controls
230 lines (204 loc) · 8.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import java.net.*;
import java.io.*;
import java.time.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.lang.Math;
import java.nio.charset.StandardCharsets;
// Twilio
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;
// EmailTracker is a functioning web file server that serves files in ./content/
public class EmailTracker {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
int portNumber = Integer.parseInt(args[0]);
String timeStr = getHTTPTime();
try {
serverSocket = new ServerSocket(portNumber);
}
catch (IOException e) {
System.err.println("Could not listen on port: " + portNumber);
System.err.println(e);
System.exit(1);
}
while(true) {
Socket clientSocket = null;
try {
// Wait for connection
clientSocket = serverSocket.accept();
}
catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
try {
System.out.println("Connected");
serveClient(clientSocket);
}
catch(Exception e) {
System.out.println("Exception!" + e);
}
}
//serverSocket.close();
}
public static void serveClient(Socket clientSocket) throws IOException {
DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader in = new BufferedReader(
new InputStreamReader( clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
String [] inputSplit = inputLine.split(" ");
System.out.print(inputLine);
// We only care about GET requests
if(inputSplit[0].equals("GET")) {
// Read rest of request:
while((inputLine = in.readLine()).length() > 0) {
System.out.print(inputLine);
}
try {
String [] sentMsg = inputSplit[1].split("/");
String fileName = sentMsg[1];
String receiver = sentMsg[2];
String subject = sentMsg[3];
File requestedFile = new File("./" + fileName);
if(!requestedFile.exists()) {
replyFileNotFound(out);
continue;
}
int fileLength = (int)requestedFile.length();
String fileSuffix = fileName.split("\\.")[1];
String contentType = getContentType(fileSuffix);
processGET(fileName, receiver, subject, out);
}
catch(Exception e) {
System.err.println(e);
}
}
}
out.close();
in.close();
clientSocket.close();
}
public static String getHTTPTime() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
return dateFormat.format(cal.getTime());
}
public static void replyFileNotFound(DataOutputStream out) throws IOException {
String msg = "HTTP/1.1 404 Not Found\r\n";
out.writeBytes(msg);
}
public static void replyFileInternalError(DataOutputStream out) throws IOException {
String msg = "HTTP/1.1 500 Internal Server Error\r\n";
out.writeBytes(msg);
}
public static String getContentType(String fileSuffix) {
/*
text/plain (.txt)
text/css (.css)
text/html (.htm, .html)
image/gif (.gif)
image/jpeg (.jpg, .jpeg)
image/png (.png)
video/webm (.webm)
video/mp4 (.mp4)
application/javascript (.js)
application/ogg (.ogg)
application/octet-stream (anything else)
*/
switch(fileSuffix) {
case "txt":
return "text/plain";
case "css":
return "text/css";
case "htm":
return "text/html";
case "html":
return "text/html";
case "gif":
return "image/gif";
case "jpg":
return "image/jpeg";
case "jpeg":
return "image/jpeg";
case "png":
return "image/png";
case "webm":
return "video/webm";
case "mp4":
return "video/mp4";
case "js":
return "application/javascript";
case "ogg":
return "application/ogg";
default:
return "application/octet-stream";
}
}
public static void processGET(String fileName, String receiver, String subject, DataOutputStream out) throws IOException, FileNotFoundException {
File requestedFile = new File("./" + fileName);
if(!requestedFile.exists()) {
replyFileNotFound(out);
return;
}
int fileLength = (int)requestedFile.length();
String fileSuffix = fileName.split("\\.")[1];
sendWholeFile(requestedFile, fileSuffix, out);
sendText(receiver, subject);
}
public static void sendWholeFile(File file, String fileSuffix, DataOutputStream out) throws IOException, FileNotFoundException {
FileInputStream fin = new FileInputStream(file);
//byte fileContent[] = new byte[CHUNK_SIZE];
String msg = "HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nKeep-Alive: timeout=50, max=1000\r\n";
msg += "Date: " + getHTTPTime() + "\r\n";
int contentLength = (int)file.length();
int size = contentLength;
msg += "Content-Length: " + contentLength + "\r\n";
String contentType = getContentType(fileSuffix);
msg += "Content-Type: " + contentType + "\r\n";
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
dateFormat.setTimeZone(TimeZone.getTimeZone("G MT"));
msg += "Last-Modified: " + dateFormat.format(file.lastModified()) + "\r\n" ;
System.out.println(msg);
out.writeBytes(msg + "\r\n");
byte fileContent[] = new byte[size];
fin.read(fileContent);
out.write(fileContent, 0, size);
out.writeBytes("\r\n");
}
public static void sendText(String receiver, String subject) {
System.out.println("");
System.out.println("Sent text!");
System.out.println("Receiver: " + receiver);
System.out.println("Subject: " + subject);
// Get time
Calendar cal = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("E dd/MM/yyyy hh:mm aa z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("EST"));
String timeString = dateFormat.format(cal.getTime());
// Send text using twilio
// Find your Account Sid and Auth Token at twilio.com/console
// In shell:
// export TWILIO_ACCOUNT_SID="YOUR_SID"
String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");
// In shell:
// export TWILIO_ACCOUNT_AUTH="YOUR_AUTH"
String AUTH_TOKEN = System.getenv("TWILIO_ACCOUNT_AUTH");
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
String messageText = "Message to: " + receiver + "\nwith Subject: " + subject + "\b has been read at: " + timeString + "!";
// SET TO PHONE NUMBER
// SET FROM PHONE NUMER
String toNumber = "TO_NUM";
String fromNumber = "FROM_NUM";
Message message = Message
.creator(new PhoneNumber(toNumber), // to
new PhoneNumber(fromNumber), // from
messageText)
.create();
System.out.println(message.getSid());
System.out.println("Sent text!");
}
}