]> git.sesse.net Git - vlc/blob - modules/misc/notify/growl_udp.c
dd434803c53eb031ad1d036b28966773d083ba74
[vlc] / modules / misc / notify / growl_udp.c
1 /*****************************************************************************
2  * growl_udp.c : growl UDP notification plugin
3  *****************************************************************************
4  * Copyright (C) 2006 - 2010 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
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_interface.h>
35 #include <vlc_playlist.h>
36 #include <vlc_meta.h>
37 #include <vlc_network.h>
38 #include <vlc_md5.h>
39
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 static int  Open    ( vlc_object_t * );
44 static void Close   ( vlc_object_t * );
45
46 static int ItemChange( vlc_object_t *, const char *,
47                        vlc_value_t, vlc_value_t, void * );
48
49 static int RegisterToGrowl( vlc_object_t *p_this );
50 static int NotifyToGrowl( vlc_object_t *p_this, const char *psz_desc );
51 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset, size_t is_ze );
52 #define GROWL_MAX_LENGTH 256
53
54 /*****************************************************************************
55  * Module descriptor
56  ****************************************************************************/
57
58 #define SERVER_DEFAULT "127.0.0.1"
59 #define SERVER_TEXT N_("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_("Password")
64 #define PASS_LONGTEXT N_("Growl password on the Growl server.")
65 #define PORT_TEXT N_("UDP port")
66 #define PORT_LONGTEXT N_("Growl UDP port on the Growl server.")
67
68 vlc_module_begin ()
69     set_category( CAT_INTERFACE )
70     set_subcategory( SUBCAT_INTERFACE_CONTROL )
71     set_shortname( "Growl-UDP" )
72     set_description( N_("Growl UDP Notification Plugin") )
73
74     add_string( "growl-server", SERVER_DEFAULT, NULL,
75                 SERVER_TEXT, SERVER_LONGTEXT, false )
76     add_password( "growl-password", PASS_DEFAULT, NULL,
77                 PASS_TEXT, PASS_LONGTEXT, false )
78     add_integer( "growl-port", 9887, NULL,
79                 PORT_TEXT, PORT_LONGTEXT, true )
80
81     set_capability( "interface", 0 )
82     set_callbacks( Open, Close )
83 vlc_module_end ()
84
85 /*****************************************************************************
86  * Open: initialize and create stuff
87  *****************************************************************************/
88 static int Open( vlc_object_t *p_this )
89 {
90     var_AddCallback( pl_Get( p_this ), "item-current", ItemChange, p_this );
91     RegisterToGrowl( p_this );
92     return VLC_SUCCESS;
93 }
94
95 /*****************************************************************************
96  * Close: destroy interface stuff
97  *****************************************************************************/
98 static void Close( vlc_object_t *p_this )
99 {
100     var_DelCallback( pl_Get( p_this ), "item-current", ItemChange, p_this );
101 }
102
103 /*****************************************************************************
104  * ItemChange: Playlist item change callback
105  *****************************************************************************/
106 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
107                        vlc_value_t oldval, vlc_value_t newval, void *param )
108 {
109     VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(newval);
110     VLC_UNUSED(param);
111     char psz_tmp[GROWL_MAX_LENGTH];
112     char *psz_title = NULL;
113     char *psz_artist = NULL;
114     char *psz_album = NULL;
115     input_thread_t *p_input;
116     p_input = playlist_CurrentInput( (playlist_t*)p_this );
117
118     if( !p_input ) return VLC_SUCCESS;
119
120     char *psz_name = input_item_GetName( input_GetItem( p_input ) );
121     if( p_input->b_dead || !psz_name )
122     {
123         /* Not playing anything ... */
124         free( psz_name );
125         vlc_object_release( p_input );
126         return VLC_SUCCESS;
127     }
128     free( psz_name );
129
130     /* Playing something ... */
131     input_item_t *p_item = input_GetItem( p_input );
132
133     psz_title = input_item_GetTitleFbName( p_item );
134     if( EMPTY_STR( psz_title ) )
135     {
136         free( psz_title );
137         vlc_object_release( p_input );
138         return VLC_SUCCESS;
139     }
140
141     psz_artist = input_item_GetArtist( p_item );
142     if( EMPTY_STR( psz_artist ) ) FREENULL( psz_artist );
143     psz_album = input_item_GetAlbum( p_item ) ;
144     if( EMPTY_STR( psz_album ) ) FREENULL( psz_album );
145
146     if( psz_artist && psz_album )
147         snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s\n%s [%s]",
148                 psz_title, psz_artist, psz_album );
149     else if( psz_artist )
150         snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s\n%s", psz_title, psz_artist );
151     else
152         snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s", psz_title );
153
154     free( psz_title );
155     free( psz_artist );
156     free( psz_album );
157
158     NotifyToGrowl( p_this, psz_tmp );
159
160     vlc_object_release( p_input );
161     return VLC_SUCCESS;
162 }
163
164 /*****************************************************************************
165  * Growl specific functions
166  *****************************************************************************/
167 #define GROWL_PROTOCOL_VERSION (1)
168 #define GROWL_TYPE_REGISTRATION (0)
169 #define GROWL_TYPE_NOTIFICATION (1)
170 #define APPLICATION_NAME "VLC media player"
171
172 #define insertstrlen( psz ) \
173 { \
174     uint16_t i_size = strlen( psz ); \
175     psz_encoded[i++] = (i_size>>8)&0xFF; \
176     psz_encoded[i++] = i_size&0xFF; \
177 }
178 /*****************************************************************************
179  * RegisterToGrowl
180  *****************************************************************************/
181 static int RegisterToGrowl( vlc_object_t *p_this )
182 {
183     uint8_t *psz_encoded = calloc( 100, 1 );
184     uint8_t i_defaults = 0;
185     static const char *psz_notifications[] = {"Now Playing", NULL};
186     bool pb_defaults[] = {true, false};
187     int i = 0, j;
188     if( psz_encoded == NULL )
189         return false;
190
191     psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
192     psz_encoded[i++] = GROWL_TYPE_REGISTRATION;
193     insertstrlen(APPLICATION_NAME);
194     i+=2;
195     strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
196     i += strlen(APPLICATION_NAME);
197     for( j = 0 ; psz_notifications[j] != NULL ; j++)
198     {
199         insertstrlen(psz_notifications[j]);
200         strcpy( (char*)(psz_encoded+i), psz_notifications[j] );
201         i += strlen(psz_notifications[j]);
202     }
203     psz_encoded[4] = j;
204     for( j = 0 ; psz_notifications[j] != NULL ; j++)
205     {
206         if(pb_defaults[j] == true)
207         {
208             psz_encoded[i++] = (uint8_t)j;
209             i_defaults++;
210         }
211     }
212     psz_encoded[5] = i_defaults;
213
214     CheckAndSend(p_this, psz_encoded, i, 100);
215     free( psz_encoded );
216     return VLC_SUCCESS;
217 }
218
219 static int NotifyToGrowl( vlc_object_t *p_this, const char *psz_desc )
220 {
221     const char *psz_type = "Now Playing", *psz_title = "Now Playing";
222     uint8_t *psz_encoded = calloc(GROWL_MAX_LENGTH + 42, 1);
223     uint16_t flags;
224     int i = 0;
225     if( psz_encoded == NULL )
226         return false;
227
228     psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
229     psz_encoded[i++] = GROWL_TYPE_NOTIFICATION;
230     flags = 0;
231     psz_encoded[i++] = (flags>>8)&0xFF;
232     psz_encoded[i++] = flags&0xFF;
233     insertstrlen(psz_type);
234     insertstrlen(psz_title);
235     insertstrlen(psz_desc);
236     insertstrlen(APPLICATION_NAME);
237     strcpy( (char*)(psz_encoded+i), psz_type );
238     i += strlen(psz_type);
239     strcpy( (char*)(psz_encoded+i), psz_title );
240     i += strlen(psz_title);
241     strcpy( (char*)(psz_encoded+i), psz_desc );
242     i += strlen(psz_desc);
243     strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
244     i += strlen(APPLICATION_NAME);
245
246     CheckAndSend(p_this, psz_encoded, i, GROWL_MAX_LENGTH + 42);
247     free( psz_encoded );
248     return VLC_SUCCESS;
249 }
250
251 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset, size_t i_size )
252 {
253     int i_handle;
254     struct md5_s md5;
255     char *psz_password = var_InheritString( p_this, "growl-password" );
256     char *psz_server = var_InheritString( p_this, "growl-server" );
257     int i_port = var_InheritInteger( p_this, "growl-port" );
258
259     if(!psz_password || !psz_server)
260         goto error;
261
262     int i_password_length = strlen( psz_password );
263     // Check that the buffer is larger enought for the string and the md5
264     if( i_offset + i_password_length + 4*4 >= i_size )
265         goto error;
266
267     strcpy( (char*)(p_data+i_offset), psz_password );
268
269     InitMD5( &md5 );
270     AddMD5( &md5, p_data, i_offset + i_password_length );
271     EndMD5( &md5 );
272
273     for( int i = 0 ; i < 4 ; i++ )
274     {
275         p_data[i_offset++] =  md5.p_digest[i]     &0xFF;
276         p_data[i_offset++] = (md5.p_digest[i]>> 8)&0xFF;
277         p_data[i_offset++] = (md5.p_digest[i]>>16)&0xFF;
278         p_data[i_offset++] = (md5.p_digest[i]>>24)&0xFF;
279     }
280
281     i_handle = net_ConnectUDP( p_this, psz_server, i_port, -1 );
282     if( i_handle == -1 )
283     {
284         msg_Err( p_this, "failed to open a connection (udp)" );
285         goto error;
286     }
287
288     shutdown( i_handle, SHUT_RD );
289     if( send( i_handle, p_data, i_offset, 0 ) == -1 )
290     {
291         msg_Warn( p_this, "send error: %m" );
292     }
293     net_Close( i_handle );
294
295     free( psz_password);
296     free( psz_server);
297     return VLC_SUCCESS;
298
299 error:
300     free( psz_password );
301     free( psz_server );
302     return VLC_EGENERIC;
303 }
304
305 #undef GROWL_PROTOCOL_VERSION
306 #undef GROWL_TYPE_REGISTRATION
307 #undef GROWL_TYPE_NOTIFICATION
308 #undef APPLICATION_NAME
309 #undef insertstrlen