Difference between revisions of "Email-to-IM"

From Second Life Wiki
Jump to navigation Jump to search
m (added to LSL Email category)
Line 6: Line 6:


You are welcome to correct, add features or otherwise improve the script.
You are welcome to correct, add features or otherwise improve the script.
<lsl>
// Email-to-IM
// DoteDote Edison


UPDATED ON 2011.03.05-Sa BY ELITE RUNNER
///////// constants /////////
// how often to check for new email when owner is online (seconds)
float FAST = 60.0;
// how often to check owner online status when owner is offline (seconds)
float SLOW = 300.0;
// timezone offset from UTC
integer OFFSET = -4;


PREVIOUS UPDATE(S):
////////// globals //////////
Improved date/time readout, improved output
key request;
key owner;
integer owner_online;


LATEST UPDATE:
string GetStamp(string time) {
CODE CLEAN-UP AND ADDED TIMEZONE FEATURE
    list weekdays = ["THU", "FRI", "SAT", "SUN", "MON", "TUE", "WED"];
<lsl>
    integer a = (integer)time + (OFFSET*3600);
//E-MAIL TO IM SCRIPT v2.1
    integer hours = a/3600;
//UPDATED/MODIFIED BY ELITE RUNNER - ELITE 3000 SECOND LIFE UNIT
    integer mins = a/60;
//DISTRIBUTED AS GPL
    string day  = llList2String(weekdays, (hours/24)%7);
//CREDITS TO FLENNAN ROFFO FOR UNIX2DATETIME
    return (string)(hours%24) + ":" + (string)(mins%60) + " " + day;
//ORIGINAL SCRIPT BY DoteDote Edison
}


