index : logbot | |
Archlinux32 log bot | gitolite user |
summaryrefslogtreecommitdiff |
author | Erich Eckner <git@eckner.net> | 2022-11-06 10:20:31 +0100 |
---|---|---|
committer | Erich Eckner <git@eckner.net> | 2022-11-06 10:20:31 +0100 |
commit | 041e30310f9892c1acc0ebd1fd7c337d3a71652d (patch) | |
tree | b30d40b925b68a24962cab899148310407bd171f | |
parent | 5479c299d14d8ef5818797709533b8f593055c5c (diff) |
-rw-r--r-- | ircbot.py | 2 | ||||
-rw-r--r-- | irclib.py | 70 | ||||
-rwxr-xr-x | logbot.py | 38 |
@@ -25,7 +25,7 @@ write simpler bots. """ import sys -from UserDict import UserDict +from collections import UserDict from irclib import SimpleIRCClient from irclib import nm_to_n, irc_lower, all_events @@ -342,7 +342,7 @@ class Connection: self.irclibobj = irclibobj def _get_socket(): - raise IRCError, "Not overridden" + raise IRCError("Not overridden") ############################## ### Convenience wrappers. @@ -433,10 +433,10 @@ class ServerConnection(Connection): self.socket.connect((self.server, self.port)) if ssl: self.ssl = socket.ssl(self.socket) - except socket.error, x: + except socket.error as x: self.socket.close() self.socket = None - raise ServerConnectionError, "Couldn't connect to socket: %s" % x + raise ServerConnectionError("Couldn't connect to socket: %s" % x) self.connected = 1 if self.irclibobj.fn_to_add_socket: self.irclibobj.fn_to_add_socket(self.socket) @@ -491,7 +491,7 @@ class ServerConnection(Connection): new_data = self.ssl.read(2**14) else: new_data = self.socket.recv(2**14) - except socket.error, x: + except socket.error as x: # The server hung up. self.disconnect("Connection reset by peer") return @@ -500,14 +500,14 @@ class ServerConnection(Connection): self.disconnect("Connection reset by peer") return - lines = _linesep_regexp.split(self.previous_buffer + new_data) + lines = _linesep_regexp.split(self.previous_buffer + new_data.decode()) # Save the last, unfinished line. self.previous_buffer = lines.pop() for line in lines: if DEBUG: - print "FROM SERVER:", line + print("FROM SERVER:", line) if not line: continue @@ -561,7 +561,7 @@ class ServerConnection(Connection): command = "privnotice" for m in messages: - if type(m) is types.TupleType: + if type(m) is tuple: if command in ["privmsg", "pubmsg"]: command = "ctcp" else: @@ -569,15 +569,15 @@ class ServerConnection(Connection): m = list(m) if DEBUG: - print "command: %s, source: %s, target: %s, arguments: %s" % ( - command, prefix, target, m) + print("command: %s, source: %s, target: %s, arguments: %s" % ( + command, prefix, target, m)) self._handle_event(Event(command, prefix, target, m)) if command == "ctcp" and m[0] == "ACTION": self._handle_event(Event("action", prefix, target, m[1:])) else: if DEBUG: - print "command: %s, source: %s, target: %s, arguments: %s" % ( - command, prefix, target, [m]) + print("command: %s, source: %s, target: %s, arguments: %s" % ( + command, prefix, target, [m])) self._handle_event(Event(command, prefix, target, [m])) else: target = None @@ -595,8 +595,8 @@ class ServerConnection(Connection): command = "umode" if DEBUG: - print "command: %s, source: %s, target: %s, arguments: %s" % ( - command, prefix, target, arguments) + print("command: %s, source: %s, target: %s, arguments: %s" % ( + command, prefix, target, arguments)) self._handle_event(Event(command, prefix, target, arguments)) def _handle_event(self, event): @@ -660,7 +660,7 @@ class ServerConnection(Connection): try: self.socket.close() - except socket.error, x: + except socket.error as x: pass self.socket = None self._handle_event(Event("disconnect", self.server, "", [message])) @@ -782,15 +782,15 @@ class ServerConnection(Connection): The string will be padded with appropriate CR LF. """ if self.socket is None: - raise ServerNotConnectedError, "Not connected." + raise ServerNotConnectedError("Not connected.") try: if self.ssl: - self.ssl.write(string + "\r\n") + self.ssl.write((string + "\r\n").encode()) else: - self.socket.send(string + "\r\n") + self.socket.send((string + "\r\n").encode()) if DEBUG: - print "TO SERVER:", string - except socket.error, x: + print("TO SERVER:", string) + except socket.error as x: # Ouch! self.disconnect("Connection reset by peer.") @@ -888,8 +888,8 @@ class DCCConnection(Connection): self.passive = 0 try: self.socket.connect((self.peeraddress, self.peerport)) - except socket.error, x: - raise DCCConnectionError, "Couldn't connect to socket: %s" % x + except socket.error as x: + raise DCCConnectionError("Couldn't connect to socket: %s" % x) self.connected = 1 if self.irclibobj.fn_to_add_socket: self.irclibobj.fn_to_add_socket(self.socket) @@ -913,8 +913,8 @@ class DCCConnection(Connection): self.socket.bind((socket.gethostbyname(socket.gethostname()), 0)) self.localaddress, self.localport = self.socket.getsockname() self.socket.listen(10) - except socket.error, x: - raise DCCConnectionError, "Couldn't bind socket: %s" % x + except socket.error as x: + raise DCCConnectionError("Couldn't bind socket: %s" % x) return self def disconnect(self, message=""): @@ -930,7 +930,7 @@ class DCCConnection(Connection): self.connected = 0 try: self.socket.close() - except socket.error, x: + except socket.error as x: pass self.socket = None self.irclibobj._handle_event( @@ -947,8 +947,8 @@ class DCCConnection(Connection): self.socket = conn self.connected = 1 if DEBUG: - print "DCC connection from %s:%d" % ( - self.peeraddress, self.peerport) + print("DCC connection from %s:%d" % ( + self.peeraddress, self.peerport)) self.irclibobj._handle_event( self, Event("dcc_connect", self.peeraddress, None, None)) @@ -956,7 +956,7 @@ class DCCConnection(Connection): try: new_data = self.socket.recv(2**14) - except socket.error, x: + except socket.error as x: # The server hung up. self.disconnect("Connection reset by peer") return @@ -985,11 +985,11 @@ class DCCConnection(Connection): target = None for chunk in chunks: if DEBUG: - print "FROM PEER:", chunk + print("FROM PEER:", chunk) arguments = [chunk] if DEBUG: - print "command: %s, source: %s, target: %s, arguments: %s" % ( - command, prefix, target, arguments) + print("command: %s, source: %s, target: %s, arguments: %s" % ( + command, prefix, target, arguments)) self.irclibobj._handle_event( self, Event(command, prefix, target, arguments)) @@ -1009,8 +1009,8 @@ class DCCConnection(Connection): if self.dcctype == "chat": self.socket.send("\n") if DEBUG: - print "TO PEER: %s\n" % string - except socket.error, x: + print("TO PEER: %s\n" % string) + except socket.error as x: # Ouch! self.disconnect("Connection reset by peer.") @@ -1183,8 +1183,8 @@ def mask_matches(nick, mask): _special = "-[]\\`^{}" nick_characters = string.ascii_letters + string.digits + _special -_ircstring_translation = string.maketrans(string.ascii_uppercase + "[]\\^", - string.ascii_lowercase + "{}|~") +_ircstring_translation = str.maketrans(string.ascii_uppercase + "[]\\^", + string.ascii_lowercase + "{}|~") def irc_lower(s): """Returns a lowercased string. @@ -1557,4 +1557,4 @@ protocol_events = [ "pong", ] -all_events = generated_events + protocol_events + numeric_events.values() +all_events = generated_events + protocol_events + list(numeric_events.values()) @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python # coding: utf-8 """ @@ -143,17 +143,17 @@ html_header = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ### Helper functions def append_line(filename, line): - data = open(filename, "rb").readlines()[:-2] + data = open(filename, "r").readlines()[:-2] data += [line, "\n<br />", "\n</body>", "\n</html>"] write_lines(filename, data) def write_lines(filename, lines): - f = open(filename, "wb") + f = open(filename, "w") f.writelines(lines) f.close() def write_string(filename, string): - f = open(filename, "wb") + f = open(filename, "w") f.write(string) f.close() @@ -223,15 +223,15 @@ class Logbot(SingleServerIRCBot): self.nick_pass = nick_pass self.load_channel_locations() - print "Logbot %s" % __version__ - print "Connecting to %s:%i..." % (server, port) - print "Press Ctrl-C to quit" + print("Logbot %s" % __version__) + print("Connecting to %s:%i..." % (server, port)) + print("Press Ctrl-C to quit") def quit(self): self.connection.disconnect("Quitting...") def color(self, user): - hash = md5(user).hexdigest() + hash = md5(user.encode()).hexdigest() r = int(hash[0:2],16) g = int(hash[2:4],16) b = int(hash[4:6],16) @@ -249,7 +249,7 @@ class Logbot(SingleServerIRCBot): def format_event(self, name, event, params): msg = self.format[name] - for key, val in params.iteritems(): + for key, val in params.items(): msg = msg.replace(key, val) # Always replace %user% with e.source() @@ -288,7 +288,7 @@ class Logbot(SingleServerIRCBot): if self.ftp and self.count > FTP_WAIT: self.count = 0 - print "Uploading to FTP..." + print("Uploading to FTP...") for root, dirs, files in os.walk("logs"): #TODO: Create folders @@ -299,25 +299,25 @@ class Logbot(SingleServerIRCBot): remote_fname = "/".join(full_fname.split("\\")[1:]) else: remote_fname = "/".join(full_fname.split("/")[1:]) - if DEBUG: print repr(remote_fname) + if DEBUG: print(repr(remote_fname)) # Upload! try: self.ftp.storbinary("STOR %s" % remote_fname, open(full_fname, "rb")) # Folder doesn't exist, try creating it and storing again - except ftplib.error_perm, e: #code, error = str(e).split(" ", 1) + except ftplib.error_perm as e: #code, error = str(e).split(" ", 1) if str(e).split(" ", 1)[0] == "553": self.ftp.mkd(os.path.dirname(remote_fname)) self.ftp.storbinary("STOR %s" % remote_fname, open(full_fname, "rb")) else: raise e # Reconnect on timeout - except ftplib.error_temp, e: self.set_ftp(connect_ftp()) + except ftplib.error_temp as e: self.set_ftp(connect_ftp()) # Unsure of error, try reconnecting except: self.set_ftp(connect_ftp()) - print "Finished uploading" + print("Finished uploading") def append_log_msg(self, channel, msg): - print "%s >>> %s" % (channel, msg) + print("%s >>> %s" % (channel, msg)) #Make sure the channel is always lowercase to prevent logs with other capitalisations to be created channel_title = channel channel = channel.lower() @@ -365,7 +365,7 @@ class Logbot(SingleServerIRCBot): def on_all_raw_messages(self, c, e): """Display all IRC connections in terminal""" - if DEBUG: print e.arguments()[0] + if DEBUG: print(e.arguments()[0]) def on_welcome(self, c, e): """Join channels after successful connection""" @@ -432,7 +432,7 @@ class Logbot(SingleServerIRCBot): self.write_event("pubnotice", e) def on_privmsg(self, c, e): - print nm_to_n(e.source()), e.arguments() + print(nm_to_n(e.source()), e.arguments()) c.privmsg(nm_to_n(e.source()), self.format["help"]) def on_quit(self, c, e): @@ -451,10 +451,10 @@ class Logbot(SingleServerIRCBot): self.channel_locations = {} if os.path.exists(CHANNEL_LOCATIONS_FILE): f = open(CHANNEL_LOCATIONS_FILE, 'r') - self.channel_locations = dict((k.lower(), v) for k, v in dict([line.strip().split(None,1) for line in f.readlines()]).iteritems()) + self.channel_locations = dict((k.lower(), v) for k, v in dict([line.strip().split(None,1) for line in f.readlines()]).items()) def connect_ftp(): - print "Using FTP %s..." % (FTP_SERVER) + print("Using FTP %s..." % (FTP_SERVER)) f = ftplib.FTP(FTP_SERVER, FTP_USER, FTP_PASS) f.cwd(FTP_FOLDER) return f |