-
Notifications
You must be signed in to change notification settings - Fork 6
/
gcibot.py
164 lines (129 loc) · 5.09 KB
/
gcibot.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# GCI bot.
# Parse tasks data and show info about them
# Copyright (C) Ignacio Rodríguez <[email protected]>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
from commands import Commands
from task_data import TaskFinder
import logging
import data
import sys
import re
import requests
import bs4
from twisted.internet import reactor, protocol
from twisted.words.protocols import irc
logging.basicConfig(level=logging.DEBUG)
welcome_back = []
welcome_back_enabled = False
class GCIBot(irc.IRCClient):
nickname = data.nickname
username = data.username
password = data.password
channels = []
def __init__(self):
self.commands = Commands(self)
self.tasks_finder = TaskFinder(self)
def connectionMade(self):
irc.IRCClient.connectionMade(self)
self.commands.register(True)
def connectionLost(self, reason):
irc.IRCClient.connectionLost(self, reason)
def joined(self, channel):
if channel not in self.channels:
self.channels.append(channel)
def left(self, channel):
if channel in self.channels:
self.channels.remove(channel)
def signedOn(self):
for c in self.factory.channels:
self.join(c)
def privmsg(self, user, channel, msg):
human_user = user.split('!', 1)[0]
if human_user in self.commands.ignored_users or human_user == self.nickname:
return
tasks = []
try:
result = self.commands.process_msg(msg, channel, user)
if result:
tasks = self.tasks_finder.process_msg(msg, channel, user)
for task in tasks:
self.msg(channel, task)
self.check_memo(user, channel)
except BaseException:
pass
if len(tasks):
return
urls = re.findall(
'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',
msg)
try:
for url in urls:
r = requests.get(url)
html = bs4.BeautifulSoup(r.text)
title = html.title.text.replace("\n", "")
self.msg(channel, "[ %s ]" % title.encode("utf-8"))
except Exception as error:
pass
def userJoined(self, user, channel):
human_user = user.split('!', 1)[0]
self.check_memo(user, channel)
if not welcome_back_enabled:
return
if "sugar" in str(channel) and not human_user.lower(
) in self.commands.ignored_users:
if human_user in welcome_back:
self.msg(channel, "Hi %s, welcome back" % human_user)
else:
self.msg(channel, "Hi %s, welcome to #sugar" % human_user)
welcome_back.append(human_user)
def check_memo(self, user, channel):
human_user = user.split('!', 1)[0].lower()
msgs_to_remove = []
for msg in self.commands.pending_msgs:
if msg[1].lower() == human_user:
chan = msg[0]
if chan == self.nickname:
self.msg(
human_user,
"Message from '%s': %s" % (msg[2],
msg[3]))
else:
self.msg(
human_user,
"Message from '%s' at '%s (UTC-3)' in channel '%s': %s" % (msg[2],
msg[4],
chan,
msg[3]))
msgs_to_remove.append(msg)
for msg in msgs_to_remove:
self.commands.pending_msgs.remove(msg)
class BotFactory(protocol.ClientFactory):
def __init__(self, channels):
self.channels = channels
def buildProtocol(self, addr):
p = GCIBot()
p.factory = self
return p
def clientConnectionLost(self, connector, reason):
connector.connect()
def clientConnectionFailed(self, connector, reason):
logging.error("** Conection failed ** ")
reactor.stop()
if __name__ == '__main__':
logging.info('** Starting GCIBot **')
f = BotFactory(sys.argv[1:])
reactor.connectTCP(data.server, data.port, f)
logging.info('** Connected to server **')
logging.info('** Channels: %s **' % ", ".join(sys.argv[1:]))
reactor.run()