1 # ===================================================================== 2 # RPX2rc.awk: convert selected RPX values into valid rc(1) assignments. 3 # 4 # Copyright (c) 2009 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 BEGIN { 22 RS = "<"; first = last = name = email = openid =_NULL; delete a 23 24 # Clear needed rc(1) vars first, just in case. 25 print "'cgi.rpx.fault'=();'cgi.rpx.uri'=()" 26 print "'cgi.rpx.uri.grep'=();'cgi.regform_url'=()" 27 } 28 29 # Note: this script already skips comments even without the next 30 # line but leaving the latter in place won't hurt anyway. 31 32 /^!-- [^>]*-->/ { next } # skip embedded comments. 33 34 { gsub(/[\t\r\n]+/,_NULL) } # no multiline input is expected. 35 36 # Detect RPX faults first. 37 /^err / { 38 sub(/[^'"]+['"]/,_NULL) 39 sub(/['"] +code *= *['"][0-9]+['"] *\/ *>.*/,_NULL) 40 print "'cgi.rpx.fault'=" _rcescape($0) 41 exit 42 } 43 44 # Extract the minimal subset of RPX data needed by TW. 45 46 # The single most important thing first. 47 /^identifier>/ { 48 sub(/[^>]+>/,_NULL) 49 sub(/ *$/,_NULL) 50 if (_isuri($0) == _TRUE) { 51 print "'cgi.rpx.uri'=" _rcescape($0) 52 openid = $0 53 # escape grep(1) regexp chars in URL. 54 gsub(/[][\\^$.*|()]/,"\\\&") 55 sub(/\/+$/,"/*") 56 print "'cgi.rpx.uri.grep'=" _rcescape($0) 57 } 58 } 59 60 # Try and get full-name from this field first. 61 /^display[Nn]ame>/ { 62 sub(/[^>]+>/,_NULL) 63 $0 = _strip($0,_O_MIDDLE) 64 sub(/ *$/,_NULL) 65 66 # It is quite unlikely that is already in 67 # the OpenID "Last, First" format, so I'll apply some 68 # (questionable) heuristics here. This is far from being 69 # bullet-proof (think of "Mr. Joseph Robert Smarr, Esq.") 70 # but, after all, the resulting stuff will end up in the 71 # registration form, where the user will be able to fix it 72 # to her likings. 73 74 if ($0 ~ /[a-zA-Z]/) { 75 76 # Revert input if it contains no commas, assuming 77 # it is in the most usual form of "First Last". 78 79 if ($0 !~ /[^,]+,[^,]+/) { 80 sub(/ /,",",$0) 81 if (split($0,a,",") > 1) 82 name = _strip(a[2],_O_MIDDLE) ", " _strip(a[1],_O_MIDDLE) 83 } 84 else { 85 if (split($0,a,",") > 1) 86 name = _strip(a[1],_O_MIDDLE) ", " _strip(a[2],_O_MIDDLE) 87 } 88 } 89 } 90 91 # This will override . 92 /^family[Nn]ame>/ { 93 sub(/[^>]+>/,_NULL) 94 sub(/ *$/,_NULL) 95 last = $0 96 } 97 98 # This will also override . 99 /^given[Nn]ame>/ { 100 sub(/[^>]+>/,_NULL) 101 sub(/ *$/,_NULL) 102 first = $0 103 } 104 105 /^email>/ { 106 sub(/[^>]+>/,_NULL) 107 sub(/ *$/,_NULL) 108 if (_isemail($0) != _EINVAL) email = $0 109 } 110 111 # This will override . 112 /^verified[Ee]mail>/ { 113 sub(/[^>]+>/,_NULL) 114 sub(/ *$/,_NULL) 115 if (_isemail($0) != _EINVAL) email = $0 116 } 117 118 END { 119 120 if (first != _NULL && last != _NULL) name = last ", " first 121 122 name = _uriencode(name) 123 email = _uriencode(email) 124 openid = _uriencode(openid) 125 126 print "'cgi.rpx.regform_url'=" _rcescape("sreg_email=" \ 127 email "&sreg_fullname=" name "&openid=" openid) 128 } 129 130 # ===================================================================== 131 # End of RPX2rc.awk 132 # =====================================================================