Email-to-IM

From Second Life Wiki
Revision as of 16:39, 5 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.05-Sa BY ELITE RUNNER

PREVIOUS UPDATE(S): Improved date/time readout, improved output

LATEST UPDATE: CODE CLEAN-UP AND ADDED TIMEZONE FEATURE <lsl> //E-MAIL TO IM SCRIPT v2.1 //UPDATED/MODIFIED BY ELITE RUNNER - ELITE 3000 SECOND LIFE UNIT //DISTRIBUTED AS GPL //CREDITS TO FLENNAN ROFFO FOR UNIX2DATETIME //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 {

   state_entry() {
       owner = llGetOwner();
       string address = (string)llGetKey() + "@lsl.secondlife.com";
       llSetText(llKey2Name(owner)+"'s 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;
   }
   //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) {
       llInstantMessage(owner, "From: "+sender +"\n \n Time/Date: \n"+ DateString(Unix2DateTime(time,-8))+"-"+weekdayname(time,-8) +
                           " "+ TimeString(Unix2DateTime(time,-8))+"SLT \n "+
                           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("", "");
   }
   timer() {
        llGetNextEmail("", "");
   }
   state_exit() {
       llSetTimerEvent(0.0);
       llSetText(llKey2Name(owner)+"'s 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>