]> git.sur5r.net Git - openldap/blob - clients/fax500/faxtotpc.c
missing 2nd argument to faxtotpc()
[openldap] / clients / fax500 / faxtotpc.c
1 /*
2  * Copyright (c) 1993 Regents of the University of Michigan.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of Michigan at Ann Arbor. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  *
12  *
13  * Routines for parsing the facsimileTelephoneNumber field out of
14  * an X.500 entry and converting it to a "tpc.int" domain name.
15  *
16  * char *faxtotpc( char *str, char *userinfo)
17  *
18  * faxtotpc() returns a pointer to a string allocated with malloc(3).
19  */
20
21 #include "portable.h"
22
23 #include <stdio.h>
24 #include <ctype.h>
25
26 #include <sys/types.h>
27 #include <ac/string.h>
28
29 #define TPCDOMAIN       "tpc.int"
30
31 /*
32  * Remove everything from 'str' which is not a digit
33  */
34 void strip_nonnum(str)
35 char *str;
36 {
37         char *p, *q;
38         p = q = str;
39         for (;;) {
40                 if (*p == '\0') {
41                         *q = *p;
42                         return;
43                 }
44
45                 if (isdigit((u_char) *p)) {
46                         *q = *p;
47                         p++;
48                         q++;
49                 } else {
50                         p++;
51                 }
52         }
53 }
54
55
56
57 /* 
58  * Remove anything of the form (blah) where
59  * "blah" contains a non-numeric character.
60  */
61 char *remove_parens(ibuf, obuf)
62 char *ibuf;
63 char *obuf;
64 {
65         char *p = ibuf;
66         char *q = obuf;
67
68         while (*p != '\0') {
69                 char *s;
70                 char *t;
71                 if (*p == '(') {
72                         /* look for a closing paren */
73                         if (( s = strchr(p, ')')) != NULL) {
74                                 /* Check the string between p and s */
75                                 /* for non-numeric characters       */
76                                 t = p + 1;
77                                 while (t < s) {
78                                         if (!isdigit((u_char) *t)) {
79                                                 /* garbage, delete */
80                                                 p = s + 1;
81                                                 t = p;
82                                                 break;
83                                         }
84                                         t++;
85                                 }
86                                 /* when we get here, p points to the first */
87                                 /* thing we want to keep, t to the last.   */
88                                 strncpy(q, p,  t - p);
89                                 q += t - p;
90                                 p = t;
91                         } else {
92                                 /* no closing paren, what to do?  keep it */
93                                 *q = *p;
94                                 p++;
95                                 q++;
96                         }
97                 } else {
98                         /* not a paren - copy out */
99                         *q = *p;
100                         p++;
101                         q++;
102                 }
103         }
104         *q = '\0';      /* terminate output string */
105         return(obuf);
106 }
107
108
109
110
111 /*
112  * Apply local fixups to phone numbers here.  Replace this routine
113  * with code to expand common "abbreviations" for phone numbers.  For
114  * example, on the U-M campus, it's only necessary to dial the last
115  * 5 digits of the telephone number, and hence people here tend to
116  * give only the last 5 digits of their fax numbers.
117  *
118  * Local U-M mods:
119  * If exactly 5 digits were provided, assume it's a campus
120  * phone number and prepend "1313nm" where "mn" are computed
121  * according to the following:
122  * first digit of 
123  * 5-digit "Local" 
124  * phone              mn
125  * -----              --
126  * 3                  76 e.g. "31234" -> "7631234"
127  * 4                  76
128  * 7                  74
129  * 6                  93
130  * 8                  99
131  */
132 char *munge_phone(ibuf, obuf)
133 char *ibuf;
134 char *obuf;
135 {
136 #define UMAREACODE      "1313"
137
138         char prefix[3];
139
140         if (strlen(ibuf) == 7) {
141                 /* Assume local number w/o area code */
142                 sprintf(obuf, "%s%s", UMAREACODE, ibuf);
143                 return(obuf);
144         }
145         if (strlen(ibuf) == 10) {
146                 /* Assume local number with area code */
147                 sprintf(obuf, "%s%s", "1", ibuf);
148                 return(obuf);
149         }
150         if (strlen(ibuf) != 5) {
151                 /* Not 5 digits - leave alone */
152                 strcpy(obuf, ibuf);
153                 return(obuf);
154         }
155
156         switch (ibuf[0]) {
157           case '3'      :
158           case '4'      :       strcpy(prefix, "76");
159                                 break;
160           case '7'      :       strcpy(prefix, "74");
161                                 break;
162           case '6'      :       strcpy(prefix, "93");
163                                 break;
164           case '8'      :       strcpy(prefix, "99");
165                                 break;
166           default       :       /* Unknown, leave alone */
167                                 strcpy(obuf, ibuf);
168                                 return(obuf);
169         }
170         sprintf(obuf, "%s%s%s", UMAREACODE, prefix, ibuf);
171         return(obuf);
172 }
173
174
175
176
177
178
179
180
181 /* 
182  * Convert string to "tpc.int" domain name.
183  */
184 char *faxtotpc(phone, userinfo)
185 char *phone;
186 char *userinfo;
187 {
188         char *p;
189         char *q;
190         char ibuf[255];
191         char obuf[255];
192
193         /* nuke spaces */
194         strcpy(ibuf, phone);
195         for (p = ibuf, q = obuf; *p != '\0'; p++) {
196                 if (*p != ' ') {
197                         *q = *p;
198                         q++;
199                 }
200         }
201         *q = '\0';
202         strcpy(ibuf, obuf);
203
204         remove_parens(ibuf, obuf);
205         strcpy(ibuf, obuf);
206
207         /* Look for "+" - if followed by a number,
208            assume it's an international number and leave
209            it alone.
210         */
211         if ((p = strchr(ibuf, '+')) != NULL) {
212                 if (isdigit((u_char) *(p + 1))) {
213                         /* strip any non-digits */
214                         strip_nonnum(ibuf);
215                 }
216         } else {
217                 strip_nonnum(ibuf);
218
219                 /* Apply local munges */
220                 munge_phone(ibuf, obuf);
221                 strcpy(ibuf, obuf);
222         }
223
224         /* Convert string of form abcd to remote-printer@d.c.b.a.tpc.int */
225         q = obuf;
226         for (p = ibuf + strlen(ibuf) - 1; p >= ibuf; p--) {
227                 *q++ = *p;
228                 *q++ = '.';
229         }
230         *q = '\0';
231         strcpy(ibuf, obuf);
232         strcpy(obuf, "remote-printer");
233
234         /* include userinfo if present */
235         if (userinfo != NULL && strlen(userinfo)) {
236                 strcat(obuf, ".");
237                 strcat(obuf, userinfo);
238         }
239         strcat(obuf, "@");
240         strcat(obuf, ibuf);             /* tack on reversed phone number */
241         strcat(obuf, TPCDOMAIN);        /* tack on domain name */
242         p = strdup(obuf);
243         return(p);
244                                 
245 }