def display_single_message(self, msg, author, unix_time): full_msg = "{0}\n{1}\n\n".format( strftime("%m-%d %H:%M", localtime(unix_time / 1000)), rfa(author)) for line in wrap(msg, self.o.cols): full_msg += rfa(line) full_msg += "\n" TextReader(full_msg, self.i, self.o, name="Matrix message display menu").activate()
def test_rfa(self): """Tests replace_filter_ascii, as well as add_character_replacement""" assert (rfa(u"may I have some lööps bröther") == "may I have some loops brother") # sneaky Russian letters assert (rfa(u"may I have some lооps brоther") == "may I have some loops brother") assert (rfa(u"may I have some lооps бrоther") == "may I have some loops brother") # Testing non-Unicode strings assert (rfa('\xc3\x85land Islands') == "Aland Islands") # And the corresponding Unicode string, just in case assert (rfa(u"Åland Islands") == "Aland Islands")
def change_wifi_country(self): with open('/usr/share/zoneinfo/iso3166.tab') as f: content = f.readlines() lc = [] current_country = self.get_current_wifi_country() for l in content: # Replace tabs with spaces and strip newlines l = l.replace('\t', ' ').strip() # Filter commented-out lines if l.startswith("#"): continue country_code, description = l.split(' ', 1) lc.append([rfa(l), country_code]) choice = Listbox(lc, self.i, self.o, name="WiFi country selection listbox", selected=current_country).activate() if choice: with LoadingBar(self.i, self.o, message="Changing WiFi country", name="WiFi country change LoadingBar"): result = self.set_wifi_country(choice) if result: Printer("Changed the country successfully!", self.i, self.o) return True else: Printer("Failed to change the country!", self.i, self.o) return False return None
def display_rooms(self): menu_contents = [] for r in self.rooms: current_room = self.rooms[r] # Count the amount of backfilled messages for each room self.stored_messages[current_room.room_id]["backfilled"] = 0 # Get the last 10 messages for each room and update stored_messages for e in reversed(current_room.get_events()): self._on_message(current_room, e) # self.stored_messages[current_room.room_id]["backfilled"] += 1 # Add an 'E' to the name of encrypted rooms room_name = "E " if current_room.encrypted else "" room_name += rfa(current_room.display_name) # Enter -> Read messages in room # Right arrow -> Write message to room menu_contents.append([ room_name, lambda r=current_room: self.display_messages(r), lambda r=current_room: self.display_room_members(r) ]) menu_contents.append(["Settings", self.show_settings]) Menu(menu_contents, self.i, self.o, name="Matrix app main menu").activate()
def about_supporters(): with open("SUPPORTERS.md", 'r') as f: supporters_md = f.read() lines = supporters_md.split('\n')[2:] supporter_names = "\n".join([rfa(line[3:]) for line in lines]) text = "ZeroPhone supporters:\n\n" + supporter_names TextReader(text, i, o, name="About supporters TextReader").activate()
def about_contributors(): with open("CONTRIBUTORS.md", 'r') as f: contributors_md = f.read() lines = contributors_md.split('\n')[2:] contributor_names = "\n".join([rfa(line[3:]) for line in lines]) text = "ZPUI contributors:\n\n" + contributor_names TextReader(text, i, o, name="About contributors TextReader").activate()
def _get_messages_menu_contents(self, room_id): # Sort messages by their timestamp sorted_messages = sorted(self.stored_messages[room_id]['events'], key=lambda k: k['timestamp']) menu_contents = [] for message in sorted_messages: if not self.config["show_join_leave_messages"] and message[ "type"] == "m.room.member": continue content = rfa(message["content"]) menu_contents.append([ content, lambda c=content, s=rfa(message['sender']), t=message[ 'timestamp']: self.display_single_message(c, s, t) ]) menu_contents.append([ "Write message", lambda r=self.rooms[room_id]: self.write_message(r) ]) return menu_contents
def display_messages(self, room): room_name = rfa(room.display_name) logger.debug(u"Viewing room: {}".format(room_name)) # Set the currently active room to this room, important for adding messages and refreshing the menu self.active_room = room.room_id cb = lambda x=room: self._handle_messages_top(x) # Create a menu to display the messages self.messages_menu = MessagesMenu( self._get_messages_menu_contents(room.room_id), self.i, self.o, name="Matrix MessageMenu for {}".format(room_name), entry_height=1, load_more_callback=lambda x=room: self._handle_messages_top(x)) self.messages_menu.activate()
def _on_message(self, room, event): if event["event_id"] in self.seen_events: logger.debug("Event {} seen, ignoring".format(event["event_id"])) return self.seen_events.append(event["event_id"]) logger.debug(u"New event: {}".format(event['type'])) event_type = event.get('type', "not_a_defined_event") # Check if a user joined the room if event_type == "m.room.member": content = event.get('content', {}) if event.get('membership', None) == "join": self._add_new_message( room.room_id, { 'timestamp': event.get('origin_server_ts', 0), 'type': event.get('type', 'unknown_type'), 'sender': unicode(rfa(event.get('sender', 'unknown_sender'))), 'content': rfa( unicode("+ {}").format( content.get('displayname', ''))), 'id': event.get('event_id', 0), 'membership': event.get('membership', None) }) elif event.get('membership', None) == "leave": # Leave events are linked to their corresponding join events, so instead of the key 'content' there is 'prev_content' # Sometimes there is also the key unsigned, will still need to investigate this further to make sure all cases are covered content = event.get('prev_content', {}) if content == {}: content = event.get('unsigned', {}) content = content.get('prev_content', {}) self._add_new_message( room.room_id, { 'timestamp': event.get('origin_server_ts', 0), 'type': event.get('type', 'unknown_type'), 'sender': unicode(rfa(event.get('sender', 'unknown_sender'))), 'content': rfa( unicode("- {}").format( content.get('displayname', ''))), 'id': event.get('event_id', 0), 'membership': event.get('membership', None) }) # Check for new messages elif event_type == "m.room.message": content = event.get('content', {}) if content.get('msgtype', None) == "m.text": prefix = "" if event.get( 'sender', None) == self.client.get_user().user_id or event.get( "sender", None) in self.config["your_other_usernames"]: # Prefix own messages with a '*' prefix = "* " elif self.config['displayname'] in content.get('body', ""): # Prefix messages with your name in it with a '#' prefix = "# " self._add_new_message( room.room_id, { 'timestamp': event.get('origin_server_ts', 0), 'type': event.get('type', 'unknown_type'), 'sender': unicode(rfa(event.get('sender', 'unknown_sender'))), 'content': unicode(prefix + rfa(content.get('body', ''))), 'id': event.get('event_id', 0) }) elif event_type == "not_a_defined_event": logger.warning("Unknown event: {}".format(event)) # Update the current view if required if self.active_room == room.room_id: self.messages_menu.set_contents( self._get_messages_menu_contents(room.room_id)) self.messages_menu.refresh() # Setting flag - if not already set if not self.has_processed_new_events.isSet(): logger.debug("New event processed, setting flag") self.has_processed_new_events.set()