Commits

Miki Tebeka committed 6577f19

one room

  • Participants
  • Parent commits abe69d9

Comments (0)

Files changed (4)

logbot/__main__.py

     parser.add_argument('--passwd', help='jabber password', default=None)
     parser.add_argument('--host', help='jabber host', default='localhost')
     parser.add_argument('--port', help='jabber port', default=5222, type=int)
-    parser.add_argument('--use-tls', help='use tls',
-                        action='store_true', default=False)
+    parser.add_argument('--no-tls', help='do not use tls', dest='use_tls',
+                        action='store_false', default=True)
     parser.add_argument('room', help='room to log')  # FIXME: Rooms
     parser.add_argument('--version', action='version',
                         version='logbot {}'.format(__version__))
     register_listener(search.index)
 
     run_thread(httpd.run)
-
-    bot.run(args.host, args.port, user, passwd, [args.room], args.use_tls)
+    bot.run(args.host, args.port, user, passwd, args.room, args.use_tls)
 
 
 if __name__ == '__main__':
 
 
 class LogBot(ClientXMPP):
-    def __init__(self, jid, password, rooms):
+    def __init__(self, jid, password, room, nick):
         super(LogBot, self).__init__(jid, password)
-        self.rooms = rooms
+        self.room = room
+        self.nick = nick
 
         self.add_event_handler("session_start", self.session_start)
         self.add_event_handler("groupchat_message", self.publish)
         self.send_presence()
         self.get_roster()
 
-        for room in self.rooms:
-            self.plugin['xep_0045'].joinMUC(room, 'logbot', wait=True)
+        self.plugin['xep_0045'].joinMUC(self.room, self.nick, wait=True)
 
     def xmpp_user(self, xmpp_msg):
         return xmpp_msg['mucnick'] or xmpp_msg['from'].split('@', 1)[0]
         publish(msg)
 
 
-def run(host, port, user, passwd, rooms, use_tls=False):
-    xmpp = LogBot(user, passwd, rooms)
+def run(host, port, user, passwd, rooms, use_tls=True, nick='logbot'):
+    xmpp = LogBot(user, passwd, rooms, nick)
 
     signal(SIGINT, lambda signum, frame: xmpp.disconnect())
 

logbot/httpd/__init__.py

 from .. import __version__
 from ..search import search as _search
 from ..common import format_message
-from ..log import list_logs, log_path, log_for_time
+from ..log import list_logs, log_path, logfile
 
-from flask import Flask, abort, Response, request, url_for
+from flask import Flask, abort, Response, request
 from jinja2 import Environment, FileSystemLoader
 
-
 from collections import namedtuple
 from httplib import NOT_FOUND
-from os.path import dirname, realpath, join, isfile
+from os.path import dirname, realpath, join, isfile, basename
+import logging
 
 static_dir = join(dirname(realpath(__file__)), 'static')
 get_template = Environment(loader=FileSystemLoader(static_dir)).get_template
 app = Flask(__name__)
 
 
+def supress_stdout_logs():
+    log = logging.getLogger('werkzeug')
+    log.setLevel(logging.ERROR)
+
+
 @app.route('/')
 def index():
     template = get_template('index.html')
         try:
             query = request.form['query'].strip()
             messages = _search(query)
-            results = [Result(log_for_time(msg.time), format_message(msg))
+            results = [Result(basename(logfile(msg.time)), format_message(msg))
                        for msg in messages]
         except Exception as err:
             error = str(err)
 
 
 def run():
+    supress_stdout_logs()
     app.run()
 from glob import glob
 from os.path import join, basename
 
-_log = _logfile = None
-
-
-def log_for_time(time):
-    return time.strftime('%Y%m%d.txt')
-
-
-def get_log(time):
-    global _log, _logfile
-
-    logfile = log_path(log_for_time(time))
-    if logfile != _logfile:
-        if _log:
-            _log.close()
-
-        _logfile = logfile
-        _log = open(logfile, 'at')
-
-    return _log
-
-
-def log(msg):
-    out = get_log(msg.time)
-    out.write('{}\n'.format(format_message(msg)))
-    out.flush()
-
-
-def list_logs():
-    return sorted(basename(log) for log in glob(join(logs_dir, '*.txt')))
-
 
 def log_path(log):
     return join(logs_dir, log)
+
+
+def logfile(time):
+    return log_path('{}.txt'.format(time.strftime('%Y%m%d')))
+
+
+def log(msg):
+    filename = logfile(msg.time)
+    with open(filename, 'at') as out:
+        out.write('{}\n'.format(format_message(msg)))
+
+
+def list_logs():
+    return sorted(basename(path) for path in glob(join(logs_dir, '*.txt')))