1 /****************************************************************************
3 * $Id: linphonec.c,v 1.57 2007/11/14 13:40:27 smorlat Exp $
5 * Copyright (C) 2006 Sandro Santilli <strk@keybit.net>
6 * Copyright (C) 2002 Florian Winterstein <flox@gmx.net>
7 * Copyright (C) 2000 Simon MORLAT <simon.morlat@free.fr>
9 ****************************************************************************
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (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-1307, USA.
25 ****************************************************************************/
29 #include <sys/types.h>
33 #include "private.h" /*coreapi/private.h, needed for LINPHONE_VERSION */
38 #include <linphonecore.h>
40 #include "linphonec.h"
49 #include <sys/socket.h>
55 #if defined(_WIN32_WCE)
57 #if !defined(PATH_MAX)
62 #define strdup _strdup
70 #define _(String) gettext(String)
73 #define _(something) (something)
77 #define PACKAGE_DIR ""
80 #ifdef HAVE_X11_XLIB_H
82 #include <SDL/SDL_syswm.h>
85 /***************************************************************************
89 ***************************************************************************/
92 LinphoneAuthInfo *elem[MAX_PENDING_AUTH];
96 /***************************************************************************
98 * Forward declarations
100 ***************************************************************************/
102 char *lpc_strip_blanks(char *input);
104 static int handle_configfile_migration(void);
105 #if !defined(_WIN32_WCE)
106 static int copy_file(const char *from, const char *to);
107 #endif /*_WIN32_WCE*/
108 static int linphonec_parse_cmdline(int argc, char **argv);
109 static int linphonec_init(int argc, char **argv);
110 static int linphonec_main_loop (LinphoneCore * opm, char * sipAddr);
111 static int linphonec_idle_call (void);
113 static int linphonec_initialize_readline(void);
114 static int linphonec_finish_readline();
115 static char **linephonec_readline_completion(const char *text,
119 /* These are callback for linphone core */
120 static void linphonec_prompt_for_auth(LinphoneCore *lc, const char *realm,
121 const char *username);
122 static void linphonec_display_refer (LinphoneCore * lc, const char *refer_to);
123 static void linphonec_display_something (LinphoneCore * lc, const char *something);
124 static void linphonec_display_url (LinphoneCore * lc, const char *something, const char *url);
125 static void linphonec_display_warning (LinphoneCore * lc, const char *something);
126 static void linphonec_notify_received(LinphoneCore *lc, LinphoneCall *call, const char *from,const char *event);
128 static void linphonec_notify_presence_received(LinphoneCore *lc,LinphoneFriend *fid);
129 static void linphonec_new_unknown_subscriber(LinphoneCore *lc,
130 LinphoneFriend *lf, const char *url);
132 static void linphonec_text_received(LinphoneCore *lc, LinphoneChatRoom *cr,
133 const char *from, const char *msg);
134 static void linphonec_display_status (LinphoneCore * lc, const char *something);
135 static void linphonec_dtmf_received(LinphoneCore *lc, LinphoneCall *call, int dtmf);
136 static void print_prompt(LinphoneCore *opm);
137 void linphonec_out(const char *fmt,...);
138 /***************************************************************************
142 ***************************************************************************/
144 LinphoneCore *linphonec;
147 static char *histfile_name=NULL;
148 static char last_in_history[256];
150 //auto answer (-a) option
151 static bool_t auto_answer=FALSE;
152 static bool_t answer_call=FALSE;
153 static bool_t vcap_enabled=FALSE;
154 static bool_t display_enabled=FALSE;
155 static bool_t preview_enabled=FALSE;
156 static bool_t show_general_state=FALSE;
157 static bool_t unix_socket=FALSE;
158 static bool_t linphonec_running=TRUE;
159 LPC_AUTH_STACK auth_stack;
160 static int trace_level = 0;
161 static char *logfile_name = NULL;
162 static char configfile_name[PATH_MAX];
163 static const char *factory_configfile_name=NULL;
164 static char *sipAddr = NULL; /* for autocall */
165 #if !defined(_WIN32_WCE)
166 static ortp_pipe_t client_sock=ORTP_PIPE_INVALID;
167 #endif /*_WIN32_WCE*/
168 char prompt[PROMPT_MAX_LEN];
169 #if !defined(_WIN32_WCE)
170 static ortp_thread_t pipe_reader_th;
171 static bool_t pipe_reader_run=FALSE;
172 #endif /*_WIN32_WCE*/
173 #if !defined(_WIN32_WCE)
174 static ortp_pipe_t server_sock;
175 #endif /*_WIN32_WCE*/
177 bool_t linphonec_camera_enabled=TRUE;
180 extern VideoParams lpc_video_params;
182 void linphonec_call_identify(LinphoneCall* call){
183 static long callid=1;
184 linphone_call_set_user_pointer (call,(void*)callid);
188 LinphoneCall *linphonec_get_call(long id){
189 const MSList *elem=linphone_core_get_calls(linphonec);
190 for (;elem!=NULL;elem=elem->next){
191 LinphoneCall *call=(LinphoneCall*)elem->data;
192 if (linphone_call_get_user_pointer (call)==(void*)id){
196 linphonec_out("Sorry, no call with id %i exists at this time.\n",id);
200 /***************************************************************************
202 * Linphone core callbacks
204 ***************************************************************************/
207 * Linphone core callback
210 linphonec_display_refer (LinphoneCore * lc, const char *refer_to)
212 linphonec_out("Receiving out of call refer to %s\n", refer_to);
216 * Linphone core callback
219 linphonec_display_something (LinphoneCore * lc, const char *something)
221 fprintf (stdout, "%s\n%s", something,prompt);
226 * Linphone core callback
229 linphonec_display_status (LinphoneCore * lc, const char *something)
231 fprintf (stdout, "%s\n%s", something,prompt);
236 * Linphone core callback
239 linphonec_display_warning (LinphoneCore * lc, const char *something)
241 fprintf (stdout, "Warning: %s\n%s", something,prompt);
246 * Linphone core callback
249 linphonec_display_url (LinphoneCore * lc, const char *something, const char *url)
251 fprintf (stdout, "%s : %s\n", something, url);
255 * Linphone core callback
258 linphonec_prompt_for_auth(LinphoneCore *lc, const char *realm, const char *username)
260 /* no prompt possible when using pipes or tcp mode*/
262 linphone_core_abort_authentication(lc,NULL);
264 LinphoneAuthInfo *pending_auth;
266 if ( auth_stack.nitems+1 > MAX_PENDING_AUTH )
269 "Can't accept another authentication request.\n"
270 "Consider incrementing MAX_PENDING_AUTH macro.\n");
274 pending_auth=linphone_auth_info_new(username,NULL,NULL,NULL,realm);
275 auth_stack.elem[auth_stack.nitems++]=pending_auth;
280 * Linphone core callback
283 linphonec_notify_received(LinphoneCore *lc, LinphoneCall *call, const char *from,const char *event)
285 if(!strcmp(event,"refer"))
287 linphonec_out("The distand endpoint %s of call %li has been transfered, you can safely close the call.\n",
288 from,(long)linphone_call_get_user_pointer (call));
294 * Linphone core callback
297 linphonec_notify_presence_received(LinphoneCore *lc,LinphoneFriend *fid)
299 char *tmp=linphone_address_as_string(linphone_friend_get_address(fid));
300 printf("Friend %s is %s\n", tmp, linphone_online_status_to_string(linphone_friend_get_status(fid)));
302 // todo: update Friend list state (unimplemented)
306 * Linphone core callback
309 linphonec_new_unknown_subscriber(LinphoneCore *lc, LinphoneFriend *lf,
312 printf("Friend %s requested subscription "
313 "(accept/deny is not implemented yet)\n", url);
314 // This means that this person wishes to be notified
315 // of your presence information (online, busy, away...).
319 static void linphonec_call_updated(LinphoneCall *call){
320 const LinphoneCallParams *cp=linphone_call_get_current_params(call);
321 if (!linphone_call_camera_enabled (call) && linphone_call_params_video_enabled (cp)){
322 linphonec_out("Far end requests to share video.\nType 'camera on' if you agree.\n");
326 static void linphonec_call_state_changed(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState st, const char *msg){
327 char *from=linphone_call_get_remote_address_as_string(call);
328 long id=(long)linphone_call_get_user_pointer (call);
330 case LinphoneCallEnd:
331 linphonec_out("Call %i with %s ended.\n", id, from);
333 case LinphoneCallResuming:
334 linphonec_out("Resuming call %i with %s.\n", id, from);
336 case LinphoneCallStreamsRunning:
337 linphonec_out("Media streams established with %s for call %i.\n", from,id);
339 case LinphoneCallPausing:
340 linphonec_out("Pausing call %i with %s.\n", id, from);
342 case LinphoneCallPaused:
343 linphonec_out("Call %i with %s is now paused.\n", id, from);
345 case LinphoneCallPausedByRemote:
346 linphonec_out("Call %i has been paused by %s.\n",id,from);
348 case LinphoneCallIncomingReceived:
349 linphonec_call_identify(call);
350 linphone_call_enable_camera (call,linphonec_camera_enabled);
351 id=(long)linphone_call_get_user_pointer (call);
352 linphonec_set_caller(from);
356 linphonec_out("Receiving new incoming call from %s, assigned id %i\n", from,id);
358 case LinphoneCallOutgoingInit:
359 linphonec_call_identify(call);
361 case LinphoneCallUpdatedByRemote:
362 linphonec_call_updated(call);
371 * Linphone core callback
374 linphonec_text_received(LinphoneCore *lc, LinphoneChatRoom *cr,
375 const char *from, const char *msg)
377 printf("%s: %s\n", from, msg);
378 // TODO: provide mechanism for answering.. ('say' command?)
382 static void linphonec_dtmf_received(LinphoneCore *lc, LinphoneCall *call, int dtmf){
383 char *from=linphone_call_get_remote_address_as_string(call);
384 fprintf(stdout,"Receiving tone %c from %s\n",dtmf,from);
389 static char received_prompt[PROMPT_MAX_LEN];
390 static ms_mutex_t prompt_mutex;
391 static bool_t have_prompt=FALSE;
393 static void *prompt_reader_thread(void *arg){
395 char tmp[PROMPT_MAX_LEN];
396 while ((ret=fgets(tmp,sizeof(tmp),stdin))!=NULL){
397 ms_mutex_lock(&prompt_mutex);
398 strcpy(received_prompt,ret);
400 ms_mutex_unlock(&prompt_mutex);
405 static void start_prompt_reader(void){
407 ms_mutex_init(&prompt_mutex,NULL);
408 ortp_thread_create(&th,NULL,prompt_reader_thread,NULL);
410 #if !defined(_WIN32_WCE)
411 static ortp_pipe_t create_server_socket(void){
414 snprintf(path,sizeof(path)-1,"linphonec-%i",getuid());
418 DWORD size=sizeof(username)-1;
419 GetUserName(username,&size);
420 snprintf(path,sizeof(path)-1,"linphonec-%s",username);
423 return ortp_server_pipe_create(path);
427 static void *pipe_thread(void*p){
429 server_sock=create_server_socket();
430 if (server_sock==ORTP_PIPE_INVALID) return NULL;
431 while(pipe_reader_run){
432 while(client_sock!=ORTP_PIPE_INVALID){ /*sleep until the last command is finished*/
439 client_sock=ortp_server_pipe_accept_client(server_sock);
440 if (client_sock!=ORTP_PIPE_INVALID){
442 /*now read from the client */
443 if ((len=ortp_pipe_read(client_sock,(uint8_t*)tmp,sizeof(tmp)-1))>0){
444 ortp_mutex_lock(&prompt_mutex);
446 strcpy(received_prompt,tmp);
447 printf("Receiving command '%s'\n",received_prompt);fflush(stdout);
449 ortp_mutex_unlock(&prompt_mutex);
451 printf("read nothing\n");fflush(stdout);
452 ortp_server_pipe_close_client(client_sock);
453 client_sock=ORTP_PIPE_INVALID;
457 if (pipe_reader_run) fprintf(stderr,"accept() failed: %s\n",strerror(errno));
460 ms_message("Exiting pipe_reader_thread.");
465 static void start_pipe_reader(void){
466 ms_mutex_init(&prompt_mutex,NULL);
467 pipe_reader_run=TRUE;
468 ortp_thread_create(&pipe_reader_th,NULL,pipe_thread,NULL);
471 static void stop_pipe_reader(void){
472 pipe_reader_run=FALSE;
473 linphonec_command_finished();
474 ortp_server_pipe_close(server_sock);
475 ortp_thread_join(pipe_reader_th,NULL);
477 #endif /*_WIN32_WCE*/
480 #define BOOL_HAVE_READLINE 1
482 #define BOOL_HAVE_READLINE 0
485 char *linphonec_readline(char *prompt){
486 if (unix_socket || !BOOL_HAVE_READLINE ){
487 static bool_t prompt_reader_started=FALSE;
488 static bool_t pipe_reader_started=FALSE;
489 if (!prompt_reader_started){
490 start_prompt_reader();
491 prompt_reader_started=TRUE;
493 if (unix_socket && !pipe_reader_started){
494 #if !defined(_WIN32_WCE)
496 pipe_reader_started=TRUE;
497 #endif /*_WIN32_WCE*/
499 fprintf(stdout,"%s",prompt);
502 ms_mutex_lock(&prompt_mutex);
504 char *ret=strdup(received_prompt);
506 ms_mutex_unlock(&prompt_mutex);
509 ms_mutex_unlock(&prompt_mutex);
510 linphonec_idle_call();
513 /* Following is to get the video window going as it
514 should. Maybe should we only have this on when the option -V
518 if (PeekMessage(&msg, NULL, 0, 0,1)) {
519 TranslateMessage(&msg);
520 DispatchMessage(&msg);
528 return readline(prompt);
533 void linphonec_out(const char *fmt,...){
536 va_start (args, fmt);
537 res=ortp_strdup_vprintf(fmt,args);
541 #if !defined(_WIN32_WCE)
542 if (client_sock!=ORTP_PIPE_INVALID){
543 if (ortp_pipe_write(client_sock,(uint8_t*)res,strlen(res))==-1){
544 fprintf(stderr,"Fail to send output via pipe: %s",strerror(errno));
547 #endif /*_WIN32_WCE*/
551 void linphonec_command_finished(void){
552 #if !defined(_WIN32_WCE)
553 if (client_sock!=ORTP_PIPE_INVALID){
554 ortp_server_pipe_close_client(client_sock);
555 client_sock=ORTP_PIPE_INVALID;
557 #endif /*_WIN32_WCE*/
560 void linphonec_set_autoanswer(bool_t enabled){
564 bool_t linphonec_get_autoanswer(){
568 LinphoneCoreVTable linphonec_vtable={0};
570 /***************************************************************************/
576 * - char *histfile_name
579 #if defined (_WIN32_WCE)
581 char **convert_args_to_ascii(int argc, _TCHAR **wargv){
583 char **result=malloc(argc*sizeof(char*));
586 wcstombs(argtmp,wargv[i],sizeof(argtmp));
587 result[i]=strdup(argtmp);
592 int _tmain(int argc, _TCHAR* wargv[]) {
593 char **argv=convert_args_to_ascii(argc,wargv);
598 main (int argc, char *argv[]) {
600 linphonec_vtable.call_state_changed=linphonec_call_state_changed;
601 linphonec_vtable.notify_presence_recv = linphonec_notify_presence_received;
602 linphonec_vtable.new_unknown_subscriber = linphonec_new_unknown_subscriber;
603 linphonec_vtable.auth_info_requested = linphonec_prompt_for_auth;
604 linphonec_vtable.display_status = linphonec_display_status;
605 linphonec_vtable.display_message=linphonec_display_something;
606 linphonec_vtable.display_warning=linphonec_display_warning;
607 linphonec_vtable.display_url=linphonec_display_url;
608 linphonec_vtable.text_received=linphonec_text_received;
609 linphonec_vtable.dtmf_received=linphonec_dtmf_received;
610 linphonec_vtable.refer_received=linphonec_display_refer;
611 linphonec_vtable.notify_recv=linphonec_notify_received;
613 if (! linphonec_init(argc, argv) ) exit(EXIT_FAILURE);
615 linphonec_main_loop (linphonec, sipAddr);
617 linphonec_finish(EXIT_SUCCESS);
619 exit(EXIT_SUCCESS); /* should never reach here */
623 * Initialize linphonec
626 linphonec_init(int argc, char **argv)
629 //g_mem_set_vtable(&dbgtable);
632 * Set initial values for global variables
638 snprintf(configfile_name, PATH_MAX, "%s/.linphonerc",
640 #elif defined(_WIN32_WCE)
641 strncpy(configfile_name,PACKAGE_DIR "\\linphonerc",PATH_MAX);
642 mylogfile=fopen(PACKAGE_DIR "\\" "linphonec.log","w");
643 printf("Logs are redirected in" PACKAGE_DIR "\\linphonec.log");
645 snprintf(configfile_name, PATH_MAX, "%s/Linphone/linphonerc",
648 /* Handle configuration filename changes */
649 switch (handle_configfile_migration())
651 case -1: /* error during file copies */
653 "Error in configuration file migration\n");
656 case 0: /* nothing done */
657 case 1: /* migrated */
663 if (NULL == bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR))
664 perror ("bindtextdomain failed");
666 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
668 textdomain (GETTEXT_PACKAGE);
670 printf ("NLS disabled.\n");
673 linphonec_parse_cmdline(argc, argv);
677 if (logfile_name != NULL)
678 mylogfile = fopen (logfile_name, "w+");
680 if (mylogfile == NULL)
684 "INFO: no logfile, logging to stdout\n");
686 linphone_core_enable_logs(mylogfile);
690 linphone_core_disable_logs();
693 * Initialize auth stack
698 * Initialize linphone core
700 linphonec=linphone_core_new (&linphonec_vtable, configfile_name, factory_configfile_name, NULL);
701 linphone_core_enable_video(linphonec,vcap_enabled,display_enabled);
702 linphone_core_enable_video_preview(linphonec,preview_enabled);
703 if (!(vcap_enabled || display_enabled)) printf("Warning: video is disabled in linphonec, use -V or -C or -D to enable.\n");
706 * Initialize readline
708 linphonec_initialize_readline();
710 #if !defined(_WIN32_WCE)
712 * Initialize signal handlers
714 signal(SIGTERM, linphonec_finish);
715 signal(SIGINT, linphonec_finish);
716 #endif /*_WIN32_WCE*/
721 void linphonec_main_loop_exit(void){
722 linphonec_running=FALSE;
726 * Close linphonec, cleanly terminating
730 linphonec_finish(int exit_status)
732 // Do not allow concurrent destroying to prevent glibc errors
733 static bool_t terminating=FALSE;
734 if (terminating) return;
736 linphonec_out("Terminating...\n");
738 /* Terminate any pending call */
739 linphone_core_terminate_all_calls(linphonec);
741 linphonec_finish_readline();
743 #if !defined(_WIN32_WCE)
746 #endif /*_WIN32_WCE*/
748 linphone_core_destroy (linphonec);
750 if (mylogfile != NULL && mylogfile != stdout)
760 * This is called from idle_call() whenever
761 * pending_auth != NULL.
763 * It prompts user for a password.
764 * Hitting ^D (EOF) would make this function
766 * Any other input would try to set linphone core
767 * auth_password for the pending_auth, add the auth_info
771 linphonec_prompt_for_auth_final(LinphoneCore *lc)
774 char auth_prompt[256];
776 rl_hook_func_t *old_event_hook;
778 LinphoneAuthInfo *pending_auth=auth_stack.elem[auth_stack.nitems-1];
780 snprintf(auth_prompt, 256, "Password for %s on %s: ",
781 pending_auth->username, pending_auth->realm);
786 * Disable event hook to avoid entering an
787 * infinite loop. This would prevent idle_call
788 * from being called during authentication reads.
789 * Note that it might be undesiderable...
791 old_event_hook=rl_event_hook;
797 input=linphonec_readline(auth_prompt);
800 * If EOF (^D) is sent you probably don't want
801 * to provide an auth password... should give up
802 * the operation, but there's no mechanism to
803 * send this info back to caller currently...
807 printf("Cancel requested, but not implemented.\n");
812 iptr=lpc_strip_blanks(input);
815 * Only blanks, continue asking
823 /* Something typed, let's try */
828 * No check is done here to ensure password is correct.
829 * I guess password will be asked again later.
831 linphone_auth_info_set_passwd(pending_auth, input);
832 linphone_core_add_auth_info(lc, pending_auth);
833 linphone_auth_info_destroy(pending_auth);
834 auth_stack.elem[auth_stack.nitems-1]=0;
835 --(auth_stack.nitems);
838 * Reset line_buffer, to avoid the password
839 * to be used again from outer readline
841 rl_line_buffer[0]='\0';
842 rl_event_hook=old_event_hook;
848 print_usage (int exit_status)
850 fprintf (stdout, "\n\
851 usage: linphonec [-c file] [-s sipaddr] [-a] [-V] [-d level ] [-l logfile]\n\
854 -b file specify path of readonly factory configuration file.\n\
855 -c file specify path of configuration file.\n\
856 -d level be verbose. 0 is no output. 6 is all output\n\
857 -l logfile specify the log file for your SIP phone\n\
858 -s sipaddress specify the sip call to do at startup\n\
859 -a enable auto answering for incoming calls\n\
860 -V enable video features globally (disabled by default)\n\
861 -C enable video capture only (disabled by default)\n\
862 -D enable video display only (disabled by default)\n\
863 -S show general state messages (disabled by default)\n\
864 -v or --version display version and exits.\n");
871 #ifdef HAVE_X11_XLIB_H
872 static void sdl_x11_apply_video_params(){
873 static SDL_SysWMinfo info;
874 bool_t wminfo_ready=FALSE;
877 SDL_VERSION(&info.version);
878 if ( SDL_GetWMInfo(&info) ) {
879 if ( info.subsystem == SDL_SYSWM_X11 ) {
884 if ( !wminfo_ready) return;
888 unsigned int flags=0;
889 Display *display = info.info.x11.display;
890 Window window = info.info.x11.wmwindow;
892 memset(&wc,0,sizeof(wc));
893 wc.x=lpc_video_params.x;
894 wc.y=lpc_video_params.y;
895 wc.width=lpc_video_params.w;
896 wc.height=lpc_video_params.h;
897 if (lpc_video_params.x!=-1 ){
900 if (lpc_video_params.w!=-1){
901 flags|=CWWidth|CWHeight;
903 info.info.x11.lock_func();
904 XConfigureWindow(display,window,flags,&wc);
905 if (lpc_video_params.show)
906 XMapWindow(display,window);
908 XUnmapWindow(display,window);
909 info.info.x11.unlock_func();
915 static void lpc_apply_video_params(){
916 static unsigned long prev_wid=0;
918 unsigned long wid=linphone_core_get_native_video_window_id (linphonec);
920 if (wid!=0 && (lpc_video_params.refresh || prev_wid!=wid)){
921 lpc_video_params.refresh=FALSE;
922 #ifdef HAVE_X11_XLIB_H
923 if (lpc_video_params.wid==0){ // do not manage window if embedded
924 sdl_x11_apply_video_params();
936 * Called every second from main read loop.
938 * Will use the following globals:
940 * - LinphoneCore linphonec
941 * - LPC_AUTH_STACK auth_stack;
945 linphonec_idle_call ()
947 LinphoneCore *opm=linphonec;
949 /* Uncomment the following to verify being called */
952 linphone_core_iterate(opm);
954 fprintf (stdout, "-------auto answering to call-------\n" );
955 linphone_core_accept_call(opm,NULL);
959 if ( auth_stack.nitems )
962 * Inhibit command completion
963 * during password prompts
966 rl_inhibit_completion=1;
968 linphonec_prompt_for_auth_final(opm);
970 rl_inhibit_completion=0;
975 lpc_apply_video_params();
985 * - char *histfile_name (also sets this)
986 * - char *last_in_history (allocates it)
989 linphonec_initialize_readline()
991 /*rl_bind_key('\t', rl_insert);*/
993 /* Allow conditional parsing of ~/.inputrc */
994 rl_readline_name = "linphonec";
996 /* Call idle_call() every second */
997 rl_set_keyboard_input_timeout(LPC_READLINE_TIMEOUT);
998 rl_event_hook=linphonec_idle_call;
1000 /* Set history file and read it */
1001 histfile_name = ms_strdup_printf ("%s/.linphonec_history",
1003 read_history(histfile_name);
1005 /* Initialized last_in_history cache*/
1006 last_in_history[0] = '\0';
1008 /* Register a completion function */
1009 rl_attempted_completion_function = linephonec_readline_completion;
1011 /* printf("Readline initialized.\n"); */
1019 * - char *histfile_name (writes history to file and frees it)
1020 * - char *last_in_history (frees it)
1024 linphonec_finish_readline()
1027 stifle_history(HISTSIZE);
1028 write_history(histfile_name);
1029 free(histfile_name);
1036 static void print_prompt(LinphoneCore *opm){
1037 #ifdef IDENTITY_AS_PROMPT
1038 snprintf(prompt, PROMPT_MAX_LEN, "%s> ",
1039 linphone_core_get_primary_contact(opm));
1041 snprintf(prompt, PROMPT_MAX_LEN, "linphonec> ");
1046 linphonec_main_loop (LinphoneCore * opm, char * sipAddr)
1048 char buf[LINE_MAX_LEN]; /* auto call handling */
1054 /* auto call handling */
1055 if (sipAddr != NULL )
1057 snprintf (buf, sizeof(buf),"call %s", sipAddr);
1058 linphonec_parse_command_line(linphonec, buf);
1061 while (linphonec_running && (input=linphonec_readline(prompt)))
1063 char *iptr; /* input and input pointer */
1067 iptr=lpc_strip_blanks(input);
1069 input_len = strlen(iptr);
1072 * Do nothing but release memory
1073 * if only blanks are read
1081 #ifdef HAVE_READLINE
1083 * Only add to history if not already
1084 * last item in it, and only if the command
1085 * doesn't start with a space (to allow for
1088 if ( iptr == input && strcmp(last_in_history, iptr) )
1090 strncpy(last_in_history,iptr,sizeof(last_in_history));
1091 last_in_history[sizeof(last_in_history)-1]='\0';
1096 linphonec_parse_command_line(linphonec, iptr);
1097 linphonec_command_finished();
1105 * Parse command line switches
1110 * - char *logfile_name
1111 * - char *configfile_name
1115 linphonec_parse_cmdline(int argc, char **argv)
1119 while (arg_num < argc)
1121 int old_arg_num = arg_num;
1122 if (strncmp ("-d", argv[arg_num], 2) == 0)
1126 trace_level = atoi (argv[arg_num]);
1130 else if (strncmp ("-l", argv[arg_num], 2) == 0)
1134 logfile_name = argv[arg_num];
1136 else if (strncmp ("-c", argv[arg_num], 2) == 0)
1138 if ( ++arg_num >= argc ) print_usage(EXIT_FAILURE);
1139 #if !defined(_WIN32_WCE)
1140 if (access(argv[arg_num],F_OK)!=0 )
1143 "Cannot open config file %s.\n",
1147 #endif /*_WIN32_WCE*/
1148 snprintf(configfile_name, PATH_MAX, "%s", argv[arg_num]);
1150 else if (strncmp ("-b", argv[arg_num], 2) == 0)
1152 if ( ++arg_num >= argc ) print_usage(EXIT_FAILURE);
1153 #if !defined(_WIN32_WCE)
1154 if (access(argv[arg_num],F_OK)!=0 )
1157 "Cannot open config file %s.\n",
1161 #endif /*_WIN32_WCE*/
1162 factory_configfile_name = argv[arg_num];
1164 else if (strncmp ("-s", argv[arg_num], 2) == 0)
1168 sipAddr = argv[arg_num];
1170 else if (strncmp ("-a", argv[arg_num], 2) == 0)
1174 else if (strncmp ("-C", argv[arg_num], 2) == 0)
1176 vcap_enabled = TRUE;
1178 else if (strncmp ("-D", argv[arg_num], 2) == 0)
1180 display_enabled = TRUE;
1182 else if (strncmp ("-V", argv[arg_num], 2) == 0)
1184 display_enabled = TRUE;
1185 vcap_enabled = TRUE;
1186 preview_enabled=TRUE;
1188 else if ((strncmp ("-v", argv[arg_num], 2) == 0)
1191 ("--version", argv[arg_num],
1192 strlen ("--version")) == 0))
1194 #if !defined(_WIN32_WCE)
1195 printf ("version: " LINPHONE_VERSION "\n");
1197 exit (EXIT_SUCCESS);
1199 else if (strncmp ("-S", argv[arg_num], 2) == 0)
1201 show_general_state = TRUE;
1203 else if (strncmp ("--pipe", argv[arg_num], 6) == 0)
1207 else if (old_arg_num == arg_num)
1209 fprintf (stderr, "ERROR: bad arguments\n");
1210 print_usage (EXIT_FAILURE);
1219 * Up to version 1.2.1 linphone used ~/.linphonec for
1220 * CLI and ~/.gnome2/linphone for GUI as configuration file.
1221 * In newer version both interfaces will use ~/.linphonerc.
1223 * This function helps transparently migrating from one
1224 * to the other layout using the following heuristic:
1226 * IF new_config EXISTS => do nothing
1227 * ELSE IF old_cli_config EXISTS => copy to new_config
1228 * ELSE IF old_gui_config EXISTS => copy to new_config
1231 * 0 if it did nothing
1232 * 1 if it migrated successfully
1236 handle_configfile_migration()
1238 #if !defined(_WIN32_WCE)
1242 #if !defined(_WIN32_WCE)
1243 const char *home = getenv("HOME");
1245 const char *home = ".";
1246 #endif /*_WIN32_WCE*/
1247 new_cfg = ms_strdup_printf("%s/.linphonerc", home);
1250 * If the *NEW* configuration already exists
1253 if (access(new_cfg,F_OK)==0)
1259 old_cfg_cli = ms_strdup_printf("%s/.linphonec", home);
1262 * If the *OLD* CLI configurations exist copy it to
1263 * the new file and make it a symlink.
1265 if (access(old_cfg_cli, F_OK)==0)
1267 if ( ! copy_file(old_cfg_cli, new_cfg) )
1273 printf("%s copied to %s\n", old_cfg_cli, new_cfg);
1280 old_cfg_gui = ms_strdup_printf("%s/.gnome2/linphone", home);
1283 * If the *OLD* GUI configurations exist copy it to
1284 * the new file and make it a symlink.
1286 if (access(old_cfg_gui, F_OK)==0)
1288 if ( ! copy_file(old_cfg_gui, new_cfg) )
1295 printf("%s copied to %s\n", old_cfg_gui, new_cfg);
1303 #endif /*_WIN32_WCE*/
1306 #if !defined(_WIN32_WCE)
1308 * Copy file "from" to file "to".
1309 * Destination file is truncated if existing.
1310 * Return 1 on success, 0 on error (printing an error).
1313 copy_file(const char *from, const char *to)
1320 /* Open "from" file for reading */
1321 in=fopen(from, "r");
1324 snprintf(message, 255, "Can't open %s for reading: %s\n",
1325 from, strerror(errno));
1326 fprintf(stderr, "%s", message);
1330 /* Open "to" file for writing (will truncate existing files) */
1334 snprintf(message, 255, "Can't open %s for writing: %s\n",
1335 to, strerror(errno));
1336 fprintf(stderr, "%s", message);
1340 /* Copy data from "in" to "out" */
1341 while ( (n=fread(buf, 1, sizeof buf, in)) > 0 )
1343 if ( ! fwrite(buf, 1, n, out) )
1354 #endif /*_WIN32_WCE*/
1356 #ifdef HAVE_READLINE
1358 linephonec_readline_completion(const char *text, int start, int end)
1360 char **matches = NULL;
1363 * Prevent readline from falling
1364 * back to filename-completion
1366 rl_attempted_completion_over=1;
1369 * If this is the start of line we complete with commands
1373 return rl_completion_matches(text, linphonec_command_generator);
1377 * Otherwise, we should peek at command name
1378 * or context to implement a smart completion.
1379 * For example: "call .." could return
1380 * friends' sip-uri as matches
1389 * Strip blanks from a string.
1390 * Return a pointer into the provided string.
1391 * Modifies input adding a NULL at first
1392 * of trailing blanks.
1395 lpc_strip_blanks(char *input)
1399 /* Find first non-blank */
1400 while(*input && isspace(*input)) ++input;
1402 /* Find last non-blank */
1403 iptr=input+strlen(input);
1405 while(isspace(*--iptr));
1412 /****************************************************************************
1414 * $Log: linphonec.c,v $
1415 * Revision 1.57 2007/11/14 13:40:27 smorlat
1416 * fix --disable-video build.
1418 * Revision 1.56 2007/09/26 14:07:27 fixkowalski
1419 * - ANSI/C++ compilation issues with non-GCC compilers
1420 * - Faster epm-based packaging
1421 * - Ability to build & run on FC6's eXosip/osip
1423 * Revision 1.55 2007/09/24 16:01:58 smorlat
1426 * Revision 1.54 2007/08/22 14:06:11 smorlat
1427 * authentication bugs fixed.
1429 * Revision 1.53 2007/02/13 21:31:01 smorlat
1430 * added patch for general state.
1431 * new doxygen for oRTP
1434 * Revision 1.52 2007/01/10 14:11:24 smorlat
1435 * add --video to linphonec.
1437 * Revision 1.51 2006/08/21 12:49:59 smorlat
1438 * merged several little patches.
1440 * Revision 1.50 2006/07/26 08:17:28 smorlat
1443 * Revision 1.49 2006/07/17 18:45:00 smorlat
1444 * support for several event queues in ortp.
1445 * glib dependency removed from coreapi/ and console/
1447 * Revision 1.48 2006/04/09 12:45:32 smorlat
1448 * linphonec improvements.
1450 * Revision 1.47 2006/04/04 08:04:34 smorlat
1451 * switched to mediastreamer2, most bugs fixed.
1453 * Revision 1.46 2006/03/16 17:17:40 smorlat
1456 * Revision 1.45 2006/03/12 21:48:31 smorlat
1457 * gcc-2.95 compile error fixed.
1458 * mediastreamer2 in progress
1460 * Revision 1.44 2006/03/04 11:17:10 smorlat
1461 * mediastreamer2 in progress.
1463 * Revision 1.43 2006/02/13 09:50:50 strk
1464 * fixed unused variable warning.
1466 * Revision 1.42 2006/02/02 15:39:18 strk
1467 * - Added 'friend list' and 'friend call' commands
1468 * - Allowed for multiple DTFM send in a single line
1469 * - Added status-specific callback (bare version)
1471 * Revision 1.41 2006/02/02 13:30:05 strk
1472 * - Padded vtable with missing callbacks
1473 * (fixing a segfault on friends subscription)
1474 * - Handled friends notify (bare version)
1475 * - Handled text messages receive (bare version)
1476 * - Printed message on subscription request (bare version)
1478 * Revision 1.40 2006/01/26 09:48:05 strk
1479 * Added limits.h include
1481 * Revision 1.39 2006/01/26 02:11:01 strk
1482 * Removed unused variables, fixed copyright date
1484 * Revision 1.38 2006/01/25 18:33:02 strk
1485 * Removed the -t swich, terminate_on_close made the default behaviour
1487 * Revision 1.37 2006/01/20 14:12:34 strk
1488 * Added linphonec_init() and linphonec_finish() functions.
1489 * Handled SIGINT and SIGTERM to invoke linphonec_finish().
1490 * Handling of auto-termination (-t) moved to linphonec_finish().
1491 * Reworked main (input read) loop to not rely on 'terminate'
1492 * and 'run' variable (dropped). configfile_name allocated on stack
1493 * using PATH_MAX limit. Changed print_usage signature to allow
1494 * for an exit_status specification.
1496 * Revision 1.36 2006/01/18 09:25:32 strk
1497 * Command completion inhibited in proxy addition and auth request prompts.
1498 * Avoided use of readline's internal filename completion.
1500 * Revision 1.35 2006/01/14 13:29:32 strk
1501 * Reworked commands interface to use a table structure,
1502 * used by command line parser and help function.
1503 * Implemented first level of completion (commands).
1504 * Added notification of invalid "answer" and "terminate"
1505 * commands (no incoming call, no active call).
1506 * Forbidden "call" intialization when a call is already active.
1507 * Cleaned up all commands, adding more feedback and error checks.
1509 * Revision 1.34 2006/01/13 13:00:29 strk
1510 * Added linphonec.h. Code layout change (added comments, forward decl,
1511 * globals on top, copyright notices and Logs). Handled out-of-memory
1512 * condition on history management. Removed assumption on sizeof(char).
1513 * Fixed bug in authentication prompt (introduced by readline).
1514 * Added support for multiple authentication requests (up to MAX_PENDING_AUTH).
1517 ****************************************************************************/