-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathOutboxToadlet.java
More file actions
159 lines (134 loc) · 5.54 KB
/
OutboxToadlet.java
File metadata and controls
159 lines (134 loc) · 5.54 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
/*
* OutboxToadlet.java
* This file is part of Freemail
* Copyright (C) 2011 Martin Nyhus
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.freenetproject.freemail.ui.web;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.freenetproject.freemail.AccountManager;
import org.freenetproject.freemail.FreemailAccount;
import org.freenetproject.freemail.FreemailPlugin;
import org.freenetproject.freemail.l10n.FreemailL10n;
import org.freenetproject.freemail.transport.MessageHandler.OutboxMessage;
import org.freenetproject.freemail.utils.Base32;
import org.freenetproject.freemail.utils.Logger;
import org.freenetproject.freemail.utils.Timer;
import org.freenetproject.freemail.wot.Identity;
import org.freenetproject.freemail.wot.WoTConnection;
import freenet.clients.http.PageNode;
import freenet.clients.http.ToadletContext;
import freenet.pluginmanager.PluginNotFoundException;
import freenet.pluginmanager.PluginRespirator;
import freenet.support.Base64;
import freenet.support.HTMLNode;
import freenet.support.IllegalBase64Exception;
import freenet.support.api.HTTPRequest;
public class OutboxToadlet extends WebPage {
private static final String PATH = WebInterface.PATH + "/Outbox";
private final AccountManager accountManager;
private final FreemailPlugin freemailPlugin;
OutboxToadlet(PluginRespirator pluginRespirator, AccountManager accountManager, FreemailPlugin freemailPlugin,
LoginManager loginManager) {
super(pluginRespirator, loginManager);
this.accountManager = accountManager;
this.freemailPlugin = freemailPlugin;
}
@Override
HTTPResponse makeWebPageGet(URI uri, HTTPRequest req, ToadletContext ctx, PageNode page) throws IOException {
Timer outboxTimer = Timer.start();
String identity = loginManager.getSession(ctx).getUserID();
FreemailAccount account = accountManager.getAccount(identity);
HTMLNode messageTable = page.content.addChild("table");
//Add the message list header
HTMLNode header = messageTable.addChild("tr");
header.addChild("th", FreemailL10n.getString("Freemail.OutboxToadlet.recipient"));
header.addChild("th", FreemailL10n.getString("Freemail.OutboxToadlet.subject"));
header.addChild("th", FreemailL10n.getString("Freemail.OutboxToadlet.firstSendTime"));
header.addChild("th", FreemailL10n.getString("Freemail.OutboxToadlet.lastSendTime"));
WoTConnection wotConnection = freemailPlugin.getWotConnection();
Timer messageRead = outboxTimer.startSubTimer();
List<OutboxMessage> messages = account.getMessageHandler().listOutboxMessages();
messageRead.log(this, 1, TimeUnit.SECONDS, "Time spent reading outbox messages");
Timer messageListing = outboxTimer.startSubTimer();
for(OutboxMessage message : messages) {
HTMLNode row = messageTable.addChild("tr");
String recipient;
try {
Identity i = wotConnection.getIdentity(message.recipient, account.getIdentity());
if(i != null) {
String domain = i.getBase32IdentityID();
recipient = i.getNickname() + "@" + domain + ".freemail";
} else {
recipient = null;
}
} catch(PluginNotFoundException e) {
recipient = null;
}
if(recipient == null) {
//Fall back to showing the address without the nickname
String id;
try {
id = Base32.encodeWithoutPadding(Base64.decode(message.recipient));
} catch (IllegalBase64Exception e) {
//This should never happen since we couldn't possibly have sent the message if
//the id couldn't be decoded
Logger.error(this, "Couldn't decode base64 id of message recipient: " + message.recipient);
id = message.recipient;
}
recipient = "unknown@" + id + ".freemail";
}
String firstSendTime;
if(message.getFirstSendTime() == null) {
firstSendTime = FreemailL10n.getString("Freemail.OutboxToadlet.neverSent");
} else {
firstSendTime = message.getFirstSendTime().toString();
}
String lastSendTime;
if(message.getFirstSendTime() == null) {
lastSendTime = FreemailL10n.getString("Freemail.OutboxToadlet.neverSent");
} else {
lastSendTime = message.getLastSendTime().toString();
}
row.addChild("td", recipient);
row.addChild("td", message.subject);
row.addChild("td", firstSendTime);
row.addChild("td", lastSendTime);
}
messageListing.log(this, "Time spent adding messages to page");
outboxTimer.log(this, 1, TimeUnit.SECONDS, "Time spent generating outbox page");
return new GenericHTMLResponse(ctx, 200, "OK", page.outer.generate());
}
@Override
HTTPResponse makeWebPagePost(URI uri, HTTPRequest req, ToadletContext ctx, PageNode page) throws IOException {
return makeWebPageGet(uri, req, ctx, page);
}
@Override
public boolean isEnabled(ToadletContext ctx) {
return ctx.isAllowedFullAccess() && loginManager.sessionExists(ctx);
}
@Override
boolean requiresValidSession() {
return true;
}
@Override
public String path() {
return PATH;
}
}