forked from emacs-slack/emacs-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack-message-sender.el
169 lines (143 loc) · 5.77 KB
/
slack-message-sender.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
;;; slack-message-sender.el --- slack message concern message sending -*- lexical-binding: t; -*-
;; Copyright (C) 2015 yuya.minami
;; Author: yuya.minami <[email protected]>
;; Keywords:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'eieio)
(require 'json)
(require 'slack-websocket)
(require 'slack-im)
(require 'slack-group)
(require 'slack-message)
(require 'slack-channel)
(defvar slack-message-minibuffer-local-map nil)
(defvar slack-buffer-function)
(defun slack-message-send ()
(interactive)
(slack-message--send (slack-message-read-from-minibuffer)))
(defun slack-message-inc-id (team)
(with-slots (message-id) team
(if (eq message-id (1- most-positive-fixnum))
(setq message-id 1)
(cl-incf message-id))))
(defun slack-escape-message (message)
"Escape '<,' '>' & '&' in MESSAGE."
(replace-regexp-in-string
">" ">"
(replace-regexp-in-string
"<" "<"
(replace-regexp-in-string "&" "&" message))))
(defun slack-link-users (message team)
"Add links to all references to valid users in MESSAGE."
(replace-regexp-in-string
"@\\<\\([A-Za-z0-9.-_]+\\)"
#'(lambda (text)
(let* ((username (match-string 1 text))
(id (slack-user-get-id username team)))
(if id
(format "<@%s|%s>" id username)
(cond
((string= username "here") "<!here|here>")
((cl-find username '("channel" "group") :test #'string=) "<!channel>")
((string= username "everyone") "<!everyone>")
(t text)))))
message t))
(defun slack-link-channels (message team)
"Add links to all references to valid channels in MESSAGE."
(let ((channel-ids
(mapcar #'(lambda (x)
(let ((channel (cdr x)))
(cons (slack-room-name channel) (slot-value channel 'id))))
(slack-channel-names team))))
(replace-regexp-in-string
"#\\<\\([A-Za-z0-9.-_]+\\)"
#'(lambda (text)
(let* ((channel (match-string 1 text))
(id (cdr (assoc channel channel-ids))))
(if id
(format "<#%s|%s>" id channel)
text)))
message t)))
(defun slack-message-prepare-links (message team)
(slack-link-channels (slack-link-users message team) team))
(defun slack-message--send (message)
(if slack-current-team-id
(let* ((team (slack-team-find slack-current-team-id))
(message (slack-message-prepare-links (slack-escape-message message) team)))
(slack-message-inc-id team)
(with-slots (message-id sent-message self-id) team
(let* ((m (list :id message-id
:channel (slack-message-get-room-id)
:type "message"
:user self-id
:text message))
(json (json-encode m))
(obj (slack-message-create m team)))
(slack-ws-send json team)
(puthash message-id obj sent-message))))
(error "Call from Slack Buffer")))
(defun slack-message-get-room-id ()
(if (and (boundp 'slack-current-room-id)
(boundp 'slack-current-team-id))
(oref (slack-room-find slack-current-room-id
(slack-team-find slack-current-team-id))
id)
(oref (slack-message-read-room (slack-team-select)) id)))
(defun slack-message-read-room (team)
(let* ((list (slack-message-room-list team))
(choices (mapcar #'car list))
(room-name (slack-message-read-room-list "Select Room: " choices))
(room (cdr (cl-assoc room-name list :test #'string=))))
room))
(defun slack-message-read-room-list (prompt choices)
(let ((completion-ignore-case t))
(completing-read (format "%s" prompt)
choices nil t nil nil choices)))
(defun slack-message-room-list (team)
(append (slack-group-names team)
(slack-im-names team)
(slack-channel-names team)))
(defun slack-message-read-from-minibuffer ()
(let ((prompt "Message: "))
(slack-message-setup-minibuffer-keymap)
(read-from-minibuffer
prompt
nil
slack-message-minibuffer-local-map)))
(defun slack-message-setup-minibuffer-keymap ()
(unless slack-message-minibuffer-local-map
(setq slack-message-minibuffer-local-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") 'newline)
(set-keymap-parent map minibuffer-local-map)
map))))
(defun slack-message-embed-channel ()
(interactive)
(let ((team (slack-team-select)))
(let* ((alist (slack-channel-names team)))
(slack-select-from-list
(alist "Select Channel: ")
(insert (concat "#" (slack-room-name selected)))))))
(defun slack-message-embed-mention ()
(interactive)
(let ((team (slack-team-select)))
(let* ((pre-defined (list (list "here" :name "here")
(list "channel" :name "channel")))
(alist (append pre-defined (slack-user-names team))))
(slack-select-from-list
(alist "Select User: ")
(insert (concat "@" (plist-get selected :name)))))))
(provide 'slack-message-sender)
;;; slack-message-sender.el ends here