File: growl_udp.c

package info (click to toggle)
vlc 1.1.3-1squeeze6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 134,496 kB
  • ctags: 53,762
  • sloc: ansic: 341,893; cpp: 80,372; objc: 23,171; sh: 12,102; makefile: 5,035; xml: 1,351; asm: 524; python: 257; perl: 76; sed: 16
file content (342 lines) | stat: -rw-r--r-- 11,467 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*****************************************************************************
 * growl_udp.c : growl UDP notification plugin
 *****************************************************************************
 * Copyright (C) 2006 - 2010 the VideoLAN team
 * $Id: 16cde456157099bae7903734fefe0301fd66fbd7 $
 *
 * Authors: Jérôme Decoodt <djc -at- videolan -dot- org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_interface.h>
#include <vlc_playlist.h>
#include <vlc_meta.h>
#include <vlc_network.h>
#include <vlc_md5.h>

/*****************************************************************************
 * intf_sys_t: description
 *****************************************************************************/
struct intf_sys_t
{
    char *psz_password;
    char *psz_server;
    int i_port;
};

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Open    ( vlc_object_t * );
static void Close   ( vlc_object_t * );

static int ItemChange( vlc_object_t *, const char *,
                       vlc_value_t, vlc_value_t, void * );

static int RegisterToGrowl( vlc_object_t *p_this );
static int NotifyToGrowl( vlc_object_t *p_this, const char *psz_desc );
static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset, size_t is_ze );
#define GROWL_MAX_LENGTH 256

/*****************************************************************************
 * Module descriptor
 ****************************************************************************/

#define SERVER_DEFAULT "127.0.0.1"
#define SERVER_TEXT N_("Server")
#define SERVER_LONGTEXT N_("This is the host to which Growl notifications " \
   "will be sent. By default, notifications are sent locally." )
#define PASS_DEFAULT ""
#define PASS_TEXT N_("Password")
#define PASS_LONGTEXT N_("Growl password on the Growl server.")
#define PORT_TEXT N_("UDP port")
#define PORT_LONGTEXT N_("Growl UDP port on the Growl server.")

vlc_module_begin ()
    set_category( CAT_INTERFACE )
    set_subcategory( SUBCAT_INTERFACE_CONTROL )
    set_shortname( "Growl-UDP" )
    set_description( N_("Growl UDP Notification Plugin") )

    add_string( "growl-server", SERVER_DEFAULT, NULL,
                SERVER_TEXT, SERVER_LONGTEXT, false )
    add_password( "growl-password", PASS_DEFAULT, NULL,
                PASS_TEXT, PASS_LONGTEXT, false )
    add_integer( "growl-port", 9887, NULL,
                PORT_TEXT, PORT_LONGTEXT, true )

    set_capability( "interface", 0 )
    set_callbacks( Open, Close )
vlc_module_end ()

/*****************************************************************************
 * Open: initialize and create stuff
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
    intf_sys_t *p_sys = p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
    if( !p_sys )
        return VLC_ENOMEM;

    p_sys->psz_password = var_InheritString( p_this, "growl-password" );
    p_sys->psz_server = var_InheritString( p_this, "growl-server" );
    p_sys->i_port = var_InheritInteger( p_this, "growl-port" );

    if( !p_sys->psz_password || !p_sys->psz_server )
    {
        free( p_sys->psz_password );
        free( p_sys->psz_server );
        free( p_sys );
        return VLC_EGENERIC;
    }

    var_AddCallback( pl_Get( p_this ), "item-current", ItemChange, p_this );
    RegisterToGrowl( p_this );
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: destroy interface stuff
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    intf_sys_t *p_sys = ((intf_thread_t*)p_this)->p_sys;

    var_DelCallback( pl_Get( p_this ), "item-current", ItemChange, p_this );

    free( p_sys->psz_password );
    free( p_sys->psz_server );
    free( p_sys );
}

/*****************************************************************************
 * ItemChange: Playlist item change callback
 *****************************************************************************/
static int ItemChange( vlc_object_t *p_this, const char *psz_var,
                       vlc_value_t oldval, vlc_value_t newval, void *param )
{
    VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(newval);
    VLC_UNUSED(param);

    char psz_tmp[GROWL_MAX_LENGTH];
    char *psz_title;
    char *psz_artist;
    char *psz_album;
    input_thread_t *p_input = playlist_CurrentInput( (playlist_t*)p_this );

    if( !p_input ) return VLC_SUCCESS;

    char *psz_name = input_item_GetName( input_GetItem( p_input ) );
    if( p_input->b_dead || !psz_name )
    {
        /* Not playing anything ... */
        free( psz_name );
        vlc_object_release( p_input );
        return VLC_SUCCESS;
    }
    free( psz_name );

    /* Playing something ... */
    input_item_t *p_item = input_GetItem( p_input );

    psz_title = input_item_GetTitleFbName( p_item );
    if( EMPTY_STR( psz_title ) )
    {
        free( psz_title );
        vlc_object_release( p_input );
        return VLC_SUCCESS;
    }

    psz_artist = input_item_GetArtist( p_item );
    if( EMPTY_STR( psz_artist ) ) FREENULL( psz_artist );
    psz_album = input_item_GetAlbum( p_item ) ;
    if( EMPTY_STR( psz_album ) ) FREENULL( psz_album );

    if( psz_artist && psz_album )
        snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s\n%s [%s]",
                psz_title, psz_artist, psz_album );
    else if( psz_artist )
        snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s\n%s", psz_title, psz_artist );
    else
        snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s", psz_title );

    free( psz_title );
    free( psz_artist );
    free( psz_album );

    NotifyToGrowl( p_this, psz_tmp );

    vlc_object_release( p_input );
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Growl specific functions
 *****************************************************************************/
