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