LinuxMCE Forums

General => Developers => Topic started by: freymann on October 21, 2008, 10:40:03 PM

Title: How to get a message in that pop-up status window
Post by: freymann on October 21, 2008, 10:40:03 PM
I'm working on a little idea of mine..

I usually watch LMCE with either my Notebook or iPod Touch nearby, and flip the device on about every 30 minutes to check for new emails. I was thinking... wouldn't it be nice to have something running on the core that can check my mail accounts and alert me when new messages come in?

Just a quick little status window in the top left corner of the screen with something like:

New Message From: <name>
Subject: <subject>

Something that pops up for a second or two then disappears.

I'm already tinkering with a simple PHP script and class and have it spitting out that information to my terminal window. The next step is to find a way to have LMCE display that on the screen.

Can that be done by writing to a log file or do I need to send a command to the dcerouter some how through PHP?
Title: Re: How to get a message in that pop-up status window
Post by: tschak909 on October 22, 2008, 02:00:35 AM
Yes, you can use the MessageSend command to do this.

http://wiki.linuxmce.org/index.php/MessageSend

Basically, the format of the messagesend command would be something like:


/usr/pluto/bin/MessageSend localhost 0 -305 1 809 9 "Testing" 70 "" 182 "10" 251 ""


a quick breakdown:

localhost = the dcerouter
0 = the from device.. nonsensical in this case, so 0
-305 = a virtual device, stating all orbiters in the house
1 = the message type.. 1 = a command
809 = the message # (in this case, command #809 is Display Alert)
the rest are parameter hashes
9 = Text
182 = message timeout (how long it stays on screen)

That should give you enough to get started. And yes, before you ask, you can do it.. the trick is knowing what to send where.

-Thom
Title: Re: How to get a message in that pop-up status window
Post by: freymann on October 22, 2008, 03:15:09 AM
Quote from: tschak909 on October 22, 2008, 02:00:35 AM
Yes, you can use the MessageSend command to do this.

http://wiki.linuxmce.org/index.php/MessageSend

Basically, the format of the messagesend command would be something like:


/usr/pluto/bin/MessageSend localhost 0 -305 1 809 9 "Testing" 70 "" 182 "10" 251 ""


Oh that's a beauty!

My test script is checking a couple of accounts and I'm receiving the little status window in the top left of the screen.

When I tidy this up I'll make it available to all.

Thanks!
Title: Re: How to get a message in that pop-up status window
Post by: freymann on October 22, 2008, 04:29:12 AM
So far tonight, if I'm at the main screen, the little pop up notices appear fine, but if I'm watching a video, I don't see the pop up.

If I'm watching TV through MythTV or watching a video, and the picture is taking up the screen, is it possible to get the pop up in this mode? I've been testing while viewing video on my Dell Notebook, which is a MD using UI1.
Title: Re: How to get a message in that pop-up status window
Post by: tschak909 on October 22, 2008, 04:33:01 AM
yes, look up the parameters for the display alert command.

you can go into wizard > devices > orbiter... find an orbiter, any orbiter, select Advanced... then select Send Command to Device

select Display Alert, you'll then see the command parameters it can accept.

the rest is left as an exercise to the reader.

-Thom
Title: Re: How to get a message in that pop-up status window
Post by: freymann on October 22, 2008, 04:52:52 AM
Quote from: tschak909 on October 22, 2008, 04:33:01 AM
yes, look up the parameters for the display alert command.

you can go into wizard > devices > orbiter... find an orbiter, any orbiter, select Advanced... then select Send Command to Device

select Display Alert, you'll then see the command parameters it can accept.

Thanks Thom.

When I did that, I got the same parameters that you gave me above and the message appears if I'm looking at UI1's menu screen but it doesn't appear while watching video playback (well, except that if I try a sample 'test' it uses a specific orbiter # instead of the generic 305 all orbiters device.

I'll tinker some more, but if it won't pop up a notice while watching video then it defeats my idea.

Interesting stuff though!

Title: Re: How to get a message in that pop-up status window
Post by: colinjones on October 22, 2008, 05:22:00 AM
Wouldn't it be the #251 interruption parameter? Looks like it defaults to blank or 0, perhaps try 1, 2, 3, 4, 8, 16 etc?
Title: Re: How to get a message in that pop-up status window
Post by: tschak909 on October 22, 2008, 05:30:32 AM
hopefully, everyone is reading this....

