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