aboutsummaryrefslogtreecommitdiffstats
path: root/local/utility.py
blob: 7c34f2eb160d72b4be14bfd767ae03d1354ab1d3 (plain) (blame)
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
import re
import math
import random
import string
import urllib.request, urllib.error, urllib.parse
from datetime import datetime, timedelta

import supybot.log as log
import supybot.conf as conf
import supybot.world as world
import supybot.ircutils as ircutils
import supybot.registry as registry

from . import globals


def registryValue(plugin, name, channel=None, value=True):
    group = conf.supybot.plugins.get(plugin)
    names = registry.split(name)
    for name in names:
        group = group.get(name)
    if channel is not None:
        try:
            if ircutils.isChannel(channel):
                group = group.get(channel)
            else:
                log.debug('registryValue got channel=%r', channel)
        except registry.NonExistentRegistryEntry:
            log.debug('non existent registry entry %r for channel %r', name, channel)
            pass
    if value:
        return group()
    else:
        return group


def configValue(name, channel=None, repo=None, type=None, module=None):
    if globals.configOverrides and name.lower() in globals.configOverrides:
        return globals.configOverrides[name.lower()]

    if channel == None and name not in ['channel', 'passcode', 'disallowChannelOverride', 'disallowConfigOverride']:
        channel = globals.channel

    return registryValue("Github", name, channel)


def addConfigOverride(name, value):
    if value.lower() == 'false':
        value = False;
    elif value.lower() == 'true':
        value = True;

    name = name.strip().lower()

    if name in ['passcode', 'disallowConfigOverride', 'allowArbitraryMessages']:
        return

    globals.configOverrides[name] = value


def resetConfigOverrides():
    globals.configOverrides = {}


def plural(number, s, p):
    if number != 1:
        return p
    return s


def parseBrackets(bracketConfig):
    if "M" in bracketConfig:
        return tuple(bracketConfig.split('M', 1))
    else:
        mid = math.floor(len(bracketConfig) / 2)
        if len(bracketConfig) % 2 == 0:
            return (bracketConfig[:mid], bracketConfig[mid:])
        else:
            # Do not include the middle character
            return (bracketConfig[:mid], bracketConfig[(mid + 1):])


def maxLen(msg, maxn=400, splitLines=True):
    """Cut down a string if its longer than `maxn` chars"""

    if msg is None:
        return None

    if splitLines is True:
        lines = msg.splitlines()
        line = lines[0] if lines else ""
    else:
        line = msg

    if len(line) > maxn:
        ret = "%s..." % (line[0:(maxn - 3)])
    elif splitLines is True and len(lines) > 1:
        ret = "%s..." % (line)
    else:
        ret = msg
    return ret

# TODO: Use a better data structure for this?
def colorAction(action):
    """Give an action string (e.g. created, edited) and get a nice IRC colouring."""

    # Fix past tense for some github verbs
    if action in ["synchronize"]:
        action += "d"

    if action in ["created", "opened", "tagged", "success", "passed", "fixed",
                  "published", "completed", "ready"]:
        return ircutils.bold(ircutils.mircColor(action, "green"))
    if action in ["deleted", "closed", "re-tagged", "deleted tag",
                  "failed", "errored", "failure", "still failing",
                  "broken", "error", "removed"]:
        return ircutils.bold(ircutils.mircColor(action, "red"))
    if action in ["assigned", "self-assigned", "merged", "synchronized",
                  "labeled"]:
        return ircutils.bold(ircutils.mircColor(action, "light blue"))
    if action in ["reopened", "pending"]:
        return ircutils.bold(ircutils.mircColor(action, "blue"))
    if action[0:5] in ["force"]:
        return ircutils.bold(ircutils.mircColor(action, "brown"))
    return action


def getShortURL(longurl):
    """ Returns a short URL generated by git.io"""
    if longurl is None:
        return None
    elif configValue("hideURL") is True:
        return None
    if configValue("shortURL") is False or not getShortURL.github.match(longurl):
        url = longurl
    else:
        data = 'url=%s' % (longurl)
        # Temporarily disabled
        url = longurl
        try:
            req = urllib.request.Request("https://git.io/", data.encode())
            response = urllib.request.urlopen(req)
            url = response.getheader('Location')
        except IOError as e:
            # Bad luck
            log.warning("URL shortening failed with: %s" % (e.message,))
            url = longurl
    return ircutils.mircColor(url, "purple")


