/
RemembranceAgentClient.java
410 lines (356 loc) · 20.5 KB
/
RemembranceAgentClient.java
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package io.p13i.ra;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import io.p13i.ra.databases.cache.LocalDiskCacheDocumentDatabase;
import io.p13i.ra.databases.gmail.GmailDocumentDatabase;
import io.p13i.ra.databases.googledrive.GoogleDriveFolderDocumentDatabase;
import io.p13i.ra.databases.localdisk.LocalDiskDocumentDatabase;
import io.p13i.ra.engine.RemembranceAgentEngine;
import io.p13i.ra.gui.GUI;
import io.p13i.ra.gui.User;
import io.p13i.ra.models.Context;
import io.p13i.ra.models.Document;
import io.p13i.ra.models.Query;
import io.p13i.ra.models.ScoredDocument;
import io.p13i.ra.utils.*;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
import javax.swing.*;
import static io.p13i.ra.gui.User.Preferences.Pref.*;
public class RemembranceAgentClient {
private static final Logger LOGGER = LoggerUtils.getLogger(RemembranceAgentClient.class);
public static final String APPLICATION_NAME = "Remembrance Agent (v" + System.getenv("VERSION") + ")";
// Settings
private static final int KEYBOARD_BUFFER_SIZE = 75;
private static final int RA_CLIENT_UPDATE_PERIOD_MS = 2500;
// Swing elements
private static JFrame sJFrame;
private static JLabel sKeystrokeBufferLabel;
private static JPanel sSuggestionsPanel;
/**
* "local" variables
*/
private static final KeyboardLoggerBreakingBuffer sBreakingBuffer = new KeyboardLoggerBreakingBuffer(KEYBOARD_BUFFER_SIZE);
private static BufferingLogFileWriter sKeyLoggerBufferLogFileWriter;
private static Timer sRemembranceAgentUpdateTimer = new Timer();
// RA variables
private static RemembranceAgentEngine sRemembranceAgentEngine;
private static String sPriorQuery;
public static void main(String[] args) {
sKeyLoggerBufferLogFileWriter = new BufferingLogFileWriter(User.Preferences.getString(KeystrokesLogFile));
sKeyLoggerBufferLogFileWriter.open();
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
System.out.println("Shutting down ...");
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
sKeyLoggerBufferLogFileWriter.flush();
sKeyLoggerBufferLogFileWriter.close();
sKeyLoggerBufferLogFileWriter = null;
}
}
});
sJFrame = new JFrame("REMEMBRANCE AGENT") {{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(GUI.WIDTH, GUI.HEIGHT);
setResizable(false);
setAlwaysOnTop(true);
add(new JPanel() {{
setLayout(null);
add(Box.createHorizontalGlue());
setJMenuBar(new JMenuBar() {{
add(new JMenu("RA Settings") {{
add(new JMenuItem("Reinitialize remembrance agent") {{
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
initializeRemembranceAgent(true);
JOptionPane.showMessageDialog(sJFrame, "Reinitialized!");
}
});
}});
add(new JSeparator());
add(new JMenuItem("Invalidate/reload cache") {{
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(sJFrame, "Reloading with new cache! GUI will freeze");
sJFrame.setEnabled(false);
initializeRemembranceAgent(false);
JOptionPane.showMessageDialog(sJFrame, "Reinitialized with new cache!");
sJFrame.setEnabled(true);
}
});
}});
add(new JSeparator());
add(new JMenuItem("Select document database directory...") {{
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
// disable the "All files" option.
fileChooser.setAcceptAllFileFilterUsed(false);
if (fileChooser.showOpenDialog(sJFrame) == JFileChooser.APPROVE_OPTION) {
User.Preferences.set(LocalDiskDocumentsFolderPath, fileChooser.getSelectedFile().toPath().toString());
LOGGER.info("Selected directory: " + User.Preferences.getString(LocalDiskDocumentsFolderPath));
}
}
});
}});
add(new JMenuItem("Set Google Drive folder ID...") {{
addActionListener(e -> {
String inputId = JOptionPane.showInputDialog("Enter a Google Drive Folder ID (leave blank to cancel):", User.Preferences.getString(GoogleDriveFolderID));
if (inputId != null && inputId.length() > 0) {
User.Preferences.set(GoogleDriveFolderID, inputId);
LOGGER.info("Set Google Drive Folder ID: " + User.Preferences.getString(GoogleDriveFolderID));
}
});
}});
add(new JSeparator());
add(new JMenuItem("Set max Gmail email index count...") {{
addActionListener(e -> {
String inputId = JOptionPane.showInputDialog("Enter a count for recent emails to index (leave blank to cancel):", User.Preferences.getInt(GmailMaxEmailsCount));
if (inputId != null && inputId.length() > 0 && IntegerUtils.isInt(inputId)) {
User.Preferences.set(GmailMaxEmailsCount, inputId);
LOGGER.info("Set GmailMaxEmailsCount: " + User.Preferences.getInt(GmailMaxEmailsCount));
}
});
}});
add(new JSeparator());
add(new JMenuItem("Select ra-client.log directory...") {{
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
// disable the "All files" option.
fileChooser.setAcceptAllFileFilterUsed(false);
if (fileChooser.showOpenDialog(sJFrame) == JFileChooser.APPROVE_OPTION) {
User.Preferences.set(RAClientLogFile, fileChooser.getSelectedFile().toPath().toString() + File.separator + "ra-client.log");
LOGGER.info("Selected ra-client.log file: " + User.Preferences.getString(RAClientLogFile));
JOptionPane.showMessageDialog(sJFrame, "Selected ra-client.log file: " + User.Preferences.getString(RAClientLogFile));
}
}
});
}});
add(new JMenuItem("Open ra-client.log log...") {{
addActionListener(e -> {
try {
Desktop.getDesktop().open(new File(User.Preferences.getString(RAClientLogFile)));
} catch (IOException ex) {
ex.printStackTrace();
}
});
}});
}});
add(new JMenu("Keylogger") {{
add(new JMenuItem("Clear buffer") {{
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (sBreakingBuffer.isEmpty()) {
JOptionPane.showMessageDialog(sJFrame, "Buffer is empty. No clearing required.");
} else {
sBreakingBuffer.clear();
sKeystrokeBufferLabel.setText("");
}
}
});
}});
add(new JSeparator());
add(new JMenuItem("Select keystrokes.log directory...") {{
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
// disable the "All files" option.
fileChooser.setAcceptAllFileFilterUsed(false);
if (fileChooser.showOpenDialog(sJFrame) == JFileChooser.APPROVE_OPTION) {
User.Preferences.set(User.Preferences.Pref.KeystrokesLogFile, fileChooser.getSelectedFile().toPath().toString() + File.separator + "keystrokes.log");
LOGGER.info("Selected keystrokes.log file directory: " + User.Preferences.getString(KeystrokesLogFile));
sKeystrokeBufferLabel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Keylogger Buffer (writing to " + User.Preferences.getString(KeystrokesLogFile) + ")"),
BorderFactory.createEmptyBorder(GUI.BORDER_PADDING, GUI.BORDER_PADDING, GUI.BORDER_PADDING, GUI.BORDER_PADDING)));
}
}
});
}});
add(new JMenuItem("Open keystrokes.log log...") {{
addActionListener(e -> {
try {
Desktop.getDesktop().open(new File(User.Preferences.getString(KeystrokesLogFile)));
} catch (IOException ex) {
ex.printStackTrace();
}
});
}});
}});
add(new JMenu("About") {{
add(new JMenuItem("Show...") {{
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(sJFrame, "© Pramod Kotipalli, http://remem.p13i.io/");
}
});
}});
}});
}});
add(sSuggestionsPanel = new JPanel() {{
setBounds(GUI.PADDING_LEFT, GUI.PADDING_TOP, GUI.WIDTH - (GUI.PADDING_LEFT + GUI.PADDING_RIGHT), GUI.RA_NUMBER_SUGGESTIONS * GUI.LINE_HEIGHT);
setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Suggestions (from " + User.Preferences.getString(LocalDiskDocumentsFolderPath) + ")"),
BorderFactory.createEmptyBorder(GUI.BORDER_PADDING, GUI.BORDER_PADDING, GUI.BORDER_PADDING, GUI.BORDER_PADDING)));
setFont(GUI.FONT);
}});
add(sKeystrokeBufferLabel = new JLabel() {{
setBounds(GUI.PADDING_LEFT, GUI.PADDING_TOP + GUI.RA_NUMBER_SUGGESTIONS * GUI.LINE_HEIGHT, GUI.WIDTH - (GUI.PADDING_LEFT + GUI.PADDING_RIGHT), GUI.LINE_HEIGHT + GUI.BORDER_PADDING * 2);
setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Keylogger Buffer (writing to " + User.Preferences.getString(KeystrokesLogFile) + ")"),
BorderFactory.createEmptyBorder(GUI.BORDER_PADDING, GUI.BORDER_PADDING, GUI.BORDER_PADDING, GUI.BORDER_PADDING)));
setFont(GUI.FONT);
}});
}});
}};
// jnativehook produces a lot of logs
Logger.getLogger(GlobalScreen.class.getPackage().getName()).setLevel(Level.WARNING);
// init!
initializeRemembranceAgent(true);
// Add the key logger
try {
GlobalScreen.registerNativeHook();
GlobalScreen.addNativeKeyListener(new NativeKeyListener() {
@Override
public void nativeKeyTyped(NativeKeyEvent nativeKeyEvent) {
}
@Override
public void nativeKeyPressed(NativeKeyEvent nativeKeyEvent) {
String keyText = NativeKeyEvent.getKeyText(nativeKeyEvent.getKeyCode());
RemembranceAgentClient.sKeyLoggerBufferLogFileWriter.queue(DateUtils.longTimestamp() + " " + keyText + "\n");
LOGGER.info("Keystroke: " + keyText);
char characterToAdd = keyText.charAt(0);
sBreakingBuffer.addCharacter(characterToAdd);
LOGGER.info(String.format("[Buffer count=%04d:] %s", sBreakingBuffer.getTotalTypedCharactersCount(), sBreakingBuffer.toString()));
sKeystrokeBufferLabel.setText(sBreakingBuffer.toString());
}
@Override
public void nativeKeyReleased(NativeKeyEvent nativeKeyEvent) {
}
});
} catch (NativeHookException e) {
throw new RuntimeException(e);
}
LOGGER.info("Added native key logger.");
sJFrame.setVisible(true);
}
private static void initializeRemembranceAgent(boolean useCache) {
// Clear the timer
if (sRemembranceAgentUpdateTimer != null) {
sRemembranceAgentUpdateTimer.cancel();
sRemembranceAgentUpdateTimer.purge();
sRemembranceAgentUpdateTimer = null;
}
LocalDiskCacheDocumentDatabase localDiskCacheDatabase = new LocalDiskCacheDocumentDatabase("/Users/p13i/Documents/RA/~cache");
if (!useCache) {
localDiskCacheDatabase
.addDocumentsToMemory(new LocalDiskDocumentDatabase(User.Preferences.getString(LocalDiskDocumentsFolderPath)) {{ loadDocuments(); }})
.addDocumentsToMemory(new GoogleDriveFolderDocumentDatabase(User.Preferences.getString(GoogleDriveFolderID)) {{ loadDocuments(); }})
.addDocumentsToMemory(new GmailDocumentDatabase(User.Preferences.getInt(GmailMaxEmailsCount)) {{ loadDocuments(); }})
.saveDocumentsInMemoryToDisk();
}
LOGGER.info("Using " + localDiskCacheDatabase.getName());
sRemembranceAgentEngine = new RemembranceAgentEngine(localDiskCacheDatabase);
LOGGER.info("Initialized Remembrance Agent.");
LOGGER.info("Loading/indexing documents...");
List<Document> documentsIndexed = sRemembranceAgentEngine.indexDocuments();
LOGGER.info(String.format("Indexing complete. Added %d documents:", documentsIndexed.size()));
for (Document document : documentsIndexed) {
LOGGER.info(document.toString());
}
sSuggestionsPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Suggestions (from " + localDiskCacheDatabase.getName() + ")"),
BorderFactory.createEmptyBorder(GUI.BORDER_PADDING, GUI.BORDER_PADDING, GUI.BORDER_PADDING, GUI.BORDER_PADDING)));
sSuggestionsPanel.invalidate();
sSuggestionsPanel.repaint();
// Start the RA task
sRemembranceAgentUpdateTimer = new Timer();
sRemembranceAgentUpdateTimer.schedule(new TimerTask() {
@Override
public void run() {
sendQueryToRemembranceAgent();
}
}, RA_CLIENT_UPDATE_PERIOD_MS, RA_CLIENT_UPDATE_PERIOD_MS);
LOGGER.info("Scheduled Remembrance Agent update task.");
}
private static void sendQueryToRemembranceAgent() {
sJFrame.setTitle("* REMEMBRANCE AGENT *");
String query = sBreakingBuffer.toString();
if (query.equals(sPriorQuery)) {
LOGGER.info("Skipping prior query: '" + sPriorQuery + "'");
} else {
Context context = new Context(null, "p13i", query, DateUtils.now());
LOGGER.info("Sending query to RA: '" + query + "'");
List<ScoredDocument> suggestions = sRemembranceAgentEngine.determineSuggestions(new Query(query, context, GUI.RA_NUMBER_SUGGESTIONS));
sSuggestionsPanel.removeAll();
if (suggestions.isEmpty()) {
LOGGER.info("No suggestions :(");
} else {
LOGGER.info(String.format("Got %d suggestion(s):", suggestions.size()));
int startY = GUI.PADDING_TOP;
for (ScoredDocument doc : suggestions) {
final int yPos = startY;
sSuggestionsPanel.add(new JLabel() {{
setText(Double.toString(doc.getScore()));
setBounds(GUI.SUGGESTION_PADDING_LEFT, yPos, GUI.SCORE_WIDTH, GUI.SUGGESTION_HEIGHT);
setPreferredSize(new Dimension(GUI.SCORE_WIDTH, GUI.SUGGESTION_HEIGHT));
}});
sSuggestionsPanel.add(new JButton() {{
setText(doc.toShortString());
setBounds(GUI.SUGGESTION_PADDING_LEFT + GUI.SCORE_WIDTH, yPos, GUI.SUGGESTION_BUTTON_WIDTH, GUI.SUGGESTION_HEIGHT);
setPreferredSize(new Dimension(GUI.SUGGESTION_BUTTON_WIDTH, GUI.SUGGESTION_HEIGHT));
setHorizontalAlignment(SwingConstants.LEFT);
addActionListener(e -> {
boolean error = false;
try {
Desktop.getDesktop().open(new File(doc.getDocument().getURL()));
} catch (Exception ex) {
ex.printStackTrace();
error = true;
}
if (error) {
try {
Desktop.getDesktop().browse(URIUtils.get(doc.getDocument().getURL()));
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
setEnabled(true);
}});
startY += GUI.SUGGESTION_HEIGHT;
}
}
sSuggestionsPanel.validate();
sSuggestionsPanel.repaint();
sPriorQuery = query;
}
sJFrame.setTitle("REMEMBRANCE AGENT");
}
}