forked from selfspy/selfspy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activity_store.py
215 lines (171 loc) · 7.73 KB
/
activity_store.py
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
import time
from datetime import datetime
NOW = datetime.now
import sqlalchemy
import platform
if platform.system() == 'Darwin':
import sniff_cocoa
else:
import sniff_x
import models
from models import Process, Window, Geometry, Click, Keys
SKIP_MODIFIERS = {"", "Shift_L", "Control_L", "Super_L", "Alt_L", "Super_R", "Control_R", "Shift_R", "[65027]"} # [65027] is AltGr in X for some ungodly reason.
class Display:
def __init__(self):
self.proc_id = None
self.win_id = None
self.geo_id = None
class KeyPress:
def __init__(self, key, time, is_repeat):
self.key = key
self.time = time
self.is_repeat = is_repeat
class ActivityStore:
def __init__(self, db_name, encrypter=None, store_text=True):
self.session_maker = models.initialize(db_name)
models.ENCRYPTER = encrypter
self.store_text = store_text
self.curtext = u""
self.key_presses = []
self.mouse_path = []
self.current_window = Display()
self.last_key_time = time.time()
self.started = NOW()
def trycommit(self):
for _ in xrange(1000):
try:
self.session.commit()
break
except sqlalchemy.exc.OperationalError:
time.sleep(1)
def run(self):
self.session = self.session_maker()
if platform.system() == 'Darwin':
self.sniffer = sniff_cocoa.SniffCocoa()
else:
self.sniffer = sniff_x.SniffX()
self.sniffer.screen_hook = self.got_screen_change
self.sniffer.key_hook = self.got_key
self.sniffer.mouse_button_hook = self.got_mouse_click
self.sniffer.mouse_move_hook = self.got_mouse_move
self.sniffer.run()
def got_screen_change(self, process_name, window_name, win_x, win_y, win_width, win_height):
""" Receives a screen change and stores any changes. If the process or window has
changed it will also store any queued pressed keys.
process_name is the name of the process running the current window
window_name is the name of the window
win_x is the x position of the window
win_y is the y position of the window
win_width is the width of the window
win_height is the height of the window """
cur_process = self.session.query(Process).filter_by(name=process_name).scalar()
if not cur_process:
cur_process = Process(process_name)
self.session.add(cur_process)
cur_geometry = self.session.query(Geometry).filter_by(xpos=win_x,
ypos=win_y,
width=win_width,
height=win_height).scalar()
if not cur_geometry:
cur_geometry = Geometry(win_x, win_y, win_width, win_height)
self.session.add(cur_geometry)
cur_window = self.session.query(Window).filter_by(title=window_name,
process_id=cur_process.id).scalar()
if not cur_window:
cur_window = Window(window_name, cur_process.id)
self.session.add(cur_window)
if not (self.current_window.proc_id == cur_process.id
and self.current_window.win_id == cur_window.id):
self.trycommit()
self.store_keys() # happens before as these keypresses belong to the previous window
self.current_window.proc_id = cur_process.id
self.current_window.win_id = cur_window.id
self.current_window.geo_id = cur_geometry.id
def filter_many(self):
specials_in_row = 0
lastpress = None
newpresses = []
for press in self.key_presses:
key = press.key
if specials_in_row and key != lastpress.key:
if specials_in_row > 1:
lastpress.key = '%s]x%d>' % (lastpress.key[:-2], specials_in_row)
newpresses.append(lastpress)
specials_in_row = 0
if len(key) > 1:
specials_in_row += 1
lastpress = press
else:
newpresses.append(press)
if specials_in_row:
if specials_in_row > 1:
lastpress.key = '%s]x%d>' % (lastpress.key[:-2], specials_in_row)
newpresses.append(lastpress)
self.key_presses = newpresses
def store_keys(self):
""" Stores the current queued key-presses """
self.filter_many()
if self.key_presses:
keys = [press.key for press in self.key_presses]
timings = [press.time for press in self.key_presses]
add = lambda count, press: count + (1 if press.is_repeat else 0)
nrkeys = reduce(add, self.key_presses, 0)
curtext = u""
if not self.store_text:
keys = []
else:
curtext = ''.join(keys)
self.session.add(Keys(curtext.encode('utf8'),
keys,
timings,
nrkeys,
self.started,
self.current_window.proc_id,
self.current_window.win_id,
self.current_window.geo_id))
self.trycommit()
self.started = NOW()
self.key_presses = []
self.last_key_time = time.time()
def got_key(self, keycode, state, string, is_repeat):
""" Receives key-presses and queues them for storage.
keycode is the code sent by the keyboard to represent the pressed key
state is the list of modifier keys pressed, each modifier key should be represented
with capital letters and optionally followed by an underscore and location
specifier, i.e: SHIFT or SHIFT_L/SHIFT_R, ALT, CTRL
string is the string representation of the key press
repeat is True if the current key is a repeat sent by the keyboard """
now = time.time()
if string in SKIP_MODIFIERS:
return
if len(state) > 1 or (len(state) == 1 and state[0] != "Shift"):
string = '<[%s: %s]>' % (' '.join(state), string)
elif len(string) > 1:
string = '<[%s]>' % string
self.key_presses.append(KeyPress(string, now - self.last_key_time, is_repeat))
self.last_key_time = now
def store_click(self, button, x, y):
""" Stores incoming mouse-clicks """
self.session.add(Click(button,
True,
x, y,
len(self.mouse_path),
self.current_window.proc_id,
self.current_window.win_id,
self.current_window.geo_id))
self.mouse_path = []
self.trycommit()
def got_mouse_click(self, button, x, y):
""" Receives mouse clicks and sends them for storage.
Mouse buttons: left: 1, middle: 2, right: 3, scroll up: 4, down:5, left:6, right:7
x,y are the coordinates of the keypress
press is True if it pressed down, False if released"""
self.store_click(button, x, y)
def got_mouse_move(self, x, y):
""" Queues mouse movements.
x,y are the new coorinates on moving the mouse"""
self.mouse_path.append([x, y])
def close(self):
""" stops the sniffer and stores the latest keys. To be used on shutdown of program"""
self.sniffer.cancel()
self.store_keys()