Mailcheck

From Scriptwiki
Revision as of 10:00, 1 April 2006 by Saturn (talk | contribs) (small bug fixed, thanks to ArKanis)
Jump to navigation Jump to search

This script is supposed to check your POP3 mailaccount and return whether you have new mails or not. You will find a little alias at the beginning where you have to put your login details in.

; this is the alias where you have to put your login details in by replacing the <...>.
; lets actually make a new alias called mailoptions 
alias mailoptions {
 ; check if we get something like $mailoptions(emailserver) or $mailoptions(emailport) and return it
 ; like pop.gmx.net
 if ($1 == emailserver) { return <your email server> }
 ; this is normally 110
 elseif ($1 == emailport) { return <your email port> }
 ; probably your email address
 elseif ($1 == emailuser) { return <your email user> }
 ; this is your password
 elseif ($1 == emailpass) { return <your email pass> }
}

; this is the alias you can use to start checking your account
; lets make another alias called mailcheck
alias mailcheck { 
 ; at first we need to check if all details are given. Only if they are, we will continue
 if ($mailoptions(emailserver)) && ($mailoptions(emailport)) && (mailsoptions(emailuser)) && (mailsoptions(emailpass)) {
   ; this command will close a socket called "mailcheck" if one exists.
   sockclose mailcheck 
   ; here we open a new socket called mailcheck to our server on the given port
   sockopen mailcheck $mailoptions(emailserver) $mailoptions(emailport) 
   ; we set a variable for a later alias
   set %mailcheck_var 0
 }
}

; this event will trigger if the new socket called mailcheck has been opened
on *:Sockopen:mailcheck: {
 ; lets check if there are any errors, if they are, echo it and halt
 if ($sockerr > 0) { echo -a ERROR | halt }
}

; this event will trigger when we receive any data through the socket
on *:Sockread:mailcheck: {
 ; sockread will put the data in a variable, here %temp
 sockread %temp
 ; the alias mailcheck_read, that we will define later, is supposed to handle eveerything we receive
 mailcheck_read %temp
}

; this is the alias mailcheck_read that will handle everything
; lets start it
alias mailcheck_read {
 ; increase the %mailcheck_var variable, we have set to 0 in the mailcheck alias, once everytime we get some new data
 inc %mailcheck_var 1
 ; we check if $1 is +OK. Read the pop3 protocol to get more info about it
 if $1 == +OK {
   ; if its the first time we get some data, send "user <username>" to the socket.
   if (%mailcheck_var == 1) { .sockwrite -n mailcheck user $mailoptions(emailuser) }
   ; after that, we need to send our password   
   if (%mailcheck_var == 2) { .sockwrite -n mailcheck pass $mailoptions(emailpass) }
   ; after sending username and password, we need to tell the server what we actually want to do. We want to know if there are new emails. So send "stat".
   if (%mailcheck_var == 3) { .sockwrite -n mailcheck stat }
   ; if we are here, %temp will contain whether we have new mails or now. $2 is the number of new mails, being 0 or 1 or any other number.
   if (%mailcheck_var == 4) {
     ; lets echo this to the status window
     echo -s New mails: $2
     ; closing the socket at the end as we are done
     sockclose mailcheck
   }
 }
}