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