pop3 client (tcl)

raspi 31.03.02 12:42

Hakee postit pop3 palvelimelta

 Tekstiversio  Arvo: 1 (1 ääntä)  Äänestä: +  -
#!/usr/bin/tclsh
# MINI POP3 Client
# Toimii ainakin windowssissa ja linuxissa
# pekkajarvinen@kolumbus.fi
# versio 0.08

set pop(addr) "localhost" ;# pop3 serverin osoite
set pop(port) 110 ;# default: 110
set pop(user) "raspi" ;# käyttäjä
set pop(pass) "sekkkret" ;# salasana
set pop(info) 1 ;# näytetäänkö "sulla on n uutta mailia"
set pop(display) 1 ;# näytetäänkö mailit?
set pop(destroy) 0 ;# tuhotaanko mailit?

set debugmode 0      ;# DEBUG
set sendchr "SEND: " ;# DEBUG
set recvchr "RCVD: " ;# DEBUG

if {[catch {set sock [socket $pop(addr) $pop(port)]} msg] == 0} {
  fconfigure $sock -buffering line
  set debug [gets $sock]
  if {$debugmode} {puts "${recvchr}$debug"}

  if {$debugmode} {puts "${sendchr}USER $pop(user)"}
  puts $sock "USER $pop(user)"
  set debug [gets $sock]
  if {$debugmode} {puts "${recvchr}$debug"}

  if {$debugmode} {puts "${sendchr}PASS $pop(pass)"}
  puts $sock "PASS $pop(pass)"
  set debug [gets $sock]
  if {$debugmode} {puts "${recvchr}$debug"}

  if {[string match "+OK*" $debug]} {

  if {$debugmode} {puts "${sendchr}STAT"}
    puts $sock "STAT" 
    set pop(stat) [gets $sock]
    if {$debugmode} {puts "${recvchr}$pop(stat)"}
    set pop(total) [lindex $pop(stat) 1] 
    set pop(msg_size) [lindex $pop(stat) 2]
 
    if {$pop(total) > 0 } {
      if {$pop(info) == 1} {puts "$pop(total) new message(s) ($pop(msg_size)) bytes"}
  
      if {$pop(display) == 1} {
        set msgno 1
  
        while {$msgno <= $pop(total)} {
          if {$debugmode} {puts "${sendchr}RETR $msgno"}
          puts $sock "RETR $msgno" 
          set debug [gets $sock]
          if {$debugmode} {puts "${recvchr}$debug"}

          set line "foo"

          while {$line != "."} {
            set line [gets $sock]
            set linesis $line
            if {[string equal $linesis "."]} {set linesis ""}
            regsub -all {^\.\.} $linesis {.} linesis ;# ".f" == "..f" // "..f" == "...f" // "f." == "f."
            puts $linesis
          }

          if {$pop(destroy) == 1} {
            if {$debugmode} {puts "${sendchr}DELE $msgno"}
            puts $sock "DELE $msgno" 
            set debug [gets $sock]
            if {$debugmode} {puts $debug}
          }
               
        set msgno [expr $msgno + 1]

        }
      } 
    } else {puts "no messages"}
  } else {puts "ERROR: $debug"}

  if {$debugmode} {puts "${sendchr}QUIT"}
  puts $sock "QUIT"
  set debug [gets $sock]
  if {$debugmode} {puts "${recvchr}$debug"}

  close $sock

} else {
  puts "ERROR: $msg"
}