-
Notifications
You must be signed in to change notification settings - Fork 1
/
quote.py
216 lines (194 loc) · 7.99 KB
/
quote.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
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
# -*- coding: utf-8 -*-
import random
#Update schema
__url__ = 'https://raw.githubusercontent.com/KittyHawkIrc/modules/production/' + __name__ + '.py'
__version__ = 1.0
def declare():
declares = ['q', 'quote',
'qs', 'quotesearch',
'qa', 'quoteadd',
'qd', 'quotedel']
return {command: 'privmsg' for command in declares}
def callback(self):
channel = self.channel
command = self.command.lower()
user = self.user.split('!')[0]
msg = self.msg
message = self.message.split(command, 1)[1].strip()
words = message.split()
locker = self.locker
dict_exists = hasattr(locker, 'quote')
sep = ' / '
author, index, quote, num_quotes = None, None, None, None
if len(words) > 0 and is_nick(words[0]):
author = words[0]
if len(words) > 1:
quote = message[len(author):].strip()
# Quote add
if command in ['qa', 'quoteadd']:
if len(words) > 1 and author:
if not dict_exists:
locker.quote = {}
if author.lower() not in locker.quote:
locker.quote[author.lower()] = []
quote_list = locker.quote[author.lower()]
if quote not in quote_list and quote:
quote_list.append(quote)
self.cache_save() #persist cache post-restarts
index = len(locker.quote[author.lower()])
output = 'Quote "{q}" added as quote #{i} for user [{a}]'
else:
index = quote_list.index(quote) + 1
output = 'Quote "{q}" exists as quote #{i} for user [{a}]'
else:
output = '{c}: <nick> <quote>'
else:
try:
# Quote delete
if command in ['qd', 'quotedel']:
if not self.isowner:
output = 'Quotes can only be deleted by bot owners.'
elif len(words) > 0 and words[0].lower() == 'all':
del locker.quote
output = 'Removed all quotes.'
elif (author and not quote) or (quote and quote == 'all'):
locker.quote.pop(author.lower())
output = 'Removed all quotes from user [{a}]'
elif author and words[1].isdigit():
index = int(words[1])
locker.quote[author.lower()].pop(int(words[1]) - 1)
output = 'Removed quote #{i} from user [{a}]'
elif author and quote in locker.quote[author.lower()]:
index = locker.quote[author.lower()].index(quote)
locker.quote[author.lower()].pop(index)
index += 1
output = 'Removed quote #{i} from user [{a}]'
elif author and quote:
output = 'Quote "{q}" not found for user [{a}]'
else:
output = '{c}: <nick> (<quote> | <quote number>)'
# Quote search
elif command in ['qs', 'quotesearch']:
if len(words) > 0:
search_quotes = search_dict(message, locker.quote)
if search_quotes:
author = random.choice(key_equal_weight(search_quotes))
quote_list = search_quotes[author.lower()]
index, quote = random.choice(quote_list)
num_quotes = len(locker.quote[author.lower()])
output = '{a}{s}#{i} of {n}{s}"{q}"'
else:
output = 'Search string "{m}" not found.'
else:
output = '{c}: <search string>'
# Standard quote retrieve
elif command in ['q', 'quote']:
if not author:
author = random.choice(key_equal_weight(locker.quote))
quote_list = locker.quote[author.lower()]
num_quotes = len(quote_list)
if len(words) > 1 and words[1].isdigit():
index = int(words[1]) - 1
else:
index = random.randrange(num_quotes)
quote = quote_list[index]
index += 1
output = '{a}{s}#{i} of {n}{s}"{q}"'
except AttributeError:
output = 'No quotes have been added yet.'
except IndexError:
output = 'Quote #{i} out of range, user [{a}] has {n} quotes.'
except KeyError:
output = 'No quotes stored for user [{a}]'
return msg(channel, output.format(u = user, c = command, s = sep,
m = message,
a = author, q = quote, i = index,
n = num_quotes))
# search_dict(search, dict_of_list) takes dict_of_list and returns dict_of_list
# where list only contains items where search in item
# search_dict: Str Dict(Any: List(Str)) -> Dict(Any: List(Tuple(Int, Str)))
def search_dict(search, dict_of_list):
out_dict = {}
for key, value in dict_of_list.items():
count = 0
for item in value:
count += 1
if search.upper().lower() in item.upper().lower():
try:
out_dict[key].append((count, item))
except:
out_dict[key] = [(count, item)]
return out_dict
# key_equal_weight(dict_of_list) takes a dict_of_list and returns a list of its
# keys such that there a k for every item in dict_of_list[k]
# key_equal_weight: Dict(Any: List(Any)) -> List(Any)
def key_equal_weight(dict_of_list):
keys = []
for key, value in dict_of_list.items():
keys.extend([key] * len(value))
return keys
# is_nick(string) takes 'string' and determines if it is a valid IRC nickname
# is_nick: Str -> Bool
# requires: isinstance(string, str)
def is_nick(string):
for i, char in enumerate(string):
if ((i > 0 and (char.isdigit() or char == '-')) or
char.isalpha() or char in '_-\[]{}^`|'):
continue
else:
return False
return True
################################ START: Testing ################################
class api:
def msg(self, channel, text):
return '[%s] %s' % (channel, text)
class empty:
pass
if __name__ == '__main__':
def cache_save():
print 'Cache saved'
api = api()
declares = declare().keys()
setattr(api, 'cache_save', cache_save)
setattr(api, 'type', 'privmsg')
setattr(api, 'channel', '#channel')
setattr(api, 'locker', empty)
setattr(api, 'user', 'nick!ident@host')
setattr(api, 'isop', False)
setattr(api, 'isowner', False)
###############nick########### START: Interactive Testing ##########################
'''
while(True):
_input = raw_input('Enter message here: ')
input_split = _input.split()
if input_split[0] == 'op':
setattr(api, 'isop', True)
print 'User opped'
continue
elif input_split[0] == 'deop':
setattr(api, 'isop', False)
print 'User deopped'
continue
elif input_split[0] == 'owner':
setattr(api, 'isowner', True)
setattr(api, 'isop', True)
print 'User ownered'
continue
elif input_split[0] == 'deowner':
setattr(api, 'isowner', False)
print 'User deownered'
continue
elif input_split[0] == 'user' and len(input_split) > 1:
setattr(api, 'user', input_split[1])
print 'User changed to {}'.format(input_split[1])
continue
elif input_split[0] == 'quit':
break
elif len(_input) > 0 and input_split[0][1:] in declares:
setattr(api, 'command', _input.split()[0][1:])
setattr(api, 'message', _input)
print callback(api)
continue
'''
########################### END: Interactive Testing ###########################
################################# END: Testing #################################