Exemplo n.º 1
0
def send_msg(wnd, msg, all, lUsrPwd, usr):
    """
    Send message to remote tcp server
    """
    answ = str
    toUser = str

    if msg == "":
        QtGui.QMessageBox.warning(wnd, 'Complete', 'Введите сообщение!', QtGui.QMessageBox.Yes)
        return

    if (usr == "") and (not all):
        """
        If message sending to single user and user not selected then fail
        """
        QtGui.QMessageBox.warning(wnd, 'Complete', 'Выберите пользователя!', QtGui.QMessageBox.Yes)
        return

    mdb = MariaDB()
    if not mdb.connect(wnd.MDBServer, wnd.MDBUser, wnd.MDBPasswd, wnd.MDBBase):
        QtGui.QMessageBox.critical(wnd, 'Ошибка', 'Ошибка соединения с Базой Данных!', QtGui.QMessageBox.Yes)
        return

    if not all:
        toUser = mdb.get_user_by_alias(usr)
    mdb.close()

    client = TcpClient()
    if not client.connect(wnd.TCPServer, wnd.TCPPort, wnd.user, lUsrPwd):
        QtGui.QMessageBox.critical(wnd, "Ошибка", "Ошибка соединения с сервером!", QtGui.QMessageBox.Yes)
        return

    if not all:
        answ = client.send_message(toUser, msg)
    else:
        answ = client.send_message("$ALL_USERS$", msg)
    client.close()

    if answ == "[FAIL]":
        QtGui.QMessageBox.critical(wnd, 'Ошибка', 'Ошибка передачи сообщения!', QtGui.QMessageBox.Yes)
        client.close()
        return

    if answ == "[FAIL-LEN]":
        QtGui.QMessageBox.critical(wnd, 'Ошибка', 'Сообщение слишком длинное!', QtGui.QMessageBox.Yes)
        client.close()
        return

    if answ == "[FAIL-ACCESS]":
        QtGui.QMessageBox.critical(wnd, 'Ошибка', 'У Вас нет прав на отправку всем пользователям!',
                                       QtGui.QMessageBox.Yes)
        client.close()
        return

    if answ == "[SEND-MSG-OK]":
        mb = MessageBase()
        if not all:
            mb.save_message(usr, msg, False)
            QtGui.QMessageBox.information(wnd, 'Complete', 'Сообщение отправлено!', QtGui.QMessageBox.Yes)
        else:
            mb.save_message("Всем", msg, False)
            QtGui.QMessageBox.information(wnd, 'Complete', 'Сообщение отправлено всем пользователям!',
                                              QtGui.QMessageBox.Yes)
Exemplo n.º 2
0
    def run(self):
        toUser = str("")
        self.client = TcpClient()

        mdb = MariaDB()
        if not mdb.connect(self._wnd.MDBServer, self._wnd.MDBUser, self._wnd.MDBPasswd, self._wnd.MDBBase):
            self.err.emit('Ошибка соединения с Базой Данных!')
            return
        toUser = mdb.get_user_by_alias(self._toUsr)
        mdb.close()

        if not os.path.exists("".join((self._wnd.app_path, "sendfiles"))):
            os.makedirs("".join((self._wnd.app_path,"sendfiles")))

        self.connectionStart.emit()
        if not self.client.connect(self._server, self._port, self._wnd.user, self._wnd.passwd):
            print("fail connection")
            self.err.emit("Ошибка соединения с сервером!")
            return

        exts = []
        try:
            exts = self.cfg.unzip_formats()
        except:
            Log().local("Error reading unzip formats")

        c_exts = []
        try:
            c_exts = self.cfg.uncrypt_formats()
        except:
            Log().local("Error reading uncrypted formats")

        print("start send")
        self.client.begin_send_files(toUser)

        for sfile in self.fileList:
            lsf = sfile.split("/")
            l = len(lsf)
            fname = lsf[l - 1]

            """
            Checking extension
			"""
            isCompress = True
            isCrypt = True

            tmp_fname = fname.split(".")
            ext = tmp_fname[len(tmp_fname) - 1].lower()

            for ex in exts:
                if ex == ext:
                    isCompress = False
                    break

            for ex in c_exts:
                if ex == ext:
                    isCrypt = False
                    break

            """
            Rename file
            """
            while True:
                try:
                    parts = fname.split("'")
                    fname = ""
                    l = len(parts)
                    i = 0
                    for part in parts:
                        i += 1
                        if i < l:
                            fname = fname + part + "_"
                        else:
                            fname = fname + part
                    break
                except:
                    break

            self.compressStart.emit(fname)
            if isCompress:
                if not zlib_compress_file(sfile, "".join((self._wnd.app_path, "sendfiles/", fname, ".z"))):
                    Log().local("Error compressing send file: " + fname)
                    print("error compressing")
                    self.client.close()
                    self.err.emit("Ошибка при сжатии файла")
                    return
                else:
                    print("".join((fname, " compressed")))
            else:
                print(fname + " not compressed")
                shutil.copy2(sfile, "sendfiles/" + fname + ".z")

            if isCrypt:
                self.cryptStart.emit(fname)
                if not AES256_encode_file("".join((self._wnd.app_path, "sendfiles/", fname, ".z")),
                                          "".join((self._wnd.app_path, "sendfiles/", fname, ".bin")),
                                          self.a_key):
                    Log().local("Error encrypting send file: " + fname)
                    print("error crypting")
                    self.client.close()
                    self.err.emit("Ошибка при шифровании сообщения")
                else:
                    print("".join((fname, " crypted")))
            else:
                print(fname + " not crypt")
                shutil.copy2("".join((self._wnd.app_path,"sendfiles/", fname, ".z")),
                             "".join((self._wnd.app_path,"sendfiles/", fname, ".bin")))

            self.sendStart.emit(fname)
            self.client.send_file("".join((self._wnd.app_path, "sendfiles/", fname)))
            self.sendFileComplete.emit()

            try:
                os.remove("".join((self._wnd.app_path, "sendfiles/", fname, ".z")))
                os.remove("".join((self._wnd.app_path, "sendfiles/", fname, ".bin")))
            except:
                Log().local("Error filename")
                self.err.emit("Ошибка в имени файла!")

        self.client.end_send_files()

        self.sendComplete.emit()
        print("send complete")
        self.client.close()