web admin...

notice that when i told you to do a send command, and select display alert.. there was a command...

go to advanced > dce > commands

do a find on the page, for 809

it should be, display alert

you'll notice that it will show which devices use this command etc...

click on display alert.

you will see the parameters that this command can send... normally, the comments section would have a list of what each parameter can take.. in this case, it's not specified.. only that it's an integer for Interruption...

searching through the source code, we find a function, okayToInterrupt... we see a reference to several constants:


bool Orbiter::OkayToInterrupt( int iInterruption )
{
        if( iInterruption==interuptAlways )
                return true;

        if( m_dwPK_Device_NowPlaying )
                // If it's video it's always false.  If it's audio only, it's true only if there's no video
                return m_bContainsVideo ? false : iInterruption==interuptOnlyAudio;

        // Nothing is playing.  So unless it's interuptNever and there's an outside application we're ok
        if( (iInterruption==interuptNever || iInterruption==interuptOnlyAudio) && m_sActiveApplication_Window.empty()==false )
                return false;
        return true;
}


looking further, I do a grep inside src/


grep -rin "interuptOnlyAudio" *
Gen_Devices/AllScreens.h:37:            interuptOnlyAudio=3 /* will not interrupt a web browser, only when audio is playing */


ah, here we go.. a variable. odd.. not a constant... but at least we know now.. looking around that area, we see:


        enum eInterruption {interuptAlways=0, /* always interupt his activity and change screens */
                interuptNever=1, /* never interupt, only change the screen if the system is idle */
                interuptNoVideo=2, /* will interrupt a web browser but not a movie */
                interuptOnlyAudio=3 /* will not interrupt a web browser, only when audio is playing */
        };



So there you go. Four values that you can put into interruption, to trigger how the alert is displayed.

This concludes tonight's lesson on code forensics and analysis.

-Thom
Title: Re: How to get a message in that pop-up status window
Post by: colinjones on October 22, 2008, 06:16:05 AM
COOL! That explained a lot on how to find these things. DAMN! I wish the forums site had the ability to book mark threads and posts to come back to later - I've often wanted that for reference, now more than ever :)
Title: Re: How to get a message in that pop-up status window
Post by: hari on October 22, 2008, 09:37:54 AM
that's why we have a wiki :-p
Title: Re: How to get a message in that pop-up status window
Post by: colinjones on October 22, 2008, 01:36:49 PM
whatever
Title: Re: How to get a message in that pop-up status window
Post by: freymann on October 22, 2008, 02:35:19 PM
Quote from: tschak909 on October 22, 2008, 05:30:32 AM
notice that when i told you to do a send command, and select display alert.. there was a command...

Awh, I missed that little bread crumb... the "interrupt" command you are speaking about...


Quote

        enum eInterruption {interuptAlways=0, /* always interupt his activity and change screens */
                interuptNever=1, /* never interupt, only change the screen if the system is idle */
                interuptNoVideo=2, /* will interrupt a web browser but not a movie */
                interuptOnlyAudio=3 /* will not interrupt a web browser, only when audio is playing */
        };



So there you go. Four values that you can put into interruption, to trigger how the alert is displayed.

Thanks Thom. This information is great!

I take this to mean the interrupt value can be 0, 1, 2, or 3. Is that correct?

Otherwise I'm not sure how you would include interuptAlways=0 on the MessageSend command line.

No matter what number I use it never displays the pop up notice while I'm watching a video on my MD. I will take the notebook to the core and the living-room and send a command to the orbiter on those machines directly from the web-admin to see if they react differently.

Title: Re: How to get a message in that pop-up status window
Post by: totallymaxed on October 22, 2008, 06:50:09 PM
Quote from: tschak909 on October 22, 2008, 05:30:32 AM
ah, here we go.. a variable. odd.. not a constant... but at least we know now.. looking around that area, we see:


        enum eInterruption {interuptAlways=0, /* always interupt his activity and change screens */
                interuptNever=1, /* never interupt, only change the screen if the system is idle */
                interuptNoVideo=2, /* will interrupt a web browser but not a movie */
                interuptOnlyAudio=3 /* will not interrupt a web browser, only when audio is playing */
        };



