Advertisement
BeamNG_IRC

Untitled

Sep 18th, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 16.58 KB | None | 0 0
  1. """
  2. By Dan Jones - 2014
  3. Contents of 'critical.fag' (needed script, without it, it will refuse everything)
  4. _______________________________________
  5. # critical example script
  6. [green];
  7. //
  8. Multi line comment
  9. everything here is ignored
  10. \\
  11. print; Hello, World!
  12. _______________________________________
  13. """
  14. try:
  15.     from Tkinter import *
  16.     import tkFileDialog
  17.     import re
  18.     import Tkinter
  19.     import os
  20.     import time
  21.     import tkMessageBox
  22.     import calendar
  23.     import socket
  24.     from random import *
  25.     import sys
  26.     import autopy               # (
  27.     import winsound
  28.     import win32com            
  29.     import win32com.client      # downloaded libraries
  30.     import ctypes
  31.     import pyttsx               # )
  32. except:
  33.     print '******************************************\nA number of libraries are missing.\nYou may be unable to run certain scripts.\n******************************************'
  34. class WTCW: # Huge thanks to Thomas Fischer (tdev) for this
  35.     WRAPPERS = ['{', '}']
  36.     STD_INPUT_HANDLE = -10 ; STD_OUTPUT_HANDLE= -11 ; STD_ERROR_HANDLE = -12
  37.     FG_BLUE = 0x01 ; FG_GREEN= 0x02 ; FG_RED  = 0x04 ; FG_INTENSITY = 0x08 ; BG_BLUE = 0x10 ; BG_GREEN= 0x20 ; BG_RED  = 0x40 ; BG_INTENSITY = 0x80
  38.     colors = {'r': FG_RED, 'g':FG_GREEN, 'b':FG_BLUE, 'R':BG_RED, 'G':BG_GREEN, 'B':BG_BLUE, 'i':FG_INTENSITY, 'I':BG_INTENSITY, 'w':FG_RED|FG_GREEN|FG_BLUE }
  39.     def _get_csbi_attributes(self):
  40.         import struct
  41.         csbi = ctypes.create_string_buffer(22) ; res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(self.handle, csbi) ; assert res
  42.         (bufx, bufy, curx, cury, wattr, left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
  43.         return wattr
  44.     def __init__(self, stream, defColor = ''):
  45.         self.stream = stream ; self.defColor = self._parseColor(defColor)
  46.         self.handle = ctypes.windll.kernel32.GetStdHandle(self.STD_OUTPUT_HANDLE)
  47.         self.reset  = self._get_csbi_attributes()
  48.     def __del__(self): self.resetColor()
  49.     def _parseColor(self, str):
  50.         colval = 0
  51.         for c in str:
  52.             if c.lower() == 'x':
  53.                 self.resetColor()
  54.                 break
  55.             colval |= self.colors[c]
  56.         return colval
  57.     def write(self, data):
  58.         if self.defColor != 0:
  59.             self.setColour(defColor)
  60.         parts = data.split(self.WRAPPERS[0])
  61.         for part in parts:
  62.             f = part.find(self.WRAPPERS[1])
  63.             if f != -1:
  64.                 self.setColour(self._parseColor(part[0:f]))
  65.                 self.stream.write(part[f+len(self.WRAPPERS[1]):])
  66.             else:
  67.                 self.stream.write(part)
  68.             self.stream.flush()
  69.         #self.resetColor() #uncomment this to reset the color after every invocation
  70.     def __getattr__(self, attr): return getattr(self.stream, attr)
  71.     def setColour(self, col): ctypes.windll.kernel32.SetConsoleTextAttribute(self.handle, col)
  72.     def resetColor(self): ctypes.windll.kernel32.SetConsoleTextAttribute(self.handle, self.reset)
  73. sys.stdout = WTCW(sys.stdout)
  74. def main():
  75.     global commands, color, substring, line, reason, noerror, mlcomment, arg, com, file, script, file_path # globalise for use outside of main()
  76.     debug = 0 # switch to 1 for debugging information
  77.     if os.path.isfile("critical.fag") == False: # before anything check for this script, if it doesn't exist, make it
  78.         file = open("critical.fag", "ab+") # lets make the file
  79.         file.write('# critical example script\n')
  80.         file.write('[green];\n')
  81.         file.write('//\n')
  82.         file.write('Multi line comment\n')
  83.         file.write('everything here is ignored\n')
  84.         file.write('\\\\\n') # needed due to escape characters
  85.         file.write('retype; Hello, World!')
  86.         file.close()
  87.         print 'created default script'
  88.     mlcomment = 0
  89.     file_path = 'null'
  90.     color = ''
  91.     commands = ['msgbox','retype','print','//','\\','[debug]','speak','beep','made','test','mouse','mouse_snap','wait','length','cal','getsubstring','[green]','binary','[red]','[blue]','list','send','randint','input','randstring','commands']
  92.     noerror = 0 # allows error script to be raised
  93.     args = 0
  94.     arg = str(sys.argv) # command line argument
  95.     try:
  96.         file = 'null' # set to null for the error script
  97.         arg = arg.split(" ")[1] # command line argument
  98.         arg = arg.replace("]","").replace("'","") # command line argument
  99.         args = 1
  100.         if arg[-4:] != '.fag':
  101.             print 'Unknown file format, attempting to open..'
  102.     except:
  103.         args = 0 # disables argument if none given
  104.     line = 0
  105.     run = 0 # run == 1 if no script found
  106.     if args == 0:
  107.         try: # the humor of a programmer is quite broad. script is .fag hahaha
  108.              # FAG = File Application Generator
  109.             file = open("critical.fag") # default script, if all else fails, open this
  110.             master = Tk() # start open file dialog
  111.             master.withdraw() # hiding tkinter window
  112.             file_path = tkFileDialog.askopenfilename(title="Open Script - (*.FAG)", filetypes=[("File App Generator",".FAG")]) # what kind of dialog?
  113.             if file_path != "":
  114.                 file = open((file_path)) # final file
  115.             else:
  116.                 window = Tkinter.Tk()
  117.                 window.wm_withdraw()
  118.                 tkMessageBox.showinfo(title='Error', message='No script selected, running default script')
  119.                 master.quit()
  120.         except:
  121.             reason = 'Internal script reading error\n\nDoes the default script exist?' # theoretically, this should never occur
  122.             com = 'null error'
  123.             arg = ' internal' # space at begining is critical!
  124.             print '\n' # for looks
  125.             error()
  126.         arg = '', str(file) # space at the start is critical!
  127.     else:
  128.         try:
  129.             file = open((arg)[1:])
  130.         except:
  131.             arg = arg.replace('-','')
  132.             reason = 'Unable to locate script: ' + arg # had to do it this way, otherwise it's a tuple
  133.             noerror = 1 # avoids calling error on a script line
  134.             error()
  135.     while 1: # script loop starts here
  136.         line = int((line)) + 1 # set line to line + 1 = current line
  137.         com = file.readline() # set com to contain current lines content
  138.         if com.isspace():
  139.             ignore()
  140.         if not com: # the following code executes once parsing of the file is done
  141.             if run == 0: # run will == 0 if no script was executed (this excludes comments)
  142.                 print '{ri}***********************************'
  143.                 print 'Error: No script found to execute!'
  144.                 print '***********************************'
  145.             break # ends the script, there has to be a better way!
  146.         if com.find('//') != -1:
  147.             mlcomment = 1
  148.         if com.find('\\') != -1:
  149.                 mlcomment = 0 # makes the script start reading script again!
  150.                 com = '# multi lined comment' # really a hack, it is horrible script lol
  151.         if mlcomment == 1:
  152.                 com = '# multil lined comment' # really a hack, it is horrible script lol
  153.         try:
  154.             comment = com[0]
  155.             prefix = com.split(';')[0]
  156.             reason = 'Unknown command: "' + prefix + '"' # again, had to do it this way
  157.             leng = len((prefix)) + 1
  158.             script = com.split(';')[1]
  159.             if prefix not in commands:
  160.                 error()
  161.         except:
  162.             if comment.find('#') != -1 or com.isspace():
  163.                 ignore()
  164.             else:
  165.                 error()
  166.         if comment.find('#') != -1:
  167.             code = 3
  168.         else:
  169.             com = com[(leng):]
  170.             if prefix == 'list':
  171.                 run = 1 # tells the parser that a valid command was found
  172.                 count = 0
  173.                 for x in com:
  174.                     if script[0].isspace():
  175.                         script = script[1:]
  176.                     else:
  177.                         count = count + 1
  178.                         if script[-1:] == '=':
  179.                             script.replace('=', '')
  180.                             if x == '=':
  181.                                 count = count - 1
  182.                             print x, count
  183.                         else:
  184.                             print x
  185.             parse = script
  186.             getsubstring(parse) # get substring
  187.             script = re.sub(r'\(.+?\)\s?','',script) # remove ()
  188.             if prefix == 'print':
  189.                 run = 1
  190.                 if script[0].isspace():
  191.                     script = script[1:]
  192.                 print script
  193.             if prefix == 'msgbox':
  194.                 run = 1
  195.                 if script[0].isspace():
  196.                     script = script[1:]
  197.                 try:
  198.                     title = script.split(',')[0]
  199.                     msg = script.split(',')[1]
  200.                     window = Tkinter.Tk()
  201.                     window.wm_withdraw()
  202.                     tkMessageBox.showinfo(title=(title), message=(msg))
  203.                 except:
  204.                     reason = 'Syntax error, try: msgbox; title,message'
  205.                     error()
  206.             if prefix == 'send':
  207.                 run = 1
  208.                 if script[0].isspace():
  209.                     script = script[1:]
  210.                     send = script
  211.                     s = socket.socket()
  212.                     host = socket.gethostname()
  213.                     port = 1234
  214.                     s.bind((host, port))
  215.                     print 'waiting to accept client...'
  216.                     s.listen(5)
  217.                     c, addr = s.accept()
  218.                     c.send(str((send)))
  219.                     s.close()
  220.                     print 'sent!'
  221.             if prefix == 'input':
  222.                 run = 1
  223.                 if script[0].isspace():
  224.                     script = script[1:]
  225.                 com = script
  226.                 input = raw_input((com))
  227.                 print 'your input:', input
  228.             if prefix == 'length':
  229.                 run = 1
  230.                 if script[0].isspace():
  231.                     script = script[1:]
  232.                 size = len((script)) - 1
  233.                 print 'string length:', size
  234.             if prefix == 'randint':
  235.                 run = 1
  236.                 if script[0].isspace():
  237.                     script = script[1:]
  238.                 try:
  239.                     low = script.split('-')[0]
  240.                     high = script.split('-')[1]
  241.                     num = randint(int((low)),int((high)))
  242.                     print num
  243.                 except:
  244.                     reason = 'Syntax error, try: randint; 0-100'
  245.                     error()
  246.             if prefix == 'commands':
  247.                 run = 1
  248.                 print commands
  249.             if prefix == 'randstring':
  250.                 run = 1
  251.                 if script[0].isspace():
  252.                     script = script[1:]
  253.                 try:
  254.                     print os.urandom(int(script)) # spices things up, not the same old try: except: crap
  255.                 except:
  256.                     reason = 'String found, you must use an integer (number)'
  257.                     error()
  258.             if prefix == '[red]':
  259.                 print '{ri}' # sets color to red
  260.                 color = '{ri}'
  261.             if prefix == '[green]':
  262.                 print '{gi}' # sets color to green         # RGB colors
  263.                 color = '{gi}'
  264.             if prefix == '[blue]':
  265.                 print '{bi}' # sets color to blue
  266.                 color = '{bi}'
  267.             if prefix == '[debug]':
  268.                 if debug == 0:
  269.                     debug = 1
  270.                 else:
  271.                     debug = 0
  272.             if prefix == 'cal':
  273.                 run = 1
  274.                 if script[0].isspace(): # if script[0] == only space do the following
  275.                     script = script[1:]
  276.                 try:
  277.                     year = script.split(',')[0]
  278.                     day = script.split(',')[1]
  279.                     cal = calendar.month(int((year)),int((day)))
  280.                     print cal
  281.                 except:
  282.                     reason = 'Calendar error, try: cal; 2015,9'
  283.                     error()
  284.             if debug == 1: # debug information
  285.                 if script == '' or script.isspace():
  286.                     script = '(no script found)'
  287.                 if prefix == '[debug]':
  288.                     ignore()
  289.                 else:
  290.                     print 'line:', str((line)) + ' prefix:', prefix + ' script:', script
  291.             if prefix == 'binary':
  292.                 run = 1
  293.                 if script[0].isspace():
  294.                     script = script[1:]
  295.                 binary = bin(reduce(lambda x, y: 256*x+y, (ord(c) for c in (script)), 0))
  296.                 print binary
  297.             if prefix == 'getsubstring': # used in debugging
  298.                 run = 1
  299.                 if script.find('(') != -1 and script.find (')') != -1:
  300.                     parse = script
  301.                     getsubstring(parse)
  302.                     print substring
  303.                 else:
  304.                     reason = 'no () found to get substring'
  305.                     error()
  306.             if prefix == 'wait':
  307.                 run = 1
  308.                 if script[0].isspace(): # if script = only space do the following
  309.                     script = script[1:]
  310.                 try:
  311.                     time.sleep(int(script))
  312.                 except:
  313.                     reason = 'Wait error, try: wait; 5'
  314.                     error()
  315.             if prefix == 'mouse':
  316.                 if script[0].isspace(): # if script = only space do the following
  317.                     script = script[1:]
  318.                 run = 1
  319.                 try:
  320.                     x = script.split(',')[0]
  321.                     y = script.split(',')[1]
  322.                     autopy.mouse.smooth_move(int((x)),int((y)))
  323.                 except:
  324.                     reason = 'invalid X,Y'
  325.                     error()
  326.             if prefix == 'mouse_snap':
  327.                 if script[0].isspace(): # if script = only space do the following
  328.                     script = script[1:]
  329.                 run = 1
  330.                 try:
  331.                     x = script.split(',')[0]
  332.                     y = script.split(',')[1]
  333.                     autopy.mouse.move(int((x)),int((y)))
  334.                 except:
  335.                     reason = 'invalid X,Y'
  336.                     error()
  337.             if prefix == 'made':
  338.                 run = 1
  339.                 shell = win32com.client.Dispatch('WScript.Shell')
  340.                 shell.Run('notepad')
  341.                 time.sleep(0.1)
  342.                 shell.AppActivate('notepad')
  343.                 shell.SendKeys("Made by Dan Jones, for Windows", 0)
  344.                 shell.SendKeys("{Enter}", 0)
  345.                 shell.SendKeys("{F5}", 0)   # F5 prints the time/date
  346.             if prefix == 'beep':
  347.                 run = 1
  348.                 try:
  349.                     x = script.split(',')[0]
  350.                     y = script.split(',')[1]
  351.                     winsound.Beep(int((x)),int((y)))
  352.                 except:
  353.                     reason = 'beep error'
  354.                     error()
  355.             if prefix == 'speak':
  356.                 if script[0].isspace(): # if script = only space do the following
  357.                     script = script[1:]
  358.                 run = 1
  359.                 engine = pyttsx.init()
  360.                 engine.say((script))
  361.                 engine.runAndWait()
  362.             if prefix == 'retype':
  363.                 if script[0].isspace(): # if script = only space do the following
  364.                     script = script[1:]
  365.                 run = 1
  366.                 pause = script.split(':')[0]
  367.                 text = script.split(':')[1]
  368.                 for x in range(len(text)):
  369.                     sys.stdout.write(text[x])
  370.                     time.sleep(float(pause))
  371.             script = '' # fixes repeated prints of script - always at bottom
  372. def getsubstring(string):
  373.     global substring
  374.     substring = string[string.find('(')+1:string.find(')')] # parse () inside a command for example getsubstring((parse))
  375. def ignore():
  376.     pass # skips the line of script, used mainly for comments
  377. def error():
  378.     print '{ri}'
  379.     if noerror == 0:
  380.         print 'File:{gi}', (arg)[1:], '{ri}Line:{gi}', str((line)) + '{ri}\n\n', com
  381.     reason.replace('\n', '') # i don't know why, but a \n occurs in (reason)
  382.     print '************************************\n'
  383.     print reason, '\n'
  384.     print '************************************'
  385.     sys.exit('Script Halted') # comment this line to continue the script when an error is found
  386. main() # starts the main script
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement