]> git.sesse.net Git - vlc/blob - modules/misc/notify/growl.c
59b38e8eb3db12e58407d8238e97f5970cbc5e38
[vlc] / modules / misc / notify / growl.c
1 /*****************************************************************************
2  * growl.c : growl notification plugin
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jérôme Decoodt <djc -at- videolan -dot- org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc_interface.h>
31 #include <vlc_playlist.h>
32 #include <vlc_meta.h>
33 #include <vlc_network.h>
34 #include <errno.h>
35 #include <vlc_md5.h>
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int  Open    ( vlc_object_t * );
41 static void Close   ( vlc_object_t * );
42
43 static int ItemChange( vlc_object_t *, const char *,
44                        vlc_value_t, vlc_value_t, void * );
45
46 static int RegisterToGrowl( vlc_object_t *p_this );
47 static int NotifyToGrowl( vlc_object_t *p_this, char *psz_type,
48                             char *psz_title, char *psz_desc );
49 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset );
50 #define GROWL_MAX_LENGTH 256
51
52 /*****************************************************************************
53  * Module descriptor
54  ****************************************************************************/
55
56 /// \bug [String] REmove all "Growl" in short desc
57
58 #define SERVER_DEFAULT "127.0.0.1"
59 #define SERVER_TEXT N_("Growl server")
60 #define SERVER_LONGTEXT N_("This is the host to which Growl notifications " \
61    "will be sent. By default, notifications are sent locally." )
62 #define PASS_DEFAULT ""
63 #define PASS_TEXT N_("Growl password")
64 /// \bug [String] Password on the Growl server.
65 #define PASS_LONGTEXT N_("Growl password on the server.")
66 #define PORT_TEXT N_("Growl UDP port")
67 /// \bug [String] UDP port on the Growl server
68 #define PORT_LONGTEXT N_("Growl UDP port on the server.")
69
70 vlc_module_begin();
71     set_category( CAT_INTERFACE );
72     set_subcategory( SUBCAT_INTERFACE_CONTROL );
73     set_shortname( "Growl" );
74     set_description( _("Growl Notification Plugin") );
75
76     add_string( "growl-server", SERVER_DEFAULT, NULL,
77                 SERVER_TEXT, SERVER_LONGTEXT, VLC_FALSE );
78     add_string( "growl-password", PASS_DEFAULT, NULL,
79                 PASS_TEXT, PASS_LONGTEXT, VLC_FALSE );
80     add_integer( "growl-port", 9887, NULL,
81                 PORT_TEXT, PORT_LONGTEXT, VLC_TRUE );
82
83     set_capability( "interface", 0 );
84     set_callbacks( Open, Close );
85 vlc_module_end();
86
87 /*****************************************************************************
88  * Open: initialize and create stuff
89  *****************************************************************************/
90 static int Open( vlc_object_t *p_this )
91 {
92     intf_thread_t *p_intf = (intf_thread_t *)p_this;
93
94     playlist_t *p_playlist = pl_Yield( p_intf );
95     var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
96     pl_Release( p_intf );
97
98     RegisterToGrowl( p_this );
99     return VLC_SUCCESS;
100 }
101
102 /*****************************************************************************
103  * Close: destroy interface stuff
104  *****************************************************************************/
105 static void Close( vlc_object_t *p_this )
106 {
107     playlist_t *p_playlist = pl_Yield( p_this );
108     var_DelCallback( p_playlist, "playlist-current", ItemChange, p_this );
109     pl_Release( p_this );
110 }
111
112 /*****************************************************************************
113  * ItemChange: Playlist item change callback
114  *****************************************************************************/
115 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
116                        vlc_value_t oldval, vlc_value_t newval, void *param )
117 {
118     char psz_tmp[GROWL_MAX_LENGTH];
119     char *psz_title = NULL;
120     char *psz_artist = NULL;
121     char *psz_album = NULL;
122     input_thread_t *p_input;
123     playlist_t *p_playlist = pl_Yield( p_this );
124
125     p_input = p_playlist->p_input;
126     vlc_object_release( p_playlist );
127
128     if( !p_input ) return VLC_SUCCESS;
129     vlc_object_yield( p_input );
130
131     if( p_input->b_dead || !input_GetItem(p_input)->psz_name )
132     {
133         /* Not playing anything ... */
134         vlc_object_release( p_input );
135         return VLC_SUCCESS;
136     }
137
138     /* Playing something ... */
139     psz_artist = input_GetItem(p_input)->p_meta->psz_artist ?
140                   strdup( input_GetItem(p_input)->p_meta->psz_artist ) :
141                   strdup( "" );
142     psz_album = input_GetItem(p_input)->p_meta->psz_album ?
143                   strdup( input_GetItem(p_input)->p_meta->psz_album ) :
144                   strdup( "" );
145     psz_title = strdup( input_GetItem(p_input)->psz_name );
146     if( psz_title == NULL ) psz_title = strdup( N_("(no title)") );
147     if( psz_artist == NULL ) psz_artist = strdup( N_("(no artist)") );
148     if( psz_album == NULL ) psz_album = strdup( N_("(no album)") );
149     snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s %s %s",
150               psz_title, psz_artist, psz_album );
151     free( psz_title );
152     free( psz_artist );
153     free( psz_album );
154
155     NotifyToGrowl( p_this, "Now Playing", "Now Playing", psz_tmp );
156
157     vlc_object_release( p_input );
158     return VLC_SUCCESS;
159 }
160
161 /*****************************************************************************
162  * Growl specific functions
163  *****************************************************************************/
164 #define GROWL_PROTOCOL_VERSION (1)
165 #define GROWL_TYPE_REGISTRATION (0)
166 #define GROWL_TYPE_NOTIFICATION (1)
167 #define APPLICATION_NAME "VLC media player"
168
169 #define insertstrlen( psz ) \
170 { \
171     uint16_t i_size = strlen( psz ); \
172     psz_encoded[i++] = (i_size>>8)&0xFF; \
173     psz_encoded[i++] = i_size&0xFF; \
174 }
175 /*****************************************************************************
176  * RegisterToGrowl
177  *****************************************************************************/
178 static int RegisterToGrowl( vlc_object_t *p_this )
179 {
180     uint8_t *psz_encoded = malloc(100);
181     uint8_t i_defaults = 0;
182     char *psz_notifications[] = {"Now Playing", NULL};
183     vlc_bool_t pb_defaults[] = {VLC_TRUE, VLC_FALSE};
184     int i = 0, j;
185     if( psz_encoded == NULL )
186         return VLC_FALSE;
187
188     memset( psz_encoded, 0, sizeof(psz_encoded) );
189     psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
190     psz_encoded[i++] = GROWL_TYPE_REGISTRATION;
191     insertstrlen(APPLICATION_NAME);
192     i+=2;
193     strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
194     i += strlen(APPLICATION_NAME);
195     for( j = 0 ; psz_notifications[j] != NULL ; j++)
196     {
197         insertstrlen(psz_notifications[j]);
198         strcpy( (char*)(psz_encoded+i), psz_notifications[j] );
199         i += strlen(psz_notifications[j]);
200     }
201     psz_encoded[4] = j;
202     for( j = 0 ; psz_notifications[j] != NULL ; j++)
203         if(pb_defaults[j] == VLC_TRUE)
204         {
205             psz_encoded[i++] = (uint8_t)j;
206             i_defaults++;
207         }
208     psz_encoded[5] = i_defaults;
209
210     CheckAndSend(p_this, psz_encoded, i);
211     free( psz_encoded );
212     return VLC_SUCCESS;
213 }
214
215 static int NotifyToGrowl( vlc_object_t *p_this, char *psz_type,
216                             char *psz_title, char *psz_desc )
217 {
218     uint8_t *psz_encoded = malloc(GROWL_MAX_LENGTH + 42);
219     uint16_t flags;
220     int i = 0;
221     if( psz_encoded == NULL )
222         return VLC_FALSE;
223
224     memset( psz_encoded, 0, sizeof(psz_encoded) );
225     psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
226     psz_encoded[i++] = GROWL_TYPE_NOTIFICATION;
227     flags = 0;
228     psz_encoded[i++] = (flags>>8)&0xFF;
229     psz_encoded[i++] = flags&0xFF;
230     insertstrlen(psz_type);
231     insertstrlen(psz_title);
232     insertstrlen(psz_desc);
233     insertstrlen(APPLICATION_NAME);
234     strcpy( (char*)(psz_encoded+i), psz_type );
235     i += strlen(psz_type);
236     strcpy( (char*)(psz_encoded+i), psz_title );
237     i += strlen(psz_title);
238     strcpy( (char*)(psz_encoded+i), psz_desc );
239     i += strlen(psz_desc);
240     strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
241     i += strlen(APPLICATION_NAME);
242
243     CheckAndSend(p_this, psz_encoded, i);
244     free( psz_encoded );
245     return VLC_SUCCESS;
246 }
247
248 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset )
249 {
250     int i, i_handle;
251     struct md5_s md5;
252     intf_thread_t *p_intf = (intf_thread_t *)p_this;
253     char *psz_password = config_GetPsz( p_intf, "growl-password" );
254     char *psz_server = config_GetPsz( p_intf, "growl-server" );
255     int i_port = config_GetInt( p_intf, "growl-port" );
256     strcpy( (char*)(p_data+i_offset), psz_password );
257     i = i_offset + strlen(psz_password);
258
259     InitMD5( &md5 );
260     AddMD5( &md5, p_data, i );
261     EndMD5( &md5 );
262
263     for( i = 0 ; i < 4 ; i++ )
264     {
265         md5.p_digest[i] = md5.p_digest[i];
266         p_data[i_offset++] =  md5.p_digest[i]     &0xFF;
267         p_data[i_offset++] = (md5.p_digest[i]>> 8)&0xFF;
268         p_data[i_offset++] = (md5.p_digest[i]>>16)&0xFF;
269         p_data[i_offset++] = (md5.p_digest[i]>>24)&0xFF;
270     }
271
272     i_handle = net_ConnectUDP( p_this, psz_server, i_port, 0 );
273     if( i_handle == -1 )
274     {
275          msg_Err( p_this, "failed to open a connection (udp)" );
276          free( psz_password);
277          free( psz_server);
278          return VLC_EGENERIC;
279     }
280
281     shutdown( i_handle, SHUT_RD );
282     if( send( i_handle, p_data, i_offset, 0 )
283           == -1 )
284     {
285         msg_Warn( p_this, "send error: %s", strerror(errno) );
286     }
287     net_Close( i_handle );
288
289     free( psz_password);
290     free( psz_server);
291     return VLC_SUCCESS;
292 }
293
294 #undef GROWL_PROTOCOL_VERSION
295 #undef GROWL_TYPE_REGISTRATION
296 #undef GROWL_TYPE_NOTIFICATION
297 #undef APPLICATION_NAME
298 #undef insertstrlen