1 from Plugins.Plugin import PluginDescriptor
2 from Screens.Screen import Screen
3 from Components.Label import Label
4 from Components.ActionMap import ActionMap
5 from enigma import eTimer
6 from Components.ConfigList import ConfigListScreen
7 from Components.config import config, getConfigListEntry, ConfigText, ConfigSelection, ConfigSubsection, ConfigYesNo
8 from urllib2 import Request, urlopen
9 from base64 import encodestring
11 from twisted.internet import reactor
15 config.plugins.DynDNS = ConfigSubsection()
16 config.plugins.DynDNS.enable = ConfigYesNo(default = False)
17 config.plugins.DynDNS.interval = ConfigSelection(default = "10", choices = [("5", _("5 min.")),("10", _("10 min.")),("15", _("15 min.")),("30", _("30 min.")),("60", _("60 min."))])
18 config.plugins.DynDNS.hostname = ConfigText(default = "", fixed_size = False)
19 config.plugins.DynDNS.user = ConfigText(default = "", fixed_size = False)
20 config.plugins.DynDNS.password = ConfigText(default = "", fixed_size = False)
22 class DynDNSScreenMain(ConfigListScreen,Screen):
24 <screen position="100,100" size="550,400" title="DynDNS Setup" >
25 <widget name="config" position="0,0" size="550,300" scrollbarMode="showOnDemand" />
26 <widget name="buttonred" position="10,360" size="100,40" backgroundColor="red" valign="center" halign="center" zPosition="2" foregroundColor="white" font="Regular;18"/>
27 <widget name="buttongreen" position="120,360" size="100,40" backgroundColor="green" valign="center" halign="center" zPosition="2" foregroundColor="white" font="Regular;18"/>
29 def __init__(self, session, args = 0):
30 self.session = session
31 Screen.__init__(self, session)
33 self.list.append(getConfigListEntry(_("activate DynDNS"), config.plugins.DynDNS.enable))
34 self.list.append(getConfigListEntry(_("Interval to check IP-Adress"), config.plugins.DynDNS.interval))
35 self.list.append(getConfigListEntry(_("Hostname"), config.plugins.DynDNS.hostname))
36 self.list.append(getConfigListEntry(_("Username"), config.plugins.DynDNS.user))
37 self.list.append(getConfigListEntry(_("Password"), config.plugins.DynDNS.password))
38 ConfigListScreen.__init__(self, self.list)
39 self["buttonred"] = Label(_("cancel"))
40 self["buttongreen"] = Label(_("ok"))
41 self["setupActions"] = ActionMap(["SetupActions"],
46 "cancel": self.cancel,
51 print "[DynDNS] saving config"
52 for x in self["config"].list:
57 for x in self["config"].list:
67 self.timer.timeout.get().append(self.checkCurrentIP)
70 if config.plugins.DynDNS.enable.value:
72 reactor.callLater(1, self.checkCurrentIP)
79 def addSession(self,session):
80 self.sessions.append(session)
82 def checkCurrentIP(self):
83 print "[DynDNS] checking IP"
85 html = self.getURL("http://checkip.dyndns.org")
86 str = html.split("<body>")[1]
87 str = str.split("</body>")[0]
88 str = str.split(":")[1]
89 str = str.lstrip().rstrip()
91 if self.lastip is not str:
93 reactor.callLater(1, self.onIPchanged)
94 self.timer.start(int(config.plugins.DynDNS.interval.value)*60000)
99 def onIPchanged(self):
100 print "[DynDNS] IP change, setting new one",self.lastip
102 url = "http://members.dyndns.org/nic/update?system=dyndns&hostname=%s&myip=%s&wildcard=ON&offline=NO"%(config.plugins.DynDNS.hostname.value,self.lastip)
103 if self.getURL(url).find("good") is not -1:
104 print "[DynDNS] ip changed"
106 print "[DynDNS] ip was not changed",e
108 def getURL(self,url):
109 request = Request(url)
110 base64string = encodestring('%s:%s' % (config.plugins.DynDNS.user.value,config.plugins.DynDNS.password.value))[:-1]
111 request.add_header("Authorization", "Basic %s" % base64string)
112 htmlFile = urlopen(request)
113 htmlData = htmlFile.read()
117 def onPluginStart(session, **kwargs):
118 session.openWithCallback(onPluginStartCB,DynDNSScreenMain)
120 def onPluginStartCB(changed):
121 print "[DynDNS] config changed=",changed
124 dyndnsservice.disable()
125 dyndnsservice.enable()
128 dyndnsservice = DynDNSService()
130 def onSessionStart(reason, **kwargs):
132 if config.plugins.DynDNS.enable.value is not False:
133 if "session" in kwargs:
134 dyndnsservice.addSession(kwargs["session"])
136 dyndnsservice.enable()
138 dyndnsservice.disable()
140 def Plugins(path,**kwargs):
141 return [PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART, PluginDescriptor.WHERE_AUTOSTART], fnc = onSessionStart),
142 PluginDescriptor(name=_("DynDNS"), description=_("use www.DynDNS.org on your Box"),where = [PluginDescriptor.WHERE_PLUGINMENU], fnc = onPluginStart, icon="icon.png")]