From 77df6feb973f93a4f3cd834322757db8e72ddf1a Mon Sep 17 00:00:00 2001 From: "louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13" Date: Wed, 8 Sep 2010 23:00:55 +0000 Subject: remove one more possible ncurses glitch by using a muttex. Also correctly receive the error messages. And little cleanup --- src/connection.py | 13 ++++++++- src/gui.py | 52 +++++++++++++++++++++++++--------- src/keyboard.py | 2 +- src/poezio.py | 4 +-- src/window.py | 85 +++++++++++++++++++++++++++++++++---------------------- 5 files changed, 104 insertions(+), 52 deletions(-) diff --git a/src/connection.py b/src/connection.py index b00bc014..797205d9 100644 --- a/src/connection.py +++ b/src/connection.py @@ -37,8 +37,19 @@ class Connection(sleekxmpp.ClientXMPP): appropriate signals """ def __init__(self): - sleekxmpp.ClientXMPP.__init__(self, None, None, ssl=True, + if config.get('jid', ''): + self.anon = False # Field used to know if we are anonymous or not. + # many features will be handled diferently + # depending on this setting + jid = config.get('jid', '') + password = config.get('password', '') + else: # anonymous auth + self.anon = True + jid = None + password = None + sleekxmpp.ClientXMPP.__init__(self, jid, password, ssl=True, resource=config.get('resource', 'poezio')) + self.registerPlugin('xep_0045') def start(self): diff --git a/src/gui.py b/src/gui.py index f4a664d5..1317a2c7 100644 --- a/src/gui.py +++ b/src/gui.py @@ -150,6 +150,8 @@ class Gui(object): self.xmpp.add_event_handler("groupchat_presence", self.on_groupchat_presence) self.xmpp.add_event_handler("groupchat_message", self.on_groupchat_message) self.xmpp.add_event_handler("message", self.on_message) + self.xmpp.add_event_handler("presence", self.on_presence) + self.xmpp.add_event_handler("roster_update", self.on_roster_update) # self.handler = Handler() # self.handler.connect('on-connected', self.on_connected) # self.handler.connect('join-room', self.join_room) @@ -166,6 +168,11 @@ class Gui(object): self.information(_("Welcome on Poezio \o/!")) self.information(_("Your JID is %s") % self.xmpp.fulljid) + if not self.xmpp.anon: + # request the roster + self.xmpp.getRoster() + # send initial presence + self.xmpp.makePresence(pfrom=self.xmpp.jid).send() rooms = config.get('rooms', '') if rooms == '' or not isinstance(rooms, str): return @@ -184,6 +191,7 @@ class Gui(object): nick = default self.open_new_room(roomname, nick) muc.join_groupchat(self.xmpp, roomname, nick) + # if not self.xmpp.anon: # Todo: SEND VCARD return if config.get('jid', '') == '': # Don't send the vcard if we're not anonymous @@ -362,13 +370,18 @@ class Gui(object): When receiving private message from a muc OR a normal message (from one of our contacts) """ + from common import debug + debug('message: %s\n' % message) if message['type'] == 'groupchat': return None # Differentiate both type of messages, and call the appropriate handler. jid_from = message['from'] for room in self.rooms: if room.jid is None and room.name == jid_from.bare: # check all the MUC we are in - return self.on_groupchat_private_message(message) + if message['type'] == 'error': + return self.room_error(message, room.name) + else: + return self.on_groupchat_private_message(message) return self.on_normal_message(message) def on_groupchat_private_message(self, message): @@ -392,8 +405,28 @@ class Gui(object): """ When receiving "normal" messages (from someone in our roster) """ + from common import debug + debug('MESSAGE: %s\n' % (presence)) + + return + + def on_presence(self, presence): + """ + """ + from common import debug + debug('PRESEEEEEEEEENCE: %s\n' % (presence)) return + def on_roster_update(self, iq): + """ + A subscription changed, or we received a roster item + after a roster request, etc + """ + from common import debug + debug("UPDATE: %s\n" % (iq)) + for subscriber in iq['roster']['items']: + debug("subscriber: %s\n" % (iq['roster']['items'][subscriber]['subscription'])) + def resize_window(self): """ Resize the whole screen @@ -479,18 +512,6 @@ class Gui(object): """ Called when Tab is pressed, complete the nickname in the input """ - # def compare_users(a, b): - # """ - # Used to sort users by their last_talked - # """ - # if not a.last_talked and b.last_talked: - # return 0 - # elif not b.last_talked and a.last_talked: - # return 1 - # if a.last_talked < b.last_talked: - # return 1 - # else: - # return -1 compare_users = lambda x: x.last_talked self.window.input.auto_completion([user.nick for user in sorted(self.current_room().users, key=compare_users, reverse=True)]) @@ -563,6 +584,8 @@ class Gui(object): """ Display the error on the room window """ + from common import debug + debug('ERROR: %s\n' % error) room = self.get_room_by_name(room_name) if not room: room = self.get_room_by_name('Info') @@ -621,7 +644,6 @@ class Gui(object): """ Triggered whenever a message is received from a multi-user chat room. """ - # FIXME: not receiving subjects? :/ delay_tag = message.find('{urn:xmpp:delay}delay') if delay_tag is not None: delayed = True @@ -638,6 +660,8 @@ class Gui(object): date = None nick_from = message['mucnick'] room_from = message.getMucroom() + if message['type'] == 'error': # Check if it's an error + return self.room_error(message, from_room) if nick_from == room_from: nick_from = None room = self.get_room_by_name(room_from) diff --git a/src/keyboard.py b/src/keyboard.py index bf10ab8a..5f7411c1 100644 --- a/src/keyboard.py +++ b/src/keyboard.py @@ -42,7 +42,7 @@ def read_char(s): see http://en.wikipedia.org/wiki/UTF-8#Description """ (first, char) = get_next_byte(s) - if first == None: # Keyboard special, like KEY_HOME etc + if not isinstance(first, int): # Keyboard special, like KEY_HOME etc return char if first == 127 or first == 8: return "KEY_BACKSPACE" diff --git a/src/poezio.py b/src/poezio.py index 237f2571..9ce5952a 100644 --- a/src/poezio.py +++ b/src/poezio.py @@ -61,8 +61,8 @@ def exception_handler(type_, value, trace): except: # if an exception is raised but initscr has never been called yet pass traceback.print_exception(type_, value, trace, None, sys.stderr) - # import os # used to quit the program even from a thread - # os.abort() + import os # used to quit the program even from a thread + os.abort() sys.excepthook = exception_handler diff --git a/src/window.py b/src/window.py index 16820b8f..5979fc6d 100644 --- a/src/window.py +++ b/src/window.py @@ -54,6 +54,21 @@ class Win(object): def refresh(self): self.win.noutrefresh() + def addnstr(self, *args): + """ + addnstr is safe + """ + try: + self.win.addnstr(*args) + except: + pass + + def addstr(self, *args): + """ + addstr is not safe + """ + self.win.addstr(*args) + class UserList(Win): def __init__(self, height, width, y, x, parent_win, visible): Win.__init__(self, height, width, y, x, parent_win) @@ -82,10 +97,10 @@ class UserList(Win): role_col = self.color_role[user.role] show_col = self.color_show[user.show] self.win.attron(curses.color_pair(show_col)) - self.win.addnstr(y, 0, theme.CHAR_STATUS, 1) + self.addnstr(y, 0, theme.CHAR_STATUS, 1) self.win.attroff(curses.color_pair(show_col)) self.win.attron(curses.color_pair(role_col)) - self.win.addnstr(y, 1, user.nick, self.width-1) + self.addnstr(y, 1, user.nick, self.width-1) self.win.attroff(curses.color_pair(role_col)) y += 1 if y == self.height: @@ -113,7 +128,7 @@ class Topic(Win): g_lock.acquire() self.win.erase() if not jid: - self.win.addnstr(0, 0, topic[:self.width-1], curses.color_pair(theme.COLOR_TOPIC_BAR)) + self.addnstr(0, 0, topic[:self.width-1], curses.color_pair(theme.COLOR_TOPIC_BAR)) while True: try: self.win.addch(' ', curses.color_pair(theme.COLOR_TOPIC_BAR)) @@ -123,7 +138,7 @@ class Topic(Win): room = jid.split('/')[0] nick = '/'.join(jid.split('/')[1:]) topic = _('%(nick)s from room %(room)s' % {'nick': nick, 'room':room}) - self.win.addnstr(0, 0, topic + " "*(self.width-len(topic)), self.width-1 + self.addnstr(0, 0, topic + " "*(self.width-len(topic)), self.width-1 , curses.color_pair(theme.COLOR_PRIVATE_ROOM_BAR)) self.win.refresh() @@ -144,7 +159,8 @@ class RoomInfo(Win): down """ if current_room.pos > 0: - self.win.addstr(' -PLUS(%s)-' % current_room.pos, curses.color_pair(theme.COLOR_SCROLLABLE_NUMBER) | curses.A_BOLD) + plus = ' -PLUS(%s)-' % current_room.pos + self.addnstr(plus, len(plus), curses.color_pair(theme.COLOR_SCROLLABLE_NUMBER) | curses.A_BOLD) def refresh(self, rooms, current): if not self.visible: @@ -155,22 +171,22 @@ class RoomInfo(Win): comp = lambda x: x.nb g_lock.acquire() self.win.erase() - self.win.addnstr(0, 0, "[", self.width + self.addnstr(0, 0, "[", self.width ,curses.color_pair(theme.COLOR_INFORMATION_BAR)) sorted_rooms = sorted(rooms, key=comp) for room in sorted_rooms: color = room.color_state try: - self.win.addstr("%s" % str(room.nb), curses.color_pair(color)) - self.win.addstr("|", curses.color_pair(theme.COLOR_INFORMATION_BAR)) + self.addstr("%s" % str(room.nb), curses.color_pair(color)) + self.addstr("|", curses.color_pair(theme.COLOR_INFORMATION_BAR)) except: # end of line break (y, x) = self.win.getyx() - self.win.addstr(y, x-1, '] '+ current.name, curses.color_pair(theme.COLOR_INFORMATION_BAR)) -# self.print_scroll_position(current) + self.addstr(y, x-1, '] '+ current.name, curses.color_pair(theme.COLOR_INFORMATION_BAR)) + self.print_scroll_position(current) while True: try: - self.win.addstr(' ', curses.color_pair(theme.COLOR_INFORMATION_BAR)) + self.addstr(' ', curses.color_pair(theme.COLOR_INFORMATION_BAR)) except: break self.win.refresh() @@ -287,7 +303,7 @@ class TextWin(Win): """ """ self.win.attron(curses.color_pair(theme.COLOR_NEW_TEXT_SEPARATOR)) - self.win.addstr(' -'*(self.width//2)) + self.addstr(' -'*(self.width//2)) self.win.attroff(curses.color_pair(theme.COLOR_NEW_TEXT_SEPARATOR)) def write_text(self, y, x, txt, color, colorized): @@ -299,7 +315,7 @@ class TextWin(Win): if color: self.win.attron(curses.color_pair(color)) try: - self.win.addstr(y, x, txt) + self.addstr(y, x, txt) except: # bug 1665 pass if color: @@ -315,22 +331,22 @@ class TextWin(Win): for word in txt.split(): if word in list(special_words.keys()): self.win.attron(curses.color_pair(special_words[word])) - self.win.addstr(word) + self.addstr(word) self.win.attroff(curses.color_pair(special_words[word])) elif word.startswith('(') and word.endswith(')'): - self.win.addstr('(', curses.color_pair(color)) - self.win.addstr(word[1:-1], curses.color_pair(theme.COLOR_CURLYBRACKETED_WORD)) - self.win.addstr(')', curses.color_pair(color)) + self.addstr('(', curses.color_pair(color)) + self.addstr(word[1:-1], curses.color_pair(theme.COLOR_CURLYBRACKETED_WORD)) + self.addstr(')', curses.color_pair(color)) elif word.startswith('{') and word.endswith('}'): - self.win.addstr(word[1:-1], curses.color_pair(theme.COLOR_ACCOLADE_WORD)) + self.addstr(word[1:-1], curses.color_pair(theme.COLOR_ACCOLADE_WORD)) elif word.startswith('[') and word.endswith(']'): - self.win.addstr(word[1:-1], curses.color_pair(theme.COLOR_BRACKETED_WORD)) + self.addstr(word[1:-1], curses.color_pair(theme.COLOR_BRACKETED_WORD)) else: self.win.attron(curses.color_pair(color)) - self.win.addstr(word) + self.addstr(word) self.win.attroff(curses.color_pair(color)) try: - self.win.addstr(' ') + self.addstr(' ') except: pass @@ -341,44 +357,44 @@ class TextWin(Win): """ if color: self.win.attron(curses.color_pair(color)) - self.win.addstr(nickname) + self.addstr(nickname) if color: self.win.attroff(curses.color_pair(color)) - self.win.addnstr("> ", 2) + self.addnstr("> ", 2) def write_time(self, time): """ Write the date on the yth line of the window """ self.win.attron(curses.color_pair(theme.COLOR_TIME_BRACKETS)) - self.win.addnstr('[', 1) + self.addnstr('[', 1) self.win.attroff(curses.color_pair(theme.COLOR_TIME_BRACKETS)) self.win.attron(curses.color_pair(theme.COLOR_TIME_NUMBERS)) - self.win.addnstr(time.strftime("%H"), 2) + self.addnstr(time.strftime("%H"), 2) self.win.attroff(curses.color_pair(theme.COLOR_TIME_NUMBERS)) self.win.attron(curses.color_pair(theme.COLOR_TIME_SEPARATOR)) - self.win.addnstr(':', 1) + self.addnstr(':', 1) self.win.attroff(curses.color_pair(theme.COLOR_TIME_SEPARATOR)) self.win.attron(curses.color_pair(theme.COLOR_TIME_NUMBERS)) - self.win.addnstr(time.strftime("%M"), 2) + self.addnstr(time.strftime("%M"), 2) self.win.attroff(curses.color_pair(theme.COLOR_TIME_NUMBERS)) self.win.attron(curses.color_pair(theme.COLOR_TIME_SEPARATOR)) - self.win.addnstr(':', 1) + self.addnstr(':', 1) self.win.attroff(curses.color_pair(theme.COLOR_TIME_SEPARATOR)) self.win.attron(curses.color_pair(theme.COLOR_TIME_NUMBERS)) - self.win.addnstr(time.strftime('%S'), 2) + self.addnstr(time.strftime('%S'), 2) self.win.attroff(curses.color_pair(theme.COLOR_TIME_NUMBERS)) self.win.attron(curses.color_pair(theme.COLOR_TIME_BRACKETS)) - self.win.addstr(']') + self.addstr(']') self.win.attroff(curses.color_pair(theme.COLOR_TIME_BRACKETS)) - self.win.addstr(' ') + self.addstr(' ') def resize(self, height, width, y, x, stdscr, visible): self.visible = visible @@ -411,7 +427,7 @@ class Input(Win): self._resize(height, width, y, x, stdscr) self.win.leaveok(0) self.win.clear() - self.win.addnstr(0, 0, self.text, self.width-1) + self.addnstr(0, 0, self.text, self.width-1) def jump_word_left(self): """ @@ -511,7 +527,6 @@ class Input(Win): if not len(self.history): return self.reset_completion() - self.win.erase() if self.histo_pos < len(self.history)-1: self.histo_pos += 1 self.text = self.history[self.histo_pos] @@ -711,10 +726,12 @@ class Input(Win): """ Refresh the line onscreen, from the pos and pos_line """ + g_lock.acquire() self.clear_text() - self.win.addstr(self.text[self.line_pos:self.line_pos+self.width-1]) + self.addstr(self.text[self.line_pos:self.line_pos+self.width-1]) self.win.move(0, self.pos) self.refresh() + g_lock.release() def refresh(self): if not self.visible: -- cgit v1.2.3