2003-11-30 17:02:00 +01:00
|
|
|
## plugins/logger.py
|
|
|
|
##
|
|
|
|
## Gajim Team:
|
2005-01-07 22:52:38 +01:00
|
|
|
## - Yann Le Boulanger <asterix@lagaule.org>
|
2004-06-18 11:25:15 +02:00
|
|
|
## - Vincent Hanquez <tab@snarc.org>
|
2003-11-30 17:02:00 +01:00
|
|
|
##
|
2005-01-07 22:52:38 +01:00
|
|
|
## Copyright (C) 2003-2005 Gajim Team
|
2003-11-30 17:02:00 +01:00
|
|
|
##
|
|
|
|
## This program is free software; you can redistribute it and/or modify
|
|
|
|
## it under the terms of the GNU General Public License as published
|
|
|
|
## by the Free Software Foundation; version 2 only.
|
|
|
|
##
|
|
|
|
## This program is distributed in the hope that it will be useful,
|
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
|
|
|
|
2004-07-17 23:00:38 +02:00
|
|
|
def usage():
|
|
|
|
#TODO: use i18n
|
|
|
|
print "usage :", sys.argv[0], ' [OPTION]'
|
|
|
|
print " -p\tport on whitch the sock plugin listen"
|
|
|
|
print " -h, --help\tdisplay this help and exit"
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import getopt, sys, pickle, socket
|
|
|
|
try:
|
|
|
|
opts, args = getopt.getopt(sys.argv[1:], "p:h", ["help"])
|
|
|
|
except getopt.GetoptError:
|
|
|
|
# print help information and exit:
|
|
|
|
usage()
|
|
|
|
sys.exit(2)
|
|
|
|
port = 8255
|
|
|
|
for o, a in opts:
|
|
|
|
if o == '-p':
|
|
|
|
port = a
|
|
|
|
if o in ("-h", "--help"):
|
|
|
|
usage()
|
|
|
|
sys.exit()
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
try:
|
|
|
|
sock.connect(('', 8255))
|
|
|
|
except:
|
|
|
|
#TODO: use i18n
|
|
|
|
print "unable to connect to localhost on port "+str(port)
|
|
|
|
else:
|
|
|
|
evp = pickle.dumps(('EXEC_PLUGIN', '', 'logger'))
|
|
|
|
sock.send('<'+evp+'>')
|
|
|
|
sock.close()
|
|
|
|
sys.exit()
|
|
|
|
|
2003-11-30 17:02:00 +01:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import common.optparser
|
2004-05-17 01:47:14 +02:00
|
|
|
from common import i18n
|
2003-11-30 17:02:00 +01:00
|
|
|
LOGPATH = os.path.expanduser("~/.gajim/logs/")
|
2004-05-17 01:47:14 +02:00
|
|
|
_ = i18n._
|
2003-11-30 17:02:00 +01:00
|
|
|
|
|
|
|
class plugin:
|
|
|
|
def read_queue(self):
|
|
|
|
while 1:
|
|
|
|
while self.queueIN.empty() == 0:
|
2004-01-24 21:32:51 +01:00
|
|
|
if self.config.has_key('lognotsep'):
|
|
|
|
lognotsep = self.config['lognotsep']
|
2003-11-30 17:02:00 +01:00
|
|
|
else:
|
|
|
|
lognotsep = 1
|
2004-01-24 21:32:51 +01:00
|
|
|
if self.config.has_key('lognotusr'):
|
|
|
|
lognotusr = self.config['lognotusr']
|
2003-11-30 17:02:00 +01:00
|
|
|
else:
|
|
|
|
lognotusr = 1
|
2004-04-05 00:09:56 +02:00
|
|
|
tim = time.time()
|
2003-11-30 17:02:00 +01:00
|
|
|
|
|
|
|
ev = self.queueIN.get()
|
|
|
|
if ev[0] == 'QUIT':
|
2004-05-17 01:47:14 +02:00
|
|
|
print _("plugin logger stopped")
|
2003-11-30 17:02:00 +01:00
|
|
|
return
|
|
|
|
elif ev[0] == 'NOTIFY':
|
2005-03-11 01:13:21 +01:00
|
|
|
jid = ev[2][0].split('/')[0]
|
2004-11-15 10:51:30 +01:00
|
|
|
if jid in self.no_log_for[ev[1]]:
|
|
|
|
break
|
2005-03-06 20:19:21 +01:00
|
|
|
if ev[1] in self.no_log_for[ev[1]]:
|
|
|
|
break
|
2004-11-15 10:51:30 +01:00
|
|
|
status = ev[2][2]
|
2003-11-30 17:02:00 +01:00
|
|
|
if not status:
|
|
|
|
status = ""
|
2005-03-11 01:13:21 +01:00
|
|
|
status = status.replace('\n', '\\n')
|
2003-11-30 17:02:00 +01:00
|
|
|
if lognotsep == 1:
|
|
|
|
fic = open(LOGPATH + "notify.log", "a")
|
2004-09-27 21:13:16 +02:00
|
|
|
fic.write("%s:%s:%s:%s\n" % (tim, ev[2][0] + '/' + ev[2][3], \
|
2004-03-22 14:09:59 +01:00
|
|
|
ev[2][1], status))
|
2003-11-30 17:02:00 +01:00
|
|
|
fic.close()
|
|
|
|
if lognotusr == 1:
|
2003-12-20 14:22:37 +01:00
|
|
|
fic = open(LOGPATH + jid, "a")
|
2004-09-27 21:13:16 +02:00
|
|
|
fic.write("%s:%s:%s:%s\n" % (tim, ev[2][0] + '/' + ev[2][3], \
|
2004-03-22 14:09:59 +01:00
|
|
|
ev[2][1], status))
|
2003-11-30 17:02:00 +01:00
|
|
|
fic.close()
|
|
|
|
elif ev[0] == 'MSG':
|
2005-03-11 01:13:21 +01:00
|
|
|
jid = ev[2][0].split('/')[0]
|
2004-11-15 10:51:30 +01:00
|
|
|
if jid in self.no_log_for[ev[1]]:
|
|
|
|
break
|
2005-03-06 20:19:21 +01:00
|
|
|
if ev[1] in self.no_log_for[ev[1]]:
|
|
|
|
break
|
2005-03-27 10:40:42 +02:00
|
|
|
if not msg:
|
|
|
|
msg = ''
|
2005-03-11 01:13:21 +01:00
|
|
|
msg = ev[2][1].replace('\n', '\\n')
|
2003-12-20 14:22:37 +01:00
|
|
|
fic = open(LOGPATH + jid, "a")
|
2004-09-27 19:51:51 +02:00
|
|
|
t = time.mktime(ev[2][2])
|
|
|
|
fic.write("%s:recv:%s\n" % (t, msg))
|
2003-11-30 17:02:00 +01:00
|
|
|
fic.close()
|
|
|
|
elif ev[0] == 'MSGSENT':
|
2005-03-11 01:13:21 +01:00
|
|
|
jid = ev[2][0].split('/')[0]
|
2004-11-15 10:51:30 +01:00
|
|
|
if jid in self.no_log_for[ev[1]]:
|
|
|
|
break
|
2005-03-06 20:19:21 +01:00
|
|
|
if ev[1] in self.no_log_for[ev[1]]:
|
|
|
|
break
|
2005-03-11 01:13:21 +01:00
|
|
|
msg = ev[2][1].replace('\n', '\\n')
|
2003-12-20 14:22:37 +01:00
|
|
|
fic = open(LOGPATH + jid, "a")
|
2004-04-05 00:09:56 +02:00
|
|
|
fic.write("%s:sent:%s\n" % (tim, msg))
|
2003-11-30 17:02:00 +01:00
|
|
|
fic.close()
|
2004-09-27 21:13:16 +02:00
|
|
|
elif ev[0] == 'GC_MSG':
|
2005-03-11 01:13:21 +01:00
|
|
|
msg = ev[2][1].replace('\n', '\\n')
|
|
|
|
jids = ev[2][0].split('/')
|
2004-09-27 21:13:16 +02:00
|
|
|
jid = jids[0]
|
|
|
|
nick = ''
|
|
|
|
if len(jids) > 1:
|
2005-03-11 01:13:21 +01:00
|
|
|
nick = ev[2][0].split('/')[1]
|
2004-09-27 21:13:16 +02:00
|
|
|
fic = open(LOGPATH + jid, "a")
|
|
|
|
t = time.mktime(ev[2][2])
|
|
|
|
fic.write("%s:recv:%s:%s\n" % (t, nick, msg))
|
|
|
|
fic.close()
|
2004-11-04 02:03:17 +01:00
|
|
|
elif ev[0] == 'CONFIG':
|
|
|
|
if ev[2][0] == 'Logger':
|
|
|
|
self.config = ev[2][1]
|
2004-11-15 10:51:30 +01:00
|
|
|
if ev[2][0] == 'accounts':
|
|
|
|
accounts = ev[2][1]
|
|
|
|
self.no_log_for = {}
|
|
|
|
for acct in accounts.keys():
|
|
|
|
self.no_log_for[acct] = []
|
|
|
|
if accounts[acct].has_key('no_log_for'):
|
|
|
|
self.no_log_for[acct] = \
|
2005-03-11 01:13:21 +01:00
|
|
|
accounts[acct]['no_log_for'].split()
|
2004-04-17 21:38:43 +02:00
|
|
|
time.sleep(0.1)
|
2003-11-30 17:02:00 +01:00
|
|
|
|
2004-01-24 21:32:51 +01:00
|
|
|
def wait(self, what):
|
|
|
|
"""Wait for a message from Core"""
|
|
|
|
#TODO: timeout, save messages that don't fit
|
|
|
|
while 1:
|
|
|
|
if not self.queueIN.empty():
|
|
|
|
ev = self.queueIN.get()
|
2004-03-22 14:09:59 +01:00
|
|
|
if ev[0] == what and ev[2][0] == 'Logger':
|
|
|
|
return ev[2][1]
|
2004-01-24 21:32:51 +01:00
|
|
|
time.sleep(0.1)
|
|
|
|
|
2003-11-30 17:02:00 +01:00
|
|
|
def __init__(self, quIN, quOUT):
|
|
|
|
self.queueIN = quIN
|
|
|
|
self.queueOUT = quOUT
|
2004-07-09 01:51:48 +02:00
|
|
|
quOUT.put(('REG_MESSAGE', 'logger', ['CONFIG', 'NOTIFY', 'MSG', \
|
2004-09-27 21:13:16 +02:00
|
|
|
'MSGSENT', 'GC_MSG', 'QUIT']))
|
2004-03-22 14:09:59 +01:00
|
|
|
quOUT.put(('ASK_CONFIG', None, ('Logger', 'Logger', {\
|
2004-02-25 01:33:06 +01:00
|
|
|
'lognotsep':1, 'lognotusr':1})))
|
2004-01-24 21:32:51 +01:00
|
|
|
self.config = self.wait('CONFIG')
|
2004-11-15 10:51:30 +01:00
|
|
|
quOUT.put(('ASK_CONFIG', None, ('Logger', 'accounts')))
|
|
|
|
accounts = self.wait('CONFIG')
|
|
|
|
self.no_log_for = {}
|
|
|
|
for acct in accounts.keys():
|
|
|
|
self.no_log_for[acct] = []
|
|
|
|
if accounts[acct].has_key('no_log_for'):
|
|
|
|
self.no_log_for[acct] = \
|
2005-03-11 01:13:21 +01:00
|
|
|
accounts[acct]['no_log_for'].split()
|
2004-01-24 21:32:51 +01:00
|
|
|
#create ~/.gajim/logs/ if it doesn't exist
|
2003-11-30 17:02:00 +01:00
|
|
|
try:
|
|
|
|
os.stat(os.path.expanduser("~/.gajim"))
|
|
|
|
except OSError:
|
|
|
|
os.mkdir(os.path.expanduser("~/.gajim"))
|
2004-05-17 01:47:14 +02:00
|
|
|
print _("creating ~/.gajim/")
|
2003-11-30 17:02:00 +01:00
|
|
|
try:
|
|
|
|
os.stat(LOGPATH)
|
|
|
|
except OSError:
|
|
|
|
os.mkdir(LOGPATH)
|
2004-05-17 01:47:14 +02:00
|
|
|
print _("creating ~/.gajim/logs/")
|
2003-11-30 17:02:00 +01:00
|
|
|
self.read_queue()
|
|
|
|
|
2004-05-17 01:47:14 +02:00
|
|
|
print _("plugin logger loaded")
|