1 # ===================================================================== 2 # unwatch: remove a specified entry from the user's watch-list. 3 # 4 # Copyright (c) 2010 Carlo Strozzi 5 # 6 # This program is free software; you can redistribute it and/or modify 7 # it under the terms of the GNU General Public License as published by 8 # the Free Software Foundation; version 2 dated June, 1991. 9 # 10 # This program is distributed in the hope that it will be useful, 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # GNU General Public License for more details. 14 # 15 # You should have received a copy of the GNU General Public License 16 # along with this program; if not, write to the Free Software 17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 # 19 # ===================================================================== 20 21 # ===================================================================== 22 # Local variables and functions 23 # ===================================================================== 24 25 cgi.group = () 26 cgi.group.literal = () 27 cgi.auth.userid = () 28 cgi.item = () 29 cgi.item.node = () 30 cgi.item.type = page # default 31 cgi.page = () 32 33 tmp2 = /dev/null 34 35 # ===================================================================== 36 # Main program 37 # ===================================================================== 38 39 # ===================================================================== 40 # Note: this program does not require authentication. The only pieces 41 # of information that are required to perform the "unwatch" action are 42 # the target user-ID and the node-ID of the item to be un-watched. 43 # ===================================================================== 44 45 csaGetArgs GET POST 46 47 #~ $REMOTE_ADDR 192.168.1.2 && csaExit.env 48 49 . $CSA_ROOT/lib/group-stuff.rc 50 51 # If a target user-ID is not explicitly supplied try current auth. 52 53 ~ $'cgi.auth.userid' . && cgi.auth.userid = $CSA_AUTH_USER 54 55 # Set early template vars as appropriate. 56 tpl.var.tw.group = $'tbl_group.g_name' 57 tpl.var.tw.descr = $'tbl_group.g_descr' 58 tpl.var.tw.auth.userid = $'cgi.auth.userid' 59 60 #~ $REMOTE_ADDR 192.168.1.2 || csaExit.fault --back 0036 61 62 . $CSA_ROOT/lib/tpl-stuff.rc 63 64 ~ $'cgi.auth.userid' () && csaExit.fault 0038 65 ~ $'cgi.item.type' page thread || csaExit.fault 0038 66 ~ $'cgi.item.node' () && csaExit.fault 0038 67 68 # Page ID is needed both for a page and for a thread. 69 ~ $'cgi.page' () && csaExit.fault 0038 70 71 # Fetch target object's meta-data. Target can be either a page or a 72 # comment thread, nothing else. Note that un-watching a page does NOT 73 # automatically propagate to those comment threads belonging to that 74 # same page and which are also being watched. 75 76 if (~ $'cgi.item.type' page) { 77 keysearch $'cgi.page' $tw_gstem/page+dat | 78 csa-tbl2rc --prefix tbl_item. > $tmp1 79 . $tmp1 80 81 # Should not occur. 82 ~ $'tbl_item.p_name' () && csaExit.fault 0037 1 $CSA_PGM($#CSA_PGM) 83 84 # Set template vars to their final values. 85 tpl.var.tw.descr = $'tbl_item.p_descr' 86 87 } else { 88 89 # Else it can only be a comment thread. Note that the node-ID 90 # listed in auth+dat should always be the one of the top-level 91 # comment in a comment thread, or new posts will not be notified 92 # to the subscriber. It is the webmaster's responsibility to 93 # ensure that node-IDs in subscription templates are those of 94 # top-level comments. See 'rpclib/cmtPost' for more info. 95 96 grep -e '^'$'cgi.item'$tab $tw_gstem/$'cgi.page'+cmt | 97 csa-tbl2rc --prefix tbl_item. > $tmp1 98 99 . $tmp1 100 101 # Should not occur. 102 ~ $'tbl_item.c_subject' () && csaExit.fault 0037 2 $CSA_PGM($#CSA_PGM) 103 104 # Set template vars to their final values. 105 tpl.var.tw.descr = $'tbl_item.c_subject' 106 } 107 108 tpl.var.tw.node = $'cgi.item.node' 109 110 # Set PLS on 'auth+dat'. This must be done early, i.e. before 111 # confirmation, or races may occur. 112 csaLock $TNS_USER_TABLE || csaExit.fault 113 114 # Fetch the requested auth record. 115 csa-tbl2rc --input $TNS_USER_TABLE \ 116 --key $'cgi.auth.userid' --prefix tbl_auth. > $tmp1 || 117 csaExit.fault 0003 csa-tbl2rc 118 119 . $tmp1 120 121 # Since this program does not require authentication, let's throw 122 # only a generic exception if the target user-ID does not exist. 123 ~ $'tbl_auth.k_user' () && csaExit.fault 0038 124 125 # Account for deleted entries; this test will be repeated also by 126 # 'awktable' further down, just in case. 127 ~ $'tbl_auth.u_email' *'@'*.* || csaExit.fault 0038 128 129 # Display target user's gravatar. This must be in MD5 format so I 130 # cannot rely on CSA_CMD_MD, because this could be anything. 131 * = `{echo -n $'tbl_auth.u_email' | md5sum} 132 ~ $1 () && csaExit.fault 0003 md5sum # just in case. 133 tpl.var.tw.auth.email.md5 = $1 134 135 # Since this program requires no authentication it may be 136 # a privacy leakage to expose personal information on the 137 # request confirmation form, while a bit more stuff can 138 # safely be exposed in the confirmation e-mail message. 139 # It is up to the webmaster to decide what to include and 140 # in which templates. 141 142 makeFullName $'tbl_auth.u_name' $'tbl_auth.u_sname' 143 tpl.var.tw.auth.fullname = $CSA_RESULT 144 145 #~ $REMOTE_ADDR 192.168.1.2 && csaExit.env 146 147 if (!csaIsConfirmed) { 148 149 if (csaIsFullPath --exists --quiet \ 150 $CSA_TPL_ROOT/tw-unwatch-confirm.txt) { 151 # custom update confirmation template. 152 tpl.include.html.body = $CSA_TPL_ROOT/tw-unwatch-confirm.txt 153 } else { 154 # default update confirmation template. 155 tpl.include.html.body = $tw_dstem/tw-unwatch-confirm.txt 156 } 157 158 # PRG confirmation page must be regarded as a view. 159 tpl.if.tw.ispage = '(::DEL:)' 160 tpl.fi.tw.ispage = '(:DEL::)' 161 tpl.if.tw.printable = '(::DEL:)' 162 tpl.fi.tw.printable = '(:DEL::)' 163 tpl.if.tw.isview = () 164 tpl.fi.tw.isview = () 165 166 csaExit.ok $tpl_file 167 } 168 169 #~ $REMOTE_ADDR 192.168.1.2 && csaExit.env 170 171 # Since this program may be called with either an idempotent or a non- 172 # idempotent method, make sure actual changes occur only if non-idempotent. 173 csaIsIdempotent && csaExit.fault 0022 174 175 csaOpen --fast $TNS_USER_TABLE || csaExit.fault 176 tmp2 = $CSA_RESULT 177 178 # I do not check whether the target node-ID is actually listed in the 179 # I simply blindly remove the specified node-ID from the user record, 180 # whether it is actually listed in the watchlist or not. 181 182 awktable -H -i $TNS_USER_TABLE '-vu_='$'cgi.auth.userid' -- ' 183 184 # Test also the e-mail field to account for deleted entries. 185 $k_user == u_ && $u_email ~ /.@.+\..+/ { 186 187 # These are some of the test values that I have used to test the 188 # following code, and they were succesfully processed. 189 #$u_other = "|1940822300" 190 #$u_other = "m^1940822300|" 191 #$u_other = "m^1940822300,12345|" 192 #$u_other = "1940822300,12345|" 193 #$u_other = "1940822300,12345" 194 #$u_other = "|m^1940822300,12345,6789|xxxx" 195 #$u_other = "" 196 #$u_other = ":" 197 198 tmp_ = $u_other; sub(/\|.*/,"",tmp_) 199 if ($u_other !~ /\|/) $u_other = "" 200 else { 201 sub(/[^|]*\|/,"|",$u_other) 202 sub(/\|+$/,"",$u_other) 203 #sub(/,+.\^$/,"",$u_other) 204 } 205 206 # Account for the many formats the watch-list can take. 207 tmp_ = "," tmp_ "," 208 gsub(/,'$'cgi.item.node',/',",",tmp_) 209 gsub(/\^'$'cgi.item.node',+/',"^",tmp_) 210 sub(/,+.\^,*$/,"",tmp_) 211 gsub(/,+/,",",tmp_); sub(/^,+$/,"",tmp_) 212 sub(/^,+/,"",tmp_); sub(/,+$/,"",tmp_) 213 $u_other = tmp_ $u_other 214 } 215 {print}' > $tmp2 || csaExit.fault 0003 awktable 216 217 #csaMkTemp debug; prtable -l300 < $tmp2 > $debug; csaExit.pcdata $debug 218 219 csaLoadLib csaEmaillib.rc || csaExit.fault # before csaCommit . 220 221 # Better to explicitly make the changes before the notification is sent. 222 csaCommit || csaExit.fault 223 224 # Notify user about the change, as we did not request authentication. 225 226 # If no custom email template exists then use default. 227 if (!csaIsFullPath --exists --quiet \ 228 $CSA_TPL_ROOT/tw-unwatch-email.txt) { 229 tpl_email = (--file-root $tw_dstem tw-unwatch-email.txt) 230 } else tpl_email = tw-unwatch-email.txt 231 232 TNS_EMAIL_TO = $'tbl_auth.u_email' \ 233 tpl.var.tw.auth.userid = $'cgi.auth.userid' { csaSendMail $tpl_email } 234 235 csaExit.ok 236 237 #EOF