So there you go. Four values that you can put into interruption, to trigger how the alert is displayed.

This concludes tonight's lesson on code forensics and analysis.

-Thom

How does the above interact with the 'Alert Filter Level' field in the Device Data? This field accepts the values 'all' , 'none' or 'important'

Andrew
Title: Re: How to get a message in that pop-up status window
Post by: tschak909 on October 22, 2008, 06:52:35 PM
I will have to look at the code to be sure, BUT, I believe the Alert Filter Level is processed before the Interruption value is used.

-Thom
Title: Re: How to get a message in that pop-up status window
Post by: freymann on October 22, 2008, 06:55:00 PM
I just had the program running and was watching a video on the living-room MD which uses UI2 and the pop ups appear fine on that set. The pop ups don't appear (when watching video) on my notebook MD which uses UI1.

That's not an issue for me, as I wanted to be aware of new emails when at the living-room or basement stations, so onwards and upwards.

Title: Re: How to get a message in that pop-up status window
Post by: freymann on October 23, 2008, 04:31:23 AM
Ok, I've been running this script for the last 4 or 5 hours and everything seems to be doing what I want.

-Define your pop3 email accounts you want monitored at the top of the script.
-SSH into the core and place the scripts where ever you want on the core. I'd suggest the home directory you land in.
-Execute the program in the background with:  php chkmail.php &   or just run it and monitor the action:  php chkmail.php

The script will check your pop3 accounts at whatever interval you set (default is 5 minutes). The script is smart enough to keep a running tally of the message id's it has already processed, so you get one warning about a new message, instead of repeated notices about messages you've already been alerted to. Therefore, whatever directory you put these scripts into must have write access. If you check your mail and clean out your inbox the script will delete the tally and start fresh.

All this does is simply flash a message on your screen informing you of new incoming email messages. I find it handy to monitor my various email accounts while watching video or tv in the living-room or basement. I can then decide if I want/need to pause the video and read/reply to the email message.

You need to get the pop3.class.inc file from this location:

http://www.phpclasses.org/browse/package/1120.html

Just scroll to the bottom where you can click on DOWNLOAD and save it. There's also a version available for PHP5.

And now here's some code I've assembled:


<?php

// A little php script to check for new mail messages and sent a display_alert
// command to alert LinuxMCE users of new email messages
// Put this script and the pop3.class.inc somewhere. Data files will be created
// in this directory to store data about incoming messages.
// To run it:  php chkmail.php
// and press Ctrl-C to stop. Or make it a background job:
// php chkmail.php &
//
// -------------------------------------------------------------
// Add your mail account info to the $mail_accounts array
// Separate your different accounts with opening/closing quotes
// Separeate your account info with a semi-colon (;)
// $mail_accounts = array("pop3.servername;pop.usename;pop3.password")
// Add as many accounts as you like
// $mail_accounts = array("pop3.servername;pop.usename;pop3.password","pop3.servername2;pop.usename2;pop3.password2")
//

$mail_accounts = array("pop3.server1;username1;password1","pop3.server2;username2;password2");

$ignore_subjects = array("Cron");

// # of seconds between mail checks. 300 = 5 mins.
$waitfor 300;

//
// You shouldn't have to edit anything below
// -------------------------------------------------------------

require_once('pop3.class.inc');

$pop3 = new POP3;

