Hey everyone!
I am back.
I have a script that handles the private messaging for a site I am working on and I created a subroutine to handle the sending of a message.
I want to check if the "to" field was left blank, contains a user name that doesn't exist or has invalid characters in it.
Everything worked fine, until I integrated the sending of messages to multiple recipients. What I did was create another subroutine to "prepare" the sending. ie: loop through the different user names, and extract proceeding, or trailing whitespace, and them send it to the "send message" subroutine.
Now for some reason it cannot tell if the user left the "to" field blank.
No matter what perl considers the "$recepient" to be true. I've tried:
-
-
if(!$recepient){
-
do something..
-
}
-
-
# AND
-
-
if($recepient !~ /[a-zA-Z]/g){
-
really bad but was just testing..
-
}
-
-
# AND
-
if(length $recepient <1){
-
do something
-
}
-
Nothing works...
If I open a message and leave the "to" field blank, and click send, I just get the "Message Sent!" message...
I can't understand it. I get the appropriate error message for both of the other "if" checks...
Here is the code.
-
my $error = qq~<font color="red"><small><ul>~;
-
-
sub sendMessage {
-
-
my ($recepient, $title, $body) = @_;
-
-
$sth = $dbh->prepare(qq~SELECT id, online, email, newsletter FROM users WHERE username=?~);
-
$sth->execute($recepient)|| Tools->listErr( $sth->errstr );
-
my @rece = $sth->fetchrow_array;
-
$sth->finish;
-
-
if(!$rece[0] && $recepient){
-
$error .= qq~<li>That user: $recepient, does not exist. Make sure you punctuated correctly</li>~;
-
}
-
-
if($recepient eq '' || $recepient eq undef){
-
$error .= qq~<li>You left the "To" field blank!</li>~;
-
}
-
-
if($recepient =~ /[^a-zA-Z0-9_\-\s]/){
-
$error .= qq~<li>You have invalid characters in the "To" field.<br /> You may only use a-z, A-Z, 0-9, spaces,<br /> underscores and dashes.</li>~;
-
}
-
-
unless(length $error > 29){
-
-
$sth = $dbh->prepare(qq~ INSERT INTO pms (user, recepient, title, body) values(?, ?, ?, ?)~);
-
$sth->execute($user->{id}, $rece[0], $title, $body)|| Tools->listErr( $sth->errstr );
-
-
if(($rece[1] == 0) && ($rece[3] == 1)){
-
my $message = qq~You have a new "Private Message" from $user->{username}, waiting for you on \nLiveAudioMag!\n\nClick The link below to log-in and read your message\nhttp://www.liveaudiomag.com/cgi-bin/index.cgi\n\nDo not reply to this message, it was auto-generated and the replies are not read. \nTo unsubscribe to these notifications, Uncheck the "Keep me up to date on LiveAudioMag news, and features." checkbox in your profile edit page.~;
-
Tools->sendmail('"LiveAudioMag" <webmaster@liveaudiomag.com>', "$rece[2]", 'New Private Message!', "$message")||
-
Tools->listErr( $! );
-
}
-
}
-
return $error;
-
}
-
-
-
-
sub prepSend {
-
-
my @recepient = split(/,/, $q->param('to'));
-
my $title = $q->param('subject')|| "none";
-
my $body = $q->escapeHTML( $q->param('body') );
-
-
for(@recepient){
-
-
$_ =~ s/^\s|\s$//g;
-
-
if(length &sendMessage($_, $title, $body) > 29){
-
$error .= qq~<br />Click "Back" on your browser to retrieve message, and resolve error.</ul></small></font>~;
-
-
Tools->create2( 'compose', 'pm', {
-
user => $user,
-
cgi => $q,
-
error => $error,
-
contacts => $contacts
-
}) || Tools->listErr( $Tools::t->error );
-
exit(0);
-
}
-
}
-
-
$error = qq~<font color="blue">Message Sent!</font>~;
-
&openInbox($error);
-
}
It's just a snippet, but I'm SURE the problem lies within this code.
When someone sends a message the "prepSend" sub is called.
Before I broke up the work, and had one sub to handle it all (only taking one user name) it worked fine.
The error WILL work if the user name is not in the database, or contains invalid characters, but it will not seem to notice that the "to" field is blank.
Is this something to do with arrays?
I was under the impression that null, undef, 0, '' are all the same thing to perl; false.
Anyone see my mistake? (Other than the misspelling of recipient..hehehe)
Thanks in advance...