nemubot/server/IRC.py

74 lines
2.7 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# Nemubot is a smart and modulable IM bot.
# Copyright (C) 2012-2014 nemunaire
#
# 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/>.
import server
from server.socket import SocketServer
class IRCServer(SocketServer):
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
self.nick = nick
self.owner = owner
self.realname = realname
2014-09-06 19:30:07 +00:00
self.capabilities = list() if not node.hasAttribute("caps") or node["caps"].lower() != "no" else None
self.id = "TODO"
def _on_connect():
# 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"])
self._on_connect = _on_connect
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")
self.write("NICK :" + self.nick)
self.write("USER %s %s bla :%s" % (self.nick, self.host, self.realname))
2014-09-06 19:30:07 +00:00
if self.capabilities is not None:
self.write("CAP END")
return True
return False
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()))
def _close(self):
self.write("QUIT")
SocketServer._close(self)