getShortURL.github = re.compile('^([a-z]*\:\/\/)?([^\/]+.)?github.com')


def saveMessages(msgs):
    """ Saves the last messages so that the plugin can be easily tested """
    if not world.testing:
        return
    globals.messageList = msgs


def isYes(string):
    """Returns True if the string represents a yes, False, if it represents
    no, and another string if it represents something else"""
    value = string.strip().lower()

    if value in ['yes', 'always', 'on', 'true']:
        return True
    if value in ['no', 'never', 'off', 'false', 'null']:
        return False
    if value in ['changed', 'change', 'onchange', 'on_change', 'diff']:
        return 'change'


def isStatusVisible(repo, status, option='showSuccessfulBuildMessages'):
    """Returns whether the build status message should be shown"""
    config = isYes(configValue(option))

    changed = False
    if status != "passed" and status != "ready":
        changed = True
    elif type(config) is bool:
        changed = config
    elif repo not in globals.travisStatuses or status != globals.travisStatuses[repo]:
        # Config is 'on_change'
        changed = True

    globals.travisStatuses[repo] = status
    return changed


def randomString(length):
    """Returns a securely generated random string of a specific length"""
    return ''.join(random.SystemRandom().choice(
        string.ascii_uppercase + string.ascii_lowercase + string.digits
    ) for _ in range(length))


def secureCompare(s1, s2):
    """Securely compare two strings"""
    return sum(i != j for i, j in zip(s1, s2)) == 0


def getChannelSecret(channel):
    """Returns a secret for a channel, or None if that channel has no secret"""
    if globals.secretDB is None:
        return None
    try:
        record = globals.secretDB.get(channel, 1)
        return record.secret
    except KeyError:
        return None


def showIssueName(repoId, issueId):
    """Returns whether we should show the issue name for a repo issue"""
    now = datetime.now()

    if not configValue("preventIssueNameSpam"):
        globals.shownIssues.clear()
        return True

    if not repoId in globals.shownIssues:
        globals.shownIssues[repoId] = {}

    # Clean up old issues
    remove = [k for k in globals.shownIssues[repoId] if now - globals.shownIssues[repoId][k] > timedelta(seconds=15)]
    for k in remove: del globals.shownIssues[repoId][k]

    exists = issueId in globals.shownIssues[repoId]

    # Add our issue to the list
    globals.shownIssues[repoId][issueId] = now

    return not exists


def hexToMirc(hash):
    colors = {
        'white': (255, 255, 255),
        'black': (0, 0, 0),
        'blue': (0, 0, 127),
        'green': (0, 147, 0),
        'red': (255, 0, 0),
        'brown': (127, 0, 0),
        'purple': (156, 0, 156),
        'orange': (252, 127, 0),
        'yellow': (255, 255, 0),
        'light green': (0, 252, 0),
        'teal': (0, 147, 147),
        'light blue': (84, 255, 255),
        'dark blue': (84, 84, 255),
        'pink': (255, 0, 255),
        'dark grey': (127, 127, 127),
        'light grey': (230, 230, 230)
    }

    rgb = _hex_to_rgb(hash)

    return min(colors, key=lambda x: _colourDistance(colors[x], rgb))


def _hex_to_rgb(value):
    value = value.lstrip('#')
    lv = len(value)
    return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))


def _colourDistance(a, b):
    # Source: http://www.compuphase.com/cmetric.htm
    rmean = math.floor((a[0] + b[0]) / 2)
    red = a[0] - b[0]
    green = a[1] - b[1]
    blue = a[2] - b[2]

    return math.sqrt((((512 + rmean) * red * red) >> 8) + 4 * green * green + (((767 - rmean) * blue * blue) >> 8))

# Possible colours:
# white, black, (light/dark) blue, (light) green, red, brown, purple,
# orange, yellow, teal, pink, light/dark gray/grey

© 2014-2024 Faster IT GmbH | imprint | privacy policy