dreamIRC initial check-in
[enigma2-plugins.git] / dreamirc / src / dreamIRCSetup.py
1 ##
2 from Screens.Screen import Screen
3 from Screens.MessageBox import MessageBox
4 from Components.ActionMap import ActionMap
5 from Components.config import *
6 from Components.ConfigList import *
7 from Components.Sources.StaticText import StaticText
8 from Tools.HardwareInfo import *
9
10 import xml.dom.minidom
11 from xml.dom.minidom import Node
12 from xml.dom import EMPTY_NAMESPACE
13 from Tools import XMLTools
14 from Tools.XMLTools import elementsWithTag, mergeText
15
16 from socket import gethostbyname_ex
17
18 from dreamIRCTools import *
19
20 accounts_xml="/etc/dreamIRC.xml"
21
22 class dreamIRCSetupScreen(ConfigListScreen, Screen):
23         from enigma import getDesktop
24         desk = getDesktop(0)
25         x= int(desk.size().width())
26         y= int(desk.size().height())
27         print "[dreamIRC] setup: current desktop size: %dx%d" % (x,y)
28
29         if (y>=720):
30                 skin = """
31                         <screen position="390,205" size="500,300" title="dreamIRC - edit settings" >
32                         <widget name="config" position="10,10" size="480,260" scrollbarMode="showOnDemand" />
33                         <ePixmap pixmap="skin_default/div-h.png" position="10,245" size="480,2" transparent="1" alphatest="on" />
34                         <ePixmap pixmap="skin_default/buttons/red.png" position="60,250" size="160,40" alphatest="on" />
35                         <ePixmap pixmap="skin_default/buttons/green.png" position="280,250" size="160,40" alphatest="on" />
36                         <widget source="key_red" render="Label" position="60,250" zPosition="1" size="150,40" font="Regular;19" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
37                         <widget source="key_green" render="Label" position="280,250" zPosition="1" size="150,40" font="Regular;19" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
38                 </screen>"""
39         else:
40                 skin = """
41                         <screen position="110,145" size="500,300" title="dreamIRC - edit settings" >
42                         <widget name="config" position="10,10" size="480,260" scrollbarMode="showOnDemand" />
43                         <ePixmap pixmap="skin_default/div-h.png" position="10,245" size="480,2" transparent="1" alphatest="on" />
44                         <ePixmap pixmap="skin_default/buttons/red.png" position="60,250" size="160,40" alphatest="on" />
45                         <ePixmap pixmap="skin_default/buttons/green.png" position="280,250" size="160,40" alphatest="on" />
46                         <widget source="key_red" render="Label" position="60,250" zPosition="1" size="150,40" font="Regular;19" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
47                         <widget source="key_green" render="Label" position="280,250" zPosition="1" size="150,40" font="Regular;19" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
48                 </screen>"""
49
50         def __init__(self, session, args = 0):
51                 Screen.__init__(self, session)
52                 self.hardware_info = HardwareInfo()
53                 self.device=self.hardware_info.get_device_name()
54                 self.mac=getMacAddress()
55                 self.mac_end=self.mac[6:]
56 #               print "device : %s  -  mac : %s  -  mac_end : %s " %(self.device,self.mac,self.mac_end)
57                 self.dreamIRCconf = ConfigSubsection()
58                 self.reloadFile()
59                 list = []
60                 list.append(getConfigListEntry(_('Nickname'), self.dreamIRCconf.nick))      
61                 if config.usage.setup_level.index > 1: # advanced
62                         list.append(getConfigListEntry(_('Passwd'), self.dreamIRCconf.passwd))      
63                 if config.usage.setup_level.index >= 1: # intermediate+
64                         list.append(getConfigListEntry(_('Server1'), self.dreamIRCconf.server1))      
65                 if config.usage.setup_level.index > 1: # advanced
66                         list.append(getConfigListEntry(_('Server2'), self.dreamIRCconf.server2))      
67                         list.append(getConfigListEntry(_('Server3'), self.dreamIRCconf.server3))      
68                 if config.usage.setup_level.index >= 1: # intermediate+
69                         list.append(getConfigListEntry(_('Port'), self.dreamIRCconf.port))          
70                 list.append(getConfigListEntry(_('Channel'), self.dreamIRCconf.channel))    
71                 if config.usage.setup_level.index > 1: # i
72                         list.append(getConfigListEntry(_('Debug'), self.dreamIRCconf.debug))        
73
74                 self["key_red"] = StaticText(_("Cancel"))
75                 self["key_green"] = StaticText(_("Save"))
76
77                 ConfigListScreen.__init__(self, list)                                     
78                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
79                                 {
80                                                 "green": self.saveAndExit,
81                                                 "red": self.dontSaveAndExit,
82                                                 "cancel": self.dontSaveAndExit
83                                 }, -1)
84
85         def load(self):
86                 self.reloadFile()
87                 self.accounts=[ircsupport.IRCAccount(self.type, string.atoi(self.nr), str(self.nick), str(self.passwd), str(self.server1), string.atoi(self.port), str(self.channel))]
88                 print self.accounts
89                 return self.accounts
90
91         def reloadFile(self):
92                 try:
93                         doc = xml.dom.minidom.parse(accounts_xml)
94                         root = doc.childNodes[0]        
95                         for node in elementsWithTag(root.childNodes, "account"):
96                                 self.nick = node.getAttribute("nick")
97                                 self.passwd = node.getAttribute("passwd")
98                                 self.server1 = node.getAttribute("server1")
99                                 self.server2 = node.getAttribute("server2")
100                                 self.server3 = node.getAttribute("server3")
101                                 self.port = node.getAttribute("port")
102                                 self.channel = node.getAttribute("channel")
103                                 self.debug = node.getAttribute("debug")
104                         if ((self.nick.lower() == "dreamircuser") or (self.nick == "") or (self.nick[0] == " ") or (self.nick.lower() == "dm8000-vip")) :
105                                 print "[dreamIRC] nickname error... restoring default..."
106                                 self.nick = self.device+"_"+self.mac_end
107                 except IOError:
108                         self.type = "IRC"
109                         self.login = "1"
110                         self.nick = self.device+"_"+self.mac_end
111                         self.passwd = ""
112                         self.server1 = "irc.belwue.de"
113                         self.server2 = "irc.freenet.de"
114                         self.server3 = "irc.tu-illmenau.de"
115                         self.port = "06667"
116                         self.channel = "#dreamirc"
117                         self.debug = ""
118 #               self.dreamIRCconf = ConfigSubsection()
119                 self.dreamIRCconf.nick = ConfigText(default = self.nick, fixed_size = False)
120                 self.dreamIRCconf.passwd = ConfigText(default = self.passwd, fixed_size = False)
121                 self.dreamIRCconf.server1 = ConfigText(default = self.server1, fixed_size = False)
122                 self.dreamIRCconf.server2 = ConfigText(default = self.server2, fixed_size = False)
123                 self.dreamIRCconf.server3 = ConfigText(default = self.server3, fixed_size = False)
124                 self.dreamIRCconf.port = ConfigInteger(default = string.atoi(self.port), limits = (0, 99999))
125                 self.dreamIRCconf.channel = ConfigText(default = self.channel, fixed_size = False)
126                 self.dreamIRCconf.debug = ConfigText(default = self.debug, fixed_size = False)
127
128         def keySave(self):
129                 self.accounts=[]
130                 self.type = "IRC"
131                 self.login = "1"
132                 self.nick = self.dreamIRCconf.nick.value
133                 self.passwd = self.dreamIRCconf.passwd.value
134                 self.server1 = self.dreamIRCconf.server1.value
135                 self.server2 = self.dreamIRCconf.server2.value
136                 self.server3 = self.dreamIRCconf.server3.value
137                 self.port = self.dreamIRCconf.port.value
138                 self.channel = self.dreamIRCconf.channel.value
139                 self.debug = self.dreamIRCconf.debug.value
140                 global accounts_xml
141                 fp = file(accounts_xml, 'w')
142                 fp.write("<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n")
143                 fp.write("<accounts>\n")
144                 fp.write("    <account type=\"%s\" login=\"%s\" nick=\"%s\" passwd=\"%s\" server1=\"%s\" server2=\"%s\" server3=\"%s\" port=\"%s\" channel=\"%s\" debug=\"%s\" />\n" % (self.type, self.login, self.nick, self.passwd, self.server1, self.server2, self.server3, self.port, self.channel, self.debug))
145                 fp.write("</accounts>\n")
146                 fp.close()
147                 if self.server1:
148                         try:
149                                 self.result1=gethostbyname_ex(self.server1)
150                         except:
151                                 self.session.open(MessageBox, _("irc server %s not responding!\nplease check your network settings and/or irc servername..." %self.server1), MessageBox.TYPE_ERROR)
152                 if self.server2:
153                         try:
154                                 self.result2=gethostbyname_ex(self.server2)
155                         except:
156                                 self.session.open(MessageBox, _("irc server %s not responding!\nplease check your network settings and/or irc servername..." %self.server2), MessageBox.TYPE_ERROR)
157                 if self.server3:
158                         try:
159                                 self.result3=gethostbyname_ex(self.server3)
160                         except:
161                                 self.session.open(MessageBox, _("irc server %s not responding!\nplease check your network settings and/or irc servername..." %self.server3), MessageBox.TYPE_ERROR)
162
163
164         def saveAndExit(self):
165                 for x in self["config"].list:
166                         x[1].save()
167                 self.keySave()
168                 self.close()
169
170         def dontSaveAndExit(self):
171                 for x in self["config"].list:
172                         x[1].cancel()
173                 self.close()
174                 
175 class dreamIRCConfig:
176         def load(self):
177                 self.pipe=MessagePipe()
178                 self.status1=False
179                 self.status2=False
180                 self.status3=False
181                 self.hardware_info = HardwareInfo()
182                 self.device=self.hardware_info.get_device_name()
183                 self.mac=getMacAddress()
184                 self.mac_end=self.mac[6:]
185 #               print "device : %s  -  mac : %s  -  mac_end : %s " %(self.device,self.mac,self.mac_end)                 
186                 try:
187                         doc = xml.dom.minidom.parse(accounts_xml)
188                         root = doc.childNodes[0]
189                         for node in elementsWithTag(root.childNodes, "account"):
190                                 self.type = node.getAttribute("type")
191                                 self.login = node.getAttribute("login")
192                                 self.nick = node.getAttribute("nick")
193                                 if ((self.nick.lower() == "dreamircuser") or (self.nick == "") or (self.nick[0] == " ") or (self.nick.lower() == "dm8000-vip")) :
194                                         print "[dreamIRC] nickname error... restoring default..."
195                                         self.nick = self.device+"_"+self.mac_end
196                                 self.passwd = node.getAttribute("passwd")
197                                 self.server1 = node.getAttribute("server1") # atm only ip.. cause of probs with theads and dns..
198                                 self.server2 = node.getAttribute("server2") 
199                                 self.server3 = node.getAttribute("server3") 
200                                 self.port = node.getAttribute("port")
201                                 self.channel = node.getAttribute("channel")
202                                 self.debug = node.getAttribute("debug") # not used yet.. later will enable/disable console debug out..
203                 except IOError:
204                         self.type = "IRC"
205                         self.login = "1"
206                         self.nick = self.device+"_"+self.mac_end
207                         self.passwd = ""
208                         self.server1 = "irc.freenet.de"
209                         self.server2 = "irc.freenet.de"
210                         self.server3 = "irc.tu-illmenau.de"
211                         self.port = "06667"
212                         self.channel = "#dreamirc"
213                         self.debug = ""
214                 self.server1 = self.server1.strip()
215                 self.server2 = self.server2.strip()
216                 self.server3 = self.server3.strip()
217                 if self.server3:
218                         try:
219                                 self.result3=gethostbyname_ex(self.server3)
220                                 if self.result3:
221                                         for ip_tmp3 in self.result3[2]:
222                                                 self.ip=ip_tmp3
223                                                 self.server=self.server3
224                                                 self.status3=True
225                         except:
226                                 print "unable to resolve hostname %s..." % self.server3
227                 if self.server2:
228                         try:
229                                 self.result2=gethostbyname_ex(self.server2)
230                                 if self.result2:
231                                         for ip_tmp2 in self.result2[2]:
232                                                 self.ip=ip_tmp2
233                                                 self.server=self.server2
234                                                 self.status2=True
235                         except:
236                                 print "unable to resolve hostname %s..." % self.server2
237                 if self.server1:
238                         try:
239                                 self.result1=gethostbyname_ex(self.server1)
240                                 if self.result1:
241                                         for ip_tmp1 in self.result1[2]:
242                                                 self.ip=ip_tmp1
243                                                 self.server=self.server1
244                                                 self.status1=True
245                         except:
246                                 print "unable to resolve hostname %s..." % self.server1
247                                 
248                 if self.status1==False and self.status2==False and self.status3==False:
249                                 self.pipe.add("ERROR!!! no irc server was valid... please check settings...")
250                                 return False
251                 else:
252                         print " account = type: %s login:%s nick:%s passwd:%s server:%s ip:%s port:%s channel:%s debug:%s " % (self.type, self.login, self.nick, self.passwd, self.server, self.ip, self.port, self.channel, self.debug)
253                         self.accounts=[ircsupport.IRCAccount(self.type, string.atoi(self.login), str(self.nick), str(self.passwd), str(self.ip), string.atoi(self.port), str(self.channel))]
254                         print self.accounts
255                         return self.accounts
256         
257         def channel(self):
258                         doc = xml.dom.minidom.parse(accounts_xml)
259                         root = doc.childNodes[0]
260                         for node in elementsWithTag(root.childNodes, "account"):
261                                 self.channel = node.getAttribute("channel")
262                         return self.channel
263                 
264
265 #def getMacAddress():
266 #       for line in os.popen("/sbin/ifconfig"):
267 #               if line.find('Ether') > -1:
268 #                       mac = line.split()[4]
269 #                       new_mac = mac.replace(":","")
270 #                       break
271 #       return new_mac