Compare commits

..

No commits in common. "44255d6bc267865a037a9df4cd0dff11ff852995" and "84d8c8bcc8f53e3107dca8cfd2887d96a5948cf7" have entirely different histories.

1 changed files with 16 additions and 92 deletions

108
sms.py
View File

@ -24,70 +24,13 @@ class HTTPServer (http4_server_do_not_use):
def html_header (title = 'No title'):
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>')
return ('<html>\n<head>\n\t<title>' + title + '</title>\n<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>')
@ -100,10 +43,10 @@ def html_list (lst):
return (t)
def html_link (url, text = None, target = '_self'):
def html_link (url, text = None):
if text is None:
text = url
return ('<a href="' + url + '" target="' + target + '">' + text + '</a>')
return ('<a href="' + url + '">' + text + '</a>')
def html_table (body, table_style = None, cell_style = None):
@ -117,22 +60,6 @@ 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>')
@ -261,15 +188,6 @@ 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 ()
@ -279,14 +197,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, target = 'messages'))
lst.append ([html_link ('/?thread=' + str (i), text = s), ls [i] [1], ls [i] [2] [0:50]])
self.wfile.write ((
html_header (title = 'Conversations list') +
html_title ('Conversation') +
html_paragraph (html_link ('/new', text = 'New conversation', target = 'messages')) +
html_paragraph (html_link ('/list', text = 'Refresh list')) +
html_paragraph (html_link ('/new', text = 'New conversation')) +
html_paragraph (html_link ('/', text = 'Refresh list')) +
html_paragraph ('List of conversations follows:') +
html_list (lst) +
html_table (lst) +
html_footer ()
).encode ('utf-8'))
elif self.path == '/new':
@ -299,6 +217,7 @@ 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 (
@ -318,6 +237,7 @@ 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:') +
@ -349,15 +269,19 @@ class Web_Service (BaseHTTPRequestHandler):
self.send_response (200)
self.send_header ('Content-type', 'text/html; charset=UTF-8')
self.end_headers ()
msgs = ''
t = []
for i in sorted (conv, key = lambda item: item [0], reverse = True) [0:2000]:
msgs += html_message (i [2], not i [1], i [0], '' if i [1] else '')
if i [1]:
t.append ([i [2], '' + i [0]])
else:
t.append (['' + i [0], i [2]])
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) +
@ -366,7 +290,7 @@ class Web_Service (BaseHTTPRequestHandler):
'/send'
) +
html_link (self.path, text = 'Refresh') +
msgs +
html_table (t, table_style = 'table-layout: fixed; border: 1px solid black', cell_style = 'width: 500px; border: 1px solid black') +
html_footer ()
).encode ('utf-8'))
else: