All pastes #215154 Raw Edit

Anonymous

public text v1 · immutable
#215154 ·published 2006-10-22 12:55 UTC
rendered paste body
import os
import sys
import re
import smtplib
import socket

version='0.1'
#Config vars
basedir='/etc/'
userfile=basedir + 'users'
backupuserfile=basedir + 'users.bak' #saves previous state of users
lockfile=basedir + 'otrs.lck' #has pid in it

users=[]
class UserFile:
	def __init__(this):
		#throws exception
		this.check_lockfile()
	
	#returns users array
	def readUsers(this):
		global users
		users = []
		uf = file(userfile)
		users = uf.readlines()
		uf.close()

	#writes users array to file
	def writeUsers(this,users):
		uf = file(userfile,'w')
		uf.writelines(users)
		uf.flush()
		uf.close()
		
	#renames current user file to user back
	def move_usersfile(this):
		os.rename(userfile, backupuserfile)	
		
	#locks users file (users.lock has pid in it)
	def lockUsers(this):
		lf = open(lockfile,'w')
		lf.write('%d' % os.getpid())
		lf.close()
	
	#removes lock file
	def unlockUsers(this):
		os.remove(lockfile)
	
	#checks if the lock file is there and other process is existant
	def check_lockfile(this):
		if os.path.exists(lockfile):
			lf = open(lockfile)
			pidnr = lf.read()
			lf.close()
			#check if pid exists
			#if yes then exit(1) + error message, if no then remove lock file
			#maybe also need to check for process name, cause pids cicle!
			if os.path.exists('/proc/%s' % pidnr) and pidnr:
				sys.exit('another process already running!!')
			else:
				print("found stale lockfile, removing")
				os.remove(lockfile)	

class UserManager:
	def rotate(this):
		uname = users.pop(0)
		users.append(uname)
		
	def getnext(this):
		if len(users) < 1:
			sys.exit("users list is empty\n%s" % users)
		return users[0]
		
	def sendmail(this):
		username = this.getnext()
		
		mailtext = 'Subject: OTRS-Verantwortlicher\n\ndiese Woche %s ist verantwortlich fuer OTRS' % username
		#send mail to users that user has work to do
		sm = smtplib.SMTP()
		for user in users:
			sm.sendmail("otrs-watcher", user, mailtext)
	
	#changes owner of the otrs-imap-folder, so the users can mark messages as red
	def give_rights(this):
		username = this.getnext()
		
def core():
	ufile = UserFile()
	ufile.lockUsers()
	ufile.readUsers()
	
	umanager = UserManager()
	umanager.sendmail()
	umanager.give_rights()
	umanager.rotate()
	
	ufile.move_userfile()
	ufile.writeUsers()
	ufile.unlockUsers()

if os.getuid() != 0:
	sys.exit("this program needs to be run as root!")

core()
sys.exit("all done.")