Version 0 of Checking your IMAP mail with Expect

Updated 2003-07-05 21:07:13

Rohan Pall, on a nice Saturday afternoon

  #!/usr/bin/expect -f

  #log_user 0    ;# hide interaction, i.e. do not display to stdout
  set timeout 10
  match_max 100000

  set server my.server.com
  set port 143
  set user {dude}
  set pass {32k$12!_a}

  spawn telnet $server $port
  expect {
    timeout {puts stdout "timeout while connecting to $server"; exit 1}
    "* OK"
  }

  send "a001 login $user $pass\r"
  expect {
    timeout {puts stdout "timed out after login"; exit 1}
    "a001 NO" {puts stdout "bad login"; exit 1}
    "a001 OK"
  }

  send "a002 examine inbox\r"
  expect {
    timeout {puts stdout "timed out after examining inbox"; exit 1}
    "a002 NO" {puts stdout "could not examine inbox"; exit 1}
    "a002 OK"
  }

  #parray expect_out

  set buffer_to_parse $expect_out(buffer)
  regexp {([0-9]+) RECENT} $buffer_to_parse -> new_msgs
  puts "new: $new_msgs"

  send "a003 logout\r"
  expect {
    timeout {puts stdout "timed out after logout"; exit 1}
    "a003 NO" {puts stdout "could not logout"; exit 1}
    "a003 OK"
  }

  exit