1 /*****************************************************************************
2 * macro.c : Custom <vlc> macro handling
3 *****************************************************************************
4 * Copyright (C) 2001-2005 the VideoLAN team
5 * $Id: http.c 12225 2005-08-18 10:01:30Z massiot $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8 * Laurent Aimar <fenrir@via.ecp.fr>
9 * Christophe Massiot <massiot@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
29 int MacroParse( macro_t *m, char *psz_src )
31 char *dup = strdup( (char *)psz_src );
36 #define EXTRACT( name, l ) \
38 p = strchr( src, '"' ); \
43 m->name = strdup( src ); \
64 if( !strncmp( src, "id=\"", 4 ) )
68 else if( !strncmp( src, "param1=\"", 8 ) )
72 else if( !strncmp( src, "param2=\"", 8 ) )
81 if( strstr( src, "/>" ) )
83 src = strstr( src, "/>" ) + 2;
94 if( m->param1 == NULL )
96 m->param1 = strdup( "" );
98 if( m->param2 == NULL )
100 m->param2 = strdup( "" );
109 void MacroClean( macro_t *m )
116 int StrToMacroType( char *name )
120 if( !name || *name == '\0')
124 for( i = 0; StrToMacroTypeTab[i].psz_name != NULL; i++ )
126 if( !strcmp( name, StrToMacroTypeTab[i].psz_name ) )
128 return StrToMacroTypeTab[i].i_type;
134 void MacroDo( httpd_file_sys_t *p_args,
136 char *p_request, int i_request,
137 char **pp_data, int *pi_data,
140 intf_thread_t *p_intf = p_args->p_intf;
141 intf_sys_t *p_sys = p_args->p_intf->p_sys;
146 int __i__ = *pp_dst - *pp_data; \
148 *pp_data = realloc( *pp_data, *pi_data ); \
149 *pp_dst = (*pp_data) + __i__; \
151 #define PRINT( str ) \
152 ALLOC( strlen( str ) + 1 ); \
153 *pp_dst += sprintf( *pp_dst, str );
155 #define PRINTS( str, s ) \
156 ALLOC( strlen( str ) + strlen( s ) + 1 ); \
158 char * psz_cur = *pp_dst; \
159 *pp_dst += sprintf( *pp_dst, str, s ); \
160 while( psz_cur && *psz_cur ) \
162 /* Prevent script injection */ \
163 if( *psz_cur == '<' ) *psz_cur = '*'; \
164 if( *psz_cur == '>' ) *psz_cur = '*'; \
169 switch( StrToMacroType( m->id ) )
176 E_(uri_extract_value)( p_request, "control", control, 512 );
177 if( *m->param1 && !strstr( m->param1, control ) )
179 msg_Warn( p_intf, "unauthorized control=%s", control );
182 switch( StrToMacroType( control ) )
189 E_(uri_extract_value)( p_request, "item", item, 512 );
190 i_item = atoi( item );
191 playlist_Control( p_sys->p_playlist, PLAYLIST_ITEMPLAY,
192 playlist_ItemGetById( p_sys->p_playlist,
194 msg_Dbg( p_intf, "requested playlist item: %i", i_item );
198 playlist_Control( p_sys->p_playlist, PLAYLIST_STOP );
199 msg_Dbg( p_intf, "requested playlist stop" );
202 playlist_Control( p_sys->p_playlist, PLAYLIST_PAUSE );
203 msg_Dbg( p_intf, "requested playlist pause" );
206 playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, 1 );
207 msg_Dbg( p_intf, "requested playlist next" );
210 playlist_Control( p_sys->p_playlist, PLAYLIST_SKIP, -1 );
211 msg_Dbg( p_intf, "requested playlist previous" );
213 case MVLC_FULLSCREEN:
216 vout_thread_t *p_vout;
217 p_vout = vlc_object_find( p_sys->p_input,
218 VLC_OBJECT_VOUT, FIND_CHILD );
222 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
223 vlc_object_release( p_vout );
224 msg_Dbg( p_intf, "requested fullscreen toggle" );
231 E_(uri_extract_value)( p_request, "seek_value", value, 30 );
232 E_(uri_decode_url_encoded)( value );
233 E_(Seek)( p_intf, value );
239 audio_volume_t i_volume;
242 E_(uri_extract_value)( p_request, "value", vol, 8 );
243 aout_VolumeGet( p_intf, &i_volume );
244 E_(uri_decode_url_encoded)( vol );
248 i_value = atoi( vol + 1 );
249 if( (i_volume + i_value) > AOUT_VOLUME_MAX )
251 aout_VolumeSet( p_intf , AOUT_VOLUME_MAX );
252 msg_Dbg( p_intf, "requested volume set: max" );
256 aout_VolumeSet( p_intf , (i_volume + i_value) );
257 msg_Dbg( p_intf, "requested volume set: +%i", (i_volume + i_value) );
260 else if( vol[0] == '-' )
262 i_value = atoi( vol + 1 );
263 if( (i_volume - i_value) < AOUT_VOLUME_MIN )
265 aout_VolumeSet( p_intf , AOUT_VOLUME_MIN );
266 msg_Dbg( p_intf, "requested volume set: min" );
270 aout_VolumeSet( p_intf , (i_volume - i_value) );
271 msg_Dbg( p_intf, "requested volume set: -%i", (i_volume - i_value) );
274 else if( strstr(vol, "%") != NULL )
276 i_value = atoi( vol );
277 if( (i_value <= 400) && (i_value>=0) ){
278 aout_VolumeSet( p_intf, (i_value * (AOUT_VOLUME_MAX - AOUT_VOLUME_MIN))/400+AOUT_VOLUME_MIN);
279 msg_Dbg( p_intf, "requested volume set: %i%%", atoi( vol ));
284 i_value = atoi( vol );
285 if( ( i_value <= AOUT_VOLUME_MAX ) && ( i_value >= AOUT_VOLUME_MIN ) )
287 aout_VolumeSet( p_intf , atoi( vol ) );
288 msg_Dbg( p_intf, "requested volume set: %i", atoi( vol ) );
294 /* playlist management */
297 char mrl[1024], psz_name[1024];
298 playlist_item_t *p_item;
300 E_(uri_extract_value)( p_request, "mrl", mrl, 1024 );
301 E_(uri_decode_url_encoded)( mrl );
302 E_(uri_extract_value)( p_request, "name", psz_name, 1024 );
303 E_(uri_decode_url_encoded)( psz_name );
306 memcpy( psz_name, mrl, 1024 );
308 p_item = E_(MRLParse)( p_intf, mrl, psz_name );
310 if( !p_item || !p_item->input.psz_uri ||
311 !*p_item->input.psz_uri )
313 msg_Dbg( p_intf, "invalid requested mrl: %s", mrl );
317 playlist_AddItem( p_sys->p_playlist, p_item,
318 PLAYLIST_APPEND, PLAYLIST_END );
319 msg_Dbg( p_intf, "requested mrl add: %s", mrl );
326 int i_item, *p_items = NULL, i_nb_items = 0;
327 char item[512], *p_parser = p_request;
329 /* Get the list of items to delete */
331 E_(uri_extract_value)( p_parser, "item", item, 512 )) )
333 if( !*item ) continue;
335 i_item = atoi( item );
336 p_items = realloc( p_items, (i_nb_items + 1) *
338 p_items[i_nb_items] = i_item;
345 for( i = 0; i < i_nb_items; i++ )
347 playlist_LockDelete( p_sys->p_playlist, p_items[i] );
348 msg_Dbg( p_intf, "requested playlist delete: %d",
354 if( p_items ) free( p_items );
359 int i_item, *p_items = NULL, i_nb_items = 0;
360 char item[512], *p_parser = p_request;
363 /* Get the list of items to keep */
365 E_(uri_extract_value)( p_parser, "item", item, 512 )) )
367 if( !*item ) continue;
369 i_item = atoi( item );
370 p_items = realloc( p_items, (i_nb_items + 1) *
372 p_items[i_nb_items] = i_item;
376 for( i = p_sys->p_playlist->i_size - 1 ; i >= 0; i-- )
378 /* Check if the item is in the keep list */
379 for( j = 0 ; j < i_nb_items ; j++ )
382 p_sys->p_playlist->pp_items[i]->input.i_id ) break;
384 if( j == i_nb_items )
386 playlist_LockDelete( p_sys->p_playlist, p_sys->p_playlist->pp_items[i]->input.i_id );
387 msg_Dbg( p_intf, "requested playlist delete: %d",
392 if( p_items ) free( p_items );
397 playlist_LockClear( p_sys->p_playlist );
398 msg_Dbg( p_intf, "requested playlist empty" );
409 E_(uri_extract_value)( p_request, "type", type, 12 );
410 E_(uri_extract_value)( p_request, "order", order, 2 );
411 E_(uri_extract_value)( p_request, "item", item, 512 );
412 i_item = atoi( item );
414 if( order[0] == '0' ) i_order = ORDER_NORMAL;
415 else i_order = ORDER_REVERSE;
417 if( !strcmp( type , "title" ) )
419 playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
420 p_sys->p_playlist->pp_views[0]->p_root,
421 SORT_TITLE_NODES_FIRST,
422 ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
423 msg_Dbg( p_intf, "requested playlist sort by title (%d)" , i_order );
425 else if( !strcmp( type , "author" ) )
427 playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
428 p_sys->p_playlist->pp_views[0]->p_root,
430 ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
431 msg_Dbg( p_intf, "requested playlist sort by author (%d)" , i_order );
433 else if( !strcmp( type , "shuffle" ) )
435 playlist_RecursiveNodeSort( p_sys->p_playlist, /*playlist_ItemGetById( p_sys->p_playlist, i_item ),*/
436 p_sys->p_playlist->pp_views[0]->p_root,
438 ( i_order == 0 ) ? ORDER_NORMAL : ORDER_REVERSE );
439 msg_Dbg( p_intf, "requested playlist shuffle");
450 E_(uri_extract_value)( p_request, "psz_pos", psz_pos, 6 );
451 E_(uri_extract_value)( p_request, "psz_newpos", psz_newpos, 6 );
452 i_pos = atoi( psz_pos );
453 i_newpos = atoi( psz_newpos );
454 if ( i_pos < i_newpos )
456 playlist_Move( p_sys->p_playlist, i_pos, i_newpos + 1 );
460 playlist_Move( p_sys->p_playlist, i_pos, i_newpos );
462 msg_Dbg( p_intf, "requested move playlist item %d to %d", i_pos, i_newpos);
470 E_(uri_extract_value)( p_request, "id", id, 512 );
471 msg_Dbg( p_intf, "requested close id=%s", id );
473 if( p_sys->p_httpd->pf_control( p_sys->p_httpd, HTTPD_SET_CLOSE, id, NULL ) )
475 msg_Warn( p_intf, "close failed for id=%s", id );
482 msg_Dbg( p_intf, "requested shutdown" );
483 p_intf->p_vlc->b_die = VLC_TRUE;
490 static const char *vlm_properties[11] =
493 "enabled", "disabled", "loop", "unloop",
495 "input", "output", "option", "date", "period", "repeat", "append",
497 vlm_message_t *vlm_answer;
499 char *psz = malloc( strlen( p_request ) + 1000 );
504 if( p_intf->p_sys->p_vlm == NULL )
505 p_intf->p_sys->p_vlm = vlm_New( p_intf );
507 if( p_intf->p_sys->p_vlm == NULL ) break;
509 E_(uri_extract_value)( p_request, "name", name, 512 );
510 if( StrToMacroType( control ) == MVLC_VLM_NEW )
513 E_(uri_extract_value)( p_request, "type", type, 20 );
514 p += sprintf( psz, "new %s %s", name, type );
518 p += sprintf( psz, "setup %s", name );
520 /* Parse the request */
521 for( i = 0; i < 11; i++ )
524 E_(uri_extract_value)( p_request,
525 vlm_properties[i], val, 512 );
526 E_(uri_decode_url_encoded)( val );
527 if( strlen( val ) > 0 && i >= 4 )
529 p += sprintf( p, " %s %s", vlm_properties[i], val );
531 else if( E_(uri_test_param)( p_request, vlm_properties[i] ) && i < 4 )
533 p += sprintf( p, " %s", vlm_properties[i] );
536 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
537 if( vlm_answer->psz_value == NULL ) /* there is no error */
539 vlm_error = strdup( "" );
543 vlm_error = malloc( strlen(vlm_answer->psz_name) +
544 strlen(vlm_answer->psz_value) +
545 strlen( " : ") + 1 );
546 sprintf( vlm_error , "%s : %s" , vlm_answer->psz_name,
547 vlm_answer->psz_value );
550 mvar_AppendNewVar( p_args->vars, "vlm_error", vlm_error );
552 vlm_MessageDelete( vlm_answer );
560 vlm_message_t *vlm_answer;
563 if( p_intf->p_sys->p_vlm == NULL )
564 p_intf->p_sys->p_vlm = vlm_New( p_intf );
566 if( p_intf->p_sys->p_vlm == NULL ) break;
568 E_(uri_extract_value)( p_request, "name", name, 512 );
569 sprintf( psz, "del %s", name );
571 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
572 /* FIXME do a vlm_answer -> var stack conversion */
573 vlm_MessageDelete( vlm_answer );
582 vlm_message_t *vlm_answer;
585 if( p_intf->p_sys->p_vlm == NULL )
586 p_intf->p_sys->p_vlm = vlm_New( p_intf );
588 if( p_intf->p_sys->p_vlm == NULL ) break;
590 E_(uri_extract_value)( p_request, "name", name, 512 );
591 if( StrToMacroType( control ) == MVLC_VLM_PLAY )
592 sprintf( psz, "control %s play", name );
593 else if( StrToMacroType( control ) == MVLC_VLM_PAUSE )
594 sprintf( psz, "control %s pause", name );
595 else if( StrToMacroType( control ) == MVLC_VLM_STOP )
596 sprintf( psz, "control %s stop", name );
597 else if( StrToMacroType( control ) == MVLC_VLM_SEEK )
600 E_(uri_extract_value)( p_request, "percent", percent, 512 );
601 sprintf( psz, "control %s seek %s", name, percent );
604 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
605 /* FIXME do a vlm_answer -> var stack conversion */
606 vlm_MessageDelete( vlm_answer );
612 vlm_message_t *vlm_answer;
616 if( p_intf->p_sys->p_vlm == NULL )
617 p_intf->p_sys->p_vlm = vlm_New( p_intf );
619 if( p_intf->p_sys->p_vlm == NULL ) break;
621 E_(uri_extract_value)( p_request, "file", file, 512 );
622 E_(uri_decode_url_encoded)( file );
624 if( StrToMacroType( control ) == MVLC_VLM_LOAD )
625 sprintf( psz, "load %s", file );
627 sprintf( psz, "save %s", file );
629 vlm_ExecuteCommand( p_intf->p_sys->p_vlm, psz, &vlm_answer );
630 /* FIXME do a vlm_answer -> var stack conversion */
631 vlm_MessageDelete( vlm_answer );
638 PRINTS( "<!-- control param(%s) unsupported -->", control );
650 if( i_request <= 0 ||
651 *m->param1 == '\0' ||
652 strstr( p_request, m->param1 ) == NULL )
656 E_(uri_extract_value)( p_request, m->param1, value, 512 );
657 E_(uri_decode_url_encoded)( value );
659 switch( StrToMacroType( m->param2 ) )
663 config_PutInt( p_intf, m->param1, i );
667 config_PutFloat( p_intf, m->param1, f );
670 config_PutPsz( p_intf, m->param1, value );
673 PRINTS( "<!-- invalid type(%s) in set -->", m->param2 )
684 if( *m->param1 == '\0' )
689 switch( StrToMacroType( m->param2 ) )
692 i = config_GetInt( p_intf, m->param1 );
693 sprintf( value, "%d", i );
696 f = config_GetFloat( p_intf, m->param1 );
697 sprintf( value, "%f", f );
700 psz = config_GetPsz( p_intf, m->param1 );
703 strncpy( value, psz,sizeof( value ) );
705 value[sizeof( value ) - 1] = '\0';
709 msg_Dbg( p_intf, "%d: value = \"%s\"", __LINE__, value );
712 snprintf( value, sizeof( value ),
713 "invalid type(%s) in set", m->param2 );
714 value[sizeof( value ) - 1] = '\0';
717 PRINTS( "%s", value );
726 EvaluateRPN( p_intf, p_args->vars, &p_args->stack, m->param1 );
727 s = SSPop( &p_args->stack );
728 v = mvar_GetValue( p_args->vars, s );
732 v = s = SSPop( &p_args->stack );
740 EvaluateRPN( p_intf, p_args->vars, &p_args->stack, m->param1 );
743 /* Useful to learn stack management */
747 msg_Dbg( p_intf, "stack" );
748 for (i=0;i<(&p_args->stack)->i_stack;i++)
749 msg_Dbg( p_intf, "%d -> %s", i, (&p_args->stack)->stack[i] );
755 PRINTS( "<!-- invalid macro id=`%s' -->", m->id );
756 msg_Dbg( p_intf, "invalid macro id=`%s'", m->id );
764 char *MacroSearch( char *src, char *end, int i_mvlc, vlc_bool_t b_after )
771 if( src + 4 < end && !strncmp( (char *)src, "<vlc", 4 ) )
776 i_skip = MacroParse( &m, src );
778 i_id = StrToMacroType( m.id );
795 if( ( i_mvlc == MVLC_END && i_level == -1 ) ||
796 ( i_mvlc != MVLC_END && i_level == 0 && i_mvlc == i_id ) )
798 return src + ( b_after ? i_skip : 0 );
800 else if( i_level < 0 )
816 void E_(Execute)( httpd_file_sys_t *p_args,
817 char *p_request, int i_request,
818 char **pp_data, int *pi_data,
820 char *_src, char *_end )
822 intf_thread_t *p_intf = p_args->p_intf;
824 char *src, *dup, *end;
827 src = dup = malloc( _end - _src + 1 );
828 end = src +( _end - _src );
830 memcpy( src, _src, _end - _src );
833 /* we parse searching <vlc */
839 p = (char *)strstr( (char *)src, "<vlc" );
840 if( p < end && p == src )
844 src += MacroParse( &m, src );
846 //msg_Dbg( p_intf, "macro_id=%s", m.id );
848 switch( StrToMacroType( m.id ) )
855 char psz_file[MAX_DIR_SIZE];
858 if( m.param1[0] != '/' )
860 strcpy( psz_file, p_args->file );
861 p = strrchr( psz_file, '/' );
863 strcpy( p + 1, m.param1 );
865 strcpy( psz_file, m.param1 );
869 strcpy( psz_file, m.param1 );
872 if( ( f = fopen( psz_file, "r" ) ) == NULL )
874 msg_Warn( p_args->p_intf,
875 "unable to include file %s (%s)",
876 psz_file, strerror(errno) );
880 /* first we load in a temporary buffer */
881 E_(FileLoad)( f, &p_buffer, &i_buffer );
883 /* we parse executing all <vlc /> macros */
884 E_(Execute)( p_args, p_request, i_request, pp_data, pi_data,
885 &dst, &p_buffer[0], &p_buffer[i_buffer] );
895 EvaluateRPN( p_intf, p_args->vars, &p_args->stack, m.param1 );
896 if( SSPopN( &p_args->stack, p_args->vars ) )
904 endif = MacroSearch( src, end, MVLC_END, VLC_TRUE );
908 char *start = MacroSearch( src, endif, MVLC_ELSE, VLC_TRUE );
912 char *stop = MacroSearch( start, endif, MVLC_END, VLC_FALSE );
915 E_(Execute)( p_args, p_request, i_request,
916 pp_data, pi_data, &dst, start, stop );
920 else if( i_test == 1 )
923 if( ( stop = MacroSearch( src, endif, MVLC_ELSE, VLC_FALSE ) ) == NULL )
925 stop = MacroSearch( src, endif, MVLC_END, VLC_FALSE );
929 E_(Execute)( p_args, p_request, i_request,
930 pp_data, pi_data, &dst, src, stop );
939 char *endfor = MacroSearch( src, end, MVLC_END, VLC_TRUE );
941 char *stop = MacroSearch( src, end, MVLC_END, VLC_FALSE );
948 if( !strcmp( m.param2, "integer" ) )
950 char *arg = SSPop( &p_args->stack );
951 index = mvar_IntegerSetNew( m.param1, arg );
954 else if( !strcmp( m.param2, "directory" ) )
956 char *arg = SSPop( &p_args->stack );
957 index = mvar_FileSetNew( p_intf, m.param1, arg );
960 else if( !strcmp( m.param2, "playlist" ) )
962 index = mvar_PlaylistSetNew( p_intf, m.param1,
963 p_intf->p_sys->p_playlist );
965 else if( !strcmp( m.param2, "information" ) )
967 index = mvar_InfoSetNew( p_intf, m.param1,
968 p_intf->p_sys->p_input );
970 else if( !strcmp( m.param2, "program" )
971 || !strcmp( m.param2, "title" )
972 || !strcmp( m.param2, "chapter" )
973 || !strcmp( m.param2, "audio-es" )
974 || !strcmp( m.param2, "video-es" )
975 || !strcmp( m.param2, "spu-es" ) )
977 index = mvar_InputVarSetNew( p_intf, m.param1,
978 p_intf->p_sys->p_input,
981 else if( !strcmp( m.param2, "vlm" ) )
983 if( p_intf->p_sys->p_vlm == NULL )
984 p_intf->p_sys->p_vlm = vlm_New( p_intf );
985 index = mvar_VlmSetNew( m.param1, p_intf->p_sys->p_vlm );
988 else if( !strcmp( m.param2, "hosts" ) )
990 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_HOSTS );
992 else if( !strcmp( m.param2, "urls" ) )
994 index = mvar_HttpdInfoSetNew( m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_URLS );
996 else if( !strcmp( m.param2, "connections" ) )
998 index = mvar_HttpdInfoSetNew(m.param1, p_intf->p_sys->p_httpd, HTTPD_GET_CONNECTIONS);
1001 else if( ( v = mvar_GetVar( p_args->vars, m.param2 ) ) )
1003 index = mvar_Duplicate( v );
1007 msg_Dbg( p_intf, "invalid index constructor (%s)", m.param2 );
1012 for( i_idx = 0; i_idx < index->i_field; i_idx++ )
1014 mvar_t *f = mvar_Duplicate( index->field[i_idx] );
1016 //msg_Dbg( p_intf, "foreach field[%d] name=%s value=%s", i_idx, f->name, f->value );
1019 f->name = strdup( m.param1 );
1022 mvar_PushVar( p_args->vars, f );
1023 E_(Execute)( p_args, p_request, i_request,
1024 pp_data, pi_data, &dst, start, stop );
1025 mvar_RemoveVar( p_args->vars, f );
1029 mvar_Delete( index );
1036 MacroDo( p_args, &m, p_request, i_request,
1037 pp_data, pi_data, &dst );
1045 i_copy = ( (p == NULL || p > end ) ? end : p ) - src;
1048 int i_index = dst - *pp_data;
1051 *pp_data = realloc( *pp_data, *pi_data );
1052 dst = (*pp_data) + i_index;
1054 memcpy( dst, src, i_copy );