-
Notifications
You must be signed in to change notification settings - Fork 0
/
bavarder.py
executable file
·297 lines (252 loc) · 8.18 KB
/
bavarder.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
import web
import json
import os
import sendmsg
import httplib2
import urllib3
from oauth2client.service_account import ServiceAccountCredentials
from google.appengine.ext import ndb
from google.appengine.api import app_identity
version = "Beta v0.5.4"
def get_at():
a=ServiceAccountCredentials.from_json_keyfile_dict(sendmsg.kfdict, "https://www.googleapis.com/auth/firebase.messaging")
at=a.get_access_token()
return at.access_token
urls=(
"/", "index",
"/changelog", "clog",
"/sendmsg", "sendmesg",
"/user","grabcontacts",
"/rscvmsgs", "grabmessages",
"/changesetting", "set",
"/delprof", "delprof",
"/associateprof", "allownotifs",
"/makeuser", "mkusr",
"/createacct", "create"
)
app=web.application(urls, globals())
render=web.template.render("templates/")
class msg(ndb.Model):
emailto=ndb.StringProperty()
number=ndb.IntegerProperty()
emailfrom=ndb.StringProperty()
message=ndb.StringProperty()
class user(ndb.Model):
user=ndb.StringProperty()
dark_theme=ndb.BooleanProperty()
noir=ndb.BooleanProperty()
contacts=ndb.StringProperty(repeated=True)
notifclientids=ndb.StringProperty(repeated=True)
class index:
def GET(self):
x=web.input(sdr="")
return render.index(x.sdr, version)
def POST(self):
x=web.input()
q=user.query()
results=q.fetch()
for y in range(len(results)):
if results[y].user==x.email:
usr=results[y].key.get()
usr.contacts.append(x.user)
usr.put()
return '<head><meta http-equiv="refresh" content="0; url=/" /></head>'
class create:
def GET(self):
return render.create()
def POST(self):
x = web.input()
return render.send(x.email, x.pswd)
class clog:
def GET(self):
return render.clog(version)
class mkusr:
def POST(self):
x = web.input()
q=user.query().fetch()
for person in q:
if person.user==x.email:
return 'user already exists'
userset = user(
user=x.email
)
set_key = userset.put()
return 'user created'
class sendmesg:
def POST(self):
q=user.query()
results=q.fetch()
q2=msg.query().order(msg.number)
results2=q2.fetch()
x=web.input()
try:
msgnumber=results2[len(results2)-1].number+1
except:
msgnumber=0
nids = []
picurl = ""
for person in results:
if person.user == x.to:
for ids in person.notifclientids:
nids.append(ids)
http = httplib2.Http()
for item in nids:
data = {
'message':{
'token' : '{0}'.format(item),
'notification':{
'body': '{0}'.format(x.message),
'title': '{0}'.format(x.email),
},
'data':{
'sender': '{0}'.format(x.email),
},
'webpush': {
'fcm_options':{
'link': 'https://bavarder.app',
},
'notification':{
'actions':[
{
'action': 'reply',
'title': 'Reply'
}
],
'badge': '/notificon.ico',
'icon': '/icon-512.png'
}
},
'android':{
'notification':{
'color': '#d32f2f',
'icon': '/icon-512.png'
}
},
},
}
auth = "Bearer " + get_at()
r = http.request(
uri="https://fcm.googleapis.com/v1/projects/lightningchat-1/messages:send",
method="POST",
headers={
"Content-Type": "application/json",
"Authorization": auth
},
body=str(data)
)
print r
message_key = msg(
emailto=x.to, emailfrom=x.email, message=x.message, number=msgnumber
)
message_key.put()
return json.dumps({'from':x.email,'to':x.to, 'msg':x.message})
class grabcontacts:
def POST(self):
x=web.input()
q=user.query()
results=q.fetch()
usercontacts=[]
darktheme=False
oleddarktheme=False
for y in range(len(results)):
if results[y].user==x.email:
for contact in results[y].contacts:
for human in results:
if human.user == contact:
usercontacts.append(
{
'name': contact
}
)
if results[y].dark_theme==True:
darktheme=True
if results[y].noir==True:
oleddarktheme=True
return json.dumps({"contacts":usercontacts, "dark":darktheme, "oleddark":oleddarktheme})
class grabmessages:
def POST(self):
x=web.input()
try:
q=msg.query(
ndb.AND(
msg.emailto.IN([x.to, x.email]),
msg.emailfrom.IN([x.to, x.email])
),
msg.number>int(x.lastnumber)
).order(msg.number).fetch()
except:
return "false"
if len(q) == 0:
return "false"
messages=[]
messagesloc=[]
lastnum=int(x.lastnumber)
for y in range(len(q)):
if (q[y].emailto==x.to) and (q[y].emailfrom==x.email): # to
messages.append(q[y].message)
messagesloc.append(True)
lastnum=q[y].number
if (q[y].emailfrom==x.to) and (q[y].emailto==x.email): # from
messages.append(q[y].message)
messagesloc.append(False)
lastnum=q[y].number
count=len(messages)
if count!=int(x.messagecount) or (count==0):
return json.dumps({"messages":messages, "lastnum":lastnum, "loc":messagesloc})
else:
return "false"
class set:
def POST(self):
q=user.query()
results=q.fetch()
x=web.input()
try:
f=x.dark
dtheme=True
except:
dtheme=False
try:
f2=x.noirmode
noirtheme=True
except:
noirtheme=False
for y in range(len(results)):
if (results[y].user==x.email):
users=results[y].key.get()
users.dark_theme=dtheme
users.noir=noirtheme
users.put()
return '<head><meta http-equiv="refresh" content="0; url=/" /></head>'
userset = user(
user=x.email, dark_theme=dtheme, noir=noirtheme
)
set_key = userset.put()
return '<head><meta http-equiv="refresh" content="0; url=/" /></head>'
class delprof:
def POST(self):
x=web.input()
q=user.query(user.user==x.email).fetch()
q[0].key.delete()
return "success"
class allownotifs:
def POST(self):
x = web.input()
q = user.query().fetch()
for y in range(len(q)):
if q[y].user==x.email:
users=q[y].key.get()
idinids=False
for z in range(0,len(users.notifclientids)):
if users.notifclientids[z]==x.notifid:
idinids=True
if idinids!=True:
users.notifclientids.append(x.notifid)
users.put()
return "successfully added notifs"
y = user(
user=x.email
)
y.notifclientids.append(x.notifid)
y.put()
return "successfully added notifs"
app=app.gaerun()