#define GROWL_PROTOCOL_VERSION (1)
#define GROWL_TYPE_REGISTRATION (0)
#define GROWL_TYPE_NOTIFICATION (1)
#define APPLICATION_NAME "VLC media player"

#define insertstrlen( psz ) \
{ \
    uint16_t i_size = strlen( psz ); \
    psz_encoded[i++] = (i_size>>8)&0xFF; \
    psz_encoded[i++] = i_size&0xFF; \
}
/*****************************************************************************
 * RegisterToGrowl
 *****************************************************************************/
static int RegisterToGrowl( vlc_object_t *p_this )
{
    uint8_t *psz_encoded = calloc( 100, 1 );
    uint8_t i_defaults = 0;
    static const char *psz_notifications[] = {"Now Playing", NULL};
    bool pb_defaults[] = {true, false};
    int i = 0, j;
    if( psz_encoded == NULL )
        return false;

    psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
    psz_encoded[i++] = GROWL_TYPE_REGISTRATION;
    insertstrlen(APPLICATION_NAME);
    i+=2;
    strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
    i += strlen(APPLICATION_NAME);
    for( j = 0 ; psz_notifications[j] != NULL ; j++)
    {
        insertstrlen(psz_notifications[j]);
        strcpy( (char*)(psz_encoded+i), psz_notifications[j] );
        i += strlen(psz_notifications[j]);
    }
    psz_encoded[4] = j;
    for( j = 0 ; psz_notifications[j] != NULL ; j++)
    {
        if(pb_defaults[j] == true)
        {
            psz_encoded[i++] = (uint8_t)j;
            i_defaults++;
        }
    }
    psz_encoded[5] = i_defaults;

    CheckAndSend(p_this, psz_encoded, i, 100);
    free( psz_encoded );
    return VLC_SUCCESS;
}

static int NotifyToGrowl( vlc_object_t *p_this, const char *psz_desc )
{
    const char *psz_type = "Now Playing", *psz_title = "Now Playing";
    uint8_t *psz_encoded = calloc(GROWL_MAX_LENGTH + 42, 1);
    uint16_t flags;
    int i = 0;
    if( psz_encoded == NULL )
        return false;

    // Check the size of the data
    size_t i_type = strlen( psz_type );
    size_t i_title = strlen( psz_title );
    size_t i_app = strlen( APPLICATION_NAME );
    size_t i_desc = strlen( psz_desc );
    if( 12 + i_type + i_title + i_desc + i_app >= GROWL_MAX_LENGTH + 42 )
    {
        free( psz_encoded );
        return false;
    }

    psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
    psz_encoded[i++] = GROWL_TYPE_NOTIFICATION;
    flags = 0;
    psz_encoded[i++] = (flags>>8)&0xFF;
    psz_encoded[i++] = flags&0xFF;
    insertstrlen(psz_type);
    insertstrlen(psz_title);
    insertstrlen(psz_desc);
    insertstrlen(APPLICATION_NAME);

    strcpy( (char*)(psz_encoded+i), psz_type );
    i += i_type;
    strcpy( (char*)(psz_encoded+i), psz_title );
    i += i_title;
    strcpy( (char*)(psz_encoded+i), psz_desc );
    i += i_desc;
    strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
    i += i_app;

    CheckAndSend(p_this, psz_encoded, i, GROWL_MAX_LENGTH + 42);
    free( psz_encoded );
    return VLC_SUCCESS;
}

static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset, size_t i_size )
{
    intf_sys_t *p_sys = ((intf_thread_t*)p_this)->p_sys;
    int i_handle;
    struct md5_s md5;

    int i_password_length = strlen( p_sys->psz_password );
    // Check that the buffer is larger enought for the string and the md5
    if( i_offset + i_password_length + 4*4 >= i_size )
        return VLC_EGENERIC;

    strcpy( (char*)(p_data+i_offset), p_sys->psz_password );

    InitMD5( &md5 );
    AddMD5( &md5, p_data, i_offset + i_password_length );
    EndMD5( &md5 );

    for( int i = 0 ; i < 4 ; i++ )
    {
        p_data[i_offset++] =  md5.p_digest[i]     &0xFF;
        p_data[i_offset++] = (md5.p_digest[i]>> 8)&0xFF;
        p_data[i_offset++] = (md5.p_digest[i]>>16)&0xFF;
        p_data[i_offset++] = (md5.p_digest[i]>>24)&0xFF;
    }

    i_handle = net_ConnectUDP( p_this, p_sys->psz_server, p_sys->i_port, -1 );
    if( i_handle == -1 )
    {
        msg_Err( p_this, "failed to open a connection (udp)" );
        return VLC_EGENERIC;
    }

    shutdown( i_handle, SHUT_RD );
    if( send( i_handle, p_data, i_offset, 0 ) == -1 )
    {
        msg_Warn( p_this, "send error: %m" );
    }
    net_Close( i_handle );

    return VLC_SUCCESS;
}

#undef GROWL_PROTOCOL_VERSION
#undef GROWL_TYPE_REGISTRATION
#undef GROWL_TYPE_NOTIFICATION
#undef APPLICATION_NAME
#undef insertstrlen