2012-11-02 11:10:37 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2014-08-30 17:15:14 +00:00
|
|
|
# Nemubot is a smart and modulable IM bot.
|
|
|
|
# Copyright (C) 2012-2014 nemunaire
|
2012-11-02 11:10:37 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2014-09-11 15:23:07 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
2014-09-12 06:12:55 +00:00
|
|
|
import bot
|
2014-09-11 15:23:07 +00:00
|
|
|
from message import Message
|
2012-11-02 11:10:37 +00:00
|
|
|
import server
|
2014-08-30 17:15:14 +00:00
|
|
|
from server.socket import SocketServer
|
2012-11-02 11:10:37 +00:00
|
|
|
|
2014-08-30 17:15:14 +00:00
|
|
|
class IRCServer(SocketServer):
|
2012-11-02 11:10:37 +00:00
|
|
|
|
2014-08-30 17:15:14 +00:00
|
|
|
def __init__(self, node, nick, owner, realname):
|
|
|
|
SocketServer.__init__(self,
|
|
|
|
node["host"],
|
|
|
|
node["port"],
|
|
|
|
node["password"],
|
|
|
|
node.hasAttribute("ssl") and node["ssl"].lower() == "true")
|
2014-08-14 10:49:38 +00:00
|
|
|
|
2014-09-12 06:12:55 +00:00
|
|
|
self.nick = nick
|
|
|
|
self.owner = owner
|
2012-11-02 11:10:37 +00:00
|
|
|
self.realname = realname
|
2014-09-12 06:12:55 +00:00
|
|
|
self.id = nick + "@" + node["host"] + ":" + node["port"]
|
|
|
|
|
2014-08-30 17:15:14 +00:00
|
|
|
|
2014-09-08 00:36:19 +00:00
|
|
|
if node.hasAttribute("caps"):
|
|
|
|
if node["caps"].lower() == "no":
|
|
|
|
self.capabilities = None
|
|
|
|
else:
|
|
|
|
self.capabilities = node["caps"].split(",")
|
|
|
|
else:
|
|
|
|
self.capabilities = list()
|
|
|
|
|
2014-09-12 06:12:55 +00:00
|
|
|
# Register CTCP capabilities
|
|
|
|
self.ctcp_capabilities = dict()
|
|
|
|
|
|
|
|
def _ctcp_clientinfo(msg):
|
|
|
|
"""Response to CLIENTINFO CTCP message"""
|
|
|
|
return _ctcp_response(msg.sender,
|
|
|
|
" ".join(self.ctcp_capabilities.keys()))
|
|
|
|
|
|
|
|
def _ctcp_dcc(msg):
|
|
|
|
"""Response to DCC CTCP message"""
|
|
|
|
try:
|
|
|
|
ip = srv.toIP(int(msg.cmds[3]))
|
|
|
|
port = int(msg.cmds[4])
|
|
|
|
conn = DCC(srv, msg.sender)
|
|
|
|
except:
|
|
|
|
return _ctcp_response(msg.sender, "ERRMSG invalid parameters provided as DCC CTCP request")
|
|
|
|
|
|
|
|
self.logger.info("Receive DCC connection request from %s to %s:%d", conn.sender, ip, port)
|
|
|
|
|
|
|
|
if conn.accept_user(ip, port):
|
|
|
|
srv.dcc_clients[conn.sender] = conn
|
|
|
|
conn.send_dcc("Hello %s!" % conn.nick)
|
|
|
|
else:
|
|
|
|
self.logger.error("DCC: unable to connect to %s:%d", ip, port)
|
|
|
|
return _ctcp_response(msg.sender, "ERRMSG unable to connect to %s:%d" % (ip, port))
|
|
|
|
|
|
|
|
self.ctcp_capabilities["ACTION"] = lambda msg: print ("ACTION receive: %s" % msg.text)
|
|
|
|
self.ctcp_capabilities["CLIENTINFO"] = _ctcp_clientinfo
|
|
|
|
#self.ctcp_capabilities["DCC"] = _ctcp_dcc
|
|
|
|
self.ctcp_capabilities["FINGER"] = lambda msg: _ctcp_response(
|
|
|
|
msg.sender, "VERSION nemubot v%s" % bot.__version__)
|
|
|
|
self.ctcp_capabilities["NEMUBOT"] = lambda msg: _ctcp_response(
|
|
|
|
msg.sender, "NEMUBOT %s" % bot.__version__)
|
|
|
|
self.ctcp_capabilities["PING"] = lambda msg: _ctcp_response(
|
|
|
|
msg.sender, "PING %s" % " ".join(msg.cmds[1:]))
|
|
|
|
self.ctcp_capabilities["SOURCE"] = lambda msg: _ctcp_response(
|
|
|
|
msg.sender, "SOURCE https://github.com/nemunaire/nemubot")
|
|
|
|
self.ctcp_capabilities["TIME"] = lambda msg: _ctcp_response(
|
|
|
|
msg.sender, "TIME %s" % (datetime.now()))
|
|
|
|
self.ctcp_capabilities["USERINFO"] = lambda msg: _ctcp_response(
|
|
|
|
msg.sender, "USERINFO %s" % self.realname)
|
|
|
|
self.ctcp_capabilities["VERSION"] = lambda msg: _ctcp_response(
|
|
|
|
msg.sender, "VERSION nemubot v%s" % bot.__version__)
|
|
|
|
|
|
|
|
self.logger.debug("CTCP capabilities setup: %s", ", ".join(self.ctcp_capabilities))
|
|
|
|
|
|
|
|
# Register hooks on some IRC CMD
|
|
|
|
self.hookscmd = dict()
|
|
|
|
|
|
|
|
def _on_ping(msg):
|
|
|
|
self.write("%s :%s" % ("PONG", msg.params[0]))
|
|
|
|
self.hookscmd["PING"] = _on_ping
|
|
|
|
|
|
|
|
def _on_connect(msg):
|
2014-09-04 08:43:50 +00:00
|
|
|
# First, JOIN some channels
|
|
|
|
for chn in node.getNodes("channel"):
|
|
|
|
if chn["password"] is not None:
|
|
|
|
self.write("JOIN %s %s" % (chn["name"], chn["password"]))
|
|
|
|
else:
|
|
|
|
self.write("JOIN %s" % chn["name"])
|
2014-09-12 06:12:55 +00:00
|
|
|
self.hookscmd["001"] = _on_connect
|
|
|
|
|
|
|
|
def _on_error(msg):
|
|
|
|
self.close()
|
|
|
|
self.hookscmd["ERROR"] = _on_error
|
2014-09-04 08:43:50 +00:00
|
|
|
|
2014-09-12 06:12:55 +00:00
|
|
|
def _on_cap(msg):
|
2014-09-08 00:36:19 +00:00
|
|
|
if len(msg.params) != 3 or msg.params[1] != "LS":
|
2014-09-12 06:12:55 +00:00
|
|
|
return
|
2014-09-08 00:36:19 +00:00
|
|
|
server_caps = msg.params[2].split(" ")
|
|
|
|
for cap in self.capabilities:
|
|
|
|
if cap not in server_caps:
|
|
|
|
self.capabilities.remove(cap)
|
2014-09-08 00:41:50 +00:00
|
|
|
if len(self.capabilities) > 0:
|
|
|
|
self.write("CAP REQ :" + " ".join(self.capabilities))
|
2014-09-08 00:36:19 +00:00
|
|
|
self.write("CAP END")
|
2014-09-12 06:12:55 +00:00
|
|
|
self.hookscmd["CAP"] = _on_cap
|
2014-09-08 00:36:19 +00:00
|
|
|
|
2014-09-04 08:43:50 +00:00
|
|
|
|
2014-08-30 17:15:14 +00:00
|
|
|
def _open(self):
|
|
|
|
if SocketServer._open(self):
|
|
|
|
if self.password is not None:
|
|
|
|
self.write("PASS :" + self.password)
|
2014-09-06 19:30:07 +00:00
|
|
|
if self.capabilities is not None:
|
|
|
|
self.write("CAP LS")
|
2014-08-30 17:15:14 +00:00
|
|
|
self.write("NICK :" + self.nick)
|
|
|
|
self.write("USER %s %s bla :%s" % (self.nick, self.host, self.realname))
|
2012-11-02 11:10:37 +00:00
|
|
|
return True
|
2014-08-30 17:15:14 +00:00
|
|
|
return False
|
2012-11-02 11:10:37 +00:00
|
|
|
|
2014-09-01 17:21:54 +00:00
|
|
|
def send_response(self, res):
|
|
|
|
if type(res.channel) != list:
|
|
|
|
res.channel = [ res.channel ]
|
|
|
|
for channel in res.channel:
|
|
|
|
if channel is not None and channel != self.nick:
|
|
|
|
self.write("%s %s :%s" % ("PRIVMSG", channel, res.get_message()))
|
|
|
|
else:
|
|
|
|
channel = res.sender.split("!")[0]
|
|
|
|
self.write("%s %s :%s" % ("NOTICE" if res.is_ctcp else "PRIVMSG", channel, res.get_message()))
|
|
|
|
|
|
|
|
|
2014-08-30 17:15:14 +00:00
|
|
|
def _close(self):
|
2014-09-08 00:30:18 +00:00
|
|
|
if self.socket is not None: self.write("QUIT")
|
|
|
|
return SocketServer._close(self)
|
2014-09-11 15:23:07 +00:00
|
|
|
|
|
|
|
def read(self):
|
|
|
|
for line in SocketServer.read(self):
|
2014-09-12 06:12:55 +00:00
|
|
|
msg = Message(line, datetime.now())
|
|
|
|
|
|
|
|
if msg.cmd in self.hookscmd:
|
|
|
|
self.hookscmd[msg.cmd](msg)
|
|
|
|
|
|
|
|
elif msg.cmd == "PRIVMSG" and msg.is_ctcp:
|
|
|
|
if msg.cmds[0] in self.ctcp_capabilities:
|
|
|
|
res = self.ctcp_capabilities[msg.cmds[0]](msg)
|
|
|
|
else:
|
|
|
|
res = _ctcp_response(msg.sender, "ERRMSG Unknown or unimplemented CTCP request")
|
|
|
|
if res is not None:
|
|
|
|
self.send_response(res)
|
|
|
|
|
|
|
|
else:
|
|
|
|
yield msg
|
|
|
|
|
|
|
|
|
|
|
|
from response import Response
|
|
|
|
|
|
|
|
def _ctcp_response(sndr, msg):
|
|
|
|
return Response(sndr, msg, ctcp=True)
|