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