| View previous topic :: View next topic |
| Author |
Message |
declanh
Joined: 24 Aug 2005 Posts: 210
|
Posted: Thu Jan 19, 2006 3:30 pm Post subject: OSD email notify |
|
|
I've knocked together a quick perl script to use mythtvosd to notify of new emails.
It's very raw, but it works so far.
| Code: |
#!/usr/bin/perl
# Popcheck.pl - Declan Higgins
# Version 0.1 Jan 2006
use Net::POP3;
sub do_account ()
{
my $unwanted = shift ;
my $mail_server = shift ;
my $username = shift ;
my $password = shift ;
my $maxmess = shift ;
$pop = Net::POP3->new($mail_server)
or die "Can't open connection to $mail_server : $!\n";
defined ($pop->login($username, $password))
or die "Can't authenticate: $!\n";
$messages = $pop->list
or die "Can't get list of undeleted messages: $!\n";
$mc = keys %$messages;
return if $mc==0;
$nmc="$username has $mc emails";
$x=`/usr/bin/mythtvosd --template=alert --alert_text="$nmc"`;
sleep 3;
foreach $msgid (keys %$messages) {
$message = $pop->get($msgid);
unless (defined $message) {
warn "Couldn't fetch $msgid from server: $!\n";
next;
}
$messno++;
$q=0;
$t="[$messno";
foreach $z (@$message)
{
if ($z=~m/^From: (.*?)$/)
{$t.=",f=$1";}
if ($z=~m/^Subject: (.*?)$/)
{ $t.=",s=$1"; }
if ($z=~m/^To: (.*?)$/)
{$t.=",t=$1";}
#last if $q++ > 18;
}
$t.="]";
$t=~s/<//g;
$t=~s/>//g;
$t=~s/\[//g;
$t=~s/\]//g;
$x=`/usr/bin/mythtvosd --template=alert --alert_text="$t"`;
sleep 3;
return if $messno >= $maxmess;
# $message is a reference to an array of lines
}
}
open CONFIG,"</popcheck/popcheck.rc";
while (<CONFIG>)
{
chomp;
@b=split /,/;
next unless $b[0] eq "account";
&do_account (@b);
}
1
|
you also need a file in /popcheck/popcheck.rc
with entries of the following form
| Code: |
account,popserver,mbox1,password,5
account,popserver,mbox2,password,5
account,popserver,mbox3,password,5
|
account is a literal - dont edit it ...
replace popserver with you pop server host
replace mbox1 with the mailbox you want polled
replace password with you mailbox
the last numeric field is used to limit the number of emails notified....
in this case it will only show the first 5 - useful if your mailbox has 500 emails.
I have this CRONed to run every 15 mins.
comments,thoughts,suggestions -
is there a better way to do this ? |
|
| Back to top |
|
 |
cesman
Joined: 19 Sep 2003 Posts: 5044 Location: Fontana, Ca
|
Posted: Thu Jan 19, 2006 8:04 pm Post subject: |
|
|
 _________________ cesman
When the source is open, the possibilities are endless! |
|
| Back to top |
|
 |
shaamone
Joined: 30 Sep 2005 Posts: 17
|
Posted: Mon Jan 23, 2006 11:39 am Post subject: |
|
|
Fantastic
I'm wondering what changes I'd need to make to have it check my Gmail account? That doesn't run on the std pop3 port. |
|
| Back to top |
|
 |
mad_paddler
Joined: 12 Jun 2005 Posts: 464 Location: UK
|
Posted: Mon Jan 23, 2006 1:13 pm Post subject: |
|
|
With some quick googling i think you could just change the line:
| Code: | | $pop = Net::POP3->new($mail_server) |
to
| Code: | | $pop = Net::POP3->new($mail_server,<gmailport>) |
Change <gmailport> to whatever port gmail uses for pop3. Would probably be better to make a variable and have it read the port from the same file as the accounts... |
|
| Back to top |
|
 |
