second steps for a control-page of the mediaplayer
[enigma2-plugins.git] / dyndns / src / plugin.py
1
2 from enigma import *
3 from Plugins.Plugin import PluginDescriptor
4 from Screens.Screen import Screen
5 from Components.Label import Label
6 from Components.ActionMap import ActionMap, NumberActionMap
7
8 from enigma import eTimer
9
10 from Components.ConfigList import *
11 from Components.config import config, getConfigListEntry,ConfigText, ConfigSelection, ConfigSubsection, ConfigInteger,ConfigYesNo
12
13 import os
14 import urllib2
15 import base64
16
17 global sessions
18 from twisted.internet import reactor
19
20 sessions = []
21
22 config.plugins.DynDNS = ConfigSubsection()
23 config.plugins.DynDNS.enable = ConfigYesNo(default = False)
24 config.plugins.DynDNS.interval = ConfigSelection(default = "10", choices = [("5", _("5 min.")),("10", _("10 min.")),("15", _("15 min.")),("30", _("30 min.")),("60", _("60 min."))])
25 config.plugins.DynDNS.hostname = ConfigText(default = "", fixed_size = False)
26 config.plugins.DynDNS.user = ConfigText(default = "", fixed_size = False)
27 config.plugins.DynDNS.password = ConfigText(default = "", fixed_size = False)
28
29 class DynDNSScreenMain(ConfigListScreen,Screen):
30     skin = """
31         <screen position="100,100" size="550,400" title="DynDNS Setup" >
32         <widget name="config" position="0,0" size="550,300" scrollbarMode="showOnDemand" />
33         <widget name="buttonred" position="10,360" size="100,40" backgroundColor="red" valign="center" halign="center" zPosition="2"  foregroundColor="white" font="Regular;18"/> 
34         <widget name="buttongreen" position="120,360" size="100,40" backgroundColor="green" valign="center" halign="center" zPosition="2"  foregroundColor="white" font="Regular;18"/> 
35         </screen>"""
36     def __init__(self, session, args = 0):
37         self.session = session
38         Screen.__init__(self, session)
39         self.list = []
40         self.list.append(getConfigListEntry(_("activate DynDNS"), config.plugins.DynDNS.enable))
41         self.list.append(getConfigListEntry(_("Interval to check IP-Adress"), config.plugins.DynDNS.interval))
42         self.list.append(getConfigListEntry(_("Hostname"), config.plugins.DynDNS.hostname))
43         self.list.append(getConfigListEntry(_("Username"), config.plugins.DynDNS.user))
44         self.list.append(getConfigListEntry(_("Password"), config.plugins.DynDNS.password))
45         ConfigListScreen.__init__(self, self.list)
46         self["buttonred"] = Label(_("cancel"))
47         self["buttongreen"] = Label(_("ok"))
48         self["setupActions"] = ActionMap(["SetupActions"],
49         {
50             "green": self.save,
51             "red": self.cancel,
52             "save": self.save,
53             "cancel": self.cancel,
54             "ok": self.save,
55         }, -2)
56
57     def save(self):
58         print "[DynDNS] saving config"
59         for x in self["config"].list:
60             x[1].save()
61         self.close(True)
62
63     def cancel(self):
64         for x in self["config"].list:
65             x[1].cancel()
66         self.close(False)
67
68 class DynDNSService:
69         enabled = False
70         sessions = []
71         lastip = ""
72         def __init__(self):
73                 self.timer = eTimer()
74                 self.timer.timeout.get().append(self.checkCurrentIP)
75                 
76         def enable(self):
77                 if config.plugins.DynDNS.enable.value:
78                         self.enabled = True
79                         reactor.callLater(1, self.checkCurrentIP)
80
81         def disable(self):
82                 if self.enabled:
83                         self.timer.stop()
84                         self.enabled = False
85                 
86         def addSession(self,session):
87                 self.sessions.append(session)
88         
89         def checkCurrentIP(self):
90                 print "[DynDNS] checking IP"
91                 try:
92                         html = self.getURL("http://checkip.dyndns.org")
93                         str = html.split("<body>")[1]
94                         str = str.split("</body>")[0]
95                         str = str.split(":")[1]
96                         str = str.lstrip().rstrip()
97                         
98                         if self.lastip is not str:
99                                 self.lastip=str
100                                 reactor.callLater(1, self.onIPchanged)
101                         self.timer.start(int(config.plugins.DynDNS.interval.value)*60000)
102                 except Exception,e:
103                         print "[DynDNS]",e
104                         str = "coundnotgetip"
105                         
106         def onIPchanged(self):
107                 print "[DynDNS] IP change, setting new one",self.lastip
108                 try:
109                         url = "http://members.dyndns.org/nic/update?system=dyndns&hostname=%s&myip=%s&wildcard=ON&offline=NO"%(config.plugins.DynDNS.hostname.value,self.lastip)
110                         if self.getURL(url).find("good") is not -1:
111                                 print "[DynDNS] ip changed"
112                 except Exception,e:
113                         print "[DynDNS] ip was not changed",e
114                         
115         def getURL(self,url):
116                 request =  urllib2.Request(url)
117                 base64string = base64.encodestring('%s:%s' % (config.plugins.DynDNS.user.value,config.plugins.DynDNS.password.value))[:-1]
118                 request.add_header("Authorization", "Basic %s" % base64string)
119                 htmlFile = urllib2.urlopen(request)
120                 htmlData = htmlFile.read()
121                 htmlFile.close()
122                 return htmlData
123
124 def onPluginStart(session, **kwargs):
125         session.openWithCallback(onPluginStartCB,DynDNSScreenMain)
126
127 def onPluginStartCB(changed):
128         print "[DynDNS] config changed=",changed
129         global dyndnsservice
130         if changed:
131                 dyndnsservice.disable()
132                 dyndnsservice.enable()
133                 
134 global dyndnsservice
135 dyndnsservice = DynDNSService()
136
137 def onSessionStart(reason, **kwargs):
138         global dyndnsservice
139         if config.plugins.DynDNS.enable.value is not False:
140                 if "session" in kwargs:
141                         dyndnsservice.addSession(kwargs["session"])
142                 if reason == 0:
143                         dyndnsservice.enable()
144                 elif reason == 1:
145                         dyndnsservice.disable()
146                         
147 def Plugins(path,**kwargs):
148         return [PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART, PluginDescriptor.WHERE_AUTOSTART], fnc = onSessionStart),
149                     PluginDescriptor(name=_("DynDNS"), description=_("use www.DynDNS.org on your Box"),where = [PluginDescriptor.WHERE_PLUGINMENU], fnc = onPluginStart, icon="icon.png")]
150