-
Notifications
You must be signed in to change notification settings - Fork 50
/
quotes.py
161 lines (135 loc) · 6.4 KB
/
quotes.py
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
#--depends-on commands
import random, time
from src import ModuleManager, utils
@utils.export("channelset", utils.BoolSetting("channel-quotes",
"Whether or not quotes added from this channel are kept in this channel"))
class Module(ModuleManager.BaseModule):
def category_and_quote(self, s):
category, sep, quote = s.partition("=")
category = category.strip()
if not sep:
return category, None
return category, quote.strip()
def _get_quotes(self, target, category):
return target.get_setting("quotes-%s" % category, [])
def _set_quotes(self, target, category, quotes):
target.set_setting("quotes-%s" % category, quotes)
@utils.hook("received.command.qadd", alias_of="quoteadd")
@utils.hook("received.command.quoteadd", min_args=1)
@utils.kwarg("help", "Add a quote to a category")
@utils.kwarg("usage", "<category> = <quote>")
def quote_add(self, event):
category, quote = self.category_and_quote(event["args"])
if category and quote:
target = event["server"]
if event["is_channel"] and event["target"].get_setting(
"channel-quotes", False):
target = event["target"]
quotes = self._get_quotes(target, category)
quotes.append([event["user"].name, int(time.time()), quote])
self._set_quotes(target, category, quotes)
event["stdout"].write("Quote added")
else:
event["stderr"].write("Please provide a category AND quote")
def _target_zip(self, target, quotes):
return [[u, t, q, target] for u, t, q in quotes]
@utils.hook("received.command.qdel", alias_of="quotedel")
@utils.hook("received.command.quotedel", min_args=1)
@utils.kwarg("help", "Delete a given quote from a given category")
@utils.kwarg("usage", "<category> = <quote>")
def quote_del(self, event):
category, remove_quote = self.category_and_quote(event["args"])
category = category or event["args"].strip()
message = None
quotes = self._target_zip(event["server"],
self._get_quotes(event["server"], category))
if event["is_channel"]:
quotes += self._target_zip(event["target"],
self._get_quotes(event["target"], category))
quotes = sorted(quotes, key=lambda q: q[1])
if not quotes:
raise utils.EventError("Quote category '%s' not found" %
category)
found_target = None
found_quote = None
if not remove_quote == None:
remove_quote_lower = remove_quote.lower().strip()
for nickname, time_added, quote, target in quotes[:]:
if remove_quote_lower in quote.lower().strip():
found_target = target
found_quote = [nickname, time_added, quote]
message = "Removed quote from '%s'"
break
else:
if quotes:
nickname, time_added, quote, target = quotes.pop(-1)
found_target = target
found_quote = [nickname, time_added, quote]
message = "Removed last '%s' quote"
if not message == None:
target_quotes = self._get_quotes(found_target, category)
target_quotes.remove(found_quote)
self._set_quotes(found_target, category, target_quotes)
_, _, quote = found_quote
message = f"{message} ({quote})"
event["stdout"].write(message % category)
else:
event["stderr"].write("Quote not found")
@utils.hook("received.command.q", alias_of="quote")
@utils.hook("received.command.quote", min_args=1)
@utils.kwarg("help", "Get a random quote from a given category")
@utils.kwarg("usage", "<category> [= <search>]")
def quote(self, event):
category, search = self.category_and_quote(event["args"])
if event["server"].has_user(category):
category = event["server"].get_user_nickname(
event["server"].get_user(category).get_id())
quotes = event["server"].get_setting("quotes-%s" % category, [])
if event["is_channel"]:
quotes += self._get_quotes(event["target"], category)
if search:
search_lower = search.lower()
quotes = [q for q in quotes if search_lower in q[-1].lower()]
if quotes:
index = random.randint(0, len(quotes)-1)
nickname, time_added, quote = quotes[index]
category_str = category
if search:
category_str = "%s (%s [%d found])" % (category_str, search,
len(quotes))
event["stdout"].write("%s: %s" % (category_str, quote))
else:
event["stderr"].write("No matching quotes")
@utils.hook("received.command.grab", alias_of="quotegrab")
@utils.hook("received.command.quotegrab", min_args=1, channel_only=True)
@utils.kwarg("help", "Grab the latest 1-3 lines from a user and add them "
"as a quote")
@utils.kwarg("usage", "<nickname> [line-count]")
def quote_grab(self, event):
line_count = 1
if len(event["args_split"]) > 1:
line_count = utils.parse.try_int(event["args_split"][1])
if not line_count or not (0 < line_count < 4):
raise utils.EventError(
"Please provide a number between 1 and 3")
target_user = event["args_split"][0]
lines = event["target"].buffer.find_many_from(target_user, line_count)
if lines:
lines.reverse()
target = event["server"]
if event["target"].get_setting("channel-quotes", False):
target = event["target"]
lines_str = []
for line in lines:
lines_str.append(line.format())
text = " ".join(lines_str)
quote_category = line.sender
if event["server"].has_user(quote_category):
quote_category = event["server"].get_user_nickname(
event["server"].get_user(quote_category).get_id())
quotes = self._get_quotes(target, quote_category)
quotes.append([event["user"].name, int(time.time()), text])
self._set_quotes(target, quote_category, quotes)
event["stdout"].write("Quote added")
else:
event["stderr"].write("Nothing found to quote")