specify except error
[enigma2-plugins.git] / dreamirc / src / dreamIRCTools.py
1 #!/usr/bin/env python
2 from enigma import *
3 from Screens.Screen import Screen
4
5 from Components.Pixmap import *
6 from Components.Pixmap import Pixmap
7 from Components.ActionMap import ActionMap, NumberActionMap
8 from Components.ScrollLabel import ScrollLabel
9 from Components.GUIComponent import *
10 from Components.MenuList import MenuList
11 from Components.Input import Input
12 from Components.Label import Label
13 from Components.config import *
14 from Components.ConfigList import ConfigList
15 from Components.MenuList import MenuList
16 from Components.MultiContent import MultiContentEntryText
17 from Plugins.Plugin import PluginDescriptor
18 from Tools.NumericalTextInput import *
19 from Tools.Directories import *
20
21 import skin
22 from Components.HTMLComponent import *
23 from Components.GUIComponent import *
24 from enigma import eLabel, eWidget, eSlider, fontRenderClass, ePoint, eSize
25
26 import os
27 import string
28 import time
29 import datetime
30 import sys
31
32 import plugin
33 from plugin import *
34
35 import ircsupport
36 import xml.dom.minidom
37 from xml.dom.minidom import Node
38 from Tools import XMLTools
39 from Tools.XMLTools import elementsWithTag, mergeText
40
41 ChatText=str()
42 OutTextTmp=str()
43 BuddyList=str()
44 NewMsg=str()
45 Channel=str("ChatBox")
46
47 x=0
48 y=0
49
50 accounts_xml="/etc/dreamIRC.xml"
51
52 class ChatWindow(ScrollLabel):
53         def __init__(self,session):
54                 ScrollLabel.__init__(self,text="")
55                 self.timer=eTimer()
56                 self.timer.timeout.get().append(self.updateChatWindow)
57                 self.timer.start(250)
58                 self.pipe=MessagePipe()
59                 self.oldText=""
60                 
61         def updateChatWindow(self):
62                 if (len(self.pipe.LastMsg()) >0) or (self.oldText!=self.pipe.getChatText()):
63                         self.oldText=self.pipe.getChatText()
64                         self.setText(self.pipe.getChatText())
65                         self.lastPage()
66                         self.pipe.setLastMsg("")
67
68 class BuddyWindow(ScrollLabel):
69         def __init__(self,session):
70                 ScrollLabel.__init__(self,text="")
71                 self.timer=eTimer()
72                 self.timer.timeout.get().append(self.updateBuddyWindow)
73                 self.timer.start(500)
74                 self.oldlist=""
75
76         def updateBuddyWindow(self):
77                 if (self.oldlist != BuddyList):
78                         self.setText(BuddyList)
79                         self.oldlist =BuddyList
80
81 class ChanName(Label):
82         def __init__(self,session):
83                 Label.__init__(self,text=Channel)
84                 self.timer=eTimer()
85                 self.timer.timeout.get().append(self.updateChanName)
86                 self.timer.start(500)
87                 self.oldname=self.text
88                 self.pipe=MessagePipe()
89
90         def updateChanName(self):
91                 self.newname=self.pipe.updateDesc()
92                 if (self.oldname != self.newname):
93                         self.setText(self.newname)
94                         self.oldname=self.newname
95
96 class MessagePipe():
97         def __init__(self):
98                 global BuddyList
99                 self.logger=MessageLogger(open("/var/log/dreamIRC.log", "a"))
100                 self.debug_state=debug()
101                 if self.debug_state==True:
102                         self.debuglogger=MessageLogger(open("/var/log/dreamIRC_debug.log", "a"))
103
104         def updateBuddyWindow(self):
105                 global BuddyList 
106                 return BuddyList
107         
108         def getChatText(self):
109                 global ChatText
110                 return ChatText
111
112         def LastMsg(self):
113                 global NewMsg
114                 return NewMsg
115
116         def setLastMsg(self,text):
117                 global NewMsg
118                 NewMsg=str(text)
119
120         def getOutText(self):
121                 global OutTextTmp
122                 return OutTextTmp
123
124         def addOutText(self,text):
125                 global OutTextTmp
126                 OutTextTmp =str(text)
127
128         def clearOutText(self):
129                 global OutTextTmp
130                 OutTextTmp=str("")
131                 return OutTextTmp
132
133         def add(self,text):
134                 timestamp = time.strftime("[%H:%M:%S]", time.localtime(time.time()))
135                 global ChatText, NewMsg
136                 ChatText=ChatText+"%s %s\n" % (timestamp,text)
137                 NewMsg="%s %s" % (timestamp,text)
138                 self.logger.log("%s %s" %(timestamp,text))
139                 if self.debug_state==True:
140                         self.debuglogger.log("%s %s" %(timestamp,text))
141
142         def debug(self,text):
143                 if self.debug_state==True:
144                         timestamp = time.strftime("[%H:%M:%S]", time.localtime(time.time()))
145                         self.debuglogger.log("%s %s" %(timestamp,text))
146                 else:
147                         print text
148
149         def clear(self):
150                 global ChatText
151                 ChatText=str("")
152
153         def close(self):
154                 self.logger.close()
155                 if self.debug_state==True:
156                         self.debuglogger.close()        
157
158         def buildBuddyList(self,text):
159                 global BuddyList    
160                 BuddyList= BuddyList+ "%s\n" %text
161
162         def clearBuddyList(self):
163                 global BuddyList
164                 BuddyList=""
165
166         def showBuddyList(self):
167                 global BuddyList    
168
169                 return BuddyList
170
171         def updateDesc(self):
172                 global Channel
173                 return Channel
174
175         def getCannelName(self,text):
176                 global Channel
177                 Channel = "ChatBox #" + "%s\n" %text
178                 
179         def resetDesc(self):
180                 global Channel
181                 Channel = "ChatBox"
182         
183
184 class MessageLogger:
185         def __init__(self, file):
186                 self.file = file
187                 print '[dreamIRC] %s  MESSAGE LOGGER = %s \n'% (time.strftime("[%H:%M:%S]", time.localtime(time.time())),self.file)
188
189         def log(self, message):
190                 print '[dreamIRC] %s\n' % (message)
191                 self.file.write('%s\n' % (message))
192                 self.file.flush()
193
194         def close(self):
195                 self.file.close()
196
197 def readLogFile(args):
198         try:
199                 fp = file(args[0], 'r')
200                 lines = fp.readlines()
201                 fp.close()
202                 output = ""
203                 for x in lines:
204                         output += x
205         except IOError:
206                 output = args[1]
207         return output
208
209 def getMacAddress():
210         for line in os.popen("/sbin/ifconfig"):
211                 if line.find('Ether') > -1:
212                         mac = line.split()[4]
213                         new_mac = mac.replace(":","")
214                         break
215         return new_mac
216
217 def debug():
218         try:    
219                 doc = xml.dom.minidom.parse(accounts_xml)
220                 root = doc.childNodes[0]
221                 for node in elementsWithTag(root.childNodes, "account"):
222                         debug = node.getAttribute("debug")
223                 if debug=="False":
224                         return False
225                 else:   
226                         return True
227         except IOError:
228                 return False