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 /***************************************************************************
84 ***************************************************************************/
87 LinphoneAuthInfo *elem[MAX_PENDING_AUTH];
91 /***************************************************************************
93 * Forward declarations
95 ***************************************************************************/
97 char *lpc_strip_blanks(char *input);
99 static int handle_configfile_migration(void);
100 #if !defined(_WIN32_WCE)
101 static int copy_file(const char *from, const char *to);
102 #endif /*_WIN32_WCE*/
103 static int linphonec_parse_cmdline(int argc, char **argv);
104 static int linphonec_init(int argc, char **argv);
105 static int linphonec_main_loop (LinphoneCore * opm, char * sipAddr);
106 static int linphonec_idle_call (void);
108 static int linphonec_initialize_readline(void);
109 static int linphonec_finish_readline();
110 static char **linephonec_readline_completion(const char *text,
114 /* These are callback for linphone core */
115 static void linphonec_prompt_for_auth(LinphoneCore *lc, const char *realm,
116 const char *username);
117 static void linphonec_display_refer (LinphoneCore * lc, const char *refer_to);
118 static void linphonec_display_something (LinphoneCore * lc, const char *something);
119 static void linphonec_display_url (LinphoneCore * lc, const char *something, const char *url);
120 static void linphonec_display_warning (LinphoneCore * lc, const char *something);
121 static void linphonec_notify_received(LinphoneCore *lc, LinphoneCall *call, const char *from,const char *event);
123 static void linphonec_notify_presence_received(LinphoneCore *lc,LinphoneFriend *fid);
124 static void linphonec_new_unknown_subscriber(LinphoneCore *lc,
125 LinphoneFriend *lf, const char *url);
127 static void linphonec_text_received(LinphoneCore *lc, LinphoneChatRoom *cr,
128 const char *from, const char *msg);
129 static void linphonec_display_status (LinphoneCore * lc, const char *something);
130 static void linphonec_dtmf_received(LinphoneCore *lc, LinphoneCall *call, int dtmf);
131 static void print_prompt(LinphoneCore *opm);
132 void linphonec_out(const char *fmt,...);
133 /***************************************************************************
137 ***************************************************************************/
139 LinphoneCore *linphonec;
142 static char *histfile_name=NULL;
143 static char last_in_history[256];
145 //auto answer (-a) option
146 static bool_t auto_answer=FALSE;
147 static bool_t answer_call=FALSE;
148 static bool_t vcap_enabled=FALSE;
149 static bool_t display_enabled=FALSE;
150 static bool_t preview_enabled=FALSE;
151 static bool_t show_general_state=FALSE;
152 static bool_t unix_socket=FALSE;
153 static bool_t linphonec_running=TRUE;
154 LPC_AUTH_STACK auth_stack;
155 static int trace_level = 0;
156 static char *logfile_name = NULL;
157 static char configfile_name[PATH_MAX];
158 static char *sipAddr = NULL; /* for autocall */
159 #if !defined(_WIN32_WCE)
160 static ortp_pipe_t client_sock=ORTP_PIPE_INVALID;
161 #endif /*_WIN32_WCE*/
162 char prompt[PROMPT_MAX_LEN];
163 #if !defined(_WIN32_WCE)
164 static ortp_thread_t pipe_reader_th;
165 static bool_t pipe_reader_run=FALSE;
166 #endif /*_WIN32_WCE*/
167 #if !defined(_WIN32_WCE)
168 static ortp_pipe_t server_sock;
169 #endif /*_WIN32_WCE*/
172 void linphonec_call_identify(LinphoneCall* call){
173 static long callid=1;
174 linphone_call_set_user_pointer (call,(void*)callid);
178 LinphoneCall *linphonec_get_call(long id){
179 const MSList *elem=linphone_core_get_calls(linphonec);
180 for (;elem!=NULL;elem=elem->next){
181 LinphoneCall *call=(LinphoneCall*)elem->data;
182 if (linphone_call_get_user_pointer (call)==(void*)id){
186 linphonec_out("Sorry, no call with id %i exists at this time.",id);
190 /***************************************************************************
192 * Linphone core callbacks
194 ***************************************************************************/
197 * Linphone core callback
200 linphonec_display_refer (LinphoneCore * lc, const char *refer_to)
202 linphonec_out("Receiving out of call refer to %s", refer_to);
206 * Linphone core callback
209 linphonec_display_something (LinphoneCore * lc, const char *something)
211 fprintf (stdout, "%s\n%s", something,prompt);
216 * Linphone core callback
219 linphonec_display_status (LinphoneCore * lc, const char *something)
221 fprintf (stdout, "%s\n%s", something,prompt);
226 * Linphone core callback
229 linphonec_display_warning (LinphoneCore * lc, const char *something)
231 fprintf (stdout, "Warning: %s\n%s", something,prompt);
236 * Linphone core callback
239 linphonec_display_url (LinphoneCore * lc, const char *something, const char *url)
241 fprintf (stdout, "%s : %s\n", something, url);
245 * Linphone core callback
248 linphonec_prompt_for_auth(LinphoneCore *lc, const char *realm, const char *username)
250 /* no prompt possible when using pipes or tcp mode*/
252 linphone_core_abort_authentication(lc,NULL);
254 LinphoneAuthInfo *pending_auth;
256 if ( auth_stack.nitems+1 > MAX_PENDING_AUTH )
259 "Can't accept another authentication request.\n"
260 "Consider incrementing MAX_PENDING_AUTH macro.\n");
264 pending_auth=linphone_auth_info_new(username,NULL,NULL,NULL,realm);
265 auth_stack.elem[auth_stack.nitems++]=pending_auth;
270 * Linphone core callback
273 linphonec_notify_received(LinphoneCore *lc, LinphoneCall *call, const char *from,const char *event)
275 if(!strcmp(event,"refer"))
277 linphonec_out("The distand endpoint %s of call %li has been transfered, you can safely close the call.\n",
278 from,(long)linphone_call_get_user_pointer (call));
284 * Linphone core callback
287 linphonec_notify_presence_received(LinphoneCore *lc,LinphoneFriend *fid)
289 char *tmp=linphone_address_as_string(linphone_friend_get_address(fid));
290 printf("Friend %s is %s\n", tmp, linphone_online_status_to_string(linphone_friend_get_status(fid)));
292 // todo: update Friend list state (unimplemented)
296 * Linphone core callback
299 linphonec_new_unknown_subscriber(LinphoneCore *lc, LinphoneFriend *lf,
302 printf("Friend %s requested subscription "
303 "(accept/deny is not implemented yet)\n", url);
304 // This means that this person wishes to be notified
305 // of your presence information (online, busy, away...).
309 static void linphonec_call_state_changed(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState st, const char *msg){
310 char *from=linphone_call_get_remote_address_as_string(call);
311 long id=(long)linphone_call_get_user_pointer (call);
313 case LinphoneCallEnd:
314 linphonec_out("Call %i with %s ended.\n", id, from);
316 case LinphoneCallResuming:
317 linphonec_out("Resuming call %i with %s.\n", id, from);
319 case LinphoneCallStreamsRunning:
320 linphonec_out("Media streams established with %s for call %i.\n", from,id);
322 case LinphoneCallPausing:
323 linphonec_out("Pausing call %i with %s.\n", id, from);
325 case LinphoneCallPaused:
326 linphonec_out("Call %i with %s is now paused.\n", id, from);
328 case LinphoneCallPausedByRemote:
329 linphonec_out("Call %i has been paused by %s.\n",id,from);
331 case LinphoneCallIncomingReceived:
332 linphonec_call_identify(call);
333 id=(long)linphone_call_get_user_pointer (call);
334 linphonec_set_caller(from);
338 linphonec_out("Receiving new incoming call from %s, assigned id %i", from,id);
340 case LinphoneCallOutgoingInit:
341 linphonec_call_identify(call);
350 * Linphone core callback
353 linphonec_text_received(LinphoneCore *lc, LinphoneChatRoom *cr,
354 const char *from, const char *msg)
356 printf("%s: %s\n", from, msg);
357 // TODO: provide mechanism for answering.. ('say' command?)
361 static void linphonec_dtmf_received(LinphoneCore *lc, LinphoneCall *call, int dtmf){
362 char *from=linphone_call_get_remote_address_as_string(call);
363 fprintf(stdout,"Receiving tone %c from %s\n",dtmf,from);
368 static char received_prompt[PROMPT_MAX_LEN];
369 static ms_mutex_t prompt_mutex;
370 static bool_t have_prompt=FALSE;
372 static void *prompt_reader_thread(void *arg){
374 char tmp[PROMPT_MAX_LEN];
375 while ((ret=fgets(tmp,sizeof(tmp),stdin))!=NULL){
376 ms_mutex_lock(&prompt_mutex);
377 strcpy(received_prompt,ret);
379 ms_mutex_unlock(&prompt_mutex);
384 static void start_prompt_reader(void){
386 ms_mutex_init(&prompt_mutex,NULL);
387 ortp_thread_create(&th,NULL,prompt_reader_thread,NULL);
389 #if !defined(_WIN32_WCE)
390 static ortp_pipe_t create_server_socket(void){
393 snprintf(path,sizeof(path)-1,"linphonec-%i",getuid());
397 DWORD size=sizeof(username)-1;
398 GetUserName(username,&size);
399 snprintf(path,sizeof(path)-1,"linphonec-%s",username);
402 return ortp_server_pipe_create(path);
406 static void *pipe_thread(void*p){
408 server_sock=create_server_socket();
409 if (server_sock==ORTP_PIPE_INVALID) return NULL;
410 while(pipe_reader_run){
411 while(client_sock!=ORTP_PIPE_INVALID){ /*sleep until the last command is finished*/
418 client_sock=ortp_server_pipe_accept_client(server_sock);
419 if (client_sock!=ORTP_PIPE_INVALID){
421 /*now read from the client */
422 if ((len=ortp_pipe_read(client_sock,(uint8_t*)tmp,sizeof(tmp)-1))>0){
423 ortp_mutex_lock(&prompt_mutex);
425 strcpy(received_prompt,tmp);
426 printf("Receiving command '%s'\n",received_prompt);fflush(stdout);
428 ortp_mutex_unlock(&prompt_mutex);
430 printf("read nothing\n");fflush(stdout);
431 ortp_server_pipe_close_client(client_sock);
432 client_sock=ORTP_PIPE_INVALID;
436 if (pipe_reader_run) fprintf(stderr,"accept() failed: %s\n",strerror(errno));
439 ms_message("Exiting pipe_reader_thread.");
444 static void start_pipe_reader(void){
445 ms_mutex_init(&prompt_mutex,NULL);
446 pipe_reader_run=TRUE;
447 ortp_thread_create(&pipe_reader_th,NULL,pipe_thread,NULL);
450 static void stop_pipe_reader(void){
451 pipe_reader_run=FALSE;
452 linphonec_command_finished();
453 ortp_server_pipe_close(server_sock);
454 ortp_thread_join(pipe_reader_th,NULL);
456 #endif /*_WIN32_WCE*/
459 #define BOOL_HAVE_READLINE 1
461 #define BOOL_HAVE_READLINE 0
464 char *linphonec_readline(char *prompt){
465 if (unix_socket || !BOOL_HAVE_READLINE ){
466 static bool_t prompt_reader_started=FALSE;
467 static bool_t pipe_reader_started=FALSE;
468 if (!prompt_reader_started){
469 start_prompt_reader();
470 prompt_reader_started=TRUE;
472 if (unix_socket && !pipe_reader_started){
473 #if !defined(_WIN32_WCE)
475 pipe_reader_started=TRUE;
476 #endif /*_WIN32_WCE*/
478 fprintf(stdout,"%s",prompt);
481 ms_mutex_lock(&prompt_mutex);
483 char *ret=strdup(received_prompt);
485 ms_mutex_unlock(&prompt_mutex);
488 ms_mutex_unlock(&prompt_mutex);
489 linphonec_idle_call();
492 /* Following is to get the video window going as it
493 should. Maybe should we only have this on when the option -V
497 if (PeekMessage(&msg, NULL, 0, 0,1)) {
498 TranslateMessage(&msg);
499 DispatchMessage(&msg);
507 return readline(prompt);
512 void linphonec_out(const char *fmt,...){
515 va_start (args, fmt);
516 res=ortp_strdup_vprintf(fmt,args);
520 #if !defined(_WIN32_WCE)
521 if (client_sock!=ORTP_PIPE_INVALID){
522 if (ortp_pipe_write(client_sock,(uint8_t*)res,strlen(res))==-1){
523 fprintf(stderr,"Fail to send output via pipe: %s",strerror(errno));
526 #endif /*_WIN32_WCE*/
530 void linphonec_command_finished(void){
531 #if !defined(_WIN32_WCE)
532 if (client_sock!=ORTP_PIPE_INVALID){
533 ortp_server_pipe_close_client(client_sock);
534 client_sock=ORTP_PIPE_INVALID;
536 #endif /*_WIN32_WCE*/
539 void linphonec_set_autoanswer(bool_t enabled){
543 bool_t linphonec_get_autoanswer(){
547 LinphoneCoreVTable linphonec_vtable={0};
549 /***************************************************************************/
555 * - char *histfile_name
558 #if defined (_WIN32_WCE)
560 char **convert_args_to_ascii(int argc, _TCHAR **wargv){
562 char **result=malloc(argc*sizeof(char*));
565 wcstombs(argtmp,wargv[i],sizeof(argtmp));
566 result[i]=strdup(argtmp);
571 int _tmain(int argc, _TCHAR* wargv[]) {
572 char **argv=convert_args_to_ascii(argc,wargv);
577 main (int argc, char *argv[]) {
579 linphonec_vtable.call_state_changed=linphonec_call_state_changed;
580 linphonec_vtable.notify_presence_recv = linphonec_notify_presence_received;
581 linphonec_vtable.new_unknown_subscriber = linphonec_new_unknown_subscriber;
582 linphonec_vtable.auth_info_requested = linphonec_prompt_for_auth;
583 linphonec_vtable.display_status = linphonec_display_status;
584 linphonec_vtable.display_message=linphonec_display_something;
585 linphonec_vtable.display_warning=linphonec_display_warning;
586 linphonec_vtable.display_url=linphonec_display_url;
587 linphonec_vtable.text_received=linphonec_text_received;
588 linphonec_vtable.dtmf_received=linphonec_dtmf_received;
589 linphonec_vtable.refer_received=linphonec_display_refer;
590 linphonec_vtable.notify_recv=linphonec_notify_received;
592 if (! linphonec_init(argc, argv) ) exit(EXIT_FAILURE);
594 linphonec_main_loop (linphonec, sipAddr);
596 linphonec_finish(EXIT_SUCCESS);
598 exit(EXIT_SUCCESS); /* should never reach here */
602 * Initialize linphonec
605 linphonec_init(int argc, char **argv)
608 //g_mem_set_vtable(&dbgtable);
611 * Set initial values for global variables
617 snprintf(configfile_name, PATH_MAX, "%s/.linphonerc",
619 #elif defined(_WIN32_WCE)
620 strncpy(configfile_name,PACKAGE_DIR "\\linphonerc",PATH_MAX);
621 mylogfile=fopen(PACKAGE_DIR "\\" "linphonec.log","w");
622 printf("Logs are redirected in" PACKAGE_DIR "\\linphonec.log");
624 snprintf(configfile_name, PATH_MAX, "%s/Linphone/linphonerc",
627 /* Handle configuration filename changes */
628 switch (handle_configfile_migration())
630 case -1: /* error during file copies */
632 "Error in configuration file migration\n");
635 case 0: /* nothing done */
636 case 1: /* migrated */
642 if (NULL == bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR))
643 perror ("bindtextdomain failed");
645 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
647 textdomain (GETTEXT_PACKAGE);
649 printf ("NLS disabled.\n");
652 linphonec_parse_cmdline(argc, argv);
656 if (logfile_name != NULL)
657 mylogfile = fopen (logfile_name, "w+");
659 if (mylogfile == NULL)
663 "INFO: no logfile, logging to stdout\n");
665 linphone_core_enable_logs(mylogfile);
669 linphone_core_disable_logs();
672 * Initialize auth stack
677 * Initialize linphone core
679 linphonec=linphone_core_new (&linphonec_vtable, configfile_name, NULL,
681 linphone_core_enable_video(linphonec,vcap_enabled,display_enabled);
682 linphone_core_enable_video_preview(linphonec,preview_enabled);
683 if (!(vcap_enabled || display_enabled)) printf("Warning: video is disabled in linphonec, use -V or -C or -D to enable.\n");
686 * Initialize readline
688 linphonec_initialize_readline();
690 #if !defined(_WIN32_WCE)
692 * Initialize signal handlers
694 signal(SIGTERM, linphonec_finish);
695 signal(SIGINT, linphonec_finish);
696 #endif /*_WIN32_WCE*/
701 void linphonec_main_loop_exit(void){
702 linphonec_running=FALSE;
706 * Close linphonec, cleanly terminating
710 linphonec_finish(int exit_status)
712 linphonec_out("Terminating...\n");
714 /* Terminate any pending call */
715 linphone_core_terminate_all_calls(linphonec);
717 linphonec_finish_readline();
719 #if !defined(_WIN32_WCE)
722 #endif /*_WIN32_WCE*/
724 linphone_core_destroy (linphonec);
726 if (mylogfile != NULL && mylogfile != stdout)
736 * This is called from idle_call() whenever
737 * pending_auth != NULL.
739 * It prompts user for a password.
740 * Hitting ^D (EOF) would make this function
742 * Any other input would try to set linphone core
743 * auth_password for the pending_auth, add the auth_info
747 linphonec_prompt_for_auth_final(LinphoneCore *lc)
750 char auth_prompt[256];
752 rl_hook_func_t *old_event_hook;
754 LinphoneAuthInfo *pending_auth=auth_stack.elem[auth_stack.nitems-1];
756 snprintf(auth_prompt, 256, "Password for %s on %s: ",
757 pending_auth->username, pending_auth->realm);
762 * Disable event hook to avoid entering an
763 * infinite loop. This would prevent idle_call
764 * from being called during authentication reads.
765 * Note that it might be undesiderable...
767 old_event_hook=rl_event_hook;
773 input=linphonec_readline(auth_prompt);
776 * If EOF (^D) is sent you probably don't want
777 * to provide an auth password... should give up
778 * the operation, but there's no mechanism to
779 * send this info back to caller currently...
783 printf("Cancel requested, but not implemented.\n");
788 iptr=lpc_strip_blanks(input);
791 * Only blanks, continue asking
799 /* Something typed, let's try */
804 * No check is done here to ensure password is correct.
805 * I guess password will be asked again later.
807 linphone_auth_info_set_passwd(pending_auth, input);
808 linphone_core_add_auth_info(lc, pending_auth);
809 linphone_auth_info_destroy(pending_auth);
810 auth_stack.elem[auth_stack.nitems-1]=0;
811 --(auth_stack.nitems);
814 * Reset line_buffer, to avoid the password
815 * to be used again from outer readline
817 rl_line_buffer[0]='\0';
818 rl_event_hook=old_event_hook;
824 print_usage (int exit_status)
826 fprintf (stdout, "\n\
827 usage: linphonec [-c file] [-s sipaddr] [-a] [-V] [-d level ] [-l logfile]\n\
830 -c file specify path of configuration file.\n\
831 -d level be verbose. 0 is no output. 6 is all output\n\
832 -l logfile specify the log file for your SIP phone\n\
833 -s sipaddress specify the sip call to do at startup\n\
834 -a enable auto answering for incoming calls\n\
835 -V enable video features globally (disabled by default)\n\
836 -C enable video capture only (disabled by default)\n\
837 -D enable video display only (disabled by default)\n\
838 -S show general state messages (disabled by default)\n\
839 -v or --version display version and exits.\n");
847 * Called every second from main read loop.
849 * Will use the following globals:
851 * - LinphoneCore linphonec
852 * - LPC_AUTH_STACK auth_stack;
856 linphonec_idle_call ()
858 LinphoneCore *opm=linphonec;
860 /* Uncomment the following to verify being called */
863 linphone_core_iterate(opm);
865 fprintf (stdout, "-------auto answering to call-------\n" );
866 linphone_core_accept_call(opm,NULL);
870 if ( auth_stack.nitems )
873 * Inhibit command completion
874 * during password prompts
877 rl_inhibit_completion=1;
879 linphonec_prompt_for_auth_final(opm);
881 rl_inhibit_completion=0;
892 * - char *histfile_name (also sets this)
893 * - char *last_in_history (allocates it)
896 linphonec_initialize_readline()
898 /*rl_bind_key('\t', rl_insert);*/
900 /* Allow conditional parsing of ~/.inputrc */
901 rl_readline_name = "linphonec";
903 /* Call idle_call() every second */
904 rl_set_keyboard_input_timeout(LPC_READLINE_TIMEOUT);
905 rl_event_hook=linphonec_idle_call;
907 /* Set history file and read it */
908 histfile_name = ms_strdup_printf ("%s/.linphonec_history",
910 read_history(histfile_name);
912 /* Initialized last_in_history cache*/
913 last_in_history[0] = '\0';
915 /* Register a completion function */
916 rl_attempted_completion_function = linephonec_readline_completion;
918 /* printf("Readline initialized.\n"); */
926 * - char *histfile_name (writes history to file and frees it)
927 * - char *last_in_history (frees it)
931 linphonec_finish_readline()
934 stifle_history(HISTSIZE);
935 write_history(histfile_name);
943 static void print_prompt(LinphoneCore *opm){
944 #ifdef IDENTITY_AS_PROMPT
945 snprintf(prompt, PROMPT_MAX_LEN, "%s> ",
946 linphone_core_get_primary_contact(opm));
948 snprintf(prompt, PROMPT_MAX_LEN, "linphonec> ");
953 linphonec_main_loop (LinphoneCore * opm, char * sipAddr)
955 char buf[LINE_MAX_LEN]; /* auto call handling */
961 /* auto call handling */
962 if (sipAddr != NULL )
964 snprintf (buf, sizeof(buf),"call %s", sipAddr);
965 linphonec_parse_command_line(linphonec, buf);
968 while (linphonec_running && (input=linphonec_readline(prompt)))
970 char *iptr; /* input and input pointer */
974 iptr=lpc_strip_blanks(input);
976 input_len = strlen(iptr);
979 * Do nothing but release memory
980 * if only blanks are read
990 * Only add to history if not already
991 * last item in it, and only if the command
992 * doesn't start with a space (to allow for
995 if ( iptr == input && strcmp(last_in_history, iptr) )
997 strncpy(last_in_history,iptr,sizeof(last_in_history));
998 last_in_history[sizeof(last_in_history)-1]='\0';
1003 linphonec_parse_command_line(linphonec, iptr);
1004 linphonec_command_finished();
1012 * Parse command line switches
1017 * - char *logfile_name
1018 * - char *configfile_name
1022 linphonec_parse_cmdline(int argc, char **argv)
1026 while (arg_num < argc)
1028 int old_arg_num = arg_num;
1029 if (strncmp ("-d", argv[arg_num], 2) == 0)
1033 trace_level = atoi (argv[arg_num]);
1037 else if (strncmp ("-l", argv[arg_num], 2) == 0)
1041 logfile_name = argv[arg_num];
1043 else if (strncmp ("-c", argv[arg_num], 2) == 0)
1045 if ( ++arg_num >= argc ) print_usage(EXIT_FAILURE);
1046 #if !defined(_WIN32_WCE)
1047 if (access(argv[arg_num],F_OK)!=0 )
1050 "Cannot open config file %s.\n",
1054 #endif /*_WIN32_WCE*/
1055 snprintf(configfile_name, PATH_MAX, "%s", argv[arg_num]);
1057 else if (strncmp ("-s", argv[arg_num], 2) == 0)
1061 sipAddr = argv[arg_num];
1063 else if (strncmp ("-a", argv[arg_num], 2) == 0)
1067 else if (strncmp ("-C", argv[arg_num], 2) == 0)
1069 vcap_enabled = TRUE;
1071 else if (strncmp ("-D", argv[arg_num], 2) == 0)
1073 display_enabled = TRUE;
1075 else if (strncmp ("-V", argv[arg_num], 2) == 0)
1077 display_enabled = TRUE;
1078 vcap_enabled = TRUE;
1079 preview_enabled=TRUE;
1081 else if ((strncmp ("-v", argv[arg_num], 2) == 0)
1084 ("--version", argv[arg_num],
1085 strlen ("--version")) == 0))
1087 #if !defined(_WIN32_WCE)
1088 printf ("version: " LINPHONE_VERSION "\n");
1090 exit (EXIT_SUCCESS);
1092 else if (strncmp ("-S", argv[arg_num], 2) == 0)
1094 show_general_state = TRUE;
1096 else if (strncmp ("--pipe", argv[arg_num], 6) == 0)
1100 else if (old_arg_num == arg_num)
1102 fprintf (stderr, "ERROR: bad arguments\n");
1103 print_usage (EXIT_FAILURE);
1112 * Up to version 1.2.1 linphone used ~/.linphonec for
1113 * CLI and ~/.gnome2/linphone for GUI as configuration file.
1114 * In newer version both interfaces will use ~/.linphonerc.
1116 * This function helps transparently migrating from one
1117 * to the other layout using the following heuristic:
1119 * IF new_config EXISTS => do nothing
1120 * ELSE IF old_cli_config EXISTS => copy to new_config
1121 * ELSE IF old_gui_config EXISTS => copy to new_config
1124 * 0 if it did nothing
1125 * 1 if it migrated successfully
1129 handle_configfile_migration()
1131 #if !defined(_WIN32_WCE)
1135 #if !defined(_WIN32_WCE)
1136 const char *home = getenv("HOME");
1138 const char *home = ".";
1139 #endif /*_WIN32_WCE*/
1140 new_cfg = ms_strdup_printf("%s/.linphonerc", home);
1143 * If the *NEW* configuration already exists
1146 if (access(new_cfg,F_OK)==0)
1152 old_cfg_cli = ms_strdup_printf("%s/.linphonec", home);
1155 * If the *OLD* CLI configurations exist copy it to
1156 * the new file and make it a symlink.
1158 if (access(old_cfg_cli, F_OK)==0)
1160 if ( ! copy_file(old_cfg_cli, new_cfg) )
1166 printf("%s copied to %s\n", old_cfg_cli, new_cfg);
1173 old_cfg_gui = ms_strdup_printf("%s/.gnome2/linphone", home);
1176 * If the *OLD* GUI configurations exist copy it to
1177 * the new file and make it a symlink.
1179 if (access(old_cfg_gui, F_OK)==0)
1181 if ( ! copy_file(old_cfg_gui, new_cfg) )
1188 printf("%s copied to %s\n", old_cfg_gui, new_cfg);
1196 #endif /*_WIN32_WCE*/
1199 #if !defined(_WIN32_WCE)
1201 * Copy file "from" to file "to".
1202 * Destination file is truncated if existing.
1203 * Return 1 on success, 0 on error (printing an error).
1206 copy_file(const char *from, const char *to)
1213 /* Open "from" file for reading */
1214 in=fopen(from, "r");
1217 snprintf(message, 255, "Can't open %s for reading: %s\n",
1218 from, strerror(errno));
1219 fprintf(stderr, "%s", message);
1223 /* Open "to" file for writing (will truncate existing files) */
1227 snprintf(message, 255, "Can't open %s for writing: %s\n",
1228 to, strerror(errno));
1229 fprintf(stderr, "%s", message);
1233 /* Copy data from "in" to "out" */
1234 while ( (n=fread(buf, 1, sizeof buf, in)) > 0 )
1236 if ( ! fwrite(buf, 1, n, out) )
1247 #endif /*_WIN32_WCE*/
1249 #ifdef HAVE_READLINE
1251 linephonec_readline_completion(const char *text, int start, int end)
1253 char **matches = NULL;
1256 * Prevent readline from falling
1257 * back to filename-completion
1259 rl_attempted_completion_over=1;
1262 * If this is the start of line we complete with commands
1266 return rl_completion_matches(text, linphonec_command_generator);
1270 * Otherwise, we should peek at command name
1271 * or context to implement a smart completion.
1272 * For example: "call .." could return
1273 * friends' sip-uri as matches
1282 * Strip blanks from a string.
1283 * Return a pointer into the provided string.
1284 * Modifies input adding a NULL at first
1285 * of trailing blanks.
1288 lpc_strip_blanks(char *input)
1292 /* Find first non-blank */
1293 while(*input && isspace(*input)) ++input;
1295 /* Find last non-blank */
1296 iptr=input+strlen(input);
1298 while(isspace(*--iptr));
1305 /****************************************************************************
1307 * $Log: linphonec.c,v $
1308 * Revision 1.57 2007/11/14 13:40:27 smorlat
1309 * fix --disable-video build.
1311 * Revision 1.56 2007/09/26 14:07:27 fixkowalski
1312 * - ANSI/C++ compilation issues with non-GCC compilers
1313 * - Faster epm-based packaging
1314 * - Ability to build & run on FC6's eXosip/osip
1316 * Revision 1.55 2007/09/24 16:01:58 smorlat
1319 * Revision 1.54 2007/08/22 14:06:11 smorlat
1320 * authentication bugs fixed.
1322 * Revision 1.53 2007/02/13 21:31:01 smorlat
1323 * added patch for general state.
1324 * new doxygen for oRTP
1327 * Revision 1.52 2007/01/10 14:11:24 smorlat
1328 * add --video to linphonec.
1330 * Revision 1.51 2006/08/21 12:49:59 smorlat
1331 * merged several little patches.
1333 * Revision 1.50 2006/07/26 08:17:28 smorlat
1336 * Revision 1.49 2006/07/17 18:45:00 smorlat
1337 * support for several event queues in ortp.
1338 * glib dependency removed from coreapi/ and console/
1340 * Revision 1.48 2006/04/09 12:45:32 smorlat
1341 * linphonec improvements.
1343 * Revision 1.47 2006/04/04 08:04:34 smorlat
1344 * switched to mediastreamer2, most bugs fixed.
1346 * Revision 1.46 2006/03/16 17:17:40 smorlat
1349 * Revision 1.45 2006/03/12 21:48:31 smorlat
1350 * gcc-2.95 compile error fixed.
1351 * mediastreamer2 in progress
1353 * Revision 1.44 2006/03/04 11:17:10 smorlat
1354 * mediastreamer2 in progress.
1356 * Revision 1.43 2006/02/13 09:50:50 strk
1357 * fixed unused variable warning.
1359 * Revision 1.42 2006/02/02 15:39:18 strk
1360 * - Added 'friend list' and 'friend call' commands
1361 * - Allowed for multiple DTFM send in a single line
1362 * - Added status-specific callback (bare version)
1364 * Revision 1.41 2006/02/02 13:30:05 strk
1365 * - Padded vtable with missing callbacks
1366 * (fixing a segfault on friends subscription)
1367 * - Handled friends notify (bare version)
1368 * - Handled text messages receive (bare version)
1369 * - Printed message on subscription request (bare version)
1371 * Revision 1.40 2006/01/26 09:48:05 strk
1372 * Added limits.h include
1374 * Revision 1.39 2006/01/26 02:11:01 strk
1375 * Removed unused variables, fixed copyright date
1377 * Revision 1.38 2006/01/25 18:33:02 strk
1378 * Removed the -t swich, terminate_on_close made the default behaviour
1380 * Revision 1.37 2006/01/20 14:12:34 strk
1381 * Added linphonec_init() and linphonec_finish() functions.
1382 * Handled SIGINT and SIGTERM to invoke linphonec_finish().
1383 * Handling of auto-termination (-t) moved to linphonec_finish().
1384 * Reworked main (input read) loop to not rely on 'terminate'
1385 * and 'run' variable (dropped). configfile_name allocated on stack
1386 * using PATH_MAX limit. Changed print_usage signature to allow
1387 * for an exit_status specification.
1389 * Revision 1.36 2006/01/18 09:25:32 strk
1390 * Command completion inhibited in proxy addition and auth request prompts.
1391 * Avoided use of readline's internal filename completion.
1393 * Revision 1.35 2006/01/14 13:29:32 strk
1394 * Reworked commands interface to use a table structure,
1395 * used by command line parser and help function.
1396 * Implemented first level of completion (commands).
1397 * Added notification of invalid "answer" and "terminate"
1398 * commands (no incoming call, no active call).
1399 * Forbidden "call" intialization when a call is already active.
1400 * Cleaned up all commands, adding more feedback and error checks.
1402 * Revision 1.34 2006/01/13 13:00:29 strk
1403 * Added linphonec.h. Code layout change (added comments, forward decl,
1404 * globals on top, copyright notices and Logs). Handled out-of-memory
1405 * condition on history management. Removed assumption on sizeof(char).
1406 * Fixed bug in authentication prompt (introduced by readline).
1407 * Added support for multiple authentication requests (up to MAX_PENDING_AUTH).
1410 ****************************************************************************/