added virtual keyboard
[enigma2-plugins.git] / setpasswd / src / plugin.py
1 from enigma import eConsoleAppContainer
2
3 from Screens.Screen import Screen
4 from Screens.MessageBox import MessageBox
5 from Screens.VirtualKeyBoard import VirtualKeyBoard
6
7 from Screens.Setup import SetupSummary
8 from Components.ConfigList import ConfigList
9 from Components.config import config, getConfigListEntry, ConfigSelection, ConfigSubsection, ConfigText
10
11 from Components.ActionMap import ActionMap
12 from Components.Label import Label
13 from Components.Pixmap import Pixmap
14 from Components.Sources.StaticText import StaticText
15 from Components.Sources.List import List
16 from Plugins.Plugin import PluginDescriptor
17
18 from __init__ import _
19
20 import string
21 import sys 
22 import time
23 from random import Random 
24
25 title=_("Change Root Password")
26
27 class ChangePasswdScreen(Screen):
28         skin = """
29                 <screen position="65,160" size="585,250" title="%s" >
30                 <widget name="passwd" position="10,10" size="565,200" scrollbarMode="showOnDemand" />
31                 <ePixmap pixmap="skin_default/div-h.png" position="10,205" size="565,2" transparent="1" alphatest="on" />
32                 <ePixmap pixmap="skin_default/buttons/red.png" position="5,210" size="140,40" alphatest="on" />
33                 <ePixmap pixmap="skin_default/buttons/green.png" position="150,210" size="140,40" alphatest="on" />
34                 <ePixmap pixmap="skin_default/buttons/yellow.png" position="295,210" size="140,40" alphatest="on" />
35                 <ePixmap pixmap="skin_default/buttons/blue.png" position="440,210" size="140,40" alphatest="on" />
36                 <widget source="key_red" render="Label" position="5,210" zPosition="1" size="140,40" font="Regular;17" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
37                 <widget source="key_green" render="Label" position="150,210" zPosition="1" size="140,40" font="Regular;17" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
38                 <widget source="key_yellow" render="Label" position="295,210" zPosition="1" size="140,40" font="Regular;17" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
39                 <widget source="key_blue" render="Label" position="440,210" zPosition="1" size="140,40" font="Regular;17" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
40         </screen>""" % title
41
42         def __init__(self, session, args = 0):
43                 Screen.__init__(self, session)
44                 self.skin = ChangePasswdScreen.skin
45
46                 self.user="root"
47                 self.output_line = ""
48                 
49                 self.password=self.GeneratePassword()
50                 self.list = []
51                 self.list.append(getConfigListEntry(_('Enter new Password'), ConfigText(default = self.password, fixed_size = False)))      
52
53                 self["passwd"] = ConfigList(self.list)
54                 self["key_red"] = StaticText(_("Cancel"))
55                 self["key_green"] = StaticText(_("Set Password"))
56                 self["key_yellow"] = StaticText(_("new Random"))
57                 self["key_blue"] = StaticText(_("virt. Keyboard"))
58
59                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
60                                 {
61                                                 "green": self.SetPasswd,
62                                                 "blue": self.bluePressed,
63                                                 "red": self.close,
64                                                 "yellow": self.buildList,
65                                                 "cancel": self.close
66                                 }, -1)
67
68         def buildList(self):
69                 self.password=self.GeneratePassword()
70                 self.list = []
71                 self.list.append(getConfigListEntry(_('Enter new Password'), ConfigText(default = self.password, fixed_size = False)))
72                 self["passwd"].setList(self.list)
73                 
74         def GeneratePassword(self): 
75                 passwdChars = string.letters + string.digits
76 #               passwdChars = string.letters + string.digits + '~!@#$%^&*-_=+?'   # use for more security :)
77                 passwdLength = 8
78                 return ''.join(Random().sample(passwdChars, passwdLength)) 
79
80         def SetPasswd(self):
81                 print "Changing password for %s to %s" % (self.user,self.password) 
82                 self.container = eConsoleAppContainer()
83                 self.container.appClosed.append(self.runFinished)
84                 self.container.dataAvail.append(self.dataAvail)
85                 retval = self.container.execute("passwd %s" % self.user)
86                 if retval==0:
87                         self.session.open(MessageBox, _("Sucessfully changed password for root user to: %s " % self.password), MessageBox.TYPE_INFO)    
88                 else:
89                         self.session.open(MessageBox, _("Unable to change/reset password for root user"), MessageBox.TYPE_ERROR)        
90
91         def dataAvail(self,data):
92                 self.output_line += data
93                 while True:
94                         i = self.output_line.find('\n')
95                         if i == -1:
96                                 break
97                         self.processOutputLine(self.output_line[:i+1])
98                         self.output_line = self.output_line[i+1:]
99
100         def processOutputLine(self,line):
101                 if line.find('password: '):
102                         self.container.write("%s\n"%self.password)
103
104         def runFinished(self,retval):
105                 del self.container.dataAvail[:]
106                 del self.container.appClosed[:]
107                 del self.container
108                 self.close()
109                 
110         def bluePressed(self):
111                 self.session.openWithCallback(self.VirtualKeyBoardTextEntry, VirtualKeyBoard, title = (_("Enter your password here:")), text = self.password)
112         
113         def VirtualKeyBoardTextEntry(self, callback = None):
114                 if callback is not None and len(callback):
115                         self.password=callback
116                         self.list = []
117                         self.list.append(getConfigListEntry(_('Enter new Password'), ConfigText(default = self.password, fixed_size = False)))
118                         self["passwd"].setList(self.list)
119
120 def startChange(menuid):
121         if menuid != "system": 
122                 return [ ]
123         return [(title, main, "change_root_passwd", 50)]
124
125 def main(session, **kwargs):
126         session.open(ChangePasswdScreen)
127
128 def Plugins(**kwargs):
129         return PluginDescriptor(
130                 name=title, 
131                 description=_("Change or reset the root password of your dreambox"),
132                 where = [PluginDescriptor.WHERE_MENU], fnc = startChange)
133