]> git.sesse.net Git - vlc/blob - modules/misc/notify/growl_udp.c
update module LIST file.
[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/vlc.h>
33 #include <vlc_interface.h>
34 #include <vlc_playlist.h>
35 #include <vlc_meta.h>
36 #include <vlc_network.h>
37 #include <errno.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( _("Growl UDP Notification Plugin") );
73
74     add_string( "growl-server", SERVER_DEFAULT, NULL,
75                 SERVER_TEXT, SERVER_LONGTEXT, VLC_FALSE );
76     add_password( "growl-password", PASS_DEFAULT, NULL,
77                 PASS_TEXT, PASS_LONGTEXT, VLC_FALSE );
78     add_integer( "growl-port", 9887, NULL,
79                 PORT_TEXT, PORT_LONGTEXT, VLC_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     playlist_t *p_playlist = pl_Yield( p_intf );
93     var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
94     pl_Release( p_intf );
95
96     RegisterToGrowl( p_this );
97     return VLC_SUCCESS;
98 }
99
100 /*****************************************************************************
101  * Close: destroy interface stuff
102  *****************************************************************************/
103 static void Close( vlc_object_t *p_this )
104 {
105     playlist_t *p_playlist = pl_Yield( p_this );
106     var_DelCallback( p_playlist, "playlist-current", ItemChange, p_this );
107     pl_Release( p_this );
108 }
109
110 /*****************************************************************************
111  * ItemChange: Playlist item change callback
112  *****************************************************************************/
113 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
114                        vlc_value_t oldval, vlc_value_t newval, void *param )
115 {
116     VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(newval);
117     VLC_UNUSED(param);
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     char *psz_name = input_item_GetName( input_GetItem( p_input ) );
132     if( p_input->b_dead || !psz_name )
133     {
134         /* Not playing anything ... */
135         free( psz_name );
136         vlc_object_release( p_input );
137         return VLC_SUCCESS;
138     }
139     free( psz_name );
140
141     /* Playing something ... */
142     input_item_t *p_item = input_GetItem( p_input );
143
144     psz_title = input_item_GetTitle( p_item );
145     if( psz_title == NULL || EMPTY_STR( psz_title ) )
146     {
147         free( psz_title );
148         psz_title = input_item_GetName( input_GetItem( p_input ) );
149         if( psz_title == NULL || EMPTY_STR( psz_title ) )
150         {
151             free( psz_title );
152             vlc_object_release( p_input );
153             return VLC_SUCCESS;
154         }
155     }
156
157     psz_artist = input_item_GetArtist( p_item );
158     if( EMPTY_STR( psz_artist ) ) FREENULL( psz_artist );
159     psz_album = input_item_GetAlbum( p_item ) ;
160     if( EMPTY_STR( psz_album ) ) FREENULL( psz_album );
161
162     if( psz_artist && psz_album )
163         snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s\n%s [%s]",
164                 psz_title, psz_artist, psz_album );
165     else if( psz_artist )
166         snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s\n%s", psz_title, psz_artist );
167     else
168         snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s", psz_title );
169
170     free( psz_title );
171     free( psz_artist );
172     free( psz_album );
173
174     NotifyToGrowl( p_this, psz_tmp );
175
176     vlc_object_release( p_input );
177     return VLC_SUCCESS;
178 }
179
180 /*****************************************************************************
181  * Growl specific functions
182  *****************************************************************************/
183 #define GROWL_PROTOCOL_VERSION (1)
184 #define GROWL_TYPE_REGISTRATION (0)
185 #define GROWL_TYPE_NOTIFICATION (1)
186 #define APPLICATION_NAME "VLC media player"
187
188 #define insertstrlen( psz ) \
189 { \
190     uint16_t i_size = strlen( psz ); \
191     psz_encoded[i++] = (i_size>>8)&0xFF; \
192     psz_encoded[i++] = i_size&0xFF; \
193 }
194 /*****************************************************************************
195  * RegisterToGrowl
196  *****************************************************************************/
197 static int RegisterToGrowl( vlc_object_t *p_this )
198 {
199     uint8_t *psz_encoded = malloc(100);
200     uint8_t i_defaults = 0;
201     static const char *psz_notifications[] = {"Now Playing", NULL};
202     vlc_bool_t pb_defaults[] = {VLC_TRUE, VLC_FALSE};
203     int i = 0, j;
204     if( psz_encoded == NULL )
205         return VLC_FALSE;
206
207     memset( psz_encoded, 0, sizeof(psz_encoded) );
208     psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
209     psz_encoded[i++] = GROWL_TYPE_REGISTRATION;
210     insertstrlen(APPLICATION_NAME);
211     i+=2;
212     strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
213     i += strlen(APPLICATION_NAME);
214     for( j = 0 ; psz_notifications[j] != NULL ; j++)
215     {
216         insertstrlen(psz_notifications[j]);
217         strcpy( (char*)(psz_encoded+i), psz_notifications[j] );
218         i += strlen(psz_notifications[j]);
219     }
220     psz_encoded[4] = j;
221     for( j = 0 ; psz_notifications[j] != NULL ; j++)
222         if(pb_defaults[j] == VLC_TRUE)
223         {
224             psz_encoded[i++] = (uint8_t)j;
225             i_defaults++;
226         }
227     psz_encoded[5] = i_defaults;
228
229     CheckAndSend(p_this, psz_encoded, i);
230     free( psz_encoded );
231     return VLC_SUCCESS;
232 }
233
234 static int NotifyToGrowl( vlc_object_t *p_this, const char *psz_desc )
235 {
236     const char *psz_type = "Now Playing", *psz_title = "Now Playing";
237     uint8_t *psz_encoded = malloc(GROWL_MAX_LENGTH + 42);
238     uint16_t flags;
239     int i = 0;
240     if( psz_encoded == NULL )
241         return VLC_FALSE;
242
243     memset( psz_encoded, 0, sizeof(psz_encoded) );
244     psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
245     psz_encoded[i++] = GROWL_TYPE_NOTIFICATION;
246     flags = 0;
247     psz_encoded[i++] = (flags>>8)&0xFF;
248     psz_encoded[i++] = flags&0xFF;
249     insertstrlen(psz_type);
250     insertstrlen(psz_title);
251     insertstrlen(psz_desc);
252     insertstrlen(APPLICATION_NAME);
253     strcpy( (char*)(psz_encoded+i), psz_type );
254     i += strlen(psz_type);
255     strcpy( (char*)(psz_encoded+i), psz_title );
256     i += strlen(psz_title);
257     strcpy( (char*)(psz_encoded+i), psz_desc );
258     i += strlen(psz_desc);
259     strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
260     i += strlen(APPLICATION_NAME);
261
262     CheckAndSend(p_this, psz_encoded, i);
263     free( psz_encoded );
264     return VLC_SUCCESS;
265 }
266
267 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset )
268 {
269     int i, i_handle;
270     struct md5_s md5;
271     intf_thread_t *p_intf = (intf_thread_t *)p_this;
272     char *psz_password = config_GetPsz( p_intf, "growl-password" );
273     char *psz_server = config_GetPsz( p_intf, "growl-server" );
274     int i_port = config_GetInt( p_intf, "growl-port" );
275     strcpy( (char*)(p_data+i_offset), psz_password );
276     i = i_offset + strlen(psz_password);
277
278     InitMD5( &md5 );
279     AddMD5( &md5, p_data, i );
280     EndMD5( &md5 );
281
282     for( i = 0 ; i < 4 ; i++ )
283     {
284         md5.p_digest[i] = md5.p_digest[i];
285         p_data[i_offset++] =  md5.p_digest[i]     &0xFF;
286         p_data[i_offset++] = (md5.p_digest[i]>> 8)&0xFF;
287         p_data[i_offset++] = (md5.p_digest[i]>>16)&0xFF;
288         p_data[i_offset++] = (md5.p_digest[i]>>24)&0xFF;
289     }
290
291     i_handle = net_ConnectUDP( p_this, psz_server, i_port, 0 );
292     if( i_handle == -1 )
293     {
294          msg_Err( p_this, "failed to open a connection (udp)" );
295          free( psz_password);
296          free( psz_server);
297          return VLC_EGENERIC;
298     }
299
300     shutdown( i_handle, SHUT_RD );
301     if( send( i_handle, p_data, i_offset, 0 )
302           == -1 )
303     {
304         msg_Warn( p_this, "send error: %m" );
305     }
306     net_Close( i_handle );
307
308     free( psz_password);
309     free( psz_server);
310     return VLC_SUCCESS;
311 }
312
313 #undef GROWL_PROTOCOL_VERSION
314 #undef GROWL_TYPE_REGISTRATION
315 #undef GROWL_TYPE_NOTIFICATION
316 #undef APPLICATION_NAME
317 #undef insertstrlen