macros.lisp 6.3 KB

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