Allow socket to print messages

This commit is contained in:
nemunaire 2015-07-17 20:04:37 +02:00
parent a00c354287
commit 0208a5d552
4 changed files with 74 additions and 47 deletions

View file

@ -17,54 +17,11 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from nemubot.message import Text
from nemubot.message.visitor import AbstractVisitor
from nemubot.message.printer.socket import Socket as SocketPrinter
class IRC(AbstractVisitor):
def __init__(self):
self.pp = ""
class IRC(SocketPrinter):
def visit_Text(self, msg):
self.pp += "PRIVMSG %s :" % ",".join(msg.to)
if isinstance(msg.message, str):
self.pp += msg.message
else:
msg.message.accept(self)
self.pp += "\r\n"
def visit_DirectAsk(self, msg):
others = [to for to in msg.to if to != msg.designated]
# Avoid nick starting message when discussing on user channel
if len(others) != len(msg.to):
res = Text(msg.message,
server=msg.server, date=msg.date,
to=msg.to, frm=msg.frm)
res.accept(self)
if len(others):
res = Text("%s: %s" % (msg.designated, msg.message),
server=msg.server, date=msg.date,
to=others, frm=msg.frm)
res.accept(self)
def visit_Command(self, msg):
res = Text("!%s%s%s" % (msg.cmd,
" " if len(msg.args) else "",
" ".join(msg.args)),
server=msg.server, date=msg.date,
to=msg.to, frm=msg.frm)
res.accept(self)
def visit_OwnerCommand(self, msg):
res = Text("`%s%s%s" % (msg.cmd,
" " if len(msg.args) else "",
" ".join(msg.args)),
server=msg.server, date=msg.date,
to=msg.to, frm=msg.frm)
res.accept(self)
SocketPrinter.visit_Text(self, msg)

View file

@ -0,0 +1,68 @@
# -*- coding: utf-8 -*-
# Nemubot is a smart and modulable IM bot.
# Copyright (C) 2012-2015 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/>.
from nemubot.message import Text
from nemubot.message.visitor import AbstractVisitor
class Socket(AbstractVisitor):
def __init__(self):
self.pp = ""
def visit_Text(self, msg):
if isinstance(msg.message, str):
self.pp += msg.message
else:
msg.message.accept(self)
def visit_DirectAsk(self, msg):
others = [to for to in msg.to if to != msg.designated]
# Avoid nick starting message when discussing on user channel
if len(others) != len(msg.to):
res = Text(msg.message,
server=msg.server, date=msg.date,
to=msg.to, frm=msg.frm)
res.accept(self)
if len(others):
res = Text("%s: %s" % (msg.designated, msg.message),
server=msg.server, date=msg.date,
to=others, frm=msg.frm)
res.accept(self)
def visit_Command(self, msg):
res = Text("!%s%s%s" % (msg.cmd,
" " if len(msg.args) else "",
" ".join(msg.args)),
server=msg.server, date=msg.date,
to=msg.to, frm=msg.frm)
res.accept(self)
def visit_OwnerCommand(self, msg):
res = Text("`%s%s%s" % (msg.cmd,
" " if len(msg.args) else "",
" ".join(msg.args)),
server=msg.server, date=msg.date,
to=msg.to, frm=msg.frm)
res.accept(self)

View file

@ -57,8 +57,8 @@ class IRC(SocketServer):
self.realname = realname
self.id = self.username + "@" + host + ":" + str(port)
self.printer = IRCPrinter
SocketServer.__init__(self, host=host, port=port, ssl=ssl)
self.printer = IRCPrinter
self.encoding = encoding

View file

@ -16,6 +16,7 @@
# 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/>.
from nemubot.message.printer.socket import Socket as SocketPrinter
from nemubot.server.abstract import AbstractServer
@ -31,6 +32,7 @@ class SocketServer(AbstractServer):
self.socket = None
self.readbuffer = b''
self.printer = SocketPrinter
def fileno(self):