dthelp: Change to ANSI function definitions
[oweals/cde.git] / cde / programs / dtmail / libDtMail / RFC / AliasExpand.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*
24  *+SNOTICE
25  *
26  *
27  *      $XConsortium: AliasExpand.C /main/4 1996/04/21 19:49:19 drk $
28  *
29  *      RESTRICTED CONFIDENTIAL INFORMATION:
30  *      
31  *      The information in this document is subject to special
32  *      restrictions in a confidential disclosure agreement bertween
33  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
34  *      document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
35  *      Sun's specific written approval.  This documment and all copies
36  *      and derivative works thereof must be returned or destroyed at
37  *      Sun's request.
38  *
39  *      Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
40  *
41  *+ENOTICE
42  */
43
44 #include <assert.h>
45
46 #include "AliasExpand.hh"
47 #include "RFCImpl.hh"
48
49 AliasKey::AliasKey(const char * str)
50 : ObjectKey("AliasKey")
51 {
52     _str = strdup(str);
53 }
54
55 AliasKey::~AliasKey(void)
56 {
57     free(_str);
58 }
59
60 int
61 AliasKey::operator==(ObjectKey & other)
62 {
63     AliasKey * ok = (AliasKey *)&other;
64
65     return(strcmp(_str, ok->_str) == 0);
66 }
67
68 int
69 AliasKey::operator!=(ObjectKey & other)
70 {
71     AliasKey * ok = (AliasKey *)&other;
72
73     return(strcmp(_str, ok->_str) != 0);
74 }
75
76 int
77 AliasKey::operator<(ObjectKey & other)
78 {
79     AliasKey * ok = (AliasKey *)&other;
80
81     return(strcmp(_str, ok->_str) < 0);
82 }
83
84 int
85 AliasKey::operator<=(ObjectKey & other)
86 {
87     AliasKey * ok = (AliasKey *)&other;
88
89     return(strcmp(_str, ok->_str) <= 0);
90 }
91
92 int
93 AliasKey::operator>(ObjectKey & other)
94 {
95     AliasKey * ok = (AliasKey *)&other;
96
97     return(strcmp(_str, ok->_str) > 0);
98 }
99
100 int
101 AliasKey::operator>=(ObjectKey & other)
102 {
103     AliasKey * ok = (AliasKey *)&other;
104
105     return(strcmp(_str, ok->_str) >= 0);
106 }
107
108 HashVal
109 AliasKey::hashValue(void)
110 {
111     return(genericHashValue(_str, strlen(_str)));
112 }
113
114 // deleteAllocatedKey: used to iterate through a hash table to remove
115 // all key values that are allocated during the duration of rfcAliasExpand
116 //
117 static int
118 deleteAllocatedKey(ObjectKey & object, AliasKey *, void *) {
119   assert(&object != NULL);
120   delete &object;
121   return(1);
122 }
123
124                    
125 void
126 rfcAliasExpand(DtMailEnv & error,
127                DtMail::MailRc & mailrc,
128                DtMailAddressSeq & addrs)
129 {
130   HashTable<DtMailBoolean>      been_there(32);
131   
132   error.clear();
133   
134   // We will go through each item in the address table.
135   // For each address, we will look it up in the alias
136   // table. If it exists there, we will check to see if
137   // we have expanded before. If not, then we expand and
138   // note our visit.
139   //
140   DtMailEnv lerror;
141   
142   for (int naddr = 0; naddr < addrs.length(); naddr++) {
143     DtMailValueAddress * caddr = addrs[naddr];
144     
145     const char * alias = mailrc.getAlias(lerror, caddr->dtm_address);
146     if (alias) {
147       AliasKey * key = new AliasKey(caddr->dtm_address);
148       if (been_there.lookup(*key)) {    // already visited address?
149         delete key;                     // yes: cleanup unneeded key
150       }
151       else {                            // no: expand the alias
152         DtMailAddressSeq exp_addrs(32);
153         RFCTransport::arpaPhrase(alias, exp_addrs);
154         
155         for (int cp = 0; cp < exp_addrs.length(); cp++) {
156           DtMailValueAddress * new_addr = new DtMailValueAddress(*exp_addrs[cp]);
157           addrs.append(new_addr);
158         }
159         
160         been_there.set(*key, DTM_TRUE);
161       }
162       
163       addrs.remove(naddr);              // no longer need this address
164       naddr -= 1;                       // ...
165       delete caddr;                     // or what it pointed to.
166     }
167   }
168   
169   // Finally, as a courtesy to the recipients, we will throw away
170   // duplicates that have resulted from our expansion.
171   //
172   HashTable<DtMailBoolean>      done_that(32);
173   for (int cur = 0; cur < addrs.length(); cur++) {
174     DtMailValueAddress * caddr = addrs[cur];
175     AliasKey * dup_key = new AliasKey(caddr->dtm_address);
176     
177     if (done_that.lookup(*dup_key)) {
178       addrs.remove(cur);
179       delete caddr;
180       delete dup_key;
181       cur -= 1;
182     }
183     else {
184       done_that.set(*dup_key, DTM_TRUE);
185     }
186   }
187 }