From 64d5c4b6f322990e94d999172c3eb4146724f872 Mon Sep 17 00:00:00 2001 From: "louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13" Date: Tue, 26 Jan 2010 19:33:40 +0000 Subject: ca fonctionne, ca clignotte, et cest gay --- src/client.py | 7 ++----- src/config.py | 39 +++++++++++++++++++++++++++-------- src/connection.py | 21 +------------------ src/gui.py | 39 ++++++++++++++++++++++++++--------- src/logging.py | 24 ++++++++++++++-------- src/multiuserchat.py | 6 +++++- src/poezio.cfg | 3 ++- src/window.py | 57 +++++++++++++++++++++++++++++++++++++++++++--------- 8 files changed, 135 insertions(+), 61 deletions(-) (limited to 'src') diff --git a/src/client.py b/src/client.py index 8ab034d0..ccc2b7b4 100644 --- a/src/client.py +++ b/src/client.py @@ -25,9 +25,6 @@ from handler import Handler from gui import Gui from curses import wrapper, initscr -logfile = config.get('logfile') -#sys.stderr = open(logfile, 'a') # print the errors in the logfile - class Client(object): """ Main class @@ -36,8 +33,8 @@ class Client(object): def __init__(self): self.handler = Handler() - self.resource = config.get('resource') - self.server = config.get('server') + self.resource = config.get('resource', 'poezio') + self.server = config.get('server', 'louiz.org') self.connection = Connection(self.server, self.resource) self.stdscr = initscr() diff --git a/src/config.py b/src/config.py index 238d2d5e..95f85c98 100644 --- a/src/config.py +++ b/src/config.py @@ -18,7 +18,8 @@ # You should have received a copy of the GNU General Public License # along with Poezio. If not, see . -from ConfigParser import RawConfigParser +from ConfigParser import RawConfigParser, NoOptionError +# from logging import logger class Config(RawConfigParser): """ @@ -30,18 +31,40 @@ class Config(RawConfigParser): RawConfigParser.__init__(self, None) RawConfigParser.read(self, file_name) - def get(self, option): + def get(self, option, default): + """ + get a value from the config but return + a default value if it is not found + The type of default defines the type + returned + """ + try: + if type(default) == int: + res = self.getint(option) + elif type(default) == float: + res = self.getfloat(option) + elif type(default) == bool: + res = self.getbool(option) + else: + res = self.getstr(option) + except NoOptionError: + # TODO + # logger.info('No value found in config file "%s" from option [%s]. Defaulting to "%s"' \ + # % (self.file_name, option, default)) + return default + return res + + def _get(self, option): return RawConfigParser.get(self, self.defsection, option) - def rget(self, option): - res = self.get(option) - print res + def getstr(self, option): + return self._get(option) def getint(self, option): - return int(self.get(option)) + return int(self._get(option)) def getfloat(self, option): - return float(self.get(option)) + return float(self._get(option)) def getboolean(self, option): return RawConfigParser.getboolean(self, self.defsection, option) @@ -52,7 +75,7 @@ class Config(RawConfigParser): def save(self): f = copen(self.filename, "w", "utf-8", "ignore") RawConfigParser.write(self, f) - f.close() + f.close() def setAndSave(self, option, value): self.set(option, value) diff --git a/src/connection.py b/src/connection.py index 2ef22425..8185994f 100644 --- a/src/connection.py +++ b/src/connection.py @@ -39,12 +39,7 @@ class Connection(Thread): self.resource = resource self.online = 0 # 1:connected, 2:auth confirmed self.jid = '' # we don't know our jid yet (anon account) - if not self.server: - logger.error('You should set a server in the configuration file') - self.port = int(config.get('port')) - if not self.port: - logger.warning('No port set in configuration file, defaulting to 5222') - self.port = 5222 + self.port = config.get('port', 5222) self.client = xmpp.Client(self.server, debug=[]) def run(self): @@ -105,17 +100,3 @@ class Connection(Thread): else: log.warning('disconnecting...') sys.exit() - -if __name__ == '__main__': - resource = config.get('resource') - server = config.get('server') - connection = Connection(server, resource) - connection.start() - rooms = config.get('rooms').split(':') - from time import sleep - print connection.online - sleep(2) - print connection.online - for room in rooms: - connection.send_join_room(room.split('/')[0], room.split('/')[1]) - i = 17 diff --git a/src/gui.py b/src/gui.py index 95025bb3..38490678 100644 --- a/src/gui.py +++ b/src/gui.py @@ -26,6 +26,8 @@ from datetime import datetime from logging import logger +from random import randrange + locale.setlocale(locale.LC_ALL, '') code = locale.getpreferredencoding() @@ -41,6 +43,7 @@ class User(object): def __init__(self, nick, affiliation, show, status, role): self.update(affiliation, show, status, role) self.change_nick(nick) + self.color = randrange(2, 10) def update(self, affiliation, show, status, role): self.affiliation = None @@ -63,7 +66,15 @@ class Room(object): self.topic = '' def add_message(self, nick, msg): - self.lines.append((datetime.now(), nick.encode('utf-8'), msg.encode('utf-8'))) + if not msg: + logger.info('msg is None..., %s' % (nick)) + return + lines = msg.split('\n') + # first line has the nick and timestamp but others don't + self.lines.append((datetime.now(), nick.encode('utf-8'), lines.pop(0).encode('utf-8'))) + if len(lines) > 0: + for line in lines: + self.lines.append((line.encode('utf-8'))) def add_info(self, info): """ info, like join/quit/status messages""" @@ -89,7 +100,7 @@ class Room(object): user.change_nick(stanza.getNick()) self.add_info('%s is now known as %s' % (nick, stanza.getNick())) return - if status == 'offline': + if status == 'offline' or role == 'none': self.users.remove(user) self.add_info('%s has left the room' % (nick)) return @@ -144,6 +155,10 @@ class Gui(object): curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLACK) # Admin curses.init_pair(4, curses.COLOR_BLUE, curses.COLOR_BLACK) # Participant curses.init_pair(5, curses.COLOR_WHITE, curses.COLOR_BLACK) # Visitor + curses.init_pair(6, curses.COLOR_CYAN, curses.COLOR_BLACK) + curses.init_pair(7, curses.COLOR_GREEN, curses.COLOR_BLACK) + curses.init_pair(8, curses.COLOR_MAGENTA, curses.COLOR_BLACK) + curses.init_pair(9, curses.COLOR_YELLOW, curses.COLOR_BLACK) def reset_curses(self): curses.echo() @@ -168,12 +183,19 @@ class Gui(object): return # ignore all messages not comming from a MUC room_from = stanza.getFrom().getStripped() nick_from = stanza.getFrom().getResource() - room = self.get_room_by_name(name) + if not nick_from: + nick_from = '' + room = self.get_room_by_name(room_from) if not room: return logger.warning("message received for a non-existing room: %s" % (name)) - room.add_message(nick_from, stanza.getBody()) + body = stanza.getBody() + if not body: + body = stanza.getSubject() + room.add_info("%s changed the subject to: %s" % (nick_from, stanza.getSubject())) + else: + room.add_message(nick_from, body) if room == self.current_room(): - self.window.text_win.refresh(room.lines) + self.window.text_win.refresh(room.lines, room.users) self.window.user_win.refresh(room.users) self.window.input.refresh() curses.doupdate() @@ -181,12 +203,12 @@ class Gui(object): def room_presence(self, stanza): from_nick = stanza.getFrom().getResource() from_room = stanza.getFrom().getStripped() - room = self.get_room_by_name() + room = self.get_room_by_name(from_room) if not room: return logger.warning("presence received for a non-existing room: %s" % (name)) room.on_presence(stanza, from_nick) - if room == self.current_rooom(): - self.window.text_win.refresh(room.lines) + if room == self.current_room(): + self.window.text_win.refresh(room.lines, room.users) self.window.user_win.refresh(room.users) curses.doupdate() @@ -218,7 +240,6 @@ class Gui(object): def main_loop(self, stdscr): while 1: curses.doupdate() - # self.window.input.refresh() key = stdscr.getch() if key == curses.KEY_RESIZE: self.window.resize(stdscr) diff --git a/src/logging.py b/src/logging.py index 4bbedb9d..bb22e5c9 100644 --- a/src/logging.py +++ b/src/logging.py @@ -17,28 +17,36 @@ # You should have received a copy of the GNU General Public License # along with Poezio. If not, see . -from config import config import sys from datetime import datetime - +from config import config class Logger(object): """ Appends things to files. Error/information/warning logs and also log the conversations to logfiles """ - def __init__(self): - self.logfile = config.get('logfile') + def __init__(self):# , logfile, loglevel): + self.logfile = config.get('logfile', 'logs') + self.loglevel = config.get('loglevel', 3) + # self.logfile = logfile + # self.loglevel = loglevel + + def info(self, msg): + if self.logfile and self.loglevel >= 3: + fd = open(self.logfile, 'a') + fd.write(datetime.now().strftime("%H:%M:%S") + ' Info [' + msg + ']\n') + fd.close() def warning(self, msg): - if self.logfile: + if self.logfile and self.loglevel >= 2: fd = open(self.logfile, 'a') - fd.write(datetime.now().strftime("%H:%M:%S") + ' Warning [' + msg + ']') + fd.write(datetime.now().strftime("%H:%M:%S") + ' Warning [' + msg + ']\n') fd.close() def error(self, msg): - if self.logfile: + if self.logfile and self.loglevel >= 1: fd = open(self.logfile, 'a') - fd.write(datetime.now().strftime("%H:%M:%S") + ' Error [' + msg + ']') + fd.write(datetime.now().strftime("%H:%M:%S") + ' Error [' + msg + ']\n') fd.close() sys.exit(-1) diff --git a/src/multiuserchat.py b/src/multiuserchat.py index 415b7f42..0d50b51d 100644 --- a/src/multiuserchat.py +++ b/src/multiuserchat.py @@ -46,7 +46,11 @@ class MultiUserChat(object): self.handler.connect('on-connected', self.on_connected) def on_connected(self): - rooms = config.get('rooms').split(':') + rooms = config.get('rooms', '') + if rooms == '': + return + else: + rooms = rooms.split(':') for room in rooms: [roomname, nick] = room.split('/') self.handler.emit('join-room', room=roomname, nick=nick) diff --git a/src/poezio.cfg b/src/poezio.cfg index 1a75f859..f453c946 100644 --- a/src/poezio.cfg +++ b/src/poezio.cfg @@ -1,6 +1,7 @@ [Poezio] -logfile = salut +logfile = logs resource = poezio server = louiz.org port = 5222 +rooms = discussion@kikoo.louiz.org/Poefion diff --git a/src/window.py b/src/window.py index 89003456..e93da43b 100644 --- a/src/window.py +++ b/src/window.py @@ -18,6 +18,22 @@ # along with Poezio. If not, see . import curses +from logging import logger + +def get_next_line(str, length): + pos = str.rfind(' ', 0, 30) + if pos == -1: + return str[:length], str[length:] + else: + return str[:pos], str[pos+1:] + +def cut_line(str, length): + tab = [] + while len(str) > length: + cut, str = get_next_line(str, length) + tab.append(cut) + tab.append(str) + return tab class Win(object): def __init__(self, height, width, y, x, parent_win): @@ -37,8 +53,8 @@ class UserList(Win): self.win.vline(0, 0, curses.ACS_VLINE, self.height) self.win.attroff(curses.color_pair(2)) self.color_dict = {'moderator': 3, - 'participant':4, - 'visitor':5} + 'participant':2, + 'visitor':4} def refresh(self, users): self.win.clear() @@ -70,22 +86,45 @@ class Info(Win): , curses.color_pair(1)) self.win.noutrefresh() - class TextWin(Win): def __init__(self, height, width, y, x, parent_win): Win.__init__(self, height, width, y, x, parent_win) self.win.idlok(True) self.pos = 0 - def refresh(self, lines): + def refresh(self, lines, users): self.win.clear() y = 0 - for line in lines[-self.height:]: + # logger.info(str(self.height)) + # logger.info(str(lines[-self.height/2:])) + for line in lines[-self.height:]: # FIXME if len(line) == 2: - self.win.addstr(y, 0, '['+line[0].strftime("%H:%M:%S") + "] *" + line[1]+"*") + try: + self.win.addstr(y, 0, '['+line[0].strftime("%H:%M:%S") + "] *" + line[1]+"*") + except: + logger.error(str(line)) + raise + y += 1 elif len(line) == 3: - self.win.addstr(y, 0, '['+line[0].strftime("%H:%M:%S") + "] " + line[1]+": "+line[2]) - y += 1 + for user in users: + if user.nick == line[1]: + break + self.win.addstr(y, 0, '['+line[0].strftime("%H:%M:%S") + "] <") + length = len('['+line[0].strftime("%H:%M:%S") + "] <") + self.win.attron(curses.color_pair(user.color)) + self.win.addstr(y, length,line[1]) + self.win.attroff(curses.color_pair(user.color)) + self.win.addstr(y, length+len(line[1]), ">: ") + remaining_width = self.width-(length+len(line[1])+3) + tab = cut_line(line[2], remaining_width-1) + for l in tab: + try: + self.win.addstr(y, length+len(line[1])+3, l) + y += 1 + except:pass + elif len(line) == 1: + self.win.addstr(y, 0, line[0]) + y += 1 self.win.noutrefresh() def resize(self, height, width, y, x, stdscr): @@ -155,7 +194,7 @@ class Window(object): self.input.resize(1, self.width, self.height-1, 0, stdscr) def refresh(self, room): - self.text_win.refresh(room.lines) + self.text_win.refresh(room.lines, room.users) self.user_win.refresh(room.users) self.topic_win.refresh(room.topic) self.info_win.refresh(room.name) -- cgit v1.2.3