macros.lisp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. (in-package :cl-user)
  2. (defpackage chatikbot.macros
  3. (:use :cl :chatikbot.utils :chatikbot.telegram :chatikbot.crypto)
  4. (:export :def-db-init
  5. :with-parsed-message
  6. :def-message-handler
  7. :def-message-cmd-handler
  8. :def-message-admin-cmd-handler
  9. :with-parsed-callback
  10. :def-callback-handler
  11. :def-callback-section-handler
  12. :def-oauth-handler
  13. :def-oauth-section-handler
  14. :with-random-state
  15. :with-chat-in-list
  16. :defcron))
  17. (in-package #:chatikbot.macros)
  18. (defmacro def-db-init (&body body)
  19. `(add-hook :db-init #'(lambda ()
  20. (handler-case (progn ,@body)
  21. (error (e) (log:error e)))
  22. (values))))
  23. (defmacro with-parsed-message (message &body body)
  24. `(let* ((*message* ,message)
  25. (*message-id* (agets *message* "message_id"))
  26. (*from-id* (agets *message* "from" "id"))
  27. (*chat-id* (agets *message* "chat" "id"))
  28. (*text* (agets *message* "text")))
  29. ,@body))
  30. (defmacro def-message-handler (name (&optional prio) &body body)
  31. (alexandria:with-gensyms (g-message)
  32. `(progn
  33. (defun ,name (,g-message)
  34. (with-parsed-message ,g-message
  35. (handler-case (progn ,@body)
  36. (error (e)
  37. (log:error "~A" e)
  38. (bot-send-message (format nil "Ошибочка вышла~@[: ~A~]"
  39. (when (is-admin) e)))))))
  40. (when ,prio (setf (get ',name :prio) ,prio))
  41. (add-hook :update-message ',name))))
  42. (defmacro def-message-cmd-handler (name (&rest commands) &body body)
  43. `(def-message-handler ,name ()
  44. (when (and *text* (equal #\/ (char *text* 0)))
  45. (multiple-value-bind (*cmd* *args*) (parse-cmd *text*)
  46. (when (member *cmd* (list ,@commands))
  47. (log:info *cmd* *message-id* *chat-id* *from-id* *args*)
  48. ,@body
  49. t)))))
  50. (defmacro def-message-admin-cmd-handler (name (&rest commands) &body body)
  51. `(def-message-handler ,name ()
  52. (when (and (is-admin)
  53. *text* (equal #\/ (char *text* 0)))
  54. (multiple-value-bind (*cmd* *args*) (parse-cmd *text*)
  55. (when (member *cmd* (list ,@commands))
  56. (log:info *cmd* *message-id* *chat-id* *from-id* *args*)
  57. ,@body
  58. t)))))
  59. (defmacro with-chat-in-list (list-id &body body)
  60. `(if (member *chat-id* (chatikbot.db:lists-get ,list-id))
  61. (progn ,@body)
  62. (bot-send-message "А вот хуй!")))
  63. (defmacro with-parsed-callback (callback &body body)
  64. `(let* ((*callback* ,callback)
  65. (*query-id* (agets *callback* "id"))
  66. (*from* (agets *callback* "from"))
  67. (*raw-data* (agets *callback* "data"))
  68. (*source-message* (agets *callback* "message"))
  69. (*inline-message-id* (agets *callback* "inline_message_id"))
  70. (*from-id* (agets *from* "id"))
  71. (*source-chat-id* (agets *source-message* "chat" "id"))
  72. (*source-message-id* (agets *source-message* "message_id"))
  73. (*chat-id* *source-chat-id*))
  74. ,@body))
  75. (defmacro def-callback-handler (name (&optional prio) &body body)
  76. (alexandria:with-gensyms (g-callback)
  77. `(progn
  78. (defun ,name (,g-callback)
  79. (with-parsed-callback ,g-callback
  80. (handler-case (progn ,@body)
  81. (error (e)
  82. (log:error "~A" e)
  83. (bot-send-message (format nil "Ошибочка вышла~@[: ~A~]"
  84. (when (is-admin) e))
  85. :chat-id (or *source-chat-id* *from-id*))))))
  86. (when ,prio (setf (get ',name :prio) ,prio))
  87. (add-hook :update-callback-query ',name))))
  88. (defmacro def-callback-section-handler (name (&rest sections) &body body)
  89. `(def-callback-handler ,name ()
  90. (when *source-chat-id*
  91. (multiple-value-bind (*data* *section*) (decode-callback-data *source-chat-id* *raw-data*)
  92. (when (member *section* (list ,@sections))
  93. (log:info *query-id* *from-id* *source-chat-id* *source-message-id* *section* *data*)
  94. ,@body
  95. t)))))
  96. (defmacro def-oauth-handler (name (code error state &optional prio) &body body)
  97. `(progn
  98. (defun ,name (,code ,error ,state)
  99. (declare (ignorable ,code ,error ,state))
  100. (handler-case (progn ,@body)
  101. (error (e)
  102. (log:error "~A" e)
  103. (hunchentoot:redirect "/error"))))
  104. (when ,prio (setf (get ',name :prio) ,prio))
  105. (add-hook :oauth ',name)))
  106. (defmacro def-oauth-section-handler (name (&rest sections) &body body)
  107. `(def-oauth-handler ,name (*code* *error* *raw-state*)
  108. (multiple-value-bind (*state* *section*) (decode-oauth-state *raw-state*)
  109. (when (member *section* (list ,@sections))
  110. ,@body
  111. t))))
  112. ;; On CCL at least, new thread clones origin *random-state* thus
  113. ;; all cron threads generate same random values. To overcome this we'll
  114. ;; generate per-thread *random-state*
  115. (defvar *random-state-lock* (bt:make-recursive-lock
  116. "random state lock") "per-thread random state lock")
  117. (defvar *thread-random-state* (tg:make-weak-hash-table :weakness :key) "Per-thread *random-state* storage")
  118. (defun get-thread-random-state ()
  119. (bt:with-recursive-lock-held (*random-state-lock*)
  120. (let ((self (bt:current-thread)))
  121. (or (gethash self *thread-random-state*)
  122. (setf (gethash self *thread-random-state*)
  123. (make-random-state t))))))
  124. (defmacro with-random-state (&body body)
  125. `(let ((*random-state* (get-thread-random-state)))
  126. ,@body))
  127. ;; Schedule
  128. (defmacro defcron (name (&rest schedule) &body body)
  129. (let ((schedule (or schedule '(:minute '* :hour '*)))
  130. (scheduler (symbol-append name '-scheduler)))
  131. `(progn
  132. (defun ,name ()
  133. (with-random-state
  134. (unwind-protect
  135. (handler-case
  136. #+sbcl (sb-sys:with-interrupts ,@body)
  137. #-sbcl (progn ,@body)
  138. (error (e) (log:error "~A" e)))
  139. (dex:clear-connection-pool))))
  140. (defun ,scheduler ()
  141. (clon:schedule-function
  142. ',name (clon:make-scheduler
  143. (clon:make-typed-cron-schedule
  144. ,@schedule)
  145. :allow-now-p t)
  146. :name ',name
  147. :thread (bt:make-thread (lambda () (loop (sleep 1)))
  148. :name (format nil "Sleeper '~A'" (symbol-name ',name))))
  149. (values))
  150. (add-hook :starting ',scheduler))))