6
""" lists per global/channel/user """
8
__copyright__ = 'this file is in the public domain'
10
from gozerbot.generic import handle_exception
11
from gozerbot.commands import cmnds
12
from gozerbot.examples import examples
13
from gozerbot.plughelp import plughelp
14
from gozerbot.users import users
15
from gozerbot.db import db
17
plughelp.add('lists', 'maintain lists')
20
""" return number of lists """
21
result = db.execute(""" SELECT COUNT(*) FROM list """)
24
def getlists(username):
25
""" get all lists of user """
26
result = db.execute(""" SELECT * FROM list WHERE username = %s """, \
30
def getlist(username, listname):
31
""" get list of user """
32
result = db.execute(""" SELECT * FROM list WHERE username = %s AND \
33
listname = %s """, (username, listname))
36
def addtolist(username, listname, item):
37
""" add item to list """
38
result = db.execute(""" INSERT INTO list(username, listname, item) \
39
VALUES(%s, %s, %s) """, (username, listname, item))
42
def delfromlist(username, indx):
43
""" delete item from list """
44
result = db.execute(""" DELETE FROM list WHERE username = %s AND indx = %s \
45
""", (username, indx))
48
def mergelist(username, listname, l):
51
addtolist(username, listname, i)
53
def handle_listsglobal(bot, ievent):
54
""" lists <listname> [',' <item>] .. global lists"""
56
ievent.missing("<listname> [',' <item>]")
59
listname, item = ievent.rest.split(',', 1)
61
l = getlist('all', ievent.rest)
63
ievent.reply('no %s list available' % ievent.rest)
67
result.append("%s) %s" % (i[0], i[3]))
70
listname = listname.strip().lower()
72
if not listname or not item:
73
ievent.missing("<listname> [',' <item>]")
77
result = addtolist('all', listname, item)
80
ievent.reply('ERROR: %s' % str(ex))
83
ievent.reply('%s added to %s list' % (item, listname))
85
ievent.reply('add failed')
87
cmnds.add('lists-global', handle_listsglobal, 'USER')
88
examples.add('lists-global', "lists-global <listname> [',' <item>] .. show \
89
content of list or add item to list", '1) lists-global bla 2) lists-global \
92
def handle_listsglobaldel(bot, ievent):
93
""" list-globaldel <listname> ',' <listofnrs> .. remove items with \
96
ievent.missing('<listofnrs>')
100
for i in ievent.rest.split():
103
ievent.reply('%s is not an integer' % i)
110
result = delfromlist('all', i)
115
except Exception, ex:
117
ievent.reply('ERROR: %s' % str(ex))
120
ievent.reply('failed to delete %s' % ' '.join(failed))
121
ievent.reply('%s item(s) removed' % itemsdeleted)
123
cmnds.add('lists-globaldel', handle_listsglobaldel, 'USER')
124
examples.add('lists-globaldel', "lists-globaldel <listname> ',' <listofnrs> \
125
.. remove items with indexnr from list", '1) lists-globaldel mekker , 1 2) \
126
lists-globaldel mekker , 0 3 6')
128
def handle_listsglobalshow(bot, ievent):
129
""" show avaiable global lists """
132
ievent.reply('no lists available')
137
if not i[2] in result:
140
ievent.reply(result, dot=True)
142
cmnds.add('lists-globalshow', handle_listsglobalshow, 'USER')
143
examples.add('lists-globalshow', 'show available global lists' , \
146
def handle_listschan(bot, ievent):
147
""" lists-chan <listname> [',' <item>] .. global lists"""
149
ievent.missing("<listname> [',' <item>]")
152
listname, item = ievent.rest.split(',', 1)
154
l = getlist(ievent.channel, ievent.rest)
156
ievent.reply('no %s list available' % ievent.rest)
160
result.append("%s) %s" % (i[0], i[3]))
163
listname = listname.strip().lower()
165
if not listname or not item:
166
ievent.missing("<listname> [',' <item>]")
170
ok = addtolist(ievent.channel, listname, item)
171
except Exception, ex:
173
ievent.reply('ERROR: %s' % str(ex))
176
ievent.reply('%s added to %s list' % (item, listname))
178
ievent.reply('add failed')
180
cmnds.add('lists-chan', handle_listschan, 'USER')
181
examples.add('lists-chan', "lists-chan <listname> [',' <item>] .. show \
182
content of list or add item to list", '1) lists-chan bla 2) lists-chan \
185
def handle_listschandel(bot, ievent):
186
""" lists-chandel <listname> ',' <listofnrs> .. remove items with \
187
indexnr from list """
189
ievent.missing('<listofnrs>')
193
for i in ievent.rest.split():
196
ievent.reply('%s is not an integer' % i)
203
result = delfromlist(ievent.channel, i)
208
except Exception, ex:
210
ievent.reply('ERROR: %s' % str(ex))
213
ievent.reply('failed to remove %s' % ' '.join(failed))
214
ievent.reply('%s item(s) removed' % itemsdeleted)
216
cmnds.add('lists-chandel', handle_listschandel, 'USER')
217
examples.add('lists-chandel', "lists-chandel <listname> ',' <listofnrs> \
218
.. remove items with indexnr from list", '1) lists-chandel mekker , 1 2) \
219
lists-chandel mekker , 0 3 6')
221
def handle_listschanshow(bot, ievent):
222
""" show avaiable lists """
223
l = getlists(ievent.channel)
225
ievent.reply('no lists available')
230
if not i[2] in result:
233
ievent.reply(result, dot=True)
235
cmnds.add('lists-chanshow', handle_listschanshow, 'USER')
236
examples.add('lists-chanshow', 'show available channel lists' , \
239
def handle_lists(bot, ievent):
240
""" lists <listname> [',' <item>] .. global lists"""
242
ievent.missing("<listname> [',' <item>]")
244
username = users.getname(ievent.userhost)
246
listname, item = ievent.rest.split(',', 1)
248
l = getlist(username, ievent.rest)
250
ievent.reply('no %s list available' % ievent.rest)
254
result.append("%s) %s" % (i[0], i[3]))
257
listname = listname.strip().lower()
259
if not listname or not item:
260
ievent.missing("<listname> [',' <item>]")
264
ok = addtolist(username, listname, item)
265
except Exception, ex:
267
ievent.reply('ERROR: %s' % str(ex))
270
ievent.reply('%s added to %s list' % (item, listname))
272
ievent.reply('add failed')
274
cmnds.add('lists', handle_lists, 'USER')
275
examples.add('lists', "lists <listname> [',' <item>] .. show content of list \
276
or add item to list", '1) lists bla 2) lists bla, mekker')
278
def handle_listsdel(bot, ievent):
279
""" lists-del <listname> ',' <listofnrs> .. remove items with indexnr \
282
ievent.missing('<listofnrs>')
286
for i in ievent.rest.split():
289
ievent.reply('%s is not an integer' % i)
291
username = users.getname(ievent.userhost)
297
result = delfromlist(username, i)
302
except Exception, ex:
304
ievent.reply('ERROR: %s' % str(ex))
307
ievent.reply('failed to delete %s' % ' '.join(failed))
308
ievent.reply('%s item(s) removed' % itemsdeleted)
310
cmnds.add('lists-del', handle_listsdel, 'USER')
311
examples.add('lists-del', "lists-del <listname> ',' <listofnrs> \
312
.. remove items with indexnr from list", '1) lists-del mekker , 1 2) \
313
lists-del mekker , 0 3 6')
315
def handle_listshow(bot, ievent):
316
""" show avaiable lists """
317
username = users.getname(ievent.userhost)
318
l = getlists(username)
320
ievent.reply('no lists available')
325
if not i[2] in result:
328
ievent.reply(result, dot=True)
330
cmnds.add('lists-show', handle_listshow, 'USER')
331
examples.add('lists-show', 'show available channel lists' , 'lists-show')
333
def handle_listsmerge(bot, ievent):
334
""" merge 2 lists """
336
(fromlist, tolist) = ievent.args
338
ievent.missing('<fromlist> <tolist>')
340
username = users.getname(ievent.userhost)
341
res = getlist(username, fromlist)
343
ievent.reply('no %s list available or empty' % fromlist)
350
result = mergelist(username, tolist, l)
351
except Exception, ex:
353
ievent.reply('ERROR: %s' % str(ex))
355
ievent.reply('%s items merged' % result)
357
cmnds.add('lists-merge', handle_listsmerge, 'USER')
358
examples.add('lists-merge', 'lists-merge <fromlist> <tolist> .. merge 2 \
359
lists', 'lists-merge mekker miep')