;;; pterodactyl.el --- some pterodactyl api things i use -*- lexical-binding: t -*- ;; Author: kpm ;; Created: 16 Sep 2024 ;; Keywords: network, API ;; URL: https://kelp.krzysckh.org ;; ;; Copyright (C) 2024 kpm ;; ;; Redistribution and use in source and binary forms, with or without ;; modification, are permitted provided that the following conditions are ;; met: ;; ;; * Redistributions of source code must retain the above copyright ;; notice, this list of conditions and the following disclaimer. ;; * Redistributions in binary form must reproduce the above ;; copyright notice, this list of conditions and the following disclaimer ;; in the documentation and/or other materials provided with the ;; distribution. ;; ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ;; ;; This file is not part of GNU Emacs. ;;; Code: (require 'request) (require 'f) (require 'json) (defun p//maybe-read (fname) (when (f-exists-p fname) (f-read-text fname))) ;; yeah (defvar p//api-key (p//maybe-read "~/txt/pt-api-key")) (defvar p//api-server (p//maybe-read "~/txt/pt-api-server")) (defvar p//server (p//maybe-read "~/txt/pt-server")) (defun p//json-read-l (&rest r) (let ((json-array-type 'list)) (apply #'json-read r))) (defun p/backup () (request (concat p//api-server "/client/servers/" p//server "/backups") :type "POST" :parser #'p//json-read-l :headers `(("Authorization" . ,(concat "Bearer " p//api-key))) :complete (cl-function (lambda (&key data &allow-other-keys) (message "%s" data)))) t) (defun p//b-show-backups (data) (let ((buf (get-buffer-create "*Pterodactyl Backups*"))) (with-current-buffer buf (erase-buffer) (--map (let ((a (cdr (assoc 'attributes it)))) (insert (format "%s: %s\n" (cdr (assoc 'name a)) (cdr (assoc 'is_successful a))))) (cdr (assoc 'data data))) (switch-to-buffer buf)))) (defun p//request (endpt type cb &rest data) (apply #'request (append (list (concat p//api-server "/client/servers/" p//server endpt) :type type :parser #'p//json-read-l :headers `(("Authorization" . ,(concat "Bearer " p//api-key))) :complete (cl-function (lambda (&key data &allow-other-keys) (when cb (funcall cb data))))) data)) t) (defun p/show-backups (&optional cb) (interactive) (when (interactive-p) (when (null cb) (setq cb #'p//b-show-backups))) (request (concat p//api-server "/client/servers/" p//server "/backups") :type "GET" :parser #'p//json-read-l :headers `(("Authorization" . ,(concat "Bearer " p//api-key))) :complete (cl-function (lambda (&key data &allow-other-keys) (when cb (funcall cb data))))) t) (defun p/execute (command &optional cb) (interactive) (p//request "/command" "POST" #'(lambda (data) (when cb (funcall cb data))) :headers `(("Authorization" . ,(concat "Bearer " p//api-key))) :data `(("command" . ,command)))) (defun p/resources (cb) (p//request "/resources" "GET" cb)) (defun p/players (cb) (p//request "/resources" "GET" #'(lambda (data) (funcall cb (cdr (assoc 'PlayerList (cdr (assoc 'minecraft (cdr (assoc 'attributes data)))))))))) (provide 'pterodactyl)