-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsugar-irc-bot.py
176 lines (134 loc) · 5.41 KB
/
sugar-irc-bot.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
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# IRC Bot for help users in #sugar channels (Freenode)
# Copyright (C) 2014, Ignacio Rodríguez <[email protected]>
# Copyright (C) 2014, Sam Parkinson <[email protected]>
# Copyright (C) 2014, Sai Vineet <[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/>.
print '\nSugarbot is loading\n'
import os
import getpass
import signal
import sys
import re
from datetime import datetime
from subprocess import call, Popen
from twisted.internet import reactor, protocol
from twisted.words.protocols import irc
from msg_scan import scan_msg, they_know_now, they_dont_know
AUTHORS = "Sam Parkinson, Sai Vineet, and Ignacio Rodriguez"
BOT_INFO_TXT = ("Hi! I'm a bot by {authors} that's here to help. "
"You can find my code here: "
"https://github.com/ignaciouy/sugar-irc")
BOT_INFO_TXT = BOT_INFO_TXT.format(authors=AUTHORS)
BOT_HELP_TXT = (": Help is on my wiki: "
"https://github.com/ignaciouy/sugar-irc/wiki/SugarBot-Help")
BOT_VERSION = "Started at " + str(datetime.now())
# The sugar channel bots, or ignored, or me! Don't talk with it
IGNORED_BOTS = ["meeting", "soakbot", "gcibot", "github",
"sbbot", "sugarbot-git", "sugarbot"]
def reload_bot():
reactor.stop()
call(['git', 'pull'])
class SugarIRCBOT(irc.IRCClient):
nickname = "sugarbot"
realname = "Sugar Labs help bot"
username = "sugarbot"
def __init__(self, p):
self.password = p
def connectionMade(self):
irc.IRCClient.connectionMade(self)
def connectionLost(self, reason):
irc.IRCClient.connectionLost(self, reason)
def signedOn(self):
self.join("sugar")
def joined(self, channel):
self.msg(channel, BOT_INFO_TXT)
def privmsg(self, user, channel, msg):
addressed = False
if msg.startswith(self.nickname):
msg = msg[len(self.nickname)+1:]
addressed = True
msg.strip()
msg = msg.lower()
nice_user = user.split('!')[0]
# Restart the bot if the user is sugarbot-git
# We need to find a elegant way.
if "sugarbot-git" in nice_user and "pushed" in msg:
reload_bot()
for ignored in IGNORED_BOTS:
if ignored in nice_user.lower():
# Just talking with bot :(
return
help_msgs = scan_msg(msg, nice_user)
if help_msgs:
for help_msg in help_msgs:
self.msg(channel, nice_user + ', ' + str(help_msg))
return
if ('i know' in msg or 'no spam for me' in msg) and addressed:
they_know_now(nice_user)
self.msg(channel, 'I now count %s as smart' % nice_user)
return
msg_match = ('spam me' in msg or 'i don\'t know' in msg or
'i dont know' in msg)
if msg_match and addressed:
they_dont_know(nice_user)
self.msg(channel, nice_user + ", you will now be help spammed")
return
re_result = re.search("([\S]{1,9999}) knows", msg)
if re_result and addressed:
they_know_now(re_result.groups()[0])
self.msg(channel, 'I now count %s as smart' %
re_result.groups()[0])
return
if 'help' in msg and addressed:
self.msg(channel, nice_user + BOT_HELP_TXT)
return
if ('info' in msg or 'hi' in msg) and addressed:
self.msg(channel, nice_user + ", " + BOT_INFO_TXT)
return
if 'ping' in msg and addressed:
self.msg(channel, nice_user + ', pong')
return
if 'version' in msg and addressed:
self.msg(channel, nice_user + ': ' + BOT_VERSION)
return
if 'freetime' in msg and addressed:
self.msg(channel, "gcibot, I love you. We should go out some time")
return
class BotFactory(protocol.ClientFactory):
def __init__(self, p):
self.password = p
def buildProtocol(self, addr):
p = SugarIRCBOT(self.password)
p.factory = self
return p
def clientConnectionLost(self, connector, reason):
connector.connect()
def clientConnectionFailed(self, connector, reason):
reactor.stop()
if __name__ == '__main__':
# Ask password (for sugarbot account) to Ignacio or Sam
# If password already exists: this use the first typed password.
# Btw: You can use: python sugar-irc-bot.py password
if len(sys.argv) == 1:
password = getpass.getpass("irc password: ")
else:
password = sys.argv[1]
f = BotFactory(password)
reactor.connectTCP("rajaniemi.freenode.net", 6667, f)
reactor.run()
# This is called after reactor.stop()
call(['python', 'sugar-irc-bot.py', password])