1
# -*- Encoding: UTF-8 -*-
3
# Copyright (c) 2010, Elián Hanisch
9
import supybot.conf as conf
10
import supybot.utils as utils
11
from supybot.commands import *
12
import supybot.plugins as plugins
13
import supybot.ircutils as ircutils
14
import supybot.callbacks as callbacks
15
from supybot.ircutils import bold
17
from storm.locals import *
18
from storm.sqlobject import AutoUnicode
21
from datetime import datetime
22
today = datetime.today
26
"""CREATE TABLE urls (\
27
id INTEGER PRIMARY KEY AUTOINCREMENT,\
28
channel TEXT COLLATE NOCASE,\
29
nick TEXT COLLATE NOCASE,\
30
url TEXT COLLATE NOCASE,\
32
UNIQUE (channel, url))
36
"""CREATE TABLE other_urls (\
37
id INTEGER PRIMARY KEY AUTOINCREMENT,\
38
channel TEXT COLLATE NOCASE,\
39
nick TEXT COLLATE NOCASE,\
40
url_id INTEGER references urls(id),\
46
__storm_table__ = 'urls'
48
id = Int(primary=True)
49
channel = AutoUnicode()
54
def __init__(self, url, nick, channel):
57
self.channel = channel
61
class OtherUrl(object):
62
__storm_table__ = 'other_urls'
64
id = Int(primary=True)
65
channel = AutoUnicode()
70
url = Reference(url_id, Url.id)
72
def __init__(self, nick, channel):
74
self.channel = channel
78
octet = r'(?:2(?:[0-4]\d|5[0-5])|1\d\d|\d{1,2})'
79
ipAddr = r'%s(?:\.%s){3}' % (octet, octet)
80
label = r'[0-9a-z][-0-9a-z]*[0-9a-z]?'
81
domain = r'%s(?:\.%s)*\.[a-z][-0-9a-z]*[a-z]?' % (label, label)
82
urlre = r'(\w+://(?:%s|%s)(?::\d+)?(?:/[^\])>\s]*)?)' % (domain, ipAddr)
83
urlre = re.compile(urlre, re.I)
87
path = conf.supybot.directories.data()
88
filename = os.path.join(path, 'Urldb.sqlite.db')
89
db = create_database('sqlite:%s' %filename)
93
store.execute(urls_schema)
94
store.execute(others_schema)
116
class Urldb(callbacks.Plugin):
117
"""Add the help for "@plugin help Urldb" here
118
This should describe *how* to use this plugin."""
124
def doPrivmsg(self, irc, msg):
125
message = msg.args[1]
126
#self.log.debug(message)
127
match = urlre.findall(message)
130
#self.log.debug('MATCH: %s' %match)
132
_url = store.find(Url, Url.url.like(url)).one()
134
other = OtherUrl(msg.nick, msg.args[0])
138
url = Url(url, msg.nick, msg.args[0])
143
class url(callbacks.Commands):
144
def search(self, irc, msg, args, channel, pattern, optlist):
145
"""[<channel>] <pattern> [--nick <pattern]
147
Search urls that matches *pattern*, use '*' or '?' for wildcards, '\\' for escapes."""
152
nick = makePattern(v)
154
if pattern[0] != '*' and pattern[-1] != '*':
155
pattern = '*%s*' %pattern
159
Url.url.like(makePattern(pattern), escape='\\'),
160
Url.channel.like(channel)]
163
expressions.append(Url.nick.like(nick, escape='\\'))
165
result = store.find(Url, And(*expressions))
166
result.order_by(Desc(Url.date))
168
if result.is_empty():
169
irc.reply("Nothing found with '%s' in %s." %(pattern, channel))
173
L.append("#%s %s by %s" %(url.id, bold(url.url), url.nick))
174
irc.reply('Found %s urls: %s' %(len(L), ', '.join(L)))
176
search = wrap(search, ['channel', 'something', getopts({'nick': 'something'})])
178
def info(self, irc, msg, args, channel, id):
179
"""[<channel>] <id|exact url>
181
Displays information about an url, like who and when it was posted.
182
Url's id must be an integer."""
186
url = store.find(Url, Url.id == id)
188
url = store.find(Url, Url.url == id, Url.channel.like(channel))
191
irc.reply("Nothing found.")
194
irc.reply('#%s %s posted by %s in %s on %s' %(url.id,
195
bold(url.url), bold(url.nick),
196
url.channel, url.date))
198
info = wrap(info, ['channel', 'text'])
200
def last(self, irc, msg, args, channel, nick):
201
"""[<channel>] [<nick>]
203
Displays the last url posted by <nick> or seen in channel."""
204
expressions = [ Url.channel.like(channel) ]
206
expressions.append(Url.nick.like(makePattern(nick)))
208
result = store.find(Url, And(*expressions))
209
result.order_by(Url.date)
211
if result.is_empty():
212
irc.reply("Nothing found.")
215
irc.reply("#%s %s" %(url.id, bold(url.url)))
217
last = wrap(last, ['channel', optional('something')])
223
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: