-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgrowl.el
239 lines (201 loc) · 7.39 KB
/
growl.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
;;; growl.el --- Send message to Growl(include clones and network).
;; Copyright (C) 2008 Takeru Naito
;; Author: Takeru Naito <[email protected]>
;; Keywords: growl
;; This file 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, or (at your option)
;; any later version.
;; This file 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 GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; * Description
;;
;; growl.el Send message to Growl(include clones and network).
;;
;; * Usage
;;
;; Just put the code like below into your .emacs:
;;
;; (require 'growl)
;;
;; and...
;;
;; (growl "Hello World.")
;;
;;; Change Log:
;; 2008-12-27:
;; * Added mumbles-send support.
;;
;; 2008-12-23:
;; * Initial import.
;;; Code:
(require 'bindat)
(or (require 'hmac-md5 nil t)
(and (require 'hex-util nil t)
(defun md5-binary (string)
(decode-hex-string (md5 string)))))
;; vars
(defconst growl-udp-port 9887)
(defconst growl-protocol-version 1)
(defconst growl-type-registration 0)
(defconst growl-type-notification 1)
(defvar growl-application-name "Emacs")
(defvar growl-default-title "Emacs")
(defvar growl-udp-all-notifes (list "Emacs Notification"))
(defvar growl-udp-default-notifies growl-udp-all-notifes)
(defvar growl-udp-registered-p nil)
(defvar growl-udp-registration-before-wait 1)
(defvar growl-udp-registration-after-wait 1)
(defvar growl-udp-password nil)
(defvar growl-udp-process nil)
(defvar growl-notify-function
(cond
((executable-find "growlnotify") 'growl-growlnotify)
((executable-find "mumbles-send") 'growl-mumbles-send)
((executable-find "whinesend") 'growl-whinesend)
(t 'growl-udp-send-generic)))
;; UDP packet definitions
(defun growl-udp-registration-packet ()
(let*
((growl-network-registration-format
'((:version u8)
(:type u8)
(:app-name-length u16)
(:num-all-notifications u8)
(:num-default-notifications u8)))
(packet
(concat
(bindat-pack
growl-network-registration-format
`((:version . ,growl-protocol-version)
(:type . ,growl-type-registration)
(:app-name-length . ,(length growl-application-name))
(:num-all-notifications . ,(length growl-udp-all-notifes))
(:num-default-notifications . ,(length growl-udp-default-notifies))))
growl-application-name
(mapconcat (lambda (notify)
(concat (bindat-pack
'((:length u16))
`((:length . ,(length notify))))
notify))
growl-udp-all-notifes nil)
(mapconcat (lambda (notify)
(when (member notify growl-udp-all-notifes)
(bindat-pack
'((:length u8))
`((:length . ,(- (length growl-udp-all-notifes)
(length (member notify growl-udp-all-notifes))))))))
growl-udp-default-notifies nil)))
(checksum (md5-binary (concat packet growl-udp-password))))
(concat packet checksum)))
(defun growl-udp-notification-packet (name title description &optional priority sticky)
(let*
((growl-network-notification-format
'((:version u8)
(:type u8)
(:flags u16)
(:name-length u16)
(:title-length u16)
(:description-length u16)
(:app-name-length u16)))
(priority (or priority 0))
(flags (logior 0 (lsh 1 (logand #x7 priority))))
(packet
(concat
(bindat-pack
growl-network-notification-format
`((:version . ,growl-protocol-version)
(:type . ,growl-type-notification)
(:flags . ,(if sticky (logior 1 flags) flags))
(:name-length . ,(length name))
(:title-length . ,(length title))
(:description-length . ,(length description))
(:app-name-length . ,(length growl-application-name))))
name
title
description
growl-application-name))
(checksum (md5-binary (concat packet growl-udp-password))))
(concat packet checksum)))
(defun growl-udp-make-network-process ()
(make-network-process
:name growl-application-name
:host 'local
:type 'datagram
:service growl-udp-port))
(defun growl-udp-make-netcat-process ()
(condition-case nil
(let ((process-connection-type nil))
(start-process "growl-nc" " *growl-netcat*"
"nc" "-u" "localhost"
(int-to-string growl-udp-port)))
(error "No usable send program")))
(defun growl-udp-get-or-create-process ()
(unless (memq growl-udp-process (process-list))
(setq growl-udp-process
(cond
((eq growl-notify-function 'growl-udp-send-udp)
(growl-udp-make-network-process))
((eq growl-notify-function 'growl-udp-send-netcat)
(growl-udp-make-netcat-process))
((eq growl-notify-function 'growl-udp-send-generic)
(condition-case nil
(growl-udp-make-network-process)
((error "Unsupported connection type")
(growl-udp-make-netcat-process)))))))
growl-udp-process)
(defun growl-udp-send (str)
(let
((process-connection-type nil)
(conn (growl-udp-get-or-create-process)))
(process-send-string conn str)))
(defun growl-udp-registration ()
(growl-udp-send (growl-udp-registration-packet)))
;; External client settings.
(defun growl-growlnotify (message)
(start-process "growlnotify" " *growlnotify*"
"growlnotify"
"--name" "Emacs"
"--appIcon" "Emacs"
"--message" message))
(defun growl-mumbles-send (message)
(start-process "mumbles-send" " *mumbles-send*"
"mumbles-send"
growl-default-title
message))
(defun growl-whinesend (message)
(start-process "whinesend" " *whinesend*"
"whinesend"
"-t" growl-default-title
"-i" (car command-line-args)
"-m" message))
;; UDP client settings.
(defun growl-udp-send-generic (message)
(unless growl-udp-registered-p
(sit-for growl-udp-registration-before-wait)
(growl-udp-registration)
(sit-for growl-udp-registration-after-wait)
(setq growl-udp-registered-p t))
(growl-udp-send
(growl-udp-notification-packet (car growl-udp-all-notifes)
growl-default-title
message)))
(defalias 'growl-udp-send-udp 'growl-udp-send-generic)
(defalias 'growl-udp-send-netcat 'growl-udp-send-generic)
;; main
(defun growl (message)
(funcall growl-notify-function message))
(provide 'growl)
;;; growl.el ends here
;; Local Variables:
;; mode: emacs-lisp
;; coding: utf-8-unix
;; indent-tabs-mode: nil
;; End: