only for backup, still in coding state - no compile!!!
[lcr.git] / options.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** PBX4Linux                                                                 **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** reading options.conf and filling structure                                **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include "main.h"
16
17 struct options options = {
18         "/usr/local/pbx/log",           /* log file */
19         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
20          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
21          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
22          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
23          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
24          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
25          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
26          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
27         0x0000,                         /* debug mode */
28         'a',                            /* a-law */
29         "0",                            /* national prefix */
30         "00",                           /* international prefix */
31         "tones_american",               /* directory of tones */
32         "",                             /* directories of tones to fetch */
33         "extensions",                   /* directory of extensions */
34         "",                             /* h323 endpoint name */
35         0,                              /* h323 ringconnect */
36         0,4, 0,2, 0, 0, 0, 0,4, 0,4, 0,64, /* h323 codecs to use */
37         0,"",1720,                      /* allow incoming h323 calls */
38         0,"",                           /* register with h323 gatekeeper */
39         5060, 5,                        /* SIP port, maxqueue */
40         0,                              /* dtmf detection on */
41         "",                             /* dummy caller id */
42         0,                              /* inband patterns on external calls */
43         0,                              /* use tones by dsp.o */
44         0,                              /* by default use priority 0 */
45         "pbx@jolly.de"                  /* source mail adress */
46 };
47
48 /* read options
49  *
50  * read options from options.conf
51  */
52 int read_options(void)
53 {
54         FILE *fp=NULL;
55         char filename[128];
56         char *p;
57         char option[32];
58         char param[256];
59         unsigned int line,i;
60         char buffer[256];
61 #ifdef H323
62         int codecpri = 0;
63 #endif
64
65         SPRINT(filename, "%s/options.conf", INSTALL_DATA);
66
67         if (!(fp=fopen(filename,"r")))
68         {
69                 PERROR("Cannot open %s\n",filename);
70                 return(-1);
71         }
72
73         line=0;
74         while((fgets(buffer,sizeof(buffer),fp)))
75         {
76                 line++;
77                 buffer[sizeof(buffer)-1]=0;
78                 if (buffer[0]) buffer[strlen(buffer)-1]=0;
79                 p=buffer;
80
81                 while(*p <= 32) /* skip spaces */
82                 {
83                         if (*p == 0)
84                                 break;
85                         p++;
86                 }
87                 if (*p==0 || *p=='#') /* ignore comments and empty line */
88                         continue;
89
90                 option[0]=0;
91                 i=0; /* read option */
92                 while(*p > 32)
93                 {
94                         if (i+1 >= sizeof(option))
95                         {
96                                 PERROR_RUNTIME("Error in %s (line %d): option too long.\n",filename,line);
97                                 goto error;
98                         }
99                         option[i+1] = '\0';
100                         option[i++] = *p++;
101                 }
102
103                 while(*p <= 32) /* skip spaces */
104                 {
105                         if (*p == 0)
106                                 break;
107                         p++;
108                 }
109
110                 param[0]=0;
111                 if (*p!=0 && *p!='#') /* param */
112                 {
113                         i=0; /* read param */
114                         while(*p > 31)
115                         {
116                                 if (i+1 >= sizeof(param))
117                                 {
118                                         PERROR_RUNTIME("Error in %s (line %d): param too long.\n",filename,line);
119                                         goto error;
120                                 }
121                                 param[i+1] = '\0';
122                                 param[i++] = *p++;
123                         }
124                 }
125
126                 /* at this point we have option and param */
127
128                 /* check option */
129                 if (!strcmp(option,"nt_if") || !strcmp(option,"te_if"))
130                 {
131                         PERROR_RUNTIME("Error in %s (line %d): obsolete option %s. Use multiple 'port' options to define ports to use.\n",filename,line,option);
132                         goto error;
133                 } else
134                 if (!strcmp(option,"debug"))
135                 {
136                         if (param[0]==0)
137                         {
138                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
139                                 goto error;
140                         }
141                         options.deb = strtol(param, NULL, 0);
142
143                         PDEBUG(DEBUG_CONFIG, "debugging: 0x%x\n", options.deb);
144                 } else
145                 if (!strcmp(option,"log"))
146                 {
147                         if (param[0]==0)
148                         {
149                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
150                                 goto error;
151                         }
152                         SCPY(options.log, param);
153
154                         PDEBUG(DEBUG_CONFIG, "log file: %s\n", options.log);
155                 } else
156                 if (!strcmp(option,"port"))
157                 {
158                         i = strtol(param, NULL, 0);
159                         if (i < 1 || i > sizeof(options.ports))
160                         {
161                                 PERROR_RUNTIME("Error in %s (line %d): port number %s out of range.\n", filename, line, option);
162                                 goto error;
163                         }
164                         options.ports[i] |= FLAG_PORT_USE;
165
166                         PDEBUG(DEBUG_CONFIG, "adding interface: %d (param=%s)\n", i, param);
167                         if (strstr(param, "ptp"))
168                         {
169                                 options.ports[i] |= FLAG_PORT_PTP;
170                                 PDEBUG(DEBUG_CONFIG, " -> interface shall be ptp\n");
171                         }
172                 } else
173 #if 0
174                 if (!strcmp(option,"ptp"))
175                 {
176                         options.ptp = 1;
177
178                         PDEBUG(DEBUG_CONFIG, "ptp layer-2 watch and keep established.\n");
179                 } else
180 #endif
181                 if (!strcmp(option,"alaw"))
182                 {
183                         options.law = 'a';
184
185                         PDEBUG(DEBUG_CONFIG, "isdn audio type: alaw\n");
186                 } else
187                 if (!strcmp(option,"ulaw"))
188                 {
189                         options.law = 'u';
190
191                         PDEBUG(DEBUG_CONFIG, "isdn audio type: ulaw\n");
192                 } else
193                 if (!strcmp(option,"h323_name"))
194                 {
195 #ifdef H323
196                         SCPY(options.h323_name, param);
197
198                         PDEBUG(DEBUG_CONFIG, "H323 endpoint name: '%s'\n", param);
199 #endif
200                 } else
201                 if (!strcmp(option,"h323_ringconnect"))
202                 {
203 #ifdef H323
204                         options.h323_ringconnect = 1;
205
206                         PDEBUG(DEBUG_CONFIG, "H323 ringconnect: enabled\n");
207 #endif
208                 } else
209                 if (!strcmp(option,"h323_gsm"))
210                 {
211 #ifdef H323
212                         codecpri ++;
213                         options.h323_gsm_pri = codecpri;
214                         options.h323_gsm_opt = atoi(param);
215                         if (atoi(param)<1 && atoi(param)>7)
216                         {
217                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s must be in range 1..7.\n",filename,line,option);
218                                 goto error;
219                         }
220
221                         PDEBUG(DEBUG_CONFIG, "H323 codec to use: GSM, MicrosoftGSM priority %d\n", codecpri);
222 #endif
223                 } else
224                 if (!strcmp(option,"h323_g726"))
225                 {
226 #ifdef H323
227                         codecpri ++;
228                         options.h323_g726_pri = codecpri;
229                         options.h323_g726_opt = atoi(param);
230                         if (atoi(param)<2 && atoi(param)>5)
231                         {
232                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s must be in range 2..5.\n",filename,line,option);
233                                 goto error;
234                         }
235
236                         PDEBUG(DEBUG_CONFIG, "H323 codec to use: G726 priority %d\n", codecpri);
237 #endif
238                 } else
239                 if (!strcmp(option,"h323_g7231"))
240                 {
241 #ifdef H323
242                         codecpri ++;
243                         options.h323_g7231_pri = codecpri;
244
245                         PDEBUG(DEBUG_CONFIG, "H323 codec to use: G7231 priority %d\n", codecpri);
246 #endif
247                 } else
248                 if (!strcmp(option,"h323_g729a"))
249                 {
250 #ifdef H323
251                         codecpri ++;
252                         options.h323_g729a_pri = codecpri;
253
254                         PDEBUG(DEBUG_CONFIG, "H323 codec to use: G729A priority %d\n", codecpri);
255 #endif
256                 } else
257                 if (!strcmp(option,"h323_lpc10"))
258                 {
259 #ifdef H323
260                         codecpri ++;
261                         options.h323_lpc10_pri = codecpri;
262
263                         PDEBUG(DEBUG_CONFIG, "H323 codec to use: LPC-10 priority %d\n", codecpri);
264 #endif
265                 } else
266                 if (!strcmp(option,"h323_speex"))
267                 {
268 #ifdef H323
269                         codecpri ++;
270                         options.h323_speex_pri = codecpri;
271                         options.h323_speex_opt = atoi(param);
272                         if (atoi(param)<2 && atoi(param)>6)
273                         {
274                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s must be in range 2..6.\n",filename,line,option);
275                                 goto error;
276                         }
277
278                         PDEBUG(DEBUG_CONFIG, "H323 codec to use: Speex priority %d\n", codecpri);
279 #endif
280                 } else
281                 if (!strcmp(option,"h323_xspeex"))
282                 {
283 #ifdef H323
284                         codecpri ++;
285                         options.h323_xspeex_pri = codecpri;
286                         options.h323_xspeex_opt = atoi(param);
287                         if (atoi(param)<2 && atoi(param)>6)
288                         {
289                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s must be in range 2..6.\n",filename,line,option);
290                                 goto error;
291                         }
292
293                         PDEBUG(DEBUG_CONFIG, "H323 codec to use: XiphSpeex priority %d\n", codecpri);
294 #endif
295                 } else
296                 if (!strcmp(option,"h323_law"))
297                 {
298 #ifdef H323
299                         codecpri ++;
300                         options.h323_law_pri = codecpri;
301                         options.h323_law_opt = atoi(param);
302                         if (atoi(param)<10 && atoi(param)>240)
303                         {
304                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s must be in range 10..240.\n",filename,line,option);
305                                 goto error;
306                         }
307
308                         PDEBUG(DEBUG_CONFIG, "H323 codec to use: Alaw, muLaw priority %d\n", codecpri);
309 #endif
310                 } else
311                 if (!strcmp(option,"h323_icall"))
312                 {
313 #ifdef H323
314                         options.h323_icall = 1;
315                         SCPY(options.h323_icall_prefix, param);
316
317                         PDEBUG(DEBUG_CONFIG, "process incoming H323 call with prefix '%s'\n", param);
318 #endif
319                 } else
320                 if (!strcmp(option,"h323_port"))
321                 {
322 #ifdef H323
323                         options.h323_port = atoi(param);
324
325                         PDEBUG(DEBUG_CONFIG, "use port for incoming H323 calls: %d\n", atoi(param));
326 #endif
327                 } else
328                 if (!strcmp(option,"sip_port"))
329                 {
330 #ifdef SIP
331                         options.sip_port = atoi(param);
332
333                         PDEBUG(DEBUG_CONFIG, "use port for incoming SIP calls: %d\n", atoi(param));
334 #endif
335                 } else
336                 if (!strcmp(option,"sip_maxqueue"))
337                 {
338 #ifdef SIP
339                         options.sip_maxqueue = atoi(param);
340
341                         PDEBUG(DEBUG_CONFIG, "number of simultanious incoming sockets for SIP calls: %d\n", atoi(param));
342 #endif
343                 } else
344                 if (!strcmp(option,"h323_gatekeeper"))
345                 {
346 #ifdef H323
347                         options.h323_gatekeeper = 1;
348                         if (param[0])
349                         {
350                                 SCPY(options.h323_gatekeeper_host, param);
351                         }
352                         PDEBUG(DEBUG_CONFIG, "register with H323 gatekeeper (%s)\n", (param[0])?param:"automatically");
353 #endif
354                 } else
355                 if (!strcmp(option,"tones_dir"))
356                 {
357                         if (param[0]==0)
358                         {
359                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
360                                 goto error;
361                         }
362                         if (param[strlen(param)-1] == '/')
363                                 param[strlen(param)-1]=0;
364                         SCPY(options.tones_dir, param);
365
366                         PDEBUG(DEBUG_CONFIG, "directory of tones: %s\n",param);
367                 } else
368                 if (!strcmp(option,"fetch_tones"))
369                 {
370                         if (param[0]==0)
371                         {
372                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
373                                 goto error;
374                         }
375                         if (param[strlen(param)-1] == '/')
376                                 param[strlen(param)-1]=0;
377                         SCPY(options.fetch_tones, param);
378
379                         PDEBUG(DEBUG_CONFIG, "directories of tones to fetch: %s\n",param);
380                 } else
381                 if (!strcmp(option,"extensions_dir"))
382                 {
383                         if (param[0]==0)
384                         {
385                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
386                                 goto error;
387                         }
388                         if (param[strlen(param)-1] == '/')
389                                 param[strlen(param)-1]=0;
390                         SCPY(options.extensions_dir, param);
391
392                         PDEBUG(DEBUG_CONFIG, "directory of extensions: %s\n",param);
393                 } else
394                 if (!strcmp(option,"national"))
395                 {
396                         SCPY(options.national, param);
397
398                         PDEBUG(DEBUG_CONFIG, "national dial prefix: %s\n", param);
399                 } else
400                 if (!strcmp(option,"international"))
401                 {
402                         SCPY(options.international, param);
403
404                         PDEBUG(DEBUG_CONFIG, "inernational dial prefix: %s\n", param);
405                 } else
406                 if (!strcmp(option,"nodtmf"))
407                 {
408                         options.nodtmf = 1;
409
410                         PDEBUG(DEBUG_CONFIG, "disable dtmf detection\n");
411                 } else
412                 if (!strcmp(option,"dummyid"))
413                 {
414                         SCPY(options.dummyid, param);
415
416                         PDEBUG(DEBUG_CONFIG, "dummy caller id\n", param);
417                 } else
418                 if (!strcmp(option,"inbandpattern"))
419                 {
420                         if (!strcasecmp(param, "yes"))
421                                 options.inbandpattern = 1;
422
423                         PDEBUG(DEBUG_CONFIG, "inband pattern = %s\n", (options.inbandpattern)?"yes":"no");
424                 } else
425                 if (!strcmp(option,"dsptones"))
426                 {
427                         if (!strcasecmp(param, "american"))
428                                 options.dsptones = DSP_AMERICAN;
429                         else if (!strcasecmp(param, "german"))
430                                 options.dsptones = DSP_GERMAN;
431                         else if (!strcasecmp(param, "oldgerman"))
432                                 options.dsptones = DSP_OLDGERMAN;
433                         else if (!strcasecmp(param, "none"))
434                                 options.dsptones = DSP_NONE;
435                         else {
436                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
437                                 goto error;
438                         }
439
440                         PDEBUG(DEBUG_CONFIG, "dsp tones = %d\n", options.dsptones);
441                 } else
442                 if (!strcmp(option,"schedule"))
443                 {
444                         options.schedule = atoi(param);
445                         if (options.schedule < 0)
446                         {
447                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s must be at least '0'.\n", filename,line,option);
448                                 goto error;
449                         }
450                         if (options.schedule > 99)
451                         {
452                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s must be '99' or less.\n", filename,line,option);
453                                 goto error;
454                         }
455
456                         if (atoi(param))
457                                 PDEBUG(DEBUG_CONFIG, "use real time scheduler priority: %d\n", atoi(param));
458                         else
459                                 PDEBUG(DEBUG_CONFIG, "don't use real time scheduler\n");
460                 } else
461                 if (!strcmp(option,"email"))
462                 {
463                         if (param[0]==0)
464                         {
465                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s missing.\n", filename,line,option);
466                                 goto error;
467                         }
468                         SCPY(options.email, param);
469
470                         PDEBUG(DEBUG_CONFIG, "source mail address of pbx: %s\n", param);
471                 } else
472                 {
473                         PERROR_RUNTIME("Error in %s (line %d): wrong option keyword %s.\n", filename,line,option);
474                         goto error;
475                 }
476         }
477
478 #if 0
479         if (!options.dsptones)
480         {
481                 PERROR_RUNTIME("Error in %s (line %d): option 'dsptones' missing.\n", filename);
482                 goto error;
483         }
484 #endif
485         if (!options.tones_dir[0])
486         {
487                 PERROR_RUNTIME("Error in %s (line %d): option 'tones_dir' with parameter missing.\n", filename);
488                 goto error;
489         }
490         if (fp) fclose(fp);
491         return(1);
492 error:
493         if (fp) fclose(fp);
494         return(0);
495 }
496
497