integer DAYS_PER_YEAR        = 365;
integer SECONDS_PER_YEAR    = 31536000;
integer SECONDS_PER_DAY      = 86400;
integer SECONDS_PER_HOUR    = 3600;
integer SECONDS_PER_MINUTE  = 60;
integer LeapYear(integer year)
{
    if (year % 4 == 0)
    {
        if (year % 100 == 0)
        {
            if (year % 400 == 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        else
        {
            return 1;
        }
    }
    else
    {
        return 0;
    }
}
integer DaysPerMonth(integer year,integer month2)
{
    if (month2 < 8)
    {
        if (month2 % 2 == 0)
        {
            if (month2 == 2)
            {
                if (LeapYear(year))
                {
                    return 29;
                }
                else
                {
                    return 28;
                }
            }
            else
            {
                return 30;
            }
        }
        else
        {
            return 31;
        }
    }
    else
    {
        if (month2 % 2 == 0)
        {
            return 31;
        }
        else
        {
            return 30;
        }
    }
}
integer DaysPerYear(integer year)
{
    if (LeapYear(year))
        return DAYS_PER_YEAR + 1;
    else
        return DAYS_PER_YEAR;
}
list Unix2DateTime(string time, float timezone)
{
    integer time2 = (integer)time;
    integer tz = (integer)timezone;
    integer days_since_1_1_1970    = (time2+(tz*3600)) / SECONDS_PER_DAY;
    integer day2 = days_since_1_1_1970 + 1;
    integer year  = 1970;
    integer days_per_year = DaysPerYear(year);
    while (day2 > days_per_year)
    {
        day2 -= days_per_year;
        ++year;
        days_per_year = DaysPerYear(year);
    }
    integer month2 = 1;
    integer days_per_month = DaysPerMonth(year,month2);
    while (day2 > days_per_month)
    {
        day2 -= days_per_month;
        if (++month2 > 12)
        {   
            ++year;
            month2 = 1;
        }
        days_per_month = DaysPerMonth(year,month2);
    }
    integer seconds_since_midnight  = (time2+(tz*3600)) % SECONDS_PER_DAY;
    integer hour24        = seconds_since_midnight / SECONDS_PER_HOUR;
    integer second      = seconds_since_midnight % SECONDS_PER_HOUR;
    integer minute      = second / SECONDS_PER_MINUTE;
    second              = second % SECONDS_PER_MINUTE;
    return [ year, month2, day2, hour24, minute, second ];
}
string DateString(list timelist)
{
    integer year      = llList2Integer(timelist,0);
    integer month2      = llList2Integer(timelist,1)-1;
    integer day2        = llList2Integer(timelist,2);
    string  monthstr    = (string)month2;
    string  daystr  = (string)day2;
    string yearstr = (string)year;
    if (month2 < 10)      monthstr    = "0" + monthstr;
    if (day2 < 10)    daystr    = "0" + daystr;
    return yearstr + "." + monthstr + "." + daystr;
}
string TimeString(list timelist)
{
    integer hour24        = llList2Integer(timelist,3);
    integer hour12      = hour24%12;
    integer minute      = llList2Integer(timelist,4);
    integer second      = llList2Integer(timelist,5);
    string apm = "am";
      if(hour24 > 11) {apm = "pm";}
      if(hour24 == 0 && minute == 0) {apm = "mn";}
      if(hour24 == 12 && minute == 0) {apm = "nn";}
    string  hourstr    = (string)hour12;
    string  minutestr  = (string)minute;
    string  secondstr  = (string)second;
    if (hour12 < 10)      hourstr    = "0" + hourstr;
    if (minute < 10)    minutestr    = "0" + minutestr;
    if (second < 10)    secondstr    = "0" + secondstr;
    return hourstr + ":" + minutestr + ":" + secondstr + apm;
}
string weekdayname( string time, float timezone){
    float timetick = ((float)time/86400)+(timezone/24);
    integer weekdaynumber = llFloor(timetick)%7;
    list weekdays = ["Th","Fr","Sa","Su","Mo","Tu","We"];
    string weekday  = llList2String(weekdays, weekdaynumber);
    return weekday;
}
key owner;
default {
default {
     state_entry() {
     state_entry() {
         owner = llGetOwner();
         owner = llGetOwner();
         string address = (string)llGetKey() + "@lsl.secondlife.com";
         string address = (string)llGetKey() + "@lsl.secondlife.com";
         llSetText(llKey2Name(owner)+"'s Email Server\nOnline", <0.25, 1.0, 0.25>, 1.0);
         llSetText("Email Server\nOnline", <0.25, 1.0, 0.25>, 1.0);
         llOwnerSay("Now online.  The Email-to-IM address for " + llKey2Name(owner) + " is:\n" + address);
         llOwnerSay("Now online.  The Email-to-IM address for " + llKey2Name(owner) + " is:\n" + address);
         llSetTimerEvent(30);
         llSetTimerEvent(FAST);
     }
     }
     on_rez(integer start_param) {
     on_rez(integer start_param) {
Line 192: Line 46:
         if (llDetectedKey(0) == owner) state off;
         if (llDetectedKey(0) == owner) state off;
     }
     }
    //CHANGE THE NUMBER -8 OR -5 TO CHOOSE YOUR TIMEZONE OFFSET FROM UTC IN HOURS AND CHANGE
    //TIMEZONE ABBREVIATION OR CITY NAME IF NEEDED
    //TO UTC (0), CET (-1), HK (Hong Kong) (8), CST (-6), TOK (Tokyo) -9, ETC
    //PLEASE SEE http://www.timeanddate.com FOR TIME LISTINGS AROUND THE WORLD
    //SUBTRACT VALUE BY 1 IF DAYLIGHT SAVING/SUMMER TIME IS PRESENT AND CHANGE
    //ABBREVIATION TO EDT, CDT, MDT, PDT, CEST, BST, ETC
     email(string time, string sender, string subject, string body, integer num_remain) {
     email(string time, string sender, string subject, string body, integer num_remain) {
         llInstantMessage(owner, "From: "+sender +"\n \n Time/Date: \n"+ DateString(Unix2DateTime(time,-8))+"-"+weekdayname(time,-8) +
         llInstantMessage(owner, "Email Received from: " + sender + " -- " + GetStamp(time));
                            " "+ TimeString(Unix2DateTime(time,-8))+"SLT \n "+
        llInstantMessage(owner, body);
                            DateString(Unix2DateTime(time,-5))+"-"+weekdayname(time,-5) +
                            " "+ TimeString(Unix2DateTime(time,-5))+"EST \n \n Subject:"+subject+"\n \n Message: \n "+body+" \n ---------------");
         if (num_remain > 0) llGetNextEmail("", "");
         if (num_remain > 0) llGetNextEmail("", "");
    }
    dataserver(key query, string data) {
        if (query == request) {
            request = "";
            if (data == "1") {
                owner_online = TRUE;
                llSetTimerEvent(FAST);
            }
            else {
                owner_online = FALSE;
                llSetTimerEvent(SLOW);
            }
        }
     }
     }
     timer() {
     timer() {
        llGetNextEmail("", "");
        request = llRequestAgentData(owner, DATA_ONLINE);
        if (owner_online) llGetNextEmail("", "");
     }
     }
     state_exit() {
     state_exit() {
         llSetTimerEvent(0.0);
         llSetTimerEvent(0.0);
         llSetText(llKey2Name(owner)+"'s Email Server\nOffline", <1.0, 0.25, 0.25>, 1.0);
         llSetText("Email Server\nOffline", <1.0, 0.25, 0.25>, 1.0);
     }
     }
}
}
 
state off {
state off {
     touch_start(integer num_detect) {
     touch_start(integer num_detect) {
Line 227: Line 85:


{{LSLC|Library}}
{{LSLC|Library}}
[[category:LSL Email]]

Revision as of 05:06, 6 March 2011

Email-to-IM

This is a basic script to translate emails into IMs. When initiated, the script says an email address, which is the UUID for the prim containing the script along with the lsl email domain. The body of any email sent to the address will be relayed as an object IM to the owner in SL. And a timestamp is included which is the time LL received the email (as opposed to the time the email is transmitted via IM).

The prim containing this script must have a stable UUID for longterm continued usage. This means, the object can not be taken into inventory and re-rezed.

You are welcome to correct, add features or otherwise improve the script. <lsl> // Email-to-IM // DoteDote Edison

///////// constants ///////// // how often to check for new email when owner is online (seconds) float FAST = 60.0; // how often to check owner online status when owner is offline (seconds) float SLOW = 300.0; // timezone offset from UTC integer OFFSET = -4;

////////// globals ////////// key request; key owner; integer owner_online;

string GetStamp(string time) {

   list weekdays = ["THU", "FRI", "SAT", "SUN", "MON", "TUE", "WED"];
   integer a = (integer)time + (OFFSET*3600);
   integer hours = a/3600;
   integer mins = a/60;
   string day  = llList2String(weekdays, (hours/24)%7);
   return (string)(hours%24) + ":" + (string)(mins%60) + " " + day;

}

default {

   state_entry() {
       owner = llGetOwner();
       string address = (string)llGetKey() + "@lsl.secondlife.com";
       llSetText("Email Server\nOnline", <0.25, 1.0, 0.25>, 1.0);
       llOwnerSay("Now online.  The Email-to-IM address for " + llKey2Name(owner) + " is:\n" + address);
       llSetTimerEvent(FAST);
   }
   on_rez(integer start_param) {
       llResetScript();
   }
   touch_start(integer num_detect) {
       if (llDetectedKey(0) == owner) state off;
   }
   email(string time, string sender, string subject, string body, integer num_remain) {
       llInstantMessage(owner, "Email Received from: " + sender + " -- " + GetStamp(time));
       llInstantMessage(owner, body);
       if (num_remain > 0) llGetNextEmail("", "");
   }
   dataserver(key query, string data) {
       if (query == request) {
           request = "";
           if (data == "1") {
               owner_online = TRUE;
               llSetTimerEvent(FAST);
           }
           else {
               owner_online = FALSE;
               llSetTimerEvent(SLOW);
           }
       }
   }
   timer() {
       request = llRequestAgentData(owner, DATA_ONLINE);
       if (owner_online) llGetNextEmail("", "");
   }
   state_exit() {
       llSetTimerEvent(0.0);
       llSetText("Email Server\nOffline", <1.0, 0.25, 0.25>, 1.0);
   }

}

state off {

   touch_start(integer num_detect) {
       if (llDetectedKey(0) == owner) state default;
   }
   on_rez(integer start_param) {
       llResetScript();
   }

} </lsl>