]> git.sur5r.net Git - openldap/blob - clients/fax500/faxtotpc.c
Provide a little information about SDF, how to use it, and where to get it.h
[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
25 #include <ac/ctype.h>
26 #include <ac/string.h>
27 #include <ac/unistd.h>
28
29 #include "fax500.h"
30
31 #define TPCDOMAIN       "tpc.int"
32
33 /*
34  * Remove everything from 'str' which is not a digit
35  */
36 void
37 strip_nonnum( char *str )
38 {
39         char *p, *q;
40         p = q = str;
41         for (;;) {
42                 if (*p == '\0') {
43                         *q = *p;
44                         return;
45                 }
46
47                 if (isdigit((u_char) *p)) {
48                         *q = *p;
49                         p++;
50                         q++;
51                 } else {
52                         p++;
53                 }
54         }
55 }
56
57
58
59 /* 
60  * Remove anything of the form (blah) where
61  * "blah" contains a non-numeric character.
62  */
63 char *
64 remove_parens( char *ibuf, char *obuf )
65 {
66         char *p = ibuf;
67         char *q = obuf;
68
69         while (*p != '\0') {
70                 char *s;
71                 char *t;
72                 if (*p == '(') {
73                         /* look for a closing paren */
74                         if (( s = strchr(p, ')')) != NULL) {
75                                 /* Check the string between p and s */
76                                 /* for non-numeric characters       */
77                                 t = p + 1;
78                                 while (t < s) {
79                                         if (!isdigit((u_char) *t)) {
80                                                 /* garbage, delete */
81                                                 p = s + 1;
82                                                 t = p;
83                                                 break;
84                                         }
85                                         t++;
86                                 }
87                                 /* when we get here, p points to the first */
88                                 /* thing we want to keep, t to the last.   */
89                                 strncpy(q, p,  t - p);
90                                 q += t - p;
91                                 p = t;
92                         } else {
93                                 /* no closing paren, what to do?  keep it */
94                                 *q = *p;
95                                 p++;
96                                 q++;
97                         }
98                 } else {
99                         /* not a paren - copy out */
100                         *q = *p;
101                         p++;
102                         q++;
103                 }
104         }
105         *q = '\0';      /* terminate output string */
106         return(obuf);
107 }
108
109
110
111
112 /*
113  * Apply local fixups to phone numbers here.  Replace this routine
114  * with code to expand common "abbreviations" for phone numbers.  For
115  * example, on the U-M campus, it's only necessary to dial the last
116  * 5 digits of the telephone number, and hence people here tend to
117  * give only the last 5 digits of their fax numbers.
118  *
119  * Local U-M mods:
120  * If exactly 5 digits were provided, assume it's a campus
121  * phone number and prepend "1313nm" where "mn" are computed
122  * according to the following:
123  * first digit of 
124  * 5-digit "Local" 
125  * phone              mn
126  * -----              --
127  * 3                  76 e.g. "31234" -> "7631234"
128  * 4                  76
129  * 7                  74
130  * 6                  93
131  * 8                  99
132  */
133 char *
134 munge_phone( char *ibuf, 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  * Convert string to "tpc.int" domain name.
178  */
179 char *
180 faxtotpc( char *phone, char *userinfo )
181 {
182         char *p;
183         char *q;
184         char ibuf[255];
185         char obuf[255];
186
187         /* nuke spaces */
188         strcpy(ibuf, phone);
189         for (p = ibuf, q = obuf; *p != '\0'; p++) {
190                 if (*p != ' ') {
191                         *q = *p;
192                         q++;
193                 }
194         }
195         *q = '\0';
196         strcpy(ibuf, obuf);
197
198         remove_parens(ibuf, obuf);
199         strcpy(ibuf, obuf);
200
201         /* Look for "+" - if followed by a number,
202            assume it's an international number and leave
203            it alone.
204         */
205         if ((p = strchr(ibuf, '+')) != NULL) {
206                 if (isdigit((u_char) *(p + 1))) {
207                         /* strip any non-digits */
208                         strip_nonnum(ibuf);
209                 }
210         } else {
211                 strip_nonnum(ibuf);
212
213                 /* Apply local munges */
214                 munge_phone(ibuf, obuf);
215                 strcpy(ibuf, obuf);
216         }
217
218         /* Convert string of form abcd to remote-printer@d.c.b.a.tpc.int */
219         q = obuf;
220         for (p = ibuf + strlen(ibuf) - 1; p >= ibuf; p--) {
221                 *q++ = *p;
222                 *q++ = '.';
223         }
224         *q = '\0';
225         strcpy(ibuf, obuf);
226         strcpy(obuf, "remote-printer");
227
228         /* include userinfo if present */
229         if (userinfo != NULL && strlen(userinfo)) {
230                 strcat(obuf, ".");
231                 strcat(obuf, userinfo);
232         }
233         strcat(obuf, "@");
234         strcat(obuf, ibuf);             /* tack on reversed phone number */
235         strcat(obuf, TPCDOMAIN);        /* tack on domain name */
236         p = strdup(obuf);
237         return(p);
238 }