Compare commits

...

2 Commits

Author SHA1 Message Date
Sasha MOREL 44255d6bc2 Add iframes
Trying more fluid experience with iframes.
2021-09-05 13:42:00 +02:00
Sasha MOREL 3dc6d4323b Nicer conversation appearance
I added some style to display messages more nicely.
2021-09-05 11:07:28 +02:00
1 changed files with 92 additions and 16 deletions

108
sms.py
View File

@ -24,13 +24,70 @@ class HTTPServer (http4_server_do_not_use):
def html_header (title = 'No title'):
return ('<html>\n<head>\n\t<title>' + title + '</title>\n<head>\n<body>')
return ('<html>\n<head>\n\t<title>' + title + '</title>\n' + '''\t<style>
body {
margin: 0 auto;
max-width: 100%;
padding: 0 20px;
}
.message {
border: 2px solid #dedede;
background-color: #f1f1f1;
border-radius: 5px;
padding: 10px;
margin: 10px 0;
}
.darker {
border-color: #ccc;
background-color: #ddd;
}
.message::after {
content: "";
clear: both;
display: table;
}
.message img {
float: left;
max-width: 60px;
width: 100%;
margin-right: 20px;
border-radius: 50%;
}
.message img.right {
float: right;
margin-left: 20px;
margin-right:0;
}
.time-right {
float: right;
color: #aaa;
}
.time-left {
float: left;
color: #999;
}
</style>''' + '<head>\n<body>')
def html_title (text):
return ('\n\t<h1>' + text + '</h1>')
def html_iframe (src, height = '150', width = '300', name = None, style = 'border:none;'):
r = '<iframe src="' + src + '" width="' + width + '" height="' + height + '" style="' + style + '"'
if name is not None:
r += ' name="' + name + '"'
r += '></iframe>'
return (r)
def html_paragraph (text):
return ('\n\t<p>' + text + '</p>')
@ -43,10 +100,10 @@ def html_list (lst):
return (t)
def html_link (url, text = None):
def html_link (url, text = None, target = '_self'):
if text is None:
text = url
return ('<a href="' + url + '">' + text + '</a>')
return ('<a href="' + url + '" target="' + target + '">' + text + '</a>')
def html_table (body, table_style = None, cell_style = None):
@ -60,6 +117,22 @@ def html_table (body, table_style = None, cell_style = None):
return (t)
def html_message (msg, sent, time, name, avatar = None):
r = '\n\t<div class="message'
if sent:
r += ' darker'
r += '">\n\t\t<img src="' + str (avatar) + '" alt="' + name + '"'
if sent:
r += ' class="right"'
r += 'style"width:100%;">\n\t\t<p>' + msg.replace ('\n', '<br />') + '</p>\n\t\t<spam class="time-'
if sent:
r += 'left'
else:
r += 'right'
r += '">' + time + '</span>\n\t</div>'
return (r)
def html_form (body, url, post = True):
return ('\n\t<form action="' + url + '"' + (' method="post"' if post else '') + '>' + body + '\n\t</form>')
@ -188,6 +261,15 @@ class Web_Service (BaseHTTPRequestHandler):
if locked_client == client:
global sms
if self.path == '/':
self.send_response (200)
self.send_header ('Content-type', 'text/html; charset=UTF-8')
self.end_headers ()
self.wfile.write ((
html_header (title = 'Termux WebSMS') +
html_iframe ('/list', height = '100%', width = '30%', name = 'list') + html_iframe ('about:blank', height = '100%', width = '70%', name = 'messages') +
html_footer ()
).encode ('utf-8'))
elif self.path == '/list':
self.send_response (200)
self.send_header ('Content-type', 'text/html; charset=UTF-8')
self.end_headers ()
@ -197,14 +279,14 @@ class Web_Service (BaseHTTPRequestHandler):
s = ls [i] [0] + ' (' + str (ls [i] [3]) + ')'
if len (ls [i]) == 5:
s = ls [i] [4] + ' (' + str (ls [i] [3]) + ')'
lst.append ([html_link ('/?thread=' + str (i), text = s), ls [i] [1], ls [i] [2] [0:50]])
lst.append (html_link ('/?thread=' + str (i), text = s, target = 'messages'))
self.wfile.write ((
html_header (title = 'Conversations list') +
html_title ('Conversation') +
html_paragraph (html_link ('/new', text = 'New conversation')) +
html_paragraph (html_link ('/', text = 'Refresh list')) +
html_paragraph (html_link ('/new', text = 'New conversation', target = 'messages')) +
html_paragraph (html_link ('/list', text = 'Refresh list')) +
html_paragraph ('List of conversations follows:') +
html_table (lst) +
html_list (lst) +
html_footer ()
).encode ('utf-8'))
elif self.path == '/new':
@ -217,7 +299,6 @@ class Web_Service (BaseHTTPRequestHandler):
lst.append (html_link ('/new?num=' + quote (i ['number']), text = i ['name'] + ' (' + i ['number'] + ')'))
self.wfile.write ((
html_header (title = 'New conversation') +
html_paragraph (html_link ('/', text = '← Back to list')) +
html_title ('Create a new conversation') +
html_paragraph ('Put number here:') +
html_form (
@ -237,7 +318,6 @@ class Web_Service (BaseHTTPRequestHandler):
number = self.path [9:]
self.wfile.write ((
html_header (title = 'New conversation') +
html_paragraph (html_link ('/', text = '← Back to list')) +
html_paragraph (html_link ('/new', text = '← Back to new message')) +
html_title ('Create a new conversation with ' + unquote (number)) +
html_paragraph ('Message:') +
@ -269,19 +349,15 @@ class Web_Service (BaseHTTPRequestHandler):
self.send_response (200)
self.send_header ('Content-type', 'text/html; charset=UTF-8')
self.end_headers ()
t = []
msgs = ''
for i in sorted (conv, key = lambda item: item [0], reverse = True) [0:2000]:
if i [1]:
t.append ([i [2], '' + i [0]])
else:
t.append (['' + i [0], i [2]])
msgs += html_message (i [2], not i [1], i [0], '' if i [1] else '')
if name is None:
name = number
else:
name = name + ' (' + number + ')'
self.wfile.write ((
html_header (title = 'Conversation with ' + name) +
html_paragraph (html_link ('/', text = '← Back to list')) +
html_title (name) +
html_form (
html_form_text ('msg', textarea = True) +
@ -290,7 +366,7 @@ class Web_Service (BaseHTTPRequestHandler):
'/send'
) +
html_link (self.path, text = 'Refresh') +
html_table (t, table_style = 'table-layout: fixed; border: 1px solid black', cell_style = 'width: 500px; border: 1px solid black') +
msgs +
html_footer ()
).encode ('utf-8'))
else: