utils.lisp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. (in-package :cl-user)
  2. (defpackage chatikbot.utils
  3. (:use :cl)
  4. (:export :*admins*
  5. :*bot-name*
  6. :*hooks*
  7. :run-hooks
  8. :add-hook
  9. :remove-hook
  10. :keyify
  11. :dekeyify
  12. :*settings*
  13. :defsetting
  14. :*backoff-start*
  15. :*backoff-max*
  16. :loop-with-error-backoff
  17. :replace-all
  18. :aget
  19. :agets
  20. :mappend
  21. :random-elt
  22. :flatten
  23. :preprocess-input
  24. :punctuation-p
  25. :read-from-string-no-punct
  26. :print-with-spaces
  27. :spaced
  28. :http-request
  29. :xml-request
  30. :get-by-tag
  31. :select-text
  32. :trim-nil
  33. :text-with-cdata
  34. :child-text
  35. :clean-text
  36. :json-request
  37. :plist-hash
  38. :plist-json
  39. :format-ts
  40. :parse-cmd
  41. :parse-float
  42. :smart-f
  43. :format-size
  44. :format-interval
  45. :symbol-append
  46. :message-id
  47. :from-id
  48. :chat-id
  49. :text
  50. :cmd
  51. :args
  52. :query-id
  53. :from
  54. :raw-data
  55. :message
  56. :data
  57. :section
  58. :code
  59. :error
  60. :raw-state
  61. :state
  62. :inline-message-id
  63. :hook
  64. :headers
  65. :paths))
  66. (in-package #:chatikbot.utils)
  67. (defvar *admins* nil "Admins chat-ids")
  68. (defvar *bot-name* nil "bot name to properly handle text input")
  69. (defvar *hooks* (make-hash-table) "Hooks storage")
  70. (defun run-hooks (event &rest arguments)
  71. (let ((hooks (gethash event *hooks*)))
  72. (labels ((try-handle (func)
  73. (apply func arguments)))
  74. (unless (some #'try-handle hooks)
  75. (log:info "unhandled" event arguments)))))
  76. (defun add-hook (event hook &optional append)
  77. (let ((existing (gethash event *hooks*)))
  78. (unless (member hook existing)
  79. (setf (gethash event *hooks*)
  80. (if append (append existing (list hook))
  81. (cons hook existing))))))
  82. (defun remove-hook (event hook)
  83. (setf (gethash event *hooks*)
  84. (remove hook (gethash event *hooks*))))
  85. (defun keyify (key)
  86. (intern (string-upcase (substitute #\- #\_ key)) :keyword))
  87. (defun dekeyify (keyword &optional preserve-dash)
  88. (let ((text (string-downcase (string keyword))))
  89. (if preserve-dash text (substitute #\_ #\- text))))
  90. ;; Settings
  91. (defvar *settings* nil "List of plugin's settings symbols")
  92. (defmacro defsetting (var &optional val doc)
  93. `(progn (defvar ,var ,val ,doc)
  94. (push ',var *settings*)))
  95. (defvar *backoff-start* 1 "Initial back-off")
  96. (defvar *backoff-max* 64 "Maximum back-off delay")
  97. (defun loop-with-error-backoff (func)
  98. (let ((backoff *backoff-start*))
  99. (loop
  100. do
  101. (handler-case
  102. (progn
  103. (funcall func)
  104. (setf backoff *backoff-start*))
  105. (error (e)
  106. (log:error e)
  107. (log:info "Backing off for" backoff)
  108. (sleep backoff)
  109. (setf backoff (min *backoff-max*
  110. (* 2 backoff))))
  111. (usocket:timeout-error (e)
  112. (log:error e)
  113. (log:info "Backing off for" backoff)
  114. (sleep backoff)
  115. (setf backoff (min *backoff-max*
  116. (* 2 backoff))))))))
  117. (defun replace-all (string part replacement &key (test #'char=))
  118. "Returns a new string in which all the occurences of the part
  119. is replaced with replacement."
  120. (with-output-to-string (out)
  121. (loop with part-length = (length part)
  122. for old-pos = 0 then (+ pos part-length)
  123. for pos = (search part string
  124. :start2 old-pos
  125. :test test)
  126. do (write-string string out
  127. :start old-pos
  128. :end (or pos (length string)))
  129. when pos do (write-string replacement out)
  130. while pos)))
  131. (defmacro aget (key alist)
  132. `(cdr (assoc ,key ,alist :test #'equal)))
  133. (defun agets (alist &rest keys)
  134. (reduce #'(lambda (a k) (aget k a)) keys :initial-value alist))
  135. (defun mappend (fn &rest lists)
  136. "Apply fn to each element of lists and append the results."
  137. (apply #'append (apply #'mapcar fn lists)))
  138. (defun random-elt (choices)
  139. "Choose an element from a list at random."
  140. (elt choices (random (length choices))))
  141. (defun flatten (the-list)
  142. "Append together elements (or lists) in the list."
  143. (mappend #'(lambda (x) (if (listp x) (flatten x) (list x))) the-list))
  144. (defun preprocess-input (text)
  145. (when text
  146. (let* ((text (subseq text (if (equal (char text 0) #\/) 1 0)))
  147. (first-space (position #\Space text))
  148. (first-word (subseq text 0 first-space)))
  149. (if (equal first-word *bot-name*)
  150. (preprocess-input (subseq text (1+ first-space)))
  151. (replace-all text *bot-name* "ты")))))
  152. (defun print-with-spaces (list)
  153. (format nil "~@(~{~a~^ ~}~)" list))
  154. (defun parse-cmd (text)
  155. (let* ((args (split-sequence:split-sequence #\Space (subseq text 1) :remove-empty-subseqs t))
  156. (cmd (subseq (car args) 0 (position #\@ (car args)))))
  157. (values (intern (string-upcase cmd) "KEYWORD") (rest args))))
  158. (defun spaced (list)
  159. (format nil "~{~A~^ ~}" list))
  160. (defun http-default (url &optional parameters)
  161. (let* ((uri (quri:uri url))
  162. (userinfo (quri:uri-userinfo uri)))
  163. (when parameters
  164. (let ((query (quri:url-encode-params parameters :encoding :utf-8)))
  165. (setf (quri:uri-query uri)
  166. (if (and (quri:uri-query uri)
  167. (string-not-equal (quri:uri-query uri) ""))
  168. (concatenate 'string (quri:uri-query uri) "&" query)
  169. query))))
  170. (when userinfo
  171. (setf (quri:uri-userinfo uri) nil))
  172. (unless (quri:uri-scheme uri)
  173. (setf (quri:uri-scheme uri) "http"))
  174. (values uri userinfo)))
  175. (defun http-request (url &rest args &key method version parameters content headers basic-auth cookie-jar keep-alive use-connection-pool (max-redirects 5) timeout force-binary want-stream ssl-key-file ssl-cert-file ssl-key-password stream verbose proxy insecure ca-path user-agent)
  176. (declare (ignore method version content basic-auth cookie-jar keep-alive use-connection-pool max-redirects timeout force-binary want-stream ssl-key-file ssl-cert-file ssl-key-password stream verbose proxy insecure ca-path))
  177. (multiple-value-bind (uri userinfo)
  178. (http-default url parameters)
  179. (when userinfo
  180. (push (cons :authorization (concatenate 'string "Basic "
  181. (base64:string-to-base64-string userinfo)))
  182. headers))
  183. (when user-agent
  184. (push (cons :user-agent user-agent) headers)
  185. (remf args :user-agent))
  186. (remf args :parameters)
  187. (remf args :headers)
  188. (apply #'dex:request uri :headers headers args)))
  189. ;; XML processing
  190. (defun xml-request (url &rest args &key method parameters content headers basic-auth cookie-jar keep-alive use-connection-pool timeout ssl-key-file ssl-cert-file ssl-key-password stream verbose proxy insecure ca-path user-agent encoding)
  191. (declare (ignore method parameters headers content basic-auth cookie-jar keep-alive use-connection-pool timeout ssl-key-file ssl-cert-file ssl-key-password stream verbose proxy insecure ca-path user-agent))
  192. (remf args :encoding)
  193. (multiple-value-bind (raw-body status headers uri)
  194. (apply #'http-request url :force-binary t args)
  195. (let ((encoding
  196. (or
  197. ;; 1. Provided encoding
  198. encoding
  199. ;; 2. Content-type header
  200. (ignore-errors
  201. (let ((ct (gethash "content-type" headers)))
  202. (subseq ct (1+ (position #\= ct)))))
  203. ;; 3. Parse first 1000 bytes
  204. (ignore-errors
  205. (let ((dom (plump:parse (flex:octets-to-string
  206. (subseq raw-body 0 (1+ (position (char-code #\>) raw-body :start 1000)))))))
  207. (or
  208. ;; 3.1 Content-type from http-equiv
  209. (ignore-errors
  210. (let ((ct (loop for meta in (get-by-tag dom "meta")
  211. for http-equiv = (plump:get-attribute meta "http-equiv")
  212. for content = (plump:get-attribute meta "content")
  213. when (equal http-equiv "Content-Type")
  214. return content)))
  215. (subseq ct (1+ (position #\= ct)))))
  216. ;; 3.2 'content' xml node attribute
  217. (ignore-errors (plump:get-attribute (plump:first-child dom) "encoding")))))
  218. ;; 4. Default 'utf-8'
  219. "utf-8")))
  220. (values
  221. (handler-bind ((flex:external-format-encoding-error
  222. (lambda (c) (use-value #\? c))))
  223. (plump:parse
  224. (flex:octets-to-string raw-body :external-format (intern encoding 'keyword))))
  225. status headers uri))))
  226. (defun get-by-tag (node tag)
  227. (nreverse (org.shirakumo.plump.dom::get-elements-by-tag-name node tag)))
  228. (defun select-text (node &optional selector)
  229. (ignore-errors
  230. (when selector (setf node (elt (clss:select selector node) 0)))
  231. (plump:traverse node #'(lambda (n) (setf (plump:text n) ""))
  232. :test #'plump:comment-p)
  233. (plump:text (plump:strip node))))
  234. (defun trim-nil (text)
  235. (when text
  236. (let ((text (string-trim " " text)))
  237. (unless (zerop (length text))
  238. text))))
  239. (defun text-with-cdata (node)
  240. "Compiles all text nodes within the nesting-node into one string."
  241. (with-output-to-string (stream)
  242. (labels ((r (node)
  243. (loop for child across (plump:children node)
  244. do (typecase child
  245. (plump:text-node (write-string (plump:text child) stream))
  246. (plump:cdata (write-string (plump:text child) stream))
  247. (plump:nesting-node (r child))))))
  248. (r node))))
  249. (defun child-text (node tag)
  250. (alexandria:when-let (child (car (get-by-tag node tag)))
  251. (trim-nil (text-with-cdata child))))
  252. (defun clean-text (text)
  253. (when text (trim-nil (plump:text (plump:parse text)))))
  254. ;; JSON processing
  255. (defun json-request (url &rest args &key method parameters content headers basic-auth cookie-jar keep-alive use-connection-pool timeout ssl-key-file ssl-cert-file ssl-key-password stream verbose proxy insecure ca-path user-agent (object-as :alist))
  256. (declare (ignore method parameters basic-auth cookie-jar keep-alive use-connection-pool timeout ssl-key-file ssl-cert-file ssl-key-password stream verbose proxy insecure ca-path user-agent))
  257. (remf args :object-as)
  258. (when content
  259. (push (cons :content-type "application/json") headers))
  260. (multiple-value-bind (body status headers uri)
  261. (apply #'http-request url args)
  262. (unless (stringp body)
  263. (setf body (babel:octets-to-string body :encoding :utf-8)))
  264. (values (yason:parse body :object-as object-as) status headers uri)))
  265. (defun plist-hash (plist &optional skip-nil (format-key #'identity) &rest hash-table-initargs)
  266. (cond
  267. ((and (consp plist) (keywordp (car plist)))
  268. (let ((table (apply #'make-hash-table hash-table-initargs)))
  269. (do ((tail plist (cddr tail)))
  270. ((not tail))
  271. (let ((key (funcall format-key (car tail)))
  272. (value (cadr tail)))
  273. (when (or value (not skip-nil))
  274. (setf (gethash key table)
  275. (if (listp value)
  276. (apply #'plist-hash value skip-nil format-key hash-table-initargs)
  277. value)))))
  278. table))
  279. ((consp plist)
  280. (loop for item in plist collect (apply #'plist-hash item skip-nil format-key hash-table-initargs)))
  281. (:default plist)))
  282. (defmethod yason:encode ((object (eql 'f)) &optional (stream *standard-output*))
  283. (write-string "false" stream)
  284. object)
  285. (defun plist-json (plist)
  286. (with-output-to-string (stream)
  287. (yason:encode (plist-hash plist t #'dekeyify) stream)))
  288. (defun format-ts (ts)
  289. (local-time:format-timestring nil ts
  290. :format '(:year "-" (:month 2) "-" (:day 2) " "
  291. (:hour 2) ":" (:min 2) ":" (:sec 2))))
  292. (defun parse-float (string)
  293. (let ((*read-eval* nil))
  294. (with-input-from-string (stream string)
  295. (read stream nil nil))))
  296. (defun smart-f (arg &optional digits)
  297. (with-output-to-string (s)
  298. (prin1 (cond ((= (round arg) arg) (round arg))
  299. (digits (float (/ (round (* arg (expt 10 digits)))
  300. (expt 10 digits))))
  301. (t arg))
  302. s)))
  303. (defun format-size (bytes)
  304. (cond
  305. ((< bytes 512) (smart-f bytes))
  306. ((< bytes (* 512 1024)) (format nil "~A KiB" (smart-f (/ bytes 1024) 1)))
  307. ((< bytes (* 512 1024 1024)) (format nil "~A MiB" (smart-f (/ bytes 1024 1024) 1)))
  308. ((< bytes (* 512 1024 1024 1024)) (format nil "~A GiB" (smart-f (/ bytes 1024 1024 1024) 1)))
  309. (:otherwise (format nil "~A TiB" (smart-f (/ bytes 1024 1024 1024 1024) 1)))))
  310. (defun format-interval (seconds)
  311. (cond
  312. ((< seconds 60) (format nil "~A sec" seconds))
  313. ((< seconds (* 60 60)) (format nil "~A mins" (round seconds 60)))
  314. ((< seconds (* 60 60 24)) (format nil "~A hours" (round seconds (* 60 60))))
  315. ((< seconds (* 60 60 24 7)) (format nil "~A days" (round seconds (* 60 60 24))))
  316. ((< seconds (* 60 60 24 7 54)) (format nil "~A weeks" (round seconds (* 60 60 24 7))))
  317. (:otherwise (format nil "~A years" (smart-f (/ seconds (* 60 60 24 365.25)) 1)))))
  318. (defun symbol-append (&rest symbols)
  319. (intern (apply #'concatenate 'string
  320. (mapcar #'symbol-name symbols))))
  321. ;; Fix bug in local-time (following symlinks in /usr/share/zoneinfo/
  322. ;; leads to bad cutoff)
  323. (in-package #:local-time)
  324. (defun reread-timezone-repository (&key (timezone-repository *default-timezone-repository-path*))
  325. (check-type timezone-repository (or pathname string))
  326. (multiple-value-bind (valid? error)
  327. (ignore-errors
  328. (truename timezone-repository)
  329. t)
  330. (unless valid?
  331. (error "REREAD-TIMEZONE-REPOSITORY was called with invalid PROJECT-DIRECTORY (~A). The error is ~A."
  332. timezone-repository error)))
  333. (let* ((root-directory timezone-repository)
  334. (cutoff-position (length (princ-to-string root-directory))))
  335. (flet ((visitor (file)
  336. (handler-case
  337. (let* ((full-name (subseq (princ-to-string file) cutoff-position))
  338. (name (pathname-name file))
  339. (timezone (%realize-timezone (make-timezone :path file :name name))))
  340. (setf (gethash full-name *location-name->timezone*) timezone)
  341. (map nil (lambda (subzone)
  342. (push timezone (gethash (subzone-abbrev subzone)
  343. *abbreviated-subzone-name->timezone-list*)))
  344. (timezone-subzones timezone)))
  345. (invalid-timezone-file () nil))))
  346. (setf *location-name->timezone* (make-hash-table :test 'equal))
  347. (setf *abbreviated-subzone-name->timezone-list* (make-hash-table :test 'equal))
  348. (cl-fad:walk-directory root-directory #'visitor :directories nil :follow-symlinks nil
  349. :test (lambda (file)
  350. (not (find "Etc" (pathname-directory file) :test #'string=))))
  351. (cl-fad:walk-directory (merge-pathnames "Etc/" root-directory) #'visitor :directories nil))))
  352. (let ((zonepath "/usr/share/zoneinfo/"))
  353. (when (directory zonepath)
  354. (local-time:reread-timezone-repository :timezone-repository zonepath)))