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