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