forked from JamezQ/Palaver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins
executable file
·446 lines (429 loc) · 14 KB
/
plugins
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#!/usr/bin/env python
# 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 3 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, see <http://www.gnu.org/licenses/>.
import argparse, shutil, os, tarfile, time, urllib2, subprocess
import sqlite3 as sql
parser = argparse.ArgumentParser(description='Install and remove Plugins.')
parser.add_argument('-i', metavar='file', type=str,help='Install a plugin - plugin .sp or .info file',default="")
parser.add_argument('-r', metavar='plugin', type=str,help='Remove the specified plugin',default="")
parser.add_argument('-p', metavar='name', type=str,help='Install a plugin from the repo',default="")
parser.add_argument('-d', metavar='plugin', type=str,help='Display info on a plugin',default="")
parser.add_argument('-l', dest='listPlugins', action='store_true',default=False,help='List the installed plugins')
parser.add_argument('-f', dest='force', action='store_true',default=False,help='Force installation with no confirmation.')
parser.add_argument("-q",dest='quiet',action='store_true',default=False,help='Run the program with no output')
args = parser.parse_args()
listPlugins = args.listPlugins
force = args.force
installGo = True
if args.quiet:
args.force = True
def print2(text):
if args.quiet == False:
print text
if args.p != '':
installGo = False
args.i = 'tmp.sp'
f = urllib2.urlopen("http://palaver.bmandesigns.com/functions.php?f=download&t=name&s="+args.p)
with open('tmp.sp', "wb") as local_file:
local_file.write(f.read())
installGo = True
encCode = 5
home = subprocess.Popen("echo $HOME", shell=True, stdout=subprocess.PIPE).communicate()[0].replace('\n','')
pluginDataBaseLocation = home + "/.palaver.d/plugins.db"
configRootFolder = home+"/.palaver.d/config/"
if not os.path.exists(configRootFolder):
os.system("mkdir "+configRootFolder)
def sqlDefault(cur):
cur.execute("CREATE TABLE IF NOT EXISTS plugins(id INTEGER PRIMARY KEY, name TEXT, version TEXT, author TEXT, binFiles TEXT, dicts TEXT, actions TEXT, configs TEXT)")
def encrypt(text,code):
try:
p = ''
code = int(code)
for each in text:
i = ord(each)
p = p + str((i+code)*(code*3))
p = p + '12506'
return p[0:-5]
except:
return None
def decrypt(text,code):
try:
l = text.split('12506')
p = ''
c=int(code)
for each in l:
i = int(each)
p = p + chr((i/(c*3)-(c)))
return p
except:
return None
root = ''.join([e+'/' for e in os.path.realpath(__file__).split('/')[0:-1]])
if listPlugins == True:
con = sql.connect(pluginDataBaseLocation)
with con:
cur = con.cursor()
sqlDefault(cur)
cur.execute("SELECT * FROM plugins")
pluginList = cur.fetchall()
for line in pluginList:
pid,name,version,author,cmdfile,dictionaries,actions,configs = line
actions=decrypt(actions,encCode)
print2("\t"+name+" - "+version)
actions = eval(actions)
for each in actions:
print2("\t\t"+each[0]+"\t\t"+each[1].replace('\n',''))
elif args.d != '':
con = sql.connect(pluginDataBaseLocation)
with con:
cur = con.cursor()
sqlDefault(cur)
cur.execute("SELECT * FROM plugins")
pluginList = cur.fetchall()
for line in pluginList:
pid,name,version,author,cmdfile,dictionaries,actions,configs = line
actions=decrypt(actions,encCode)
#print2 ("\t"+name+" - "+version)
if name == args.d:
print2 (name)
print2 ("\tAuthor: "+author)
print2 ("\tVersion: "+version)
print2 ("\tCommand(s) :"+str(cmdfile))
print2 ("\tDictionaries: "+str(dictionaries))
print2 ("\tActions:")
actions = eval(actions)
for each in actions:
print2 ("\t\t"+each[0]+"\t\t"+each[1].replace('\n',''))
print2 ("\tCommands:")
dictionary = open("Recognition/modes/main.dic")
adding = False
for line in dictionary:
if adding == False:
add = False
if line.startswith('#PLUGIN: '):
if line.replace('#PLUGIN: ','').replace('\n','') == args.d:
add = False
adding = True
else:
add = True
if line == "#END\n":
adding = False
add = False
if add == True:
print2 ("\t\t"+line.replace('\n',''))
dictionary.close()
elif args.i != '':
while installGo == False:
time.sleep(0.1)
filename = args.i
plugins = {}
con = sql.connect(pluginDataBaseLocation)
with con:
cur = con.cursor()
sqlDefault(cur)
cur.execute("SELECT * FROM plugins")
pluginList = cur.fetchall()
for line in pluginList:
pid,name,version,author,cmdfile,dictionaries,actions,configs = line
plugins[name] = [float(version),eval(cmdfile)]
if filename.endswith('.sp'):
tar = tarfile.open(mode='r:gz',fileobj=file(filename))
data = tar.extractfile('plugin.info')
else:
try:
data = open(filename)
except:
print2 ('Invalid File - not found')
recActions = False
multiDesc = False
actions = []
depend = []
dictionaries = []
description = ''
configs = []
for line in data:
if line.startswith("name"):
name = line.replace('name =','').replace(' ','').replace('\n','')
multiDesc = False
recActions = False
elif line.startswith("version"):
version = float(line.replace('version =','').replace(' ','').replace('\n',''))
multiDesc = False
recActions = False
elif line.startswith("description"):
multiDesc = True
recActions = False
elif line.startswith('dictionaries'):
dictionaries = line.replace('dictionaries =','').replace(' ','').replace('\n','')
try:
dictionaries = dictionaries.split(',')
except:
dictionaries = [dictionaries]
recActions = False
multiDesc = False
elif line.startswith('configs'):
configs = line.replace('configs =','').replace(' ','').replace('\n','')
try:
configs = configs.split(',')
except:
configs = [configs]
recActions = False
multiDesc = False
elif line.startswith("file"):
cmdfile = line.replace('file =','').replace(' ','').replace('\n','')
try:
cmdfile = cmdfile.split(",")
except:
cmdfile = [cmdfile]
multiDesc = False
recActions = False
elif line.startswith("author"):
author = line.replace("author =",'').replace('\n','')
if author.startswith(' '):
author = author[1:]
multiDesc = False
recActions = False
elif line.startswith("dependencies"):
depend = line.replace("dependencies = ",'').replace('\n','')
multiDesc = False
recActions = False
try:
depend = depend.split(" ")
except:
depend = [depend]
elif line.startswith("actions"):
recActions = True
multiDesc = False
if multiDesc == True:
if line != '':
description = description + line.replace('description =','').replace('\n','')
if recActions == True:
if line.replace('actions =','').replace('\n','').replace(' ','') != '':
actions.append(line.replace('\n','').split(',',1))
if vars().has_key("configs") == False:
configs = list()
data.close()
install = True
try:
currentVersion = plugins[name][0]
if currentVersion > version:
print2 ("A newer version is already installed")
with open('InstallResult','w') as d:
d.write("A newer version is already installed")
elif currentVersion == version:
print2 ("This version is already installed")
with open('InstallResult','w') as d:
d.write("This version is already installed")
else:
print2 ("Updating Files Has Not Yet Been Added, please remove the old version with -r and then install again")
with open('InstallResult','w') as d:
d.write("Updating Files Has Not Yet Been Added, please remove the old version with -r and then install again")
#Install Files
except:
for each in plugins:
for exe in plugins[each][1]:
if exe in cmdfile and exe != 'NONE' and len(plugins[each][1]) != 1 and install == True:
print2 ("This plugin has a conflict with "+each)
print2 ("Please rename the executable")
with open('InstallResult','w') as d:
d.write("This plugin has a conflict with "+each)
install = False
if install != False:
print2 (name)
print
print2 (description)
if type(configs) != list:
try:
configs = eval(configs)
except:
print "Error Loading Config Files"
if len(depend) != 0:
print2 ("WARNING: THIS PLUGIN REQUIRES:")
for each in depend:
print2 (each)
print
print
if force == False:
go = raw_input("Are you sure you want to continue? [Y/N]").lower() == 'y'
else:
go = True
if go == True:
try:
dictionary = open(root+'Recognition/modes/main.dic')
text = dictionary.read()
dictionary.close()
if filename.endswith('.sp'):
command = tar.extractfile('actions.dic').read()
else:
data = open(''.join([e+'/' for e in args.i.split('/')[0:-1]]) + 'actions.dic')
command = data.read()
data.close()
try:
dictionary = open(root+'Recognition/modes/main.dic','w')
dictionary.write("#PLUGIN: "+name+"\n")
dictionary.write(command)
dictionary.write("#END\n")
dictionary.write(text)
except:
print2 ("Error Installing Plugin In main.dic")
dictionary.close()
except:
print2 ("Dictionary Not Found")
if filename.endswith('.sp'):
try:
for dic in dictionaries:
binfile = open(root+"Recognition/modes/"+dic,'w')
binfile.write(tar.extractfile(dic).read())
binfile.close()
print2 ("File Installed to modes "+dic)
except:
print2 ("Error Installing to modes "+dic)
try:
for c in configs:
try:
os.system("mkdir "+configRootFolder+name+"/")
except:
pass
confile = open(configRootFolder+name+"/"+c,'w')
confile.write(tar.extractfile(c).read())
confile.close()
print2 ("File Installed to config "+c)
except:
print2 ("Error Installing to config "+c)
else:
src = ''.join([e+'/' for e in filename.split('/')[0:-1]])
try:
for dic in dictionaries:
shutil.copy(src+exe,root+"Recognition/modes/")
print2 ("File Installed to Modes "+dic)
except:
print2 ("Error Moving File to Modes "+dic)
try:
for c in config:
try:
os.system("mkdir "+configRootFolder+name+"/")
except:
pass
shutil.copy(src+c,configRootFolder+args.i+"/")
print2 ("File Installed to Config "+c)
except:
print2 ("Error Moving File To Config "+c)
if cmdfile != "NONE":
if filename.endswith('.sp'):
try:
for exe in cmdfile:
binfile = open(root+"Recognition/bin/"+exe,'w')
binfile.write(tar.extractfile(exe).read())
binfile.close()
print2 ("File Installed to Bin "+exe)
except:
print2 ("Error Installing to Bin "+exe)
else:
src = ''.join([e+'/' for e in filename.split('/')[0:-1]])
try:
for exe in cmdfile:
shutil.copy(src+exe,root+"Recognition/bin/")
print2 ("File Installed to Bin "+exe)
except:
print2 ("Error Moving File")
for exe in cmdfile:
os.system("chmod +x "+root+"Recognition/bin/"+exe)
for c in configs:
if c.endswith(".conf") == False:
os.system("chmod +x "+home+"/.palaver.d/config/"+name+"/"+c)
con = sql.connect(pluginDataBaseLocation)
with open('InstallResult','w') as d:
d.write(name+" Installed")
with con:
cur = con.cursor()
sqlDefault(cur)
execline = "INSERT INTO plugins VALUES(NULL, \""+name+"\", \""+str(version)+"\", \""+author+"\", \""+str(cmdfile)+"\", \""+str(dictionaries)+"\", \""+encrypt(str(actions),encCode)+"\", \""+str(configs)+"\")"
cur.execute(execline)
else:
print2 ("Quiting Installation")
elif args.r != '':
pluginName = args.r
try:
pluginList = open(root+'plugins')
except:
print2 ("Plugins Library Not Found")
good = False
try:
dictionary = open(root+'Recognition/modes/main.dic')
text = ""
deleting = False
for line in dictionary:
if deleting == False:
add = True
if line.startswith('#PLUGIN: '):
if line.replace('#PLUGIN: ','').replace('\n','') == pluginName:
add = False
deleting = True
else:
add = False
if line == "#END\n":
deleting = False
if add == True:
text = text + line
dictionary.close()
try:
dictionary = open(root+'Recognition/modes/main.dic','w')
dictionary.write(text)
except:
print2 ("Error Installing Plugin In main.dic")
dictionary.close()
except:
print2 ("Dictionary Not Found")
found = False
con = sql.connect(pluginDataBaseLocation)
with con:
cur = con.cursor()
sqlDefault(cur)
cur.execute("SELECT * FROM plugins")
pluginList = cur.fetchall()
for line in pluginList:
pid,name,version,author,cmdfile,dictionaries,actions,configs = line
actions=decrypt(actions,encCode)
cmdfile = eval(cmdfile)
dictionaries = eval(dictionaries)
configs = eval(configs)
if name == pluginName:
good = True
if cmdfile[0] != "NONE":
for exe in cmdfile:
try:
os.remove(root+"Recognition/bin/"+exe)
print2 ("Removed from Bin "+exe)
except:
print2 ("Couldn't remove "+exe+" from bin")
for dic in dictionaries:
try:
os.remove(root+"Recognition/modes/"+dic)
print2 ('Removed from Modes '+dic)
except:
print2 ("Couldn't remove "+dic+" from modes")
for c in configs:
try:
os.remove(configRootFolder+args.r+"/"+c)
print2 ('Removed from Config '+c)
except:
print2 ("Couldn't remove "+c+" from config")
os.system("rm "+configRootFolder+args.r+" -r")
cur.execute("DELETE FROM plugins WHERE id = "+str(pid))
print2 (name+" Uninstalled")
with open('InstallResult','w') as d:
d.write(name+" Uninstalled")
found = True
if found == False:
print2 ("Plugin Not Installed")
with open('InstallResult','w') as d:
d.write("Plugin Not Installed")
else:
print2 ("No Action Supplied")