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 char *sipAddr = NULL; /* for autocall */
164 #if !defined(_WIN32_WCE)
165 static ortp_pipe_t client_sock=ORTP_PIPE_INVALID;
166 #endif /*_WIN32_WCE*/
167 char prompt[PROMPT_MAX_LEN];
168 #if !defined(_WIN32_WCE)
169 static ortp_thread_t pipe_reader_th;
170 static bool_t pipe_reader_run=FALSE;
171 #endif /*_WIN32_WCE*/
172 #if !defined(_WIN32_WCE)
173 static ortp_pipe_t server_sock;
174 #endif /*_WIN32_WCE*/
177 extern VideoParams lpc_video_params;
179 void linphonec_call_identify(LinphoneCall* call){
180 static long callid=1;
181 linphone_call_set_user_pointer (call,(void*)callid);
185 LinphoneCall *linphonec_get_call(long id){
186 const MSList *elem=linphone_core_get_calls(linphonec);
187 for (;elem!=NULL;elem=elem->next){
188 LinphoneCall *call=(LinphoneCall*)elem->data;
189 if (linphone_call_get_user_pointer (call)==(void*)id){
193 linphonec_out("Sorry, no call with id %i exists at this time.",id);
197 /***************************************************************************
199 * Linphone core callbacks
201 ***************************************************************************/
204 * Linphone core callback
207 linphonec_display_refer (LinphoneCore * lc, const char *refer_to)
209 linphonec_out("Receiving out of call refer to %s", refer_to);
213 * Linphone core callback
216 linphonec_display_something (LinphoneCore * lc, const char *something)
218 fprintf (stdout, "%s\n%s", something,prompt);
223 * Linphone core callback
226 linphonec_display_status (LinphoneCore * lc, const char *something)
228 fprintf (stdout, "%s\n%s", something,prompt);
233 * Linphone core callback
236 linphonec_display_warning (LinphoneCore * lc, const char *something)
238 fprintf (stdout, "Warning: %s\n%s", something,prompt);
243 * Linphone core callback
246 linphonec_display_url (LinphoneCore * lc, const char *something, const char *url)
248 fprintf (stdout, "%s : %s\n", something, url);
252 * Linphone core callback
255 linphonec_prompt_for_auth(LinphoneCore *lc, const char *realm, const char *username)
257 /* no prompt possible when using pipes or tcp mode*/
259 linphone_core_abort_authentication(lc,NULL);
261 LinphoneAuthInfo *pending_auth;
263 if ( auth_stack.nitems+1 > MAX_PENDING_AUTH )
266 "Can't accept another authentication request.\n"
267 "Consider incrementing MAX_PENDING_AUTH macro.\n");
271 pending_auth=linphone_auth_info_new(username,NULL,NULL,NULL,realm);
272 auth_stack.elem[auth_stack.nitems++]=pending_auth;
277 * Linphone core callback
280 linphonec_notify_received(LinphoneCore *lc, LinphoneCall *call, const char *from,const char *event)
282 if(!strcmp(event,"refer"))
284 linphonec_out("The distand endpoint %s of call %li has been transfered, you can safely close the call.\n",
285 from,(long)linphone_call_get_user_pointer (call));
291 * Linphone core callback
294 linphonec_notify_presence_received(LinphoneCore *lc,LinphoneFriend *fid)
296 char *tmp=linphone_address_as_string(linphone_friend_get_address(fid));
297 printf("Friend %s is %s\n", tmp, linphone_online_status_to_string(linphone_friend_get_status(fid)));
299 // todo: update Friend list state (unimplemented)
303 * Linphone core callback
306 linphonec_new_unknown_subscriber(LinphoneCore *lc, LinphoneFriend *lf,
309 printf("Friend %s requested subscription "
310 "(accept/deny is not implemented yet)\n", url);
311 // This means that this person wishes to be notified
312 // of your presence information (online, busy, away...).
316 static void linphonec_call_state_changed(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState st, const char *msg){
317 char *from=linphone_call_get_remote_address_as_string(call);
318 long id=(long)linphone_call_get_user_pointer (call);
320 case LinphoneCallEnd:
321 linphonec_out("Call %i with %s ended.\n", id, from);
323 case LinphoneCallResuming:
324 linphonec_out("Resuming call %i with %s.\n", id, from);
326 case LinphoneCallStreamsRunning:
327 linphonec_out("Media streams established with %s for call %i.\n", from,id);
329 case LinphoneCallPausing:
330 linphonec_out("Pausing call %i with %s.\n", id, from);
332 case LinphoneCallPaused:
333 linphonec_out("Call %i with %s is now paused.\n", id, from);
335 case LinphoneCallPausedByRemote:
336 linphonec_out("Call %i has been paused by %s.\n",id,from);
338 case LinphoneCallIncomingReceived:
339 linphonec_call_identify(call);
340 id=(long)linphone_call_get_user_pointer (call);
341 linphonec_set_caller(from);
345 linphonec_out("Receiving new incoming call from %s, assigned id %i", from,id);
347 case LinphoneCallOutgoingInit:
348 linphonec_call_identify(call);
357 * Linphone core callback
360 linphonec_text_received(LinphoneCore *lc, LinphoneChatRoom *cr,
361 const char *from, const char *msg)
363 printf("%s: %s\n", from, msg);
364 // TODO: provide mechanism for answering.. ('say' command?)
368 static void linphonec_dtmf_received(LinphoneCore *lc, LinphoneCall *call, int dtmf){
369 char *from=linphone_call_get_remote_address_as_string(call);
370 fprintf(stdout,"Receiving tone %c from %s\n",dtmf,from);
375 static char received_prompt[PROMPT_MAX_LEN];
376 static ms_mutex_t prompt_mutex;
377 static bool_t have_prompt=FALSE;
379 static void *prompt_reader_thread(void *arg){
381 char tmp[PROMPT_MAX_LEN];
382 while ((ret=fgets(tmp,sizeof(tmp),stdin))!=NULL){
383 ms_mutex_lock(&prompt_mutex);
384 strcpy(received_prompt,ret);
386 ms_mutex_unlock(&prompt_mutex);
391 static void start_prompt_reader(void){
393 ms_mutex_init(&prompt_mutex,NULL);
394 ortp_thread_create(&th,NULL,prompt_reader_thread,NULL);
396 #if !defined(_WIN32_WCE)
397 static ortp_pipe_t create_server_socket(void){
400 snprintf(path,sizeof(path)-1,"linphonec-%i",getuid());
404 DWORD size=sizeof(username)-1;
405 GetUserName(username,&size);
406 snprintf(path,sizeof(path)-1,"linphonec-%s",username);
409 return ortp_server_pipe_create(path);
413 static void *pipe_thread(void*p){
415 server_sock=create_server_socket();
416 if (server_sock==ORTP_PIPE_INVALID) return NULL;
417 while(pipe_reader_run){
418 while(client_sock!=ORTP_PIPE_INVALID){ /*sleep until the last command is finished*/
425 client_sock=ortp_server_pipe_accept_client(server_sock);
426 if (client_sock!=ORTP_PIPE_INVALID){
428 /*now read from the client */
429 if ((len=ortp_pipe_read(client_sock,(uint8_t*)tmp,sizeof(tmp)-1))>0){
430 ortp_mutex_lock(&prompt_mutex);
432 strcpy(received_prompt,tmp);
433 printf("Receiving command '%s'\n",received_prompt);fflush(stdout);
435 ortp_mutex_unlock(&prompt_mutex);
437 printf("read nothing\n");fflush(stdout);
438 ortp_server_pipe_close_client(client_sock);
439 client_sock=ORTP_PIPE_INVALID;
443 if (pipe_reader_run) fprintf(stderr,"accept() failed: %s\n",strerror(errno));
446 ms_message("Exiting pipe_reader_thread.");
451 static void start_pipe_reader(void){
452 ms_mutex_init(&prompt_mutex,NULL);
453 pipe_reader_run=TRUE;
454 ortp_thread_create(&pipe_reader_th,NULL,pipe_thread,NULL);
457 static void stop_pipe_reader(void){
458 pipe_reader_run=FALSE;
459 linphonec_command_finished();
460 ortp_server_pipe_close(server_sock);
461 ortp_thread_join(pipe_reader_th,NULL);
463 #endif /*_WIN32_WCE*/
466 #define BOOL_HAVE_READLINE 1
468 #define BOOL_HAVE_READLINE 0
471 char *linphonec_readline(char *prompt){
472 if (unix_socket || !BOOL_HAVE_READLINE ){
473 static bool_t prompt_reader_started=FALSE;
474 static bool_t pipe_reader_started=FALSE;
475 if (!prompt_reader_started){
476 start_prompt_reader();
477 prompt_reader_started=TRUE;
479 if (unix_socket && !pipe_reader_started){
480 #if !defined(_WIN32_WCE)
482 pipe_reader_started=TRUE;
483 #endif /*_WIN32_WCE*/
485 fprintf(stdout,"%s",prompt);
488 ms_mutex_lock(&prompt_mutex);
490 char *ret=strdup(received_prompt);
492 ms_mutex_unlock(&prompt_mutex);
495 ms_mutex_unlock(&prompt_mutex);
496 linphonec_idle_call();
499 /* Following is to get the video window going as it
500 should. Maybe should we only have this on when the option -V
504 if (PeekMessage(&msg, NULL, 0, 0,1)) {
505 TranslateMessage(&msg);
506 DispatchMessage(&msg);
514 return readline(prompt);
519 void linphonec_out(const char *fmt,...){
522 va_start (args, fmt);
523 res=ortp_strdup_vprintf(fmt,args);
527 #if !defined(_WIN32_WCE)
528 if (client_sock!=ORTP_PIPE_INVALID){
529 if (ortp_pipe_write(client_sock,(uint8_t*)res,strlen(res))==-1){
530 fprintf(stderr,"Fail to send output via pipe: %s",strerror(errno));
533 #endif /*_WIN32_WCE*/
537 void linphonec_command_finished(void){
538 #if !defined(_WIN32_WCE)
539 if (client_sock!=ORTP_PIPE_INVALID){
540 ortp_server_pipe_close_client(client_sock);
541 client_sock=ORTP_PIPE_INVALID;
543 #endif /*_WIN32_WCE*/
546 void linphonec_set_autoanswer(bool_t enabled){
550 bool_t linphonec_get_autoanswer(){
554 LinphoneCoreVTable linphonec_vtable={0};
556 /***************************************************************************/
562 * - char *histfile_name
565 #if defined (_WIN32_WCE)
567 char **convert_args_to_ascii(int argc, _TCHAR **wargv){
569 char **result=malloc(argc*sizeof(char*));
572 wcstombs(argtmp,wargv[i],sizeof(argtmp));
573 result[i]=strdup(argtmp);
578 int _tmain(int argc, _TCHAR* wargv[]) {
579 char **argv=convert_args_to_ascii(argc,wargv);
584 main (int argc, char *argv[]) {
586 linphonec_vtable.call_state_changed=linphonec_call_state_changed;
587 linphonec_vtable.notify_presence_recv = linphonec_notify_presence_received;
588 linphonec_vtable.new_unknown_subscriber = linphonec_new_unknown_subscriber;
589 linphonec_vtable.auth_info_requested = linphonec_prompt_for_auth;
590 linphonec_vtable.display_status = linphonec_display_status;
591 linphonec_vtable.display_message=linphonec_display_something;
592 linphonec_vtable.display_warning=linphonec_display_warning;
593 linphonec_vtable.display_url=linphonec_display_url;
594 linphonec_vtable.text_received=linphonec_text_received;
595 linphonec_vtable.dtmf_received=linphonec_dtmf_received;
596 linphonec_vtable.refer_received=linphonec_display_refer;
597 linphonec_vtable.notify_recv=linphonec_notify_received;
599 if (! linphonec_init(argc, argv) ) exit(EXIT_FAILURE);
601 linphonec_main_loop (linphonec, sipAddr);
603 linphonec_finish(EXIT_SUCCESS);
605 exit(EXIT_SUCCESS); /* should never reach here */
609 * Initialize linphonec
612 linphonec_init(int argc, char **argv)
615 //g_mem_set_vtable(&dbgtable);
618 * Set initial values for global variables
624 snprintf(configfile_name, PATH_MAX, "%s/.linphonerc",
626 #elif defined(_WIN32_WCE)
627 strncpy(configfile_name,PACKAGE_DIR "\\linphonerc",PATH_MAX);
628 mylogfile=fopen(PACKAGE_DIR "\\" "linphonec.log","w");
629 printf("Logs are redirected in" PACKAGE_DIR "\\linphonec.log");
631 snprintf(configfile_name, PATH_MAX, "%s/Linphone/linphonerc",
634 /* Handle configuration filename changes */
635 switch (handle_configfile_migration())
637 case -1: /* error during file copies */
639 "Error in configuration file migration\n");
642 case 0: /* nothing done */
643 case 1: /* migrated */
649 if (NULL == bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR))
650 perror ("bindtextdomain failed");
652 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
654 textdomain (GETTEXT_PACKAGE);
656 printf ("NLS disabled.\n");
659 linphonec_parse_cmdline(argc, argv);
663 if (logfile_name != NULL)
664 mylogfile = fopen (logfile_name, "w+");
666 if (mylogfile == NULL)
670 "INFO: no logfile, logging to stdout\n");
672 linphone_core_enable_logs(mylogfile);
676 linphone_core_disable_logs();
679 * Initialize auth stack
684 * Initialize linphone core
686 linphonec=linphone_core_new (&linphonec_vtable, configfile_name, NULL,
688 linphone_core_enable_video(linphonec,vcap_enabled,display_enabled);
689 linphone_core_enable_video_preview(linphonec,preview_enabled);
690 if (!(vcap_enabled || display_enabled)) printf("Warning: video is disabled in linphonec, use -V or -C or -D to enable.\n");
693 * Initialize readline
695 linphonec_initialize_readline();
697 #if !defined(_WIN32_WCE)
699 * Initialize signal handlers
701 signal(SIGTERM, linphonec_finish);
702 signal(SIGINT, linphonec_finish);
703 #endif /*_WIN32_WCE*/
708 void linphonec_main_loop_exit(void){
709 linphonec_running=FALSE;
713 * Close linphonec, cleanly terminating
717 linphonec_finish(int exit_status)
719 linphonec_out("Terminating...\n");
721 /* Terminate any pending call */
722 linphone_core_terminate_all_calls(linphonec);
724 linphonec_finish_readline();
726 #if !defined(_WIN32_WCE)
729 #endif /*_WIN32_WCE*/
731 linphone_core_destroy (linphonec);
733 if (mylogfile != NULL && mylogfile != stdout)
743 * This is called from idle_call() whenever
744 * pending_auth != NULL.
746 * It prompts user for a password.
747 * Hitting ^D (EOF) would make this function
749 * Any other input would try to set linphone core
750 * auth_password for the pending_auth, add the auth_info
754 linphonec_prompt_for_auth_final(LinphoneCore *lc)
757 char auth_prompt[256];
759 rl_hook_func_t *old_event_hook;
761 LinphoneAuthInfo *pending_auth=auth_stack.elem[auth_stack.nitems-1];
763 snprintf(auth_prompt, 256, "Password for %s on %s: ",
764 pending_auth->username, pending_auth->realm);
769 * Disable event hook to avoid entering an
770 * infinite loop. This would prevent idle_call
771 * from being called during authentication reads.
772 * Note that it might be undesiderable...
774 old_event_hook=rl_event_hook;
780 input=linphonec_readline(auth_prompt);
783 * If EOF (^D) is sent you probably don't want
784 * to provide an auth password... should give up
785 * the operation, but there's no mechanism to
786 * send this info back to caller currently...
790 printf("Cancel requested, but not implemented.\n");
795 iptr=lpc_strip_blanks(input);
798 * Only blanks, continue asking
806 /* Something typed, let's try */
811 * No check is done here to ensure password is correct.
812 * I guess password will be asked again later.
814 linphone_auth_info_set_passwd(pending_auth, input);
815 linphone_core_add_auth_info(lc, pending_auth);
816 linphone_auth_info_destroy(pending_auth);
817 auth_stack.elem[auth_stack.nitems-1]=0;
818 --(auth_stack.nitems);
821 * Reset line_buffer, to avoid the password
822 * to be used again from outer readline
824 rl_line_buffer[0]='\0';
825 rl_event_hook=old_event_hook;
831 print_usage (int exit_status)
833 fprintf (stdout, "\n\
834 usage: linphonec [-c file] [-s sipaddr] [-a] [-V] [-d level ] [-l logfile]\n\
837 -c file specify path of configuration file.\n\
838 -d level be verbose. 0 is no output. 6 is all output\n\
839 -l logfile specify the log file for your SIP phone\n\
840 -s sipaddress specify the sip call to do at startup\n\
841 -a enable auto answering for incoming calls\n\
842 -V enable video features globally (disabled by default)\n\
843 -C enable video capture only (disabled by default)\n\
844 -D enable video display only (disabled by default)\n\
845 -S show general state messages (disabled by default)\n\
846 -v or --version display version and exits.\n");
853 #ifdef HAVE_X11_XLIB_H
854 static void sdl_x11_apply_video_params(){
855 static SDL_SysWMinfo info;
856 bool_t wminfo_ready=FALSE;
859 SDL_VERSION(&info.version);
860 if ( SDL_GetWMInfo(&info) ) {
861 if ( info.subsystem == SDL_SYSWM_X11 ) {
866 if ( !wminfo_ready) return;
870 unsigned int flags=0;
871 Display *display = info.info.x11.display;
872 Window window = info.info.x11.wmwindow;
874 memset(&wc,0,sizeof(wc));
875 wc.x=lpc_video_params.x;
876 wc.y=lpc_video_params.y;
877 wc.width=lpc_video_params.w;
878 wc.height=lpc_video_params.h;
879 if (lpc_video_params.x!=-1 ){
882 if (lpc_video_params.w!=-1){
883 flags|=CWWidth|CWHeight;
885 info.info.x11.lock_func();
886 XConfigureWindow(display,window,flags,&wc);
887 if (lpc_video_params.show)
888 XMapWindow(display,window);
890 XUnmapWindow(display,window);
891 info.info.x11.unlock_func();
897 static void lpc_apply_video_params(){
898 static unsigned long prev_wid=0;
900 unsigned long wid=linphone_core_get_native_video_window_id (linphonec);
902 if (wid!=0 && (lpc_video_params.refresh || prev_wid!=wid)){
903 lpc_video_params.refresh=FALSE;
904 #ifdef HAVE_X11_XLIB_H
905 sdl_x11_apply_video_params();
916 * Called every second from main read loop.
918 * Will use the following globals:
920 * - LinphoneCore linphonec
921 * - LPC_AUTH_STACK auth_stack;
925 linphonec_idle_call ()
927 LinphoneCore *opm=linphonec;
929 /* Uncomment the following to verify being called */
932 linphone_core_iterate(opm);
934 fprintf (stdout, "-------auto answering to call-------\n" );
935 linphone_core_accept_call(opm,NULL);
939 if ( auth_stack.nitems )
942 * Inhibit command completion
943 * during password prompts
946 rl_inhibit_completion=1;
948 linphonec_prompt_for_auth_final(opm);
950 rl_inhibit_completion=0;
955 lpc_apply_video_params();
965 * - char *histfile_name (also sets this)
966 * - char *last_in_history (allocates it)
969 linphonec_initialize_readline()
971 /*rl_bind_key('\t', rl_insert);*/
973 /* Allow conditional parsing of ~/.inputrc */
974 rl_readline_name = "linphonec";
976 /* Call idle_call() every second */
977 rl_set_keyboard_input_timeout(LPC_READLINE_TIMEOUT);
978 rl_event_hook=linphonec_idle_call;
980 /* Set history file and read it */
981 histfile_name = ms_strdup_printf ("%s/.linphonec_history",
983 read_history(histfile_name);
985 /* Initialized last_in_history cache*/
986 last_in_history[0] = '\0';
988 /* Register a completion function */
989 rl_attempted_completion_function = linephonec_readline_completion;
991 /* printf("Readline initialized.\n"); */
999 * - char *histfile_name (writes history to file and frees it)
1000 * - char *last_in_history (frees it)
1004 linphonec_finish_readline()
1007 stifle_history(HISTSIZE);
1008 write_history(histfile_name);
1009 free(histfile_name);
1016 static void print_prompt(LinphoneCore *opm){
1017 #ifdef IDENTITY_AS_PROMPT
1018 snprintf(prompt, PROMPT_MAX_LEN, "%s> ",
1019 linphone_core_get_primary_contact(opm));
1021 snprintf(prompt, PROMPT_MAX_LEN, "linphonec> ");
1026 linphonec_main_loop (LinphoneCore * opm, char * sipAddr)
1028 char buf[LINE_MAX_LEN]; /* auto call handling */
1034 /* auto call handling */
1035 if (sipAddr != NULL )
1037 snprintf (buf, sizeof(buf),"call %s", sipAddr);
1038 linphonec_parse_command_line(linphonec, buf);
1041 while (linphonec_running && (input=linphonec_readline(prompt)))
1043 char *iptr; /* input and input pointer */
1047 iptr=lpc_strip_blanks(input);
1049 input_len = strlen(iptr);
1052 * Do nothing but release memory
1053 * if only blanks are read
1061 #ifdef HAVE_READLINE
1063 * Only add to history if not already
1064 * last item in it, and only if the command
1065 * doesn't start with a space (to allow for
1068 if ( iptr == input && strcmp(last_in_history, iptr) )
1070 strncpy(last_in_history,iptr,sizeof(last_in_history));
1071 last_in_history[sizeof(last_in_history)-1]='\0';
1076 linphonec_parse_command_line(linphonec, iptr);
1077 linphonec_command_finished();
1085 * Parse command line switches
1090 * - char *logfile_name
1091 * - char *configfile_name
1095 linphonec_parse_cmdline(int argc, char **argv)
1099 while (arg_num < argc)
1101 int old_arg_num = arg_num;
1102 if (strncmp ("-d", argv[arg_num], 2) == 0)
1106 trace_level = atoi (argv[arg_num]);
1110 else if (strncmp ("-l", argv[arg_num], 2) == 0)
1114 logfile_name = argv[arg_num];
1116 else if (strncmp ("-c", argv[arg_num], 2) == 0)
1118 if ( ++arg_num >= argc ) print_usage(EXIT_FAILURE);
1119 #if !defined(_WIN32_WCE)
1120 if (access(argv[arg_num],F_OK)!=0 )
1123 "Cannot open config file %s.\n",
1127 #endif /*_WIN32_WCE*/
1128 snprintf(configfile_name, PATH_MAX, "%s", argv[arg_num]);
1130 else if (strncmp ("-s", argv[arg_num], 2) == 0)
1134 sipAddr = argv[arg_num];
1136 else if (strncmp ("-a", argv[arg_num], 2) == 0)
1140 else if (strncmp ("-C", argv[arg_num], 2) == 0)
1142 vcap_enabled = TRUE;
1144 else if (strncmp ("-D", argv[arg_num], 2) == 0)
1146 display_enabled = TRUE;
1148 else if (strncmp ("-V", argv[arg_num], 2) == 0)
1150 display_enabled = TRUE;
1151 vcap_enabled = TRUE;
1152 preview_enabled=TRUE;
1154 else if ((strncmp ("-v", argv[arg_num], 2) == 0)
1157 ("--version", argv[arg_num],
1158 strlen ("--version")) == 0))
1160 #if !defined(_WIN32_WCE)
1161 printf ("version: " LINPHONE_VERSION "\n");
1163 exit (EXIT_SUCCESS);
1165 else if (strncmp ("-S", argv[arg_num], 2) == 0)
1167 show_general_state = TRUE;
1169 else if (strncmp ("--pipe", argv[arg_num], 6) == 0)
1173 else if (old_arg_num == arg_num)
1175 fprintf (stderr, "ERROR: bad arguments\n");
1176 print_usage (EXIT_FAILURE);
1185 * Up to version 1.2.1 linphone used ~/.linphonec for
1186 * CLI and ~/.gnome2/linphone for GUI as configuration file.
1187 * In newer version both interfaces will use ~/.linphonerc.
1189 * This function helps transparently migrating from one
1190 * to the other layout using the following heuristic:
1192 * IF new_config EXISTS => do nothing
1193 * ELSE IF old_cli_config EXISTS => copy to new_config
1194 * ELSE IF old_gui_config EXISTS => copy to new_config
1197 * 0 if it did nothing
1198 * 1 if it migrated successfully
1202 handle_configfile_migration()
1204 #if !defined(_WIN32_WCE)
1208 #if !defined(_WIN32_WCE)
1209 const char *home = getenv("HOME");
1211 const char *home = ".";
1212 #endif /*_WIN32_WCE*/
1213 new_cfg = ms_strdup_printf("%s/.linphonerc", home);
1216 * If the *NEW* configuration already exists
1219 if (access(new_cfg,F_OK)==0)
1225 old_cfg_cli = ms_strdup_printf("%s/.linphonec", home);
1228 * If the *OLD* CLI configurations exist copy it to
1229 * the new file and make it a symlink.
1231 if (access(old_cfg_cli, F_OK)==0)
1233 if ( ! copy_file(old_cfg_cli, new_cfg) )
1239 printf("%s copied to %s\n", old_cfg_cli, new_cfg);
1246 old_cfg_gui = ms_strdup_printf("%s/.gnome2/linphone", home);
1249 * If the *OLD* GUI configurations exist copy it to
1250 * the new file and make it a symlink.
1252 if (access(old_cfg_gui, F_OK)==0)
1254 if ( ! copy_file(old_cfg_gui, new_cfg) )
1261 printf("%s copied to %s\n", old_cfg_gui, new_cfg);
1269 #endif /*_WIN32_WCE*/
1272 #if !defined(_WIN32_WCE)
1274 * Copy file "from" to file "to".
1275 * Destination file is truncated if existing.
1276 * Return 1 on success, 0 on error (printing an error).
1279 copy_file(const char *from, const char *to)
1286 /* Open "from" file for reading */
1287 in=fopen(from, "r");
1290 snprintf(message, 255, "Can't open %s for reading: %s\n",
1291 from, strerror(errno));
1292 fprintf(stderr, "%s", message);
1296 /* Open "to" file for writing (will truncate existing files) */
1300 snprintf(message, 255, "Can't open %s for writing: %s\n",
1301 to, strerror(errno));
1302 fprintf(stderr, "%s", message);
1306 /* Copy data from "in" to "out" */
1307 while ( (n=fread(buf, 1, sizeof buf, in)) > 0 )
1309 if ( ! fwrite(buf, 1, n, out) )
1320 #endif /*_WIN32_WCE*/
1322 #ifdef HAVE_READLINE
1324 linephonec_readline_completion(const char *text, int start, int end)
1326 char **matches = NULL;
1329 * Prevent readline from falling
1330 * back to filename-completion
1332 rl_attempted_completion_over=1;
1335 * If this is the start of line we complete with commands
1339 return rl_completion_matches(text, linphonec_command_generator);
1343 * Otherwise, we should peek at command name
1344 * or context to implement a smart completion.
1345 * For example: "call .." could return
1346 * friends' sip-uri as matches
1355 * Strip blanks from a string.
1356 * Return a pointer into the provided string.
1357 * Modifies input adding a NULL at first
1358 * of trailing blanks.
1361 lpc_strip_blanks(char *input)
1365 /* Find first non-blank */
1366 while(*input && isspace(*input)) ++input;
1368 /* Find last non-blank */
1369 iptr=input+strlen(input);
1371 while(isspace(*--iptr));
1378 /****************************************************************************
1380 * $Log: linphonec.c,v $
1381 * Revision 1.57 2007/11/14 13:40:27 smorlat
1382 * fix --disable-video build.
1384 * Revision 1.56 2007/09/26 14:07:27 fixkowalski
1385 * - ANSI/C++ compilation issues with non-GCC compilers
1386 * - Faster epm-based packaging
1387 * - Ability to build & run on FC6's eXosip/osip
1389 * Revision 1.55 2007/09/24 16:01:58 smorlat
1392 * Revision 1.54 2007/08/22 14:06:11 smorlat
1393 * authentication bugs fixed.
1395 * Revision 1.53 2007/02/13 21:31:01 smorlat
1396 * added patch for general state.
1397 * new doxygen for oRTP
1400 * Revision 1.52 2007/01/10 14:11:24 smorlat
1401 * add --video to linphonec.
1403 * Revision 1.51 2006/08/21 12:49:59 smorlat
1404 * merged several little patches.
1406 * Revision 1.50 2006/07/26 08:17:28 smorlat
1409 * Revision 1.49 2006/07/17 18:45:00 smorlat
1410 * support for several event queues in ortp.
1411 * glib dependency removed from coreapi/ and console/
1413 * Revision 1.48 2006/04/09 12:45:32 smorlat
1414 * linphonec improvements.
1416 * Revision 1.47 2006/04/04 08:04:34 smorlat
1417 * switched to mediastreamer2, most bugs fixed.
1419 * Revision 1.46 2006/03/16 17:17:40 smorlat
1422 * Revision 1.45 2006/03/12 21:48:31 smorlat
1423 * gcc-2.95 compile error fixed.
1424 * mediastreamer2 in progress
1426 * Revision 1.44 2006/03/04 11:17:10 smorlat
1427 * mediastreamer2 in progress.
1429 * Revision 1.43 2006/02/13 09:50:50 strk
1430 * fixed unused variable warning.
1432 * Revision 1.42 2006/02/02 15:39:18 strk
1433 * - Added 'friend list' and 'friend call' commands
1434 * - Allowed for multiple DTFM send in a single line
1435 * - Added status-specific callback (bare version)
1437 * Revision 1.41 2006/02/02 13:30:05 strk
1438 * - Padded vtable with missing callbacks
1439 * (fixing a segfault on friends subscription)
1440 * - Handled friends notify (bare version)
1441 * - Handled text messages receive (bare version)
1442 * - Printed message on subscription request (bare version)
1444 * Revision 1.40 2006/01/26 09:48:05 strk
1445 * Added limits.h include
1447 * Revision 1.39 2006/01/26 02:11:01 strk
1448 * Removed unused variables, fixed copyright date
1450 * Revision 1.38 2006/01/25 18:33:02 strk
1451 * Removed the -t swich, terminate_on_close made the default behaviour
1453 * Revision 1.37 2006/01/20 14:12:34 strk
1454 * Added linphonec_init() and linphonec_finish() functions.
1455 * Handled SIGINT and SIGTERM to invoke linphonec_finish().
1456 * Handling of auto-termination (-t) moved to linphonec_finish().
1457 * Reworked main (input read) loop to not rely on 'terminate'
1458 * and 'run' variable (dropped). configfile_name allocated on stack
1459 * using PATH_MAX limit. Changed print_usage signature to allow
1460 * for an exit_status specification.
1462 * Revision 1.36 2006/01/18 09:25:32 strk
1463 * Command completion inhibited in proxy addition and auth request prompts.
1464 * Avoided use of readline's internal filename completion.
1466 * Revision 1.35 2006/01/14 13:29:32 strk
1467 * Reworked commands interface to use a table structure,
1468 * used by command line parser and help function.
1469 * Implemented first level of completion (commands).
1470 * Added notification of invalid "answer" and "terminate"
1471 * commands (no incoming call, no active call).
1472 * Forbidden "call" intialization when a call is already active.
1473 * Cleaned up all commands, adding more feedback and error checks.
1475 * Revision 1.34 2006/01/13 13:00:29 strk
1476 * Added linphonec.h. Code layout change (added comments, forward decl,
1477 * globals on top, copyright notices and Logs). Handled out-of-memory
1478 * condition on history management. Removed assumption on sizeof(char).
1479 * Fixed bug in authentication prompt (introduced by readline).
1480 * Added support for multiple authentication requests (up to MAX_PENDING_AUTH).
1483 ****************************************************************************/