Skip to content

Instantly share code, notes, and snippets.

@T31337
Last active July 12, 2019 20:13
Show Gist options
  • Save T31337/b08ac20011b1a395a26f to your computer and use it in GitHub Desktop.
Save T31337/b08ac20011b1a395a26f to your computer and use it in GitHub Desktop.
PyTimeBomb!
import platform
from tkinter import *
from tkinter import messagebox
import configparser, pickle
import os, time, subprocess
# Show Debug Messages
DEBUG = True
def printDebugBanner():
print(".-------------------.")
print("| DEBUGGING ENABLED |")
print("'-------------------'")
class TimeBomb:
#Testing: Is This Helpful?
DisableAfterExecution = 0
EnablePasswordLock = False
#Global Variables, Please Don't Play With These
conf = configparser.ConfigParser()#global config manager
Password = ""
def __init__(self):
self.command = ""
self.getConfig()
if DEBUG:
printDebugBanner()
print('os: ' + platform.system())
tos = platform.system()
if tos == 'Linux':
self.command = 'shutdown -hP +5 "Computer Will shutdown In 5 Minutes!"'
if tos == "Windows":
self.command = 'shutdown /s /t 500 /c "Computer Will Shutdown In 5 Minutes!\n\nSave Your Data!\n\nYou Will Not Be Warned Again!"'
else:
if DEBUG:
print("Sorry, Mac OSX Support Not Yet Implemented...")
messagebox.showinfo("TimeBomb!","Sorry, Mac OSX Not Yet Supported")
self.CheckCode()
def saveConfig(self):
if DEBUG:
print('Saving Settings...')
print(".--------.")
print('|Settings|')
print("'--------'")
print("Code: "+self.conf.get("TimeBomb", "Code"))
print("Active: "+self.conf.get("TimeBomb", "Active"))
print("Hour: "+self.conf.get("TimeBomb", "KillHour"))
print("Minuet: "+self.conf.get("TimeBomb", "KillMin"))
print("AutoDisable: "+self.conf.get("TimeBomb", "AutoDisable"))
print("PasswordEnabled: "+self.conf.get("TimeBomb", "PasswordLock"))
print('--------------------')
try:
file = open('TimeBomb.settings', 'wb')
pickle.dump(self.conf, file)
file.close()
except:
messagebox.showerror("TimeBomb!","Error:\nFailed To Save TimeBomb Settings!")
if DEBUG:
print('Error:\nFailed To Save Settings...')
#messagebox.showinfo(title='TimeBomb!', message='Settings Saved Successfully!')
if DEBUG:
print('Settings Saved Successfully!')
def getConfig(self):
dir = os.getcwd()
#Check For Config File
if os.path.exists('./TimeBomb.settings'):
#Config Exists, Try To Load It!
if DEBUG:
print('TimeBomb Config Exists...')
try:
cfgFile = open('TimeBomb.settings', 'rb')
self.conf=(pickle.load(cfgFile))
cfgFile.close()
except:
msg = messagebox.showerror("TimeBomb!","Error Loading Settings")
if DEBUG:
print("Error Loading Settings...")
if DEBUG:
print('Settings Loaded Successfully!')
else:
if DEBUG:
print('No Existing Settings...\nCreating Default Settings Now...')
try:
file = open('TimeBomb.settings','wb')
self.conf.add_section("TimeBomb")
self.conf.set("TimeBomb","Code", "")
self.conf.set("TimeBomb","Active",'False')
self.conf.set("TimeBomb","KillHour", value='23')
self.conf.set("TimeBomb", "KillMin", value='20')
self.conf.set("TimeBomb", "AutoDisable", value="0")
self.conf.set("TimeBomb", "PasswordLock", 'False')
# We Don't Want To Do This, It Would Render Settings In PlainText
# This Would Allow External Modifications And Reveal The LockCode
##self.conf.write(file)
#Save The Settings File
pickle.dump(self.conf,file)
file.close()
except:
msg = messagebox.showerror("TimeBomb!","OOPS!\nAn Error Has Been Encountered While Trying To Create The Default Settings\n \
This Could Cause The Program To Malfunction")
if DEBUG:
print('Error Creating Default Settings...')
if DEBUG:
print('Default Settings Created Successfully!')
def NewLockCode(self,opt=None):
def SavePasswd(opt=None):
if self.passwd.getvar(name='passwd')==self.passwd2.getvar(name='passwd2'):
self.Password = self.passwd.getvar(name='passwd')
self.conf.set('TimeBomb', 'Code', self.Password)
self.saveConfig()
if DEBUG:
print("Password Saved!")
print("Password: "+self.Password)
self.newpas.destroy()
else:
msg = messagebox.showerror(title="TimeBomb!",message="Passwords Do Not Match!\r\nPlease Try Again!")
self.passwd.setvar(name='passwd', value='')
self.passwd2.setvar(name="passwd2",value='')
self.passwd.focus()
self.conf.set("TimeBomb","PasswordLock","True")
self.conf.set("TimeBomb","Code",self.Password)
self.saveConfig()
self.newpas = Tk()
self.newpas.title('TimeBomb!')
lbl = Label(self.newpas, text='Password:').grid(row=0, column=0)
self.passwd = Entry(self.newpas,show='*',textvariable='passwd')
self.passwd.grid(row=1,column=0)
self.passwd2 = Entry(self.newpas,show='*',textvariable='passwd2')
self.passwd2.grid(row=2,column=0)
SaveBtn = Button(self.newpas, text='Save!', command=SavePasswd).grid(row=3,column=0)
self.newpas.bind("<Return>", SavePasswd)
#SaveBtn.grid(row=3, column=0)
self.newpas.mainloop()
def Locked(self):
self.pas = Tk()
self.pas.bind('<Return>',self.Unlock)
self.pas.title('TimeBomb!')
lbl = Label(self.pas, text='Password:').grid(row=0, column=0)
self.passwd = Entry(self.pas,show='*',textvariable='passwd')
self.passwd.grid(row=1,column=0)
SaveBtn = Button(self.pas, text='Unlock!', command=self.Unlock)
SaveBtn.grid(row=3, column=0)
self.passwd.focus()
self.pas.mainloop()
if (self.conf.getboolean("TimeBomb", "Active")):
if DEBUG:
print('TimeBomb Active!')
self.TimeCheck()
else:
if DEBUG:
print("TimeBomb Not Active!")
def Unlock(self,opt=None):
# opt=None Hack To Allow Bind Enter To Unlock Function
if self.pas.getvar('passwd')==self.conf.get("TimeBomb","Code"):
self.pas.destroy()
self.GUI()
else:
msg = messagebox.showerror(title="TimeBomb!",message="Wrong Password! \n Go Away!")
self.pas.destroy()
def CheckCode(self):
self.getConfig()
if self.conf.get("TimeBomb","Code"):
if DEBUG:
print('TimeBomb Code: '+self.conf.get('TimeBomb',"Code"))
if self.conf.getboolean("TimeBomb", "PasswordLock"):
self.Locked()
else:
self.GUI()
else:
if self.conf.get("TimeBomb", "Code") != "":
self.NewLockCode()
else:
self.GUI()
def SetUp_Password(self):
self.EnablePasswordLock = True
self.NewLockCode()
def DisablePassword(self):
self.EnablePasswordLock = False
self.conf.set("TimeBomb", "PasswordLock", 'False')
self.conf.set("TimeBomb", "Code", "")
self.saveConfig()
def GUI(self):
#Build GUI
self.root = Tk()
#self.root.geometry(newGeometry="300x180")
self.root.resizable(width=False, height=False)
self.root.title('TimeBomb!')
lbl = Label(self.root, text='Execution Time:')
lbl.grid(row=0, column=0)
self.KillHourSel = Spinbox(self.root, from_=00, to=23,value=23)
self.KillHourSel.grid(row=1, column=0)
self.KillMinSel = Spinbox(self.root, from_=00, to=59,value=20)
self.KillMinSel.grid(row=2, column=0)
self.killHour = self.KillHourSel.get()
self.killMin = self.KillMinSel.get()
self.cmdlbl = Label(self.root,text='Command:').grid(row=3,column=0)
self.cmd = Entry(self.root,textvariable='command',width=47)
self.cmd.setvar(name='command',value=self.command)
self.cmd.grid(row=4, column=0, padx=7)
self.AutoDisable = Checkbutton(self.root,text="Disable After Execution", variable="AutoDisable")
self.AutoDisable.grid(row=5,column=0)
self.PasswordLockOption = Button(self.root)
#self.PasswordLockOption = Button(self.root,text="Password Lock",command=self.SetUp_Password)
self.PasswordLockOption.grid(row=6, column=0, pady=7)
if(self.conf.getboolean("TimeBomb", "Active")):
self.btnEnable=Button(self.root, text="Disable!", command=self.DisableTimeBomb)
else:
self.btnEnable = Button(self.root, text='Enable!', command=self.EnableTimeBomb)
self.btnEnable.grid(row=7, column=0, pady=7)
self.btnSave=Button(self.root, text="Save Settings!", command=self.saveConfig)
self.btnSave.grid(row=8, column=0, pady=7)
self.command = self.cmd.getvar(name='command')
if DEBUG:
print('TimeBomb Active: ' + self.conf.get('TimeBomb', "Active"))
if DEBUG:
print("AutoDisable: "+self.AutoDisable.getvar(name="AutoDisable"))
self.DisableAfterExecution = self.AutoDisable.getvar(name="AutoDisable")
self.conf.set("TimeBomb", "AutoDisable", self.DisableAfterExecution)
if(self.conf.get("TimeBomb","PasswordLock")=="True"):
self.PasswordLockOption["text"]="Disable Password"
self.PasswordLockOption['command']=self.DisablePassword
else:
self.PasswordLockOption['text']="Password Lock"
self.PasswordLockOption['command']=self.SetUp_Password
#Show GUI
self.root.mainloop()
if(self.conf.getboolean("TimeBomb","Active")):
self.TimeCheck()
def TimeCheck(self):
self.killHour = self.conf.get('TimeBomb','KillHour')
self.killMin = self.conf.get('TimeBomb','KillMin')
while 1:
print('TimeBomb Set For => '+self.killHour+':'+self.killMin)
# Get Current Time/Date And Parse Out The Time Info We Need...
ttime = str(time.ctime(time.time())).split(' ')
clockTime = ttime[3]
if DEBUG:
print('clockTime: '+clockTime)
stime = str(clockTime).split(':')
if DEBUG:
print('stime: '+str(stime))
hours = stime[0]
mins = stime[1]
if int(hours) >= int(self.killHour) and int(mins) >= int(self.killMin):
if DEBUG:
print('Time: ' + clockTime)
if self.conf.getboolean('TimeBomb', 'Active'):
print('TimeBomb Execution Commencing...')
subprocess.call(self.command,shell=True)
#Should We Disable the TimeBomb After Command Execution?
if self.DisableAfterExecution:
#Command Has Been Executed, Disable The TimeBomb And Update The Config
self.conf.set('TimeBomb','Active','False')
self.saveConfig()
break
else:
if DEBUG:
print('TimeBomb Is Not Enabled...')
break
else:
if DEBUG:
print('Time:> ' + hours + ':' + mins + '\nTimeBomb Is Waiting...')
time.sleep(30)
def EnableTimeBomb(self):
#Activate The TimeBomb
self.conf.set('TimeBomb', 'Active', 'True')
self.conf.set('TimeBomb', 'KillHour', self.KillHourSel.get())
self.conf.set('TimeBomb', 'KillMin', self.KillMinSel.get())
self.saveConfig()
self.btnEnable['text']="Disable!"
self.btnEnable['command']=self.DisableTimeBomb
messagebox.showinfo(title='TimeBomb!', message='TimeBomb Enabled!')
self.root.destroy() # Destroy/Close The GUI
self.TimeCheck()#Time Check Wait Loop
def DisableTimeBomb(self):
self.conf.set('TimeBomb', 'Active', 'False')
self.saveConfig()
messagebox.showinfo(title='TimeBomb!', message='TimeBomb Disabled!')
self.btnEnable['text']="Enable!"
self.btnEnable['command']=self.EnableTimeBomb
if __name__ == '__main__':
TimeBomb()
@T31337
Copy link
Author

T31337 commented Jul 12, 2019

Fixed Blank Window During TimeCheck Function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment