1 # ===================================================================== 2 # watch: add a specified entry to 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.item = () 28 cgi.item.type = page # default 29 cgi.page = () 30 31 tmp2 = /dev/null 32 33 # ===================================================================== 34 # Main program 35 # ===================================================================== 36 37 csaGetArgs GET POST 38 39 #~ $REMOTE_ADDR 2001:470:1f0b:13a5:1::2 && csaExit.env 40 41 . $CSA_ROOT/lib/group-stuff.rc 42 43 # Set early template vars as appropriate. 44 tpl.var.tw.group = $'tbl_group.g_name' 45 tpl.var.tw.descr = $'tbl_group.g_descr' 46 47 #~ $REMOTE_ADDR 192.168.1.2 || csaExit.fault --back 0036 48 49 . $CSA_ROOT/lib/tpl-stuff.rc 50 51 ~ $'cgi.item.type' page thread || csaExit.fault 0038 52 53 # Page ID is needed both for a page and for a thread. 54 ~ $'cgi.page' () && csaExit.fault 0038 55 56 # Make sure subscription requests come from registerd user, i.e. one 57 # whose e-mail we have on file. 58 59 if (!csaTrue $CSA_AUTH_OK) { 60 61 # Show self-registration form if appropriate. 62 csaIsInteractive && ~ $TNS_SELFREG_AUTH *[a-z]* && 63 csaTrue $TNS_SELFREG_AUTOASK && 64 csaExit.location $CSA_RPC_URI'?0=showStatic&x-csa-lang='$CSA_LANG^'&1='$'cgi.group'^'&2=tw-reg-form' 65 66 csaExit.needauth 67 } 68 69 # Fetch target object's meta-data. Target can be either a page or a 70 # comment thread, nothing else. 71 72 if (~ $'cgi.item.type' page) { 73 keysearch $'cgi.page' $tw_gstem/page+dat | 74 csa-tbl2rc --prefix tbl_item. > $tmp1 75 . $tmp1 76 77 # Should not occur. 78 ~ $'tbl_item.p_name' () && csaExit.fault 0037 1 $CSA_PGM($#CSA_PGM) 79 80 # Set template vars to their final values. 81 tpl.var.tw.descr = $'tbl_item.p_descr' 82 83 } else { 84 85 # Else it can only be a comment thread. 86 87 grep -e '^'$'cgi.item'$tab $tw_gstem/$'cgi.page'+cmt | 88 csa-tbl2rc --prefix tbl_item. > $tmp1 89 90 . $tmp1 91 92 # Should not occur. 93 ~ $'tbl_item.c_subject' () && csaExit.fault 0037 2 $CSA_PGM($#CSA_PGM) 94 95 # Now I need to "climb" backward in the comment table to get the 96 # node-ID of the top-level comment in the requested thread. 97 98 if (!~ $'tbl_item.c_parent' $'tbl_item.k_cmt') { 99 100 # If no thread node is found (which should not occur unless 101 # there are inconsistencies in the data), next line will 102 # return an empty string, causing the subsequent test to fail 103 # and the notification process to be skipped. 104 105 tbl_item.k_node = `{awktable -i $tw_pstem+cmt -- \ 106 '$k_cmt == '"$'tbl_cmt.c_parent'"' {print $k_node; exit}'} 107 } 108 109 # Should not occur. 110 ~ $'tbl_item.k_node' && csaExit.fault 0037 2 $CSA_PGM($#CSA_PGM) 111 112 # Set template vars to their final values. 113 tpl.var.tw.descr = $'tbl_item.c_subject' 114 } 115 116 tpl.var.tw.node = $'tbl_item.k_node' 117 118 # Set PLS on 'auth+dat'. This must be done early, i.e. before 119 # confirmation, or races may occur. 120 csaLock $TNS_USER_TABLE || csaExit.fault 121 122 # Fetch the requested auth record. 123 csa-tbl2rc --input $TNS_USER_TABLE \ 124 --key $CSA_AUTH_USER --prefix tbl_auth. > $tmp1 || 125 csaExit.fault 0003 csa-tbl2rc 126 127 . $tmp1 128 129 # Should not occur. 130 ~ $'tbl_auth.k_user' && csaExit.fault 0037 3 $CSA_PGM($#CSA_PGM) 131 132 # Account for deleted entries; this test will be repeated also by 133 # 'awktable' further down, just in case. 134 135 # Should not occur. 136 ~ $'tbl_auth.u_email' *'@'*.* || csaExit.fault 0037 4 $CSA_PGM($#CSA_PGM) 137 138 # Display target user's gravatar. This must be in MD5 format so I 139 # cannot rely on CSA_CMD_MD, because this could be anything. 140 * = `{echo -n $'tbl_auth.u_email' | md5sum} 141 ~ $1 () && csaExit.fault 0003 md5sum # just in case. 142 tpl.var.tw.auth.email.md5 = $1 143 144 makeFullName $'tbl_auth.u_name' $'tbl_auth.u_sname' 145 tpl.var.tw.auth.fullname = $CSA_RESULT 146 147 #~ $REMOTE_ADDR 192.168.1.2 && csaExit.env 148 149 if (!csaIsConfirmed) { 150 151 if (csaIsFullPath --exists --quiet \ 152 $CSA_TPL_ROOT/tw-watch-confirm.txt) { 153 # custom update confirmation template. 154 tpl.include.html.body = $CSA_TPL_ROOT/tw-watch-confirm.txt 155 } else { 156 # default update confirmation template. 157 tpl.include.html.body = $tw_dstem/tw-watch-confirm.txt 158 } 159 160 # PRG confirmation page must be regarded as a view. 161 tpl.if.tw.ispage = '(::DEL:)' 162 tpl.fi.tw.ispage = '(:DEL::)' 163 tpl.if.tw.printable = '(::DEL:)' 164 tpl.fi.tw.printable = '(:DEL::)' 165 tpl.if.tw.isview = () 166 tpl.fi.tw.isview = () 167 168 csaExit.ok $tpl_file 169 } 170 171 #~ $REMOTE_ADDR 2001:470:1f0b:13a5:1::2 && csaExit.env 172 173 # Since this program may be called with either an idempotent or a non- 174 # idempotent method, make sure actual changes occur only if non-idempotent. 175 csaIsIdempotent && csaExit.fault 0022 176 177 #~ $REMOTE_ADDR 2001:470:1f0b:13a5:1::2 && csaExit.env 178 179 csaOpen --fast $TNS_USER_TABLE || csaExit.fault 180 tmp2 = $CSA_RESULT 181 182 # To avoid redundancies the target node is first removed (whether 183 # it was already there or not) and then appended to the notify list. 184 185 awktable -H -i $TNS_USER_TABLE '-vu_='$CSA_AUTH_USER -- ' 186 187 # Test also the e-mail field to account for deleted entries. 188 $k_user == u_ && $u_email ~ /.@.+\..+/ { 189 190 tmp_ = $u_other; sub(/\|.*/,"",tmp_) 191 if ($u_other !~ /\|/) $u_other = "" 192 else { 193 sub(/[^|]*\|/,"|",$u_other) 194 sub(/\|+$/,"",$u_other) 195 #sub(/,+.\^$/,"",$u_other) 196 } 197 198 # Account for the many formats the watch-list can take. 199 tmp_ = "," tmp_ "," 200 gsub(/,'$'tbl_item.k_node',/',",",tmp_) 201 gsub(/\^'$'tbl_item.k_node',+/',"^",tmp_) 202 sub(/,+.\^,*$/,"",tmp_) 203 gsub(/,+/,",",tmp_); sub(/^,+$/,"",tmp_) 204 sub(/^,+/,"",tmp_); sub(/,+$/,"",tmp_) 205 $u_other = tmp_ ",'$'tbl_item.k_node','" $u_other 206 } 207 {print}' > $tmp2 || csaExit.fault 0003 awktable 208 209 #csaMkTemp debug; prtable -l300 < $tmp2 > $debug; csaExit.pcdata $debug 210 211 # Take the client back to the target object. 212 213 if (~ $'cgi.item.type' page) { 214 csaExit.ok --confirmed \ 215 $CSA_RPC_URI/$CSA_LANG/$'tbl_group.g_uri'/$'tbl_item.p_uri' 216 } else { 217 csaExit.ok --confirmed \ 218 $CSA_RPC_URI/$CSA_LANG/$'tbl_group.g_uri'/$'cgi.page'/comment/$'tbl_item.k_cmt' 219 } 220 221 #EOF