forked from ycahome/pp-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
772 lines (597 loc) · 37.6 KB
/
plugin.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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
# pp-manager - PythonPlugin Manager
#
# Author: ycahome, 2018
#
# Since (2018-02-23): Initial Version
#
#
"""
<plugin key="PP-MANAGER" name="Python Plugin Manager" author="ycahome" version="1.5.17" externallink="https://www.domoticz.com/forum/viewtopic.php?f=65&t=22339">
<description>
<h2>Python Plugin Manager v.1.5.17</h2><br/>
<h3>Features</h3>
<ul style="list-style-type:square">
<li>Install plugins</li>
<li>Update All/Selected plugins</li>
<li>Update Notification for All/Selected</li>
</ul>
<h3>----------------------------------------------------------------------</h3>
<h3>WARNING:</h3>
<h2> Auto Updating plugins without verifying their code</h2>
<h2> makes you system vulnerable to developer's code intensions!!</h2>
<h3>----------------------------------------------------------------------</h3>
<h2>NOTE: After selecting your options press "Update" button!!</h2>
</description>
<params>
<param field="Mode2" label="Plugin to install" width="200px">
<options>
<option label="Idle" value="Idle" default="true" />
<option label="Dummy Plugin" value="Dummy_Plugin"/>
<option label="Battery monitoring for Z-Wave nodes" value="BatteryLevel"/>
<option label="Buienradar.nl (Weather lookup)" value="Buienradar"/>
<option label="Crow Runner Alarm" value="AAPIPModule"/>
<option label="deCONZ bridge (For Conbee,Raspbee)" value="deCONZ"/>
<option label="Denon/Marantz Amplifier" value="Denon4306"/>
<option label="Disc usage" value="xfr_discusage"/>
<option label="Dutch earthquakes" value="xfr_aardbeving"/>
<option label="Eartquake EMSC Data" value="SeismicPortal"/>
<option label="ebusd bridge" value="ebusd"/>
<option label="eQ-3 MAX!" value="eq3max"/>
<option label="Freebox Revolution" value="freeboxv6"/>
<option label="Global Cache 100" value="GC-100"/>
<option label="Homewizard" value="Homewizard"/>
<option label="Hive Plugin" value="HivePlug"/>
<option label="iDetect Presence Detection" value="iDetect"/>
<option label="IKEA Tradfri" value="IKEA-Tradfri"/>
<option label="Life 360 Presence" value="Life360"/>
<option label="Linky" value="Linky"/>
<option label="Meteo Alarm EU RSS Reader" value="MeteoAlarmEU"/>
<option label="Mikrotik RouterOS" value="mikrotik-routeros"/>
<option label="Moon Phases" value="MoonPhases"/>
<option label="MQTT discovery" value="MQTTDiscovery"/>
<option label="Onkyo AV Receiver" value="Onkyo"/>
<option label="OpenAQ" value="xfr_openaq"/>
<option label="Pi-hole summary" value="xfr_pihole"/>
<option label="PiMonitor" value="xfr-pimonitor"/>
<option label="Pioneer AVR" value="PioneerAVR"/>
<option label="RAVEn Zigbee energy monitor" value="RAVEn"/>
<option label="SmogTok Air Quality monitor" value="SmogTok"/>
<option label="SNMP Reader" value="SNMPreader"/>
<option label="Sonos Players" value="Sonos"/>
<option label="Sony Bravia TV (with Kodi remote)" value="sony"/>
<option label="Speedtest" value="xfr_speedtest"/>
<option label="SYSFS-Switches" value="SYSFS-Switches"/>
<option label="UPS Monitor" value="NUT_UPS"/>
<option label="Wan IP Checker" value="WAN-IP-CHECKER"/>
<option label="Xiaomi Mi Flower Mate" value="Mi_Flower_mate_plugin"/>
<option label="Xiaomi Mi Robot Vacuum" value="xiaomi-mi-robot-vacuum"/>
<option label="Xiaomi PM2.5 Sensor" value="XiaomiPM"/>
<option label="Yamaha AV Receiver" value="YamahaPlug"/>
<option label="Zigate plugin" value="Zigate"/>
<option label="Zigbee2Mqtt" value="Zigbee2Mqtt"/>
</options>
</param>
<param field="Mode4" label="Auto Update" width="175px">
<options>
<option label="All" value="All"/>
<option label="All (NotifyOnly)" value="AllNotify" default="true"/>
<option label="Selected" value="Selected"/>
<option label="Selected (NotifyOnly)" value="SelectedNotify"/>
<option label="None" value="None"/>
</options>
</param>
<param field="Mode5" label="Security Scan (Experimental)" width="75px">
<options>
<option label="True" value="True"/>
<option label="False" value="False" default="true" />
</options>
</param>
<param field="Mode6" label="Debug" width="75px">
<options>
<option label="True" value="Debug"/>
<option label="False" value="Normal" default="true" />
</options>
</param>
</params>
</plugin>
"""
import Domoticz
import os
import subprocess
import sys
import urllib
import urllib.request
import urllib.error
import re
import time
import platform
#from urllib2 import urlopen
from datetime import datetime, timedelta
class BasePlugin:
enabled = False
pluginState = "Not Ready"
sessionCookie = ""
privateKey = b""
socketOn = "FALSE"
def __init__(self):
self.debug = False
self.error = False
self.nextpoll = datetime.now()
self.pollinterval = 60 #Time in seconds between two polls
self.ExceptionList = []
self.SecPolUserList = {}
self.plugindata = {
# Plugin Key: [gitHub author, repository, plugin Text, Branch]
"Idle": ["Idle", "Idle", "Idle", "master"],
"Dummy_Plugin": ["ycahome", "Dummy_Plugin", "Dummy Plugin", "master"],
"BatteryLevel": ["999LV", "BatteryLevel", "Battery monitoring for Z-Wave nodes", "master"],
"Buienradar": ["ffes", "domoticz-buienradar", "Buienradar.nl (Weather lookup)", "master"],
"AAPIPModule": ["febalci", "DomoticzCrowAlarm", "Crow Runner Alarm", "master"],
"deCONZ": ["Smanar", "Domoticz-deCONZ", "deCONZ bridge (For Conbee,Raspbee)","master"],
"Denon4306": ["dnpwwo", "Domoticz-Denon-Plugin", "Denon/Marantz Amplifier", "master"],
"xfr_discusage": ["Xorfor", "Domoticz-Disc-usage-Plugin", "Disc usage", "master"],
"xfr_aardbeving": ["Xorfor", "Domoticz-LastDutchEarthquake-Plugin", "Dutch earthquakes", "master"],
"SeismicPortal": ["febalci", "DomoticzEarthquake", "Eartquake EMSC Data", "master"],
"ebusd": ["guillaumezin", "DomoticzEbusd", "ebusd bridge", "master"],
"eq3max": ["mvzut", "maxcube-Domoticz-plugin", "eQ-3 MAX! Cube", "master"],
"freeboxv6": ["supermat", "PluginDomoticzFreebox", "Freebox V6 (Revolution)", "master"],
"GC-100": ["dnpwwo", "Domoticz-GlobalCache-Plugin", "Global Cache 100", "master"],
"Homewizard": ["rvdvoorde", "domoticz-homewizard", "Homewizard", "master"],
"HivePlug": ["imcfarla2003", "domoticz-hive", "Hive Plugin", "master"],
"iDetect": ["d-EScape", "Domoticz_iDetect", "iDetect Presence Detection", "master"],
"IKEA-Tradfri": ["moroen", "IKEA-Tradfri-plugin", "IKEA Tradfri", "master"],
"Life360": ["febalci", "DomoticzLife360", "Life 360 Presence", "master"],
"Linky": ["guillaumezin", "DomoticzLinky", "Linky", "master"],
"MeteoAlarmEU": ["ycahome", "MeteoAlarmEU", "Meteo Alarm EU RSS Reader", "master"],
"mikrotik-routeros": ["mrin", "domoticz-routeros-plugin", "Mikrotik RouterOS", "master"],
"MoonPhases": ["ycahome", "MoonPhases", "Moon Phases", "master"],
"MQTTDiscovery": ["emontnemery", "domoticz_mqtt_discovery", "MQTT discovery", "master"],
"Onkyo": ["jorgh6", "domoticz-onkyo-plugin", "Onkyo AV Receiver", "master"],
"xfr_openaq": ["Xorfor", "Domoticz-OpenAQ-Plugin", "OpenAQ", "master"],
"xfr_pihole": ["Xorfor", "Domoticz-Pi-hole-Plugin", "Pi-hole summary", "master"],
"xfr-pimonitor": ["Xorfor", "Domoticz-PiMonitor-Plugin", "PiMonitor", "master"],
"PioneerAVR": ["febalci", "DomoticzPioneerAVR", "Pioneer AVR", "master"],
"RAVEn": ["dnpwwo", "Domoticz-RAVEn-Plugin", "RAVEn Zigbee energy monitor", "master"],
"SmogTok": ["smogtok", "smogtokdomoticzplug", "SmogTok Air Quality monitor", "master"],
"SNMPreader": ["ycahome", "SNMPreader", "SNMP Reader", "master"],
"Sonos": ["gerard33", "sonos", "Sonos Players", "master"],
"sony": ["gerard33", "sony-bravia", "Sony Bravia TV (with Kodi remote)", "master"],
"xfr_speedtest": ["Xorfor", "Domoticz-Speedtest-Plugin", "Speedtest", "master"],
"SYSFS-Switches": ["flatsiedatsie", "GPIO-SYSFS-Switches", "SYSFS-Switches", "master"],
"NUT_UPS": ["999LV", "NUT_UPS", "UPS Monitor", "master"],
"WAN-IP-CHECKER": ["ycahome", "WAN-IP-CHECKER", "Wan IP Checker", "master"],
"Mi_Flower_mate_plugin": ["flatsiedatsie", "Mi_Flower_mate_plugin", "Xiaomi Mi Flower Mate", "master"],
"xiaomi-mi-robot-vacuum": ["mrin", "domoticz-mirobot-plugin", "Xiaomi Mi Robot Vacuum", "master"],
"XiaomiPM": ["febalci", "DomoticzXiaomiPM2.5", "Xiaomi PM2.5 Sensor", "master"],
"YamahaPlug": ["thomas-villagers","domoticz-yamaha", "Yamaha AV Receiver", "master"],
"Zigate": ["sasu-drooz", "Domoticz-Zigate", "Zigate plugin", "stable"],
"Zigbee2Mqtt": ["stas-demydiuk", "domoticz-zigbee2mqtt-plugin", "Zigbee2Mqtt", "master"],
}
return
def onStart(self):
Domoticz.Debug("onStart called")
if Parameters["Mode6"] == 'Debug':
self.debug = True
Domoticz.Debugging(1)
DumpConfigToLog()
else:
Domoticz.Debugging(0)
Domoticz.Log("Domoticz Node Name is:" + platform.node())
Domoticz.Log("Domoticz Platform System is:" + platform.system())
Domoticz.Debug("Domoticz Platform Release is:" + platform.release())
Domoticz.Debug("Domoticz Platform Version is:" + platform.version())
Domoticz.Log("Default Python Version is:" + str(sys.version_info[0]) + "." + str(sys.version_info[1]) + "." + str(sys.version_info[2]) + ".")
if platform.system() == "Windows":
Domoticz.Error("Windows Platform NOT YET SUPPORTED!!")
return
pluginText = ""
pluginAuthor = ""
pluginRepository = ""
pluginKey = ""
pluginKey = Parameters["Mode2"]
pluginAuthor = self.plugindata[pluginKey][0]
pluginRepository = self.plugindata[pluginKey][1]
pluginText = self.plugindata[pluginKey][2]
pluginBranch = self.plugindata[pluginKey][3] # GitHub branch to clone
if (Parameters["Mode5"] == 'True'):
Domoticz.Log("Plugin Security Scan is enabled")
# Reading secpoluserFile and populating array of values
secpoluserFile = str(os.getcwd()) + "/plugins/PP-MANAGER/secpoluser.txt"
Domoticz.Debug("Checking for SecPolUser file on:" + secpoluserFile)
if (os.path.isfile(secpoluserFile) == True):
Domoticz.Log("secpoluser file found. Processing!!!")
# Open the file
secpoluserFileHandle = open(secpoluserFile)
# use readline() to read the first line
line = secpoluserFileHandle.readline()
while line:
if mid(line,0,4) == "--->":
secpoluserSection = mid(line,4,len(line))
Domoticz.Log("secpoluser settings found for plugin:" + secpoluserSection)
if ((mid(line,0,4) != "--->") and (line.strip() != "") and (line.strip() != " ")):
Domoticz.Debug("SecPolUserList exception (" + secpoluserSection.strip() + "):'" + line.strip() + "'")
#SecPolUserList.append(line.strip())
#SecPolUserList[secpoluserSection].append(line.strip())
if secpoluserSection.strip() not in self.SecPolUserList:
self.SecPolUserList[secpoluserSection.strip()] = []
self.SecPolUserList[secpoluserSection.strip()].append(line.strip())
# use realine() to read next line
line = secpoluserFileHandle.readline()
secpoluserFileHandle.close()
Domoticz.Log("SecPolUserList exception:" + str(self.SecPolUserList))
else:
self.SecPolUserList = {"Global":[]}
i = 0
path = str(os.getcwd()) + "/plugins/"
for (path, dirs, files) in os.walk(path):
for dir in dirs:
if str(dir) != "":
#self.UpdatePythonPlugin(pluginAuthor, pluginRepository, str(dir))
#parseFileForSecurityIssues(str(os.getcwd()) + "/plugins/PP-MANAGER/plugin.py")
if (os.path.isfile(str(os.getcwd()) + "/plugins/" + str(dir) + "/plugin.py") == True):
self.parseFileForSecurityIssues(str(os.getcwd()) + "/plugins/" + str(dir) + "/plugin.py", str(dir))
i += 1
if i >= 1:
break
# Reading exception file and populating array of values
exceptionFile = str(os.getcwd()) + "/plugins/PP-MANAGER/exceptions.txt"
Domoticz.Debug("Checking for Exception file on:" + exceptionFile)
if (os.path.isfile(exceptionFile) == True):
Domoticz.Log("Exception file found. Processing!!!")
# Open the file
f = open(exceptionFile)
# use readline() to read the first line
line = f.readline()
while line:
if ((line[:1].strip() != "#") and (line[:1].strip() != " ") and (line[:1].strip() != "")):
Domoticz.Log("File ReadLine result:'" + line.strip() + "'")
self.ExceptionList.append(line.strip())
# use realine() to read next line
line = f.readline()
f.close()
Domoticz.Debug("self.ExceptionList:" + str(self.ExceptionList))
if Parameters["Mode4"] == 'All':
Domoticz.Log("Updating All Plugins!!!")
i = 0
path = str(os.getcwd()) + "/plugins/"
for (path, dirs, files) in os.walk(path):
for dir in dirs:
if str(dir) != "":
if str(dir) in self.plugindata:
self.UpdatePythonPlugin(pluginAuthor, pluginRepository, str(dir))
elif str(dir) == "PP-MANAGER":
Domoticz.Debug("PP-Manager Folder found. Skipping!!")
else:
Domoticz.Log("Plugin:" + str(dir) + " cannot be managed with PP-Manager!!.")
i += 1
if i >= 1:
break
if Parameters["Mode4"] == 'AllNotify':
Domoticz.Log("Collecting Updates for All Plugins!!!")
i = 0
path = str(os.getcwd()) + "/plugins/"
for (path, dirs, files) in os.walk(path):
for dir in dirs:
if str(dir) != "":
if str(dir) in self.plugindata:
self.CheckForUpdatePythonPlugin(pluginAuthor, pluginRepository, str(dir))
elif str(dir) == "PP-MANAGER":
Domoticz.Debug("PP-Manager Folder found. Skipping!!")
else:
Domoticz.Log("Plugin:" + str(dir) + " cannot be managed with PP-Manager!!.")
i += 1
if i >= 1:
break
if (Parameters["Mode4"] == 'SelectedNotify'):
Domoticz.Log("Collecting Updates for Plugin:" + pluginKey)
self.CheckForUpdatePythonPlugin(pluginAuthor, pluginRepository, pluginKey)
if pluginKey == "Idle":
Domoticz.Log("Plugin Idle")
Domoticz.Heartbeat(60)
else:
Domoticz.Debug("Checking for dir:" + str(os.getcwd()) + "/plugins/" + pluginKey)
#If plugin Directory exists
if (os.path.isdir(str(os.getcwd()) + "/plugins/" + pluginKey)) == True:
Domoticz.Debug("Folder for Plugin:" + pluginKey + " already exists!!!")
#Domoticz.Debug("Set 'Python Plugin Manager'/ 'Domoticz plugin' attribute to 'idle' in order t.")
if Parameters["Mode4"] == 'Selected':
Domoticz.Debug("Updating Enabled for Plugin:" + pluginText + ".Checking For Update!!!")
self.UpdatePythonPlugin(pluginAuthor, pluginRepository, pluginKey)
Domoticz.Heartbeat(60)
else:
Domoticz.Log("Installation requested for Plugin:" + pluginText)
Domoticz.Debug("Installation URL is:" + "https://github.com/" + pluginAuthor +"/" + pluginRepository)
Domoticz.Debug("Current Working dir is:" + str(os.getcwd()))
if pluginKey in self.plugindata:
Domoticz.Log("Plugin Display Name:" + pluginText)
Domoticz.Log("Plugin Author:" + pluginAuthor)
Domoticz.Log("Plugin Repository:" + pluginRepository)
Domoticz.Log("Plugin Key:" + pluginKey)
Domoticz.Log("Plugin Branch:" + pluginBranch)
self.InstallPythonPlugin(pluginAuthor, pluginRepository, pluginKey, pluginBranch)
Domoticz.Heartbeat(60)
def onStop(self):
Domoticz.Debug("onStop called")
Domoticz.Log("Plugin is stopping.")
self.UpdatePythonPlugin("ycahome", "pp-manager", "PP-MANAGER")
Domoticz.Debugging(0)
def onHeartbeat(self):
Domoticz.Debug("onHeartbeat called")
pluginKey = Parameters["Mode2"]
CurHr = str(datetime.now().hour)
CurMin = str(datetime.now().minute)
if len(CurHr) == 1: CurHr = "0" + CurHr
if len(CurMin) == 1: CurMin = "0" + CurMin
Domoticz.Debug("Current time:" + CurHr + ":" + CurMin)
if (mid(CurHr,0,2) == "12" and mid(CurMin,0,2) == "00"):
Domoticz.Log("Its time!!. Trigering Actions!!!")
#-------------------------------------
if Parameters["Mode4"] == 'All':
Domoticz.Log("Checking Updates for All Plugins!!!")
i = 0
path = str(os.getcwd()) + "/plugins/"
for (path, dirs, files) in os.walk(path):
for dir in dirs:
if str(dir) != "":
self.UpdatePythonPlugin(self.plugindata[Parameters["Mode2"]][0], self.plugindata[Parameters["Mode2"]][1], str(dir))
i += 1
if i >= 1:
break
if Parameters["Mode4"] == 'AllNotify':
Domoticz.Log("Collecting Updates for All Plugins!!!")
i = 0
path = str(os.getcwd()) + "/plugins/"
for (path, dirs, files) in os.walk(path):
for dir in dirs:
if str(dir) != "":
self.CheckForUpdatePythonPlugin(self.plugindata[Parameters["Mode2"]][0], self.plugindata[Parameters["Mode2"]][1], str(dir))
i += 1
if i >= 1:
break
if Parameters["Mode4"] == 'SelectedNotify':
Domoticz.Log("Collecting Updates for Plugin:" + pluginKey)
self.CheckForUpdatePythonPlugin(self.plugindata[Parameters["Mode2"]][0], self.plugindata[Parameters["Mode2"]][1], Parameters["Mode2"])
#-------------------------------------
if Parameters["Mode4"] == 'Selected':
Domoticz.Log("Checking Updates for Plugin:" + self.plugindata[pluginKey][2])
self.UpdatePythonPlugin(self.plugindata[Parameters["Mode2"]][0], self.plugindata[Parameters["Mode2"]][1], Parameters["Mode2"])
#if Parameters["Mode2"] == "Idle":
#Domoticz.Log("Plugin Idle. No actions to be performed!!!")
# InstallPyhtonPlugin function
def InstallPythonPlugin(self, ppAuthor, ppRepository, ppKey, ppBranch):
Domoticz.Debug("InstallPythonPlugin called")
Domoticz.Log("Installing Plugin:" + self.plugindata[ppKey][2])
ppCloneCmd = "LANG=en_US /usr/bin/git clone -b " + ppBranch + " https://github.com/" + ppAuthor + "/" + ppRepository + ".git " + ppKey
Domoticz.Log("Calling:" + ppCloneCmd)
try:
pr = subprocess.Popen( ppCloneCmd , cwd = os.path.dirname(str(os.getcwd()) + "/plugins/"), shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
(out, error) = pr.communicate()
if out:
Domoticz.Log("Succesfully installed:" + str(out).strip)
Domoticz.Log("---Restarting Domoticz MAY BE REQUIRED to activate new plugins---")
if error:
Domoticz.Debug("Git Error:" + str(error))
if str(error).find("Cloning into") != -1:
Domoticz.Log("Plugin " + ppKey + " installed Succesfully")
except OSError as e:
Domoticz.Error("Git ErrorNo:" + str(e.errno))
Domoticz.Error("Git StrError:" + str(e.strerror))
#try:
# pr1 = subprocess.Popen( "/etc/init.d/domoticz.sh restart" , cwd = os.path.dirname(str(os.getcwd()) + "/plugins/"), shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
# (out1, error1) = pr1.communicate()
# if out1:
# Domoticz.Log("Command Response1:" + str(out1))
# if error1:
# Domoticz.Log("Command Error1:" + str(error1.strip()))
#except OSError1 as e1:
# Domoticz.Error("Command ErrorNo1:" + str(e1.errno))
# Domoticz.Error("Command StrError1:" + str(e1.strerror))
return None
# UpdatePyhtonPlugin function
def UpdatePythonPlugin(self, ppAuthor, ppRepository, ppKey):
Domoticz.Debug("UpdatePythonPlugin called")
if ppKey == "PP-MANAGER":
Domoticz.Log("Self Update Initiated")
ppGitReset = "LANG=en_US /usr/bin/git reset --hard HEAD"
try:
pr = subprocess.Popen( ppGitReset , cwd = str(os.getcwd() + "/plugins/" + ppKey), shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
(out, error) = pr.communicate()
if out:
Domoticz.Debug("Git Response:" + str(out))
if error:
Domoticz.Debug("Git Error:" + str(error.strip()))
except OSError as eReset:
Domoticz.Error("Git ErrorNo:" + str(eReset.errno))
Domoticz.Error("Git StrError:" + str(eReset.strerror))
elif (self.plugindata[ppKey][2] in self.ExceptionList):
Domoticz.Log("Plugin:" + self.plugindata[ppKey][2] + " excluded by Exclusion file (exclusion.txt). Skipping!!!")
return
Domoticz.Log("Updating Plugin:" + ppKey)
ppUrl = "LANG=en_US /usr/bin/git pull --force"
Domoticz.Debug("Calling:" + ppUrl + " on folder " + str(os.getcwd()) + "/plugins/" + ppKey)
try:
pr = subprocess.Popen( ppUrl , cwd = str(os.getcwd() + "/plugins/" + ppKey), shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
(out, error) = pr.communicate()
if out:
Domoticz.Debug("Git Response:" + str(out))
if (str(out).find("Already up-to-date") != -1) or (str(out).find("Already up to date") != -1):
Domoticz.Log("Plugin " + ppKey + " already Up-To-Date")
#Domoticz.Log("find(error):" + str(str(out).find("error")))
elif (str(out).find("Updating") != -1) and (str(str(out).find("error")) == "-1"):
ppUrl = "chmod "
Domoticz.Log("Succesfully pulled gitHub update:" + str(out)[str(out).find("Updating")+8:26] + " for plugin " + ppKey)
Domoticz.Log("---Restarting Domoticz MAY BE REQUIRED to activate new plugins---")
else:
Domoticz.Error("Something went wrong with update of " + str(ppKey))
if error:
Domoticz.Debug("Git Error:" + str(error.strip()))
if str(error).find("Not a git repository") != -1:
Domoticz.Log("Plugin:" + ppKey + " is not installed from gitHub. Cannot be updated with PP-Manager!!.")
except OSError as e:
Domoticz.Error("Git ErrorNo:" + str(e.errno))
Domoticz.Error("Git StrError:" + str(e.strerror))
return None
# UpdateNotifyPyhtonPlugin function
def CheckForUpdatePythonPlugin(self, ppAuthor, ppRepository, ppKey):
Domoticz.Debug("CheckForUpdatePythonPlugin called")
if (self.plugindata[ppKey][2] in self.ExceptionList):
Domoticz.Log("Plugin:" + self.plugindata[ppKey][2] + " excluded by Exclusion file (exclusion.txt). Skipping!!!")
return
Domoticz.Debug("Checking Plugin:" + ppKey + " for updates")
#Domoticz.Log("Fetching Repository Details")
ppGitFetch = "LANG=en_US /usr/bin/git fetch"
try:
prFetch = subprocess.Popen( ppGitFetch , cwd = str(os.getcwd() + "/plugins/" + ppKey), shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
(outFetch, errorFetch) = prFetch.communicate()
if outFetch:
Domoticz.Debug("Git Response:" + str(outFetch))
if errorFetch:
Domoticz.Debug("Git Error:" + str(errorFetch.strip()))
except OSError as eFetch:
Domoticz.Error("Git ErrorNo:" + str(eFetch.errno))
Domoticz.Error("Git StrError:" + str(eFetch.strerror))
ppUrl = "LANG=en_US /usr/bin/git status -uno"
Domoticz.Debug("Calling:" + ppUrl + " on folder " + str(os.getcwd()) + "/plugins/" + ppKey)
try:
pr = subprocess.Popen( ppUrl , cwd = str(os.getcwd() + "/plugins/" + ppKey), shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
(out, error) = pr.communicate()
if out:
Domoticz.Debug("Git Response:" + str(out))
if (str(out).find("up-to-date") != -1) or (str(out).find("up to date") != -1):
Domoticz.Log("Plugin " + ppKey + " already Up-To-Date")
Domoticz.Debug("find(error):" + str(str(out).find("error")))
elif (str(out).find("Your branch is behind") != -1) and (str(str(out).find("error")) == "-1"):
Domoticz.Log("Found that we are behind on plugin " + ppKey)
self.fnSelectedNotify(ppKey)
elif (str(out).find("Your branch is ahead") != -1) and (str(str(out).find("error")) == "-1"):
Domoticz.Debug("Found that we are ahead on plugin " + ppKey + ". No need for update")
else:
Domoticz.Error("Something went wrong with update of " + str(ppKey))
if error:
Domoticz.Debug("Git Error:" + str(error.strip()))
if str(error).find("Not a git repository") != -1:
Domoticz.Log("Plugin:" + ppKey + " is not installed from gitHub. Ignoring!!.")
except OSError as e:
Domoticz.Error("Git ErrorNo:" + str(e.errno))
Domoticz.Error("Git StrError:" + str(e.strerror))
return None
# fnSelectedNotify function
def fnSelectedNotify(self, pluginText):
Domoticz.Debug("fnSelectedNotify called")
Domoticz.Log("Preparing Notification")
ServerURL = "http://127.0.0.1:8080/json.htm?param=sendnotification&type=command"
MailSubject = urllib.parse.quote(platform.node() + ":Domoticz Plugin Updates Available for " + self.plugindata[pluginText][2])
MailBody = urllib.parse.quote(self.plugindata[pluginText][2] + " has updates available!!")
MailDetailsURL = "&subject=" + MailSubject + "&body=" + MailBody + "&subsystem=email"
notificationURL = ServerURL + MailDetailsURL
Domoticz.Debug("ConstructedURL is:" + notificationURL)
try:
response = urllib.request.urlopen(notificationURL, timeout = 30).read()
except urllib.error.HTTPError as err1:
Domoticz.Error("HTTP Request error: " + str(err1) + " URL: " + notificationURL)
return
Domoticz.Debug("Notification URL is :" + str(notificationURL))
return None
#
# Parse an int and return None if no int is given
#
def parseIntValue(s):
Domoticz.Debug("parseIntValue called")
try:
return int(s)
except:
return None
def parseFileForSecurityIssues(self, pyfilename, pypluginid):
Domoticz.Debug("parseFileForSecurityIssues called")
secmonitorOnly = False
if Parameters["Mode5"] == 'Monitor':
Domoticz.Log("Plugin Security Scan is enabled")
secmonitorOnly = True
# Open the file
file = open(pyfilename, "r")
ips = {}
#safeStrings = ["['http://schemas.xmlsoap.org/soap/envelope/', 'http://schemas.xmlsoap.org/soap/encoding/']",
# "127.0.0.1",
# "http://schemas.xmlsoap.org/soap/envelope/'",
# "import json",
# "import time",
# "import platform",
# 'import re']
if pypluginid not in self.SecPolUserList:
self.SecPolUserList[pypluginid] = []
lineNum = 1
#Domoticz.Error("self.SecPolUserList[pypluginid]:" + str(self.SecPolUserList[pypluginid]))
for text in file.readlines():
text = text.rstrip()
#Domoticz.Log("'text' is:'" + str(text))
regexFound = re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})',text)
paramFound = re.findall(r'<param field=',text)
if ((regexFound) and not (paramFound)):
#regexFound[rex] = regexFound[rex].strip('"]')
#Domoticz.Error("Security Finding(IPregex):" + str(regexFound) + " LINE: " + str(lineNum) + " FILE:" + pyfilename)
for rex in range(0,len(regexFound)):
if ((str(text).strip() not in self.SecPolUserList["Global"]) and (str(text).strip() not in self.SecPolUserList[pypluginid]) and (str(text).strip() != "") and (mid(text,0,1) != "#")):
Domoticz.Error("Security Finding(IP):-->" + str(text).strip() + "<-- LINE: " + str(lineNum) + " FILE:" + pyfilename)
#Domoticz.Error("Security Finding(IPr):" + regexFound[rex] + " LINE: " + str(lineNum) + " FILE:" + pyfilename)
ips["IP" + str(lineNum)] = (regexFound[rex], "IP Address")
#rex = 0
#regexFound = re.findall('import', text)
#if regexFound:
#regexFound[rex] = regexFound[rex].strip('"]')
#Domoticz.Error("Security Finding(IPregex):" + str(regexFound) + " LINE: " + str(lineNum) + " FILE:" + pyfilename)
# for rex in range(0,len(regexFound)):
# if ((str(text).strip() not in self.SecPolUserList["Global"]) and (str(text).strip() not in self.SecPolUserList[pypluginid]) and (str(text).strip() != "") and (mid(text,0,1) != "#")):
# Domoticz.Error("Security Finding(IMP):-->" + str(text) + "<-- LINE: " + str(lineNum) + " FILE:" + pyfilename)
#Domoticz.Error("Security Finding(IPr):" + regexFound[rex] + " LINE: " + str(lineNum) + " FILE:" + pyfilename)
# ips["IP" + str(lineNum)] = (regexFound[rex], "Import")
#rex = 0
#regexFound = re.findall('subprocess.Popen', text)
#if regexFound:
#regexFound[rex] = regexFound[rex].strip('"]')
#Domoticz.Error("Security Finding(IPregex):" + str(regexFound) + " LINE: " + str(lineNum) + " FILE:" + pyfilename)
# for rex in range(0,len(regexFound)):
# if ((str(text).strip() not in self.SecPolUserList["Global"]) and (str(text).strip() not in self.SecPolUserList[pypluginid]) and (str(text).strip() != "") and (mid(text,0,1) != "#")):
# Domoticz.Error("Security Finding(SUB):-->" + str(text) + "<-- LINE: " + str(lineNum) + " FILE:" + pyfilename)
#Domoticz.Error("Security Finding(IPr):" + regexFound[rex] + " LINE: " + str(lineNum) + " FILE:" + pyfilename)
# ips["IP" + str(lineNum)] = (regexFound[rex], "Subprocess")
#rex = 0
#regexFound = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', text)
#paramFound = re.findall(r'<param field=',text)
#if ((regexFound) and not (paramFound)):
#regexFound[rex] = regexFound[rex].strip('"]')
#Domoticz.Error("Security Finding(IPregex):" + str(regexFound) + " LINE: " + str(lineNum) + " FILE:" + pyfilename)
# for rex in range(0,len(regexFound)):
# if ((str(text).strip() not in self.SecPolUserList[pypluginid]) and (str(text).strip() != "") and (mid(text,0,1) != "#")):
# Domoticz.Error("Security Finding(HTTP):-->" + str(text) + "<-- LINE: " + str(lineNum) + " FILE:" + pyfilename)
#Domoticz.Error("Security Finding(IPr):" + regexFound[rex] + " LINE: " + str(lineNum) + " FILE:" + pyfilename)
# ips["IP" + str(lineNum)] = (regexFound[rex], "HTTP Address")
lineNum = lineNum + 1
file.close()
Domoticz.Debug("IPS Table contents are:" + str(ips))
global _plugin
_plugin = BasePlugin()
def onStart():
global _plugin
_plugin.onStart()
def onStop():
global _plugin
_plugin.onStop()
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()
# Generic helper functions
def DumpConfigToLog():
for x in Parameters:
if Parameters[x] != "":
Domoticz.Debug( "'" + x + "':'" + str(Parameters[x]) + "'")
Domoticz.Debug("Device count: " + str(len(Devices)))
for x in Devices:
Domoticz.Debug("Device: " + str(x) + " - " + str(Devices[x]))
Domoticz.Debug("Device ID: '" + str(Devices[x].ID) + "'")
Domoticz.Debug("Device Name: '" + Devices[x].Name + "'")
Domoticz.Debug("Device nValue: " + str(Devices[x].nValue))
Domoticz.Debug("Device sValue: '" + Devices[x].sValue + "'")
return
def mid(s, offset, amount):
#Domoticz.Debug("mid called")
return s[offset:offset+amount]