-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathotr.py
510 lines (356 loc) · 10.9 KB
/
otr.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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
__module_name__ = 'otr'
__module_author__ = 'SpiralP'
__module_version__ = '1'
__module_description__ = 'OTR for HexChat'
import sys, os
import urllib2
import hashlib
import time
import hexchat
import potr
GIT_URL='https://github.com/SpiralP/HexChat-otr'
REMOTE_FILE='https://raw.githubusercontent.com/SpiralP/HexChat-otr/master/'+__module_name__+'.py'
STATUS_URL='https://api.github.com/repos/SpiralP/HexChat-otr/git/refs'
HEXCHAT_CONFIG_DIR=hexchat.get_info('configdir')
HEXCHAT_ADDONS_DIR=os.path.join(HEXCHAT_CONFIG_DIR,'addons')
OTR_DIR=os.path.join(HEXCHAT_CONFIG_DIR, 'otr')
CLEAR='\017'
BOLD='\002'
UNDERLINE='\037'
COLOR='\003'
RED=COLOR+'04'
GREEN=COLOR+'03'
BLUE=COLOR+'02'
DEFAULT_POLICY_FLAGS = {
'ALLOW_V1':False,
'ALLOW_V2':True,
'REQUIRE_ENCRYPTION':True,
}
# 'SEND_TAG':True, # basically advertise your version
PROTOCOL='irc'
MMS=400
accounts = {}
class PythonVersion2(object):
def __init__(self):
return
def to_unicode(self, str): # utf8 -> unicode
if isinstance(str, unicode):
return str
else:
return str.decode('utf-8', 'replace')
def to_str(self, str): # unicode -> utf8
return str.encode('utf-8', 'replace')
class PythonVersion3(object):
def __init__(self, minor):
self.minor = minor
def to_unicode(self, str): # utf8 -> unicode
if isinstance(str, bytes):
return str.decode('utf-8', 'replace')
else:
return str
def to_str(self, str): # unicode -> utf8
return str
if sys.version_info.major >= 3:
PYVER = PythonVersion3(sys.version_info.minor)
else:
PYVER = PythonVersion2()
def success(msg):
hexchat.prnt(BOLD + GREEN + msg)
def info(msg):
hexchat.prnt(BOLD + BLUE + msg)
def warn(msg):
hexchat.prnt(BOLD + RED + msg)
def first_instance(objs, Class):
for obj in objs:
if isinstance(obj, Class):
return obj
def message(who,msg):
hexchat.prnt('[OTR] {}\t{}'.format(who,msg))
def getAccount(id=None):
name = False
if id is None:
name = hexchat.get_info('nick')
if name is None:
return False
network = hexchat.get_info('network')
if network is None:
return False
id = name+'@'+network
if id not in accounts:
hexchat.prnt(BLUE+'Creating new account for: %s' % id)
accounts[id] = MyAccount(id,name)
return accounts[id]
def getChannel():
return hexchat.get_info('channel')
def say(who,msg):
for line in msg.split('\n'):
hexchat.command('PRIVMSG %s :%s' % (who,line))
return
updateChecked=False
def updateCheck():
global updateChecked
if not updateChecked:
updateChecked=True
try:
http = urllib2.urlopen(REMOTE_FILE+'?'+str(time.time()))
except:
warn('Error retrieving update data!')
return False
remotedata = http.read()
http.close()
localpath = os.path.join(HEXCHAT_ADDONS_DIR,__module_name__+'.py')
try:
with open(localpath, 'rb') as file:
localdata = file.read()
except:
warn('Error reading local file!')
return False
remotehash = hashlib.sha1(remotedata).hexdigest()
localhash = hashlib.sha1(localdata).hexdigest()
updateAvailable = (remotehash!=localhash)
if updateAvailable:
success('New Update Available! Get it from: %s' % (UNDERLINE+BLUE+GIT_URL))
else:
info('Your version of %s is up to date!' % __module_name__)
return True
return
class MyContext(potr.context.Context):
def __init__(self, account, peer):
super(MyContext, self).__init__(account, peer)
self.in_smp = False
self.smp_question = False
def getPolicy(self, key):
if key in DEFAULT_POLICY_FLAGS:
return DEFAULT_POLICY_FLAGS[key]
else:
return False
def inject(self, msg, appdata=None):
say(self.peer,msg)
def setState(self, newstate):
oldstate = self.state
desc = 'WOT'
if newstate==potr.context.STATE_PLAINTEXT:
desc = 'Plain Text'
elif newstate==potr.context.STATE_ENCRYPTED:
desc = 'Encrypted'
elif newstate==potr.context.STATE_FINISHED:
desc = 'Finished!'
success('NEW STATE: ' + BLUE + desc)
# started with encryption
if not(oldstate==potr.context.STATE_ENCRYPTED) and (newstate==potr.context.STATE_ENCRYPTED):
trust = self.getCurrentTrust()
if trust is None:
info('Fingerprint: %s' % str(self.getCurrentKey()))
if bool(trust):
success('AUTHENTICATED')
else:
warn('UNAUTHENTICATED!!!')
if oldstate!=potr.context.STATE_PLAINTEXT and newstate==potr.context.STATE_PLAINTEXT:
info('conversation ended!')
super(MyContext, self).setState(newstate)
def smpFinish(self):
self.in_smp = False
self.smp_question = False
self.user.saveTrusts()
def handleTLV(self, data):
smp1q = first_instance(data, potr.proto.SMP1QTLV)
smp3 = first_instance(data, potr.proto.SMP3TLV)
smp4 = first_instance(data, potr.proto.SMP4TLV)
if first_instance(data, potr.proto.SMPABORTTLV):
warn('SMP aborted by peer ( might mean they guessed a wrong password )')
elif self.in_smp and not self.smpIsValid():
warn('SMP aborted?')
elif first_instance(data, potr.proto.SMP1TLV):
self.in_smp = True
info('Peer requested SMP verification.\nUse /otr smp respond <secret>')
elif smp1q:
self.in_smp = True
self.smp_question = True
info('Peer requested SMP verification: "%s"\nUse /otr smp respond <secret>' % PYVER.to_unicode(smp1q.msg))
elif first_instance(data, potr.proto.SMP2TLV):
if not self.in_smp:
self.smpFinish()
else:
info('SMP processing...')
elif smp3 or smp4:
if smp3:
info('smp3')
if smp4:
info('smp4')
if self.smpIsSuccess():
if self.smp_question:
self.smpFinish()
success('SMP verification succeeded!')
if not bool(self.getCurrentTrust()):
warn('You do not yet trust your peer, try asking your own question: /otr smp ask [question] <secret>')
else:
self.smpFinish()
success('SMP verification succeeded!')
else:
self.smpFinish()
warn('SMP verification FAILED!')
class MyAccount(potr.context.Account):
#important lol
contextclass = MyContext
def __init__(self, id, myname=False):
global PROTOCOL, MMS
super(MyAccount, self).__init__(id, PROTOCOL, MMS)
if not myname:
myname = id
self.myname = myname
if not os.path.exists(OTR_DIR):
info('Creating new directory: %s' % OTR_DIR)
os.makedirs(OTR_DIR)
self.keyFilePath=os.path.join(OTR_DIR, '{}.key3'.format(id))
self.trustsFilePath=os.path.join(OTR_DIR, '{}.trusts'.format(id))
self.loadTrusts()
def loadPrivkey(self):
info('Loading Private Key')
if not os.path.exists(self.keyFilePath):
return None
try:
with open(self.keyFilePath, 'rb') as file:
return potr.crypt.PK.parsePrivateKey(file.read())[0]
except IOError, e:
pass
return None
def savePrivkey(self):
info('Saving Private Key')
if not os.path.exists(self.keyFilePath):
info('Creating new file: %s' % self.keyFilePath)
with open(self.keyFilePath, 'wb') as file:
file.write(self.getPrivkey().serializePrivateKey())
return
def loadTrusts(self):
info('Loading Trusts')
if not os.path.exists(self.trustsFilePath):
return
with open(self.trustsFilePath, 'rb') as file:
for line in file:
context, account, fingerprint, trust = PYVER.to_unicode(line[:-1]).split('\t')
if account == self.name:
info('trusting %s %s' % (fingerprint,trust))
self.setTrust(context, fingerprint, trust)
return
def saveTrusts(self):
info('Saving Trusts')
with open(self.trustsFilePath,'wb') as file:
for uid, trusts in self.trusts.items():
for fingerprint, trust in trusts.items():
info('writing %s %s' % (fingerprint,trust))
file.write(
PYVER.to_str(
'\t'.join((uid, self.name, fingerprint, trust))
)
)
file.write('\n')
return
def message_callback(word, word_eol, userdata):
who = word[0]
msg = word[1]
account = getAccount()
if not account:
return hexchat.EAT_NONE
chan = getChannel()
if chan not in account.ctxs:
return hexchat.EAT_NONE
context = account.getContext(chan) # or who? for channels?
try: # TODO handle more things
msg,data = context.receiveMessage(msg)
context.handleTLV(data)
except potr.context.UnencryptedMessage:
return hexchat.EAT_NONE
if msg is None:
return hexchat.EAT_ALL
message(BLUE+chan,msg)
return hexchat.EAT_ALL
def keypress(word, word_eol, userdata):
key = word[0]
# alt = word[1]
# letter = word[2]
if not(key=='65293'): # return key
return
msg = hexchat.get_info('inputbox')
if len(msg)==0 or msg[0]=='/':
return hexchat.EAT_NONE
account = getAccount()
if not account:
return hexchat.EAT_NONE
chan = getChannel()
if chan not in account.ctxs:
return hexchat.EAT_NONE
context = account.getContext(chan)
if context.state==potr.context.STATE_ENCRYPTED:
message(account.myname,msg)
context.sendMessage(0,msg)
hexchat.command('settext') # the hacks
return hexchat.EAT_ALL
return hexchat.EAT_NONE
def command_callback(word, word_eol, userdata):
updateCheck()
account = getAccount()
if not account:
return hexchat.EAT_NONE
chan = getChannel()
if chan not in account.ctxs:
hexchat.prnt(BLUE+'Adding context for: %s' % chan)
context = account.getContext(chan)
cmd = len(word)>1 and word[1] or ''
if cmd=='go' or cmd=='start' or cmd=='init':
say(context.peer,context.sendMessage(0,'?OTR?')) # this will actually send the correct version-query
info('Query Sent...')
elif cmd=='stop' or cmd=='finish':
context.disconnect()
elif cmd=='trust':
context.setCurrentTrust('verified')
elif cmd=='smp':
if len(word)==2:
return hexchat.EAT_ALL
action = word[2]
if action=='respond': # /otr smp respond secret
secret = word[3]
if secret:
secret = PYVER.to_str(secret)
context.smpGotSecret(secret)
elif action=='ask':
question = None
secret = None
if len(word)==4: # /otr smp ask secret
secret = word[3]
elif len(word)==5: # /otr smp ask question secret
question = word[3]
secret = word[4]
else:
return hexchat.EAT_ALL
if secret:
secret = PYVER.to_str(secret)
if question:
question = PYVER.to_str(question)
try:
context.smpInit(secret, question)
except potr.context.NotEncryptedError:
warn('No session with %s' % context.peer)
return hexchat.EAT_ALL
else: # except-else wtf python
if question:
info('SMP question sending...')
else:
info('SMP challenge sending...')
elif action=='abort':
try:
context.smpAbort()
except potr.context.NotEncryptedError:
warn('No session with %s' % context.peer)
else:
info('SMP aborted')
elif cmd=='send':
context.sendMessage(0,word[3])
return hexchat.EAT_ALL
hexchat.hook_print('Private Message to Dialog',message_callback)
hexchat.hook_print('Key Press',keypress)
hexchat.hook_command('otr',command_callback)
def unload_callback(userdata):
info('Unloading {}'.format(__module_name__))
hexchat.hook_unload(unload_callback)
print('%s version %s loaded.' % (__module_name__,__module_version__))
getAccount()