Print a warning when send a message to a non-connected server

This commit is contained in:
Némunaire 2012-11-01 09:49:17 +01:00
parent 9e452d22a9
commit 7d0d678f3b

View file

@ -136,7 +136,10 @@ class Server(threading.Thread):
"""Send a message as CTCP response""" """Send a message as CTCP response"""
if msg is not None and to is not None: if msg is not None and to is not None:
for line in msg.split("\n"): for line in msg.split("\n"):
if line != "": if self.s is None:
print ("\033[1;35mWarning:\033[0m Attempt to send message on a non connected server: %s: %s" % (self.id, msg))
traceback.print_stack()
elif line != "":
self.s.send (("%s %s :\x01%s\x01%s" % (cmd, to.split("!")[0], line, endl)).encode ()) self.s.send (("%s %s :\x01%s\x01%s" % (cmd, to.split("!")[0], line, endl)).encode ())
def send_dcc(self, msg, to): def send_dcc(self, msg, to):
@ -152,13 +155,15 @@ class Server(threading.Thread):
def send_msg_final(self, channel, msg, cmd = "PRIVMSG", endl = "\r\n"): def send_msg_final(self, channel, msg, cmd = "PRIVMSG", endl = "\r\n"):
"""Send a message without checks""" """Send a message without checks"""
if channel == self.nick: if channel == self.nick:
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(exc_type, exc_value, exc_traceback)
print ("\033[1;35mWarning:\033[0m Nemubot talks to himself: %s" % msg) print ("\033[1;35mWarning:\033[0m Nemubot talks to himself: %s" % msg)
traceback.print_stack()
if msg is not None and channel is not None: if msg is not None and channel is not None:
for line in msg.split("\n"): for line in msg.split("\n"):
if line != "": if line != "":
if len(line) < 442: if self.s is None:
print ("\033[1;35mWarning:\033[0m Attempt to send message on a non connected server: %s: %s" % (self.id, msg))
traceback.print_stack()
elif len(line) < 442:
self.s.send (("%s %s :%s%s" % (cmd, channel, line, endl)).encode ()) self.s.send (("%s %s :%s%s" % (cmd, channel, line, endl)).encode ())
else: else:
self.s.send (("%s %s :%s%s" % (cmd, channel, line[0:442]+"...", endl)).encode ()) self.s.send (("%s %s :%s%s" % (cmd, channel, line[0:442]+"...", endl)).encode ())
@ -270,8 +275,8 @@ class Server(threading.Thread):
self.s.connect((self.host, self.port)) #Connect to server self.s.connect((self.host, self.port)) #Connect to server
except socket.error as e: except socket.error as e:
self.s = None self.s = None
print ("Unable to connect to %s:%d: %s" % (self.host, self.port, print ("\033[1;31mError:\033[0m Unable to connect to %s:%d: %s"
os.strerror(e.errno))) % (self.host, self.port, os.strerror(e.errno)))
return return
self.stopping.clear() self.stopping.clear()
self.connected = True self.connected = True