-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.py
827 lines (685 loc) · 31 KB
/
Server.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
import json
import platform
import time
from key import Key
import collections
from collections import OrderedDict
import pymongo
import pymongo.read_preferences
from gridfs import GridFS
import sys
import markdown
import datetime
from libs import bbcodepy
from bs4 import BeautifulSoup
import psycopg2
from psycopg2.extras import RealDictConnection
import ServerSettings
import tornado.escape
import html
import queue
import multiprocessing
from TavernUtils import TavernCache
import logging
import os
import re
from TavernUtils import memorise
import TavernUtils
defaultsettings = ServerSettings.ServerSettings('cache.TavernSettings')
class FakeMongo():
def __init__(self, host, port, name):
# Create a connection to Postgres.
self.conn = psycopg2.connect(
dbname=name,
user=server.serversettings.settings['postgres-user'],
host=host,
port=port,
connection_factory=psycopg2.extras.RealDictConnection)
self.conn.autocommit = True
def find(self, collection,
query={}, limit=-1, skip=0, sortkey=None, sortdirection="ascending"):
cur = self.conn.cursor()
jsonquery = json.dumps(query)
cur.callproc('find', [collection, jsonquery, limit, skip])
results = []
# reconstruct the results
for row in cur.fetchall():
row = row['find']
results.append(row)
# Sort if necessary
if sortdirection not in ['ascending', 'descending']:
raise Exception(
'Sort direction must be either ascending or descending')
if sortkey is not None:
arglist = sortkey.split(".")
results = sorted(results, key=lambda e:
functools.reduce(lambda m, k: m[k], arglist, e))
if sortdirection == "descending":
results.reverse()
return results
def find_one(self, collection,
query={}, limit=-1, skip=0, sortkey=None, sortdirection="ascending"):
result = self.find(
collection=collection,
query=query,
limit=1,
skip=skip,
sortkey=sortkey,
sortdirection=sortdirection)
if len(result) == 0:
return None
else:
return result[0]
def save(self, collection, query):
cur = self.conn.cursor()
jsonquery = json.dumps(query)
result = cur.callproc('save', [collection, jsonquery])
return result
# TODO - Change insert to not insert dups. Probably in mongolike.
def insert(self, collection, query):
cur = self.conn.cursor()
jsonquery = json.dumps(query)
result = cur.callproc('save', [collection, jsonquery])
return result
def count(self, collection, query={}):
cur = self.conn.cursor()
jsonquery = json.dumps(query)
cur.callproc('find', [collection, jsonquery])
results = []
return cur.rowcount
def drop_collection(self, collection):
cur = self.conn.cursor()
cur.callproc('drop_collection', [collection])
def map_reduce(self, collection, map, reduce, out):
cur = self.conn.cursor()
query = {
'mapreduce': collection,
'map': map,
'reduce': reduce,
'out': out
}
jsonquery = json.dumps(query)
cur.callproc('runcommand', [jsonquery])
# Export out results.
self.drop_collection(out)
results = []
rows = cur.fetchone()['runcommand']
for row in rows:
self.insert(out, row)
def ensure_index(self, collection, index):
# TODO - add logic to detect existing idex, and not try to re-create.
cur = self.conn.cursor()
cursor.execute(
'CREATE INDEX idx_' +
collection +
' ON col_' +
collection +
"(find_in_obj('data','" +
index +
"'));")
cursor.execute()
class MongoWrapper():
def __init__(self, host, port, name, safe=True):
if safe:
# Slower, more reliable mongo connection.
self.safeconn = pymongo.MongoClient(
host,
port,
safe=True,
journal=True,
max_pool_size=defaultsettings.settings['mongo-connections'])
self.mongo = self.safeconn[name]
else:
# Create a fast, unsafe mongo connection. Writes might get lost.
self.unsafeconn = pymongo.MongoClient(
host,
port,
read_preference=pymongo.read_preferences.ReadPreference.SECONDARY_PREFERRED,
max_pool_size=defaultsettings.settings['mongo-connections'])
self.mongo = self.unsafeconn[name]
def drop_collection(self, collection):
self.mongo.drop_collection(collection)
def find(self, collection,
query={}, limit=0, skip=0, sortkey=None, sortdirection="ascending"):
if sortdirection not in ['ascending', 'descending']:
raise Exception(
'Sort direction must be either ascending or descending')
if sortkey is not None:
if sortdirection == "ascending":
direction = pymongo.ASCENDING
else:
direction = pymongo.DESCENDING
res = self.mongo[collection].find(
query, skip=skip, limit=limit, sort=[(sortkey, direction)])
else:
res = self.mongo[collection].find(query, skip=skip, limit=limit)
results = []
for row in res:
results.append(row)
return results
def find_one(self, collection,
query={}, skip=0, sortkey=None, sortdirection="ascending"):
if sortdirection not in ['ascending', 'descending']:
raise Exception(
'Sort direction must be either ascending or descending')
if sortkey is not None:
if sortdirection == "ascending":
direction = pymongo.ASCENDING
else:
direction = pymongo.DESCENDING
res = self.mongo[collection].find_one(
query, skip=skip, sort=[(sortkey, direction)])
else:
res = self.mongo[collection].find_one(query, skip=skip)
return res
def save(self, collection, query):
return self.mongo[collection].save(query)
def insert(self, collection, query):
return self.mongo[collection].insert(query)
def map_reduce(self, collection, map, reduce, out):
return (
self.mongo[collection].map_reduce(map=map, reduce=reduce, out=out)
)
def count(self, collection, query={}):
return self.mongo[collection].find(query).count()
def ensure_index(self, collection, index):
return self.mongo[collection].create_index(index)
class DBWrapper():
def __init__(self, name, dbtype=None, host=None, port=None):
if dbtype == "mongo":
if host is None:
host = server.serversettings.settings['mongo-hostname']
if port is None:
port = server.serversettings.settings['mongo-port']
self.safe = MongoWrapper(
safe=True,
host=host,
port=port,
name=name)
self.unsafe = MongoWrapper(
safe=False,
host=host,
port=port,
name=name)
elif dbtype == "postgres":
if host is None:
host = server.serversettings.settings['postgres-hostname']
if port is None:
port = server.serversettings.settings['postgres-port']
self.safe = FakeMongo(host=host, port=port, name=name)
self.unsafe = FakeMongo(host=host, port=port, name=name)
else:
raise Exception('DBError', 'Invalid type of database')
def print_timing(func):
def wrapper(*arg):
t1 = time.time()
res = func(*arg)
t2 = time.time()
print((t2 - t1) * 1000.0)
return res
return wrapper
class Server(TavernUtils.instancer):
# We want to share state across all Server instances.
# This way we can create a new instance, anywhere, in any subclass, and
# get the same settings/etc
class FancyDateTimeDelta(object):
"""Format the date / time difference between the supplied date and the
current time using approximate measurement boundaries."""
def __init__(self, dt):
now = datetime.datetime.now()
delta = now - dt
self.year = round(delta.days / 365)
self.month = round(delta.days / 30 - (12 * self.year))
if self.year > 0:
self.day = 0
else:
self.day = delta.days % 30
self.hour = round(delta.seconds / 3600)
self.minute = round(delta.seconds / 60 - (60 * self.hour))
self.second = delta.seconds - (self.hour * 3600) - \
(60 * self.minute)
self.millisecond = delta.microseconds / 1000
def format(self):
# Round down. People don't want the exact time.
# For exact time, reverse array.
fmt = ""
for period in ['millisecond', 'second', 'minute', 'hour', 'day', 'month', 'year']:
value = getattr(self, period)
if value:
if value > 1:
period += "s"
fmt = str(value) + " " + period
return fmt + " ago"
def getnextint(self, queuename, forcewrite=False):
if not 'queues' in self.serversettings.settings:
self.serversettings.settings['queues'] = {}
if not queuename in self.serversettings.settings['queues']:
self.serversettings.settings['queues'][queuename] = 0
self.serversettings.settings['queues'][queuename] += 1
if forcewrite:
self.serversettings.saveconfig()
return self.serversettings.settings['queues'][queuename]
def __init__(self, settingsfile=None, workingdir=None, slot='default'):
super().__init__(slot)
# Don't run this more than once.
if self.__dict__.get('set') is True:
return
else:
self.set = True
self.logger = logging.getLogger('Tavern')
# Should the server run in debug mode
self.debug = False
self.serversettings = ServerSettings.ServerSettings(settingsfile)
if self.serversettings.updateconfig():
self.serversettings.saveconfig()
if not 'pubkey' in self.serversettings.settings or not 'privkey' in self.serversettings.settings:
# Generate New config
self.logger.info("Generating new Config")
self.ServerKeys = Key()
self.ServerKeys.generate()
# We can't encrypt this.. We'd need to store the key here on the machine...
# If you're worried about it, encrypt the disk, or use a loopback.
self.serversettings.settings['pubkey'] = self.ServerKeys.pubkey
self.serversettings.settings['privkey'] = self.ServerKeys.privkey
self.serversettings.updateconfig()
self.serversettings.saveconfig()
self.mc = OrderedDict
self.unusedkeycache = multiprocessing.Queue(
self.serversettings.settings['KeyGenerator']['num_pregens'])
# Break out the settings into it's own file, so we can include it without including all of server
# This does cause a few shenanigans while loading here, but hopefully
# it's minimal
self.ServerKeys = Key(pub=self.serversettings.settings['pubkey'],
priv=self.serversettings.settings['privkey'])
self.db = DBWrapper(
name=self.serversettings.settings['dbname'],
dbtype=self.serversettings.settings['dbtype'],
host=self.serversettings.settings['mongo-hostname'],
port=self.serversettings.settings['mongo-port'])
self.sessions = DBWrapper(
name=self.serversettings.settings['sessions-db-name'],
dbtype=self.serversettings.settings['dbtype'],
host=self.serversettings.settings['sessions-db-hostname'],
port=self.serversettings.settings['sessions-db-port'])
self.binaries = DBWrapper(
name=self.serversettings.settings['bin-mongo-db'],
dbtype='mongo',
host=self.serversettings.settings['bin-mongo-hostname'],
port=self.serversettings.settings['bin-mongo-port'])
self.bin_GridFS = GridFS(self.binaries.unsafe.mongo)
# Ensure we have Proper indexes.
self.db.safe.ensure_index('envelope', 'envelope.local.time_added')
self.db.safe.ensure_index('envelope', 'envelope.local.sorttopic')
self.db.safe.ensure_index('envelope', 'envelope.local.payload_sha512')
self.db.safe.ensure_index('envelope', 'envelope.payload.class')
self.db.safe.ensure_index('envelope', 'envelope.payload.regarding')
self.db.safe.ensure_index(
'envelope',
'envelope.payload.binaries.sha_512')
self.db.safe.ensure_index('envelope', 'envelope.local.payload_sha512')
self.db.safe.ensure_index('envelope', 'envelope.payload.author.pubkey')
self.db.safe.ensure_index('envelope', 'envelope.payload.author.pubkey')
self.db.safe.ensure_index('envelope', 'usertrusts.asking')
self.db.safe.ensure_index('envelope', 'incomingtrust')
self.binaries.safe.ensure_index('fs.files', 'filename')
self.binaries.safe.ensure_index('fs.files', 'uploadDate')
self.binaries.safe.ensure_index('fs.files', '_id')
self.binaries.safe.ensure_index('fs.files', 'uploadDate')
# Get a list of all the valid templates that can be used, to compare
# against later on.
self.availablethemes = []
for name in os.listdir('themes'):
if os.path.isdir(os.path.join('themes', name)):
if name[:1] != ".":
self.availablethemes.append(name)
self.serversettings.settings['static-revision'] = int(time.time())
self.fortune = TavernUtils.randomWords(fortunefile="data/fortunes")
self.wordlist = TavernUtils.randomWords(fortunefile="data/wordlist")
# Define out logging options.
formatter = logging.Formatter('[%(levelname)s] %(message)s')
self.consolehandler = logging.StreamHandler()
self.consolehandler.setFormatter(formatter)
self.handler_file = logging.FileHandler(
filename=self.serversettings.settings['logfile'])
self.handler_file.setFormatter(formatter)
self.logger.setLevel(self.serversettings.settings['loglevel'])
level = self.serversettings.settings['loglevel']
# Cache our JS, so we can include it later.
file = open("static/scripts/instance.min.js")
self.logger.info("Cached JS")
TavernCache.cache['instance.js'] = file.read()
file.close()
self.guestacct = None
def start(self):
"""Stuff that should be done when the server is running as a process,
not just imported as a obj."""
self.external = embedis.embedis()
from uasparser import UASparser
self.logger.info("Loading Browser info")
self.browserdetector = UASparser()
# Start actually logging to file or console
if self.debug:
self.logger.setLevel("DEBUG")
self.logger.addHandler(self.consolehandler)
# logging.getLogger('gnupg').setLevel("DEBUG")
logging.getLogger('gnupg').addHandler(self.consolehandler)
self.logger.addHandler(self.handler_file)
print("Logging Server started at level: " +
str(self.logger.getEffectiveLevel()))
# Pregenerate some users in the background.
self.keygenerator = KeyGenerator.KeyGenerator()
self.keygenerator.start()
if not 'guestacct' in self.serversettings.settings:
self.logger.info("Generating a Guest user acct.")
self.guestacct = User()
self.guestacct.generate(AllowGuestKey=False)
self.serversettings.settings['guestacct'] = {}
self.serversettings.settings[
'guestacct'][
'pubkey'] = self.guestacct.Keys[
'master'].pubkey
self.serversettings.saveconfig()
self.guestacct.savemongo()
else:
self.logger.info("Loading the Guest user acct.")
self.guestacct = User()
self.guestacct.load_mongo_by_pubkey(
self.serversettings.settings['guestacct']['pubkey'])
def stop(self):
"""Stop all server procs."""
self.logger.info("Shutting down Server instance")
self.keygenerator.stop()
def prettytext(self):
newstr = json.dumps(
self.serversettings.settings, indent=2, separators=(', ', ': '))
return newstr
@memorise(ttl=defaultsettings.settings['cache']['sorttopic']['seconds'], maxsize=defaultsettings.settings['cache']['sorttopic']['size'])
def sorttopic(self, topic):
if topic is not None:
topic = topic.lower()
topic = self.urlize(topic)
else:
topic = None
return topic
@memorise(ttl=defaultsettings.settings['cache']['error_envelope']['seconds'], maxsize=defaultsettings.settings['cache']['error_envelope']['size'])
def error_envelope(self, subject="Error", topic="sitecontent", body=None):
if body is None:
body = """
Oh my, something seems to have happened that we weren't expecting.
Hopefully this will get cleared up relatively quickly.
If not, you might want to send a note to support@tavern.is, with the URL, and notes on how you got here :/
So sorry for the trouble.
-The Barkeep
"""
e = Envelope()
e.dict['envelope']['payload'] = OrderedDict()
e.dict['envelope']['payload']['subject'] = subject
e.dict['envelope']['payload']['topic'] = topic
e.dict['envelope']['payload']['formatting'] = "markdown"
e.dict['envelope']['payload']['class'] = "message"
e.dict['envelope']['payload'][
'body'] = body
e.dict['envelope']['payload']['author'] = OrderedDict()
e.dict['envelope']['payload']['author']['pubkey'] = "1234"
e.dict['envelope']['payload']['author']['friendlyname'] = "ERROR!"
e.dict['envelope']['payload']['author']['useragent'] = "Error Agent"
e.dict['envelope']['payload']['author']['friendlyname'] = "Error"
e.addStamp(
stampclass='author',
keys=self.ServerKeys,
friendlyname=defaultsettings.settings['hostname'])
e.flatten()
e.munge()
e.dict['envelope']['local']['time_added'] = 1297396876
e.dict['envelope']['local'][
'author_wordhash'] = "Automatically generated message"
e.dict['envelope']['local']['sorttopic'] = "error"
e.dict['envelope']['local']['payload_sha512'] = e.payload.hash()
return e
# Cache to failfast on receiving dups
@memorise(ttl=defaultsettings.settings['cache']['receiveEnvelope']['seconds'], maxsize=defaultsettings.settings['cache']['receiveEnvelope']['size'])
def receiveEnvelope(self, envstr=None, env=None):
"""Receive an envelope for processing in the server.
Can take either a string, or an envelope obj.
"""
if envstr is None and env is None:
raise Exception(
'receiveEnvelope MUST receive an envelope. Really! ;)')
if env is not None:
# If we get an envelope, flatten it - The caller may not have.
c = env.flatten()
else:
c = Envelope()
c.loadstring(importstring=envstr)
# Fill-out the message's local fields.
c.munge()
# Make sure the message is valid, meets our standards, and is good to
# accept and save.
if not c.validate():
self.logger.info(
"Received an Envelope which does not validate- " + c.payload.hash())
self.logger.debug(c.text())
return False
utctime = time.time()
existing = self.db.unsafe.find_one(
'envelopes', {'envelope.local.payload_sha512': c.dict['envelope']['local']['payload_sha512']})
if existing is not None:
self.logger.debug("We already have that msg.")
return c.dict['envelope']['local']['payload_sha512']
# Sign the message to saw we saw it.
if self.serversettings.settings['mark-seen']:
c.addStamp(
stampclass='server',
keys=self.ServerKeys,
hostname=defaultsettings.settings['hostname'])
# Store the time, in full UTC (with precision). This is used to skip
# pages in the viewer later.
c.dict['envelope']['local']['time_added'] = utctime
if c.dict['envelope']['payload']['class'] == "message":
# If the message referenes anyone, mark the original, for ease of finding it later.
# Do this in the 'local' block, so we don't waste bits passing this on to others.
# Partners can calculate this when they receive it.
if 'regarding' in c.dict['envelope']['payload']:
repliedTo = Envelope()
if repliedTo.loadmongo(mongo_id=c.dict['envelope']['payload']['regarding']):
self.logger.debug(
" I am :: " + c.dict['envelope']['local']['payload_sha512'])
self.logger.debug(
" Adding a cite on my parent :: " +
repliedTo.dict[
'envelope'][
'local'][
'payload_sha512'])
repliedTo.addcite(
c.dict[
'envelope'][
'local'][
'payload_sha512'])
c.addAncestor(c.dict['envelope']['payload']['regarding'])
print("id is :" + c.dict['envelope']['local']['payload_sha512'])
# It could also be that this message is cited BY others we already have!
# Sometimes we received them out of order. Better check.
for citedict in self.db.unsafe.find('envelopes', {'envelope.payload.regarding': c.dict['envelope']['local']['payload_sha512']}):
self.logger.debug('found existing cite, bad order. ')
self.logger.debug(
" I am :: " + c.dict['envelope']['local']['payload_sha512'])
self.logger.debug(" Found pre-existing cite at :: " +
citedict['envelope']['local']['payload_sha512'])
# If it's a message, write that in the reply, and in me.
if citedict['envelope']['payload']['class'] == 'message':
citedme = Envelope()
citedme.loaddict(citedict)
c.addcite(
citedme.dict[
'envelope'][
'local'][
'payload_sha512'])
citedme.addAncestor(
c.dict[
'envelope'][
'local'][
'payload_sha512'])
citedme.saveMongo()
# If it's an edit, write that in me.
elif citedict['envelope']['payload']['class'] == 'messagerevision':
c.addEdit(citedict['envelope']['local']['payload_sha512'])
elif citedict['envelope']['payload']['class'] == 'messagerating':
citedme = Envelope()
citedme.loaddict(citedict)
citedme.dict[
'envelope'][
'local'][
'regardingAuthor'] = c.dict[
'envelope'][
'payload'][
'author']
citedme.saveMongo()
elif c.dict['envelope']['payload']['class'] == "messagerating":
# If this is a rating, cache the AUTHOR of the rated message.
regardingPost = self.db.unsafe.find_one(
'envelopes',
{'envelope.local.payload_sha512': c.dict['envelope']['payload']['regarding']})
if regardingPost is not None:
c.dict[
'envelope'][
'local'][
'regardingAuthor'] = regardingPost[
'envelope'][
'payload'][
'author']
elif c.dict['envelope']['payload']['class'] == "messagerevision":
# This is an edit to an existing message.
regardingPost = self.db.unsafe.find_one(
'envelopes',
{'envelope.local.payload_sha512': c.dict['envelope']['payload']['regarding']})
if regardingPost is not None:
if 'priority' in c.dict['envelope']['payload']:
c.dict[
'envelope'][
'local'][
'priority'] = c.dict[
'envelope'][
'payload'][
'priority']
else:
c.dict['envelope']['local']['priority'] = 0
# Store this edit.
# Save this message out to mongo, so we can then retrieve it in
# addEdit().
c.saveMongo()
# Modify the original message.
r = Envelope()
r.loaddict(regardingPost)
r.addEdit(c.dict['envelope']['local']['payload_sha512'])
# Ensure we have the freshest version in memory.
c.reloadmongo()
else:
self.logger.debug("Received an edit without the original")
# Store our Envelope
c.saveMongo()
return c.dict['envelope']['local']['payload_sha512']
@memorise(ttl=defaultsettings.settings['cache']['formatText']['seconds'], maxsize=defaultsettings.settings['cache']['formatText']['size'])
def formatText(self, text=None, formatting='markdown'):
# Run the text through Tornado's escape to ensure you're not a badguy.
text = tornado.escape.xhtml_escape(text)
if formatting == 'bbcode':
formatted = bbcodepy.Parser().to_html(text)
elif formatting == "plaintext":
formatted = "<pre>" + text + "</pre>"
else:
# If we don't know, you get Markdown
formatted = markdown.markdown(
text,
output_format="html5",
safe_mode='escape',
enable_attributes=False,
extensions=['nl2br',
'footnotes',
'tables',
'headerid(level=3)'])
# We can't use tornado.escape.linkify to linkify, since it ALSO
# escapes everything.
formatted = self.autolink(formatted)
return formatted
@memorise(ttl=defaultsettings.settings['cache']['getUsersPosts']['seconds'], maxsize=defaultsettings.settings['cache']['getUsersPosts']['size'])
def getUsersPosts(self, pubkey, limit=1000):
envelopes = []
for envelope in self.db.safe.find('envelopes', {'envelope.local.author.pubkey': pubkey, 'envelope.payload.class': 'message'}, limit=limit, sortkey='envelope.local.time_added', sortdirection='descending'):
messagetext = json.dumps(envelope, separators=(',', ':'))
e = Envelope()
e.loadstring(messagetext)
envelopes.append(e)
return envelopes
def getOriginalMessage(self, messageid):
env = Envelope()
# First, pull the referenced message.
if env.loadmongo(mongo_id=messageid):
if env.dict['envelope']['payload']['class'] == 'message':
return env.dict['envelope']['local']['payload_sha512']
else:
return env.dict['envelope']['payload']['regarding']
return None
def getTopMessage(self, messageid):
# Find the top level of a post that we currently have.
env = Envelope()
# First, pull the referenced message.
if env.loadmongo(mongo_id=messageid):
# If we have no references, congrats, we're the top.
if not 'regarding' in env.dict['envelope']['payload']:
return env.dict['envelope']['local']['payload_sha512']
if env.dict['envelope']['payload']['regarding'] is None:
return env.dict['envelope']['local']['payload_sha512']
# If we're here, it means we have a parent.
# Recurse upstream
result = self.getTopMessage(
env.dict['envelope']['payload']['regarding'])
# Don't blindly return it, since it might be None in broken chains.
# In that case, return yourself.
if result is not None:
return result
else:
return env.dict['envelope']['local']['payload_sha512']
else:
return None
def urlize(self, url):
# I do NOT want to urlencode this, because that encodes the unicode characters.
# Browsers are perfectly capable of handling these!
url = re.sub('[/? ]', '-', url)
url = re.sub(r'[^\w-]', '', url)
return url
# Autolink from http://greaterdebater.com/blog/gabe/post/4
def autolink(self, html):
# match all the urls
# this returns a tuple with two groups
# if the url is part of an existing link, the second element
# in the tuple will be "> or </a>
# if not, the second element will be an empty string
urlre = re.compile(
"(\(?https?://[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|])(\">|</a>)?")
urls = urlre.findall(html)
clean_urls = []
# remove the duplicate matches
# and replace urls with a link
for url in urls:
# ignore urls that are part of a link already
if url[1]:
continue
c_url = url[0]
# ignore parens if they enclose the entire url
if c_url[0] == '(' and c_url[-1] == ')':
c_url = c_url[1:-1]
if c_url in clean_urls:
continue # We've already linked this url
clean_urls.append(c_url)
# substitute only where the url is not already part of a
# link element.
html = re.sub("(?<!(=\"|\">))" + re.escape(c_url),
"<a rel=\"noreferrer nofollow\" href=\"" +
c_url + "\">" + c_url + "</a>",
html)
return html
server = Server()
from User import User
from Envelope import Envelope
import embedis
import KeyGenerator