home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2004 April
/
PCWorld_2004-04_cd.bin
/
software
/
temacd
/
remotany
/
RemotelyAnywhere.msi
/
Email.sma
< prev
next >
Wrap
Text File
|
2003-08-19
|
6KB
|
216 lines
////////////////////////////////////////////////////////////////////////////////
// //
// RemotelyAnywhere Sample 'SMALL' Scripts //
// //
// RemotelyAnywhere provides a mechanism for scripting via a built-in //
// language. The language is called Small, and more information about //
// it can be found at http://compuphase.com/small.htm. //
// //
// Before you use these sample scripts, you should tailor their //
// behavior to better suit your configuration and your needs. //
// //
// This script will check if you have email. It can be run interactively (ie. //
// from the Scripts menu, or can be called from a Monitoring Script. //
// //
// To make effective use of it, edit the variables at the beginning of the //
// 'main()' function. //
// //
// While this script is of little to no use in its current form, it can be //
// easily modified to monitor a proprietary service that utilizes TCP/IP for //
// communication. //
// //
// THIS SOFTWARE IS PROVIDED BY 3AM LABS LTD ``AS IS'' AND //
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE //
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE //
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE //
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL //
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS //
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) //
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT //
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY //
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF //
// SUCH DAMAGE. //
// //
////////////////////////////////////////////////////////////////////////////////
#include <ra>
//
// Utility function
//
// Reads a line of text from a socket
//
readline(sock, str[], len)
{
new l=0, d[1], r, c;
while (l<len-1) {
r=recv(sock, d, 1);
if (!r)
break;
c=gb(d, 0);
str{l++}=c;
if (c=='\n')
break;
}
str{l}=0;
return l;
}
//
// Utility function
//
// Sends some data to the SMTP server and waits for a response
//
sendcheck(sock, str[], buf[], len)
{
new l=strlen(str);
// Convert str to a "C-Style" string
strize(str);
send(sock, str, l);
l=readline(sock, buf, len);
return (l>0 && buf{0}=='+');
}
//
// The main program
//
public main()
{
//
// These variables define your POP3 server and your POP3 account
//
new sock=socket("your.mail.server", 110);
new user[]="user _your account_\r\n", pass[]="pass _your password_\r\n";
//
// The return value. We'll return this to the monitoring script. If
// it contains a non-zero value, the handler is executed.
//
new ret=0;
//
// Variables used in the script
//
new r, outp, err=0, list[]="list\r\n", quit[]="quit\r\n";
//
// Try to send HTML output. If it fails, we are being called from
// a monitoring script.
//
outp = htmlBeginOutput("Check mail");
//
// If no output is possible, do not bother
//
if (outp)
htmlBeginDialog("Check mail messages");
//
// If succesfully connected
//
if (sock) {
//
// A buffer of 256 integers
//
new data[256];
//
// Read the 'HELO' string from the POP3 server
//
r=readline(sock, data, 1024);
//
// If received the HELO string, we're on our way.
// First, always make sure that the first character is a
// '+' sign. This identifies a positive response from the
// POP3 server,
//
// Then, attempt to log in and send the list command.
//
if (r>0 && data{0}=='+' &&
sendcheck(sock, user, data, 1024) &&
sendcheck(sock, pass, data, 1024) &&
sendcheck(sock, list, data, 1024))
{
//
// Read through the list of messages, incrementing the count
// by one for each.
//
while (readline(sock, data, 1024)>0 && data{0}!='.')
ret++;
} else {
//
// If could not log in, report the error.
//
if (outp)
htmlError("Failed to authenticate");
err=1;
}
//
// Terminate the connection.
//
sendcheck(sock, quit, data, 1024);
closesocket(sock);
} else if (outp) {
//
// If could not connect, report the error.
//
htmlError("Failed to connect to your POP3 server");
err=1;
}
if (outp) {
if (!err) {
//
// If running in interactive mode, report the findings.
//
if (ret) {
new msg[64];
sprintf(msg, "You have %d messages in your mailbox", ret);
htmlWrite(msg);
} else {
htmlWrite("You have no messages in your mailbox");
}
}
htmlEndDialog();
htmlEndOutput();
}
//
// Return the number of messages in the inbox to the caller.
//
return ret;
}