2005-05-12 15:43:17 +02:00
|
|
|
## logger.py
|
2003-11-30 17:02:00 +01:00
|
|
|
##
|
2005-12-09 18:15:30 +01:00
|
|
|
## Contributors for this file:
|
2005-05-12 15:43:17 +02:00
|
|
|
## - Yann Le Boulanger <asterix@lagaule.org>
|
2005-05-20 02:05:18 +02:00
|
|
|
## - Nikos Kouremenos <kourem@gmail.com>
|
2003-11-30 17:02:00 +01:00
|
|
|
##
|
2005-12-10 00:30:28 +01:00
|
|
|
## Copyright (C) 2003-2004 Yann Le Boulanger <asterix@lagaule.org>
|
|
|
|
## Vincent Hanquez <tab@snarc.org>
|
|
|
|
## Copyright (C) 2005 Yann Le Boulanger <asterix@lagaule.org>
|
|
|
|
## Vincent Hanquez <tab@snarc.org>
|
|
|
|
## Nikos Kouremenos <nkour@jabber.org>
|
|
|
|
## Dimitur Kirov <dkirov@gmail.com>
|
|
|
|
## Travis Shirk <travis@pobox.com>
|
|
|
|
## Norman Rasmussen <norman@rasmussen.co.za>
|
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.
|
|
|
|
##
|
|
|
|
|
|
|
|
import os
|
2005-11-23 20:12:52 +01:00
|
|
|
import sys
|
2003-11-30 17:02:00 +01:00
|
|
|
import time
|
2005-11-23 20:12:52 +01:00
|
|
|
import datetime
|
2005-08-25 18:50:02 +02:00
|
|
|
|
2005-12-05 12:13:08 +01:00
|
|
|
import exceptions
|
|
|
|
import i18n
|
2004-05-17 01:47:14 +02:00
|
|
|
_ = i18n._
|
2003-11-30 17:02:00 +01:00
|
|
|
|
2005-11-23 20:12:52 +01:00
|
|
|
try:
|
|
|
|
from pysqlite2 import dbapi2 as sqlite
|
|
|
|
except ImportError:
|
2005-12-01 18:16:53 +01:00
|
|
|
raise exceptions.PysqliteNotAvailable
|
|
|
|
|
2005-11-23 20:12:52 +01:00
|
|
|
if os.name == 'nt':
|
|
|
|
try:
|
|
|
|
# Documents and Settings\[User Name]\Application Data\Gajim\logs.db
|
|
|
|
LOG_DB_PATH = os.path.join(os.environ['appdata'], 'Gajim', 'logs.db')
|
|
|
|
except KeyError:
|
|
|
|
# win9x, ./logs.db
|
|
|
|
LOG_DB_PATH = 'logs.db'
|
|
|
|
else: # Unices
|
|
|
|
LOG_DB_PATH = os.path.expanduser('~/.gajim/logs.db')
|
|
|
|
|
|
|
|
try:
|
|
|
|
LOG_DB_PATH = LOG_DB_PATH.decode(sys.getfilesystemencoding())
|
|
|
|
except:
|
|
|
|
pass
|
2005-09-02 23:45:35 +02:00
|
|
|
|
2005-11-26 00:23:25 +01:00
|
|
|
class Constants:
|
|
|
|
def __init__(self):
|
|
|
|
(
|
|
|
|
self.JID_NORMAL_TYPE,
|
2005-11-30 15:16:26 +01:00
|
|
|
self.JID_ROOM_TYPE
|
2005-11-26 00:23:25 +01:00
|
|
|
) = range(2)
|
|
|
|
|
|
|
|
(
|
|
|
|
self.KIND_STATUS,
|
|
|
|
self.KIND_GCSTATUS,
|
|
|
|
self.KIND_GC_MSG,
|
|
|
|
self.KIND_SINGLE_MSG_RECV,
|
|
|
|
self.KIND_CHAT_MSG_RECV,
|
|
|
|
self.KIND_SINGLE_MSG_SENT,
|
|
|
|
self.KIND_CHAT_MSG_SENT
|
|
|
|
) = range(7)
|
|
|
|
|
|
|
|
(
|
|
|
|
self.SHOW_ONLINE,
|
|
|
|
self.SHOW_CHAT,
|
|
|
|
self.SHOW_AWAY,
|
|
|
|
self.SHOW_XA,
|
|
|
|
self.SHOW_DND,
|
|
|
|
self.SHOW_OFFLINE
|
|
|
|
) = range(6)
|
|
|
|
|
|
|
|
constants = Constants()
|
2005-11-24 19:29:45 +01:00
|
|
|
|
2005-04-16 00:02:13 +02:00
|
|
|
class Logger:
|
|
|
|
def __init__(self):
|
2005-12-05 14:56:02 +01:00
|
|
|
self.jids_already_in = [] # holds jids that we already have in DB
|
|
|
|
|
2005-11-23 20:12:52 +01:00
|
|
|
if not os.path.exists(LOG_DB_PATH):
|
|
|
|
# this can happen only the first time (the time we create the db)
|
2005-11-26 00:23:25 +01:00
|
|
|
# db is not created here but in src/common/checks_paths.py
|
2005-11-23 20:12:52 +01:00
|
|
|
return
|
2005-12-08 10:46:59 +01:00
|
|
|
self.init_vars()
|
2005-11-23 20:12:52 +01:00
|
|
|
|
2005-12-08 10:46:59 +01:00
|
|
|
def init_vars(self):
|
2005-11-27 13:22:07 +01:00
|
|
|
# if locked, wait up to 20 sec to unlock
|
|
|
|
# before raise (hopefully should be enough)
|
2005-12-05 18:22:50 +01:00
|
|
|
self.con = sqlite.connect(LOG_DB_PATH, timeout = 20.0,
|
2005-11-27 13:22:07 +01:00
|
|
|
isolation_level = 'IMMEDIATE')
|
2005-12-05 18:22:50 +01:00
|
|
|
self.cur = self.con.cursor()
|
2005-11-26 00:23:25 +01:00
|
|
|
|
2005-11-23 20:12:52 +01:00
|
|
|
self.get_jids_already_in_db()
|
2005-07-02 01:13:46 +02:00
|
|
|
|
2005-11-23 20:12:52 +01:00
|
|
|
def get_jids_already_in_db(self):
|
2005-12-05 18:22:50 +01:00
|
|
|
self.cur.execute('SELECT jid FROM jids')
|
|
|
|
rows = self.cur.fetchall() # list of tupples: (u'aaa@bbb',), (u'cc@dd',)]
|
2005-11-23 20:12:52 +01:00
|
|
|
for row in rows:
|
|
|
|
# row[0] is first item of row (the only result here, the jid)
|
|
|
|
self.jids_already_in.append(row[0])
|
|
|
|
|
2005-11-26 00:23:25 +01:00
|
|
|
def jid_is_from_pm(self, jid):
|
2005-11-23 20:12:52 +01:00
|
|
|
'''if jid is gajim@conf/nkour it's likely a pm one, how we know
|
|
|
|
gajim@conf is not a normal guy and nkour is not his resource?
|
2005-11-26 00:23:25 +01:00
|
|
|
we ask if gajim@conf is already in jids (with type room jid)
|
|
|
|
this fails if user disables logging for room and only enables for
|
|
|
|
pm (so higly unlikely) and if we fail we do not go chaos
|
|
|
|
(user will see the first pm as if it was message in room's public chat)
|
|
|
|
and after that all okay'''
|
2005-11-23 20:12:52 +01:00
|
|
|
|
|
|
|
possible_room_jid, possible_nick = jid.split('/', 1)
|
|
|
|
|
2005-12-05 18:22:50 +01:00
|
|
|
self.cur.execute('SELECT jid_id FROM jids WHERE jid="%s" AND type=%d' %\
|
2005-11-26 00:23:25 +01:00
|
|
|
(possible_room_jid, constants.JID_ROOM_TYPE))
|
2005-12-05 18:22:50 +01:00
|
|
|
row = self.cur.fetchone()
|
2005-11-26 00:23:25 +01:00
|
|
|
if row is not None:
|
2005-11-23 20:12:52 +01:00
|
|
|
return True
|
2005-11-27 13:22:07 +01:00
|
|
|
else:
|
2005-11-23 20:12:52 +01:00
|
|
|
return False
|
|
|
|
|
2005-11-26 01:03:09 +01:00
|
|
|
def get_jid_id(self, jid, typestr = None):
|
2005-11-23 20:12:52 +01:00
|
|
|
'''jids table has jid and jid_id
|
|
|
|
logs table has log_id, jid_id, contact_name, time, kind, show, message
|
|
|
|
so to ask logs we need jid_id that matches our jid in jids table
|
|
|
|
this method asks jid and returns the jid_id for later sql-ing on logs
|
|
|
|
'''
|
2005-11-24 19:29:45 +01:00
|
|
|
if jid.find('/') != -1: # if it has a /
|
2005-11-26 01:03:09 +01:00
|
|
|
jid_is_from_pm = self.jid_is_from_pm(jid)
|
|
|
|
if not jid_is_from_pm: # it's normal jid with resource
|
2005-11-24 19:29:45 +01:00
|
|
|
jid = jid.split('/', 1)[0] # remove the resource
|
2005-11-23 20:12:52 +01:00
|
|
|
if jid in self.jids_already_in: # we already have jids in DB
|
2005-12-05 18:22:50 +01:00
|
|
|
self.cur.execute('SELECT jid_id FROM jids WHERE jid="%s"' % jid)
|
|
|
|
jid_id = self.cur.fetchone()[0]
|
2005-11-26 00:23:25 +01:00
|
|
|
else: # oh! a new jid :), we add it now
|
2005-11-26 01:03:09 +01:00
|
|
|
if typestr == 'ROOM':
|
|
|
|
typ = constants.JID_ROOM_TYPE
|
|
|
|
else:
|
|
|
|
typ = constants.JID_NORMAL_TYPE
|
2005-12-05 18:22:50 +01:00
|
|
|
self.cur.execute('INSERT INTO jids (jid, type) VALUES (?, ?)', (jid, typ))
|
2005-11-26 16:21:52 +01:00
|
|
|
try:
|
2005-12-05 18:22:50 +01:00
|
|
|
self.con.commit()
|
2005-11-26 16:21:52 +01:00
|
|
|
except sqlite.OperationalError, e:
|
2005-11-29 19:31:45 +01:00
|
|
|
print >> sys.stderr, str(e)
|
2005-12-05 18:22:50 +01:00
|
|
|
jid_id = self.cur.lastrowid
|
2005-11-23 20:12:52 +01:00
|
|
|
self.jids_already_in.append(jid)
|
|
|
|
return jid_id
|
|
|
|
|
2005-11-26 00:23:25 +01:00
|
|
|
def convert_human_values_to_db_api_values(self, kind, show):
|
|
|
|
'''coverts from string style to constant ints for db'''
|
|
|
|
if kind == 'status':
|
|
|
|
kind_col = constants.KIND_STATUS
|
|
|
|
elif kind == 'gcstatus':
|
|
|
|
kind_col = constants.KIND_GCSTATUS
|
|
|
|
elif kind == 'gc_msg':
|
|
|
|
kind_col = constants.KIND_GC_MSG
|
|
|
|
elif kind == 'single_msg_recv':
|
|
|
|
kind_col = constants.KIND_SINGLE_MSG_RECV
|
|
|
|
elif kind == 'single_msg_sent':
|
|
|
|
kind_col = constants.KIND_SINGLE_MSG_SENT
|
|
|
|
elif kind == 'chat_msg_recv':
|
|
|
|
kind_col = constants.KIND_CHAT_MSG_RECV
|
|
|
|
elif kind == 'chat_msg_sent':
|
|
|
|
kind_col = constants.KIND_CHAT_MSG_SENT
|
2005-11-28 17:41:34 +01:00
|
|
|
|
2005-11-26 00:23:25 +01:00
|
|
|
if show == 'online':
|
|
|
|
show_col = constants.SHOW_ONLINE
|
|
|
|
elif show == 'chat':
|
|
|
|
show_col = constants.SHOW_CHAT
|
|
|
|
elif show == 'away':
|
|
|
|
show_col = constants.SHOW_AWAY
|
|
|
|
elif show == 'xa':
|
|
|
|
show_col = constants.SHOW_XA
|
|
|
|
elif show == 'dnd':
|
|
|
|
show_col = constants.SHOW_DND
|
|
|
|
elif show == 'offline':
|
|
|
|
show_col = constants.SHOW_OFFLINE
|
2005-11-28 18:56:57 +01:00
|
|
|
elif show is None:
|
|
|
|
show_col = None
|
2005-11-28 18:34:43 +01:00
|
|
|
else: # invisible in GC when someone goes invisible
|
|
|
|
# it's a RFC violation .... but we should not crash
|
|
|
|
show_col = 'UNKNOWN'
|
2005-11-26 00:23:25 +01:00
|
|
|
|
|
|
|
return kind_col, show_col
|
|
|
|
|
|
|
|
def commit_to_db(self, values):
|
|
|
|
#print 'saving', values
|
2005-11-26 16:21:52 +01:00
|
|
|
sql = 'INSERT INTO logs (jid_id, contact_name, time, kind, show, message, subject) VALUES (?, ?, ?, ?, ?, ?, ?)'
|
2005-12-05 18:22:50 +01:00
|
|
|
self.cur.execute(sql, values)
|
2005-11-26 16:21:52 +01:00
|
|
|
try:
|
2005-12-05 18:22:50 +01:00
|
|
|
self.con.commit()
|
2005-11-26 16:21:52 +01:00
|
|
|
except sqlite.OperationalError, e:
|
2005-11-29 19:31:45 +01:00
|
|
|
print >> sys.stderr, str(e)
|
2005-11-26 00:23:25 +01:00
|
|
|
|
|
|
|
def write(self, kind, jid, message = None, show = None, tim = None, subject = None):
|
2005-11-23 20:12:52 +01:00
|
|
|
'''write a row (status, gcstatus, message etc) to logs database
|
|
|
|
kind can be status, gcstatus, gc_msg, (we only recv for those 3),
|
|
|
|
single_msg_recv, chat_msg_recv, chat_msg_sent, single_msg_sent
|
|
|
|
we cannot know if it is pm or normal chat message, we try to guess
|
2005-11-26 00:23:25 +01:00
|
|
|
see jid_is_from_pm() which is called by get_jid_id()
|
2005-11-23 20:12:52 +01:00
|
|
|
|
|
|
|
we analyze jid and store it as follows:
|
|
|
|
jids.jid text column will hold JID if TC-related, room_jid if GC-related,
|
|
|
|
ROOM_JID/nick if pm-related.'''
|
2005-11-24 19:29:45 +01:00
|
|
|
|
2005-12-05 14:56:02 +01:00
|
|
|
if self.jids_already_in == []: # only happens if we just created the db
|
2005-12-05 18:22:50 +01:00
|
|
|
self.con = sqlite.connect(LOG_DB_PATH, timeout = 20.0,
|
2005-11-27 13:22:07 +01:00
|
|
|
isolation_level = 'IMMEDIATE')
|
2005-12-05 18:22:50 +01:00
|
|
|
self.cur = self.con.cursor()
|
2005-11-24 19:29:45 +01:00
|
|
|
|
2005-11-03 09:50:35 +01:00
|
|
|
jid = jid.lower()
|
2005-11-23 20:12:52 +01:00
|
|
|
contact_name_col = None # holds nickname for kinds gcstatus, gc_msg
|
|
|
|
# message holds the message unless kind is status or gcstatus,
|
|
|
|
# then it holds status message
|
|
|
|
message_col = message
|
2005-11-26 00:23:25 +01:00
|
|
|
subject_col = subject
|
2005-11-23 20:12:52 +01:00
|
|
|
if tim:
|
|
|
|
time_col = int(float(time.mktime(tim)))
|
2005-04-16 00:02:13 +02:00
|
|
|
else:
|
2005-11-23 20:12:52 +01:00
|
|
|
time_col = int(float(time.time()))
|
|
|
|
|
2005-11-26 01:03:09 +01:00
|
|
|
kind_col, show_col = self.convert_human_values_to_db_api_values(kind,
|
|
|
|
show)
|
2005-11-28 17:41:34 +01:00
|
|
|
|
2005-11-26 00:23:25 +01:00
|
|
|
# now we may have need to do extra care for some values in columns
|
2005-11-23 20:12:52 +01:00
|
|
|
if kind == 'status': # we store (not None) time, jid, show, msg
|
|
|
|
# status for roster items
|
2005-11-26 01:03:09 +01:00
|
|
|
jid_id = self.get_jid_id(jid)
|
2005-11-28 18:34:43 +01:00
|
|
|
if show is None: # show is None (xmpp), but we say that 'online'
|
|
|
|
show_col = constants.SHOW_ONLINE
|
2005-07-21 23:39:47 +02:00
|
|
|
|
2005-11-23 20:12:52 +01:00
|
|
|
elif kind == 'gcstatus':
|
|
|
|
# status in ROOM (for pm status see status)
|
2005-11-28 18:34:43 +01:00
|
|
|
if show is None: # show is None (xmpp), but we say that 'online'
|
|
|
|
show_col = constants.SHOW_ONLINE
|
2005-11-23 20:12:52 +01:00
|
|
|
jid, nick = jid.split('/', 1)
|
2005-11-26 01:03:09 +01:00
|
|
|
jid_id = self.get_jid_id(jid, 'ROOM') # re-get jid_id for the new jid
|
2005-11-23 20:12:52 +01:00
|
|
|
contact_name_col = nick
|
2005-11-26 00:23:25 +01:00
|
|
|
|
2005-11-23 20:12:52 +01:00
|
|
|
elif kind == 'gc_msg':
|
|
|
|
if jid.find('/') != -1: # if it has a /
|
|
|
|
jid, nick = jid.split('/', 1)
|
2005-06-08 11:19:54 +02:00
|
|
|
else:
|
2005-11-23 20:12:52 +01:00
|
|
|
# it's server message f.e. error message
|
|
|
|
# when user tries to ban someone but he's not allowed to
|
|
|
|
nick = None
|
2005-11-26 01:03:09 +01:00
|
|
|
jid_id = self.get_jid_id(jid, 'ROOM') # re-get jid_id for the new jid
|
2005-11-23 20:12:52 +01:00
|
|
|
contact_name_col = nick
|
2005-11-26 01:03:09 +01:00
|
|
|
else:
|
|
|
|
jid_id = self.get_jid_id(jid)
|
2005-11-28 18:34:43 +01:00
|
|
|
|
|
|
|
if show_col == 'UNKNOWN': # unknown show, do not log
|
|
|
|
return
|
2005-11-26 01:03:09 +01:00
|
|
|
|
2005-11-26 00:23:25 +01:00
|
|
|
values = (jid_id, contact_name_col, time_col, kind_col, show_col,
|
|
|
|
message_col, subject_col)
|
|
|
|
self.commit_to_db(values)
|
2005-06-13 16:46:08 +02:00
|
|
|
|
2005-11-23 20:12:52 +01:00
|
|
|
def get_last_conversation_lines(self, jid, restore_how_many_rows,
|
|
|
|
pending_how_many, timeout):
|
|
|
|
'''accepts how many rows to restore and when to time them out (in minutes)
|
|
|
|
(mark them as too old) and number of messages that are in queue
|
|
|
|
and are already logged but pending to be viewed,
|
|
|
|
returns a list of tupples containg time, kind, message,
|
|
|
|
list with empty tupple if nothing found to meet our demands'''
|
|
|
|
jid = jid.lower()
|
|
|
|
jid_id = self.get_jid_id(jid)
|
2005-11-27 18:33:57 +01:00
|
|
|
now = int(float(time.time()))
|
|
|
|
timed_out = now - (timeout * 60) # before that they are too old
|
2005-11-23 20:12:52 +01:00
|
|
|
# so if we ask last 5 lines and we have 2 pending we get
|
|
|
|
# 3 - 8 (we avoid the last 2 lines but we still return 5 asked)
|
2005-12-05 18:22:50 +01:00
|
|
|
self.cur.execute('''
|
2005-11-23 20:12:52 +01:00
|
|
|
SELECT time, kind, message FROM logs
|
2005-11-27 18:33:57 +01:00
|
|
|
WHERE jid_id = %d AND kind IN (%d, %d, %d, %d) AND time > %d
|
2005-11-23 20:12:52 +01:00
|
|
|
ORDER BY time DESC LIMIT %d OFFSET %d
|
2005-11-26 23:02:07 +01:00
|
|
|
''' % (jid_id, constants.KIND_SINGLE_MSG_RECV, constants.KIND_CHAT_MSG_RECV,
|
2005-11-26 00:23:25 +01:00
|
|
|
constants.KIND_SINGLE_MSG_SENT, constants.KIND_CHAT_MSG_SENT,
|
2005-11-27 18:33:57 +01:00
|
|
|
timed_out, restore_how_many_rows, pending_how_many)
|
2005-11-23 20:12:52 +01:00
|
|
|
)
|
2005-04-16 00:02:13 +02:00
|
|
|
|
2005-12-05 18:22:50 +01:00
|
|
|
results = self.cur.fetchall()
|
2005-11-27 18:33:57 +01:00
|
|
|
results.reverse()
|
|
|
|
return results
|
2005-11-24 02:39:47 +01:00
|
|
|
|
|
|
|
def get_unix_time_from_date(self, year, month, day):
|
|
|
|
# year (fe 2005), month (fe 11), day (fe 25)
|
|
|
|
# returns time in seconds for the second that starts that date since epoch
|
|
|
|
# gimme unixtime from year month day:
|
|
|
|
d = datetime.date(year, month, day)
|
|
|
|
local_time = d.timetuple() # time tupple (compat with time.localtime())
|
|
|
|
start_of_day = int(time.mktime(local_time)) # we have time since epoch baby :)
|
|
|
|
return start_of_day
|
|
|
|
|
2005-11-23 20:12:52 +01:00
|
|
|
def get_conversation_for_date(self, jid, year, month, day):
|
|
|
|
'''returns contact_name, time, kind, show, message
|
|
|
|
for each row in a list of tupples,
|
|
|
|
returns list with empty tupple if we found nothing to meet our demands'''
|
|
|
|
jid = jid.lower()
|
|
|
|
jid_id = self.get_jid_id(jid)
|
2005-11-20 22:58:08 +01:00
|
|
|
|
2005-11-24 02:39:47 +01:00
|
|
|
start_of_day = self.get_unix_time_from_date(year, month, day)
|
2005-11-20 22:58:08 +01:00
|
|
|
|
2005-11-25 23:13:39 +01:00
|
|
|
seconds_in_a_day = 86400 # 60 * 60 * 24
|
|
|
|
last_second_of_day = start_of_day + seconds_in_a_day - 1
|
2005-11-20 22:58:08 +01:00
|
|
|
|
2005-12-05 18:22:50 +01:00
|
|
|
self.cur.execute('''
|
2005-11-23 20:12:52 +01:00
|
|
|
SELECT contact_name, time, kind, show, message FROM logs
|
|
|
|
WHERE jid_id = %d
|
|
|
|
AND time BETWEEN %d AND %d
|
|
|
|
ORDER BY time
|
2005-11-25 23:13:39 +01:00
|
|
|
''' % (jid_id, start_of_day, last_second_of_day))
|
2005-11-20 22:58:08 +01:00
|
|
|
|
2005-12-05 18:22:50 +01:00
|
|
|
results = self.cur.fetchall()
|
2005-11-23 20:12:52 +01:00
|
|
|
return results
|
2005-11-29 15:41:01 +01:00
|
|
|
|
|
|
|
def get_search_results_for_query(self, jid, query):
|
|
|
|
'''returns contact_name, time, kind, show, message
|
|
|
|
for each row in a list of tupples,
|
|
|
|
returns list with empty tupple if we found nothing to meet our demands'''
|
|
|
|
jid = jid.lower()
|
|
|
|
jid_id = self.get_jid_id(jid)
|
|
|
|
if False: #query.startswith('SELECT '): # it's SQL query
|
|
|
|
try:
|
2005-12-05 18:22:50 +01:00
|
|
|
self.cur.execute(query)
|
2005-11-29 15:41:01 +01:00
|
|
|
except sqlite.OperationalError, e:
|
|
|
|
results = [('', '', '', '', str(e))]
|
|
|
|
return results
|
|
|
|
|
|
|
|
else: # user just typed something, we search in message column
|
|
|
|
like_sql = '%' + query + '%'
|
2005-12-05 18:22:50 +01:00
|
|
|
self.cur.execute('''
|
2005-11-30 18:30:58 +01:00
|
|
|
SELECT contact_name, time, kind, show, message, subject FROM logs
|
2005-11-29 15:41:01 +01:00
|
|
|
WHERE jid_id = ? AND message LIKE ?
|
|
|
|
ORDER BY time
|
2005-11-30 22:56:42 +01:00
|
|
|
''', (jid_id, like_sql))
|
2005-11-29 15:41:01 +01:00
|
|
|
|
2005-12-05 18:22:50 +01:00
|
|
|
results = self.cur.fetchall()
|
2005-11-29 15:41:01 +01:00
|
|
|
return results
|
|
|
|
|
2005-12-21 11:54:41 +01:00
|
|
|
def get_days_with_logs(self, jid, year, month, max_day):
|
|
|
|
'''returns the list of days that have logs (not status messages)'''
|
2005-11-24 02:39:47 +01:00
|
|
|
jid = jid.lower()
|
|
|
|
jid_id = self.get_jid_id(jid)
|
2005-12-21 11:54:41 +01:00
|
|
|
list = []
|
|
|
|
|
|
|
|
# First select all date of month whith logs we want
|
|
|
|
start_of_month = self.get_unix_time_from_date(year, month, 1)
|
2005-11-24 02:39:47 +01:00
|
|
|
seconds_in_a_day = 86400 # 60 * 60 * 24
|
2005-12-21 11:54:41 +01:00
|
|
|
last_second_of_month = start_of_month + (seconds_in_a_day * max_day) - 1
|
|
|
|
|
2005-12-05 18:22:50 +01:00
|
|
|
self.cur.execute('''
|
2005-12-21 11:54:41 +01:00
|
|
|
SELECT time FROM logs
|
2005-11-24 02:39:47 +01:00
|
|
|
WHERE jid_id = %d
|
|
|
|
AND time BETWEEN %d AND %d
|
2005-11-29 14:18:21 +01:00
|
|
|
AND kind NOT IN (%d, %d)
|
2005-12-21 11:54:41 +01:00
|
|
|
ORDER BY time
|
|
|
|
''' % (jid_id, start_of_month, last_second_of_month,
|
2005-11-29 14:18:21 +01:00
|
|
|
constants.KIND_STATUS, constants.KIND_GCSTATUS))
|
2005-12-21 11:54:41 +01:00
|
|
|
result = self.cur.fetchall()
|
|
|
|
|
|
|
|
#Copy all interesant time in a temporary table
|
|
|
|
self.cur.execute('CREATE TEMPORARY TABLE blabla(time,INTEGER)')
|
|
|
|
for line in result:
|
|
|
|
self.cur.execute('''
|
|
|
|
INSERT INTO blabla (time) VALUES (%d)
|
|
|
|
''' % (line[0]))
|
|
|
|
|
|
|
|
#then search in this small temp table for each day
|
|
|
|
for day in xrange(1, max_day):
|
|
|
|
start_of_day = self.get_unix_time_from_date(year, month, day)
|
|
|
|
last_second_of_day = start_of_day + seconds_in_a_day - 1
|
|
|
|
|
|
|
|
# just ask one row to see if we have sth for this date
|
|
|
|
self.cur.execute('''
|
|
|
|
SELECT time FROM blabla
|
|
|
|
WHERE time BETWEEN %d AND %d
|
|
|
|
LIMIT 1
|
|
|
|
''' % (start_of_day, last_second_of_day))
|
|
|
|
result = self.cur.fetchone()
|
|
|
|
if result:
|
|
|
|
list[0:0]=[day]
|
|
|
|
|
|
|
|
#Delete temporary table
|
|
|
|
self.cur.execute('DROP TABLE blabla')
|
2005-12-05 18:22:50 +01:00
|
|
|
result = self.cur.fetchone()
|
2005-12-21 11:54:41 +01:00
|
|
|
return list
|
2005-11-30 22:56:42 +01:00
|
|
|
|
|
|
|
def get_last_date_that_has_logs(self, jid):
|
|
|
|
'''returns last time (in seconds since EPOCH) for which
|
|
|
|
we had logs (excluding statuses)'''
|
|
|
|
jid = jid.lower()
|
|
|
|
jid_id = self.get_jid_id(jid)
|
2005-12-05 18:22:50 +01:00
|
|
|
self.cur.execute('''
|
2005-11-30 22:56:42 +01:00
|
|
|
SELECT time FROM logs
|
|
|
|
WHERE jid_id = ?
|
|
|
|
AND kind NOT IN (?, ?)
|
|
|
|
ORDER BY time DESC LIMIT 1
|
|
|
|
''', (jid_id, constants.KIND_STATUS, constants.KIND_GCSTATUS))
|
2005-12-26 19:23:57 +01:00
|
|
|
|
|
|
|
results = self.cur.fetchone()
|
|
|
|
if len(results) > 0:
|
|
|
|
result = results[0]
|
|
|
|
else:
|
|
|
|
result = None
|
|
|
|
|
2005-11-30 22:56:42 +01:00
|
|
|
return result
|