dreamIRC initial check-in
[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 xml.dom.minidom
33 from xml.dom.minidom import Node
34 from xml.dom import EMPTY_NAMESPACE
35 from Tools import XMLTools
36 from Tools.XMLTools import elementsWithTag, mergeText
37
38 import plugin
39 from plugin import *
40
41 import ircsupport
42
43 ChatText=str()
44 OutTextTmp=str()
45 BuddyList=str()
46 NewMsg=str()
47 Channel=str("ChatBox")
48
49 x=0
50 y=0
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
101         def updateBuddyWindow(self):
102                 global BuddyList 
103                 return BuddyList
104         
105         def getChatText(self):
106                 global ChatText
107                 return ChatText
108
109         def LastMsg(self):
110                 global NewMsg
111                 return NewMsg
112
113         def setLastMsg(self,text):
114                 global NewMsg
115                 NewMsg=str(text)
116
117         def getOutText(self):
118                 global OutTextTmp
119                 return OutTextTmp
120
121         def addOutText(self,text):
122                 global OutTextTmp
123                 OutTextTmp =str(text)
124                 print "OUTTEXT %s" % OutTextTmp
125
126         def clearOutText(self):
127                 global OutTextTmp
128                 OutTextTmp=str("")
129                 return OutTextTmp
130
131         def add(self,text):
132                 timestamp = time.strftime("[%H:%M:%S]", time.localtime(time.time()))
133                 global ChatText, NewMsg
134                 ChatText=ChatText+"%s %s\n" % (timestamp,text)
135                 NewMsg="%s %s" % (timestamp,text)
136                 self.logger.log("%s %s" %(timestamp,text))
137
138         def clear(self):
139                 global ChatText
140                 ChatText=str("")
141
142         def close(self):
143                 self.logger.close()
144
145         def buildBuddyList(self,text):
146                 global BuddyList    
147                 BuddyList= BuddyList+ "%s\n" %text
148
149         def clearBuddyList(self):
150                 global BuddyList
151                 BuddyList=""
152
153         def showBuddyList(self):
154                 global BuddyList    
155 #               self["buddy"].setText(BuddyList)
156                 return BuddyList
157
158         def updateDesc(self):
159                 global Channel
160                 return Channel
161
162         def getCannelName(self,text):
163                 global Channel
164                 Channel = "ChatBox #" + "%s\n" %text
165                 
166         def resetDesc(self):
167                 global Channel
168                 Channel = "ChatBox"
169         
170
171 class MessageLogger:
172         def __init__(self, file):
173                 self.file = file
174                 print '[dreamIRC] %s  MESSAGE LOGGER = %s \n'% (time.strftime("[%H:%M:%S]", time.localtime(time.time())),self.file)
175
176         def log(self, message):
177 #               timestamp = time.strftime("[%H:%M:%S]", time.localtime(time.time()))
178                 print '[dreamIRC] %s\n' % (message)
179                 self.file.write('%s\n' % (message))
180                 self.file.flush()
181
182         def close(self):
183                 self.file.close()
184
185 def readLogFile(args):
186         try:
187                 fp = file(args[0], 'r')
188                 lines = fp.readlines()
189                 fp.close()
190                 output = ""
191                 for x in lines:
192                         output += x
193         except IOError:
194                 output = args[1]
195         return output
196
197 def getMacAddress():
198         for line in os.popen("/sbin/ifconfig"):
199                 if line.find('Ether') > -1:
200                         mac = line.split()[4]
201                         new_mac = mac.replace(":","")
202                         break
203         return new_mac
204