diff --git a/irc.py b/irc.py index 41e615f..10a562d 100644 --- a/irc.py +++ b/irc.py @@ -61,13 +61,22 @@ SERVICES = [ class PesterIRC(QtCore.QThread): """Class for making a thread that manages the connection to server.""" - def __init__(self, window, server: str, port: int, ssl: bool, verify_hostname=True): + def __init__( + self, + window, + server: str, + port: int, + ssl: bool, + password="", + verify_hostname=True, + ): QtCore.QThread.__init__(self) self.mainwindow = window self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server = server # Server to connect to. self.port = port # Port on server to connect to. + self.password = password # Optional password for PASS. self.ssl = ssl # Whether to connect over SSL/TLS. self.verify_hostname = ( verify_hostname # Whether to verify server hostname. (SSL-only) @@ -179,6 +188,8 @@ class PesterIRC(QtCore.QThread): self.socket.settimeout(90) self._send_irc.socket = self.socket + if self.password: + self._send_irc.pass_(self.password) self._send_irc.nick(self.mainwindow.profile().handle) self._send_irc.user("pcc31", "pcc31") @@ -404,7 +415,7 @@ class PesterIRC(QtCore.QThread): self.mainwindow.doAutoIdentify() self.mainwindow.autoJoinDone = False self.mainwindow.doAutoJoins() - self.updateMood() + self.update_mood() @QtCore.pyqtSlot() def update_mood(self): diff --git a/pesterchum.py b/pesterchum.py index 8248f57..52ebb81 100755 --- a/pesterchum.py +++ b/pesterchum.py @@ -3805,9 +3805,10 @@ class PesterWindow(MovingWindow): "port": int( server_and_port[1] ), # to make sure port is a valid integer, and raise an exception if it cannot be converted. + "pass": self.auth_pass_qline.text(), "TLS": self.TLS_checkbox.isChecked(), } - PchumLog.info("server: " + str(server)) + PchumLog.info("server: %s", server) except: msgbox = QtWidgets.QMessageBox() msgbox.setStyleSheet( @@ -3950,12 +3951,24 @@ class PesterWindow(MovingWindow): layout.addWidget(cancel) layout.addWidget(ok) main_layout = QtWidgets.QVBoxLayout() + nep_prompt = QtWidgets.QLabel( ":33 < Please put in the server's address in the format HOSTNAME:PORT\n:33 < Fur example, irc.pesterchum.xyz:6697" ) nep_prompt.setStyleSheet("QLabel { color: #416600; font-weight: bold;}") + + auth_pass_prompt = QtWidgets.QLabel(":33 < type the password!! (optional)") + auth_pass_prompt.setStyleSheet( + "QLabel { color: #416600; font-weight: bold;}" + ) + + self.auth_pass_qline = QtWidgets.QLineEdit(self) + self.auth_pass_qline.setMinimumWidth(200) + main_layout.addWidget(nep_prompt) main_layout.addWidget(self.customServerPrompt_qline) + main_layout.addWidget(auth_pass_prompt) + main_layout.addWidget(self.auth_pass_qline) main_layout.addLayout(TLS_layout) main_layout.addLayout(layout) @@ -4048,6 +4061,11 @@ class PesterWindow(MovingWindow): try: selected_entry = self.serverBox.currentIndex() + PchumLog.debug( + "'%s' == '%s'", + server_obj[selected_entry]["server"], + self.serverBox.currentText(), + ) assert ( server_obj[selected_entry]["server"] == self.serverBox.currentText() ) @@ -4060,9 +4078,13 @@ class PesterWindow(MovingWindow): try: with open(_datadir + "server.json", "w") as server_file: + password = "" + if "pass" in server_obj[selected_entry]: + password = server_obj[selected_entry]["pass"] json_server_file = { "server": server_obj[selected_entry]["server"], "port": server_obj[selected_entry]["port"], + "pass": password, "TLS": server_obj[selected_entry]["TLS"], } server_file.write(json.dumps(json_server_file, indent=4)) @@ -4089,12 +4111,13 @@ class PesterWindow(MovingWindow): for i in range(len(server_obj)): server_list_items.append(server_obj[i]["server"]) except: + PchumLog.exception("") if not self.chooseServerAskedToReset: self.chooseServerAskedToReset = True self.resetServerlist() return 1 - PchumLog.info("server_list_items: " + str(server_list_items)) + PchumLog.info("server_list_items: %s", server_list_items) # Widget 1 self.chooseServerWidged = QtWidgets.QDialog() @@ -4326,6 +4349,7 @@ class MainProgram(QtCore.QObject): self.widget.config.server(), self.widget.config.port(), self.widget.config.ssl(), + password=self.widget.config.password(), ) self.connectWidgets(self.irc, self.widget) @@ -4503,6 +4527,7 @@ class MainProgram(QtCore.QObject): self.widget.config.server(), self.widget.config.port(), self.widget.config.ssl(), + password=self.widget.config.password(), verify_hostname=verify_hostname, ) self.connectWidgets(self.irc, self.widget) diff --git a/profile.py b/profile.py index 13ce1f4..a06020d 100644 --- a/profile.py +++ b/profile.py @@ -446,10 +446,10 @@ with a backup from: %s" try: with open(_datadir + "server.json") as server_file: read_file = server_file.read() - server_file.close() server_obj = json.loads(read_file) return server_obj["server"] except: + PchumLog.exception("Failed to load server, falling back to default.") try: with open(_datadir + "server.json", "w") as server_file: json_server_file = { @@ -458,7 +458,6 @@ with a backup from: %s" "TLS": True, } server_file.write(json.dumps(json_server_file, indent=4)) - server_file.close() server = "irc.pesterchum.xyz" except: return self.config.get("server", "irc.pesterchum.xyz") @@ -469,11 +468,11 @@ with a backup from: %s" try: with open(_datadir + "server.json") as server_file: read_file = server_file.read() - server_file.close() - server_obj = json.loads(read_file) + server_obj = json.loads(read_file) port = server_obj["port"] return port except: + PchumLog.exception("Failed to load port, falling back to default.") return self.config.get("port", "6697") def ssl(self): @@ -482,10 +481,23 @@ with a backup from: %s" try: with open(_datadir + "server.json") as server_file: read_file = server_file.read() - server_file.close() - server_obj = json.loads(read_file) + server_obj = json.loads(read_file) return server_obj["TLS"] except: + PchumLog.exception("Failed to load TLS setting, falling back to default.") + return self.config.get("TLS", True) + + def password(self): + try: + with open(_datadir + "server.json") as server_file: + read_file = server_file.read() + server_obj = json.loads(read_file) + password = "" + if "pass" in server_obj: + password = server_obj["pass"] + return password + except: + PchumLog.exception("Failed to load TLS setting, falling back to default.") return self.config.get("TLS", True) def soundOn(self): diff --git a/scripts/irc_protocol.py b/scripts/irc_protocol.py index 81bfde3..4c054fd 100644 --- a/scripts/irc_protocol.py +++ b/scripts/irc_protocol.py @@ -54,6 +54,12 @@ class SendIRC: """Send PONG command to reply to server PING.""" self._send("PONG", token) + def pass_(self, password): + """Send a 'connection password' to the server. + + Function is 'pass_' because 'pass' is reserved.""" + self._send("PASS", text=password) + def nick(self, nick): """Send USER command to communicate nick to server.""" self._send("NICK", nick)