1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
|
#!/usr/bin/env tclsh
##
# impersonal.tcl - export impersonal mail via the web
#
# (c) 1999 Marshall T. Rose
# Hold harmless the author, and any lawful use is allowed.
#
package require Tcl 8.3
global options
# begin of routines that may be redefined in configFile
proc tclLog {message} {
global options
if {([info exists options(debugP)]) && ($options(debugP) > 0)} {
puts stderr $message
}
if {([string first "DEBUG " $message] == 0) \
|| ([catch { set fd [open $options(logFile) \
{ WRONLY CREAT APPEND }] }])} {
return
}
regsub -all "\n" $message " " message
catch { puts -nonewline $fd \
[format "%s %-8.8s %06d %s\n" \
[clock format [clock seconds] -format "%m/%d %T"] \
personal [expr {[pid]%65535}] $message] }
catch { close $fd }
}
# end of routines that may be redefined in configFile
proc firstext {mime} {
array set props [mime::getproperty $mime]
if {[info exists props(parts)]} {
foreach part $props(parts) {
if {[string compare [firstext $part] ""]} {
return $part
}
}
} else {
switch -- $props(content) {
text/plain
-
text/html {
return $mime
}
}
}
}
proc sanitize {text} {
regsub -all "&" $text {\&} text
regsub -all "<" $text {\<} text
return $text
}
proc cleanup {{message ""} {code 500}} {
global errorCode errorInfo
set ecode $errorCode
set einfo $errorInfo
if {[string compare $message ""]} {
tclLog $message
catch {
puts stdout "HTTP/1.0 $code Server Error
Content-Type: text/html
Status: 500 Server Error
<html><head><title>Service Problem</title></head>
<body><h1>Service Problem</h1>
<b>Reason:</b> [sanitize $message]"
if {$code == 505} {
puts stdout "<br>
<b>Stack:</b>
<pre>[sanitize $einfo]</pre>
<hr></hr>"
}
puts stdout "</body></html>"
}
}
flush stdout
exit 0
}
if {[catch {
set program impersonal
package require mbox 1.0
package require mutl 1.0
package require smtp 1.1
package require Tclx 8.0
# move stdin, close stdin/stderr
dup [set null [open /dev/null { RDWR }]] stderr
set stdin [dup stdin]
dup $null stdin
close $null
fconfigure $stdin -translation crlf
fconfigure stdout -translation crlf
# parse arguments and initialize environment
set program [file tail [file rootname $argv0]]
set configFile .${program}-config.tcl
set debugP 0
set userName ""
for {set argx 0} {$argx < $argc} {incr argx} {
set option [lindex $argv $argx]
if {[incr argx] >= $argc} {
cleanup "missing argument to $option"
}
set value [lindex $argv $argx]
switch -- $option {
-config {
set configFile $value
}
-debug {
set options(debugP) [set debugP [smtp::boolean $value]]
}
-user {
set userName $value
}
default {
cleanup "unknown option $option"
}
}
}
if {[string compare $userName ""]} {
if {[catch { id convert user $userName }]} {
cleanup "userName doesn't exist: $userName"
}
if {([catch { file isdirectory ~$userName } result]) \
|| (!$result)} {
cleanup "userName doesn't have a home directory: $userName"
}
umask 0077
cd ~$userName
}
if {![file exists $configFile]} {
cleanup "configFile file doesn't exist: $configFile"
}
source $configFile
set options(debugP) $debugP
foreach {k v} [array get options] {
if {![string compare $v ""]} {
unset options($k)
}
}
foreach k [list dataDirectory foldersFile foldersDirectory] {
if {![info exists options($k)]} {
cleanup "configFile didn't define $k: $configFile"
}
}
if {![file isdirectory $options(dataDirectory)]} {
file mkdir $options(dataDirectory)
}
# crack the request
set request ""
set eol ""
while {1} {
if {[catch { gets $stdin line } result]} {
cleanup "lost connection"
}
if {$result < 0} {
break
}
set gotP 0
foreach c [split $line ""] {
if {($c == " ") || ($c == "\t") || [ctype print $c]} {
if {!$gotP} {
append request $eol
set gotP 1
}
append request $c
}
}
if {!$gotP} {
break
}
set eol "\n"
}
set request [string tolower $request]
set getP 0
foreach param [split $request "\n"] {
if {[string first "get " $param] == 0} {
set getP 1
if {[catch { lindex [split $param " "] 1 } page]} {
cleanup "server supports only HTTP/1.0" 501
}
}
}
if {!$getP} {
cleanup "server supports only GET" 405
}
if {[string first /news? $page] != 0} {
cleanup "page $page unavailable" 504
}
foreach param [split [string range $page 6 end] &] {
if {[set x [string first = $param]] <= 0} {
cleanup "page $request unavailable" 504
}
set key [string range $param 0 [expr {$x-1}]]
set arg($key) [string range $param [expr {$x+1}] end]
}
set expires [mime::parsedatetime -now proper]
# /news?index=newsgroups OR /news?index=recent
if {![catch { set arg(index) } index]} {
switch -- $index {
newsgroups {
set lastN 0
}
recent {
set lastN -1
}
default {
cleanup "page $request unavailable" 504
}
}
catch { set lastN $arg(lastn) }
if {[catch { open $options(foldersFile) { RDONLY } } fd]} {
cleanup $fd 505
}
set folders ""
set suffix [lindex [set prefix [file split \
$options(foldersDirectory)]] \
end]
set prefix [eval [list file join] [lreplace $prefix end end]]
for {set lineNo 1} {[gets $fd line] >= 0} {incr lineNo} {
if {[string first $suffix $line] != 0} {
continue
}
set file [file join $prefix $line]
if {[catch { file stat $file stat } result]} {
tclLog $result
continue
}
if {![string compare $stat(type) file]} {
lappend folders [list [eval [list file join] \
[lrange [file split $line] \
1 end]] \
$stat(mtime)]
}
}
catch {close $fd }
switch -- $index {
recent {
set folders [lsort -integer -decreasing -index 1 $folders]
}
default {
set folders [lsort -dictionary -increasing -index 0 $folders]
}
}
puts stdout "HTTP/1.0 200
Content-Type: text/html
Pragma: no-cache
Expires: $expires
<html><head><title>newsgroups</title></head><body>
<table cellborder=0 cellpadding=0 cellspacing=0>"
foreach entry $folders {
set folder [lindex $entry 0]
set t [fmtclock [set mtime [lindex $entry 1]] "%m/%d %H:%M"]
puts stdout "<tr><td><a href=\"news?folder=$folder&lastN=$lastN&mtime=$mtime\">$t</a></td><td width=5></td><td><b>$folder</b></td></tr>"
}
puts stdout "</table>
</body></html>"
cleanup
}
# /news?folder="whatever"
if {[catch { set arg(folder) } folder]} {
cleanup "page $request unavailable" 504
}
foreach p [file split $folder] {
if {(![string compare $p ""]) || ([string first . $p] >= 0)} {
cleanup "page $request unavailable" 504
}
}
set file [file join $options(foldersDirectory) $folder]
if {([catch { file type $file } type]) \
|| ([string compare $type file])} {
cleanup "page $request unavailable" 504
}
if {[catch { mbox::initialize -file $file } mbox]} {
cleanup $mbox 505
}
# /news?folder="whatever"&lastN="N"
if {![catch { set arg(lastn) } lastN]} {
array set props [mbox::getproperty $mbox]
if {$lastN < 0} {
set diff [expr {-($lastN*86400)}]
set last 0
for {set msgNo $props(last)} {$msgNo > 0} {incr msgNo -1} {
if {[catch { mbox::getmsgtoken $mbox $msgNo } mime]} {
tclLog $mime
continue
}
if {[catch { lindex [mime::getheader $mime Date] 0 } value]} {
set value ""
}
if {![catch { mime::parsedatetime $value rclock } rclock]} {
if {$rclock < $diff} {
if {$last == 0} {
set last $msgNo
}
set first $msgNo
}
if {$last == 0} {
break
}
}
}
if {$last > 0} {
set last $props(last)
}
} elseif {[set first \
[expr {[set last $props(last)]-($lastN+1)}]] <= 0} {
set first 1
}
puts stdout "HTTP/1.0 200
Content-Type: text/html
Pragma: no-cache
Expires: $expires
<html><head><title>$folder</title></head><body>"
if {$last == 0} {
puts stdout "<b>Empty.</b>
</body></html>"
cleanup
}
puts stdout "<table cellborder=0 cellpadding=0 cellspacing=0>"
for {set msgNo $last} {$msgNo >= $first} {incr msgNo -1} {
if {[catch { mbox::getmsgtoken $mbox $msgNo } mime]} {
tclLog $mime
continue
}
set date ""
catch {
set value [lindex [mime::getheader $mime Date] 0]
append date [format %02d \
[mime::parsedatetime $value mon]] / \
[format %02d [mime::parsedatetime $value mday]] " " \
[format %02d [mime::parsedatetime $value hour]] : \
[format %02d [mime::parsedatetime $value min]]
}
if {![string compare $date ""]} {
set date "unknown date"
}
set from ""
catch {
set from [mutl::firstaddress [mime::getheader $mime From]]
catch { unset aprops }
array set aprops [lindex [mime::parseaddress $from] 0]
set from "<a href='mailto:$aprops(local)@$aprops(domain)'>$aprops(friendly)</a>"
}
set subject ""
catch {
set subject [lindex [mime::getheader $mime Subject] 0]
}
puts stdout "<tr><td><a href=\"news?folder=$folder&msgNo=$msgNo\">$date</a></td><td width=5></td><td><b>$from</b></td><td width=5></td><td>$subject</td></tr>"
}
puts stdout "</table>
</body></html>"
cleanup
}
# /news?folder="whatever"&msgNo="N"
if {![catch { set arg(msgno) } msgNo]} {
if {[catch { mbox::getmsgtoken $mbox $msgNo } mime]} {
cleanup $mime 505
}
if {![string compare [set part [firstext $mime]] ""]} {
set part $mime
}
switch -- [set content [mime::getproperty $part content]] {
text/plain {
regsub -all "\n\n" [mime::getbody $part] "<p>" body
set result "<html><head><title>$folder $msgNo</title></head>
<body>$body</body></html>"
}
text/html {
set result [mime::getbody $part]
}
default {
set result "<html><head><title>$folder $msgNo</title></head>
<body>
Message is $content.
</body></html>"
}
}
puts stdout "HTTP/1.0 200
Content-Type: text/html
$result"
cleanup
}
cleanup "page $request unavailable" 504
} result]} {
global errorCode errorInfo
set ecode $errorCode
set einfo $errorInfo
if {(![catch { info body tclLog } result2]) \
&& ([string compare [string trim $result2] \
{catch {puts stderr $string}}])} {
catch { tclLog $result }
}
if {![string first "POSIX EPIPE" $ecode]} {
exit 0
}
catch {
smtp::sendmessage \
[mime::initialize \
-canonical text/plain \
-param {charset us-ascii} \
-string "$result\n\nerrorCode: $ecode\n\n$einfo"] \
-originator "" \
-header [list From [id user]@[info hostname]] \
-header [list To operator@[info hostname]] \
-header [list Subject "[info hostname] fatal $program"]
}
cleanup $result
}
exit 75
|