diff --git a/credentials.json b/credentials.json index e3db49c..2d681f7 100644 --- a/credentials.json +++ b/credentials.json @@ -1,5 +1,6 @@ { - "username":"@username:homeserver.org", + "home_server":"homeserver.org", + "username":"username", "password":"cantguessthis", - "server":"https://myhomeserver.org:8910" + "base_url":"https://myhomeserver.org:8910" } diff --git a/redpill.py b/redpill.py index 117fc7c..42251f0 100644 --- a/redpill.py +++ b/redpill.py @@ -17,23 +17,25 @@ #from requests import requests #requests.packages.urllib3.disable_warnings() +def log(obj, filename='redpill.log'): + with open(filename, 'a') as the_file: + the_file.write(str(obj) + "\n") + def loadCredentials(filename): - global password, username, server - json_data = open(filename) - data = json.load(json_data) - json_data.close() + global server, base_url, username, access_token, password + with open(filename) as json_data: + data = json.load(json_data) + server = data["home_server"] + base_url = data.get("base_url", "https://{}".format(server)) username = data["username"] - password = data["password"] - server = data["server"] - + access_token = data.get("access_token", None) + password = None if access_token else data["password"] def processMessage(obj): global room, rooms, lastEventRoom, room_keys - with open('event.log', 'a') as the_file: - the_file.write(str(obj) + "\n") - + log(obj, 'redpill-event.log') if "room_id" in obj: if room != all_rooms: @@ -65,7 +67,8 @@ def getFirstRoomAlias(r): def start(stdscr): - global size, room, data, rooms, access_token, endTime, rooms, all_rooms, lastEventRoom, room_keys + global server, base_url, username, access_token, password + global size, room, rooms, all_rooms, lastEventRoom, room_keys curses.curs_set(0) curses.use_default_colors() @@ -75,22 +78,20 @@ def start(stdscr): stdscr.refresh() loadCredentials("./credentials.json") - client = MatrixClient(server) - access_token = client.login_with_password( - username, - password, - size[0]) + client = MatrixClient(base_url, token=access_token, user_id='@{}:{}'.format(username, server)) + if access_token is None: + access_token = client.login_with_password( + username, + password, + size[0]) rooms = client.get_rooms() all_rooms = "all rooms" - rooms[all_rooms] = Room(client, all_rooms) + rooms[all_rooms] = all_rooms - rooms[all_rooms].events = [] room_keys = list(rooms.keys()) - room = all_rooms #room_keys[1] # "all_rooms" - nextRoom = 1 - endTime = client.end + room = all_rooms curses.halfdelay(10) maxDisplayName = 24 @@ -113,36 +114,38 @@ def start(stdscr): stdscr.clear() + line = "redpill v0.7" + line += " · screen size: " + str(size) + if isinstance(rooms[room], Room): + line += " · chat size: " + str(len(rooms[room].events)) + line += " · room: " + # we want NAME aka ALIAS[0] (ROOM_ID) # or 2nd choice: ALIAS[0] (ROOM_ID) # or fallback: ROOM_ID - line = str(room) - if line == all_rooms: - pass - elif rooms[room].name is None: - if len(rooms[room].aliases) > 0 and rooms[room].aliases[0] != room: - line = rooms[room].aliases[0] + " (" + line + ")" - else: + if room == all_rooms: + line += str(room) + elif rooms[room].name: if len(rooms[room].aliases) > 0 and rooms[room].aliases[0] != room: - line = rooms[room].name + " aka " + getFirstRoomAlias(rooms[room]) + " (" + line + ")" + line += rooms[room].name + " aka " + getFirstRoomAlias(rooms[room]) + " (" + str(room) + ")" + elif rooms[room].name != room: + line += rooms[room].name + " (" + str(room) + ")" else: - if rooms[room].name != room: - line = rooms[room].name + " (" + line + ")" + line += str(room) + elif len(rooms[room].aliases) > 0 and rooms[room].aliases[0] != room: + line += rooms[room].aliases[0] + " (" + str(line) + ")" + else: + line += str(room) - #line.encode("utf-8") - if rooms[room].topic is not None: + if isinstance(rooms[room], Room) and rooms[room].topic is not None: line += " · topic: " + rooms[room].topic - stdscr.addstr( - 0, 0, ( - "redpill v0.7 · screen size: " + str(size) + " · chat size: " - + str(len(rooms[room].events)) + " · room: " + str(line) + " the variables: room: " + room + " last: " - + lastEventRoom - ), curses.A_UNDERLINE - ) + line += " · variables: room: " + room + ", last: " + lastEventRoom - current = len(rooms[room].events) - 1 + stdscr.addstr(0, 0, line, curses.A_UNDERLINE) + + current = len(rooms[room].events) - 1 if isinstance(rooms[room], Room) else -1 if True: y = 1 @@ -160,6 +163,9 @@ def start(stdscr): stdscr.addstr(currentLine - 1, 0, space, curses.A_UNDERLINE) for event in reversed(rooms[room].events): + log(event) + log("maxDisplayName: {}, size: {}, currentLine: {}".format(maxDisplayName, size, currentLine)) + if event["type"] == "m.typing": #if True: continue # do something clever @@ -193,7 +199,7 @@ def start(stdscr): else: #currentLine = size[0] - y - currentLine -= 1 + #currentLine -= 1 if currentLine < 3: # how many lines we want to reserve break @@ -207,29 +213,17 @@ def start(stdscr): ).strftime('%Y-%m-%d %H:%M:%S') # assumption: body == normal message - length = 0 - if "user_id" in event: - length = len( - event["user_id"] - ) + length = len(event["sender"]) if "sender" in event else 0 + log("length: {}, currentLine: {}".format(length, currentLine)) + if "body" in event["content"]: - rawText = event["content"]["body"].encode('utf-8') + rawText = event["content"]["body"] # .encode('utf-8') if event["content"]["msgtype"] == "m.emote": if len(rawText) > 0 and rawText[0] == " ": rawText = rawText[1:] - linesNeeded = (displayNamestartingPos + maxDisplayName + 3 + len(rawText)) / size[1] - lin = (displayNamestartingPos + maxDisplayName + 3 + len(rawText)) - - #if currentLine == size[0] - 2: - # stdscr.addstr(currentLine, 0, str(lin) + " " + str(size[1]) + " " + str(linesNeeded) + " ", curses.A_UNDERLINE) - #else: - # stdscr.addstr(currentLine, 0, str(lin) + " " + str(size[1]) + " " + str(linesNeeded) + " ") - - - linesNeeded = 0 buf = "" @@ -246,9 +240,9 @@ def start(stdscr): buf += bufSinceLastWord if PAD_COMMENTS or first: - linesNeeded += (displayNamestartingPos + maxDisplayName + 3 + len(buf)) / size[1] + linesNeeded += int((displayNamestartingPos + maxDisplayName + 3 + len(buf)) / size[1]) else: - linesNeeded += len(buf) / size[1] + linesNeeded += int(len(buf) / size[1]) first = False lineByLineText.append(buf) @@ -291,27 +285,17 @@ def start(stdscr): # self.X += 1 buf += bufSinceLastWord lineByLineText.append(buf) - linesNeeded += (displayNamestartingPos + maxDisplayName + 3 + len(buf)) / size[1] + linesNeeded += int((displayNamestartingPos + maxDisplayName + 3 + len(buf)) / size[1]) buf = "" - currentLine -= linesNeeded if currentLine - linesNeeded < 2: # how many lines we want to reserve break - if currentLine == size[0] - 2: - stdscr.addstr(currentLine, 0, str(lin) + " " + str(size[1]) + " " + str(linesNeeded) + " ", curses.A_UNDERLINE) - else: - stdscr.addstr(currentLine, 0, str(lin) + " " + str(size[1]) + " " + str(linesNeeded) + " ") - - #for i in range(linesNeeded): - - if PAD_COMMENTS: pad = displayNamestartingPos + maxDisplayName + 3 - - #if linesNeeded == 0: linesNeeded += 1 + currentLine -= linesNeeded for i in range(linesNeeded): buf = rawText[:size[1] - pad] @@ -320,15 +304,17 @@ def start(stdscr): if currentLine + i == size[0] - 2: stdscr.addstr( - currentLine + i, displayNamestartingPos + - maxDisplayName + 3, lineByLineText[i], + currentLine + i, + displayNamestartingPos + maxDisplayName + 3, + lineByLineText[i], curses.A_BOLD + curses.A_UNDERLINE ) else: try: stdscr.addstr( - currentLine + i, displayNamestartingPos + - maxDisplayName + 3, lineByLineText[i], + currentLine + i, + displayNamestartingPos + maxDisplayName + 3, + lineByLineText[i], curses.A_BOLD ) except: @@ -340,36 +326,41 @@ def start(stdscr): else: # TODO: need to split this out to get proper underline + + currentLine -= linesNeeded if currentLine == size[0] - 2: stdscr.addstr( - currentLine, displayNamestartingPos + - maxDisplayName + 3, rawText, + currentLine, + displayNamestartingPos + maxDisplayName + 3, + rawText, curses.A_BOLD + curses.A_UNDERLINE ) else: stdscr.addstr( - currentLine, displayNamestartingPos + - maxDisplayName + 3, rawText, + currentLine, + displayNamestartingPos + maxDisplayName + 3, + rawText, curses.A_BOLD ) - usern = event["user_id"] + usern = event["sender"] if length > maxDisplayName: usern = usern[:maxDisplayName - 3] + "..." if event["content"]["msgtype"] == "m.emote": - usern = "* " + usern if currentLine == size[0] - 2: stdscr.addstr( - currentLine, displayNamestartingPos + max(0, maxDisplayName - length), + currentLine, + displayNamestartingPos + max(0, maxDisplayName - length), str(usern), curses.A_UNDERLINE + curses.A_BOLD ) else: stdscr.addstr( - currentLine, displayNamestartingPos + max(0, maxDisplayName - length), + currentLine, + displayNamestartingPos + max(0, maxDisplayName - length), str(usern), curses.A_BOLD ) @@ -377,13 +368,15 @@ def start(stdscr): usern = "<" + usern + ">" if currentLine == size[0] - 2: stdscr.addstr( - currentLine, displayNamestartingPos + max(0, maxDisplayName - length), + currentLine, + displayNamestartingPos + max(0, maxDisplayName - length), str(usern), curses.A_UNDERLINE ) else: stdscr.addstr( - currentLine, displayNamestartingPos + max(0, maxDisplayName - length), + currentLine, + displayNamestartingPos + max(0, maxDisplayName - length), str(usern) ) @@ -417,12 +410,13 @@ def start(stdscr): elif event["content"]["membership"] == "leave": buf = " has left" + currentLine -= 1 if length > maxDisplayName: if currentLine == size[0] - 2: stdscr.addstr( currentLine, displayNamestartingPos + 1, - str(event["user_id"]), + str(event["sender"]), curses.A_DIM + curses.A_UNDERLINE ) stdscr.addstr( @@ -435,7 +429,7 @@ def start(stdscr): stdscr.addstr( currentLine, displayNamestartingPos + 1, - str(event["user_id"]), + str(event["sender"]), curses.A_DIM ) stdscr.addstr( @@ -451,7 +445,7 @@ def start(stdscr): currentLine, displayNamestartingPos + 1 + maxDisplayName - length, - str(event["user_id"]), + str(event["sender"]), curses.A_DIM + curses.A_UNDERLINE ) stdscr.addstr( @@ -465,7 +459,7 @@ def start(stdscr): currentLine, displayNamestartingPos + 1 + maxDisplayName - length, - str(event["user_id"]), + str(event["sender"]), curses.A_DIM ) stdscr.addstr( @@ -525,11 +519,11 @@ def start(stdscr): if c == 9: #stdscr.addstr(1, 0, "%s was pressed\n" % c) - room = room_keys[nextRoom] - nextRoom = (nextRoom + 1) % len(rooms) + log("key pressed: 0x{:X}".format(c)) + room = room_keys[(room_keys.index(room)+1) % len(rooms)] the_room_to_post_to = None elif c == 10: # enter - with open('sends.log', 'a') as the_file: + with open('redpill-sends.log', 'a') as the_file: the_file.write("the_room_to_post_to:" + str(the_room_to_post_to) + "\n") the_file.write("lastEventRoom: " + str(lastEventRoom) + "\n") the_file.write("room: " + str(room) + "\n") @@ -676,7 +670,13 @@ def start(stdscr): main_screen.keypad(1) curses.start_color() - start(main_screen) + try: + start(main_screen) + except: + import traceback + with open('redpill-exception.log', 'a') as f: + traceback.print_exc(file=f) + raise except curses.error: curses.nocbreak()