-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainFile.py
More file actions
760 lines (602 loc) · 32.3 KB
/
MainFile.py
File metadata and controls
760 lines (602 loc) · 32.3 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
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
#!/usr/bin/python
""" MainFile.py
COMPSYS302 - Software Design - Python Project
Author: Savi Mohan (smoh944@auckland.ac.nz)
Last Edited: 11/06/2017
This program uses the CherryPy web server (from www.cherrypy.org).
"""
# Requires: CherryPy 3.2.2 (www.cherrypy.org)
# Python (We use 2.7)
# The address we listen for connections on
listen_ip = "0.0.0.0"
listen_port = 10001
SALT = "COMPSYS302-2017"
DB_USER_DATA = "sqliteDatabase.db"
import cherrypy
import hashlib
import urllib
import urllib2
import sqlite3
import json
import time
import os #used to figure out what operating system this is running on
import webbrowser
import socket
import thread
import base64
import markdown
import dbFunctions #contains all the database functions
import OfflineMessaging #contains the offline messaging functions
class MainApp(object):
def getLocation(self,ip):
"""Determines the location of the client based on the ip"""
if (('130.216.' in ip)or('10.103.' in ip)or('10.104.' in ip)):
return '0' #Uni Desktop
elif(('172.23.' in ip)or('172.24.' in ip)):
return '1' #Uni WiFi
else:
return '2' #Rest of World
def getIP(self):
"""Determines the ip adddress of the client"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8",80))
internalIP = s.getsockname()[0] #get internal ip
s.close()
externalIP = (urllib2.urlopen(urllib2.Request('http://ident.me'))).read().decode('utf8') #retrieves external ip
location = self.getLocation(internalIP) #get location value that corresponds to the internal ip
if((location == '0')or(location == '1')):
print('The IP of this client is: '+str(internalIP))
return str(internalIP)
else:
print('The IP of this client is: '+str(externalIP))
return str(externalIP)
@cherrypy.expose
def updateClientProfileDetails(self,username=None, fullname=None, position=None, description=None, location=None, picture=None):
"""Updates a row of the ClientProfiles tables which has a username value that corresponds to an input username value """
conn = sqlite3.connect(DB_USER_DATA)
c = conn.cursor()
if not((fullname == "")or(fullname==None)):#As long as the input values are not empty, insert them into the database
c.execute('''UPDATE ClientProfiles SET fullname = ? WHERE profile_username = ?''',(fullname, username))
if not((position == "")or(position==None)):
c.execute('''UPDATE ClientProfiles SET position = ? WHERE profile_username = ?''',(position, username))
if not((description == "")or(description==None)):
c.execute('''UPDATE ClientProfiles SET description = ? WHERE profile_username = ?''',(description, username))
if not((location == "")or(location==None)):
c.execute('''UPDATE ClientProfiles SET location = ? WHERE profile_username = ?''',(location, username))
if not((picture == "")or(picture==None)):
c.execute('''UPDATE ClientProfiles SET picture = ? WHERE profile_username = ?''',(picture, username))
conn.commit()
conn.close()
raise cherrypy.HTTPRedirect('/') #redirect back to the index
#Initialise all the tables on startup
dbFunctions.createAllUsersTable()
dbFunctions.createMessagesTable()
dbFunctions.populateAllUsersTable()
dbFunctions.createClientProfilesTable()
dbFunctions.populateClientProfilesTable()
dbFunctions.createRequestRateTable()
def __init__(self):
"""Init function that sets up the Main User variables when this class is initialised"""
cherrypy.engine.subscribe('stop',self.serverExitLogOff)#On server shutdown, automatically logs off current logged in user
self.currentUser = None
self.currentUserHashedPassword = None
# If they try somewhere we don't know, catch it here and send them to the right place.
@cherrypy.expose
def default(self, *args, **kwargs):
"""The default page, given when we don't recognise where the request is for."""
Page = "I don't know where you're trying to go, so have a 404 Error."
cherrypy.response.status = 404
return Page
# PAGES (which return HTML that can be viewed in browser)
@cherrypy.expose
def index(self):
"""Redirects to the User list page or to the login page if there is no current user logged in"""
try:
username = cherrypy.session['username']
raise cherrypy.HTTPRedirect('/openUsersListPage')
except KeyError: #There is no username
raise cherrypy.HTTPRedirect('/login')
return Page
@cherrypy.expose
def listAPI(self):
"""Returns the APIs supported , as well as the encryption and hashing standards """
return 'Available APIs:\n/listAPI\n/ping\n/receiveMessage [sender] [destination] [message] [stamp] [markdown(opt)] [encryption(opt)] [hashing(opt)] [hash(opt)] [decryptionKey(opt)]\n/getProfile [profile_username] [sender]\n/receiveFile [sender] [destination] [file] [filename] [content_type] [stamp] [encryption(opt)] [hashing(opt)] [hash(opt)] [decryptionKey(opt)]\n/retrieveMessages [requestor]\n/getStatus [profile_username] \nEncryption 0 \nHashing 0'
@cherrypy.expose
def login(self):
"""Opens the login.html page"""
try:
Page = open('login.html').read().format(statusText = cherrypy.session['loginStatusText'])
except:
Page = open('login.html').read().format(statusText = 'Enter your Username and Password')
return Page
@cherrypy.expose
def openMessagingPage(self,destination=None):
"""Opens the messaging.html page and inserts into it the messages between the logged in user and the destination user """
try:
client = cherrypy.session['username']
destinationUserData = dbFunctions.getUserData(destination) #get user data for destination user
messages = dbFunctions.getMessages(client,destination)#retrieve all stored messages between these 2 users
messages.reverse()#reverse this list so that the newest messages will appear at the top of the page
if (not(destinationUserData[4]==None)):
lastLogin = (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(float(destinationUserData[4]))))#convert epoch time to readable format
else:
lastLogin = ''
destinationUserDetails = destination+' '+'Last Login:'+lastLogin+' '+destinationUserData[6]
userConversation = ''
for message in messages:
if (str(message[5])=='1'):
userMessage = markdown.markdown(message[3])#if markdown flag is 1, then carry out this function
else:
userMessage = message[3]
if((client==message[1])and(destination==message[2])):#message sent by client
userConversation += ('<li class="i"> <div class="head"> <span class="time">'+(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(float(message[4]))))+'</span> <span class="name">You</span> </div> <div class="message">'+userMessage+'</div> </li>')
elif((client==message[2])and(destination==message[1])):#message sent from destination
userConversation += ('<li class="friend"> <div class="head"> <span class="name">'+destination+'</span> <span class="time">'+(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(float(message[4]))))+'</span> </div> <div class="message">'+userMessage+'</div> </li>')
Page = open('messaging.html').read().format(receiverUsername = destination, senderUsername = client,userDetails = destinationUserDetails,userMessages=userConversation)
return Page
except:
pass
raise cherrypy.HTTPRedirect('/login') #redirect back to login page
@cherrypy.expose
def openUsersListPage(self):
"""Opens the usersPage.html page and inserts into it the list of users on the server and their online status """
try:
client = cherrypy.session['username']
usersData = dbFunctions.getAllUsersData()
userDetails = ''
for user in usersData:
userDetails += ('''<div class="user"><button type="submit" class="msgBtn2" onclick="window.location.href='/openMessagingPage?destination='''+user[1]+''''">'''+user[1]+'''---'''+user[6]+'''</button><button type=submit class="msgBtn3" onclick="window.location.href='/viewProfilePage?username='''+user[1]+''''">View Profile</button></div>''')
Page = open('usersPage.html').read().format(userList = userDetails,username=client,userstatus=cherrypy.session['userStatus'])
return Page
except:
raise cherrypy.HTTPRedirect('/login') #redirect back to login page
@cherrypy.expose
def viewProfilePage(self,username=None):
"""Opens the viewProfile.html page and inserts into it the profile data that corresponds to the input username """
try:
client = cherrypy.session['username']
except:
raise cherrypy.HTTPRedirect('/login')#if no client is logged in then redirect back to the login page
try:
if username == None:
username = client
if (client == username): #The current logged in user requesting to see their own profile page, so no need to do an external getProfile request
clientData = dbFunctions.getClientProfile(client)
Page = open('viewProfile.html').read().format(profileHeading = 'MY',UPI = client,fullname=clientData[2],position=clientData[3],description=clientData[4],location=clientData[5],image=clientData[6])
return Page
else:
userData = dbFunctions.getUserData(username) #get user data of the profile to display
ip = userData[2]
port = userData[5]
output_dict = {'sender':client,'profile_username':username}
data = json.dumps(output_dict) #data is a JSON object
request = urllib2.Request('http://'+ ip + ':' + port + '/getProfile' , data, {'Content-Type':'application/json'}) #Do a getProfile request to the user
response = urllib2.urlopen(request,timeout=5).read()
responseDict = (json.loads(response))
Page = open('viewProfile.html').read().format(profileHeading = 'USER',UPI = username,fullname=responseDict['fullname'],position=responseDict['position'],description=responseDict['description'],location=responseDict['location'],image=responseDict['picture'])
return Page
except:
Page = open('viewProfile.html').read().format(profileHeading = 'No data available for USER',UPI = username,fullname='',position='',description='',location='',image='')
return Page
@cherrypy.expose
def editProfile(self):
"""Opens the editProfile.html page """
try:
Page = open('editProfile.html').read().format(clientUsername=cherrypy.session['username'])
return Page
except:
raise cherrypy.HTTPRedirect('/openUsersListPage') #if no user is logged in redirect back to the login page
@cherrypy.expose
def ping(self, sender=None):
"""Allows another client to check if this client is still here before initiating other communication. """
try:
if(dbFunctions.checkIfRateLimited(sender)):#Check for rate limiting
return '11: Blacklisted or Rate Limited'
except:
pass
if (sender ==None):
return '1: Missing Compulsory Field'
else:
return '0'
@cherrypy.expose
@cherrypy.tools.json_in()
def getProfile(self):
"""This API allows another client to request information about the user operating this client """
try:
input = cherrypy.request.json
if((not('sender' in input))or(not('profile_username' in input))):
return '1: Missing Compulsory Field'
if(dbFunctions.checkIfRateLimited(input['sender'])):#Check for rate limiting
return '11: Blacklisted or Rate Limited'
profileData = dbFunctions.getClientProfile(input['profile_username'])#get profile data from database
outputDict = {'fullname':profileData[2],'position':profileData[3],'description':profileData[4], 'location':profileData[5], 'picture':profileData[6], 'encoding':profileData[7], 'encryption':profileData[8], 'decryptionKey': profileData[9]}
return json.dumps(outputDict) #data is a JSON object
except:
return 'Error: Something Went Wrong'
@cherrypy.expose
@cherrypy.tools.json_in()
def receiveFile(self):
"""This API allows another client to send this client an arbitrary file (in binary) """
try:
input_data = cherrypy.request.json
if(not('sender' in input_data))or(not('destination' in input_data))or(not('file' in input_data))or(not('stamp' in input_data)or(not('filename' in input_data))or(not('content_type' in input_data))):
return '1: Missing Compulsory Field'
if(dbFunctions.checkIfRateLimited(input_data['sender'])):#Check for rate limiting
return '11: Blacklisted or Rate Limited'
if('encryption' in input_data):
if (input_data['encryption'] == None):
pass
elif (not(str(input_data['encryption']) == '0')):
return '9: Encryption Standard Not Supported'
print ('file sent from: '+input_data['sender'])
base64Length = len(input_data['file'])
fileLength = base64Length * 0.75
if (fileLength>5242880):#Check to see if file is less than 5MB
return ('4: File exceeds 5MB')
inputFile = (input_data['file']).decode('base64')
fileName = input_data['filename']
#Depending on the content_type of the file, an html line of code will be stored in the database so that file can be viewed in an embedded form in the messages page
if 'image/' in input_data['content_type']:
fileMessage = '<img src="receivedFiles/'+fileName+'" alt= Picture 380x320 height="320" width="380">'#embedded image
elif 'video/' in input_data['content_type']:
fileMessage = '<video width="380" height="320" controls><source src="'+'receivedFiles/'+fileName+'">Your browser does not support the video tag.</video>'#embedded video player
elif 'audio/' in input_data['content_type']:
fileMessage = '<audio controls> <source src="'+'receivedFiles/'+fileName+'">Your browser does not support the audio element.</audio>'#embedded audio player
else:
fileMessage = '<a href="receivedFiles/'+fileName+'" download>'+fileName +'</a>'#download link for file
dbFunctions.insertIntoMessagesTable(input_data['sender'], input_data['destination'], fileMessage, input_data['stamp'], '0','true', fileName,input_data['content_type'])
#save the file in the receiveFiles folder
outFile = open('public/receivedFiles/'+fileName,'wb')
outFile.write(inputFile)
outFile.close
print (fileName+' has been received')
return ('0: Success')
except:
return ('Error: Something went wrong')
@cherrypy.expose
def sendFile(self, sender=None, destination=None, outFile=None,stamp = None,encryption=0, hashing = 0, hashedFile = None, decryptionKey=None):
"""Allows the client to send another user a file and its metadata"""
try:
if(dbFunctions.checkIfRateLimited(sender)): #Check for rate limiting
return '11: Blacklisted or Rate Limited'
destinationUserData = dbFunctions.getUserData(destination)
ip = destinationUserData[2]
port = destinationUserData[5]
if (stamp == None)or(stamp== ''):#if no stamp value is provided, generate one
stamp = float(time.time())
fileName = outFile.filename
content_type = outFile.content_type.value
print (fileName + ' is preparing to be sent')
encoded = base64.b64encode(outFile.file.read())#encode file to base64
output_dict = {'sender':sender,'destination':destination,'file':encoded, 'stamp':stamp, 'filename':fileName,'content_type':content_type, 'encryption':encryption, 'hashing':hashing, 'hash': hashedFile}#, 'decryptionKey':decryptionKey}
data = json.dumps(output_dict) #data is a JSON object
sendResponse = ' '
fileSentOffline = False
try:
request = urllib2.Request('http://'+ ip + ':' + port + '/receiveFile' , data, {'Content-Type':'application/json'})
response = urllib2.urlopen(request,timeout=5)
sendResponse = response.read()
print ('file send response: '+sendResponse)
except:
#if the user is offline, send the file to other online users
thread.start_new_thread(OfflineMessaging.sendOfflineFile, (data, cherrypy.session['username'], cherrypy.session['hashedPassword']))
fileSentOffline = True
#Depending on the content_type of the file, an html line of code will be stored in the database so that file can be viewed in an embedded form in the messages page
if 'image/' in output_dict['content_type']:
fileMessage = '<img src="receivedFiles/'+fileName+'" alt= Picture 380x320 height="320" width="380">'#embedded image
elif 'video/' in output_dict['content_type']:
fileMessage = '<video width="380" height="320" controls><source src="'+'receivedFiles/'+fileName+'">Your browser does not support the video tag.</video>'#embedded video
elif 'audio/' in output_dict['content_type']:
fileMessage = '<audio controls> <source src="'+'receivedFiles/'+fileName+'">Your browser does not support the audio element.</audio>'#embedded audio
else:
fileMessage = '<a href="receivedFiles/'+fileName+'" download>'+fileName +'</a>'#embedded download link
if (str(sendResponse[0]) == '0')or(str(sendResponse[1]) == '0')or(fileSentOffline == True):
#save the file in the receivedFiles folder
saveFile = open('public/receivedFiles/'+fileName,'wb')
saveFile.write((output_dict['file']).decode('base64'))
saveFile.close
dbFunctions.insertIntoMessagesTable(output_dict['sender'], output_dict['destination'], fileMessage, output_dict['stamp'], '0','true', fileName, content_type)
else:
print 'file send confirmation not received'
except:
print 'file send error'
if(sender==None)or(destination==None):
raise cherrypy.HTTPRedirect('/login')
raise cherrypy.HTTPRedirect('/openMessagingPage?destination='+destination)
#redirect back to destination user page
# LOGGING IN AND OUT
@cherrypy.expose
def signin(self, username=None, password=None):
"""Check their name and password and send them either to the main page, or back to the main login screen."""
try:
if(dbFunctions.checkIfRateLimited(username)):#check for rate limiting
return '11: Blacklisted or Rate Limited'
except:
pass
hashOfPasswordPlusSalt = None
if(not(password==None)):
passwordPlusSalt = password + SALT
hashOfPasswordPlusSalt = hashlib.sha256(passwordPlusSalt).hexdigest()
error = self.authoriseUserLogin(username,hashOfPasswordPlusSalt, True)
if (error == 0):#check if login is successful
cherrypy.session['username'] = username;
cherrypy.session['hashedPassword'] = hashOfPasswordPlusSalt;#storing hashed password is more secure than storing plain password
cherrypy.session['userStatus'] = 'Online'
self.currentUser = cherrypy.session['username']
self.currentUserHashedPassword = cherrypy.session['hashedPassword']
self.serverReportThreading(cherrypy.session['username'],cherrypy.session['hashedPassword'])#start new thread that will be used to continously log the client back into the login server
raise cherrypy.HTTPRedirect('/openUsersListPage')
else:
cherrypy.session['loginStatusText'] = "Username or Password is Incorrect"
raise cherrypy.HTTPRedirect('/login')
@cherrypy.expose
def signout(self,serverShutDown = False):
"""Logs the current user out, expires their session"""
redirectToLoginPage = False
username = None
try:
try:
username = cherrypy.session['username']
hashedPassword = cherrypy.session['hashedPassword']
except:
username = self.currentUser
hashedPassword = self.currentUserHashedPassword
if (username == None):
pass
else:
logoutRequest = urllib2.Request('https://cs302.pythonanywhere.com/logoff?username=' + username + '&password=' + hashedPassword + '&enc=0')
logoutResponse = urllib2.urlopen(logoutRequest)
logoutData = logoutResponse.read()
print ('Server Logout Report: '+logoutData)
if(serverShutDown == True):
return
cherrypy.lib.sessions.expire() #expire session data
redirectToLoginPage = True
except:
try:
cherrypy.lib.sessions.expire()
except:
pass
redirectToLoginPage = True
if(serverShutDown == True):#if this function is called because of server shutdown, then no need to redirect back to the login page
return
if(redirectToLoginPage):
raise cherrypy.HTTPRedirect('/login')
def serverExitLogOff(self):
"""This function is called when the server shuts down, this function then calls the signout function to log out the current user (if any) """
print('Server Exit Procedure Initiated')
self.signout(True)
def serverReportThreading(self,username,hashedPassword):
"""This function starts a new thread that runs the serverReportTimer function, which is used to communicate with the login server regularly"""
thread.start_new_thread(self.serverReportTimer, (username,hashedPassword))
def serverReportTimer(self,username,hashedPassword):
"""This function first calls the requestOfflineMessages function to retrieve any messages that this client may have received when they were online
and then after that, every 30 seconds logs the current client back into the login server and also resets the RateRequest table"""
beginTime=time.time()
loop = True
try:
self.requestOfflineMessages(username,hashedPassword)#request messages that client received while offline
except:
print 'offline msg request failed'
while (loop == True):
if(self.checkIfStillLoggedIn(username,hashedPassword)): #if the user has been logged out, don't try logging them in again and kill the thread
try:
dbFunctions.clearRateRequestTable()#clear request number values back to 0
self.authoriseUserLogin(username,hashedPassword)#log the user in periodically
except:
pass
time.sleep(30.0-((time.time()-beginTime)%30.0))#how many secs there are to the nearest 30 sec block of time, 30 minus that to figure out how long you have to sleep, the reason I do this is to account for variable execution times for self.authoriseUserLogin()
else:
loop = False
print 'server login report thread exited'
thread.exit()
def checkIfStillLoggedIn(self, username=None, hashedPassword=None):
"""Calls the login server's getList method to determine whether the input username is still logged in"""
try:
ip = self.getIP()
location = self.getLocation(ip)
onlineUsersRequest = urllib2.Request('http://cs302.pythonanywhere.com/getList?username='+username+'&password='+hashedPassword+'&enc=0&json=1')
onlineUsersResponse = urllib2.urlopen(onlineUsersRequest)
onlineUsersData = onlineUsersResponse.read()
onlineUsersData = json.loads(onlineUsersData)
for value in onlineUsersData.itervalues():
if(value['username']==username):
return True
except:
pass
return False
def authoriseUserLogin(self, username=None, hashedPassword=None, firstLogin = False):
"""Logs a client into the login server with the input credentials also updates the AllUsers table with new online user data from the login server"""
try:
ip = self.getIP()
location = self.getLocation(ip)
if ((username==None)or(username=='')or(hashedPassword==None)or(hashedPassword=='')):
return 1
loginRequest = urllib2.Request('http://cs302.pythonanywhere.com/report?username='+username+'&password='+hashedPassword+'&location='+location+'&ip='+ip+'&port='+str(listen_port)+'&enc=0') #Object which represents the HTTP request we are making
loginResponse = urllib2.urlopen(loginRequest,timeout=10)#Returns a response object for the requested URL
loginData = loginResponse.read() #The response is a file-like object, so .read() can be called on it
print ('Server login report: '+loginData)
if(loginData[0]=='0'):#if login is successful, request online user data
onlineUsersRequest = urllib2.Request('http://cs302.pythonanywhere.com/getList?username='+username+'&password='+hashedPassword+'&enc=0&json=1')
onlineUsersResponse = urllib2.urlopen(onlineUsersRequest,timeout=5)
onlineUsersData = onlineUsersResponse.read()
dbFunctions.updateAllUsersTable(onlineUsersData, firstLogin)
if (loginData[0] == "0") :
return 0
else:
return 1
except:
print 'Login Report To Server Failed'
return 1
@cherrypy.expose
@cherrypy.tools.json_in()
def retrieveMessages(self):
"""This API allows a requesting client that has recently logged on to ask this client for any messages that were intended for the requesting client when they were offline"""
try:
input = cherrypy.request.json
if(not('requestor' in input)):
return '1: Missing Compulsory Field'
elif((input['requestor'])==None)or((input['requestor'])==''):
return '1: Missing Compulsory Field'
requestor = input['requestor']
if(dbFunctions.checkIfRateLimited(input['requestor'])):#check for rate limiting
return '11: Blacklisted or Rate Limited'
messagesForRequestor = dbFunctions.getMessagesForOneUser(requestor)
for message in messagesForRequestor:
stamp = message[4]
if (message[6]=='false'):#send message
self.sendMessage(message[1], message[2], message[3],message[5],float(stamp))
elif (message[6]=='true'):
fileName = message[7]
return '0: Success'
except:
return 'Error: Something Went Wrong'
@cherrypy.expose
def openSetUserStatusPage(self):
"""Opens the editUserStatus.html page"""
try:
username = cherrypy.session['username']
Page = open('editUserStatus.html')
return Page
except:
pass
raise cherrypy.HTTPRedirect('/openUsersListPage')
@cherrypy.expose
def setUserStatus(self,userStatus = 'Online'):
"""Updates the status of the current logged in user with the input value """
try:
cherrypy.session['userStatus'] = userStatus
conn = sqlite3.connect(DB_USER_DATA)
c = conn.cursor()
c.execute('''UPDATE AllUsers SET status = ? WHERE username = ?''',(userStatus, cherrypy.session['username']))
conn.commit() # commit actions to the database
conn.close()
except:
print 'error setting user status'
raise cherrypy.HTTPRedirect('/openUsersListPage')
@cherrypy.expose
@cherrypy.tools.json_in()
def getStatus(self):
"""This API allows another client to request information about the current status of the user operating this client. The valid values to be returned are {Online, Idle, Away, Do Not Disturb, Offline}."""
try:
input = cherrypy.request.json
if(not('profile_username' in input)):
return '1: Missing Compulsory Field'
userStatus = dbFunctions.getUserData(input['profile_username'])#get user status from database
outputDict = {'status':userStatus[6]}
return json.dumps(outputDict)
except:
return '3: Client Currently Unavailable'
@cherrypy.expose
def requestOfflineMessages(self,username,hashedPassword):
"""Calls the retrieveMessages API of all logged in users to get any messages this client may have received while they were offline"""
try:
if(dbFunctions.checkIfRateLimited(username)): #check for rate limiting
return '11: Blacklisted or Rate Limited'
onlineUsersRequest = urllib2.Request('http://cs302.pythonanywhere.com/getList?username='+username+'&password='+hashedPassword+'&enc=0&json=1')
onlineUsersResponse = urllib2.urlopen(onlineUsersRequest,timeout=5)
onlineUsersData = onlineUsersResponse.read()
onlineUsersData = json.loads(onlineUsersData)
output_dict = {'requestor':username}
data = json.dumps(output_dict) #data is a JSON object
for value in onlineUsersData.itervalues():#loop through all online users
try:
userToRequest = value['username']
if (not(userToRequest == username)):
destinationUserData = dbFunctions.getUserData(userToRequest)
ip = destinationUserData[2]
port = destinationUserData[5]
#call retrieveMessages API on all online users
request = urllib2.Request('http://'+ ip + ':' + port + '/retrieveMessages', data, {'Content-Type':'application/json'})
response = urllib2.urlopen(request,timeout=1)
print('offline messages request response: '+response.read())
except:
pass
except:
print('Error trying to retrieve Offline Messages')
@cherrypy.expose
@cherrypy.tools.json_in()
def receiveMessage(self):
"""This API allows another client to send this client a message"""
try:
input_data = cherrypy.request.json
if(not('sender' in input_data))or(not('destination' in input_data))or(not('message' in input_data))or(not('stamp' in input_data)):
return '1: Missing Compulsory Field'
if(dbFunctions.checkIfRateLimited(input_data['sender'])):#Check for rate limiting
return '11: Blacklisted or Rate Limited'
if('encryption' in input_data):
if (input_data['encryption'] == None):
pass
elif (not(str(input_data['encryption']) == '0')):
return '9: Encryption Standard Not Supported'
markDown='0'
if ('markdown' in input_data):
if (input_data['markdown'] == None):
message = input_data['message']
elif ((str(input_data['markdown'])) == '1'):
message = markdown.markdown(input_data['message'])
markDown = '1'
else:
message = input_data['message']
else:
message = input_data['message']
dbFunctions.insertIntoMessagesTable(input_data['sender'], input_data['destination'], message, input_data['stamp'], markDown,'false', None,None)
return ('0: Success')
except Exception as e:
print e
return ('Error: Something went wrong')
@cherrypy.expose
def sendMessage(self, sender=None, destination=None, message='Default Message',markDown='0',stamp=None,encryption=0, hashing = 0, hashedMessage = None, decryptionKey=None):
"""This function allows the logged in client to send a message and its metadata to another user"""
try:
if(dbFunctions.checkIfRateLimited(sender)): #Check for rate limiting
return '11: Blacklisted or Rate Limited'
if ((message == None)or(message == '')): #if the message field is empty redirect back to the messaging page
raise cherrypy.HTTPRedirect('/openMessagingPage?destination='+destination)
destinationUserData = dbFunctions.getUserData(destination)
ip = destinationUserData[2]
port = destinationUserData[5]
if (stamp == None)or(stamp== ''):#generate stamp if it doesn't already exist
stamp = float(time.time())
output_dict = {'sender':sender,'destination':destination,'message':message, 'stamp':stamp, 'markdown':int(markDown), 'encryption':encryption, 'hashing':hashing, 'hash': hashedMessage, 'decryptionKey':decryptionKey}
data = json.dumps(output_dict) #data is a JSON object
try:
request = urllib2.Request('http://'+ ip + ':' + port + '/receiveMessage', data, {'Content-Type':'application/json'})
response = urllib2.urlopen(request,timeout=5)
print response.read()
except:
#send message offline if the destination user is offline
message = 'Msg Sent Offline: ' + message
output_dict = {'sender':sender,'destination':destination,'message':message, 'stamp':stamp, 'markdown':int(markDown), 'encryption':encryption, 'hashing':hashing, 'hash': hashedMessage, 'decryptionKey':decryptionKey}
data = json.dumps(output_dict) #data is a JSON object
#call sendOfflineMessage function on its own thread, so that the rest of the program doesn't have to wait on it.
thread.start_new_thread(OfflineMessaging.sendOfflineMessage, (data, cherrypy.session['username'], cherrypy.session['hashedPassword']))
if (str(markDown)=='1'):
message = markdown.markdown(output_dict['message'])
#save message in database
dbFunctions.insertIntoMessagesTable(output_dict['sender'], output_dict['destination'], message, output_dict['stamp'], int(markDown),'false', None,None)
except:
print 'send message error'
if(sender==None)or(destination==None):
raise cherrypy.HTTPRedirect('/login')
#redirect back to messaging page
raise cherrypy.HTTPRedirect('/openMessagingPage?destination='+destination)
cherrypy.config.update({'error_page.404': default,'server.socket_host': '127.0.0.1','server.socket_port': 10001,'engine.autoreload.on': True,'tools.sessions.on': True,'tools.encode.on': True,'tools.encode.encoding': 'utf-8','tools.staticdir.on' : True, 'tools.staticdir.dir' : os.path.join(os.getcwd(), 'public'),'tools.staticdir.index' : 'login.html'})
def runMainApp():
# Create an instance of MainApp and tell Cherrypy to send all requests under / to it.
cherrypy.tree.mount(MainApp(), "/")
# Tell Cherrypy to listen for connections on the configured address and port.
cherrypy.config.update({'server.socket_host': listen_ip,
'server.socket_port': listen_port,
'engine.autoreload.on': True,
})
print "========================="
print "University of Auckland"
print "COMPSYS302 - Software Design Application"
print "========================================"
# Start the web server
cherrypy.engine.start()
# And stop doing anything else. Let the web server take over.
cherrypy.engine.block()
#Run the function to start everything
runMainApp()