LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-25-2010, 04:19 AM   #1
d1s4st3r
Member
 
Registered: May 2004
Location: Italy
Posts: 70

Rep: Reputation: 16
Perl: how to save an e-mail attachment on disk keeping the "&" character (no "%26"!!)


Hi,

I'm working on a Perl script that handles e-mails and attachments using MIME::Tools.

This script saves the attachments on filesystem, but whenever it finds an "&" (ampersand) into the filename (the filename is given by the "path" method of the "MIME::Body" class), it is changed into "%26" and then stored on disk.

To make an example, if an attachment is named "You & me.pdf", it is saved on disk as "You %26 me.pdf".

I also tried to change that char with a regexp and setting again the path passing the new (and working) value as a parameter to the "path" method, and if I print the value it is right, but on the filesystem is *always* stored with the "%26" string.

Any suggestion on how to bypass this problem?

Thanks in advance for any precious help.

:-D
 
Old 02-25-2010, 04:34 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by d1s4st3r View Post
Hi,

I'm working on a Perl script that handles e-mails and attachments using MIME::Tools.

This script saves the attachments on filesystem, but whenever it finds an "&" (ampersand) into the filename (the filename is given by the "path" method of the "MIME::Body" class), it is changed into "%26" and then stored on disk.

To make an example, if an attachment is named "You & me.pdf", it is saved on disk as "You %26 me.pdf".

I also tried to change that char with a regexp and setting again the path passing the new (and working) value as a parameter to the "path" method, and if I print the value it is right, but on the filesystem is *always* stored with the "%26" string.

Any suggestion on how to bypass this problem?

Thanks in advance for any precious help.

:-D
Why do you think people here should guess what your code is ?

Regarding '&' vs '%26' - file names with special characters are not shell friendly. For example, if in a shell you type

cat some&thing

, the shell will do the following:

Code:
sergei@amdam2:~/junk> cat some&thing
[1] 13933
cat: some: No such file or directory

bash: thing: command not found
[1]+  Exit 1                  cat some
- first it'll try to execute 'cat some' in the background (because of '&'), then it will try to execute 'thing'.

You can still have almost any character in a filename - except for '/' which is, of course, path element separator. without seeing you code it's unnecessarily difficult to to guess what exactly needs to be changed.
 
Old 02-25-2010, 05:07 AM   #3
d1s4st3r
Member
 
Registered: May 2004
Location: Italy
Posts: 70

Original Poster
Rep: Reputation: 16
Question

Yeah, I know that concepts, and I know that an ampersand in a file name may be "dangerous" somehow (even if the shell normally escapes it).

But as long as I can do
Code:
touch 'This file contains an &.pdf'
I'd wish the Perl script to do the same.

I haven't written the Perl script by myself, I'm just modifying it in some parts, by the way the situation is the following:

there is a while loop looping through the parts of an Entity (parsed with MIME::Parser), so there is the following line:
Code:
[...]
$fileName1 = $entity->parts($i)->head->recommended_filename;
[...]
if I print that variable, it prints "You & me.pdf" (and this is right).
Moreover there is this line:
Code:
[...]
$fileName2 = $entity->parts($i)->bodyhandle->path;
[...]
and if I print that second variable, it prints "/some_path/msg-1267095268-5557-0/You %26 me.pdf" (and here you can see the "%26" instead of the "&"). That absolute path is where the e-mail attachment is saved.

Now, if I try to change the "&" into something else (for example, "and") with:
Code:
$fileName2 = $entity->parts($i)->bodyhandle->path;
$fileName2 =~ s/%26/and/g;
$entity->parts($i)->bodyhandle->path($fileName2);
and print the variable again, now it prints "/some_path/msg-1267095268-5557-0/You and me.pdf" (and this is OK!), but the real file on the filesystem *is still saved* with the "%26" in its name.

What a crap...
 
Old 02-25-2010, 05:30 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by d1s4st3r View Post
...
Code:
[...]
$fileName2 = $entity->parts($i)->bodyhandle->path;
[...]
and if I print that second variable, it prints "/some_path/msg-1267095268-5557-0/You %26 me.pdf" (and here you can see the "%26" instead of the "&"). That absolute path is where the e-mail attachment is saved.
...
What a crap...
If something is saved, it is a file. If it is a file, the file can be renamed - 'perldoc -f rename' is your friend.

So, why's the fuss about crap ?
 
Old 02-25-2010, 11:05 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,342

Rep: Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746
Just because you can
[quote]
touch 'This file contains an &.pdf'
[/quote ]
doesn't mean you should.
Using special chars like space, & etc in filenames WILL cause you endless grief...
as I said in the other version of your qn in another forum here.
 
Old 09-29-2010, 09:30 PM   #6
sheer
LQ Newbie
 
Registered: Sep 2010
Posts: 1

Rep: Reputation: 0
Fix..

If it really irritates you, just edit your instance of MIME::Parser::Filer to add & to the list of characters allowed in evil_filename. Or alternately, you can derive a class from MIME::Parser::Filer and jump thruogh a lot of hoops. ;-)

I changed mine to return 1 if ($name =~ /[^-A-Z0-9_+=.,@\#\&\$\% ]/i); # Only allow good chars
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Chromium doesn't save "default browser"+ Desktop isn't "snappy" ohadbasan Arch 1 01-25-2010 08:07 PM
automatic mail of "Last User Login Logs" & "AIX Logs" nishith AIX 3 12-04-2009 12:42 PM
Perl - Can't use string ("html") as an ARRAY ref while "strict refs" OldGaf Programming 9 08-11-2009 11:14 AM
"Failed Dependency error" while installing RPM for "DateTime" perl modules giridhargopal.cj Linux - Newbie 7 11-19-2008 12:05 AM
problem "make"ing gtk+ "/usr/bin/env: perl -w" caid Linux - Newbie 8 07-29-2005 04:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration