view av_log.py @ 508:64f8666690d8

README update (Logical change 1.149)
author optonline.net!jeffpc
date Fri, 16 Jan 2004 02:53:48 +0000
parents 524a0baf7f24
children 903781c081b7
line wrap: on
line source

#/*
# * AV Admin - Helps to manage an AV department
# *
# * Copyright (C) 2003, 2004 Josef "Jeff" Sipek
# *
# * 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; either version 2 of the License, or
# * (at your option) any later version.
# *
# * 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.
# *
# * You should have received a copy of the GNU General Public License
# * along with this program; if not, write to the Free Software
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# *
# * $Id$
# */

import re

import av_debug

SYSLOG	= 1
SECLOG	= 2
EQLOG	= 3
USERLOG	= 4
WORKLOG	= 5

def syslog(conn,text):
	genericlog(conn,text,SYSLOG)

def seclog(conn,text):
	genericlog(conn,text,SECLOG)
		
def eqlog(conn,text):
	genericlog(conn,text,EQLOG)

def userlog(conn,text):
	genericlog(conn,text,USERLOG)
	
def worklog(conn,text):
	genericlog(conn,text,WORKLOG)

def genericlog(conn,text,logtype):
	log = Log(conn)
	log.autosetIP()
	log.autosetSID()
	log.autosetEDATE()
	log.setPRIV(logtype)
	log.setMESS(text)
	log.Add()
	
class Log:
	""" Class to hold all log data and more """
	def __init__(self,conn,id=-1):
		""" Set all variables to their default values

		does not return anything """
		self.conn = conn
		self.db = conn.db
		self.html = conn.html
		self.user = conn.user
		self.utils = conn.utils
		
		self.__debug = {"usable":0}
		
		if (id==-1):
			self.__ID    = -1
			self.__IP    = ""
			self.__SID   = ""
			self.__EDATE = ""
			self.__PRIV  = 0
			self.__MESS  = ""
		else:
			self.Load(id)
			
	def Load(self,id):
		dbid = self.db.newid()
		dbid.execute("SELECT `ip`, `sid`, `edate`, `priv`, `mess` FROM `log` WHERE `id` = " + str(id) + ";")
		av_debug.BUGON(dbid.rowcount()!=1, "Number of log messages is NOT equal to 1") # FIXME: get rid of this, and add proper logging
		rec = dbid.fetchone()
			
		self.__ID   = id
		self.__IP    = rec[0]
		self.__SID   = rec[1]
		self.__EDATE = rec[2]
		self.__PRIV  = rec[3]
		self.__MESS  = self.utils.desqlify(rec[4])
		self.__debug["usable"] = 1
	
	def getID(self):
		av_debug.BUGON((not self.__debug["usable"]), "Using unusable data") # FIXME: include file name and line number
		return self.__ID
		
	def setID(self,id):
		self.__ID = id
		self.__debug["usable"] = 0
		
	def getIP(self):
		av_debug.BUGON((not self.__debug["usable"]), "Using unusable data") # FIXME: include file name and line number
		return self.__IP
		
	def setIP(self,ip):
		self.__IP = ip
		self.__debug["usable"] = 0
		
	def autosetIP(self):
		self.__IP = self.conn.getIP()
		self.__debug["usable"] = 0
		
	def getSID(self):
		av_debug.BUGON((not self.__debug["usable"]), "Using unusable data") # FIXME: include file name and line number
		return self.__SID
		
	def setSID(self,sid):
		self.__SID = sid
		self.__debug["usable"] = 0
		
	def autosetSID(self):
		self.__SID = self.user.getSID()
		self.__debug["usable"] = 0
		
	def getEDATE(self):
		av_debug.BUGON((not self.__debug["usable"]), "Using unusable data") # FIXME: include file name and line number
		return self.__EDATE
		
	def setEDATE(self,edate):
		self.__EDATE = edate
		self.__debug["usable"] = 0
	
	def autosetEDATE(self):
		self.__EDATE = self.conn.text_now()
		self.__debug["usable"] = 0
		
	def getPRIV(self):
		av_debug.BUGON((not self.__debug["usable"]), "Using unusable data") # FIXME: include file name and line number
		return self.__PRIV
		
	def getPRIVStr(self):
		av_debug.BUGON((not self.__debug["usable"]), "Using unusable data") # FIXME: include file name and line number
		if (self.__PRIV==SYSLOG):
			return "Sys"
		if (self.__PRIV==SECLOG):
			return "Sec"
		if (self.__PRIV==WORKLOG):
			return "Work"
		if (self.__PRIV==USERLOG):
			return "User"
		if (self.__PRIV==EQLOG):
			return "Equip"
		return "unknown"
		
	def getPRIVFile(self):
		av_debug.BUGON((not self.__debug["usable"]), "Using unusable data") # FIXME: include file name and line number
		if (self.__PRIV==SYSLOG):
			return "sys_log.py"
		if (self.__PRIV==SECLOG):
			return "sec_log.py"
		if (self.__PRIV==WORKLOG):
			return "work_log.py"
		if (self.__PRIV==USERLOG):
			return "user_log.py"
		if (self.__PRIV==EQLOG):
			return "eq_log.py"
		return ""
		
	def setPRIV(self,priv):
		self.__PRIV = priv
		self.__debug["usable"] = 0
		
	def getMESS(self):
		av_debug.BUGON((not self.__debug["usable"]), "Using unusable data") # FIXME: include file name and line number
		return self.__MESS
		
	def setMESS(self,mess):
		self.__MESS = mess
		self.__debug["usable"] = 0
		
	def Add(self):
		""" Adds the variables into the DB, and sets the correct ID on success
		
		returns nothing """
		dbid = self.db.newid()
		dbid.execute("INSERT INTO `log` (`ip`,`sid`,`edate`,`priv`,`mess`) VALUES (\"" + self.__IP + "\", \"" + self.__SID + "\", \"" + self.__EDATE + "\", " + str(self.__PRIV) + ", '" + self.utils.sqlify(self.__MESS) + "');")
		dbid.execute("SELECT `id` FROM `log` WHERE `ip` = \"" + self.__IP + "\" AND `sid` = \"" + self.__SID + "\" AND `edate` = \"" + self.__EDATE + "\";")
		rec = dbid.fetchone()
		if (rec):
			self.__ID = rec[0]
			self.__debug["usable"] = 1
	
if __name__ == "__main__":
	print "ERROR"