Skip to content

Commit f2004f2

Browse files
committed
Replaced twisted with new IRC backend.
1 parent a348eef commit f2004f2

File tree

5 files changed

+95
-36
lines changed

5 files changed

+95
-36
lines changed

botmily/bot.py

+15-18
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,27 @@
22
from __future__ import print_function
33
from __future__ import unicode_literals
44

5+
import asyncore
56
import pkgutil
67
import re
8+
import socket
79
import sys
810

9-
from twisted.words.protocols import irc
10-
1111
from botmily import config
12+
from botmily import irc
1213
import plugins
1314

14-
def splituser(user):
15-
nick, remainder = user.split('!', 1)
16-
ident, host = remainder.split('@', 1)
17-
return nick, ident, host
18-
19-
class Bot(irc.IRCClient):
15+
class bot():
2016
def __init__(self):
17+
self.server = config.server
2118
self.nickname = config.name
2219
self.realname = b"Botmily https://github.com/kgc/botmily"
2320
self.channels = config.channels
2421

22+
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
23+
self.socket.connect((self.server, 6667))
24+
self.irc = irc.irc_handler(self.socket, self)
25+
2526
print("Initializing plugins...")
2627
self.commands = {}
2728
self.triggers = []
@@ -31,21 +32,17 @@ def __init__(self):
3132
self.commands.update(plugin.commands)
3233
self.triggers.extend(plugin.triggers)
3334

34-
def signedOn(self):
35-
print("Signed on to the IRC server")
36-
for channel in self.channels:
37-
self.join(str(channel))
35+
asyncore.loop()
3836

39-
def joined(self, channel):
37+
def join(self, channel):
4038
print("Joined channel " + channel)
4139

42-
def privmsg(self, user, channel, message):
43-
nick, ident, host = splituser(user)
40+
def privmsg(self, nick, user, host, channel, message):
4441
message_data = {"nick": nick,
4542
"user": user,
4643
"host": host,
4744
"channel": channel,
48-
"message": unicode(message, encoding='utf-8')}
45+
"message": message}
4946
command_match = re.match("\.([^ ]+) ?(.*)", message_data["message"])
5047
if command_match is not None:
5148
sent_command = command_match.group(1)
@@ -77,7 +74,7 @@ def say(self, nick, channel, output):
7774
if output is None:
7875
return
7976
if self.nickname == channel:
80-
self.msg(str(nick), output.encode("utf-8"))
77+
self.irc.privmsg(nick, output)
8178
else:
82-
self.msg(channel, str(nick) + str(": ") + output.encode("utf-8"))
79+
self.irc.privmsg(channel, nick + ": " + output)
8380

botmily/irc.py

+76
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from __future__ import print_function
33
from __future__ import unicode_literals
44

5+
import asynchat
6+
import time
57
import unicodedata
68

