CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
substitute \' in mail with '

 



saaiber
Novice

Sep 2, 2006, 9:33 AM

Post #1 of 8 (4093 views)
substitute \' in mail with ' Can't Post

Hello,

I use a pl script to parse my incoming e-mails in a ticket system. Mails are piped to this script. Now I have this problem. From our forum contactform mails are sent to this script. But the forum sends e.g. an ' as \'. When the scripts tries to parse this \' I get an error and the mailbody isn't saved in my database.

I already put this line in the scripts to substitute ' in " but it isn't right as it should be:


Code
$body =~ s/'/"/g;


It allows me to work with my ticket system but off course it changes every ' into ".

In the same script I saw that URL's aren't parsed in the correct way too:

http://www.domain.be/phpBB2/viewtopic.php?p=232143#232143 will be parsed as
http://www.domain.be/phpBB2/viewtopic.php?p#2143#232143

I have all these lines in the script:


Code
#right and left trim 
$body =~ s/<script[^>]*?>.*?<\/script>//g;
$body =~ s/<[\/!]*?[^<>]*?>//g;
$body =~ s/'/\\'/g;
$body =~ s/--$//g;
$body =~ s/[\n|<br>]*$//gi;
$body =~ s/^(.*)>/<$1>/i;
$body =~ s/'/"/g;
$body =~ s/&quot;/"/g;
#$body =~ s/^[\n|<br>]*//gi;


The last line is commented because it cut off the first character of all sentences beginning with b or r.

Can someone explain me how I could resolve this problem an d eventually explain me what these lines exactly do? Is there a possibility to have \' replaced by '?

Sorry for my bad English, it is not my native language.

Grtz,

Nic


KevinR
Veteran


Sep 2, 2006, 10:44 AM

Post #2 of 8 (4092 views)
Re: [saaiber] substitute \' in mail with ' [In reply to] Can't Post

this line appears to be converting ' into \'

$body =~ s/'/\\'/g;

comment it out and run the script and see how it works. What type of database are you refering to? "sn't saved in my database."

#right and left trim
$body =~ s/<script[^>]*?>.*?<\/script>//g;
$body =~ s/<[\/!]*?[^<>]*?>//g;
#$body =~ s/'/\\'/g;
$body =~ s/--$//g;
$body =~ s/(\n|<br>)*$//gi;
$body =~ s/^(.*)>/<$1>/i;
#$body =~ s/'/"/g;
$body =~ s/&quot;/"/g;
$body =~ s/^(\n|<br>)*//gi;
-------------------------------------------------


saaiber
Novice

Sep 2, 2006, 1:42 PM

Post #3 of 8 (4089 views)
Re: [KevinR] substitute \' in mail with ' [In reply to] Can't Post

Great, that did the job. Now that you mention it, I understand the line and see what happened. The database I was referring to, is the MySQL database of the ticket system that I use.

KevinR, a great thank you!

Grtz,

Nic


saaiber
Novice

Sep 2, 2006, 2:03 PM

Post #4 of 8 (4088 views)
Re: [KevinR] substitute \' in mail with ' [In reply to] Can't Post

I can't follow anymore. If I comment out the line you gave me, the mails from the form are parsed as it should be, because message body already has a \ before every '.

But when I send a normal mail with a ' in the message body, the ticket system collapses again.

It seems that I inverted the problem, regular mail isn't working anymore but the mail sent by the mailform is working. :-(

Maybe I'd better find a solution in the PHP script which sends out the mail from the phpBB2 board so that there's no more \ before each '.

This is driving me crazy ;)

Nic


(This post was edited by saaiber on Sep 2, 2006, 3:55 PM)


KevinR
Veteran


Sep 2, 2006, 3:23 PM

Post #5 of 8 (4087 views)
Re: [saaiber] substitute \' in mail with ' [In reply to] Can't Post

Not sure where to problem is, but I have a feeling you should be using placeholders (parameter binding) for data storage in your MySQL database.

http://search.cpan.org/~timb/dbd-summaries/dbd-mysql-msql.pod#Parameter_Binding
-------------------------------------------------


rork
User

Sep 5, 2006, 2:20 AM

Post #6 of 8 (4075 views)
Re: [saaiber] substitute \' in mail with ' [In reply to] Can't Post

The mail from the forum has \' in it, the regexp substitutes the ' in \' so it will end in \\' where the first \ escapes the second \ and leaves a normal'. Now the text has a regular ' and the script crashes.

Uncomment the line and all original ' will create the problem. You have to test if an ' is escaped allready but it might be easier to also substitute the original \:

try this one:

Code
s/\\?'/\\'/g


But what if someone sends you some nice ASCII art by mail? \\'O'//
Edit:
Well most of it would be destroyed by the other substitution operators but not the backslashes. Maybe < $body =~ s/<[\/!]*?[^<>]*?>//g; > doesn't exactly do what you want.

(I took another look because I wondered how it handled normal backslashes in the text but it doesn't do anything with them it seems)
--
Don't reinvent the wheel, use it, abuse it or hack it.

(This post was edited by rork on Sep 5, 2006, 10:11 AM)


saaiber
Novice

Sep 5, 2006, 2:11 PM

Post #7 of 8 (4065 views)
Re: [rork] substitute \' in mail with ' [In reply to] Can't Post

Thanks for your suggestions. I managed to arrange the problem for now. ;) I did a stripslash on the $comments field in my form script (php) and apllied your suggestions and now it works.

' is accepted and \' is also accepted, the only problem still would be a \\' but since nobody would use this combination (I hope Wink ) in regular mail and neither the forumsoftware would use it, I think I'm safe for now.

Thank you KevinR and rork!

Greetings,

Nic


(This post was edited by saaiber on Sep 5, 2006, 2:11 PM)


saaiber
Novice

Sep 21, 2006, 5:44 AM

Post #8 of 8 (4023 views)
Re: [saaiber] substitute \' in mail with ' [In reply to] Can't Post


Code
sub decode_qp ($) 
{
my $res = shift;
$res =~ s/\r\n/\n/g; # normalize newlines
$res =~ s/[ \t]+\n/\n/g; # rule #3 (trailing space must be deleted)
$res =~ s/=\n//g; # rule #5 (soft line breaks)
if (ord('A') == 193) { # EBCDIC style machine
if (ord('[') == 173) {
$res =~ s/=([\da-fA-F]{2})/Encode::encode('cp1047',Encode::decode('iso-8859-1',pack("C", hex($1))))/ge;
}
elsif (ord('[') == 187) {
$res =~ s/=([\da-fA-F]{2})/Encode::encode('posix-bc',Encode::decode('iso-8859-1',pack("C", hex($1))))/ge;
}
elsif (ord('[') == 186) {
$res =~ s/=([\da-fA-F]{2})/Encode::encode('cp37',Encode::decode('iso-8859-1',pack("C", hex($1))))/ge;
}
}
else { # ASCII style machine
$res =~ s/=([\da-fA-F]{2})/pack("C", hex($1))/ge;
}
$res;
}

1;


This is being used to prevent =20 line breaks in incoming e-mails, now I guess that this line:

Code
else { # ASCII style machine 
$res =~ s/=([\da-fA-F]{2})/pack("C", hex($1))/ge;
}

is responsible for the replacement.

Big problem: any url wich ends with .php?p=2038#2038 in the mails is shown as .php?p%038#2038. This, off course, makes the url useless in the mail.

Is there any possibility that I can tell this system not to touch URL's?

This thing is driving me crazy ;)

Nic

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives