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