2023-10-19 17:49:54 +02:00
|
|
|
;; tinmop: a multiprotocol client
|
2023-10-19 17:46:22 +02:00
|
|
|
;; Copyright © cage
|
2020-05-08 15:45:43 +02:00
|
|
|
|
|
|
|
;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
(in-package :os-utils)
|
|
|
|
|
|
|
|
(alexandria:define-constant +proc-file-system+
|
|
|
|
(concatenate 'string filesystem-utils:*directory-sep*
|
|
|
|
"proc")
|
|
|
|
:test #'string=)
|
|
|
|
|
|
|
|
(alexandria:define-constant +proc-cpuinfo+
|
|
|
|
(concatenate 'string +proc-file-system+ filesystem-utils:*directory-sep*
|
|
|
|
"cpuinfo")
|
|
|
|
:test #'string=)
|
|
|
|
|
2020-10-11 18:22:07 +02:00
|
|
|
(alexandria:define-constant +ssl-cert-name+ "cert.pem" :test #'string=)
|
|
|
|
|
|
|
|
(alexandria:define-constant +ssl-key-name+ "key" :test #'string=)
|
|
|
|
|
2020-05-08 15:45:43 +02:00
|
|
|
(declaim (ftype (function () fixnum) cpu-number))
|
|
|
|
|
|
|
|
(defun cpu-number ()
|
|
|
|
#+windows (the fixnum 1)
|
|
|
|
#-windows
|
|
|
|
(with-open-file (stream +proc-cpuinfo+ :direction :input
|
|
|
|
:if-does-not-exist :error)
|
|
|
|
(do ((line (read-line stream nil nil) (read-line stream nil nil))
|
|
|
|
(cpu-count 0))
|
|
|
|
((not line) (the fixnum cpu-count))
|
|
|
|
(when (cl-ppcre:scan "^processor" line)
|
|
|
|
(incf cpu-count)))))
|
|
|
|
|
|
|
|
(defun xdg-open (file)
|
2021-03-15 19:28:07 +01:00
|
|
|
(let ((cmd-line (format nil "~a '~a'" +xdg-open-bin+ file)))
|
|
|
|
#+debug-mode
|
|
|
|
(misc:dbg "xdg-open: ~a" cmd-line)
|
|
|
|
(uiop:launch-program cmd-line :output nil)))
|
2020-05-08 15:45:43 +02:00
|
|
|
|
2023-08-14 17:12:39 +02:00
|
|
|
(defun getenv (name &key (default nil))
|
|
|
|
(or (nix:getenv name)
|
|
|
|
default))
|
2020-05-08 15:45:43 +02:00
|
|
|
|
|
|
|
(defun default-temp-dir ()
|
2023-08-14 17:12:39 +02:00
|
|
|
(getenv "TMPDIR" :default "/tmp/"))
|
2020-05-08 15:45:43 +02:00
|
|
|
|
2021-03-28 14:33:56 +02:00
|
|
|
(defun pwd ()
|
2023-08-14 17:12:39 +02:00
|
|
|
(getenv "PWD"))
|
2021-03-28 14:33:56 +02:00
|
|
|
|
2020-05-08 15:45:43 +02:00
|
|
|
(defun external-editor ()
|
2021-05-28 20:55:22 +02:00
|
|
|
(let* ((editor (or (swconf:external-editor)
|
|
|
|
(and (text-utils:string-not-empty-p (getenv "VISUAL"))
|
|
|
|
(getenv "VISUAL"))
|
|
|
|
(and (text-utils:string-not-empty-p (getenv "EDITOR"))
|
|
|
|
(getenv "EDITOR"))
|
2021-05-28 20:58:22 +02:00
|
|
|
constants:+standard-editor+))
|
2021-05-28 20:55:22 +02:00
|
|
|
(space (cl-ppcre:scan "\\s" editor)))
|
|
|
|
(if space
|
|
|
|
(let ((exe (subseq editor 0 space))
|
|
|
|
(args (subseq editor (1+ space))))
|
|
|
|
(values exe args))
|
|
|
|
(values editor nil))))
|
2020-05-08 15:45:43 +02:00
|
|
|
|
2021-05-09 16:32:32 +02:00
|
|
|
(defun run-external-program (program args
|
|
|
|
&key
|
|
|
|
(wait t)
|
|
|
|
search
|
|
|
|
input
|
|
|
|
output
|
2022-12-10 14:11:13 +01:00
|
|
|
(error :output)
|
2022-12-24 14:39:53 +01:00
|
|
|
(external-format :default)
|
2022-12-10 14:11:13 +01:00
|
|
|
#+sbcl (if-output-exists :supersede)
|
|
|
|
#+sbcl (if-error-exists :supersede))
|
2022-03-20 12:32:41 +01:00
|
|
|
(declare (ignorable search))
|
|
|
|
#+ecl (ext:run-program program
|
|
|
|
args
|
|
|
|
:input input
|
|
|
|
:output output
|
|
|
|
:error error
|
|
|
|
:wait wait)
|
|
|
|
#+sbcl (sb-ext:run-program program
|
2022-12-10 14:11:13 +01:00
|
|
|
args
|
|
|
|
:wait wait
|
|
|
|
:search search
|
|
|
|
:input input
|
|
|
|
:output output
|
|
|
|
:error error
|
2022-12-24 14:39:53 +01:00
|
|
|
:external-format external-format
|
2022-12-10 14:11:13 +01:00
|
|
|
:if-output-exists if-output-exists
|
|
|
|
:if-error-exists if-error-exists))
|
2021-05-09 16:32:32 +02:00
|
|
|
|
2021-09-12 12:16:29 +02:00
|
|
|
(defun process-exit-code (process)
|
2022-03-20 12:32:41 +01:00
|
|
|
#+ecl (nth-value 1 (ext:external-process-status process))
|
|
|
|
#+sbcl (sb-ext:process-exit-code process))
|
2021-09-12 12:16:29 +02:00
|
|
|
|
|
|
|
(defun process-exit-success-p (process)
|
|
|
|
(= (process-exit-code process) 0))
|
|
|
|
|
2023-07-07 14:45:05 +02:00
|
|
|
(defun process-output-stream (process)
|
|
|
|
(process-output process))
|
|
|
|
|
2022-12-24 14:39:53 +01:00
|
|
|
(defmacro gen-process-stream (name)
|
|
|
|
`(defun ,(misc:format-fn-symbol t "process-~a" name) (process)
|
|
|
|
(,(misc:format-fn-symbol 'sb-ext "process-~a" name) process)))
|
|
|
|
|
|
|
|
(gen-process-stream output)
|
|
|
|
|
|
|
|
(gen-process-stream input)
|
|
|
|
|
|
|
|
(gen-process-stream error)
|
|
|
|
|
2020-05-08 15:45:43 +02:00
|
|
|
(defun open-with-editor (file)
|
|
|
|
(multiple-value-bind (exe args)
|
|
|
|
(external-editor)
|
2020-09-18 21:43:13 +02:00
|
|
|
(let ((actual-args (if args
|
2020-10-23 20:57:17 +02:00
|
|
|
(text-utils:split-words args)
|
2020-09-18 21:43:13 +02:00
|
|
|
nil)))
|
2021-05-09 16:32:32 +02:00
|
|
|
(run-external-program exe
|
|
|
|
(append actual-args
|
|
|
|
(list file))
|
|
|
|
:search t
|
|
|
|
:wait t
|
|
|
|
:input t
|
|
|
|
:output t
|
|
|
|
:error t))))
|
2020-05-08 15:45:43 +02:00
|
|
|
|
|
|
|
(defun exit-program (&optional (exit-code 0))
|
|
|
|
(uiop:quit exit-code))
|
|
|
|
|
|
|
|
(defun user-cache-dir (&rest more)
|
|
|
|
(fs:pathname->namestring (apply #'uiop:xdg-cache-home
|
|
|
|
(append (list +program-name+) more))))
|
|
|
|
|
|
|
|
(defun cached-file-path (filename)
|
|
|
|
(text-utils:strcat (user-cache-dir) fs:*directory-sep* filename))
|
2020-10-11 18:22:07 +02:00
|
|
|
|
|
|
|
(defun generate-ssl-certificate (outdir)
|
|
|
|
(let* ((cert-file (text-utils:strcat outdir fs:*directory-sep* +ssl-cert-name+))
|
|
|
|
(key-file (text-utils:strcat outdir fs:*directory-sep* +ssl-key-name+))
|
|
|
|
(cmd-args (format nil
|
2022-01-16 16:49:00 +01:00
|
|
|
(text-utils:strcat "req -new -nodes -x509 -days 365 -subj / "
|
2020-10-11 18:22:07 +02:00
|
|
|
"-keyout ~a -outform PEM -out ~a")
|
|
|
|
key-file
|
|
|
|
cert-file)))
|
2021-05-09 16:32:32 +02:00
|
|
|
(run-external-program +openssl-bin+
|
|
|
|
(text-utils:split-words cmd-args)
|
|
|
|
:output nil
|
|
|
|
:error :output)
|
2020-10-11 18:22:07 +02:00
|
|
|
(values cert-file key-file)))
|
2020-12-30 12:24:13 +01:00
|
|
|
|
2024-02-04 14:57:49 +01:00
|
|
|
(defun change-ssl-key-passphrase (keypath old-passphrase new-passphrase)
|
|
|
|
(fs:with-anaphoric-temp-file (stream :unlink t)
|
|
|
|
(with-input-from-string (passphrase-stream new-passphrase)
|
|
|
|
(let* ((cmd-args (format nil
|
|
|
|
(text-utils:strcat "rsa -aes256 -in ~a -out ~a"
|
|
|
|
" -passin pass:~a -passout stdin")
|
|
|
|
keypath
|
|
|
|
fs:temp-file
|
|
|
|
old-passphrase
|
|
|
|
new-passphrase)))
|
|
|
|
(let ((output-string (misc:make-fresh-array 0 #\a 'character nil)))
|
|
|
|
(with-output-to-string (output-stream output-string)
|
|
|
|
(let ((process (run-external-program +openssl-bin+
|
|
|
|
(text-utils:split-words cmd-args)
|
|
|
|
:input passphrase-stream
|
|
|
|
:output output-stream
|
|
|
|
:error :output
|
|
|
|
:wait t)))
|
|
|
|
(if (process-exit-success-p process)
|
|
|
|
(fs:copy-a-file fs:temp-file keypath :overwrite t)
|
|
|
|
(error "error changing passphrase for key ~a ~a"
|
|
|
|
keypath
|
|
|
|
output-string)))))))))
|
|
|
|
|
2024-02-14 14:41:45 +01:00
|
|
|
(defun ssl-key-has-empty-password-p (key-path)
|
2024-02-17 11:05:58 +01:00
|
|
|
(with-input-from-string (passphrase-stream (format nil "~%"))
|
|
|
|
(let* ((cmd-args (format nil "rsa -passin stdin -noout -text -in ~a" key-path))
|
|
|
|
(process (run-external-program +openssl-bin+
|
|
|
|
(text-utils:split-words cmd-args)
|
|
|
|
:input passphrase-stream
|
|
|
|
:output nil
|
2024-02-17 14:03:39 +01:00
|
|
|
:error nil
|
2024-02-17 11:05:58 +01:00
|
|
|
:wait t)))
|
|
|
|
(process-exit-success-p process))))
|
2024-02-14 14:41:45 +01:00
|
|
|
|
2020-12-30 12:24:13 +01:00
|
|
|
(defun send-to-pipe (data program-and-args)
|
|
|
|
(croatoan:end-screen)
|
|
|
|
(with-input-from-string (stream data)
|
|
|
|
(let ((command-line-splitted (text-utils:split-words program-and-args)))
|
2021-05-09 16:32:32 +02:00
|
|
|
(run-external-program (first command-line-splitted)
|
|
|
|
(rest command-line-splitted)
|
|
|
|
:search t
|
|
|
|
:wait t
|
|
|
|
:input stream
|
|
|
|
:output t
|
|
|
|
:error t))))
|
2021-02-10 17:43:40 +01:00
|
|
|
|
2022-01-08 13:18:45 +01:00
|
|
|
(defun open-link-with-program (program-and-args link &key (wait nil))
|
2021-02-10 18:02:06 +01:00
|
|
|
(let* ((command-line-splitted (text-utils:split-words program-and-args))
|
|
|
|
(program (first command-line-splitted))
|
|
|
|
(args (append (rest command-line-splitted)
|
|
|
|
(list link))))
|
2021-05-09 16:32:32 +02:00
|
|
|
(run-external-program program
|
|
|
|
args
|
|
|
|
:search t
|
2022-01-08 13:18:45 +01:00
|
|
|
:wait wait
|
2021-05-09 16:32:32 +02:00
|
|
|
:output nil
|
|
|
|
:error :output)))
|
2021-03-28 14:33:56 +02:00
|
|
|
|
2022-01-08 13:18:45 +01:00
|
|
|
(defun open-resource-with-external-program (resource give-focus-to-message-window
|
|
|
|
&key (open-for-edit nil))
|
2022-01-08 19:03:41 +01:00
|
|
|
(flet ((edit (file)
|
2022-01-06 17:59:22 +01:00
|
|
|
(croatoan:end-screen)
|
2022-01-08 19:03:41 +01:00
|
|
|
(os-utils:open-with-editor file)))
|
2023-06-24 12:01:31 +02:00
|
|
|
(alexandria::when-let* ((parsed-as-iri (iri:iri-parse resource :null-on-error t))
|
2023-06-24 11:46:49 +02:00
|
|
|
(parsed-no-fragment (iri:remove-fragment parsed-as-iri)))
|
|
|
|
(let ((program (if (iri:absolute-url-p resource)
|
|
|
|
(swconf:link-regex->program-to-use (text-utils:to-s parsed-no-fragment))
|
|
|
|
(swconf:link-regex->program-to-use resource))))
|
|
|
|
(if program
|
|
|
|
(cond
|
|
|
|
((swconf:use-editor-as-external-program-p program)
|
|
|
|
(edit resource))
|
|
|
|
((swconf:use-tinmop-as-external-program-p program)
|
|
|
|
(if open-for-edit
|
|
|
|
(edit resource)
|
|
|
|
(gemini-viewer:load-gemini-url resource
|
|
|
|
:give-focus-to-message-window
|
|
|
|
give-focus-to-message-window)))
|
|
|
|
(t
|
|
|
|
(os-utils:open-link-with-program program resource :wait open-for-edit)))
|
|
|
|
(if open-for-edit
|
2023-07-15 14:30:09 +02:00
|
|
|
(error (_ "No program defined in configuration file to edit this kind of files"))
|
2023-06-24 11:46:49 +02:00
|
|
|
(os-utils:xdg-open resource)))))))
|
2021-08-20 14:23:21 +02:00
|
|
|
|
2023-01-12 20:09:01 +01:00
|
|
|
(defun open-resource-with-tinmop-p (resource)
|
|
|
|
(alexandria:when-let ((program (swconf:link-regex->program-to-use resource)))
|
|
|
|
(swconf:use-tinmop-as-external-program-p program)))
|
|
|
|
|
2023-07-07 14:45:05 +02:00
|
|
|
(defgeneric file->mime-type (object))
|
|
|
|
|
|
|
|
(defmethod file->mime-type ((object string))
|
|
|
|
(let ((process (run-external-program +file-bin+
|
|
|
|
(list "-E" "--mime-type" "--brief" object)
|
|
|
|
:search t
|
|
|
|
:wait t
|
|
|
|
:output :stream
|
|
|
|
:error nil)))
|
|
|
|
(if (process-exit-success-p process)
|
|
|
|
(text-utils:trim-blanks (read-line (process-output-stream process)))
|
|
|
|
nil)))
|
|
|
|
|
|
|
|
(defmethod file->mime-type ((object pathname))
|
|
|
|
(file->mime-type (fs:pathname->namestring object)))
|
|
|
|
|
2021-08-20 14:23:21 +02:00
|
|
|
(defun unzip-file (zip-file destination-dir)
|
|
|
|
(cond
|
|
|
|
((not (fs:file-exists-p zip-file))
|
|
|
|
(error (format nil (_ "File ~s does not exists"))))
|
|
|
|
((not (fs:directory-exists-p destination-dir))
|
|
|
|
(error (format nil (_ "Destination directory ~s does not exists"))))
|
|
|
|
(t
|
|
|
|
(run-external-program +unzip-bin+
|
|
|
|
(list "-o" zip-file "-d" destination-dir)
|
|
|
|
:search t
|
|
|
|
:wait t
|
2021-08-22 12:56:35 +02:00
|
|
|
:output nil
|
|
|
|
:error :output))))
|
|
|
|
|
|
|
|
(defun unzip-single-file (zip-file file-entry)
|
|
|
|
(with-output-to-string (stream)
|
2022-12-24 14:39:53 +01:00
|
|
|
(let* ((process (run-external-program +unzip-bin+
|
|
|
|
(list "-p" zip-file file-entry)
|
|
|
|
:search t
|
|
|
|
:wait t
|
|
|
|
:output stream
|
|
|
|
:error :output)))
|
2022-03-20 12:32:41 +01:00
|
|
|
(when (not (process-exit-success-p process))
|
2022-12-24 14:39:53 +01:00
|
|
|
(error (format nil (_ "File ~s extraction from ~s failed") file-entry zip-file))))))
|
2022-03-04 14:36:11 +01:00
|
|
|
|
|
|
|
(defun copy-to-clipboard (text)
|
|
|
|
(trivial-clipboard:text text))
|