Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/services/console/lib/purk/scripts/theme.py
blob: 7fda4d2dc32a62e797e4ef55c43b864cd51ba9ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import time

import windows
import widgets
import chaninfo

from conf import conf

textareas = {}
if 'font' in conf:
    textareas['font'] = conf['font']
if 'bg_color' in conf:
    textareas['bg'] = conf['bg_color']
if 'fg_color' in conf:
    textareas['fg'] = conf['fg_color']

widgets.set_style("view", textareas)
widgets.set_style("nicklist", textareas)

#copied pretty directly from something that was probably copied from wine sources
def RGBtoHSL(r, g, b):
    maxval = max(r, g, b)
    minval = min(r, g, b)
    
    luminosity = ((maxval + minval) * 240 + 255) // 510
    
    if maxval == minval:
        saturation = 0
        hue = 160
    else:
        delta = maxval - minval
        
        if luminosity <= 120:
            saturation = ((maxval+minval)//2 + delta*240) // (maxval + minval)
        else:
            saturation = ((150-maxval-minval)//2 + delta*240) // (150-maxval-minval)
        
        #sigh..
        rnorm = (delta//2 + maxval*40 - r*40)//delta
        gnorm = (delta//2 + maxval*40 - g*40)//delta
        bnorm = (delta//2 + maxval*40 - b*40)//delta
        
        if r == maxval:
            hue = bnorm-gnorm
        elif g == maxval:
            hue = 80+rnorm-bnorm
        else:
            hue = 160+gnorm-rnorm
        hue = hue % 240
    return hue, saturation, luminosity

#copied from the same place
def huetoRGB(hue, mid1, mid2):
    hue = hue % 240
    
    if hue > 160:
        return mid1
    elif hue > 120:
        hue = 160 - hue
    elif hue > 40:
        return mid2
    return ((hue * (mid2 - mid1) + 20) // 40) + mid1

#this too
def HSLtoRGB(hue, saturation, luminosity):
    if saturation != 0:
        if luminosity > 120:
            mid2 = saturation + luminosity - (saturation * luminosity + 120)//240
        else:
            mid2 = ((saturation + 240) * luminosity + 120)//240
        
        mid1 = luminosity * 2 - mid2
        
        return tuple((huetoRGB(hue+x, mid1, mid2) * 255 + 120) // 240 for x in (80,0,-80))
    else:
        value = luminosity * 255 // 240
        return value, value, value

def gethashcolor(string):
    h = hash(string)
    rgb = HSLtoRGB(h%241, 100-h//241%61, 90)
    return "%02x%02x%02x" % rgb

#take an event e and trigger the highlight event if necessary
def hilight_text(e):
    if not hasattr(e, 'Highlight'):
        e.Highlight = []
        core.events.trigger('Highlight', e)

#hilight own nick
def onHighlight(e):
    lowertext = e.text.lower()
    for word in conf.get('highlight_words', []) + [e.network.me] + e.network.nicks:
        lowerword = word.lower()
        pos = lowertext.find(lowerword, 0)
        while pos != -1:
            e.Highlight.append((pos, pos+len(word)))
            pos = lowertext.find(lowerword, pos+1)

def prefix(e):
    return time.strftime(conf.get('timestamp', ''))

def getsourcecolor(e):
    address = getattr(e, "address", "")
    if address:
        if e.network.me == e.source:
            e.network._my_address = address
    elif e.network.me == e.source:
        address = getattr(e.network, "_my_address", "")
    if '@' in address:
        address = address.split('@')[1]
    if not address:
        address = e.source
    return "\x04%s" % gethashcolor(address)

def format_source(e):
    highlight = getattr(e, "Highlight", "") and '\x02' or ''
    return "%s\x04%s%s" % (highlight, getsourcecolor(e), e.source)

def format_info_source(e):
    if e.source == e.network.me:
        return "\x04%sYou" % (getsourcecolor(e))
    else:
        return "\x04%s%s" % (getsourcecolor(e), e.source)

def address(e):
    #if e.source != e.network.me:
    #    return "%s " % info_in_brackets(e.address)
    #else:
    #    return ""
    return ""

def text(e):
    if e.text:
        #return " %s" % info_in_brackets(e.text)
        return ": \x0F%s" % e.text
    else:
        return ""
        
def info_in_brackets(text):
    return "(\x044881b6%s\x0F)" % text

def pretty_time(secs):
    times = (
        #("years", "year", 31556952),
        ("weeks", "week", 604800),
        ("days", "day", 86400),
        ("hours", "hour", 3600),
        ("minutes", "minute", 60),
        ("seconds", "second", 1),
        )
    if secs == 0:
        return "0 seconds"
    result = ""
    for plural, singular, amount in times:
        n, secs = divmod(secs, amount)
        if n == 1:
            result = result + " %s %s" % (n, singular)
        elif n:
            result = result + " %s %s" % (n, plural)
    return result[1:]

def onText(e):
    hilight_text(e)
    color = getsourcecolor(e)
    to_write = prefix(e)
    if e.network.me == e.target:    # this is a pm
        if e.window.id == e.network.norm_case(e.source):
            to_write += "\x02<\x0F%s\x0F\x02>\x0F " % (format_source(e))
        else:
            to_write += "\x02*\x0F%s\x0F\x02*\x0F " % (format_source(e))
    else:
        if e.window.id == e.network.norm_case(e.target):
            to_write += "\x02<\x0F%s\x0F\x02>\x0F " % (format_source(e))
        else:
            to_write += "\x02<\x0F%s:%s\x0F\x02>\x0F " % (format_source(e), e.target)
    to_write += e.text
    
    if e.Highlight:
        e.window.write(to_write, widgets.HILIT)
    else:
        e.window.write(to_write, widgets.TEXT)
    
def onOwnText(e):
    color = getsourcecolor(e)
    to_write = prefix(e)
    if e.window.id == e.network.norm_case(e.target):
        to_write += "\x02<\x0F%s\x0F\x02>\x0F %s" % (format_source(e), e.text)
    else:
        to_write += "%s->\x0F \x02*\x0F%s\x0F\x02*\x0F %s" % (color, e.target, e.text)
    
    e.window.write(to_write)
    
def onAction(e):
    hilight_text(e)
    color = color = getsourcecolor(e)
    to_write = "%s\x02*\x0F %s\x0F %s" % (prefix(e), format_source(e), e.text)
    
    if e.Highlight:
        e.window.write(to_write, widgets.HILIT)
    else:
        e.window.write(to_write, widgets.TEXT)
    
def onOwnAction(e):
    color = getsourcecolor(e)
    to_write = "%s\x02*\x0F %s\x0F %s" % (prefix(e), format_source(e), e.text)
    
    e.window.write(to_write)

def onNotice(e):
    hilight_text(e)
    color = getsourcecolor(e)
    to_write = prefix(e)
    if e.network.me == e.target:    # this is a pm
        to_write += "\x02-\x0F%s\x0F\x02-\x0F " % (format_source(e))
    else:
        to_write += "\x02-\x0F%s:%s\x0F\x02-\x0F " % (format_source(e), e.target)
    to_write += e.text
    
    e.window.write(to_write, (e.Highlight and widgets.HILIT) or widgets.TEXT)

def onOwnNotice(e):
    color = getsourcecolor(e)
    to_write = "%s-> \x02-\x02%s\x0F\x02-\x0F %s" % (prefix(e), e.target, e.text)
    
    e.window.write(to_write)

def onCtcp(e):
    color = getsourcecolor(e)
    to_write = "%s\x02[\x02%s\x0F\x02]\x0F %s" % (prefix(e), format_source(e), e.text)
    
    if not e.quiet:
        e.window.write(to_write)

def onCtcpReply(e):
    color = getsourcecolor(e)
    to_write = "%s%s--- %s reply from %s:\x0F %s" % (prefix(e), color, e.name.capitalize(), format_source(e), ' '.join(e.args))
    
    window = windows.manager.get_active()
    if window.network != e.network:
        window = windows.get_default(e.network)
    window.write(to_write, widgets.TEXT)

def onJoin(e):
    if e.source == e.network.me:
        to_write = "%s%s %sjoin %s" % (prefix(e), format_info_source(e), address(e), e.target)
    else:
        to_write = "%s%s %sjoins %s" % (prefix(e), format_info_source(e), address(e), e.target)

    e.window.write(to_write)
        
def onPart(e):
    if e.source == e.network.me:
        to_write = "%s%s leave %s%s" % (prefix(e), format_info_source(e), e.target, text(e))
    else:
        to_write = "%s%s leaves %s%s" % (prefix(e), format_info_source(e), e.target, text(e))
    
    e.window.write(to_write)

def onKick(e):
    if e.source == e.network.me:
        to_write = "%s%s kick %s%s" % (prefix(e), format_info_source(e), e.target, text(e))
    else:
        to_write = "%s%s kicks %s%s" % (prefix(e), format_info_source(e), e.target, text(e))
    
    e.window.write(to_write, (e.target == e.network.me and widgets.HILIT) or widgets.EVENT)
        
def onMode(e):
    if e.source == e.network.me:
        to_write = "%s%s set mode:\x0F %s" % (prefix(e), format_info_source(e), e.text)
    else:
        to_write = "%s%s sets mode:\x0F %s" % (prefix(e), format_info_source(e), e.text)
    
    e.window.write(to_write)
        
def onQuit(e):
    to_write = "%s%s leaves%s" % (prefix(e), format_info_source(e), text(e))
    
    for channame in chaninfo.channels(e.network):
        if chaninfo.ison(e.network, channame, e.source):
            window = windows.get(windows.ChannelWindow, e.network, channame, core)
            if window:
                window.write(to_write)

def onNick(e):
    color = getsourcecolor(e)
    if e.source == e.network.me:
        to_write = "%s%sYou are now known as %s" % (prefix(e), color, e.target)
    else:
        to_write = "%s%s%s is now known as %s" % (prefix(e), color, e.source, e.target)
    
    if e.source == e.network.me:
        for window in windows.get_with(core.manager, network=e.network):
            window.write(to_write)
    else:
        for channame in chaninfo.channels(e.network):
            if chaninfo.ison(e.network,channame,e.source):
                window = windows.get(windows.ChannelWindow, e.network, channame)
                if window:
                    window.write(to_write)

def onTopic(e):
    if e.source == e.network.me:
        to_write = "%s%s set topic:\x0F %s" % (prefix(e), format_info_source(e), e.text)
    else:
        to_write = "%s%s sets topic:\x0F %s" % (prefix(e), format_info_source(e), e.text)
    
    e.window.write(to_write)

def onRaw(e):
    if not e.quiet:
        if e.msg[1].isdigit():
            if e.msg[1] == '332':
                window = windows.get(windows.ChannelWindow, e.network, e.msg[3], core) or e.window
                window.write(
                    "%sTopic on %s is: %s" % 
                        (prefix(e), e.msg[3], e.text)
                        )
                
            elif e.msg[1] == '333':
                window = windows.get(windows.ChannelWindow, e.network, e.msg[3], core) or e.window
                window.write(
                    "%sTopic on %s set by %s at time %s" % 
                        (prefix(e), e.msg[3], e.msg[4], time.ctime(int(e.msg[5])))
                        )
            
            elif e.msg[1] == '329': #RPL_CREATIONTIME
                pass
            
            elif e.msg[1] == '311': #RPL_WHOISUSER
                e.window.write("* %s is %s@%s * %s" % (e.msg[3], e.msg[4], e.msg[5], e.msg[7]))
            
            elif e.msg[1] == '312': #RPL_WHOISSERVER
                e.window.write("* %s on %s (%s)" % (e.msg[3], e.msg[4], e.msg[5]))
            
            elif e.msg[1] == '317': #RPL_WHOISIDLE
                e.window.write("* %s has been idle for %s" % (e.msg[3], pretty_time(int(e.msg[4]))))
                if e.msg[5].isdigit():
                    e.window.write("* %s signed on %s" % (e.msg[3], time.ctime(int(e.msg[5]))))
            
            elif e.msg[1] == '319': #RPL_WHOISCHANNELS
                e.window.write("* %s on channels: %s" % (e.msg[3], e.msg[4]))
            
            elif e.msg[1] == '330': #RPL_WHOISACCOUNT
                #this appears to conflict with another raw, so if there's anything weird about it,
                # we fall back on the default
                if len(e.msg) == 6 and not e.msg[4].isdigit() and not e.msg[5].isdigit():
                    e.window.write("* %s %s %s" % (e.msg[3], e.msg[5], e.msg[4]))
                else:
                    e.window.write("* %s" % ' '.join(e.msg[3:]))
            
            else:
                e.window.write("* %s" % ' '.join(e.msg[3:]))
        elif e.msg[1] == 'ERROR':
            e.window.write("Error: %s" % e.text)

def onDisconnect(e):
    to_write = '%s* Disconnected' % prefix(e)
    if e.error:
        to_write += ' (%s)' % e.error

    for window in windows.get_with(network=e.network):
        if isinstance(window, windows.StatusWindow):
            window.write(to_write, widgets.TEXT)
        else:
            window.write(to_write, widgets.EVENT)