-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
281 lines (248 loc) · 9.35 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import pickle
import sys
import os
import time
VERSION = "0.3b"
DEBUG = True
PDIR = os.getcwd()
"""
If you'll modifying the shell, please consider crediting to the creator which is me, AzizBgBoss.
https://github.com/AzizBgBoss/POWER-Shell
"""
def execution(LINE, COMMAND, ELEMENTS, LINEN):
global HISTORY
if COMMAND == 'kill':
print("Shell stopped.")
save_history()
return 'break'
elif COMMAND == 'help':
print(
f"\nPOWER shell created by AzizBgBoss || Version: {VERSION}" +
"\n\nkill: Quits the shell." +
"\nhelp: POWER shell manual. (this menu)" +
"\nprint: Prints a string (use '${VAR}' to append a variable)." +
"\nvar *variable* = *value*: defines a variable." +
"\nvar *variable*: prints the value of a variable." +
"\nhistory: shows you the commands history." +
"\nhistory clear: Clears the history." +
"\nfappend *file name* *content*: appends data to a file." +
"\nfdelete *file name*: deletes a file." +
"\nfread *file name*: prints the content of a file." +
"\nwait *milliseconds*: waits :D." +
"\nscript *file name*: runs the script in a file." +
f"\ndebug *boolean*: shows Python fatal error when possible (default: {DEBUG.lower()})." +
"\nread *var* *line*: prints *line* and save user answer in *var*." +
"\ndir *path*: change the file directory path (enter whole path (c:/.../folder))." +
"\n\nRemember, spaces matter!\n"
)
elif COMMAND == "print":
PLINE = LINE.replace("print", '', 1).replace(" ", '', 1)
VARD = PLINE.count('$')
VARO = PLINE.count('{')
VARC = PLINE.count('}')
if VARD != 0 and VARO != 0 and VARC != 0:
for i in range(VARD):
try:
if PLINE[PLINE.index('$')+1] == '{' and '}' in PLINE:
VAR = PLINE[PLINE.index('{')+1:PLINE.index('}')]
try:
PLINE = PLINE.replace(
'${'+VAR+'}', str(globals()[VAR]))
except:
PLINE = PLINE.replace('${'+VAR+'}', '')
except:
continue
print(PLINE)
else:
print(str(LINE.replace("print", '', 1).replace(" ", '', 1)))
del PLINE, VARD, VARC, VARO
elif COMMAND == "var":
if len(ELEMENTS) == 1:
print("No variables called or declared. Spaces matter!")
return 'continue'
elif len(ELEMENTS) == 2:
try:
print(globals()[ELEMENTS[1]])
except:
print(f"Error at line {LINEN}: Variable " +
ELEMENTS[1] + " is not defined")
elif len(ELEMENTS) >= 4 and ELEMENTS[2] == '=':
if ELEMENTS[1] in ["HISTORY", "LINE", "COMMAND", "ELEMENTS", "DEBUG", "VERSION", "SCRIPTMODE", "PDIR"]:
print(
"Illegal variable cannot be changed. Try naming something different."
)
return 'continue'
else:
globals()[ELEMENTS[1]] = LINE.replace(
"var " + ELEMENTS[1] + " = ", '')
else:
print(
f"Error at line {LINEN}: Wrong syntax. Type 'help'. Spaces matter!")
elif COMMAND == "history":
if len(ELEMENTS) != 1:
if ELEMENTS[1] == "clear":
HISTORY = []
save_history()
HISTORY = get_history()
return 'continue'
else:
print("\n".join(HISTORY))
elif COMMAND == "fappend":
if len(ELEMENTS) >= 2:
FILE = open(ELEMENTS[1], 'a')
FILE.write(
LINE.replace("fappend " + ELEMENTS[1], '',
1).replace(" ", '', 1).replace('\\n', '\n'))
FILE.close()
del FILE
else:
print(
f"Error at line {LINEN}: Wrong syntax. Type 'help'. Spaces matter!")
elif COMMAND == "fdelete":
if len(ELEMENTS) == 2:
if os.path.exists(ELEMENTS[1]):
if input("ARE YOU SURE YOU WANT TO DELETE \'" + ELEMENTS[1] +
"\'? (y/n) >>") == 'y':
os.remove(ELEMENTS[1])
print("\'" + ELEMENTS[1] + "\' removed.")
else:
print("Canceled.")
else:
print('\'' + ELEMENTS[1] + "\' does not exist.")
else:
print(
f"Error at line {LINEN}: Wrong syntax. Type 'help'. Spaces matter!")
elif COMMAND == "fread":
if len(ELEMENTS) == 2:
if os.path.exists(ELEMENTS[1]):
FILE = open(ELEMENTS[1], 'r')
print(FILE.read())
FILE.close()
del FILE
else:
print('\'' + ELEMENTS[1] + "\' does not exist.")
else:
print(
f"Error at line {LINEN}: Wrong syntax. Type 'help'. Spaces matter!")
elif COMMAND == "wait":
try:
if len(ELEMENTS) == 2:
time.sleep(int(ELEMENTS[1])/1000)
else:
print(
f"Error at line {LINEN}: Wrong syntax. Type 'help'. Spaces matter!")
except TypeError:
print(f"Error at line {LINEN}: entered time must be an integer.")
except:
print(
f"Error at line {LINEN}: Wrong syntax. Type 'help'. Spaces matter!")
elif COMMAND == "read":
if len(ELEMENTS) >= 2:
globals()[ELEMENTS[1]] = input(LINE.replace(
f"read {ELEMENTS[1]}", '', 1).replace(" ", '', 1))
else:
print(
f"Error at line {LINEN}: Wrong syntax. Type 'help'. Spaces matter!")
else:
print(
f"Error at line {LINEN}: Command not recognized. Type 'help' for help.")
HISTORY.append(LINE)
def get_history():
global PDIR
if ".his" in os.listdir():
with open(PDIR+'\\.his', 'rb') as fd:
return pickle.load(fd)
else:
return list()
def save_history():
global PDIR
with open(PDIR+'\\.his', 'wb') as fd:
pickle.dump(HISTORY, fd)
print(f"POWER shell by AzizBgBoss || Version: {VERSION}\n")
try:
HISTORY = get_history()
LINEN = ''
while True:
LINE = input(f'{os.getcwd()} >> ')
if LINE == '':
print("No command entered. Type 'kill' to quit.")
continue
try:
ELEMENTS = LINE.split()
COMMAND = ELEMENTS[0]
except IndexError:
print("No command entered. Type 'kill' to quit.")
continue
if COMMAND == 'script':
if len(ELEMENTS) == 2:
if os.path.exists(ELEMENTS[1]):
FILE = open(ELEMENTS[1], 'r')
LINEN = 0
for LINE in (FILE.readlines()):
LINEN += 1
try:
ELEMENTS = LINE.split()
COMMAND = ELEMENTS[0]
except:
break
RETURN = execution(LINE.replace(
'\n', '', 1), COMMAND, ELEMENTS, LINEN)
if RETURN == 'break':
break
elif RETURN == 'continue':
continue
del RETURN
FILE.close()
del FILE
LINEN = ''
else:
print('\'' + ELEMENTS[1] + "\' does not exist.")
else:
print(
f"Error at line {LINEN}: Wrong syntax. Type 'help'. Spaces matter!")
elif COMMAND == "debug":
if len(ELEMENTS) == 1:
print(str(DEBUG).lower())
elif len(ELEMENTS) == 2:
if ELEMENTS[1] == 'true':
DEBUG = True
elif ELEMENTS[1] == 'false':
DEBUG = False
else:
print(
f"Error at line {LINEN}: Wrong syntax. Type 'help'. Spaces matter!")
else:
print(
f"Error at line {LINEN}: Wrong syntax. Type 'help'. Spaces matter!")
elif COMMAND == 'dir':
if len(ELEMENTS) != 1:
os.chdir(LINE.replace("dir ", '', 1))
else:
print(
f"Error at line {LINEN}: Wrong syntax. Type 'help'. Spaces matter!")
else:
RETURN = execution(LINE, COMMAND, ELEMENTS, '')
if RETURN == 'break':
break
elif RETURN == 'continue':
continue
del RETURN
except KeyboardInterrupt:
print("Shell stopped (User CTRL+C).")
save_history()
sys.exit()
except Exception as EXCEPTION:
if DEBUG == False:
print(
"QUITTING! FATAL ERROR.\nIF PERSISTENT PLEASE WAIT FOR UPDATES AND REPORT AT https://github.com/AzizBgBoss/POWER-Shell."
)
else:
print(str(EXCEPTION))
del EXCEPTION
try:
save_history()
except:
time.sleep(3)
sys.exit()
time.sleep(3)
sys.exit()