1# $Id: addrsave.tcl 1204 2009-02-02 19:54:23Z hubert@u.washington.edu $
2# ========================================================================
3# Copyright 2006 University of Washington
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# ========================================================================
12
13#  addrsave.tcl
14#
15#  Purpose:  CGI script to handle address book change/save
16#	     via addredit generated form
17#
18#  Input:
19set abs_vars {
20  {cid		"Missing Command ID"}
21  {save		{}	0}
22  {delete	{}	0}
23  {replace	{}	0}
24  {compose	{}	0}
25  {take		{}	0}
26  {help		{}	0}
27  {cancel	{}	0}
28  {oncancel	{}	""}
29}
30
31#  Output:
32#
33
34## read vars
35foreach item $abs_vars {
36  if {[catch {cgi_import [lindex $item 0].x}]} {
37    if {[catch {eval WPImport $item} result]} {
38      error [list _action "Impart Variable" $result]
39    }
40  } else {
41    set [lindex $item 0] 1
42  }
43}
44
45if {$cid != [WPCmd PEInfo key]} {
46  catch {WPCmd PEInfo statmsg "Invalid Command ID"}
47}
48
49if {$help != 0} {
50  set save_vars {
51    {book {} 0}
52    {newnick {} ""}
53    {nick {} ""}
54    {ai {} 0}
55    {fn {} ""}
56    {addrs {} ""}
57    {fcc {} ""}
58    {comment {} ""}
59    {add {} 0}
60    {take {} 0}
61  }
62
63  foreach i $abs_vars {
64    lappend ae_help_state [list [lindex $i 0] [subst $[lindex $i 0]]]
65  }
66
67  foreach i $save_vars {
68    eval WPImport $i
69    lappend ae_help_state [list [lindex $i 0] [subst $[lindex $i 0]]]
70  }
71
72  WPCmd PEInfo set ae_help_state $ae_help_state
73
74  set _cgi_uservar(topic) takeedit
75  set _cgi_uservar(oncancel) addredit
76  if {$take} {
77    set _cgi_uservar(index) none
78  }
79
80  set src help
81
82} elseif {$save == 1 || [string compare [string tolower $save] "save entry"] == 0} {
83  WPLoadCGIVar book
84  WPLoadCGIVar newnick
85  WPLoadCGIVar ai
86  WPLoadCGIVar fn
87  WPLoadCGIVar addrs
88  WPLoadCGIVar fcc
89  WPLoadCGIVar comment
90  if {[catch {cgi_import_cookie add}] != 0} {set add 0}
91  if {[catch {cgi_import_cookie nick}] != 0} {set nick ""}
92
93  if {![string length $newnick]} {
94    WPCmd PEInfo statmsg "No entry created. New entries must include a Nickname."
95    set src takeedit
96  } else {
97    if {!$take || [catch {WPCmd PEAddress entry $book $newnick $ai} result] || ![llength $result]} {
98      set result ""
99      if {[catch {WPCmd PEAddress edit $book $newnick $ai $fn $addrs $fcc $comment $add $nick} result]} {
100	catch {WPCmd PEInfo statmsg "Address Set Failure: $result"}
101	set adderr $result
102      } elseif {[string length $result]} {
103	catch {WPCmd PEInfo statmsg "$result"}
104	set adderr $result
105      }
106
107      if {$take == 1} {
108	if {[info exists adderr]} {
109	  WPCmd PEInfo statmsg "No address book entry changed: $adderr"
110	} else {
111	  WPCmd PEInfo statmsg "New address book entry \"$newnick\" created."
112	}
113      }
114    } else {
115      set src takesame
116    }
117
118    if {$take == 1 && ![info exists src]} {
119      set src $oncancel
120    }
121  }
122} elseif {$delete == 1 || [string compare [string tolower $delete] "delete entry"] == 0} {
123  WPLoadCGIVar book
124  WPLoadCGIVar nick
125  WPLoadCGIVar ai
126
127  set result ""
128  if {[catch {WPCmd PEAddress delete $book $nick $ai} result]} {
129    catch {WPCmd PEInfo statmsg "Address Set Failure $result"}
130  } elseif {[string compare $result ""]} {
131    catch {WPCmd PEInfo statmsg "$result"}
132  }
133} elseif {[string compare [string tolower $replace] "replace entry"] == 0} {
134
135  WPLoadCGIVar book
136  WPLoadCGIVar newnick
137  WPLoadCGIVar ai
138  WPLoadCGIVar fn
139  WPLoadCGIVar addrs
140  WPLoadCGIVar fcc
141  WPLoadCGIVar comment
142  if {[catch {cgi_import_cookie add}] != 0} {set add 0}
143  if {[catch {cgi_import_cookie nick}] != 0} {set nick ""}
144
145  if {![string length $newnick]} {
146    WPCmd PEInfo statmsg "No entry created. New entries must include a Nickname."
147    set src takeedit
148  } else {
149    if {[catch {WPCmd PEAddress delete $book $newnick $ai} result]} {
150      set adderr $result
151    } else {
152      set result ""
153      if {[catch {WPCmd PEAddress edit $book $newnick $ai $fn $addrs $fcc $comment $add $nick} result]} {
154	catch {WPCmd PEInfo statmsg "Address Set Failure result"}
155	set adderr $result
156      } elseif {[string length $result]} {
157	catch {WPCmd PEInfo statmsg "$result"}
158	set adderr $result
159      }
160    }
161
162    if {$take == 1} {
163      if {[info exists adderr]} {
164	WPCmd PEInfo statmsg "No address book entry changed: $adderr."
165      } else {
166	WPCmd PEInfo statmsg "Address book entry \"$newnick\" replaced."
167      }
168
169      if {![info exists src]} {
170	set src $oncancel
171      }
172    }
173  }
174} elseif {[string compare [string tolower $replace] "add to entry"] == 0} {
175  WPLoadCGIVar book
176  WPLoadCGIVar newnick
177  WPLoadCGIVar ai
178  WPLoadCGIVar fn
179  WPLoadCGIVar addrs
180  WPLoadCGIVar fcc
181  WPLoadCGIVar comment
182  if {[catch {cgi_import_cookie add}] != 0} {set add 0}
183  if {[catch {cgi_import_cookie nick}] != 0} {set nick ""}
184
185  if {![string length $newnick]} {
186    WPCmd PEInfo statmsg "No entry created. New entries must include a Nickname."
187    set src takeedit
188  } else {
189    if {[catch {WPCmd PEAddress fullentry $book $newnick $ai} result] || ![llength $result]} {
190      if {[string length $result]} {
191	set adderr $result
192      } else {
193	set adderr "No pre-existing entry"
194      }
195    } else {
196      set fn [lindex $result 1]
197      set newaddrs [join [lindex $result 2] ","]
198      append newaddrs ", $addrs"
199      set fcc [lindex $result 3]
200      set comment [lindex $result 4]
201      set result ""
202      if {[catch {WPCmd PEAddress edit $book $newnick $ai $fn $newaddrs $fcc $comment 0 $newnick} result]} {
203	set adderr $result
204      } elseif {[string length $result]} {
205	catch {WPCmd PEInfo statmsg "$result"}
206	set adderr $result
207      }
208    }
209
210    if {$take == 1} {
211      if {[info exists adderr]} {
212	WPCmd PEInfo statmsg "No address book entry created: $adderr."
213      } else {
214	WPCmd PEInfo statmsg "Address book entry \"$newnick\" appended"
215      }
216
217      if {![info exists src]} {
218	set src $oncancel
219      }
220    }
221  }
222} elseif {[string compare [string tolower $replace] edit] == 0} {
223  set src takeedit
224} elseif {$compose == 1} {
225  set oncancel addrbook
226  set src compose
227} elseif {$cancel == 1 || [string compare [string tolower $cancel] cancel] == 0} {
228  if {$take == 1} {
229    set act "Take Address"
230  } else {
231    set act "Address Edit"
232  }
233
234  catch {WPCmd PEInfo statmsg "$act cancelled.  Address book unchanged."}
235  set src $oncancel
236} else {
237  catch {WPCmd PEInfo statmsg "Unknown Address Book Operation"}
238}
239
240if {![info exists src]} {
241  set src addrbook
242}
243
244source [WPTFScript $src]
245