| | 331 | |
| | 332 | sub sendmessage |
| | 333 | { |
| | 334 | my ($req, $err, $flags) = @_; |
| | 335 | |
| | 336 | return fail($err, 315) if $LJ::DISABLED{user_messaging}; |
| | 337 | |
| | 338 | return undef unless authenticate($req, $err, $flags); |
| | 339 | my $u = $flags->{'u'}; |
| | 340 | |
| | 341 | return fail($err, 314) unless $u->get_cap('paid'); |
| | 342 | my $msg_limit = LJ::get_cap($u, "usermessage_length"); |
| | 343 | |
| | 344 | my @errors; |
| | 345 | |
| | 346 | my $subject_text = LJ::strip_html($req->{'subject'}); |
| | 347 | return fail($err, 208, 'subject') |
| | 348 | unless LJ::text_in($subject_text); |
| | 349 | |
| | 350 | # strip HTML from body and test encoding and length |
| | 351 | my $body_text = LJ::strip_html($req->{'body'}); |
| | 352 | return fail($err, 208, 'body') |
| | 353 | unless LJ::text_in($body_text); |
| | 354 | |
| | 355 | my ($msg_len_b, $msg_len_c) = LJ::text_length($body_text); |
| | 356 | return fail($err, 212, 'found: ' . LJ::commafy($msg_len_c) . ' characters, it should not exceed ' . LJ::commafy($msg_limit)) |
| | 357 | unless ($msg_len_c <= $msg_limit); |
| | 358 | |
| | 359 | my @to = (ref $req->{'to'}) ? @{$req->{'to'}} : ($req->{'to'}); |
| | 360 | return fail($err, 200) unless scalar @to; |
| | 361 | |
| | 362 | # remove duplicates |
| | 363 | my %to = map { lc($_), 1 } @to; |
| | 364 | @to = keys %to; |
| | 365 | |
| | 366 | my @msg; |
| | 367 | BML::set_language('en'); # FIXME |
| | 368 | |
| | 369 | foreach my $to (@to) { |
| | 370 | my $tou = LJ::load_user($to); |
| | 371 | return fail($err, 100, $to) |
| | 372 | unless $tou; |
| | 373 | |
| | 374 | my $msg = LJ::Message->new({ |
| | 375 | journalid => $u->userid, |
| | 376 | otherid => $tou->userid, |
| | 377 | subject => $subject_text, |
| | 378 | body => $body_text, |
| | 379 | parent_msgid => defined $req->{'parent'} ? $req->{'parent'} + 0 : undef, |
| | 380 | userpic => $req->{'userpic'} || undef, |
| | 381 | }); |
| | 382 | |
| | 383 | push @msg, $msg |
| | 384 | if $msg->can_send(\@errors); |
| | 385 | } |
| | 386 | return fail($err, 203, join('; ')) |
| | 387 | if scalar @errors; |
| | 388 | |
| | 389 | foreach my $msg (@msg) { |
| | 390 | $msg->send(\@errors); |
| | 391 | } |
| | 392 | |
| | 393 | return { 'sent_count' => scalar @msg, |
| | 394 | (@errors ? ('last_errors' => \@errors) : () ), |
| | 395 | }; |
| | 396 | } |
| | 397 | |