fix enigma2-dependens
[enigma2-plugins.git] / passwordchanger / src / plugin.py
1 ##
2 ## PasswordChanger
3 ## by AliAbdul
4 ##
5 from Components.ActionMap import ActionMap
6 from Components.config import config, ConfigText, ConfigSubsection, getConfigListEntry
7 from Components.ConfigList import ConfigListScreen
8 from Components.Language import language
9 from Components.ScrollLabel import ScrollLabel
10 from os import environ
11 from Plugins.Plugin import PluginDescriptor
12 from Screens.MessageBox import MessageBox
13 from Screens.Screen import Screen
14 from telnetlib import Telnet
15 from Tools.Directories import resolveFilename, SCOPE_LANGUAGE, SCOPE_PLUGINS
16 import gettext
17
18 ############################################
19
20 def localeInit():
21         lang = language.getLanguage()
22         environ["LANGUAGE"] = lang[:2]\r
23         gettext.bindtextdomain("enigma2", resolveFilename(SCOPE_LANGUAGE))\r
24         gettext.textdomain("enigma2")
25         gettext.bindtextdomain("PasswordChanger", "%s%s" % (resolveFilename(SCOPE_PLUGINS), "Extensions/PasswordChanger/locale/"))
26
27 def _(txt):\r
28         t = gettext.dgettext("PasswordChanger", txt)\r
29         if t == txt:\r
30                 t = gettext.gettext(txt)\r
31         return t
32
33 localeInit()\r
34 language.addCallback(localeInit)
35
36 ############################################
37
38 config.plugins.PasswordChanger = ConfigSubsection()
39 config.plugins.PasswordChanger.old_password = ConfigText(default="", fixed_size=False)
40 config.plugins.PasswordChanger.new_password = ConfigText(default="", fixed_size=False)
41
42 ############################################
43
44 class PasswordChanger(ConfigListScreen, Screen):
45         skin = """
46                 <screen position="center,center" size="420,70" title="%s" >
47                         <widget name="config" position="0,0" size="420,70" scrollbarMode="showOnDemand" />
48                 </screen>""" % _("Password Changer")
49
50         def __init__(self, session, args=None):
51                 Screen.__init__(self, session)
52                 self.session = session
53                 
54                 ConfigListScreen.__init__(self, [
55                 getConfigListEntry(_("Old password:"), config.plugins.PasswordChanger.old_password),
56                 getConfigListEntry(_("New password:"), config.plugins.PasswordChanger.new_password)])
57                 
58                 self["actions"] = ActionMap(["OkCancelActions"], {"ok": self.changePassword, "cancel": self.exit}, -2)
59
60         def changePassword(self):
61                 old_pass = config.plugins.PasswordChanger.old_password.value
62                 new_pass = config.plugins.PasswordChanger.new_password.value
63                 
64                 if len(new_pass) > 4 and len(new_pass) < 9:
65                         self.session.open(PasswordChangerConsole, old_pass, new_pass)
66                 else:
67                         self.session.open(MessageBox, _("Incorrect new password!\nMinimum length: 5\nMaximum length: 8"), MessageBox.TYPE_ERROR)
68
69         def exit(self):
70                 for x in self["config"].list:
71                         x[1].cancel()
72                 
73                 self.close()
74
75 ############################################
76
77 class PasswordChangerConsole(Screen):
78         skin = """
79                 <screen position="center,center" size="520,400" title="%s" >
80                         <widget name="label" position="0,0" size="520,400" font="Regular;20" />
81                 </screen>""" % _("Password Changer")
82
83         def __init__(self, session, old_pass, new_pass):
84                 Screen.__init__(self, session)
85                 
86                 self.working = True
87                 self.old_pass = old_pass
88                 self.new_pass = new_pass
89                 self.log = ""
90                 self.timeout = 2
91                 
92                 self["label"] = ScrollLabel("")
93                 
94                 self["actions"] = ActionMap(["WizardActions"],
95                         {
96                                 "ok": self.exit,
97                                 "back": self.exit,
98                                 "up": self["label"].pageUp,
99                                 "down": self["label"].pageDown,
100                                 "left": self["label"].pageUp,
101                                 "right": self["label"].pageDown
102                         }, -1)
103                 
104                 self.onLayoutFinish.append(self.run)
105
106         def exit(self):
107                 if not self.working:
108                         self.sendMessage("exit")
109                         self.close()
110
111         def sendMessage(self, message):
112                 if self.t is not None:
113                         self.t.write(message + "\n")
114                         r = self.t.read_until("UNKNOWN", self.timeout)
115                         self.log += r
116                         return r
117                 else:
118                         return ""
119
120         def run(self):
121                 logged_in = False
122                 
123                 try:
124                         self.t = Telnet("localhost")
125                         
126                         self.log = self.t.read_until("login:", self.timeout)
127                         if self.log.__contains__("login:"):
128                                 r = self.sendMessage("root")
129                                 if r.__contains__("~#"):
130                                         logged_in = True
131                                 
132                                 elif r.__contains__("Password:"):
133                                         r = self.sendMessage(self.old_pass)
134                                         if r.__contains__("~#"):
135                                                 logged_in = True
136                 except:
137                         self.t = None
138                 
139                 if logged_in:
140                         self.changePassword()
141                 else:
142                         self.log += _("Could not log in!")
143                         self["label"].setText(self.log)
144                         self.working = False
145
146         def changePassword(self):
147                 try:
148                         r = self.sendMessage("passwd")
149                         if r.__contains__("Enter new password:"):
150                                 r = self.sendMessage(self.new_pass)
151                                 
152                                 if r.__contains__("Re-enter new password:"):
153                                         r = self.sendMessage(self.new_pass)
154                 except:
155                         self.log += _("Error while setting new password!")
156                 
157                 self["label"].setText(self.log)
158                 self.working = False
159
160 ############################################
161
162 def start(session, **kwargs):
163         session.open(PasswordChanger)
164
165 def main(menuid):
166         if menuid != "system": 
167                 return [ ]
168         return [(_("Password Changer"), start, "password_changer", None)]
169
170 ############################################
171
172 def Plugins(**kwargs):
173         return PluginDescriptor(name=_("Password Changer"), description=_("Change your ftp and telnet password"), where=PluginDescriptor.WHERE_MENU, fnc=main)