forked from DesertBot/DesertBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
URLFollow.py
102 lines (79 loc) · 3.71 KB
/
URLFollow.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
"""
Created on Jan 27, 2013
@author: StarlitGhost
"""
from twisted.plugin import IPlugin
from desertbot.moduleinterface import IModule
from desertbot.modules.commandinterface import BotCommand
from zope.interface import implementer
from urllib.parse import urlparse
import re
from desertbot.message import IRCMessage
from desertbot.response import IRCResponse, ResponseType
@implementer(IPlugin, IModule)
class URLFollow(BotCommand):
def actions(self):
return super(URLFollow, self).actions() + [('action-channel', 1, self.handleURL),
('action-user', 1, self.handleURL),
('message-channel', 1, self.handleURL),
('message-user', 1, self.handleURL),
('urlfollow', 1, self.dispatchToFollows)]
def triggers(self):
return ['urlfollow', 'follow']
def help(self, query):
return ('Automatic module that follows urls '
'and grabs information about the resultant webpage')
def onLoad(self):
self.autoFollow = True
def execute(self, message: IRCMessage):
if message.parameterList[0].lower() == 'on':
self.autoFollow = True
return IRCResponse(ResponseType.Say, 'Auto-follow on', message.replyTo)
if message.parameterList[0].lower() == 'off':
self.autoFollow = False
return IRCResponse(ResponseType.Say, 'Auto-follow off', message.replyTo)
return self.handleURL(message, auto=False)
def handleURL(self, message: IRCMessage, auto: bool=True):
if auto and message.command:
return
if auto and not self.autoFollow:
return
if auto and self.checkIgnoreList(message):
return
match = re.search(r'(?P<url>(https?://|www\.)[^\s]+)', message.messageString, re.IGNORECASE)
if not match:
if not auto:
return IRCResponse(ResponseType.Say,
'[no url recognized]',
message.replyTo,
{'urlfollowURL': '[no url recognized]'})
return
url = match.group('url')
follows = self.bot.moduleHandler.runActionUntilValue('urlfollow', message, url)
if not follows:
if not auto:
return IRCResponse(ResponseType.Say,
'[no follows worked for {}]'.format(url),
message.replyTo,
{'urlfollowURL': '[no follows worked for {}]'})
return
text, url = follows
return IRCResponse(ResponseType.Say, text, message.replyTo, {'urlfollowURL': url})
def dispatchToFollows(self, message: IRCMessage, url: str):
if not re.search('\.(jpe?g|gif|png|bmp)$', url):
return self.FollowStandard(message, url)
def FollowStandard(self, message: IRCMessage, url: str):
response = self.bot.moduleHandler.runActionUntilValue('fetch-url', url)
if not response:
return
if response.url != url:
return self.bot.moduleHandler.runActionUntilValue('urlfollow', message, response.url)
short = self.bot.moduleHandler.runActionUntilValue('shorten-url', url)
title = self.bot.moduleHandler.runActionUntilValue('get-html-title', response.content)
if title is not None:
domain = urlparse(response.url).netloc
return ('{title} (at {domain}) {short}'
.format(title=title, domain=domain, short=short),
url)
return
urlfollow = URLFollow()