while (
1) {

   foreach (
$mail_accounts as $account) {

list($mailserver,$username,$password) = explode(";"$account);

// Read in data file for this account
$file_name $mailserver "-" $username ".cfg";
$msgids read_file($file_name);
if ($msgids == false) {
// That's ok, no previous data
$ids = array();
$orig_count 0;
} else {
$ids explode("\n"$msgids);
$tally count($ids) - 1;
for ($i 0$i $tally$i++) {
// Want to strip carraige returns and line feeds so our array
// comparisons later match correctly
$ids[$i] = ereg_replace("/\n\r|\r\n/"""$ids[$i]);
}
$orig_count = (count($ids));
}

// Connect to mail server
$do $pop3->connect($mailserver);
if ($do == false) {
echo $pop3->error;
continue;
}

// Login to your inbox
$do $pop3->login($username$password);
if ($do == false) {
    
  echo $pop3->error;
continue;
}

// Get office status
$status $pop3->get_office_status();

if ($status == false) {
echo $pop3->error;
continue;
}

$count $status['count_mails'];

for ($i 1$i <= $count$i++) {
$email $pop3->get_mail($i);

$email parse_email($email);

if ($email == false) {
echo $pop3->error;
continue;
}

// Check to see if the Message-ID is in our ids array.
// Only announce if it's a new message and NOT in the existing array
$testid trim($email['headers']['Message-ID']);
$testid get_simple_email($testid);
if (empty($testid) || $testid == " ") {
// Try different spelling
$testid trim($email['headers']['Message-Id']);
$testid get_simple_email($testid);
}
if (empty($testid)) {
print_r($email[headers]);
$testid time();
}
if (!in_array($testid$ids)) {
// add this one to the ids array
if (count($ids)==0) {
$ids = array($testid);
} else {
array_push($ids$testid);
}
// echo a few things to the terminal for monitoring purposes
echo "Message-ID: [$testid]\n";
$efrom $email['headers']['From'];
$efrom get_simple_email($efrom);
echo date("Y-m-d H:i:s") . " New messages from: $efrom\n";
echo 'Subject: ' $email['headers']['Subject'] . "\n\n";
// send message to dcerouter if subject isn't in ignore list
$found 0;
foreach ($ignore_subjects as $subj) { 
if (eregi($subj$email['headers']['Subject'])) { 
$found 1;
break;
}
}
if (!$found) { 
$message "Email from $efrom regarding " $email['headers']['Subject'];
exec("/usr/pluto/bin/MessageSend localhost 0 -305 1 809 9 \"$message\" 70 \"\" 182 10 251 0");
// Let the pop up show for multiple messages
sleep(10);
}
}

}

if ($count == '0') {
echo date("Y-m-d H:i:s") . " There are no new e-mails\n";
// This is a good place to clear out the data file since the mailbox is now empty
if (file_exists($file_name)) {
@unlink($file_name);
}
}

// See if there's been any additions to the ids array
// and if so, write back the new info
$new_count = (count($ids));
if ($orig_count $new_count) {
write_file($file_name$ids);
}

$pop3->close();

   }
   unset(
$msgids);
   unset(
$ids);
   
sleep($waitfor);
}


function 
parse_email ($email) {
    
// Split header and message
    
$header = array();
    
$message = array();

    
$is_header true;
    foreach (
$email as $line) {
        if (
$line == '<HEADER> ' "\r\n") continue;
        if (
$line == '<MESSAGE> ' "\r\n") continue;
        if (
$line == '</MESSAGE> ' "\r\n") continue;
        if (
$line == '</HEADER> ' "\r\n") { $is_header false; continue; }

        if (
$is_header == true) {
            
$header[] = $line;
        } else {
            
$message[] = $line;
        }
    }

    
// Parse headers
    
$headers = array();
    foreach (
$header as $line) {
        
$colon_pos strpos($line':');
        
$space_pos strpos($line' ');

        if (
$colon_pos === false OR $space_pos $colon_pos) {
            
// attach to previous
            
$previous .= "\r\n" $line;
            continue;
        }

        
// Get key
        
$key substr($line0$colon_pos);

        
// Get value
        
$value substr($line$colon_pos+2);
        
$headers[$key] = $value;

        
$previous =& $headers[$key];
    }

    
// Parse message
    
$message implode(''$message);

    
// Return array
    
$email = array();
    
$email['message'] = $message;
    
$email['headers'] = $headers;

    return 
$email;
}

function 
get_simple_email($str) {

$open strpos($str"<");
$close strpos($str">");

if (($open === false) && ($close === false)) {
return $str;
}

return substr($str$open 1$close $open 1);

}


function 
read_file($name) {

$contents false;

if (file_exists($name))  {
if ($fd = @fopen($name"r")) {
$contents fread($fdfilesize($name));
fclose($fd);
}
}

return $contents;
}