declanh
Joined: 24 Aug 2005 Posts: 210
|
Posted: Wed Feb 01, 2006 4:27 pm Post subject: |
|
|
Here is a tiny update
a)fixed a typo with message counts
b)and added non reporting of messages if their subject contains the word SPAM
b) is useful if you use a spam filter (like spampal) which prefilters your emails - I run spampal on my main pc and have configured popcheck.rc to fetches from it ... doing this avoids spam emails disrupting my viewing...
| Code: |
#!/usr/bin/perl
# Popcheck.pl - Declan Higgins
# Version 0.1b Jan 2006
use Net::POP3;
sub do_account ()
{
my $unwanted = shift ;
my $mail_server = shift ;
my $username = shift ;
my $password = shift ;
my $maxmess = shift ;
my $messno = 0;
$pop = Net::POP3->new($mail_server)
or die "Can't open connection to $mail_server : $!\n";
defined ($pop->login($username, $password))
or die "Can't authenticate: $!\n";
$messages = $pop->list
or die "Can't get list of undeleted messages: $!\n";
$mc = keys %$messages;
return if $mc==0;
$nmc="$username has $mc emails";
$x=`/usr/bin/mythtvosd --template=alert --alert_text="$nmc"`;
sleep 3;
foreach $msgid (keys %$messages) {
$message = $pop->get($msgid);
unless (defined $message) {
warn "Couldn't fetch $msgid from server: $!\n";
next;
}
$messno++;
$q=0;
$t="[$messno";
foreach $z (@$message)
{
if ($z=~m/^From: (.*?)$/)
{$t.=",f=$1";}
if ($z=~m/^Subject: (.*?)$/)
{ $t.=",s=$1";
next if $z=~m/SPAM/;
}
if ($z=~m/^To: (.*?)$/)
{$t.=",t=$1";}
#last if $q++ > 18;
}
$t.="]";
$t=~s/<//g;
$t=~s/>//g;
$t=~s/\[//g;
$t=~s/\]//g;
$x=`/usr/bin/mythtvosd --template=alert --alert_text="$t"`;
sleep 3;
return if $messno >= $maxmess;
# $message is a reference to an array of lines
}
}
open CONFIG,"</usr/declan/popcheck.rc";
while (<CONFIG>)
{
chomp;
@b=split /,/;
next unless $b[0] eq "account";
&do_account (@b);
}
1
|
|
|
| Back to top |
|
 |
victor97
Joined: 24 Feb 2006 Posts: 65
|
Posted: Sun Jan 27, 2008 5:38 am Post subject: |
|
|
Hi,
I tried to install this on my system, but I retrieve the following errors:
| Code: | root@mythtv:/myth/emailnotify# sh Popcheck.pl
: command not found:
: command not found:
Popcheck.pl: line 6: use: command not found
: command not found:
: command not found:
: command not found:
Popcheck.pl: line 9: syntax error near unexpected token `('
'opcheck.pl: line 9: `sub do_account ()
root@mythtv:/myth/emailnotify#
|
Do I need to install something else to make this script work?
Regards,
Victor |
|
| Back to top |
|
 |
slowtolearn
Joined: 16 Nov 2005 Posts: 1323 Location: Farmington, MI USA
|
Posted: Sun Jan 27, 2008 6:00 am Post subject: |
|
|
| victor97 wrote: | Hi,
I tried to install this on my system, but I retrieve the following errors:
| Code: | root@mythtv:/myth/emailnotify# sh Popcheck.pl
: command not found:
: command not found:
Popcheck.pl: line 6: use: command not found
: command not found:
: command not found:
: command not found:
Popcheck.pl: line 9: syntax error near unexpected token `('
'opcheck.pl: line 9: `sub do_account ()
root@mythtv:/myth/emailnotify#
|
Do I need to install something else to make this script work?
Regards,
Victor | Remove the blank line at the top of your script - The very first line should be #!/usr/bin/perl. |
|
| Back to top |
|
 |
