|
1
|
|
|
import re |
|
2
|
|
|
from datetime import datetime |
|
3
|
|
|
class bcolors: |
|
4
|
|
|
""" |
|
5
|
|
|
Allows for prettyprinting to the console for debugging. |
|
6
|
|
|
""" |
|
7
|
|
|
HEADER = '\033[95m' |
|
8
|
|
|
OKBLUE = '\033[94m' |
|
9
|
|
|
OKGREEN = '\033[92m' |
|
10
|
|
|
WARNING = '\033[93m' |
|
11
|
|
|
CYAN = '\033[36m' |
|
12
|
|
|
GREEN = '\033[32m' |
|
13
|
|
|
YELLOW = '\033[33m' |
|
14
|
|
|
FAIL = '\033[91m' |
|
15
|
|
|
ENDC = '\033[0m' |
|
16
|
|
|
|
|
17
|
|
|
def strip_nick(nick): |
|
18
|
|
|
""" |
|
19
|
|
|
Clean up nicks of their op levels (&Schooly_D, ~BoneKin, etc) |
|
20
|
|
|
""" |
|
21
|
|
|
nick = re.sub('[@~+]', '', nick) |
|
22
|
|
|
return nick |
|
23
|
|
|
|
|
24
|
|
|
def depends(self, module_name): |
|
25
|
|
|
for m in self.loaded_modules: |
|
26
|
|
|
if m.__class__.__name__ == module_name: |
|
27
|
|
|
return m |
|
28
|
|
|
return None |
|
29
|
|
|
|
|
30
|
|
|
def commands(*command_list): |
|
31
|
|
|
def add_attribute(function): |
|
32
|
|
|
if not hasattr(function, "commands"): |
|
33
|
|
|
function.commands = [] |
|
34
|
|
|
function.commands.extend(command_list) |
|
35
|
|
|
return function |
|
36
|
|
|
return add_attribute |
|
37
|
|
|
|
|
38
|
|
|
def parse_line(line): |
|
39
|
|
|
""" |
|
40
|
|
|
returns an object with a nice set of line-pulled-apart members |
|
41
|
|
|
""" |
|
42
|
|
|
class Parsed(): |
|
43
|
|
|
def __init(self): |
|
44
|
|
|
self.first_word = None |
|
45
|
|
|
self.message = None |
|
46
|
|
|
self.channel = None |
|
47
|
|
|
self.user = None |
|
48
|
|
|
|
|
49
|
|
|
def startswith(self, thing): |
|
50
|
|
|
if self.message.startswith(thing): |
|
51
|
|
|
return True |
|
52
|
|
|
return False |
|
53
|
|
|
|
|
54
|
|
|
parsed = Parsed() |
|
55
|
|
|
|
|
56
|
|
|
try: |
|
57
|
|
|
parsed.first_word = line.split(":", 2)[2] |
|
58
|
|
|
parsed.message = line.split(":",2)[2] |
|
59
|
|
|
parsed.channel = line.split()[2] |
|
60
|
|
|
if "JOIN" in line or "QUIT" in line: |
|
61
|
|
|
parsed.user = line.split("!")[0].replace(":","") |
|
62
|
|
|
else: |
|
63
|
|
|
parsed.user = line.split(":")[1].rsplit("!")[0] # nick is first thing on line |
|
64
|
|
|
except IndexError: |
|
65
|
|
|
return None |
|
66
|
|
|
else: |
|
67
|
|
|
return parsed |
|
68
|
|
|
|
|
69
|
|
|
def __prettyDate(time): |
|
70
|
|
|
""" |
|
71
|
|
|
Similar to Rails's nice time since thing. |
|
72
|
|
|
""" |
|
73
|
|
|
now = datetime.now() |
|
74
|
|
|
if type(time) is int: |
|
75
|
|
|
diff = now - datetime.fromtimestamp(time) |
|
76
|
|
|
elif isinstance(time,datetime): |
|
77
|
|
|
diff = now - time |
|
78
|
|
|
second_diff = diff.seconds |
|
79
|
|
|
day_diff = diff.days |
|
80
|
|
|
|
|
81
|
|
|
if day_diff == 0: |
|
82
|
|
|
if second_diff < 10: |
|
83
|
|
|
return "just now" |
|
84
|
|
|
if second_diff < 60: |
|
85
|
|
|
return str(second_diff) + " seconds ago" |
|
86
|
|
|
if second_diff < 120: |
|
87
|
|
|
return "a minute ago" |
|
88
|
|
|
if second_diff < 3600: |
|
89
|
|
|
return str( second_diff / 60 ) + " minutes ago" |
|
90
|
|
|
if second_diff < 7200: |
|
91
|
|
|
return "an hour ago" |
|
92
|
|
|
if second_diff < 86400: |
|
93
|
|
|
return str( second_diff / 3600 ) + " hours ago" |
|
94
|
|
|
if day_diff == 1: |
|
95
|
|
|
return "yesterday" |
|
96
|
|
|
if day_diff < 7: |
|
97
|
|
|
return str(day_diff) + " days ago" |
|
98
|
|
|
if day_diff < 31: |
|
99
|
|
|
return str(day_diff/7) + " weeks ago" |
|
100
|
|
|
if day_diff < 365: |
|
101
|
|
|
return str(day_diff/30) + " months ago" |
|
102
|
|
|
return str(day_diff/365) + " years ago" |
|
103
|
|
|
|