]> git.sesse.net Git - vlc/blob - modules/misc/notify/growl.c
forward port [17012] and make update-po
[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/intf.h>
31 #include <vlc_meta.h>
32 #include <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 /// \bug [String] REmove all "Growl" in short desc
56
57 #define SERVER_DEFAULT "127.0.0.1"
58 #define SERVER_TEXT N_("Growl server")
59 #define SERVER_LONGTEXT N_("This is the host to which Growl notifications " \
60    "will be sent. By default, notifications are sent locally." )
61 #define PASS_DEFAULT ""
62 #define PASS_TEXT N_("Growl password")
63 /// \bug [String] Password on the Growl server.
64 #define PASS_LONGTEXT N_("Growl password on the server.")
65 #define PORT_TEXT N_("Growl UDP port")
66 /// \bug [String] UDP port on the Growl server
67 #define PORT_LONGTEXT N_("Growl UDP port on the server.")
68
69 vlc_module_begin();
70     set_category( CAT_INTERFACE );
71     set_subcategory( SUBCAT_INTERFACE_CONTROL );
72     set_shortname( _( "Growl" ) );
73     set_description( _("Growl Notification Plugin") );
74
75     add_string( "growl-server", SERVER_DEFAULT, NULL,
76                 SERVER_TEXT, SERVER_LONGTEXT, VLC_FALSE );
77     add_string( "growl-password", PASS_DEFAULT, NULL,
78                 PASS_TEXT, PASS_LONGTEXT, VLC_FALSE );
79     add_integer( "growl-port", 9887, NULL,
80                 PORT_TEXT, PORT_LONGTEXT, VLC_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_Yield( 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_Yield( 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     char psz_tmp[GROWL_MAX_LENGTH];
118     char *psz_title = NULL;
119     char *psz_artist = NULL;
120     char *psz_album = NULL;
121     input_thread_t *p_input;
122     playlist_t *p_playlist = pl_Yield( p_this );
123
124     p_input = p_playlist->p_input;
125     vlc_object_release( p_playlist );
126
127     if( !p_input ) return VLC_SUCCESS;
128     vlc_object_yield( p_input );
129
130     if( p_input->b_dead || !p_input->input.p_item->psz_name )
131     {
132         /* Not playing anything ... */
133         vlc_object_release( p_input );
134         return VLC_SUCCESS;
135     }
136
137     /* Playing something ... */
138     psz_artist = p_input->input.p_item->p_meta->psz_artist ?
139                   strdup( p_input->input.p_item->p_meta->psz_artist ) :
140                   strdup( "" );
141     psz_album = p_input->input.p_item->p_meta->psz_album ?
142                   strdup( p_input->input.p_item->p_meta->psz_album ) :
143                   strdup( "" );
144     psz_title = strdup( p_input->input.p_item->psz_name );
145     if( psz_title == NULL ) psz_title = strdup( N_("(no title)") );
146     if( psz_artist == NULL ) psz_artist = strdup( N_("(no artist)") );
147     if( psz_album == NULL ) psz_album = strdup( N_("(no album)") );
148     snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s %s %s",
149               psz_title, psz_artist, psz_album );
150     free( psz_title );
151     free( psz_artist );
152     free( psz_album );
153
154     NotifyToGrowl( p_this, "Now Playing", "Now Playing", psz_tmp );
155
156     vlc_object_release( p_input );
157     return VLC_SUCCESS;
158 }
159
160 /*****************************************************************************
161  * Growl specific functions
162  *****************************************************************************/
163 #define GROWL_PROTOCOL_VERSION (1)
164 #define GROWL_TYPE_REGISTRATION (0)
165 #define GROWL_TYPE_NOTIFICATION (1)
166 #define APPLICATION_NAME "VLC media player"
167
168 #define insertstrlen( psz ) \
169 { \
170     uint16_t i_size = strlen( psz ); \
171     psz_encoded[i++] = (i_size>>8)&0xFF; \
172     psz_encoded[i++] = i_size&0xFF; \
173 }
174 /*****************************************************************************
175  * RegisterToGrowl
176  *****************************************************************************/
177 static int RegisterToGrowl( vlc_object_t *p_this )
178 {
179     uint8_t *psz_encoded = malloc(100);
180     uint8_t i_defaults = 0;
181     char *psz_notifications[] = {"Now Playing", NULL};
182     vlc_bool_t pb_defaults[] = {VLC_TRUE, VLC_FALSE};
183     int i = 0, j;
184     if( psz_encoded == NULL )
185         return VLC_FALSE;
186
187     memset( psz_encoded, 0, sizeof(psz_encoded) );
188     psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
189     psz_encoded[i++] = GROWL_TYPE_REGISTRATION;
190     insertstrlen(APPLICATION_NAME);
191     i+=2;
192     strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
193     i += strlen(APPLICATION_NAME);
194     for( j = 0 ; psz_notifications[j] != NULL ; j++)
195     {
196         insertstrlen(psz_notifications[j]);
197         strcpy( (char*)(psz_encoded+i), psz_notifications[j] );
198         i += strlen(psz_notifications[j]);
199     }
200     psz_encoded[4] = j;
201     for( j = 0 ; psz_notifications[j] != NULL ; j++)
202         if(pb_defaults[j] == VLC_TRUE)
203         {
204             psz_encoded[i++] = (uint8_t)j;
205             i_defaults++;
206         }
207     psz_encoded[5] = i_defaults;
208
209     CheckAndSend(p_this, psz_encoded, i);
210     free( psz_encoded );
211     return VLC_SUCCESS;
212 }
213
214 static int NotifyToGrowl( vlc_object_t *p_this, char *psz_type,
215                             char *psz_title, char *psz_desc )
216 {
217     uint8_t *psz_encoded = malloc(GROWL_MAX_LENGTH + 42);
218     uint16_t flags;
219     int i = 0;
220     if( psz_encoded == NULL )
221         return VLC_FALSE;
222
223     memset( psz_encoded, 0, sizeof(psz_encoded) );
224     psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
225     psz_encoded[i++] = GROWL_TYPE_NOTIFICATION;
226     flags = 0;
227     psz_encoded[i++] = (flags>>8)&0xFF;
228     psz_encoded[i++] = flags&0xFF;
229     insertstrlen(psz_type);
230     insertstrlen(psz_title);
231     insertstrlen(psz_desc);
232     insertstrlen(APPLICATION_NAME);
233     strcpy( (char*)(psz_encoded+i), psz_type );
234     i += strlen(psz_type);
235     strcpy( (char*)(psz_encoded+i), psz_title );
236     i += strlen(psz_title);
237     strcpy( (char*)(psz_encoded+i), psz_desc );
238     i += strlen(psz_desc);
239     strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
240     i += strlen(APPLICATION_NAME);
241
242     CheckAndSend(p_this, psz_encoded, i);
243     free( psz_encoded );
244     return VLC_SUCCESS;
245 }
246
247 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset )
248 {
249     int i, i_handle;
250     struct md5_s md5;
251     intf_thread_t *p_intf = (intf_thread_t *)p_this;
252     char *psz_password = config_GetPsz( p_intf, "growl-password" );
253     char *psz_server = config_GetPsz( p_intf, "growl-server" );
254     int i_port = config_GetInt( p_intf, "growl-port" );
255     strcpy( (char*)(p_data+i_offset), psz_password );
256     i = i_offset + strlen(psz_password);
257
258     InitMD5( &md5 );
259     AddMD5( &md5, p_data, i );
260     EndMD5( &md5 );
261
262     for( i = 0 ; i < 4 ; i++ )
263     {
264         md5.p_digest[i] = md5.p_digest[i];
265         p_data[i_offset++] =  md5.p_digest[i]     &0xFF;
266         p_data[i_offset++] = (md5.p_digest[i]>> 8)&0xFF;
267         p_data[i_offset++] = (md5.p_digest[i]>>16)&0xFF;
268         p_data[i_offset++] = (md5.p_digest[i]>>24)&0xFF;
269     }
270
271     i_handle = net_ConnectUDP( p_this, psz_server, i_port, 0 );
272     if( i_handle == -1 )
273     {
274          msg_Err( p_this, "failed to open a connection (udp)" );
275          free( psz_password);
276          free( psz_server);
277          return VLC_EGENERIC;
278     }
279
280     net_StopRecv( i_handle );
281     if( send( i_handle, p_data, i_offset, 0 )
282           == -1 )
283     {
284         msg_Warn( p_this, "send error: %s", strerror(errno) );
285     }
286     net_Close( i_handle );
287
288     free( psz_password);
289     free( psz_server);
290     return VLC_SUCCESS;
291 }
292
293 #undef GROWL_PROTOCOL_VERSION
294 #undef GROWL_TYPE_REGISTRATION
295 #undef GROWL_TYPE_NOTIFICATION
296 #undef APPLICATION_NAME
297 #undef insertstrlen