5
""" lists per global/channel/user """
7
__copyright__ = 'this file is in the public domain'
9
from gozerbot.commands import cmnds
10
from gozerbot.examples import examples
11
from gozerbot.plughelp import plughelp
12
from gozerbot.users import users
13
from gozerbot.persist import Persist
14
from gozerbot.datadir import datadir
15
from gozerbot.nextid import nextid
18
plughelp.add('lists', 'maintain lists')
20
# indx, username, listname, txt
21
lists = Persist(datadir + os.sep + 'newlists')
26
""" return number of lists """
27
return len(lists.data)
29
def getlists(username):
30
""" return lists of username """
32
return lists.data[username].keys()
36
def getlist(username, listname):
37
""" get list of username """
38
listname = listname.strip()
40
return lists.data[username][listname]
44
def addtolist(username, listname, item):
45
""" add item to list of username """
46
listname = listname.strip()
47
nid = nextid.next('lists')
48
if not lists.data.has_key(username):
49
lists.data[username] = {}
50
if not lists.data[username].has_key(listname):
51
lists.data[username][listname] = []
52
lists.data[username][listname].append((nid, item))
55
def mergelist(username, listname, itemlist):
56
""" merge new list into existing list """
57
listname = listname.strip()
58
nidlist = nextid.nextlist('lists', len(itemlist))
59
if not lists.data.has_key(username):
60
lists.data[username] = {}
61
if not lists.data[username].has_key(listname):
62
lists.data[username][listname] = []
66
lists.data[username][listname].append((nid, i[1]))
70
def delfromlist(username, listname, index):
71
""" remove item at index from list """
72
listname = listname.strip()
74
l = lists.data[username][listname]
77
for i in range(len(l)-1, -1, -1):
83
def handle_listsglobal(bot, ievent):
84
""" lists <listname> [',' <item>] .. global lists"""
86
ievent.missing("<listname> [',' <item>]")
89
listname, item = ievent.rest.split(',', 1)
91
l = getlist('all', ievent.rest)
93
ievent.reply('no %s list available or empty' % ievent.rest)
97
result.append("%s) %s" % (i[0], i[1]))
100
listname = listname.strip().lower()
102
if not listname or not item:
103
ievent.missing("<listname> [',' <item>]")
105
addtolist('all', listname, item)
106
ievent.reply('%s added to %s list' % (item, listname))
108
cmnds.add('lists-global', handle_listsglobal, 'USER')
109
examples.add('lists-global', "lists-global <listname> [',' <item>] .. show \
110
content of list or add item to list", '1) lists-global bla 2) lists-global \
113
def handle_listsglobaldel(bot, ievent):
114
""" list-globaldel <listname> ',' <listofnrs> .. remove items with \
115
indexnr from list """
117
ievent.reply('<listname> , <listofnrs>')
120
listname, nrs = ievent.rest.split(',', 1)
122
ievent.missing('<listname> , <listofnrs>')
127
nrslist.append(int(i))
129
ievent.reply('%s is not an integer' % i)
135
if not delfromlist('all', listname, i):
140
ievent.reply('failed to delete %s .. %s item(s) deleted' % \
141
(str(failed), itemsdeleted))
143
ievent.reply('%s item(s) removed' % itemsdeleted)
145
cmnds.add('lists-globaldel', handle_listsglobaldel, 'USER')
146
examples.add('lists-globaldel', "lists-globaldel <listname> ',' <listofnrs> \
147
.. remove items with indexnr from list", '1) lists-globaldel mekker , 1 2) \
148
lists-globaldel mekker , 0 3 6')
150
def handle_listsglobalshow(bot, ievent):
151
""" show avaiable global lists """
154
ievent.reply('no lists available')
157
ievent.reply("global lists: ", l, dot=True)
159
cmnds.add('lists-globalshow', handle_listsglobalshow, 'USER')
160
examples.add('lists-globalshow', 'show available global lists' , \
163
def handle_listschan(bot, ievent):
164
""" lists-chan <listname> [',' <item>] .. global lists"""
166
ievent.missing("<listname> [',' <item>]")
169
listname, item = ievent.rest.split(',', 1)
171
l = getlist(ievent.channel, ievent.rest)
173
ievent.reply('no %s list available or empty' % ievent.rest)
177
result.append("%s) %s" % (i[0], i[1]))
180
listname = listname.strip().lower()
182
if not listname or not item:
183
ievent.missing("<listname> [',' <item>]")
185
addtolist(ievent.channel, listname, item)
186
ievent.reply('%s added to %s list' % (item, listname))
188
cmnds.add('lists-chan', handle_listschan, 'USER')
189
examples.add('lists-chan', "lists-chan <listname> [',' <item>] .. show \
190
content of list or add item to list", '1) lists-chan bla 2) lists-chan \
193
def handle_listschandel(bot, ievent):
194
""" lists-chandel <listname> ',' <listofnrs> .. remove items with \
195
indexnr from list """
197
ievent.reply('<listname> , <listofnrs>')
200
listname, nrs = ievent.rest.split(',', 1)
202
ievent.missing('<listname> , <listofnrs>')
207
nrslist.append(int(i))
209
ievent.reply('%s is not an integer' % i)
215
if not delfromlist(ievent.channel, listname, i):
220
ievent.reply('failed to delete %s .. %s item(s) deleted' % \
221
(str(failed), itemsdeleted))
223
ievent.reply('%s item(s) removed' % itemsdeleted)
225
cmnds.add('lists-chandel', handle_listschandel, 'USER')
226
examples.add('lists-chandel', "lists-chandel <listname> ',' <listofnrs> \
227
.. remove items with indexnr from list", '1) lists-chandel mekker , 1 2) \
228
lists-chandel mekker , 0 3 6')
230
def handle_listschanshow(bot, ievent):
231
""" show avaiable lists """
232
l = getlists(ievent.channel)
234
ievent.reply('no lists available')
237
ievent.reply("channel lists: ", l, dot=True)
239
cmnds.add('lists-chanshow', handle_listschanshow, 'USER')
240
examples.add('lists-chanshow', 'show available channel lists' , \
243
def handle_lists(bot, ievent):
244
""" lists <listname> [',' <item>] .. global lists"""
246
ievent.missing("<listname> [',' <item>]")
248
username = users.getname(ievent.userhost)
250
listname, item = ievent.rest.split(',', 1)
252
l = getlist(username, ievent.rest)
254
ievent.reply('no %s list available or empty' % ievent.rest)
258
result.append("%s) %s" % (i[0], i[1]))
261
listname = listname.strip().lower()
263
if not listname or not item:
264
ievent.missing("<listname> [',' <item>]")
266
addtolist(username, listname, item)
267
ievent.reply('%s added to %s list' % (item, listname))
269
cmnds.add('lists', handle_lists, 'USER')
270
examples.add('lists', "lists <listname> [',' <item>] .. show content of list \
271
or add item to list", '1) lists bla 2) lists bla, mekker')
273
def handle_listsdel(bot, ievent):
274
""" lists-del <listname> ',' <listofnrs> .. remove items with indexnr \
277
ievent.missing('<listname> , <listofnrs>')
280
listname, nrs = ievent.rest.split(',', 1)
282
ievent.missing('<listname> , <listofnrs>')
286
for i in nrs.split():
287
nrslist.append(int(i))
289
ievent.reply('%s is not an integer' % i)
291
username = users.getname(ievent.userhost)
296
if not delfromlist(username, listname, i):
301
ievent.reply('failed to delete %s .. %s item(s) deleted' % \
302
(str(failed), itemsdeleted))
304
ievent.reply('%s item(s) removed' % itemsdeleted)
306
cmnds.add('lists-del', handle_listsdel, 'USER')
307
examples.add('lists-del', "lists-del <listname> ',' <listofnrs> \
308
.. remove items with indexnr from list", '1) lists-del mekker , 1 2) \
309
lists-del mekker , 0 3 6')
311
def handle_listshow(bot, ievent):
312
""" show avaiable lists """
313
username = users.getname(ievent.userhost)
314
l = getlists(username)
316
ievent.reply('no lists available')
319
ievent.reply("your lists: ", l, dot=True)
321
cmnds.add('lists-show', handle_listshow, 'USER')
322
examples.add('lists-show', 'show available channel lists' , 'lists-show')
324
def handle_listsmerge(bot, ievent):
325
""" merge two lists """
327
(fromlist, tolist) = ievent.args
329
ievent.missing('<fromlist> <tolist>')
331
username = users.getname(ievent.userhost)
332
l = getlist(username, fromlist)
334
ievent.reply('no %s list exists' % fromlist)
337
mergelist(username, tolist, l)
338
except Exception, ex:
339
ievent.reply('error: %s' % str(ex))
341
ievent.reply('%s items merged' % len(l))
343
cmnds.add('lists-merge', handle_listsmerge, 'USER')
344
examples.add('lists-merge', 'merge 2 lists (users lists only)', \
345
'lists-merge mekker miep')