]> git.sesse.net Git - vlc/blob - modules/misc/notify/growl.c
6f6ba179093f28e54794a76424dbad0bca59ea2b
[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
28 #include <vlc/vlc.h>
29 #include <vlc_interface.h>
30 #include <vlc_playlist.h>
31 #include <vlc_meta.h>
32 #include <vlc_network.h>
33 #include <errno.h>
34 #include <vlc_md5.h>
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39 static int  Open    ( vlc_object_t * );
40 static void Close   ( vlc_object_t * );
41
42 static int ItemChange( vlc_object_t *, const char *,
43                        vlc_value_t, vlc_value_t, void * );
44
45 static int RegisterToGrowl( vlc_object_t *p_this );
46 static int NotifyToGrowl( vlc_object_t *p_this, char *psz_type,
47                             char *psz_title, char *psz_desc );
48 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset );
49 #define GROWL_MAX_LENGTH 256
50
51 /*****************************************************************************
52  * Module descriptor
53  ****************************************************************************/
54
55 #define SERVER_DEFAULT "127.0.0.1"
56 #define SERVER_TEXT N_("Server")
57 #define SERVER_LONGTEXT N_("This is the host to which Growl notifications " \
58    "will be sent. By default, notifications are sent locally." )
59 #define PASS_DEFAULT ""
60 #define PASS_TEXT N_("Password")
61 #define PASS_LONGTEXT N_("Growl password on the Growl server.")
62 #define PORT_TEXT N_("UDP port")
63 #define PORT_LONGTEXT N_("Growl UDP port on the Growl server.")
64
65 vlc_module_begin();
66     set_category( CAT_INTERFACE );
67     set_subcategory( SUBCAT_INTERFACE_CONTROL );
68     set_shortname( "Growl" );
69     set_description( _("Growl Notification Plugin") );
70
71     add_string( "growl-server", SERVER_DEFAULT, NULL,
72                 SERVER_TEXT, SERVER_LONGTEXT, VLC_FALSE );
73     add_string( "growl-password", PASS_DEFAULT, NULL,
74                 PASS_TEXT, PASS_LONGTEXT, VLC_FALSE );
75     add_integer( "growl-port", 9887, NULL,
76                 PORT_TEXT, PORT_LONGTEXT, VLC_TRUE );
77
78     set_capability( "interface", 0 );
79     set_callbacks( Open, Close );
80 vlc_module_end();
81
82 /*****************************************************************************
83  * Open: initialize and create stuff
84  *****************************************************************************/
85 static int Open( vlc_object_t *p_this )
86 {
87     intf_thread_t *p_intf = (intf_thread_t *)p_this;
88
89     playlist_t *p_playlist = pl_Yield( p_intf );
90     var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
91     pl_Release( p_intf );
92
93     RegisterToGrowl( p_this );
94     return VLC_SUCCESS;
95 }
96
97 /*****************************************************************************
98  * Close: destroy interface stuff
99  *****************************************************************************/
100 static void Close( vlc_object_t *p_this )
101 {
102     playlist_t *p_playlist = pl_Yield( p_this );
103     var_DelCallback( p_playlist, "playlist-current", ItemChange, p_this );
104     pl_Release( p_this );
105 }
106
107 /*****************************************************************************
108  * ItemChange: Playlist item change callback
109  *****************************************************************************/
110 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
111                        vlc_value_t oldval, vlc_value_t newval, void *param )
112 {
113     char psz_tmp[GROWL_MAX_LENGTH];
114     char *psz_title = NULL;
115     char *psz_artist = NULL;
116     char *psz_album = NULL;
117     input_thread_t *p_input;
118     playlist_t *p_playlist = pl_Yield( p_this );
119
120     p_input = p_playlist->p_input;
121     vlc_object_release( p_playlist );
122
123     if( !p_input ) return VLC_SUCCESS;
124     vlc_object_yield( p_input );
125
126     char *psz_name = input_item_GetName( input_GetItem( p_input ) );
127     if( p_input->b_dead || !psz_name )
128     {
129         /* Not playing anything ... */
130         free( psz_name );
131         vlc_object_release( p_input );
132         return VLC_SUCCESS;
133     }
134     free( psz_name );
135
136     /* Playing something ... */
137     psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
138     if( psz_artist == NULL ) psz_artist = strdup( "" );
139     psz_album = input_item_GetAlbum( input_GetItem( p_input ) ) ;
140     if( psz_album == NULL ) psz_album = strdup( "" );
141     psz_title = input_item_GetName( input_GetItem( p_input ) );
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