summaryrefslogtreecommitdiff
path: root/modules/quotes.py
blob: 30a2c258e1521e7f0ec21558e010205588f97c5a (plain)
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
info = {
	'author': 'Jon Bergli Heier',
	'title': 'IRC Quotes',
	'description': 'Allows users to access a quote database.',
}

from pyPgSQL import PgSQL

class Quotes:
	def __init__(self, db = None):
		self.randseed = False
		self.connect(db)

	def format_quote(self, q):
		return q

	def connect(self, db):
		self.db = PgSQL.connect()
		self.updatecount()

	def updatecount(self):
		cur = self.db.cursor()
		cur.execute('select count(id) from quotes.quotes')
		self.count = int(cur.fetchone()[0])
		cur.close()

	def add(self, nick, quote):
		cur = self.db.cursor()
		cur.execute('insert into quotes.quotes (nick, date, quote) values (%s, %s, %s)', (nick, int(time.time()), quote))
		cur.execute('select currval(\'quotes.quotes_id_seq\')')
		lastrowid = cur.fetchone()[0]
		cur.close()
		self.db.commit()
		return lastrowid

	def delete(self, qid = None):
		cur = self.db.cursor()
		cur.execute('delete from quotes.quotes where id = %s limit 1', (qid,))
		cur.close()
		self.db.commit()

	def find(self, search, func = None):
		search = re.compile(search) if func == 'rfind' else search.lower() if func == 'efind' else [x.lower() for x in search.split(' ')]
		dosearch = lambda quote: search.search(quote) != None if func == 'rfind' else search in quote if func == 'efind' else len([x for x in search if x in quote]) if func == 'afind' else len([x for x in search if x in quote]) == len(search)
		cur = self.db.cursor()
		cur.execute('select id, quote from quotes.quotes order by id asc')
		ret = []
		i = 0
		for q in cur.fetchall():
			i += 1
			if dosearch(q[1].lower()):
				ret.append((q[0], self.format_quote(q[1])))
		cur.close()
		return ret

	def get(self, qid = None):
		cur = self.db.cursor()

class IRCHandler(Quotes):
	def add(self, *args):
		'Add a quote to the database.'
		quote = ' '.join(args)
		qid = Quotes.add(self, self.nick, quote)
		yield '\002Quote #%d\002 has been added' % (qid)

	def format_quote(self, quote):
		return quote

	def output(self, quotes):
		for n, q in quotes:
			yield '\002Quote #%d:\002 %s' % (n, q)

	def _find(self, args, func = 'find'):
		if not len(args):
			yield 'Quote: Missing search pattern.'
			return
		args = ' '.join(args)
		results = Quotes.find(self, args, func)
		leftovers = []
		if len(results) > 1:
			yield 'Quote: Found \002%d\002 results for "%s"%s' % (len(results), args, ', here are the last two:' if len(results) > 2 else ':' if len(results) else '')
		if len(results):
			if len(results) > 2:
				leftovers, results = results[:-2], results[-2:]
			for l in self.output(results):
				yield l
			if leftovers:
				yield 'Other quotes: %s' % ', '.join(['#%d' % x[0] for x in leftovers])

	def find(self, *args):
		'Find quotes containing all given words.'
		return self._find(args)

	def afind(self, *args):
		'Find quotes containing any given word.'
		return self._find(args, 'afind')

	def efind(self, *args):
		'Find quotes containing the exact given phrase.'
		return self._find(args, 'efind')

	def rfind(self, *args):
		'Find quotes using regexp search.'
		return self._find(args, 'rfind')

	def get(self, *args):
		'Print a quote.'
		if not len(args):

quote_handler = IRCHandler()

class Module:
	def __init__(self, bot):
		self.irc = bot

	def __call__(self, nick, channel, msg):
		if not msg.startswith('!quote'):
			return
		args = msg.split(' ')
		if msg.startswith('!quote'):
			args = args[1:]
		cmd = args[0] if len(args) and len(args[0].strip()) else 'random'
		args = args[1:]
	
		if cmd.isdigit() or (cmd[0] == '#' and cmd[1:].isdigit()):
			cmd, args = 'get', [cmd]
	
		quote_handler.nick = nick.split('!')[0]
		if hasattr(quote_handler, cmd) and callable(getattr(quote_handler, cmd)):
			for line in getattr(quote_handler, cmd)(*args):
				self.irc.msg(channel if not channel == self.irc.nickname else nick.split('!')[0], line)
		else:
			self.irc.msg(channel if not channel == self.irc.nickname else nick.split('!')[0], '%s, invalid command "%s"' % (nick.split('!')[0], cmd))