79
controls = {'bold': '\u0002',
@@ -37,3 +39,77 @@ def bold(message):
3739

3840
def color(message, color):
3941
return controls['color'] + colors[color] + message + controls['clear']
42+
43+
def split_prefix(prefix):
44+
nick, remainder = prefix[1:].split(b"!", 1)
45+
user, host = remainder.split(b"@", 1)
46+
return nick, user, host
47+
48+
class irc_handler(asynchat.async_chat):
49+
def __init__(self, sock, bot):
50+
asynchat.async_chat.__init__(self, sock=sock)
51+
self.ibuffer = b""
52+
self.set_terminator(b"\r\n")
53+
self.bot = bot
54+
self.push(b"NICK botmily\r\n")
55+
self.push(b"USER botmily 0 0 :Botmily\r\n")
56+
57+
def collect_incoming_data(self, data):
58+
self.ibuffer += data
59+
60+
def found_terminator(self):
61+
current_line = self.ibuffer
62+
self.ibuffer = b""
63+
prefix = None
64+
if current_line[0] == b":":
65+
prefix, null, current_line = current_line.partition(b" ")
66+
command, null, params = current_line.partition(b" ")
67+
if b":" in params:
68+
other_params, null, trailing = params.partition(b":")
69+
if len(other_params) == 0:
70+
params = [trailing]
71+
else:
72+
params = other_params.split(" ") + [trailing]
73+
else:
74+
params = params.split(" ")
75+
if command[0] >= b"0" and command[0] <= b"9":
76+
if command == b"001":
77+
for channel in self.bot.channels:
78+
self.join(str(channel))
79+
else:
80+
try:
81+
handler = getattr(self, "raw_" + command)
82+
except AttributeError:
83+
return
84+
handler(prefix, params)
85+
86+
def join(self, channel):
87+
self.push(b"JOIN " + channel + b"\r\n")
88+
89+
def part(self, channel):
90+
self.push(b"PART " + channel + b"\r\n")
91+
92+
def kick(self, channel, user):
93+
self.push(b"KICK " + str(channel) + b" " + str(user) + b"\r\n")
94+
95+
def privmsg(self, receiver, message):
96+
self.push(b"PRIVMSG " + str(receiver) + b" :" + message.encode("utf-8") + b"\r\n")
97+
98+
def notice(self, receiver, message):
99+
self.push(b"NOTICE " + str(receiver) + b" :" + message.encode("utf-8") + b"\r\n")
100+
101+
def pong(self, message):
102+
self.push(b"PONG " + message + b"\r\n")
103+
104+
def raw_PRIVMSG(self, prefix, params):
105+
nick, user, host = split_prefix(prefix)
106+
channel = params[0]
107+
message = params[-1].decode("utf-8")
108+
self.bot.privmsg(nick, user, host, channel, message)
109+
110+
def raw_PING(self, prefix, params):
111+
self.pong(params[0])
112+
113+
def raw_JOIN(self, prefix, params):
114+
self.bot.join(params[0])
115+

main.py

+2-16
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,13 @@
22
from __future__ import print_function
33
from __future__ import unicode_literals
44

5-
from twisted.internet import protocol, reactor
6-
75
from botmily import bot
86
from botmily import config
97
from botmily import db
108

11-
class BotFactory(protocol.ClientFactory):
12-
def __init__(self):
13-
print("Creating the BotFactory")
14-
15-
def buildProtocol(self, addr):
16-
print("Connected")
17-
p = bot.Bot()
18-
p.factory = self
19-
return p
20-
219
if __name__ == '__main__':
2210
print("Starting the bot")
2311
config.getConfig()
2412
db.connect()
25-
f = BotFactory()
26-
print("Connecting...")
27-
reactor.connectTCP(config.server, 6667, f)
28-
reactor.run()
13+
bot.bot()
14+

plugins/privilege.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from botmily import irc
88

99
def privilege(message_data, bot):
10-
bot.kick(str(message_data["channel"]), str(message_data["nick"]));
10+
bot.irc.kick(message_data["channel"], message_data["nick"]);
1111
return 'out'
1212

1313
commands = {}

plugins/tell.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def set_tell(message_data, bot):
2020
def get_tell(message_data, bot):
2121
row = db.execute("select nick_to, nick_from, message, time, channel from tells where nick_to=:nick_to", {"nick_to": message_data["nick"].lower()}).fetchone()
2222
if row:
23-
bot.notice(str(message_data["nick"]), str(row[1]) + str(" said ") + str(datetime.timedelta(seconds=(time.time() - row[3]))) + str(" ago in ") + str(row[4]) + str(" the following: ") + row[2].encode('utf-8'))
23+
bot.irc.notice(message_data["nick"], row[1] + " said " + str(datetime.timedelta(seconds=(time.time() - row[3]))) + " ago in " + row[4] + " the following: " + row[2])
2424
db.execute("delete from tells where nick_to=:nick_to and nick_from=:nick_from and message=:message and time=:time and channel=:channel", {"nick_to": row[0], "nick_from": row[1], "message": row[2], "time": row[3], "channel": row[4]})
2525
db.commit()
2626

0 commit comments

Comments
 (0)