function 
write_file($name$data) {

if ($fd = @fopen($name"w")) {
foreach ($data as $line) {
$line ereg_replace("/\n\r|\r\n/"""$line);
if (!empty($line)) { 
fwrite($fd$line "\n");
}
}
fclose($fd);
return true;
} else {
return false;
}
}

?>



Save this as whatever you want. I called it chkmail.php

That's it!


Title: Re: How to get a message in that pop-up status window
Post by: chriss on October 23, 2008, 09:24:05 AM
Wow, nice work. Can you put that on the wiki?  8)
Title: Re: How to get a message in that pop-up status window
Post by: totallymaxed on October 23, 2008, 12:51:47 PM
Quote from: freymann on October 22, 2008, 06:55:00 PM
I just had the program running and was watching a video on the living-room MD which uses UI2 and the pop ups appear fine on that set. The pop ups don't appear (when watching video) on my notebook MD which uses UI1.

That's not an issue for me, as I wanted to be aware of new emails when at the living-room or basement stations, so onwards and upwards.



Is your notebook running the Windows Orbiter or is it pxe booted as a full MD?

Andrew
Title: Re: How to get a message in that pop-up status window
Post by: freymann on October 23, 2008, 01:45:39 PM
Quote from: totallymaxed on October 23, 2008, 12:51:47 PM
Quote from: freymann on October 22, 2008, 06:55:00 PM
I just had the program running and was watching a video on the living-room MD which uses UI2 and the pop ups appear fine on that set. The pop ups don't appear (when watching video) on my notebook MD which uses UI1.

That's not an issue for me, as I wanted to be aware of new emails when at the living-room or basement stations, so onwards and upwards.



Is your notebook running the Windows Orbiter or is it pxe booted as a full MD?


Hi Andrew. It boots over the network and is a full MD.

It uses UI1. I do see the message pop-ups when at the menu but not when watching video.
Title: Re: How to get a message in that pop-up status window
Post by: freymann on October 23, 2008, 01:46:30 PM
Quote from: chriss on October 23, 2008, 09:24:05 AM
Wow, nice work. Can you put that on the wiki?  8)

Thanks. Yes, I was going to do that after everybody has had a chance to check it out.
Title: Re: How to get a message in that pop-up status window
Post by: Marie.O on October 24, 2008, 09:37:47 PM
Could you put the needed pop3 php code someplace where one does not need to be registered?

rgds
Oliver
Title: Re: How to get a message in that pop-up status window
Post by: tschak909 on October 24, 2008, 09:40:46 PM
it is worth noting, that eventually LinuxMCE will have a mail client of its own on the orbiter, and will thus provide email notifications on its own.

-Thom
Title: Re: How to get a message in that pop-up status window
Post by: freymann on October 24, 2008, 09:47:37 PM
Quote from: posde on October 24, 2008, 09:37:47 PM
Could you put the needed pop3 php code someplace where one does not need to be registered?

I wasn't registered. I just went to the bottom of the screen, clicked on the link for the needed include file and off it went.

But yes, if you can't get from there, try it from here:

http://ww2.interpool.ca/files/pop3.class.inc
Title: Re: How to get a message in that pop-up status window
Post by: freymann on October 24, 2008, 09:48:24 PM
Quote from: tschak909 on October 24, 2008, 09:40:46 PM
it is worth noting, that eventually LinuxMCE will have a mail client of its own on the orbiter, and will thus provide email notifications on its own.

Kewl! Until then this method is working great!

Title: Re: How to get a message in that pop-up status window
Post by: dlewis on October 24, 2008, 10:08:36 PM
freyman, create and article and post this to the wiki? Thanks!
Title: Re: How to get a message in that pop-up status window
Post by: freymann on October 24, 2008, 10:37:58 PM
Quote from: dlewis on October 24, 2008, 10:08:36 PM
freyman, create and article and post this to the wiki? Thanks!

Yes, I planned on doing that after I had used the thing for a few days. I updated my script included at the top of page 2 once, and that's the script I'm using to this day...

So probably on Monday I'll create the wiki page for this.
Title: Re: How to get a message in that pop-up status window
Post by: freymann on October 27, 2008, 04:12:50 PM
There is now a wiki page for this:

http://wiki.linuxmce.org/index.php/Display_Alert_Email_Notification

I added an array you can use to ignore messages with matching words in the subject line.