1<?php
2
3require_once(dirname(__FILE__)."/../pfccommand.class.php");
4
5class pfcCommand_send extends pfcCommand
6{
7  function run(&$xml_reponse, $p)
8  {
9    $clientid    = $p["clientid"];
10    $param       = $p["param"];
11    $sender      = $p["sender"];
12    $recipient   = $p["recipient"];
13    $recipientid = $p["recipientid"];
14
15    $c  =& pfcGlobalConfig::Instance();
16    $u  =& pfcUserConfig::Instance();
17    $ct =& pfcContainer::Instance();
18
19    $nick = $ct->getNickname($u->nickid); //phpFreeChat::FilterSpecialChar($sender);
20    $text = phpFreeChat::PreFilterMsg($param);
21
22    //        $offline = $container->getMeta("offline", "nickname", $u->privmsg[$recipientid]["name"]);
23
24    // if this channel is a pv (one to one channel),
25    // first of all, check if the other user is connected
26    // if he is not connected anymore, display an error
27    $can_send = true;
28    if (isset($u->privmsg[$recipientid]))
29    {
30      $pvnickid = $u->privmsg[$recipientid]["pvnickid"];
31      $pvnick   = $ct->getNickname($pvnickid);//$u->privmsg[$recipientid]["name"];
32      //      $pvnickid = $ct->getNickId($pvnick);
33
34      // now check if this user is currently online
35      $onlineusers = $ct->getOnlineNick(NULL);
36      if (!in_array($pvnickid, $onlineusers["nickid"]))
37      {
38        // send an error because the user is not online
39        $cmdp = $p;
40        $cmdp["param"] = _pfc("Can't send the message, %s is offline", $pvnick);
41        $cmd =& pfcCommand::Factory("error");
42        $cmd->run($xml_reponse, $cmdp);
43        $can_send = false;
44      }
45    }
46
47
48    // check the sent text is not empty and the user has a none empty nickname
49    $errors = array();
50    if ($text == "") $errors["pfc_words"]  = _pfc("Text cannot be empty");
51    if ($nick == "") $errors["pfc_handle"] = _pfc("Please enter your nickname");
52    if (count($errors) > 0)
53    {
54      // an error occured, just ignore the message and display errors
55      $cmdp = $p;
56      $cmdp["param"] = $errors;
57      $cmd =& pfcCommand::Factory("error");
58      $cmd->run($xml_reponse, $cmdp);
59      if (isset($errors["pfc_handle"])) // the nick is empty so give it focus
60        $xml_reponse->script("$('pfc_handle').focus();");
61      $can_send = false;
62    }
63
64
65    // Now send the message if there is no errors
66    if ($can_send)
67    {
68      $msgid = $ct->write($recipient, $nick, "send", $text);
69      if (is_array($msgid))
70      {
71        $cmdp = $p;
72        $cmdp["param"] = implode(",",$msgid);
73        $cmd =& pfcCommand::Factory("error");
74        $cmd->run($xml_reponse, $cmdp);
75        return;
76      }
77
78      // a message has been posted so :
79      // - clear errors
80      // - give focus to "words" field
81      // @todo move this code in the handleResponse function
82      $xml_reponse->script("pfc.clearError(Array('pfc_words"."','pfc_handle"."'));");
83      $xml_reponse->script("$('pfc_words').focus();");
84    }
85
86    $xml_reponse->script("pfc.handleResponse('".$this->name."', 'ok', '');");
87  }
88}
89
90?>
91