diff options
-rw-r--r-- | src/core.py | 201 | ||||
-rw-r--r-- | src/multiuserchat.py | 5 | ||||
-rw-r--r-- | src/room.py | 139 | ||||
-rw-r--r-- | src/tabs.py | 567 | ||||
-rw-r--r-- | src/text_buffer.py | 6 | ||||
-rw-r--r-- | src/windows.py | 47 |
6 files changed, 458 insertions, 507 deletions
diff --git a/src/core.py b/src/core.py index e1bae799..ac8842e7 100644 --- a/src/core.py +++ b/src/core.py @@ -43,7 +43,6 @@ from data_forms import DataFormsTab from config import config, options from logger import logger from user import User -from room import Room from roster import Roster, RosterGroup, roster from contact import Contact, Resource from text_buffer import TextBuffer @@ -295,6 +294,9 @@ class Core(object): return self.information_buffer def grow_information_win(self, nb=1): + if self.information_win_size >= self.current_tab().height -5 or \ + self.information_win_size+nb >= self.current_tab().height-4: + return if self.information_win_size == 14: return self.information_win_size += nb @@ -397,10 +399,10 @@ class Core(object): nick = message['mucnick'] room_from = message.getMucroom() tab = self.get_tab_by_name(room_from, tabs.MucTab) - if tab and tab.get_room() and tab.get_room().get_user_by_name(nick): - tab.get_room().get_user_by_name(nick).chatstate = state + if tab and tab.get_user_by_name(nick): + tab.get_user_by_name(nick).chatstate = state if tab == self.current_tab(): - tab.user_win.refresh(tab._room.users) + tab.user_win.refresh(tab.users) tab.input.refresh() self.doupdate() @@ -419,7 +421,6 @@ class Core(object): logger.log_roster_change(jid.bare, 'got offline') if not contact: return - log.debug('on_got_offline: %s' % presence) resource = contact.get_resource_by_fulljid(jid.full) if not resource: return @@ -465,7 +466,7 @@ class Core(object): """ tab = self.get_tab_by_name(jid, tabs.ConversationTab) if tab: - self.add_message_to_text_buffer(tab.get_room(), msg) + self.add_message_to_text_buffer(tab._text_buffer, msg) def on_failed_connection(self): """ @@ -479,7 +480,7 @@ class Core(object): """ for tab in self.tabs: if isinstance(tab, tabs.MucTab): - tab.get_room().disconnect() + tab.disconnect() self.information(_("Disconnected from server.")) def on_failed_auth(self, event): @@ -580,7 +581,7 @@ class Core(object): def on_user_changed_status_in_private(self, jid, msg): tab = self.get_tab_by_name(jid) if tab: # display the message in private - tab.get_room().add_message(msg) + tab.add_message(msg) def on_message(self, message): """ @@ -606,16 +607,16 @@ class Core(object): jid = message['from'] nick_from = jid.resource room_from = jid.bare - room = self.get_room_by_name(jid.full) # get the tab with the private conversation - if not room: # It's the first message we receive: create the tab - room = self.open_private_window(room_from, nick_from, False) - if not room: + tab = self.get_tab_by_name(jid.full, tabs.PrivateTab) # get the tab with the private conversation + if not tab: # It's the first message we receive: create the tab + tab = self.open_private_window(room_from, nick_from, False) + if not tab: return body = xhtml.get_body_from_message_stanza(message) if not body: return - room.add_message(body, time=None, nickname=nick_from, - forced_user=self.get_room_by_name(room_from).get_user_by_name(nick_from)) + tab.add_message(body, time=None, nickname=nick_from, + forced_user=self.get_tab_by_name(room_from, tabs.MucTab).get_user_by_name(nick_from)) conversation = self.get_tab_by_name(jid.full, tabs.PrivateTab) if conversation and conversation.remote_wants_chatstates is None: if message['chat_state']: @@ -628,6 +629,7 @@ class Core(object): if conversation is self.current_tab(): self.refresh_window() else: + conversation.state = 'private' self.refresh_tab_win() def focus_tab_named(self, tab_name): @@ -669,7 +671,7 @@ class Core(object): remote_nick = roster.get_contact_by_jid(jid.bare).get_name() or jid.user else: remote_nick = jid.user - conversation.get_room().add_message(body, nickname=remote_nick, nick_color=get_theme().COLOR_REMOTE_USER) + conversation._text_buffer.add_message(body, nickname=remote_nick, nick_color=get_theme().COLOR_REMOTE_USER) if conversation.remote_wants_chatstates is None: if message['chat_state']: conversation.remote_wants_chatstates = True @@ -679,7 +681,7 @@ class Core(object): if 'private' in config.get('beep_on', 'highlight private').split(): curses.beep() if self.current_tab() is not conversation: - conversation.set_color_state(get_theme().COLOR_TAB_PRIVATE) + conversation.state = 'private' self.refresh_tab_win() else: self.refresh_window() @@ -741,7 +743,7 @@ class Core(object): roster.add_contact(contact, jid) roster.edit_groups_of_contact(contact, []) contact.set_ask('asked') - self.get_tab_by_number(0).set_color_state(get_theme().COLOR_TAB_HIGHLIGHT) + self.get_tab_by_number(0).state = 'highlight' self.information('%s wants to subscribe to your presence'%jid, 'Roster') if isinstance(self.current_tab(), tabs.RosterInfoTab): self.refresh_window() @@ -829,7 +831,7 @@ class Core(object): for tab in self.tabs: if isinstance(tab, tabs.ConversationTab): if tab.get_name() == jid: - return tab.get_room() + return tab return None def get_tab_by_name(self, name, typ=None): @@ -850,16 +852,6 @@ class Core(object): return tab return None - def get_room_by_name(self, name): - """ - returns the room that has this name - """ - for tab in self.tabs: - if (isinstance(tab, tabs.MucTab) or - isinstance(tab, tabs.PrivateTab)) and tab.get_name() == name: - return tab.get_room() - return None - def init_curses(self, stdscr): """ ncurses initialization @@ -892,7 +884,7 @@ class Core(object): """ Refresh everything """ - self.current_tab().set_color_state(get_theme().COLOR_TAB_CURRENT) + self.current_tab().state = 'current' self.current_tab().refresh() self.doupdate() @@ -921,8 +913,7 @@ class Core(object): """ Open a new tab.MucTab containing a muc Room, using the specified nick """ - r = Room(room, nick) - new_tab = tabs.MucTab(r) + new_tab = tabs.MucTab(room, nick) self.add_tab(new_tab, focus) self.refresh_window() @@ -940,19 +931,19 @@ class Core(object): - A Muc with any new message """ for tab in self.tabs: - if tab.get_color_state() == get_theme().COLOR_TAB_PRIVATE: + if tab.state == 'private': self.command_win('%s' % tab.nb) return for tab in self.tabs: - if tab.get_color_state() == get_theme().COLOR_TAB_HIGHLIGHT: + if tab.state == 'highlight': self.command_win('%s' % tab.nb) return for tab in self.tabs: - if tab.get_color_state() == get_theme().COLOR_TAB_NEW_MESSAGE: + if tab.state == 'message': self.command_win('%s' % tab.nb) return for tab in self.tabs: - if tab.get_color_state() == get_theme().COLOR_TAB_DISCONNECTED: + if tab.state == 'disconnected': self.command_win('%s' % tab.nb) return for tab in self.tabs: @@ -1009,20 +1000,20 @@ class Core(object): def room_error(self, error, room_name): """ - Display the error on the room window + Display the error in the tab """ - room = self.get_room_by_name(room_name) + tab = self.get_tab_by_name(room_name) error_message = self.get_error_message_from_error_stanza(error) - self.add_message_to_text_buffer(room, error_message) + self.add_message_to_text_buffer(tab._text_buffer, error_message) code = error['error']['code'] if code == '401': msg = _('To provide a password in order to join the room, type "/join / password" (replace "password" by the real password)') - self.add_message_to_text_buffer(room, msg) + self.add_message_to_text_buffer(tab._text_buffer, msg) if code == '409': if config.get('alternative_nickname', '') != '': - self.command_join('%s/%s'% (room.name, room.own_nick+config.get('alternative_nickname', ''))) + self.command_join('%s/%s'% (tab.name, tab.own_nick+config.get('alternative_nickname', ''))) else: - self.add_message_to_text_buffer(room, _('You can join the room with an other nick, by typing "/join /other_nick"')) + self.add_message_to_text_buffer(tab._text_buffer, _('You can join the room with an other nick, by typing "/join /other_nick"')) self.refresh_window() def open_conversation_window(self, jid, focus=True): @@ -1036,6 +1027,8 @@ class Core(object): return tab new_tab = tabs.ConversationTab(jid) # insert it in the rooms + if not focus: + new_tab.state = "private" self.add_tab(new_tab, focus) self.refresh_window() return new_tab @@ -1046,19 +1039,18 @@ class Core(object): if isinstance(tab, tabs.PrivateTab): if tab.get_name() == complete_jid: self.command_win('%s' % tab.nb) - return tab.get_room() + return tab # create the new tab - room = self.get_room_by_name(room_name) - if not room: + tab = self.get_tab_by_name(room_name, tabs.MucTab) + if not tab: return None - own_nick = room.own_nick - r = Room(complete_jid, own_nick) # PrivateRoom here - new_tab = tabs.PrivateTab(r) + new_tab = tabs.PrivateTab(complete_jid, tab.own_nick) + if not focus: + new_tab.state = "private" # insert it in the tabs self.add_tab(new_tab, focus) - # self.window.new_room(r) self.refresh_window() - return r + return new_tab def on_groupchat_subject(self, message): """ @@ -1066,15 +1058,15 @@ class Core(object): """ nick_from = message['mucnick'] room_from = message.getMucroom() - room = self.get_room_by_name(room_from) + tab = self.get_tab_by_name(room_from, tabs.MucTab) subject = message['subject'] - if not subject or not room: + if not subject or not tab: return if nick_from: - self.add_message_to_text_buffer(room, _("%(nick)s set the subject to: %(subject)s") % {'nick':nick_from, 'subject':subject}, time=None) + self.add_message_to_text_buffer(tab._text_buffer, _("%(nick)s set the subject to: %(subject)s") % {'nick':nick_from, 'subject':subject}, time=None) else: - self.add_message_to_text_buffer(room, _("The subject is: %(subject)s") % {'subject':subject}, time=None) - room.topic = subject + self.add_message_to_text_buffer(tab._text_buffer, _("The subject is: %(subject)s") % {'subject':subject}, time=None) + tab.topic = subject if self.get_tab_by_name(room_from, tabs.MucTab) is self.current_tab(): self.refresh_window() @@ -1102,34 +1094,33 @@ class Core(object): room_from = message.getMucroom() if message['type'] == 'error': # Check if it's an error return self.room_error(message, room_from) - room = self.get_room_by_name(room_from) tab = self.get_tab_by_name(room_from, tabs.MucTab) - if tab and tab.get_room() and tab.get_room().get_user_by_name(nick_from) and\ - tab.get_room().get_user_by_name(nick_from) in tab.ignores: - return - if not room: + if not tab: self.information(_("message received for a non-existing room: %s") % (room_from)) return + if tab.get_user_by_name(nick_from) and\ + tab.get_user_by_name(nick_from) in tab.ignores: + return body = xhtml.get_body_from_message_stanza(message) if body: date = date if delayed == True else None - self.add_message_to_text_buffer(room, body, date, nick_from, history=True if date else False) + tab.add_message(body, date, nick_from, history=True if date else False) if tab is self.current_tab(): - tab.text_win.refresh(tab._room) - tab.info_header.refresh(tab._room, tab.text_win) + tab.text_win.refresh() + tab.info_header.refresh(tab, tab.text_win) self.refresh_tab_win() if 'message' in config.get('beep_on', 'highlight private').split(): curses.beep() - def add_message_to_text_buffer(self, room, txt, time=None, nickname=None, history=None): + def add_message_to_text_buffer(self, buff, txt, time=None, nickname=None, history=None): """ Add the message to the room if possible, else, add it to the Info window (in the Info tab of the info window in the RosterTab) """ - if not room: + if not buff: self.information('Trying to add a message in no room: %s' % txt, 'Error') else: - room.add_message(txt, time, nickname, history=history) + buff.add_message(txt, time, nickname, history=history) def command_help(self, arg): """ @@ -1179,12 +1170,14 @@ class Core(object): pres['type'] = show pres.send() current = self.current_tab() - if isinstance(current, tabs.MucTab) and current.get_room().joined: + if isinstance(current, tabs.MucTab) and current.joined and show in ('away', 'xa'): current.send_chat_state('inactive') for tab in self.tabs: - if isinstance(tab, tabs.MucTab) and tab.get_room().joined: - muc.change_show(self.xmpp, tab.get_room().name, tab.get_room().own_nick, show, msg) + if isinstance(tab, tabs.MucTab) and tab.joined: + muc.change_show(self.xmpp, tab.name, tab.own_nick, show, msg) self.set_status(show, msg) + if isinstance(current, tabs.MucTab) and current.joined and show not in ('away', 'xa'): + current.send_chat_state('active') def completion_status(self, the_input): return the_input.auto_completion([status for status in possible_show], ' ') @@ -1228,18 +1221,20 @@ class Core(object): """ /version <jid> """ + def callback(res): + if not res: + return self.information('Could not get the software version from %s' % (jid,), 'Warning') + version = '%s is running %s version %s on %s' % (jid, + res.get('name') or _('an unknown software'), + res.get('version') or _('unknown'), + res.get('os') or _('on an unknown platform')) + self.information(version, 'Info') + args = common.shell_split(arg) if len(args) < 1: return self.command_help('version') jid = args[0] - res = self.xmpp.plugin['xep_0092'].get_version(jid) - if not res: - return self.information('Could not get the software version from %s' % (jid,), 'Warning') - version = '%s is running %s version %s on %s' % (jid, - res.get('name') or _('an unknown software'), - res.get('version') or _('unknown'), - res.get('os') or _('on an unknown platform')) - self.information(version, 'Info') + self.xmpp.plugin['xep_0092'].get_version(jid, callback=callback) def command_reconnect(self, args): """ @@ -1359,11 +1354,11 @@ class Core(object): args = common.shell_split(arg) password = None if len(args) == 0: - t = self.current_tab() - if not isinstance(t, tabs.MucTab) and not isinstance(t, tabs.PrivateTab): + tab = self.current_tab() + if not isinstance(tab, tabs.MucTab) and not isinstance(tab, tabs.PrivateTab): return - room = JID(t.get_name()).bare - nick = t.get_room().own_nick + room = JID(tab.get_name()).bare + nick = tab.own_nick else: info = JID(args[0]) if info.resource == '': @@ -1374,12 +1369,12 @@ class Core(object): else: nick = info.resource if info.bare == '': # happens with /join /nickname, which is OK - t = self.current_tab() - if not isinstance(t, tabs.MucTab): + tab = self.current_tab() + if not isinstance(tab, tabs.MucTab): return - room = t.get_name() + room = tab.get_name() if nick == '': - nick = t.get_room().own_nick + nick = tab.own_nick else: room = info.bare if room.find('@') == -1: # no server is provided, like "/join hello" @@ -1392,25 +1387,25 @@ class Core(object): self.information(_("You didn't specify a server for the room you want to join"), 'Error') return room = room.lower() - r = self.get_room_by_name(room) + tab = self.get_tab_by_name(room, tabs.MucTab) if len(args) == 2: # a password is provided password = args[1] - if r and r.joined: # if we are already in the room - self.focus_tab_named(r.name) + if tab and tab.joined: # if we are already in the room + self.focus_tab_named(tab.name) return if room.startswith('@'): room = room[1:] current_status = self.get_status() - if r and not r.joined: + if tab and not tab.joined: muc.join_groupchat(self.xmpp, room, nick, password, histo_length, current_status.message, current_status.show) - if not r: # if the room window exists, we don't recreate it. + if not tab: self.open_new_room(room, nick) muc.join_groupchat(self.xmpp, room, nick, password, histo_length, current_status.message, current_status.show) else: - r.own_nick = nick - r.users = [] + tab.own_nick = nick + tab.users = [] self.enable_private_tabs(room) def get_bookmark_nickname(self, room_name): @@ -1434,10 +1429,10 @@ class Core(object): if len(args) == 0 and not isinstance(self.current_tab(), tabs.MucTab): return if len(args) == 0: - room = self.current_tab().get_room() - roomname = self.current_tab().get_name() - if room.joined: - nick = room.own_nick + tab = self.current_tab() + roomname = tab.get_name() + if tab.joined: + nick = tab.own_nick else: info = JID(args[0]) if info.resource != '': @@ -1519,9 +1514,9 @@ class Core(object): return for tab in self.tabs: if isinstance(tab, tabs.MucTab) and JID(tab.get_name()).domain == domain: - if tab.get_room().joined: - muc.leave_groupchat(tab.core.xmpp, tab.get_name(), tab.get_room().own_nick, message) - tab.get_room().joined = False + if tab.joined: + muc.leave_groupchat(tab.core.xmpp, tab.get_name(), tab.own_nick, message) + tab.joined = False self.command_join(tab.get_name()) def command_bind(self, arg): @@ -1578,17 +1573,18 @@ class Core(object): self.pop_information_win_up(nb_lines, popup_time) else: if self.information_win_size != 0: - self.information_win.refresh(self.information_buffer) + self.information_win.refresh() self.current_tab().input.refresh() - def disconnect(self, msg=None, reconnect=False): + def disconnect(self, msg='', reconnect=False): """ Disconnect from remote server and correctly set the states of all parts of the client (for example, set the MucTabs as not joined, etc) """ + msg = msg or '' for tab in self.tabs: - if isinstance(tab, tabs.MucTab): - muc.leave_groupchat(self.xmpp, tab.get_room().name, tab.get_room().own_nick, msg) + if isinstance(tab, tabs.MucTab) and tab.joined: + tab.command_part(msg) roster.empty() self.save_config() # Ugly fix thanks to gmail servers @@ -1637,7 +1633,6 @@ class Core(object): def remove_timed_event(self, event): if event and event in self.timed_events: - log.debug('removing event') self.timed_events.remove(event) def add_timed_event(self, event): diff --git a/src/multiuserchat.py b/src/multiuserchat.py index 264f0e4a..ac910837 100644 --- a/src/multiuserchat.py +++ b/src/multiuserchat.py @@ -65,7 +65,10 @@ def leave_groupchat(xmpp, jid, own_nick, msg): """ Leave the groupchat """ - xmpp.plugin['xep_0045'].leaveMUC(jid, own_nick, msg) + try: + xmpp.plugin['xep_0045'].leaveMUC(jid, own_nick, msg) + except KeyError: + log.debug("WARNING: in muc.leave_groupchat: could not leave the room") def set_user_role(xmpp, jid, nick, reason, role): """ diff --git a/src/room.py b/src/room.py deleted file mode 100644 index b97dd0b6..00000000 --- a/src/room.py +++ /dev/null @@ -1,139 +0,0 @@ -# Copyright 2010-2011 Florent Le Coz <louiz@louiz.org> -# -# This file is part of Poezio. -# -# Poezio is free software: you can redistribute it and/or modify -# it under the terms of the zlib license. See the COPYING file. - -from text_buffer import TextBuffer, Message -from datetime import datetime -from random import randrange -from config import config -from logger import logger - -import common -from theming import get_theme - -import logging -import curses - -log = logging.getLogger(__name__) - -class Room(TextBuffer): - def __init__(self, name, nick, messages_nb_limit=config.get('max_messages_in_memory', 2048)): - TextBuffer.__init__(self, messages_nb_limit) - self.name = name - self.own_nick = nick - self.color_state = get_theme().COLOR_TAB_NORMAL # color used in RoomInfo - self.joined = False # false until self presence is receied - self.users = [] # User objects - self.topic = '' - - def disconnect(self): - """ - Set the state of the room as not joined, so - we can know if we can join it, send messages to it, etc - """ - self.users = [] - self.color_state = get_theme().COLOR_TAB_DISCONNECTED - self.joined = False - - def get_single_line_topic(self): - """ - Return the topic as a single-line string (for the window header) - """ - return self.topic.replace('\n', '|') - - def log_message(self, txt, time, nickname): - """ - Log the messages in the archives, if it needs - to be - """ - if time is None and self.joined: # don't log the history messages - logger.log_message(self.name, nickname, txt) - - def do_highlight(self, txt, time, nickname): - """ - Set the tab color and returns the nick color - """ - color = None - if not time and nickname and nickname != self.own_nick and self.joined: - if self.own_nick.lower() in txt.lower(): - if self.color_state != get_theme().COLOR_TAB_CURRENT: - self.set_color_state(get_theme().COLOR_TAB_HIGHLIGHT) - color = get_theme().COLOR_HIGHLIGHT_NICK - else: - highlight_words = config.get('highlight_on', '').split(':') - for word in highlight_words: - if word and word.lower() in txt.lower(): - if self.color_state != get_theme().COLOR_TAB_CURRENT: - self.set_color_state(get_theme().COLOR_TAB_HIGHLIGHT) - color = get_theme().COLOR_HIGHLIGHT_NICK - break - if color: - beep_on = config.get('beep_on', 'highlight private').split() - if 'highlight' in beep_on and 'message' not in beep_on: - curses.beep() - return color - - def get_user_by_name(self, nick): - """ - Gets the user associated with the given nick, or None if not found - """ - for user in self.users: - if user.nick == nick: - return user - return None - - def set_color_state(self, color): - """ - Set the color that will be used to display the room's - number in the RoomInfo window - """ - self.color_state = color - - def add_message(self, txt, time=None, nickname=None, forced_user=None, nick_color=None, history=None): - """ - Note that user can be None even if nickname is not None. It happens - when we receive an history message said by someone who is not - in the room anymore - """ - self.log_message(txt, time, nickname) - special_message = False - if txt.startswith('/me '): - txt = "\x192}* \x195}" + nickname + ' ' + txt[4:] - special_message = True - user = self.get_user_by_name(nickname) if nickname is not None else None - if user: - user.set_last_talked(datetime.now()) - if not user and forced_user: - user = forced_user - if not time and nickname and\ - nickname != self.own_nick and\ - self.color_state != get_theme().COLOR_TAB_CURRENT: - if self.color_state != get_theme().COLOR_TAB_HIGHLIGHT: - self.set_color_state(get_theme().COLOR_TAB_NEW_MESSAGE) - nick_color = nick_color or None - if not nickname or time: - txt = '\x195}%s' % (txt,) - else: # TODO - highlight = self.do_highlight(txt, time, nickname) - if highlight: - nick_color = highlight - if special_message: - txt = '\x195}%s' % (txt,) - nickname = None - time = time or datetime.now() - message = Message(txt='%s\x19o'%(txt.replace('\t', ' '),), nick_color=nick_color, - time=time, str_time=time.strftime("%Y-%m-%d %H:%M:%S")\ - if history else time.strftime("%H:%M:%S"),\ - nickname=nickname, user=user) - while len(self.messages) > self.messages_nb_limit: - self.messages.pop(0) - self.messages.append(message) - for window in self.windows: # make the associated windows - # build the lines from the new message - nb = window.build_new_message(message, history=history) - if window.pos != 0: - window.scroll_up(nb) - return nb diff --git a/src/tabs.py b/src/tabs.py index d605d251..07652f74 100644 --- a/src/tabs.py +++ b/src/tabs.py @@ -13,8 +13,8 @@ Each Tab object has different refresh() and resize() methods, defining how its Windows are displayed, resized, etc """ -MIN_WIDTH = 50 -MIN_HEIGHT = 22 +MIN_WIDTH = 42 +MIN_HEIGHT = 6 import logging log = logging.getLogger(__name__) @@ -41,6 +41,7 @@ from sleekxmpp.xmlstream.stanzabase import JID from config import config from roster import RosterGroup, roster from contact import Contact, Resource +from text_buffer import TextBuffer from user import User from os import getenv, path from logger import logger @@ -57,12 +58,32 @@ SHOW_NAME = { NS_MUC_USER = 'http://jabber.org/protocol/muc#user' +STATE_COLORS = { + 'disconnected': lambda: get_theme().COLOR_TAB_DISCONNECTED, + 'message': lambda: get_theme().COLOR_TAB_NEW_MESSAGE, + 'highlight': lambda: get_theme().COLOR_TAB_HIGHLIGHT, + 'private': lambda: get_theme().COLOR_TAB_PRIVATE, + 'normal': lambda: get_theme().COLOR_TAB_NORMAL, + 'current': lambda: get_theme().COLOR_TAB_CURRENT, +# 'attention': lambda: get_theme().COLOR_TAB_ATTENTION, + } + +STATE_PRIORITY = { + 'normal': -1, + 'current': -1, + 'disconnected': 0, + 'message': 1, + 'highlight': 2, + 'private': 2, +# 'attention': 3 + } + class Tab(object): number = 0 tab_core = None def __init__(self): self.input = None - self._color_state = get_theme().COLOR_TAB_NORMAL + self._state = 'normal' self.need_resize = False self.nb = Tab.number Tab.number += 1 @@ -87,6 +108,25 @@ class Tab(object): def info_win(self): return self.core.information_win + @property + def color(self): + return STATE_COLORS[self._state]() + + @property + def state(self): + return self._state + + @state.setter + def state(self, value): + if not value in STATE_COLORS: + log.debug("WARNING: invalid value for tab state") + return + elif STATE_PRIORITY[value] < STATE_PRIORITY[self._state] and \ + value != 'current': + log.debug("WARNING: did not set status because of lower priority") + return + self._state = value + @staticmethod def resize(scr): Tab.size = (Tab.height, Tab.width) = scr.getmaxyx() @@ -167,18 +207,6 @@ class Tab(object): """ raise NotImplementedError - def get_color_state(self): - """ - returns the color that should be used in the GlobalInfoBar - """ - return self._color_state - - def set_color_state(self, color): - """ - set the color state - """ - pass - def get_name(self): """ get the name of the tab @@ -198,22 +226,13 @@ class Tab(object): """ called when this tab loses the focus. """ - self._color_state = get_theme().COLOR_TAB_NORMAL + self.state = 'normal' def on_gain_focus(self): """ called when this tab gains the focus. """ - self._color_state = get_theme().COLOR_TAB_CURRENT - - def add_message(self): - """ - Adds a message in the tab. - If the tab cannot add a message in itself (for example - FormTab, where text is not intented to be appened), it returns False. - If the tab can, it returns True - """ - return False + self.state = 'current' def on_scroll_down(self): """ @@ -258,9 +277,9 @@ class ChatTab(Tab): Also, ^M is already bound to on_enter And also, add the /say command """ - def __init__(self, room): + def __init__(self): Tab.__init__(self) - self._room = room + self._text_buffer = TextBuffer() self.remote_wants_chatstates = None # change this to True or False when # we know that the remote user wants chatstates, or not. # None means we don’t know yet, and we send only "active" chatstates @@ -286,7 +305,7 @@ class ChatTab(Tab): # build the list of the recent words char_we_dont_want = string.punctuation+' ' words = list() - for msg in self._room.messages[:-40:-1]: + for msg in self.messages[:-40:-1]: if not msg: continue txt = xhtml.clean_text(msg.txt) @@ -312,7 +331,7 @@ class ChatTab(Tab): """ Send an empty chatstate message """ - if not isinstance(self, MucTab) or self.get_room().joined: + if not isinstance(self, MucTab) or self.joined: if state in ('active', 'inactive', 'gone') and self.core.status.show in ('xa', 'away') and not always_send: return msg = self.core.xmpp.make_message(self.get_name()) @@ -371,7 +390,7 @@ class ChatTab(Tab): def move_separator(self): self.text_win.remove_line_separator() self.text_win.add_line_separator() - self.text_win.refresh(self._room) + self.text_win.refresh() self.input.refresh() def get_conversation_messages(self): @@ -386,15 +405,20 @@ class MucTab(ChatTab): It contains an userlist, an input, a topic, an information and a chat zone """ message_type = 'groupchat' - def __init__(self, room): - ChatTab.__init__(self, room) + def __init__(self, jid, nick): + ChatTab.__init__(self) + self.own_nick = nick + self.name = jid + self.joined = False + self.users = [] + self.topic = '' self.remote_wants_chatstates = True # We send active, composing and paused states to the MUC because # the chatstate may or may not be filtered by the MUC, # that’s not our problem. self.topic_win = windows.Topic() self.text_win = windows.TextWin() - room.add_window(self.text_win) + self._text_buffer.add_window(self.text_win) self.v_separator = windows.VerticalSeparator() self.user_win = windows.UserList() self.info_header = windows.MucInfoWin() @@ -427,19 +451,19 @@ class MucTab(ChatTab): def scroll_user_list_up(self): self.user_win.scroll_up() - self.user_win.refresh(self._room.users) + self.user_win.refresh(self.users) self.input.refresh() def scroll_user_list_down(self): self.user_win.scroll_down() - self.user_win.refresh(self._room.users) + self.user_win.refresh(self.users) self.input.refresh() def command_info(self, arg): args = common.shell_split(arg) if len(args) != 1: return self.core.information("Info command takes only 1 argument") - user = self.get_room().get_user_by_name(args[0]) + user = self.get_user_by_name(args[0]) if not user: return self.core.information("Unknown user: %s" % args[0]) info = '%s%s: show: %s, affiliation: %s, role: %s%s' % (args[0], @@ -474,35 +498,34 @@ class MucTab(ChatTab): """ /clear """ - self._room.messages = [] - self.text_win.rebuild_everything(self._room) + self.messages = [] + self.text_win.rebuild_everything(self._text_buffer) self.refresh() self.core.doupdate() def command_cycle(self, arg): - if self.get_room().joined: - muc.leave_groupchat(self.core.xmpp, self.get_name(), self.get_room().own_nick, arg) - self.get_room().disconnect() - self.core.disable_private_tabs(self.get_room().name) - self.core.command_join('"/%s"' % self.core.get_bookmark_nickname(self.get_room().name), '0') + if self.joined: + muc.leave_groupchat(self.core.xmpp, self.get_name(), self.own_nick, arg) + self.disconnect() + self.core.disable_private_tabs(self.name) + self.core.command_join('"/%s"' % self.core.get_bookmark_nickname(self.name), '0') self.user_win.pos = 0 def command_recolor(self, arg): """ Re-assign color to the participants of the room """ - room = self.get_room() compare_users = lambda x: x.last_talked - users = list(room.users) + users = list(self.users) # search our own user, to remove it from the room for user in users: - if user.nick == room.own_nick: + if user.nick == self.own_nick: users.remove(user) nb_color = len(get_theme().LIST_COLOR_NICKNAMES) for i, user in enumerate(sorted(users, key=compare_users, reverse=True)): user.color = get_theme().LIST_COLOR_NICKNAMES[i % nb_color] - self.text_win.rebuild_everything(self._room) - self.text_win.refresh(self._room) + self.text_win.rebuild_everything(self._text_buffer) + self.text_win.refresh() self.input.refresh() def command_version(self, arg): @@ -521,8 +544,8 @@ class MucTab(ChatTab): args = common.shell_split(arg) if len(args) < 1: return - if args[0] in [user.nick for user in self.get_room().users]: - jid = self._room.name + '/' + args[0] + if args[0] in [user.nick for user in self.users]: + jid = self.name + '/' + args[0] else: jid = args[0] self.core.xmpp.plugin['xep_0092'].get_version(jid, callback=callback) @@ -535,29 +558,27 @@ class MucTab(ChatTab): if len(args) != 1: return nick = args[0] - room = self.get_room() - if not room.joined: + if not self.joined: return current_status = self.core.get_status() - muc.change_nick(self.core.xmpp, room.name, nick, current_status.message, current_status.show) + muc.change_nick(self.core.xmpp, self.name, nick, current_status.message, current_status.show) def command_part(self, arg): """ /part [msg] """ args = arg.split() - room = self.get_room() if len(args): msg = ' '.join(args) else: msg = None - if self.get_room().joined: - muc.leave_groupchat(self.core.xmpp, room.name, room.own_nick, arg) - self.get_room().joined = False - self.get_room().add_message(_("\x195}You left the chatroom\x193}")) - self.refresh() + if self.joined: + muc.leave_groupchat(self.core.xmpp, self.name, self.own_nick, arg) + self.add_message(_("\x195}You left the chatroom\x193}")) + if self == self.core.current_tab(): + self.refresh() self.core.doupdate() - self.core.disable_private_tabs(self.get_room().name) + self.core.disable_private_tabs(self.name) def command_close(self, arg): """ @@ -575,11 +596,10 @@ class MucTab(ChatTab): if len(args) < 1: return nick = args[0] - room = self.get_room() r = None - for user in room.users: + for user in self.users: if user.nick == nick: - r = self.core.open_private_window(room.name, user.nick) + r = self.core.open_private_window(self.name, user.nick) if r and len(args) > 1: msg = arg[len(nick)+1:] self.core.current_tab().command_say(msg) @@ -591,23 +611,21 @@ class MucTab(ChatTab): /topic [new topic] """ if not arg.strip(): - self.core.add_message_to_text_buffer(self.get_room(), - _("The subject of the room is: %s") % self.get_room().topic) - self.text_win.refresh(self.get_room()) + self._text_buffer.add_message(_("The subject of the room is: %s") % self.topic) + self.text_win.refresh() self.input.refresh() return subject = arg - muc.change_subject(self.core.xmpp, self.get_room().name, subject) + muc.change_subject(self.core.xmpp, self.name, subject) def command_names(self, arg=None): """ /names """ - room = self.get_room() - if not room.joined: + if not self.joined: return users, visitors, moderators, participants, others = [], [], [], [], [] - for user in room.users: + for user in self.users: if user.role == 'visitor': visitors.append(user.nick) elif user.role == 'participant': @@ -629,12 +647,13 @@ class MucTab(ChatTab): message += '%s, ' % item message += '%s\n' % last - self.core.add_message_to_text_buffer(room, message) - self.text_win.refresh(self.get_room()) + # self.core.add_message_to_text_buffer(room, message) + self._text_buffer.add_message(message) + self.text_win.refresh() self.input.refresh() def completion_topic(self, the_input): - current_topic = self.get_room().topic + current_topic = self.topic return the_input.auto_completion([current_topic], '') def command_kick(self, arg): @@ -645,7 +664,7 @@ class MucTab(ChatTab): if not len(args): self.core.command_help('kick') else: - self.command_role('none '+arg) + self.command_role(arg+ ' none') def command_role(self, arg): """ @@ -662,7 +681,7 @@ class MucTab(ChatTab): reason = ' '.join(args[2:]) else: reason = '' - if not self.get_room().joined or \ + if not self.joined or \ not role in ('none', 'visitor', 'participant', 'moderator'): return res = muc.set_user_role(self.core.xmpp, self.get_name(), nick, reason, role) @@ -684,7 +703,7 @@ class MucTab(ChatTab): reason = ' '.join(args[2:]) else: reason = '' - if not self.get_room().joined or \ + if not self.joined or \ not affiliation in ('none', 'member', 'admin', 'owner'): # replace this ↑ with this ↓ when the ban list support is done # not affiliation in ('outcast', 'none', 'member', 'admin', 'owner'): @@ -717,7 +736,7 @@ class MucTab(ChatTab): self.core.command_help('ignore') return nick = args[0] - user = self._room.get_user_by_name(nick) + user = self.get_user_by_name(nick) if not user: self.core.information(_('%s is not in the room') % nick) elif user in self.ignores: @@ -735,7 +754,7 @@ class MucTab(ChatTab): self.core.command_help('unignore') return nick = args[0] - user = self._room.get_user_by_name(nick) + user = self.get_user_by_name(nick) if not user: self.core.information(_('%s is not in the room') % nick) elif user not in self.ignores: @@ -758,7 +777,7 @@ class MucTab(ChatTab): self.topic_win.resize(1, self.width, 0, 0) self.v_separator.resize(self.height-3, 1, 1, 9*(self.width//10)) self.text_win.resize(self.height-4-self.core.information_win_size, text_width, 1, 0) - self.text_win.rebuild_everything(self._room) + self.text_win.rebuild_everything(self._text_buffer) self.user_win.resize(self.height-3-self.core.information_win_size-1, self.width-text_width-1, 1, text_width+1) self.info_header.resize(1, self.width, self.height-3-self.core.information_win_size, 0) self.input.resize(1, self.width, self.height-1, 0) @@ -767,13 +786,13 @@ class MucTab(ChatTab): if self.need_resize: self.resize() log.debug(' TAB Refresh: %s'%self.__class__.__name__) - self.topic_win.refresh(self._room.get_single_line_topic()) - self.text_win.refresh(self._room) + self.topic_win.refresh(self.get_single_line_topic()) + self.text_win.refresh() self.v_separator.refresh() - self.user_win.refresh(self._room.users) - self.info_header.refresh(self._room, self.text_win) + self.user_win.refresh(self.users) + self.info_header.refresh(self, self.text_win) self.tab_win.refresh() - self.info_win.refresh(self.core.informations) + self.info_win.refresh() self.input.refresh() def on_input(self, key): @@ -794,8 +813,8 @@ class MucTab(ChatTab): # If we are not completing a command or a command's argument, complete a nick compare_users = lambda x: x.last_talked - word_list = [user.nick for user in sorted(self._room.users, key=compare_users, reverse=True)\ - if user.nick != self._room.own_nick] + word_list = [user.nick for user in sorted(self.users, key=compare_users, reverse=True)\ + if user.nick != self.own_nick] after = config.get('after_completion', ',')+" " input_pos = self.input.pos + self.input.line_pos if ' ' not in self.input.get_text()[:input_pos] or (self.input.last_completion and\ @@ -808,33 +827,30 @@ class MucTab(ChatTab): self.send_composing_chat_state(empty_after) def get_color_state(self): - return self._room.color_state + return self.color_state def set_color_state(self, color): - self._room.set_color_state(color) + self.set_color_state(color) def get_name(self): - return self._room.name + return self.name def get_text_window(self): return self.text_win - def get_room(self): - return self._room - def on_lose_focus(self): - self._room.set_color_state(get_theme().COLOR_TAB_NORMAL) + self.state = 'normal' self.text_win.remove_line_separator() self.text_win.add_line_separator() if config.get('send_chat_states', 'true') == 'true' and not self.input.get_text(): self.send_chat_state('inactive') def on_gain_focus(self): - self._room.set_color_state(get_theme().COLOR_TAB_CURRENT) + self.state = 'current' if self.text_win.built_lines and self.text_win.built_lines[-1] is None: self.text_win.remove_line_separator() curses.curs_set(1) - if self.get_room().joined and config.get('send_chat_states', 'true') == 'true' and not self.input.get_text(): + if self.joined and config.get('send_chat_states', 'true') == 'true' and not self.input.get_text(): self.send_chat_state('active') def on_scroll_up(self): @@ -864,91 +880,89 @@ class MucTab(ChatTab): role = presence['muc']['role'] jid = presence['muc']['jid'] typ = presence['type'] - room = self.get_room() - if not room.joined: # user in the room BEFORE us. + if not self.joined: # user in the room BEFORE us. # ignore redondant presence message, see bug #1509 - if from_nick not in [user.nick for user in room.users] and typ != "unavailable": + if from_nick not in [user.nick for user in self.users] and typ != "unavailable": new_user = User(from_nick, affiliation, show, status, role, jid) - room.users.append(new_user) - if from_nick == room.own_nick: - room.joined = True + self.users.append(new_user) + if from_nick == self.own_nick: + self.joined = True if self.core.current_tab() == self and self.core.status.show not in ('xa', 'away'): self.send_chat_state('active') new_user.color = get_theme().COLOR_OWN_NICK - room.add_message(_("\x195}Your nickname is \x193}%s") % (from_nick)) + self.add_message(_("\x195}Your nickname is \x193}%s") % (from_nick)) if '170' in status_codes: - room.add_message('\x191}Warning: \x195}this room is publicly logged') + self.add_message('\x191}Warning: \x195}this room is publicly logged') else: change_nick = '303' in status_codes kick = '307' in status_codes and typ == 'unavailable' ban = '301' in status_codes and typ == 'unavailable' - user = room.get_user_by_name(from_nick) + user = self.get_user_by_name(from_nick) # New user if not user: - self.on_user_join(room, from_nick, affiliation, show, status, role, jid) + self.on_user_join(from_nick, affiliation, show, status, role, jid) # nick change elif change_nick: - self.on_user_nick_change(room, presence, user, from_nick, from_room) + self.on_user_nick_change(presence, user, from_nick, from_room) elif ban: - self.on_user_banned(room, presence, user, from_nick) + self.on_user_banned(presence, user, from_nick) # kick elif kick: - self.on_user_kicked(room, presence, user, from_nick) + self.on_user_kicked(presence, user, from_nick) # user quit elif typ == 'unavailable': - self.on_user_leave_groupchat(room, user, jid, status, from_nick, from_room) + self.on_user_leave_groupchat(user, jid, status, from_nick, from_room) # status change else: - self.on_user_change_status(room, user, from_nick, from_room, affiliation, role, show, status) + self.on_user_change_status(user, from_nick, from_room, affiliation, role, show, status) if self.core.current_tab() is self: - self.text_win.refresh(self._room) - self.user_win.refresh(self._room.users) - self.info_header.refresh(self._room, self.text_win) + self.text_win.refresh() + self.user_win.refresh(self.users) + self.info_header.refresh(self, self.text_win) self.input.refresh() self.core.doupdate() - def on_user_join(self, room, from_nick, affiliation, show, status, role, jid): + def on_user_join(self, from_nick, affiliation, show, status, role, jid): """ When a new user joins the groupchat """ user = User(from_nick, affiliation, show, status, role, jid) - room.users.append(user) + self.users.append(user) hide_exit_join = config.get('hide_exit_join', -1) if hide_exit_join != 0: color = user.color[0] if config.get('display_user_color_in_join_part', '') == 'true' else 3 if not jid.full: - room.add_message('\x194}%(spec)s \x19%(color)d}%(nick)s\x195} joined the room' % {'nick':from_nick, 'color':color, 'spec':get_theme().CHAR_JOIN}) + self.add_message('\x194}%(spec)s \x19%(color)d}%(nick)s\x195} joined the room' % {'nick':from_nick, 'color':color, 'spec':get_theme().CHAR_JOIN}) else: - room.add_message('\x194}%(spec)s \x19%(color)d}%(nick)s \x195}(\x194}%(jid)s\x195}) joined the room' % {'spec':get_theme().CHAR_JOIN, 'nick':from_nick, 'color':color, 'jid':jid.full}) + self.add_message('\x194}%(spec)s \x19%(color)d}%(nick)s \x195}(\x194}%(jid)s\x195}) joined the room' % {'spec':get_theme().CHAR_JOIN, 'nick':from_nick, 'color':color, 'jid':jid.full}) self.core.on_user_rejoined_private_conversation(room.name, from_nick) - - def on_user_nick_change(self, room, presence, user, from_nick, from_room): + def on_user_nick_change(self, presence, user, from_nick, from_room): new_nick = presence.find('{%s}x/{%s}item' % (NS_MUC_USER, NS_MUC_USER)).attrib['nick'] - if user.nick == room.own_nick: - room.own_nick = new_nick + if user.nick == self.own_nick: + self.own_nick = new_nick # also change our nick in all private discussion of this room for _tab in self.core.tabs: - if isinstance(_tab, PrivateTab) and JID(_tab.get_name()).bare == room.name: - _tab.get_room().own_nick = new_nick + if isinstance(_tab, PrivateTab) and JID(_tab.get_name()).bare == self.name: + _tab.own_nick = new_nick user.change_nick(new_nick) color = user.color[0] if config.get('display_user_color_in_join_part', '') == 'true' else 3 - room.add_message('\x19%(color)d}%(old)s\x195} is now known as \x19%(color)d}%(new)s' % {'old':from_nick, 'new':new_nick, 'color':color}) + self.add_message('\x19%(color)d}%(old)s\x195} is now known as \x19%(color)d}%(new)s' % {'old':from_nick, 'new':new_nick, 'color':color}) # rename the private tabs if needed - self.core.rename_private_tabs(room.name, from_nick, new_nick) + self.core.rename_private_tabs(self.name, from_nick, new_nick) - def on_user_banned(self, room, presence, user, from_nick): + def on_user_banned(self, presence, user, from_nick): """ When someone is banned from a muc """ - room.users.remove(user) + self.users.remove(user) by = presence.find('{%s}x/{%s}item/{%s}actor' % (NS_MUC_USER, NS_MUC_USER, NS_MUC_USER)) reason = presence.find('{%s}x/{%s}item/{%s}reason' % (NS_MUC_USER, NS_MUC_USER, NS_MUC_USER)) by = by.attrib['jid'] if by is not None else None - if from_nick == room.own_nick: # we are banned - room.disconnect() - self.core.disable_private_tabs(room.name) + if from_nick == self.own_nick: # we are banned + self.disconnect() + self.core.disable_private_tabs(self.name) self.tab_win.refresh() self.core.doupdate() if by: @@ -963,19 +977,19 @@ class MucTab(ChatTab): kick_msg = _('\x191}%(spec)s \x19%(color)d}%(nick)s\x195} has been banned') % {'spec':get_theme().CHAR_KICK, 'nick':from_nick.replace('"', '\\"'), 'color':color} if reason is not None and reason.text: kick_msg += _('\x195} Reason: \x196}%(reason)s\x195}') % {'reason': reason.text} - room.add_message(kick_msg) + self._text_buffer.add_message(kick_msg) - def on_user_kicked(self, room, presence, user, from_nick): + def on_user_kicked(self, presence, user, from_nick): """ When someone is kicked from a muc """ - room.users.remove(user) + self.users.remove(user) by = presence.find('{%s}x/{%s}item/{%s}actor' % (NS_MUC_USER, NS_MUC_USER, NS_MUC_USER)) reason = presence.find('{%s}x/{%s}item/{%s}reason' % (NS_MUC_USER, NS_MUC_USER, NS_MUC_USER)) by = by.attrib['jid'] if by is not None else None - if from_nick == room.own_nick: # we are kicked - room.disconnect() - self.core.disable_private_tabs(room.name) + if from_nick == self.own_nick: # we are kicked + self.disconnect() + self.core.disable_private_tabs(self.name) self.tab_win.refresh() self.core.doupdate() if by: @@ -984,7 +998,7 @@ class MucTab(ChatTab): kick_msg = _('\x191}%(spec)s \x193}You\x195} have been kicked.') % {'spec':get_theme().CHAR_KICK} # try to auto-rejoin if config.get('autorejoin', 'false') == 'true': - muc.join_groupchat(self.core.xmpp, room.name, room.own_nick) + muc.join_groupchat(self.core.xmpp, self.name, self.own_nick) else: color = user.color[0] if config.get('display_user_color_in_join_part', '') == 'true' else 3 if by: @@ -993,16 +1007,16 @@ class MucTab(ChatTab): kick_msg = _('\x191}%(spec)s \x19%(color)d}%(nick)s\x195} has been kicked') % {'spec':get_theme().CHAR_KICK, 'nick':from_nick.replace('"', '\\"'), 'color':color} if reason is not None and reason.text: kick_msg += _('\x195} Reason: \x196}%(reason)s') % {'reason': reason.text} - room.add_message(kick_msg) + self.add_message(kick_msg) - def on_user_leave_groupchat(self, room, user, jid, status, from_nick, from_room): + def on_user_leave_groupchat(self, user, jid, status, from_nick, from_room): """ When an user leaves a groupchat """ - room.users.remove(user) - if room.own_nick == user.nick: + self.users.remove(user) + if self.own_nick == user.nick: # We are now out of the room. Happens with some buggy (? not sure) servers - room.disconnect() + self.disconnect() self.core.disable_private_tabs(from_room) self.tab_win.refresh() self.core.doupdate() @@ -1016,11 +1030,11 @@ class MucTab(ChatTab): leave_msg = _('\x191}%(spec)s \x19%(color)d}%(nick)s\x195} (\x194}%(jid)s\x195}) has left the room') % {'spec':get_theme().CHAR_QUIT, 'nick':from_nick, 'color':color, 'jid':jid.full} if status: leave_msg += ' (%s)' % status - room.add_message(leave_msg) + self.add_message(leave_msg) self.core.refresh_window() self.core.on_user_left_private_conversation(from_room, from_nick, status) - def on_user_change_status(self, room, user, from_nick, from_room, affiliation, role, show, status): + def on_user_change_status(self, user, from_nick, from_room, affiliation, role, show, status): """ When an user changes her status """ @@ -1028,7 +1042,7 @@ class MucTab(ChatTab): display_message = False # flag to know if something significant enough # to be displayed has changed color = user.color[0] if config.get('display_user_color_in_join_part', '') == 'true' else 3 - if from_nick == room.own_nick: + if from_nick == self.own_nick: msg = _('\x193}You\x195} changed: ') else: msg = _('\x19%(color)d}%(nick)s\x195} changed: ') % {'nick': from_nick.replace('"', '\\"'), 'color': color} @@ -1060,27 +1074,119 @@ class MucTab(ChatTab): hide_status_change = -1 if (hide_status_change == -1 or \ user.has_talked_since(hide_status_change) or\ - user.nick == room.own_nick)\ + user.nick == self.own_nick)\ and\ (affiliation != user.affiliation or\ role != user.role or\ show != user.show or\ status != user.status): # display the message in the room - room.add_message(msg) + self._text_buffer.add_message(msg) self.core.on_user_changed_status_in_private('%s/%s' % (from_room, from_nick), msg) # finally, effectively change the user status user.update(affiliation, show, status, role) + def disconnect(self): + """ + Set the state of the room as not joined, so + we can know if we can join it, send messages to it, etc + """ + self.users = [] + self.state = 'disconnected' + self.joined = False + + def get_single_line_topic(self): + """ + Return the topic as a single-line string (for the window header) + """ + return self.topic.replace('\n', '|') + + def log_message(self, txt, time, nickname): + """ + Log the messages in the archives, if it needs + to be + """ + if time is None and self.joined: # don't log the history messages + logger.log_message(self.name, nickname, txt) + + def do_highlight(self, txt, time, nickname): + """ + Set the tab color and returns the nick color + """ + color = None + if not time and nickname and nickname != self.own_nick and self.joined: + if self.own_nick.lower() in txt.lower(): + if self.state != 'current': + self.state = 'highlight' + color = get_theme().COLOR_HIGHLIGHT_NICK + else: + highlight_words = config.get('highlight_on', '').split(':') + for word in highlight_words: + if word and word.lower() in txt.lower(): + if self.state != 'current': + self.state = 'highlight' + color = get_theme().COLOR_HIGHLIGHT_NICK + break + if color: + beep_on = config.get('beep_on', 'highlight private').split() + if 'highlight' in beep_on and 'message' not in beep_on: + curses.beep() + return color + + def get_user_by_name(self, nick): + """ + Gets the user associated with the given nick, or None if not found + """ + for user in self.users: + if user.nick == nick: + return user + return None + + def add_message(self, txt, time=None, nickname=None, forced_user=None, nick_color=None, history=None): + """ + Note that user can be None even if nickname is not None. It happens + when we receive an history message said by someone who is not + in the room anymore + """ + self.log_message(txt, time, nickname) + special_message = False + if txt.startswith('/me '): + txt = "\x192}* \x195}" + nickname + ' ' + txt[4:] + special_message = True + user = self.get_user_by_name(nickname) if nickname is not None else None + if user: + user.set_last_talked(datetime.now()) + if not user and forced_user: + user = forced_user + if not time and nickname and\ + nickname != self.own_nick and\ + self.state != 'current': + if self.state != 'highlight': + self.state = 'message' + nick_color = nick_color or None + if not nickname or time: + txt = '\x195}%s' % (txt,) + else: # TODO + highlight = self.do_highlight(txt, time, nickname) + if highlight: + nick_color = highlight + if special_message: + txt = '\x195}%s' % (txt,) + nickname = None + time = time or datetime.now() + self._text_buffer.add_message(txt, time, nickname, nick_color, history, user) + class PrivateTab(ChatTab): """ The tab containg a private conversation (someone from a MUC) """ message_type = 'chat' - def __init__(self, room): - ChatTab.__init__(self, room) + def __init__(self, name, nick): + ChatTab.__init__(self) + self.own_nick = nick + self.name = name self.text_win = windows.TextWin() - room.add_window(self.text_win) + self._text_buffer.add_window(self.text_win) self.info_header = windows.PrivateInfoWin() self.input = windows.MessageInput() # keys @@ -1091,7 +1197,7 @@ class PrivateTab(ChatTab): self.commands['close'] = (self.command_unquery, _("Usage: /close\nClose: close the tab"), None) self.commands['version'] = (self.command_version, _('Usage: /version\nVersion: get the software version of the current interlocutor (usually its XMPP client and Operating System)'), None) self.resize() - self.parent_muc = self.core.get_tab_by_name(JID(room.name).bare, MucTab) + self.parent_muc = self.core.get_tab_by_name(JID(name).bare, MucTab) self.on = True def completion(self): @@ -1111,10 +1217,9 @@ class PrivateTab(ChatTab): needed = 'inactive' if self.core.status.show in ('xa', 'away') else 'active' msg['chat_state'] = needed msg.send() - self.core.add_message_to_text_buffer(self.get_room(), line, None, self.core.own_nick or self.get_room().own_nick) - logger.log_message(JID(self.get_name()).bare, self.core.own_nick, line) + self.core.add_message_to_text_buffer(self._text_buffer, line, None, self.core.own_nick or self.own_nick) self.cancel_paused_delay() - self.text_win.refresh(self._room) + self.text_win.refresh() self.input.refresh() def command_unquery(self, arg): @@ -1135,7 +1240,7 @@ class PrivateTab(ChatTab): res.get('version') or _('unknown'), res.get('os') or _('on an unknown platform')) self.core.information(version, 'Info') - jid = self.get_room().name + jid = self.name self.core.xmpp.plugin['xep_0092'].get_version(jid, callback=callback) def command_info(self, arg): @@ -1145,7 +1250,7 @@ class PrivateTab(ChatTab): if arg: self.parent_muc.command_info(arg) else: - user = JID(self.get_room().name).resource + user = JID(self.name).resource self.parent_muc.command_info(user) def resize(self): @@ -1153,7 +1258,7 @@ class PrivateTab(ChatTab): return self.need_resize = False self.text_win.resize(self.height-3-self.core.information_win_size, self.width, 0, 0) - self.text_win.rebuild_everything(self._room) + self.text_win.rebuild_everything(self._text_buffer) self.info_header.resize(1, self.width, self.height-3-self.core.information_win_size, 0) self.input.resize(1, self.width, self.height-1, 0) @@ -1161,27 +1266,18 @@ class PrivateTab(ChatTab): if self.need_resize: self.resize() log.debug(' TAB Refresh: %s'%self.__class__.__name__) - self.text_win.refresh(self._room) - self.info_header.refresh(self._room, self.text_win, self.chatstate) - self.info_win.refresh(self.core.informations) + self.text_win.refresh() + self.info_header.refresh(self.name, self.text_win, self.chatstate) + self.info_win.refresh() self.tab_win.refresh() self.input.refresh() def refresh_info_header(self): - self.info_header.refresh(self._room, self.text_win, self.chatstate) + self.info_header.refresh(self.name, self.text_win, self.chatstate) self.input.refresh() - def get_color_state(self): - if self._room.color_state == get_theme().COLOR_TAB_NORMAL or\ - self._room.color_state == get_theme().COLOR_TAB_CURRENT: - return self._room.color_state - return get_theme().COLOR_TAB_PRIVATE - - def set_color_state(self, color): - self._room.color_state = color - def get_name(self): - return self._room.name + return self.name def on_input(self, key): if key in self.key_func: @@ -1191,22 +1287,24 @@ class PrivateTab(ChatTab): if not self.on: return False empty_after = self.input.get_text() == '' or (self.input.get_text().startswith('/') and not self.input.get_text().startswith('//')) - tab = self.core.get_tab_by_name(JID(self.get_room().name).bare, MucTab) - if tab and tab.get_room().joined: + tab = self.core.get_tab_by_name(JID(self.name).bare, MucTab) + if tab and tab.joined: self.send_composing_chat_state(empty_after) return False def on_lose_focus(self): - self._room.set_color_state(get_theme().COLOR_TAB_NORMAL) + self.state = 'normal' self.text_win.remove_line_separator() self.text_win.add_line_separator() - if self.get_room().joined and config.get('send_chat_states', 'true') == 'true' and not self.input.get_text(): + tab = self.core.get_tab_by_name(JID(self.name).bare, MucTab) + if tab.joined and config.get('send_chat_states', 'true') == 'true' and not self.input.get_text(): self.send_chat_state('inactive') def on_gain_focus(self): - self._room.set_color_state(get_theme().COLOR_TAB_CURRENT) + self.state = 'current' curses.curs_set(1) - if self.get_room().joined and config.get('send_chat_states', 'true') == 'true' and not self.input.get_text(): + tab = self.core.get_tab_by_name(JID(self.name).bare, MucTab) + if tab.joined and config.get('send_chat_states', 'true') == 'true' and not self.input.get_text(): self.send_chat_state('active') def on_scroll_up(self): @@ -1221,9 +1319,6 @@ class PrivateTab(ChatTab): self.text_win.resize(self.height-3-self.core.information_win_size, self.width, 0, 0) self.info_header.resize(1, self.width, self.height-3-self.core.information_win_size, 0) - def get_room(self): - return self._room - def get_text_window(self): return self.text_win @@ -1232,9 +1327,9 @@ class PrivateTab(ChatTab): The user changed her nick in the corresponding muc: update the tab’s name and display a message. """ - self.get_room().add_message(_('"[%(old_nick)s]" is now known as "[%(new_nick)s]"') % {'old_nick':old_nick.replace('"', '\\"'), 'new_nick':new_nick.replace('"', '\\"')}) - new_jid = JID(self.get_room().name).bare+'/'+new_nick - self.get_room().name = new_jid + self.add_message('\x193}%(old)s\x195} is now known as \x193}%(new)s' % {'old':old_nick, 'new':new_nick}) + new_jid = JID(self.name).bare+'/'+new_nick + self.name = new_jid def user_left(self, status_message, from_nick): """ @@ -1242,9 +1337,9 @@ class PrivateTab(ChatTab): """ self.deactivate() if not status_message: - self.get_room().add_message(_('\x191}%(spec)s \x193}%(nick)s\x195} has left the room') % {'nick':from_nick.replace('"', '\\"'), 'spec':get_theme().CHAR_QUIT.replace('"', '\\"')}) + self.add_message(_('\x191}%(spec)s \x193}%(nick)s\x195} has left the room') % {'nick':from_nick.replace('"', '\\"'), 'spec':get_theme().CHAR_QUIT.replace('"', '\\"')}) else: - self.get_room().add_message(_('\x191}%(spec)s \x193}%(nick)s\x195} has left the room (%(status)s)"') % {'nick':from_nick.replace('"', '\\"'), 'spec':get_theme().CHAR_QUIT, 'status': status_message.replace('"', '\\"')}) + self.add_message(_('\x191}%(spec)s \x193}%(nick)s\x195} has left the room (%(status)s)"') % {'nick':from_nick.replace('"', '\\"'), 'spec':get_theme().CHAR_QUIT, 'status': status_message.replace('"', '\\"')}) if self.core.current_tab() is self: self.refresh() self.core.doupdate() @@ -1254,18 +1349,26 @@ class PrivateTab(ChatTab): The user (or at least someone with the same nick) came back in the MUC """ self.activate() - self.get_room().add_message('\x194}%(spec)s \x19%(color)d}%(nick)s\x195} joined the room' % {'nick':nick, 'color':user.color, 'spec':get_theme().CHAR_JOIN}) + tab = self.core.get_tab_by_name(JID(self.name).bare, MucTab) + color = None + if tab: + user = tab.get_user_by_name(nick) + if user: + color = user.color + self.add_message('\x194}%(spec)s \x19%(color)d}%(nick)s\x195} joined the room' % {'nick':nick, 'color': color or 3, 'spec':get_theme().CHAR_JOIN}) if self.core.current_tab() is self: self.refresh() self.core.doupdate() - def activate(self): self.on = True def deactivate(self): self.on = False + def add_message(self, txt, time=None, nickname=None, forced_user=None): + self._text_buffer.add_message(txt, time, nickname, None, None, forced_user) + class RosterInfoTab(Tab): """ A tab, splitted in two, containing the roster and infos @@ -1280,7 +1383,7 @@ class RosterInfoTab(Tab): self.contact_info_win = windows.ContactInfoWin() self.default_help_message = windows.HelpText("Enter commands with “/”. “o”: toggle offline show") self.input = self.default_help_message - self.set_color_state(get_theme().COLOR_TAB_NORMAL) + self.state = 'normal' self.key_func['^I'] = self.completion self.key_func[' '] = self.on_space self.key_func["/"] = self.on_slash @@ -1587,19 +1690,13 @@ class RosterInfoTab(Tab): self.v_separator.refresh() self.roster_win.refresh(roster) self.contact_info_win.refresh(self.roster_win.get_selected_row()) - self.information_win.refresh(self.core.informations) + self.information_win.refresh() self.tab_win.refresh() self.input.refresh() def get_name(self): return self.name - def get_color_state(self): - return self._color_state - - def set_color_state(self, color): - self._color_state = color - def on_input(self, key): if key == '^M': selected_row = self.roster_win.get_selected_row() @@ -1647,18 +1744,15 @@ class RosterInfoTab(Tab): return self.reset_help_message() def on_lose_focus(self): - self._color_state = get_theme().COLOR_TAB_NORMAL + self.state = 'normal' def on_gain_focus(self): - self._color_state = get_theme().COLOR_TAB_CURRENT + self.state = 'current' if isinstance(self.input, windows.HelpText): curses.curs_set(0) else: curses.curs_set(1) - def add_message(self): - return False - def move_cursor_down(self): if isinstance(self.input, windows.CommandInput): return @@ -1755,12 +1849,11 @@ class ConversationTab(ChatTab): """ message_type = 'chat' def __init__(self, jid): - txt_buff = text_buffer.TextBuffer() - ChatTab.__init__(self, txt_buff) - self.color_state = get_theme().COLOR_TAB_NORMAL + ChatTab.__init__(self) + self.state = 'normal' self._name = jid # a conversation tab is linked to one specific full jid OR bare jid self.text_win = windows.TextWin() - txt_buff.add_window(self.text_win) + self._text_buffer.add_window(self.text_win) self.upper_bar = windows.ConversationStatusMessageWin() self.info_header = windows.ConversationInfoWin() self.input = windows.MessageInput() @@ -1769,6 +1862,7 @@ class ConversationTab(ChatTab): # commands self.commands['unquery'] = (self.command_unquery, _("Usage: /unquery\nUnquery: close the tab"), None) self.commands['close'] = (self.command_unquery, _("Usage: /close\Close: close the tab"), None) + self.commands['version'] = (self.command_version, _('Usage: /version\nVersion: get the software version of the current interlocutor (usually its XMPP client and Operating System)'), None) self.resize() def completion(self): @@ -1786,21 +1880,36 @@ class ConversationTab(ChatTab): needed = 'inactive' if self.core.status.show in ('xa', 'away') else 'active' msg['chat_state'] = needed msg.send() - self.core.add_message_to_text_buffer(self.get_room(), line, None, self.core.own_nick) + self.core.add_message_to_text_buffer(self._text_buffer, line, None, self.core.own_nick) logger.log_message(JID(self.get_name()).bare, self.core.own_nick, line) self.cancel_paused_delay() - self.text_win.refresh(self._room) + self.text_win.refresh() self.input.refresh() def command_unquery(self, arg): self.core.close_tab() + def command_version(self, arg): + """ + /version + """ + def callback(res): + if not res: + return self.core.information('Could not get the software version from %s' % (jid,), 'Warning') + version = '%s is running %s version %s on %s' % (jid, + res.get('name') or _('an unknown software'), + res.get('version') or _('unknown'), + res.get('os') or _('on an unknown platform')) + self.core.information(version, 'Info') + jid = self._name + self.core.xmpp.plugin['xep_0092'].get_version(jid, callback=callback) + def resize(self): if self.core.information_win_size >= self.height-3 or not self.visible: return self.need_resize = False self.text_win.resize(self.height-4-self.core.information_win_size, self.width, 1, 0) - self.text_win.rebuild_everything(self._room) + self.text_win.rebuild_everything(self._text_buffer) self.upper_bar.resize(1, self.width, 0, 0) self.info_header.resize(1, self.width, self.height-3-self.core.information_win_size, 0) self.input.resize(1, self.width, self.height-1, 0) @@ -1809,26 +1918,17 @@ class ConversationTab(ChatTab): if self.need_resize: self.resize() log.debug(' TAB Refresh: %s'%self.__class__.__name__) - self.text_win.refresh(self._room) + self.text_win.refresh() self.upper_bar.refresh(self.get_name(), roster.get_contact_by_jid(self.get_name())) - self.info_header.refresh(self.get_name(), roster.get_contact_by_jid(self.get_name()), self._room, self.text_win, self.chatstate) - self.info_win.refresh(self.core.informations) + self.info_header.refresh(self.get_name(), roster.get_contact_by_jid(self.get_name()), self.text_win, self.chatstate) + self.info_win.refresh() self.tab_win.refresh() self.input.refresh() def refresh_info_header(self): - self.info_header.refresh(self.get_name(), roster.get_contact_by_jid(self.get_name()), self._room, self.text_win, self.chatstate) + self.info_header.refresh(self.get_name(), roster.get_contact_by_jid(self.get_name()), self.text_win, self.chatstate) self.input.refresh() - def get_color_state(self): - if self.color_state == get_theme().COLOR_TAB_NORMAL or\ - self.color_state == get_theme().COLOR_TAB_CURRENT: - return self.color_state - return get_theme().COLOR_TAB_PRIVATE - - def set_color_state(self, color): - self.color_state = color - def get_name(self): return self._name @@ -1842,14 +1942,14 @@ class ConversationTab(ChatTab): return False def on_lose_focus(self): - self.set_color_state(get_theme().COLOR_TAB_NORMAL) + self.state = 'normal' self.text_win.remove_line_separator() self.text_win.add_line_separator() if config.get('send_chat_states', 'true') == 'true' and not self.input.get_text() or not self.input.get_text().startswith('//'): self.send_chat_state('inactive') def on_gain_focus(self): - self.set_color_state(get_theme().COLOR_TAB_CURRENT) + self.state = 'current' curses.curs_set(1) if config.get('send_chat_states', 'true') == 'true' and not self.input.get_text() or not self.input.get_text().startswith('//'): self.send_chat_state('active') @@ -1866,9 +1966,6 @@ class ConversationTab(ChatTab): self.text_win.resize(self.height-4-self.core.information_win_size, self.width, 1, 0) self.info_header.resize(1, self.width, self.height-3-self.core.information_win_size, 0) - def get_room(self): - return self._room - def get_text_window(self): return self.text_win @@ -1884,7 +1981,7 @@ class MucListTab(Tab): """ def __init__(self, server): Tab.__init__(self) - self._color_state = get_theme().COLOR_TAB_NORMAL + self.state = 'normal' self.name = server self.upper_message = windows.Topic() self.upper_message.set_message('Chatroom list on server %s (Loading)' % self.name) @@ -1998,15 +2095,12 @@ class MucListTab(Tab): return self.key_func[key]() def on_lose_focus(self): - self._color_state = get_theme().COLOR_TAB_NORMAL + self.state = 'normal' def on_gain_focus(self): - self._color_state = get_theme().COLOR_TAB_CURRENT + self.state = 'current' curses.curs_set(0) - def get_color_state(self): - return self._color_state - def on_scroll_up(self): self.listview.scroll_up() @@ -2021,7 +2115,7 @@ class SimpleTextTab(Tab): """ def __init__(self, text): Tab.__init__(self) - self._color_state = get_theme().COLOR_TAB_NORMAL + self.state = 'normal' self.text_win = windows.SimpleTextWin(text) self.default_help_message = windows.HelpText("“Ctrl+q”: close") self.input = self.default_help_message @@ -2064,15 +2158,12 @@ class SimpleTextTab(Tab): self.input.refresh() def on_lose_focus(self): - self._color_state = get_theme().COLOR_TAB_NORMAL + self.state = 'normal' def on_gain_focus(self): - self._color_state = get_theme().COLOR_TAB_CURRENT + self.state = 'current' curses.curs_set(0) - def get_color_state(self): - return self._color_state - def diffmatch(search, string): """ Use difflib and a loop to check if search_pattern can diff --git a/src/text_buffer.py b/src/text_buffer.py index f39f147a..94bd94b9 100644 --- a/src/text_buffer.py +++ b/src/text_buffer.py @@ -34,19 +34,19 @@ class TextBuffer(object): def add_window(self, win): self.windows.append(win) - def add_message(self, txt, time=None, nickname=None, nick_color=None, history=None): + def add_message(self, txt, time=None, nickname=None, nick_color=None, history=None, user=None): time = time or datetime.now() msg = Message(txt='%s\x19o'%(txt.replace('\t', ' '),), nick_color=nick_color, time=time, str_time=time.strftime("%Y-%m-%d %H:%M:%S")\ if history else time.strftime("%H:%M:%S"),\ - nickname=nickname, user=None) + nickname=nickname, user=user) self.messages.append(msg) while len(self.messages) > self.messages_nb_limit: self.messages.pop(0) ret_val = None for window in self.windows: # make the associated windows # build the lines from the new message - nb = window.build_new_message(msg) + nb = window.build_new_message(msg, history=history) if ret_val is None: ret_val = nb if window.pos != 0: diff --git a/src/windows.py b/src/windows.py index 4f2c68c6..42c9bfa0 100644 --- a/src/windows.py +++ b/src/windows.py @@ -62,9 +62,10 @@ class Win(object): self._win = None def _resize(self, height, width, y, x): - self.height, self.width, self.x, self.y = height, width, x, y if height == 0 or width == 0: + self.height, self.width = height, width return + self.height, self.width, self.x, self.y = height, width, x, y if not self._win: self._win = curses.newwin(height, width, y, x) else: @@ -191,19 +192,19 @@ class UserList(Win): def __init__(self): Win.__init__(self) self.pos = 0 - self.color_role = {'moderator': get_theme().COLOR_USER_MODERATOR, - 'participant':get_theme().COLOR_USER_PARTICIPANT, - 'visitor':get_theme().COLOR_USER_VISITOR, - 'none':get_theme().COLOR_USER_NONE, - '':get_theme().COLOR_USER_NONE - } - self.color_show = {'xa':get_theme().COLOR_STATUS_XA, - 'none':get_theme().COLOR_STATUS_NONE, - '':get_theme().COLOR_STATUS_NONE, - 'dnd':get_theme().COLOR_STATUS_DND, - 'away':get_theme().COLOR_STATUS_AWAY, - 'chat':get_theme().COLOR_STATUS_CHAT - } + self.color_role = {'moderator': lambda: get_theme().COLOR_USER_MODERATOR, + 'participant': lambda: get_theme().COLOR_USER_PARTICIPANT, + 'visitor': lambda: get_theme().COLOR_USER_VISITOR, + 'none': lambda: get_theme().COLOR_USER_NONE, + '': lambda: get_theme().COLOR_USER_NONE + } + self.color_show = {'xa': lambda: get_theme().COLOR_STATUS_XA, + 'none': lambda: get_theme().COLOR_STATUS_NONE, + '': lambda: get_theme().COLOR_STATUS_NONE, + 'dnd': lambda: get_theme().COLOR_STATUS_DND, + 'away': lambda: get_theme().COLOR_STATUS_AWAY, + 'chat': lambda: get_theme().COLOR_STATUS_CHAT + } def scroll_up(self): self.pos += self.height-1 @@ -228,11 +229,11 @@ class UserList(Win): if not user.role in self.color_role: role_col = get_theme().COLOR_USER_NONE else: - role_col = self.color_role[user.role] + role_col = self.color_role[user.role]() if not user.show in self.color_show: show_col = get_theme().COLOR_STATUS_NONE else: - show_col = self.color_show[user.show] + show_col = self.color_show[user.show]() if user.chatstate == 'composing': char = 'X' elif user.chatstate == 'active': @@ -297,7 +298,7 @@ class GlobalInfoBar(Win): self.addstr(0, 0, "[", to_curses_attr(get_theme().COLOR_INFORMATION_BAR)) sorted_tabs = sorted(self.core.tabs, key=comp) for tab in sorted_tabs: - color = tab.get_color_state() + color = tab.color if config.get('show_inactive_tabs', 'true') == 'false' and\ color == get_theme().COLOR_TAB_NORMAL: continue @@ -340,18 +341,18 @@ class PrivateInfoWin(InfoWin): def __init__(self): InfoWin.__init__(self) - def refresh(self, room, window, chatstate): + def refresh(self, name, window, chatstate): log.debug('Refresh: %s'%self.__class__.__name__) with g_lock: self._win.erase() - self.write_room_name(room) + self.write_room_name(name) self.print_scroll_position(window) self.write_chatstate(chatstate) self.finish_line(get_theme().COLOR_INFORMATION_BAR) self._refresh() - def write_room_name(self, room): - jid = JID(room.name) + def write_room_name(self, name): + jid = JID(name) room_name, nick = jid.bare, jid.resource self.addstr(nick, to_curses_attr(get_theme().COLOR_PRIVATE_NAME)) txt = ' from room %s' % room_name @@ -379,7 +380,7 @@ class ConversationInfoWin(InfoWin): def __init__(self): InfoWin.__init__(self) - def refresh(self, jid, contact, text_buffer, window, chatstate): + def refresh(self, jid, contact, window, chatstate): # contact can be None, if we receive a message # from someone not in our roster. In this case, we display # only the maximum information from the message we can get. @@ -605,7 +606,7 @@ class TextWin(Win): self.built_lines.pop(0) return len(lines) - def refresh(self, room): + def refresh(self): log.debug('Refresh: %s'%self.__class__.__name__) if self.height <= 0: return |