utils.lisp 19 KB

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