Re: [gentoo-user] PERL and 'mail notification'
On Monday 08 September 2003 02:12, Doug Weimer wrote: > On Sun, 2003-09-07 at 08:01, Jason Stubbs wrote: > > > > # Can I combine the following two lines in any way? > > # $mail_body = @$mail->body(); does not work. > > $mail_body_ref = $mail->body(); > > @mail_body = @$mail_body_ref; > > @{$mail->body()} works here. If you want, you can also use > foreach(@{$mail->body(}) below. Okay, that worked great! > > @body_text = split("\n", join(" ", @body_text)); > > > > @ntfy_mail = ("Date: $msg_date", > > "From: $msg_from", > > "Subject:$msg_subj", > > "\n"); > > > > # Can I combine this into one line? Can I combine it with the above? > > for ($i = 0; $i < 20; $i++) { > > @ntfy_mail = (@ntfy_mail, "@body_text[$i]\n"); > > } > > Since you only want 20 lines you can use @body_text[0 .. 19] in the > join. If you want to reduce everything to a few lines, you can use a > second join to add the newlines: > > $text = join("\n", split("\n", join(" ", @body_text[0 .. 19]))); The problem with this is that the original @body_text sometimes has more than one newline per element. Thus, I was getting up to 40 lines output by doing something similar previously. However... @body_text = split("\n", join("", @body_text)); @ntfy_mail = ("Date: $msg_date", "From: $msg_from", "Subject:$msg_subj", "\n", join("\n", @body_text[0..19])); This does exactly what I want. And changing the " " to "" in the initial join fixed both bugs too! For the record, the bugs were: 1) A preceding the first body line. 2) Certain characters of Japanese text were being corrupted. Thanks for everybody's help! Best regards, Jason -- [EMAIL PROTECTED] mailing list
Re: [gentoo-user] PERL and 'mail notification'
On Sun, 2003-09-07 at 08:01, Jason Stubbs wrote: > # Can I combine the following two lines in any way? > # $mail_body = @$mail->body(); does not work. > $mail_body_ref = $mail->body(); > @mail_body = @$mail_body_ref; @{$mail->body()} works here. If you want, you can also use foreach(@{$mail->body(}) below. > @body_text = split("\n", join(" ", @body_text)); > > @ntfy_mail = ("Date: $msg_date", > "From: $msg_from", > "Subject:$msg_subj", > "\n"); > > # Can I combine this into one line? Can I combine it with the above? > for ($i = 0; $i < 20; $i++) { > @ntfy_mail = (@ntfy_mail, "@body_text[$i]\n"); > } > Since you only want 20 lines you can use @body_text[0 .. 19] in the join. If you want to reduce everything to a few lines, you can use a second join to add the newlines: $text = join("\n", split("\n", join(" ", @body_text[0 .. 19]))); or use map to keep the result as an array: @body_text = map { $_ ."\n" } split( ); Good luck, Doug signature.asc Description: This is a digitally signed message part
Re: [gentoo-user] PERL and 'mail notification'
Jason Stubbs ([EMAIL PROTECTED]) scribbled: > On Tuesday 02 September 2003 10:50, Marshal Newrock wrote: > > On Mon, 1 Sep 2003, Jason wrote: > > > above, what I want to do is: > > > > > > @sometext = ("text\n\with\nmore\nthan\none\nnewline\n", > > > "some\nother\nline\n"); > > > @newtext = ("text\n", "with\n", "more\n", "than\n", "one\n", "newline\n", > > > "some\n", > > > "other\n", "line\n"); > > > #Do something magic to @sometext here to make it turn into @newtext > > > > Just two things: > > 1) perldoc -f split > > 2) man perlfunc > > > > Perl typically excels at text processing. You can also look at the > > 'join' command if you wish. But certainly 'man perlfunc' along with > > 'perldoc -f $function_name' will let you go far with your scripts. :) > > Sorry for the extremely late reply. Thanks for the info. I eventually used it > to write a working script, which I have included below. > > I was using "man perl*" quite often. I haven't read all the way through > anything except perlintro yet though. I find when looking up language > reference, you have to know what you want to be able to find it. That is to > say, I don't tend to search for how a function works unless I expect the > function to exist. Needless to say, I didn't think the function to exist - > even though I knew that perl's forte is text-processing. This is my first > attempt at anything in perl, too! > > Still a bit of functionality to add before I'll be happy - but I'll do that as > needed. There's also two bugs, one small and one big, but I'll work on those > independently until I get too frustrated. ;-) > > I do have a couple of questions which I haven't been able to find the answer > to. I've embedded them within the code. This may be OT for this thread, but there may be a simpler way to accomplish the same results. iirc, you said you were using procmail to filter incoming mail and wanted to use a perl script to send notification to another email address. I do the exact same thing, but without the perl script. Take a look at this slice of my .procmailrc: # Begin ~/.procmailrc [EMAIL PROTECTED] #- # BEGIN SAFETY NET # # All mail to Maildir first # :0c Maildir/ # END SAFETY NET # # Mail from work # :0hc # h=header only, c=copy * ^From.*(work|slavedriver|ballandchain) # usual filter !$FWD_URGENT # send the header to cellphone :0 # deposit msg to sep dir * ^From.*(work|slavedriver|ballandchain) WorkMail/ # End ~/.procmailrc I am by no means even remotely good at procmail scripts, I just hacked this once and stuck with it. All it does is copy the header info of the relevant message to my cell phone's email address. This way I know right away if I need to check mail (or if innaccessible, call the person). btw - this only works well if people use short, clear subject lines. All you'll get is the sender's email address and subj. HTH, Cooper. -- [EMAIL PROTECTED] mailing list
Re: [gentoo-user] PERL and 'mail notification'
On Tuesday 02 September 2003 10:50, Marshal Newrock wrote: > On Mon, 1 Sep 2003, Jason wrote: > > above, what I want to do is: > > > > @sometext = ("text\n\with\nmore\nthan\none\nnewline\n", > > "some\nother\nline\n"); > > @newtext = ("text\n", "with\n", "more\n", "than\n", "one\n", "newline\n", > > "some\n", > > "other\n", "line\n"); > > #Do something magic to @sometext here to make it turn into @newtext > > Just two things: > 1) perldoc -f split > 2) man perlfunc > > Perl typically excels at text processing. You can also look at the > 'join' command if you wish. But certainly 'man perlfunc' along with > 'perldoc -f $function_name' will let you go far with your scripts. :) Sorry for the extremely late reply. Thanks for the info. I eventually used it to write a working script, which I have included below. I was using "man perl*" quite often. I haven't read all the way through anything except perlintro yet though. I find when looking up language reference, you have to know what you want to be able to find it. That is to say, I don't tend to search for how a function works unless I expect the function to exist. Needless to say, I didn't think the function to exist - even though I knew that perl's forte is text-processing. This is my first attempt at anything in perl, too! Still a bit of functionality to add before I'll be happy - but I'll do that as needed. There's also two bugs, one small and one big, but I'll work on those independently until I get too frustrated. ;-) I do have a couple of questions which I haven't been able to find the answer to. I've embedded them within the code. #!/usr/bin/perl use Mail::Internet; use HTML::Parser; $mail = Mail::Internet->new(*STDIN); $mail_head = $mail->head(); $msg_date = $mail_head->get("Date"); $msg_from = $mail_head->get("From"); $msg_subj = $mail_head->get("Subject"); $mail->tidy_body(); # Can I combine the following two lines in any way? # $mail_body = @$mail->body(); does not work. $mail_body_ref = $mail->body(); @mail_body = @$mail_body_ref; @body_text = ""; $body_pars = HTML::Parser->new( text_h => [sub { $text = shift; if ($text =~ /[0-9a-zA-Z]/) { @body_text = (@body_text, $text); } }, 'dtext']); foreach (@mail_body) { $body_pars->parse("$_"); } $body_pars->eof(); @body_text = split("\n", join(" ", @body_text)); @ntfy_mail = ("Date: $msg_date", "From: $msg_from", "Subject:$msg_subj", "\n"); # Can I combine this into one line? Can I combine it with the above? for ($i = 0; $i < 20; $i++) { @ntfy_mail = (@ntfy_mail, "@body_text[$i]\n"); } open(XMAIL, "| mail -s 'Mail Notification' [EMAIL PROTECTED]"); print XMAIL @ntfy_mail; close(XMAIL); Thanks for all your help! Jason -- [EMAIL PROTECTED] mailing list
RE: [gentoo-user] PERL and 'mail notification'
On Mon, 1 Sep 2003, Jason wrote: > >The problem here is you have to define a "line". A line of email can have > >up to 1000 (I think) characters. Postfix will split the line at 990 > >characters by default. And then there's all the extra information added > >by using HTML. > > All the extra HTML information is removed by the script already - except for > the stylesheet > information in certain situations due to the bug. To be more specific about my > question > above, what I want to do is: > > @sometext = ("text\n\with\nmore\nthan\none\nnewline\n", > "some\nother\nline\n"); > @newtext = ("text\n", "with\n", "more\n", "than\n", "one\n", "newline\n", > "some\n", > "other\n", "line\n"); > #Do something magic to @sometext here to make it turn into @newtext > > > Probably have to write a routine to do searches of \n to make a new array and > then > replace the old one with it, right? I was thinking that it would be a little > slow like that, > however, and was wondering if there was some better way. Just two things: 1) perldoc -f split 2) man perlfunc Perl typically excels at text processing. You can also look at the 'join' command if you wish. But certainly 'man perlfunc' along with 'perldoc -f $function_name' will let you go far with your scripts. :) Another option would be to use the 's' operator (man perlop) to change all newlines to spaces. eg: $sometext =~ s/\n/ /g You'd probably want to add a newline to end if you do this. -- Marshal Newrock, unemployed Linux user in Lansing, MI Caution: Product will be hot after heating -- [EMAIL PROTECTED] mailing list
RE: [gentoo-user] PERL and 'mail notification'
>= Original Message From Marshal Newrock <[EMAIL PROTECTED]> = >On Mon, 1 Sep 2003, Jason Stubbs wrote: > >> The only other thing which I haven't had time to really check into yet - too >> much time spent reading man pages! - is that it seems 1 element of @bodytext >> has more than one line of text; that is to say, each of element of @bodytext >> has >=1 newline. This results in @bodytext[1..5] returning about 30 lines of >> text with the test email I was using. How can I use perl to get just 5 (or >> 10) lines of text? > >The problem here is you have to define a "line". A line of email can have >up to 1000 (I think) characters. Postfix will split the line at 990 >characters by default. And then there's all the extra information added >by using HTML. All the extra HTML information is removed by the script already - except for the stylesheet information in certain situations due to the bug. To be more specific about my question above, what I want to do is: @sometext = ("text\n\with\nmore\nthan\none\nnewline\n", "some\nother\nline\n"); @newtext = ("text\n", "with\n", "more\n", "than\n", "one\n", "newline\n", "some\n", "other\n", "line\n"); #Do something magic to @sometext here to make it turn into @newtext Probably have to write a routine to do searches of \n to make a new array and then replace the old one with it, right? I was thinking that it would be a little slow like that, however, and was wondering if there was some better way. Regards, Jason - Shanje.NET The webmaster's 1st choice for Windows 2003 Hosting http://www.Shanje.NET/ - -- [EMAIL PROTECTED] mailing list
Re: [gentoo-user] PERL and 'mail notification'
On Mon, 1 Sep 2003, Jason Stubbs wrote: > The only other thing which I haven't had time to really check into yet - too > much time spent reading man pages! - is that it seems 1 element of @bodytext > has more than one line of text; that is to say, each of element of @bodytext > has >=1 newline. This results in @bodytext[1..5] returning about 30 lines of > text with the test email I was using. How can I use perl to get just 5 (or > 10) lines of text? The problem here is you have to define a "line". A line of email can have up to 1000 (I think) characters. Postfix will split the line at 990 characters by default. And then there's all the extra information added by using HTML. -- Marshal Newrock, unemployed Linux user in Lansing, MI Caution: Product will be hot after heating -- [EMAIL PROTECTED] mailing list
Re: [gentoo-user] PERL and 'mail notification'
Well, Background: I'm writing a script to send a notification mail to an e-mail address based on an incoming mail. I'm using procmail to decide which messages should be notified about and to pipe them one by one into said script. [edit]I wish I could perform edits like in the forums ;-)[/edit] Here's what I've got so far. It mostly works but a couple of things left to do. Obviously, I still have to send the notification mail out. I'll try to do that in Perl but will probably end up using mailx so I don't have to bother with the mail headers. I also have to detect whether there are several MIME sections and deal with that - if there's a plain text one grab that else use the html. Those things I should be able to do by myself. #!/usr/bin/perl use Mail::Internet; use HTML::Parser; $mail = Mail::Internet->new(*STDIN); $mail_head = $mail->head(); $msg_date = $mail_head->get("Date"); $msg_from = $mail_head->get("From"); $msg_subj = $mail_head->get("Subject"); $mail->tidy_body(); $mail_body_ref = $mail->body(); @mail_body = @$mail_body_ref; @body_text = ""; $body_pars = HTML::Parser->new( text_h => [sub { $text = shift; if ($text =~ /[0-9a-zA-Z]/) { @body_text = (@body_text, $text); } }, 'dtext']); foreach (@mail_body) { $body_pars->parse("$_\n"); } $body_pars->eof(); $ntfy_head = Mail::Header->new(); $ntfy_head->add("From:", "[EMAIL PROTECTED]"); $ntfy_head->add("To:", "[EMAIL PROTECTED]"); $ntfy_head->add("Subject:", "Mail Notification"); $ntfy = Mail::Internet->new(Header => $ntfy_head); @ntfy_mail = ("Date: $msg_date", "From: $msg_from", "Subject:$msg_subj", "\n", @body_text[1..5], "\n"); $ntfy->body(@ntfy_mail); $ntfy->print(); What I am having problems with is getting around a known bug in HTML::Parser. It doesn't deal with embedded style sheets in HTML (or however they're correctly termed). Does anybody have any suggestions on how I can get around that? The mail I was using to test the script was a "Core Java Technologies Newsletter" but running any mail with embedded HTML that uses an embedded style sheet will show up what I mean. The only other thing which I haven't had time to really check into yet - too much time spent reading man pages! - is that it seems 1 element of @bodytext has more than one line of text; that is to say, each of element of @bodytext has >=1 newline. This results in @bodytext[1..5] returning about 30 lines of text with the test email I was using. How can I use perl to get just 5 (or 10) lines of text? Regards, Jason -- [EMAIL PROTECTED] mailing list