2012-08-14 03:51:55 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Nemubot is a modulable IRC bot, built around XML configuration files.
|
|
|
|
# Copyright (C) 2012 Mercier Pierre-Olivier
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2012-04-09 02:19:39 +00:00
|
|
|
import socket
|
2012-07-22 23:23:04 +00:00
|
|
|
import sys
|
2012-05-21 10:21:14 +00:00
|
|
|
import threading
|
2012-07-22 23:23:04 +00:00
|
|
|
import traceback
|
2012-04-09 02:19:39 +00:00
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
import channel
|
|
|
|
import DCC
|
|
|
|
import message
|
|
|
|
import xmlparser
|
2012-04-09 02:19:39 +00:00
|
|
|
|
2012-05-21 10:21:14 +00:00
|
|
|
class Server(threading.Thread):
|
2012-06-23 11:38:34 +00:00
|
|
|
def __init__(self, node, nick, owner, realname, socket = None):
|
2012-05-21 10:21:14 +00:00
|
|
|
self.stop = False
|
|
|
|
self.stopping = threading.Event()
|
2012-04-09 02:19:39 +00:00
|
|
|
self.nick = nick
|
|
|
|
self.owner = owner
|
|
|
|
self.realname = realname
|
2012-06-23 11:38:34 +00:00
|
|
|
self.s = socket
|
|
|
|
self.connected = self.s is not None
|
|
|
|
self.node = node
|
2012-04-09 02:19:39 +00:00
|
|
|
|
2012-04-30 16:22:10 +00:00
|
|
|
self.listen_nick = True
|
|
|
|
|
2012-07-03 11:00:16 +00:00
|
|
|
self.dcc_clients = dict()
|
|
|
|
|
2012-06-23 13:14:36 +00:00
|
|
|
self.channels = dict()
|
|
|
|
for chn in node.getNodes("channel"):
|
2012-07-23 17:20:46 +00:00
|
|
|
chan = channel.Channel(chn, self)
|
2012-06-23 13:14:36 +00:00
|
|
|
self.channels[chan.name] = chan
|
2012-04-09 02:19:39 +00:00
|
|
|
|
2012-05-21 10:21:14 +00:00
|
|
|
threading.Thread.__init__(self)
|
|
|
|
|
2012-07-22 23:23:04 +00:00
|
|
|
def isDCC(self, to=None):
|
|
|
|
return to is not None and to in self.dcc_clients
|
2012-07-03 11:00:16 +00:00
|
|
|
|
2012-06-23 11:38:34 +00:00
|
|
|
@property
|
|
|
|
def host(self):
|
|
|
|
if self.node.hasAttribute("server"):
|
|
|
|
return self.node["server"]
|
|
|
|
else:
|
|
|
|
return "localhost"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def port(self):
|
|
|
|
if self.node.hasAttribute("port"):
|
|
|
|
return self.node.getInt("port")
|
|
|
|
else:
|
|
|
|
return "6667"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def password(self):
|
|
|
|
if self.node.hasAttribute("password"):
|
|
|
|
return self.node["password"]
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def partner(self):
|
|
|
|
if self.node.hasAttribute("partner"):
|
|
|
|
return self.node["partner"]
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2012-07-22 23:23:04 +00:00
|
|
|
@property
|
|
|
|
def ip(self):
|
2012-07-23 17:20:46 +00:00
|
|
|
"""Convert common IP representation to little-endian integer representation"""
|
2012-07-22 23:23:04 +00:00
|
|
|
sum = 0
|
|
|
|
if self.node.hasAttribute("ip"):
|
|
|
|
for b in self.node["ip"].split("."):
|
|
|
|
sum = 256 * sum + int(b)
|
|
|
|
else:
|
|
|
|
#TODO: find the external IP
|
|
|
|
pass
|
|
|
|
return sum
|
|
|
|
|
|
|
|
def toIP(self, input):
|
|
|
|
"""Convert little-endian int to IPv4 adress"""
|
|
|
|
ip = ""
|
|
|
|
for i in range(0,4):
|
|
|
|
mod = input % 256
|
|
|
|
ip = "%d.%s" % (mod, ip)
|
|
|
|
input = (input - mod) / 256
|
|
|
|
return ip[:len(ip) - 1]
|
|
|
|
|
2012-06-28 23:02:15 +00:00
|
|
|
@property
|
|
|
|
def autoconnect(self):
|
|
|
|
if self.node.hasAttribute("autoconnect"):
|
|
|
|
value = self.node["autoconnect"].lower()
|
|
|
|
return value != "no" and value != "off" and value != "false"
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2012-05-14 15:50:11 +00:00
|
|
|
@property
|
|
|
|
def id(self):
|
|
|
|
return self.host + ":" + str(self.port)
|
|
|
|
|
2012-07-03 11:00:16 +00:00
|
|
|
def send_ctcp(self, to, msg, cmd = "NOTICE", endl = "\r\n"):
|
2012-07-23 17:20:46 +00:00
|
|
|
"""Send a message as CTCP response"""
|
2012-07-03 11:00:16 +00:00
|
|
|
if msg is not None and to is not None:
|
2012-05-21 16:14:33 +00:00
|
|
|
for line in msg.split("\n"):
|
|
|
|
if line != "":
|
2012-07-23 00:19:43 +00:00
|
|
|
self.s.send (("%s %s :\x01%s\x01%s" % (cmd, to.split("!")[0], line, endl)).encode ())
|
2012-07-03 11:00:16 +00:00
|
|
|
|
|
|
|
def send_dcc(self, msg, to):
|
2012-07-22 23:23:04 +00:00
|
|
|
"""Send a message through DCC connection"""
|
2012-07-03 11:00:16 +00:00
|
|
|
if msg is not None and to is not None:
|
2012-07-23 02:19:53 +00:00
|
|
|
realname = to.split("!")[1]
|
2012-07-23 09:50:08 +00:00
|
|
|
if realname not in self.dcc_clients.keys():
|
|
|
|
d = dcc.DCC(self, to)
|
2012-07-23 02:19:53 +00:00
|
|
|
self.dcc_clients[realname] = d
|
|
|
|
self.dcc_clients[realname].send_dcc(msg)
|
2012-07-22 23:23:04 +00:00
|
|
|
|
2012-05-21 16:14:33 +00:00
|
|
|
|
2012-04-30 16:22:10 +00:00
|
|
|
def send_msg_final(self, channel, msg, cmd = "PRIVMSG", endl = "\r\n"):
|
2012-07-23 17:20:46 +00:00
|
|
|
"""Send a message without checks"""
|
2012-06-23 11:38:34 +00:00
|
|
|
if channel == self.nick:
|
2012-07-22 23:23:04 +00:00
|
|
|
exc_type, exc_value, exc_traceback = sys.exc_info()
|
|
|
|
traceback.print_exception(exc_type, exc_value, exc_traceback)
|
2012-06-23 11:38:34 +00:00
|
|
|
print ("\033[1;35mWarning:\033[0m Nemubot talks to himself: %s" % msg)
|
2012-04-18 21:35:58 +00:00
|
|
|
if msg is not None and channel is not None:
|
|
|
|
for line in msg.split("\n"):
|
2012-04-30 16:22:10 +00:00
|
|
|
if line != "":
|
2012-06-30 14:38:07 +00:00
|
|
|
if len(line) < 442:
|
|
|
|
self.s.send (("%s %s :%s%s" % (cmd, channel, line, endl)).encode ())
|
|
|
|
else:
|
|
|
|
self.s.send (("%s %s :%s%s" % (cmd, channel, line[0:442]+"...", endl)).encode ())
|
2012-04-09 02:19:39 +00:00
|
|
|
|
2012-07-03 11:00:16 +00:00
|
|
|
def send_msg_prtn(self, msg):
|
2012-07-22 23:23:04 +00:00
|
|
|
"""Send a message to partner bot"""
|
2012-05-14 15:50:11 +00:00
|
|
|
self.send_msg_final(self.partner, msg)
|
|
|
|
|
2012-07-03 11:00:16 +00:00
|
|
|
def send_msg_usr(self, user, msg):
|
2012-07-23 17:20:46 +00:00
|
|
|
"""Send a message to a user instead of a channel"""
|
2012-04-30 16:22:10 +00:00
|
|
|
if user is not None and user[0] != "#":
|
2012-07-23 09:50:08 +00:00
|
|
|
realname = user.split("!")[1]
|
2012-07-23 02:19:53 +00:00
|
|
|
if realname in self.dcc_clients:
|
2012-07-03 11:00:16 +00:00
|
|
|
self.send_dcc(msg, user)
|
|
|
|
else:
|
2012-07-23 00:19:43 +00:00
|
|
|
self.send_msg_final(user.split('!')[0], msg)
|
2012-04-30 16:22:10 +00:00
|
|
|
|
2012-07-23 00:19:43 +00:00
|
|
|
def send_msg(self, channel, msg, cmd = "PRIVMSG", endl = "\r\n"):
|
2012-07-23 17:20:46 +00:00
|
|
|
"""Send a message to a channel"""
|
2012-04-30 16:22:10 +00:00
|
|
|
if self.accepted_channel(channel):
|
|
|
|
self.send_msg_final(channel, msg, cmd, endl)
|
2012-04-09 02:19:39 +00:00
|
|
|
|
2012-07-23 00:19:43 +00:00
|
|
|
def send_msg_verified(self, sender, channel, msg, cmd = "PRIVMSG", endl = "\r\n"):
|
2012-07-23 17:20:46 +00:00
|
|
|
"""Send a message to a channel, only if the source user is on this channel too"""
|
2012-07-20 01:24:51 +00:00
|
|
|
if self.accepted_channel(channel, sender):
|
|
|
|
self.send_msg_final(channel, msg, cmd, endl)
|
|
|
|
|
2012-07-23 00:19:43 +00:00
|
|
|
def send_global(self, msg, cmd = "PRIVMSG", endl = "\r\n"):
|
2012-07-23 17:20:46 +00:00
|
|
|
"""Send a message to all channels on this server"""
|
2012-06-23 13:14:36 +00:00
|
|
|
for channel in self.channels.keys():
|
2012-07-23 00:19:43 +00:00
|
|
|
self.send_msg(channel, msg, cmd, endl)
|
2012-04-09 02:19:39 +00:00
|
|
|
|
|
|
|
|
2012-07-20 01:24:51 +00:00
|
|
|
def accepted_channel(self, chan, sender = None):
|
2012-07-23 17:20:46 +00:00
|
|
|
"""Return True if the channel (or the user) is authorized"""
|
2012-04-30 16:22:10 +00:00
|
|
|
if self.listen_nick:
|
2012-07-20 01:24:51 +00:00
|
|
|
return (chan in self.channels and (sender is None or sender in self.channels[chan].people)) or chan == self.nick
|
2012-04-30 16:22:10 +00:00
|
|
|
else:
|
2012-07-20 01:24:51 +00:00
|
|
|
return chan in self.channels and (sender is None or sender in self.channels[chan].people)
|
2012-04-09 02:19:39 +00:00
|
|
|
|
2012-05-21 10:21:14 +00:00
|
|
|
def disconnect(self):
|
2012-07-23 17:20:46 +00:00
|
|
|
"""Close the socket with the server and all DCC client connections"""
|
2012-05-21 10:21:14 +00:00
|
|
|
if self.connected:
|
|
|
|
self.stop = True
|
|
|
|
self.s.shutdown(socket.SHUT_RDWR)
|
2012-07-22 23:23:04 +00:00
|
|
|
|
|
|
|
#Close all DCC connection
|
|
|
|
for clt in self.dcc_clients:
|
|
|
|
self.dcc_clients[clt].disconnect()
|
|
|
|
|
2012-05-21 10:21:14 +00:00
|
|
|
self.stopping.wait()
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2012-06-23 11:38:34 +00:00
|
|
|
def kill(self):
|
|
|
|
if self.connected:
|
|
|
|
self.stop = True
|
|
|
|
self.connected = False
|
|
|
|
#Send a message in order to close the socket
|
|
|
|
self.s.send(("WHO %s\r\n" % self.nick).encode ())
|
|
|
|
self.stopping.wait()
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2012-06-23 13:14:36 +00:00
|
|
|
def join(self, chan, password = None):
|
2012-07-23 17:20:46 +00:00
|
|
|
"""Join a channel"""
|
2012-06-23 13:14:36 +00:00
|
|
|
if chan is not None and self.connected and chan not in self.channels:
|
2012-07-16 22:40:08 +00:00
|
|
|
chn = xmlparser.module_state.ModuleState("channel")
|
2012-06-23 13:14:36 +00:00
|
|
|
chn["name"] = chan
|
|
|
|
chn["password"] = password
|
|
|
|
self.node.addChild(chn)
|
2012-07-23 17:20:46 +00:00
|
|
|
self.channels[chan] = channel.Channel(chn, self)
|
2012-06-23 13:14:36 +00:00
|
|
|
if password is not None:
|
|
|
|
self.s.send(("JOIN %s %s\r\n" % (chan, password)).encode ())
|
|
|
|
else:
|
|
|
|
self.s.send(("JOIN %s\r\n" % chan).encode ())
|
2012-05-21 17:08:41 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2012-06-23 13:14:36 +00:00
|
|
|
def leave(self, chan):
|
2012-07-23 17:20:46 +00:00
|
|
|
"""Leave a channel"""
|
2012-06-23 13:14:36 +00:00
|
|
|
if chan is not None and self.connected and chan in self.channels:
|
|
|
|
self.s.send(("PART %s\r\n" % self.channels[chan].name).encode ())
|
|
|
|
del self.channels[chan]
|
2012-05-21 17:08:41 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
def launch(self, context, verb=True):
|
2012-07-23 17:20:46 +00:00
|
|
|
"""Connect to the server if it is no yet connected"""
|
2012-08-14 03:51:55 +00:00
|
|
|
self.context = context
|
2012-05-21 10:21:14 +00:00
|
|
|
if not self.connected:
|
|
|
|
self.stop = False
|
|
|
|
self.start()
|
2012-08-14 03:51:55 +00:00
|
|
|
elif verb:
|
2012-05-21 10:21:14 +00:00
|
|
|
print (" Already connected.")
|
2012-04-09 02:19:39 +00:00
|
|
|
|
2012-07-23 17:20:46 +00:00
|
|
|
def treat_msg(self, line, private = False):
|
2012-07-03 11:00:16 +00:00
|
|
|
try:
|
2012-07-24 00:34:01 +00:00
|
|
|
msg = message.Message (self, line, private)
|
2012-08-14 03:51:55 +00:00
|
|
|
msg.treat(self.context.hooks)
|
2012-07-03 11:00:16 +00:00
|
|
|
except:
|
2012-07-22 23:23:04 +00:00
|
|
|
print ("\033[1;31mERROR:\033[0m occurred during the processing of the message: %s" % line)
|
2012-07-03 11:00:16 +00:00
|
|
|
exc_type, exc_value, exc_traceback = sys.exc_info()
|
|
|
|
traceback.print_exception(exc_type, exc_value, exc_traceback)
|
|
|
|
|
2012-05-21 10:21:14 +00:00
|
|
|
def run(self):
|
2012-06-23 11:38:34 +00:00
|
|
|
if not self.connected:
|
|
|
|
self.s = socket.socket() #Create the socket
|
|
|
|
self.s.connect((self.host, self.port)) #Connect to server
|
|
|
|
self.stopping.clear()
|
|
|
|
self.connected = True
|
|
|
|
|
|
|
|
if self.password != None:
|
|
|
|
self.s.send(b"PASS " + self.password.encode () + b"\r\n")
|
|
|
|
self.s.send(("NICK %s\r\n" % self.nick).encode ())
|
|
|
|
self.s.send(("USER %s %s bla :%s\r\n" % (self.nick, self.host, self.realname)).encode ())
|
|
|
|
print ("Connection to %s:%d completed" % (self.host, self.port))
|
|
|
|
|
|
|
|
if len(self.channels) > 0:
|
2012-06-23 13:14:36 +00:00
|
|
|
for chn in self.channels.keys():
|
|
|
|
if self.channels[chn].password is not None:
|
|
|
|
self.s.send(("JOIN %s %s\r\n" % (self.channels[chn].name, self.channels[chn].password)).encode ())
|
|
|
|
else:
|
|
|
|
self.s.send(("JOIN %s\r\n" % self.channels[chn].name).encode ())
|
|
|
|
print ("Listen to channels: %s" % ' '.join (self.channels.keys()))
|
2012-04-09 02:19:39 +00:00
|
|
|
|
2012-07-03 11:00:16 +00:00
|
|
|
readbuffer = b'' #Here we store all the messages from server
|
2012-05-21 10:21:14 +00:00
|
|
|
while not self.stop:
|
2012-07-03 11:00:16 +00:00
|
|
|
raw = self.s.recv(1024) #recieve server messages
|
|
|
|
if not raw:
|
|
|
|
break
|
|
|
|
readbuffer = readbuffer + raw
|
|
|
|
temp = readbuffer.split(b'\n')
|
|
|
|
readbuffer = temp.pop()
|
2012-05-21 10:21:14 +00:00
|
|
|
|
|
|
|
for line in temp:
|
2012-07-03 11:00:16 +00:00
|
|
|
self.treat_msg(line)
|
|
|
|
|
2012-06-23 11:38:34 +00:00
|
|
|
if self.connected:
|
|
|
|
self.s.close()
|
|
|
|
self.connected = False
|
|
|
|
print ("Server `%s' successfully stopped." % self.id)
|
2012-05-21 10:21:14 +00:00
|
|
|
self.stopping.set()
|
|
|
|
#Rearm Thread
|
|
|
|
threading.Thread.__init__(self)
|