Email-to-IM

From Second Life Wiki
Revision as of 22:48, 3 March 2011 by Elite Runner (talk | contribs)
Jump to navigation Jump to search

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.

UPDATED ON 2011.03.03-Th BY ELITE RUNNER LATEST UPDATE: Improved date/time readout, improved output <lsl> integer timezone = -8; //Change to -7 if DST is present 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) {

   integer time2 = (integer)time;
   integer days_since_1_1_1970     = (time2+(timezone*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+(timezone*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 timetick = ((float)time/86400)+((float)timezone/24);
   integer weekdaynumber = llFloor(timetick)%7;
   list weekdays = ["Th","Fr","Sa","Su","Mo","Tu","We"];
   string weekday  = llList2String(weekdays, weekdaynumber);
   return weekday;

}

key request; key owner; integer owner_online;

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(30);
   }
   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, "From: "+sender +"\n \n Time/Date: \n " + DateString(Unix2DateTime(time))+"-"+weekdayname(time) +
                           "\n"+ TimeString(Unix2DateTime(time))+"SLT \n \n Subject:"+subject+"\n \n Message: \n "+body+" \n ---------------");
       if (num_remain > 0) llGetNextEmail("", "");
   }
   dataserver(key query, string data) {
       if (query == request) {
           request = "";
           if (data == "1") {
               owner_online = TRUE;
               llSetTimerEvent(30);
           }
           else {
               owner_online = FALSE;
               llSetTimerEvent(60);
           }
       }
   }
   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>