LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Perl: how to save an e-mail attachment on disk keeping the "&" character (no "%26"!!) (http://www.linuxquestions.org/questions/programming-9/perl-how-to-save-an-e-mail-attachment-on-disk-keeping-the-and-character-no-26-a-791450/)

d1s4st3r 02-25-2010 04:19 AM

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

Sergei Steshenko 02-25-2010 04:34 AM

Quote:

Originally Posted by d1s4st3r (Post 3876131)
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.

d1s4st3r 02-25-2010 05:07 AM

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... :(

Sergei Steshenko 02-25-2010 05:30 AM

Quote:

Originally Posted by d1s4st3r (Post 3876160)
...
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 ?

chrism01 02-25-2010 11:05 PM

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.

sheer 09-29-2010 09:30 PM

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


All times are GMT -5. The time now is 05:28 AM.