about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/elpa/gntp-20141025.250/gntp.el
blob: d7c729fafc5723746ebb704db8ddc6eff42db09a (plain) (blame)
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
240
241
242
243
;;; gntp.el --- Growl Notification Protocol for Emacs -*- lexical-binding: t -*-

;; Author: Engelke Eschner <tekai@gmx.li>
;; Version: 0.1
;; Package-Version: 20141025.250
;; Created: 2013-03-21

;; LICENSE
;; Copyright (c) 2013 Engelke Eschner
;; All rights reserved.

;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;;     * Redistributions of source code must retain the above copyright
;;       notice, this list of conditions and the following disclaimer.
;;     * Redistributions in binary form must reproduce the above
;;       copyright notice, this list of conditions and the following
;;       disclaimer in the documentation and/or other materials provided
;;       with the distribution.
;;     * Neither the name of the gntp.el nor the names of its
;;       contributors may be used to endorse or promote products derived
;;       from this software without specific prior written permission.

;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT
;; HOLDER> BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
;; EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
;; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
;; OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


;;; Commentary:
;; This package implements the Growl Notification Protocol GNTP
;; described at http://www.growlforwindows.com/gfw/help/gntp.aspx
;; It is incomplete as it only lets you send but not receive
;; notifications.

;;; Code:

(defgroup gntp nil
  "GNTP, send/register growl notifications via GNTP from within emacs."
  :group 'external)

(defcustom gntp-application-name "Emacs/gntp.el"
  "Name of the application gntp registers itself."
  :type '(string))

(defcustom gntp-application-icon nil
  "Icon to display as the application icon.
Either a URL or a path to a file."
  :type '(string))

(defcustom gntp-server "localhost"
  "Default port of the server.
Standard says can't be changed, but port-forwarding etc."
  :type '(string))

(defcustom gntp-server-port 23053
  "Default port of the server.
Standard says can't be changed, but port-forwarding etc."
  :type '(integer))

(defcustom gntp-register-alist nil
  "Registration item list."
  :type '(choice string (const nil)))

(defun gntp-register (&optional notifications server  port)
  (interactive)
  "Register NOTIFICATIONS at SERVER:PORT.
PORT defaults to `gntp-server-port'."
  (let ((message (gntp-build-message-register (if notifications notifications gntp-register-alist))))
    (gntp-send message (if server server gntp-server) port)))

;;;###autoload
(defun gntp-notify (name title text server &optional port priority icon)
  "Send notification NAME with TITLE, TEXT, PRIORITY and ICON to SERVER:PORT.
PORT defaults to `gntp-server-port'"
  (let ((message (gntp-build-message-notify name title text priority icon)))
    (gntp-send message server port)))

(defun gntp-build-message-register (notifications)
  "Build the message to register NOTIFICATIONS types."
  (let ((lines (list "GNTP/1.0 REGISTER NONE"
                     (format "Application-Name: %s"
                             gntp-application-name)
                     (format "Notifications-Count: %d"
                             (length notifications))))
        (icon-uri (gntp-app-icon-uri))
        (icon-data (gntp-app-icon-data))
        (icons (list)))

    ;; append icon uri
    (when icon-uri
      (nconc lines (list (format "Application-Icon: %s" icon-uri)))
      ;; and data when it exists
      (when icon-data
        (setq icons (cons icon-data icons))))

    (dolist (notice notifications)
      ;; "For each notification being registered:
      ;; Each notification being registered should be seperated by a
      ;; blank line, including the first notification
      (nconc lines (cons "" (gntp-notification-lines notice)))
      ;; c
      (let ((icon (gntp-notice-icon-data notice)))
        (when icon
          (nconc icons (list "" icon)))))

    ;; icon data must come last
    (when icons
      (nconc lines (cons "" icons)))

    (mapconcat 'identity (remove nil lines) "\r\n")))

(defun gntp-notification-lines (notice)
  "Transform NOTICE into a list of strings."
  (let ((display-name (gntp-notice-get notice :display))
        (enabled (gntp-notice-get notice :enabled))
        (icon-uri (gntp-notice-icon-uri notice)))
  (list
   ;; Required - The name (type) of the notification being registered
   (concat "Notification-Name: " (gntp-notice-name notice))
   ;; Optional - The name of the notification that is displayed to
   ;; the user (defaults to the same value as Notification-Name)
   (when display-name
     (concat "Notification-Display-Name: " display-name))
   ;; Optional - Indicates if the notification should be enabled by
   ;; default (defaults to False)
   (when enabled
     "Notification-Enabled: True")
   ;; Optional - The default icon to use for notifications of this type
   (when icon-uri
     (concat "Notification-Icon: " icon-uri)))))

(defun gntp-build-message-notify (name title text &optional priority icon)
  "Build a message of type NAME with TITLE and TEXT."

  (format
   "GNTP/1.0 NOTIFY NONE\r\n\
Application-Name: %s\r\n\
Notification-Name: %s\r\n\
Notification-Title: %s\r\n\
Notification-Text: %s\r\n\
Notification-Priority: %s\r\n\
Notification-Icon: %s\r\n\
\r\n"
          gntp-application-name
          (if (symbolp name) (symbol-name name) name)
          title
          ;; no CRLF in the text to avoid accidentel msg end
          (replace-regexp-in-string "\r\n" "\n" text)
          (if priority priority "0")
          (if icon (gntp-icon-uri icon) "")))

;; notice
;;(list name ; everthing else is optional
;;      :display "name to display"
;;      :enabled nil
;;      :icon "url or file")


(defun gntp-notice-icon-uri (notice)
  "Get the icon URI from NOTICE."
  (gntp-icon-uri (gntp-notice-get notice :icon)))

(defun gntp-notice-icon-data (notice)
  "Get icon data from NOTICE."
  (gntp-icon-data (gntp-notice-get notice :icon)))

(defun gntp-app-icon-uri ()
  "Return the value to be used in the Application-Icon header."
  (gntp-icon-uri gntp-application-icon))

(defun gntp-app-icon-data ()
  "Return the value to be used in the Application-Icon header."
  (gntp-icon-data gntp-application-icon))

(defun gntp-icon-uri (icon)
  "Get the URI of ICON."
  (when icon
    (cond ((string-equal (substring icon 0 7) "http://") icon)
          ((and (file-exists-p icon) (file-readable-p icon))
           (concat "x-growl-resource://" (md5 icon))))))

(defun gntp-icon-data (icon)
  "Get the URI of ICON."
  (when (and icon (not (string-equal (substring icon 0 7) "http://"))
             (file-exists-p icon) (file-readable-p icon))
    (let ((id (md5 icon))
          (data (gntp-file-string icon)))
      (format "Identifier: %s\r\nLength: %d\r\n\r\n%s"
              id (length data) data))))

(defun gntp-notice-name (notice)
  "Get the name of NOTICE.  The name must be either a symbol or string."
  (let ((name (car notice)))
    (if (symbolp name)
        (symbol-name name)
      name)))

(defun gntp-notice-get (notice property)
  "Get PROPERTY from NOTICE."
  (plist-get (cdr notice) property))

(defun gntp-send (message server &optional port)
  "Send MESSAGE to SERVER:PORT.  PORT defaults to `gntp-server-port'."
  (let ((proc (make-network-process
               :name "gntp"
               :host server
               :server nil
               :service (if port port gntp-server-port)
               ;;:sentinel 'gntp-sentinel
               :filter 'gntp-filter)))
    ;; hmm one CRLF too much?
    (process-send-string proc (concat message "\r\n\r\n\r\n"))))

(defun gntp-filter (proc string)
  "Filter for PROC started by `gntp-send'.
Argument STRING reply from the server."
  (when (string-equal "GNTP/1.0 -ERROR" (substring string 0 15))
    (error "GNTP: Something went wrong take a look at the reply:\n %s"
           string)))

;; (defun gntp-sentinel (proc msg)
;;   (when (string= msg "connection broken by remote peer\n")
;;     (message (format "client %s has quit" proc))))


(defun gntp-file-string (file)
  "Read the contents of a FILE and return as a string."
  (with-temp-buffer
    (insert-file-contents-literally file)
    (buffer-string)))

(provide 'gntp)

;;; gntp.el ends here