]> git.sesse.net Git - vlc/blob - modules/misc/notify/growl_udp.c
msn: simplify (and remove some dummy strdup).
[vlc] / modules / misc / notify / growl_udp.c
1 /*****************************************************************************
2  * growl_udp.c : growl UDP 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
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 );
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     intf_thread_t *p_intf = (intf_thread_t *)p_this;
91
92     var_AddCallback( pl_Get( p_intf ), "item-current", ItemChange, p_intf );
93
94     RegisterToGrowl( p_this );
95     return VLC_SUCCESS;
96 }
97
98 /*****************************************************************************
99  * Close: destroy interface stuff
100  *****************************************************************************/
101 static void Close( vlc_object_t *p_this )
102 {
103     var_DelCallback( pl_Get( p_this ), "item-current", ItemChange, p_this );
104 }
105
106 /*****************************************************************************
107  * ItemChange: Playlist item change callback
108  *****************************************************************************/
109 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
110                        vlc_value_t oldval, vlc_value_t newval, void *param )
111 {
112     VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(newval);
113     VLC_UNUSED(param);
114     char psz_tmp[GROWL_MAX_LENGTH];
115     char *psz_title = NULL;
116     char *psz_artist = NULL;
117     char *psz_album = NULL;
118     input_thread_t *p_input;
119     p_input = playlist_CurrentInput( (playlist_t*)p_this );
120
121     if( !p_input ) return VLC_SUCCESS;
122
123     char *psz_name = input_item_GetName( input_GetItem( p_input ) );
124     if( p_input->b_dead || !psz_name )
125     {
126         /* Not playing anything ... */
127         free( psz_name );
128         vlc_object_release( p_input );
129         return VLC_SUCCESS;
130     }
131     free( psz_name );
132
133     /* Playing something ... */
134     input_item_t *p_item = input_GetItem( p_input );
135
136     psz_title = input_item_GetTitleFbName( p_item );
137     if( EMPTY_STR( psz_title ) )
138     {
139         free( psz_title );
140         vlc_object_release( p_input );
141         return VLC_SUCCESS;
142     }
143
144     psz_artist = input_item_GetArtist( p_item );
145     if( EMPTY_STR( psz_artist ) ) FREENULL( psz_artist );
146     psz_album = input_item_GetAlbum( p_item ) ;
147     if( EMPTY_STR( psz_album ) ) FREENULL( psz_album );
148
149     if( psz_artist && psz_album )
150         snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s\n%s [%s]",
151                 psz_title, psz_artist, psz_album );
152     else if( psz_artist )
153         snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s\n%s", psz_title, psz_artist );
154     else
155         snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s", psz_title );
156
157     free( psz_title );
158     free( psz_artist );
159     free( psz_album );
160
161     NotifyToGrowl( p_this, psz_tmp );
162
163     vlc_object_release( p_input );
164     return VLC_SUCCESS;
165 }
166
167 /*****************************************************************************
168  * Growl specific functions
169  *****************************************************************************/
170 #define GROWL_PROTOCOL_VERSION (1)
171 #define GROWL_TYPE_REGISTRATION (0)
172 #define GROWL_TYPE_NOTIFICATION (1)
173 #define APPLICATION_NAME "VLC media player"
174
175 #define insertstrlen( psz ) \
176 { \
177     uint16_t i_size = strlen( psz ); \
178     psz_encoded[i++] = (i_size>>8)&0xFF; \
179     psz_encoded[i++] = i_size&0xFF; \
180 }
181 /*****************************************************************************
182  * RegisterToGrowl
183  *****************************************************************************/
184 static int RegisterToGrowl( vlc_object_t *p_this )
185 {
186     uint8_t *psz_encoded = malloc(100);
187     uint8_t i_defaults = 0;
188     static const char *psz_notifications[] = {"Now Playing", NULL};
189     bool pb_defaults[] = {true, false};
190     int i = 0, j;
191     if( psz_encoded == NULL )
192         return false;
193
194     memset( psz_encoded, 0, sizeof(psz_encoded) );
195     psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
196     psz_encoded[i++] = GROWL_TYPE_REGISTRATION;
197     insertstrlen(APPLICATION_NAME);
198     i+=2;
199     strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
200     i += strlen(APPLICATION_NAME);
201     for( j = 0 ; psz_notifications[j] != NULL ; j++)
202     {
203         insertstrlen(psz_notifications[j]);
204         strcpy( (char*)(psz_encoded+i), psz_notifications[j] );
205         i += strlen(psz_notifications[j]);
206     }
207     psz_encoded[4] = j;
208     for( j = 0 ; psz_notifications[j] != NULL ; j++)
209         if(pb_defaults[j] == true)
210         {
211             psz_encoded[i++] = (uint8_t)j;
212             i_defaults++;
213         }
214     psz_encoded[5] = i_defaults;
215
216     CheckAndSend(p_this, psz_encoded, i);
217     free( psz_encoded );
218     return VLC_SUCCESS;
219 }
220
221 static int NotifyToGrowl( vlc_object_t *p_this, const char *psz_desc )
222 {
223     const char *psz_type = "Now Playing", *psz_title = "Now Playing";
224     uint8_t *psz_encoded = malloc(GROWL_MAX_LENGTH + 42);
225     uint16_t flags;
226     int i = 0;
227     if( psz_encoded == NULL )
228         return false;
229
230     memset( psz_encoded, 0, sizeof(psz_encoded) );
231     psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
232     psz_encoded[i++] = GROWL_TYPE_NOTIFICATION;
233     flags = 0;
234     psz_encoded[i++] = (flags>>8)&0xFF;
235     psz_encoded[i++] = flags&0xFF;
236     insertstrlen(psz_type);
237     insertstrlen(psz_title);
238     insertstrlen(psz_desc);
239     insertstrlen(APPLICATION_NAME);
240     strcpy( (char*)(psz_encoded+i), psz_type );
241     i += strlen(psz_type);
242     strcpy( (char*)(psz_encoded+i), psz_title );
243     i += strlen(psz_title);
244     strcpy( (char*)(psz_encoded+i), psz_desc );
245     i += strlen(psz_desc);
246     strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
247     i += strlen(APPLICATION_NAME);
248
249     CheckAndSend(p_this, psz_encoded, i);
250     free( psz_encoded );
251     return VLC_SUCCESS;
252 }
253
254 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset )
255 {
256     int i, i_handle;
257     struct md5_s md5;
258     intf_thread_t *p_intf = (intf_thread_t *)p_this;
259     char *psz_password = var_InheritString( p_intf, "growl-password" );
260     char *psz_server = var_InheritString( p_intf, "growl-server" );
261     int i_port = var_InheritInteger( p_intf, "growl-port" );
262     strcpy( (char*)(p_data+i_offset), psz_password );
263     i = i_offset + strlen(psz_password);
264
265     InitMD5( &md5 );
266     AddMD5( &md5, p_data, i );
267     EndMD5( &md5 );
268
269     for( i = 0 ; i < 4 ; i++ )
270     {
271         md5.p_digest[i] = md5.p_digest[i];
272         p_data[i_offset++] =  md5.p_digest[i]     &0xFF;
273         p_data[i_offset++] = (md5.p_digest[i]>> 8)&0xFF;
274         p_data[i_offset++] = (md5.p_digest[i]>>16)&0xFF;
275         p_data[i_offset++] = (md5.p_digest[i]>>24)&0xFF;
276     }
277
278     i_handle = net_ConnectUDP( p_this, psz_server, i_port, -1 );
279     if( i_handle == -1 )
280     {
281          msg_Err( p_this, "failed to open a connection (udp)" );
282          free( psz_password);
283          free( psz_server);
284          return VLC_EGENERIC;
285     }
286
287     shutdown( i_handle, SHUT_RD );
288     if( send( i_handle, p_data, i_offset, 0 )
289           == -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
300 #undef GROWL_PROTOCOL_VERSION
301 #undef GROWL_TYPE_REGISTRATION
302 #undef GROWL_TYPE_NOTIFICATION
303 #undef APPLICATION_NAME
304 #undef insertstrlen