Add a growl (http://growl.info/) notification plugin (most of code from msn.c)
[vlc.git] / modules / control / telnet.c
1 /*****************************************************************************
2  * telnet.c: VLM interface plugin
3  *****************************************************************************
4  * Copyright (C) 2000-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Simon Latapie <garf@videolan.org>
8  *          Laurent Aimar <fenrir@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29
30 #include <vlc/vlc.h>
31 #include <vlc/intf.h>
32
33 #include <vlc/input.h>
34
35 #include <sys/stat.h>
36
37 #include <errno.h>
38 #include <fcntl.h>
39
40 #ifdef HAVE_SYS_TIME_H
41 #    include <sys/time.h>
42 #endif
43
44 #ifdef HAVE_UNISTD_H
45 #   include <unistd.h>
46 #endif
47
48 #if defined( UNDER_CE )
49 #   include <winsock.h>
50 #elif defined( WIN32 )
51 #   include <winsock2.h>
52 #else
53 #   include <sys/socket.h>
54 #endif
55
56 #include "network.h"
57
58 #include "vlc_vlm.h"
59
60 #define READ_MODE_PWD 1
61 #define READ_MODE_CMD 2
62 #define WRITE_MODE_PWD 3 // when we write the word "Password:"
63 #define WRITE_MODE_CMD 4
64
65 /* telnet commands */
66 #define TEL_WILL    251
67 #define TEL_WONT    252
68 #define TEL_DO      253
69 #define TEL_DONT    254
70 #define TEL_IAC     255
71 #define TEL_ECHO    1
72
73 /*****************************************************************************
74  * Module descriptor
75  *****************************************************************************/
76 static int  Open ( vlc_object_t * );
77 static void Close( vlc_object_t * );
78
79 #define TELNETHOST_TEXT N_( "Telnet Interface host" )
80 #define TELNETHOST_LONGTEXT N_( "Default to listen on all network interfaces" )
81 #define TELNETPORT_TEXT N_( "Telnet Interface port" )
82 #define TELNETPORT_LONGTEXT N_( "Default to 4212" )
83 #define TELNETPORT_DEFAULT 4212
84 #define TELNETPWD_TEXT N_( "Telnet Interface password" )
85 #define TELNETPWD_LONGTEXT N_( "Default to admin" )
86 #define TELNETPWD_DEFAULT "admin"
87
88 vlc_module_begin();
89     set_shortname( "Telnet" );
90     set_category( CAT_INTERFACE );
91     set_subcategory( SUBCAT_INTERFACE_GENERAL );
92     add_string( "telnet-host", "", NULL, TELNETHOST_TEXT,
93                  TELNETHOST_LONGTEXT, VLC_TRUE );
94     add_integer( "telnet-port", TELNETPORT_DEFAULT, NULL, TELNETPORT_TEXT,
95                  TELNETPORT_LONGTEXT, VLC_TRUE );
96     add_string( "telnet-password", TELNETPWD_DEFAULT, NULL, TELNETPWD_TEXT,
97                 TELNETPWD_LONGTEXT, VLC_TRUE );
98     set_description( _("VLM remote control interface") );
99     add_category_hint( "VLM", NULL, VLC_FALSE );
100     set_capability( "interface", 0 );
101     set_callbacks( Open , Close );
102 vlc_module_end();
103
104 /*****************************************************************************
105  * Local prototypes.
106  *****************************************************************************/
107 static void Run( intf_thread_t * );
108
109 typedef struct
110 {
111     int        i_mode; /* read or write */
112     int        fd;
113     char       buffer_read[1000]; // 1000 byte per command should be sufficient
114     char      *buffer_write;
115     char      *p_buffer_read;
116     char      *p_buffer_write; // the position in the buffer
117     int        i_buffer_write; // the number of byte we still have to send
118     int        i_tel_cmd; // for specific telnet commands
119
120 } telnet_client_t;
121
122 static char *MessageToString( vlm_message_t *, int );
123 static void Write_message( telnet_client_t *, vlm_message_t *, char *, int );
124
125 struct intf_sys_t
126 {
127    telnet_client_t **clients;
128    int             i_clients;
129    int             *pi_fd;
130    vlm_t           *mediatheque;
131 };
132
133 /* 
134  * getPort: Decide which port to use. There are two possibilities to
135  * specify a port: integrated in the --telnet-host option with :PORT
136  * or using the --telnet-port option. The --telnet-port option has
137  * precedence. 
138  * This code relies upon the fact the url.i_port is 0 if the :PORT 
139  * option is missing from --telnet-host.
140  */
141 static int getPort(intf_thread_t *p_intf, vlc_url_t url, int i_port)
142     {
143     // Print error if two different ports have been specified
144     if (url.i_port != 0  &&
145         i_port != TELNETPORT_DEFAULT && 
146         url.i_port != i_port )
147     {
148             msg_Err( p_intf, "ignoring port %d and using %d", url.i_port,
149                  i_port);
150     }
151     if (i_port != TELNETPORT_DEFAULT)
152     {
153             return i_port;
154     }
155     if (url.i_port != 0)
156     {
157             return url.i_port;
158     }
159     // return default
160     return i_port;
161 }
162
163 /*****************************************************************************
164  * Open: initialize dummy interface
165  *****************************************************************************/
166 static int Open( vlc_object_t *p_this )
167 {
168     intf_thread_t *p_intf = (intf_thread_t*) p_this;
169     vlm_t *mediatheque;
170     char *psz_address;
171     vlc_url_t url;
172     int i_telnetport;
173
174     if( !(mediatheque = vlm_New( p_intf )) )
175     {
176         msg_Err( p_intf, "cannot start VLM" );
177         return VLC_EGENERIC;
178     }
179
180     msg_Info( p_intf, "Using the VLM interface plugin..." );
181
182     i_telnetport = config_GetInt( p_intf, "telnet-port" );
183     psz_address  = config_GetPsz( p_intf, "telnet-host" );
184
185     vlc_UrlParse(&url, psz_address, 0);
186
187     // There might be two ports given, resolve any potentially
188     // conflict
189     url.i_port = getPort(p_intf, url, i_telnetport);
190
191     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
192     if( ( p_intf->p_sys->pi_fd = net_ListenTCP( p_intf, url.psz_host, url.i_port ) )
193                 == NULL )
194     {
195         msg_Err( p_intf, "cannot listen for telnet" );
196         vlc_UrlClean(&url);
197         free( psz_address );
198         free( p_intf->p_sys );
199         return VLC_EGENERIC;
200     }
201     msg_Info( p_intf, 
202               "Telnet interface started on interface %s %d",
203               url.psz_host, url.i_port );
204
205     p_intf->p_sys->i_clients   = 0;
206     p_intf->p_sys->clients     = NULL;
207     p_intf->p_sys->mediatheque = mediatheque;
208     p_intf->pf_run = Run;
209
210     vlc_UrlClean(&url);
211     free( psz_address );
212     return VLC_SUCCESS;
213 }
214
215 /*****************************************************************************
216  * Close:
217  *****************************************************************************/
218 static void Close( vlc_object_t *p_this )
219 {
220     intf_thread_t *p_intf = (intf_thread_t*)p_this;
221     intf_sys_t    *p_sys  = p_intf->p_sys;
222     int i;
223
224     for( i = 0; i < p_sys->i_clients; i++ )
225     {
226         telnet_client_t *cl = p_sys->clients[i];
227
228         net_Close( cl->fd );
229         free( cl );
230     }
231     if( p_sys->clients != NULL ) free( p_sys->clients );
232
233     net_ListenClose( p_sys->pi_fd );
234
235     vlm_Delete( p_sys->mediatheque );
236
237     free( p_sys );
238 }
239
240 /*****************************************************************************
241  * Run: main loop
242  *****************************************************************************/
243 static void Run( intf_thread_t *p_intf )
244 {
245     intf_sys_t     *p_sys = p_intf->p_sys;
246     struct timeval  timeout;
247     char           *psz_password;
248
249     psz_password = config_GetPsz( p_intf, "telnet-password" );
250
251     while( !p_intf->b_die )
252     {
253         fd_set fds_read, fds_write;
254         int    i_handle_max = 0;
255         int    i_ret, i_len, fd, i;
256
257         /* if a new client wants to communicate */
258         fd = net_Accept( p_intf, p_sys->pi_fd, p_sys->i_clients > 0 ? 0 : -1 );
259         if( fd > 0 )
260         {
261             telnet_client_t *cl;
262
263             /* to be non blocking */
264 #if defined( WIN32 ) || defined( UNDER_CE )
265             {
266                 unsigned long i_dummy = 1;
267                 ioctlsocket( fd, FIONBIO, &i_dummy );
268             }
269 #else
270             fcntl( fd, F_SETFL, O_NONBLOCK );
271 #endif
272             cl = malloc( sizeof( telnet_client_t ));
273             cl->i_tel_cmd = 0;
274             cl->fd = fd;
275             cl->buffer_write = NULL;
276             cl->p_buffer_write = cl->buffer_write;
277             Write_message( cl, NULL, "Password: \xff\xfb\x01", WRITE_MODE_PWD );
278
279             TAB_APPEND( p_sys->i_clients, p_sys->clients, cl );
280         }
281
282         /* to do a proper select */
283         FD_ZERO( &fds_read );
284         FD_ZERO( &fds_write );
285
286         for( i = 0 ; i < p_sys->i_clients ; i++ )
287         {
288             telnet_client_t *cl = p_sys->clients[i];
289
290             if( cl->i_mode == WRITE_MODE_PWD || cl->i_mode == WRITE_MODE_CMD )
291             {
292                 FD_SET( cl->fd , &fds_write );
293             }
294             else
295             {
296                 FD_SET( cl->fd , &fds_read );
297             }
298             i_handle_max = __MAX( i_handle_max, cl->fd );
299         }
300
301         timeout.tv_sec = 0;
302         timeout.tv_usec = 500*1000;
303
304         i_ret = select( i_handle_max + 1, &fds_read, &fds_write, 0, &timeout );
305         if( i_ret == -1 && errno != EINTR )
306         {
307             msg_Warn( p_intf, "cannot select sockets" );
308             msleep( 1000 );
309             continue;
310         }
311         else if( i_ret <= 0 )
312         {
313             continue;
314         }
315
316         /* check if there is something to do with the socket */
317         for( i = 0 ; i < p_sys->i_clients ; i++ )
318         {
319             telnet_client_t *cl = p_sys->clients[i];
320
321             if( FD_ISSET(cl->fd , &fds_write) && cl->i_buffer_write > 0 )
322             {
323                 i_len = send( cl->fd , cl->p_buffer_write ,
324                               cl->i_buffer_write , 0 );
325                 if( i_len > 0 )
326                 {
327                     cl->p_buffer_write += i_len;
328                     cl->i_buffer_write -= i_len;
329                 }
330             }
331             else if( FD_ISSET( cl->fd, &fds_read) )
332             {
333                 int i_end = 0;
334                 int i_recv;
335
336                 while( (i_recv=recv( cl->fd, cl->p_buffer_read, 1, 0 )) > 0 &&
337                        cl->p_buffer_read - cl->buffer_read < 999 )
338                 {
339                     switch( cl->i_tel_cmd )
340                     {
341                     case 0:
342                         switch( *(uint8_t *)cl->p_buffer_read )
343                         {
344                         case '\r':
345                             break;
346                         case '\n':
347                             *cl->p_buffer_read = '\n';
348                             i_end = 1;
349                             break;
350                         case TEL_IAC: // telnet specific command
351                             cl->i_tel_cmd = 1;
352                             cl->p_buffer_read++;
353                             break;
354                         default:
355                             cl->p_buffer_read++;
356                             break;
357                         }
358                         break;
359                     case 1:
360                         switch( *(uint8_t *)cl->p_buffer_read )
361                         {
362                         case TEL_WILL: case TEL_WONT:
363                         case TEL_DO: case TEL_DONT:
364                             cl->i_tel_cmd++;
365                             cl->p_buffer_read++;
366                             break;
367                         default:
368                             cl->i_tel_cmd = 0;
369                             cl->p_buffer_read--;
370                             break;
371                         }
372                         break;
373                     case 2:
374                         cl->i_tel_cmd = 0;
375                         cl->p_buffer_read -= 2;
376                         break;
377                     }
378
379                     if( i_end != 0 ) break;
380                 }
381
382                 if( cl->p_buffer_read - cl->buffer_read == 999 )
383                 {
384                     Write_message( cl, NULL, "Line too long\r\n",
385                                    cl->i_mode + 2 );
386                 }
387
388                 if (i_recv == 0)
389                 {
390                     net_Close( cl->fd );
391                     TAB_REMOVE( p_intf->p_sys->i_clients ,
392                                 p_intf->p_sys->clients , cl );
393                     free( cl );
394                 }
395             }
396         }
397
398         /* and now we should bidouille the data we received / send */
399         for( i = 0 ; i < p_sys->i_clients ; i++ )
400         {
401             telnet_client_t *cl = p_sys->clients[i];
402
403             if( cl->i_mode >= WRITE_MODE_PWD && cl->i_buffer_write == 0 )
404             {
405                // we have finished to send
406                cl->i_mode -= 2; // corresponding READ MODE
407             }
408             else if( cl->i_mode == READ_MODE_PWD &&
409                      *cl->p_buffer_read == '\n' )
410             {
411                 *cl->p_buffer_read = '\0';
412                 if( strcmp( psz_password, cl->buffer_read ) == 0 )
413                 {
414                     Write_message( cl, NULL, "\xff\xfc\x01\r\nWelcome, "
415                                    "Master\r\n> ", WRITE_MODE_CMD );
416                 }
417                 else
418                 {
419                     /* wrong password */
420                     Write_message( cl, NULL,
421                                    "\r\nWrong password.\r\nPassword: ",
422                                    WRITE_MODE_PWD );
423                 }
424             }
425             else if( cl->i_mode == READ_MODE_CMD &&
426                      *cl->p_buffer_read == '\n' )
427             {
428                 /* ok, here is a command line */
429                 if( !strncmp( cl->buffer_read, "logout", 6 ) ||
430                     !strncmp( cl->buffer_read, "quit", 4 )  ||
431                     !strncmp( cl->buffer_read, "exit", 4 ) )
432                 {
433                     net_Close( cl->fd );
434                     TAB_REMOVE( p_intf->p_sys->i_clients ,
435                                 p_intf->p_sys->clients , cl );
436                     free( cl );
437                 }
438                 else if( !strncmp( cl->buffer_read, "shutdown", 8 ) )
439                 {
440                     msg_Err( p_intf, "shutdown requested" );
441                     p_intf->p_vlc->b_die = VLC_TRUE;
442                 }
443                 else
444                 {
445                     vlm_message_t *message;
446
447                     /* create a standard string */
448                     *cl->p_buffer_read = '\0';
449
450                     vlm_ExecuteCommand( p_sys->mediatheque, cl->buffer_read,
451                                         &message );
452                     Write_message( cl, message, NULL, WRITE_MODE_CMD );
453                     vlm_MessageDelete( message );
454                 }
455             }
456         }
457     }
458 }
459
460 static void Write_message( telnet_client_t *client, vlm_message_t *message,
461                            char *string_message, int i_mode )
462 {
463     char *psz_message;
464
465     client->p_buffer_read = client->buffer_read;
466     (client->p_buffer_read)[0] = 0; // if (cl->p_buffer_read)[0] = '\n'
467     if( client->buffer_write ) free( client->buffer_write );
468
469     /* generate the psz_message string */
470     if( message )
471     {
472         /* ok, look for vlm_message_t */
473         psz_message = MessageToString( message, 0 );
474     }
475     else
476     {
477         /* it is a basic string_message */
478         psz_message = strdup( string_message );
479     }
480
481     client->buffer_write = client->p_buffer_write = psz_message;
482     client->i_buffer_write = strlen( psz_message );
483     client->i_mode = i_mode;
484 }
485
486 /* We need the level of the message to put a beautiful indentation.
487  * first level is 0 */
488 static char *MessageToString( vlm_message_t *message, int i_level )
489 {
490 #define STRING_CR "\r\n"
491 #define STRING_TAIL "> "
492
493     char *psz_message;
494     int i, i_message = sizeof( STRING_TAIL );
495
496     if( !message || !message->psz_name )
497     {
498         return strdup( STRING_CR STRING_TAIL );
499     }
500     else if( !i_level && !message->i_child && !message->psz_value  )
501     {
502         /* A command is successful. Don't write anything */
503         return strdup( STRING_CR STRING_TAIL );
504     }
505
506     i_message += strlen( message->psz_name ) + i_level * sizeof( "    " ) + 1;
507     psz_message = malloc( i_message ); *psz_message = 0;
508     for( i = 0; i < i_level; i++ ) strcat( psz_message, "    " );
509     strcat( psz_message, message->psz_name );
510
511     if( message->psz_value )
512     {
513         i_message += sizeof( " : " ) + strlen( message->psz_value ) +
514             sizeof( STRING_CR );
515         psz_message = realloc( psz_message, i_message );
516         strcat( psz_message, " : " );
517         strcat( psz_message, message->psz_value );
518         strcat( psz_message, STRING_CR );
519     }
520     else
521     {
522         i_message += sizeof( STRING_CR );
523         psz_message = realloc( psz_message, i_message );
524         strcat( psz_message, STRING_CR );
525     }
526
527     for( i = 0; i < message->i_child; i++ )
528     {
529         char *child_message =
530             MessageToString( message->child[i], i_level + 1 );
531
532         i_message += strlen( child_message );
533         psz_message = realloc( psz_message, i_message );
534         strcat( psz_message, child_message );
535         free( child_message );
536     }
537
538     if( i_level == 0 ) strcat( psz_message, STRING_TAIL );
539
540     return psz_message;
541 }