victor97
Joined: 24 Feb 2006 Posts: 65
|
Posted: Sun Jan 27, 2008 6:54 am Post subject: |
|
|
The first line is as mentioned in the script, no blank line. (I copied and past the script from the forum into a new file and checked if the first line is #!urs/bin/perl)
Still get the same errors.
Any ideas? Must be something small I think.
Victor |
|
| Back to top |
|
 |
slowtolearn
Joined: 16 Nov 2005 Posts: 1323 Location: Farmington, MI USA
|
Posted: Sun Jan 27, 2008 7:22 am Post subject: |
|
|
| victor97 wrote: | The first line is as mentioned in the script, no blank line. (I copied and past the script from the forum into a new file and checked if the first line is #!urs/bin/perl)
Still get the same errors.
Any ideas? Must be something small I think.
Victor | Did you copy/paste from a Windoze machine? If so you'll need to run it through dos2unix - man dos2unix for details.
Just re-read your post, you need to make the file executable and call it directly (eg. ./Popcheck.pl or /path/to/Popcheck.pl). Perl should be the command interpreter, not bash (which is what your sh Popcheck.pl is using) |
|
| Back to top |
|
 |
mythedoff
Joined: 31 Jul 2006 Posts: 117
|
Posted: Sun Jan 27, 2008 10:03 am Post subject: |
|
|
| If I understand this correctly, a message could pop up during a movie or during some other activity. Correct? That could be quite bothersome. Maybe assigning the script to a button on the remote is an alternative. |
|
| Back to top |
|
 |
declanh
Joined: 24 Aug 2005 Posts: 210
|
Posted: Sun Jan 27, 2008 12:26 pm Post subject: |
|
|
| victor97 wrote: | The first line is as mentioned in the script, no blank line. (I copied and past the script from the forum into a new file and checked if the first line is #!urs/bin/perl)
Still get the same errors.
Any ideas? Must be something small I think.
Victor |
first line must be
exactly as shown
this only works if you have perl located in /usr/bin
you can check this by typing
note that the exclamation mark is essential
typing
will tell the session to run the script using /bin/sh which wont work.
you should also be able to do
which should work
Last edited by declanh on Sun Jan 27, 2008 12:39 pm; edited 1 time in total |
|
| Back to top |
|
 |
declanh
Joined: 24 Aug 2005 Posts: 210
|
Posted: Sun Jan 27, 2008 12:28 pm Post subject: |
|
|
| mythedoff wrote: | | If I understand this correctly, a message could pop up during a movie or during some other activity. Correct? That could be quite bothersome. Maybe assigning the script to a button on the remote is an alternative. |
depends on your needs - i have mine croned to run every 15 mins and the messages are not that intrusive to me - but then again i tend to read alot of emails as they come in
To me the advange is not having to remember to press a key on the remote. |
|
| Back to top |
|
 |
victor97
Joined: 24 Feb 2006 Posts: 65
|
Posted: Sun Jan 27, 2008 1:39 pm Post subject: |
|
|
Ok, I got it working. I had to give executable permission and after that I could run the script. I'm learning every day!
But it is not working perfect. For most of my e-mails it won't show the subject of the message. Not sure why.
Also my gmail account is not working. I tried what has been raised earlier by mad_paddler, but that is not working for me.
Do you guys have the same behaviour? Besides this, I really loving it! Great feature! |
|
| Back to top |
|
 |
mjl
Joined: 12 Jun 2005 Posts: 3158 Location: Warwick, RI
|
Posted: Sun Jan 27, 2008 7:28 pm Post subject: |
|
|
Hi,
Looks to be a rather cool "feature"
Only thing that may not have been clear is:
| Code: | | open CONFIG,"</usr/declan/popcheck.rc"; |
needs to be set for the individuals setup
You did hint to put popcheck.rc into popcheck/ folder which is a good location for both the popcheck.pl & popcheck.rc
ie,
/home/mythtv/popcheck/popcheck.rc
I think it does need a test message available in case there isn't any mail when you first run it as it just exits gracefully if nothing has arrived so one can only assume all is well.
Thank you
Mike
I do see this error if I add the #!/usr/bin/perl -w
Name "main::q" used only once: possible typo at popcheck.pl line 40. |
|
| Back to top |
|
 |
mjl
Joined: 12 Jun 2005 Posts: 3158 Location: Warwick, RI
|
Posted: Sun Jan 27, 2008 9:17 pm Post subject: |
|
|
| mjl wrote: | Hi,
Looks to be a rather cool "feature"
Only thing that may not have been clear is:
| Code: | | open CONFIG,"</usr/declan/popcheck.rc"; |
needs to be set for the individuals setup
You did hint to put popcheck.rc into popcheck/ folder which is a good location for both the popcheck.pl & popcheck.rc
ie,
/home/mythtv/popcheck/popcheck.rc
I think it does need a test message available in case there isn't any mail when you first run it as it just exits gracefully if nothing has arrived so one can only assume all is well.
Thank you
Mike
I do see this error if I add the #!/usr/bin/perl -w
Name "main::q" used only once: possible typo at popcheck.pl line 40. |
Seems to run, however I see no osd on the tv... what did I mis..... |
|
| Back to top |
|
 |
|