1<?php
2
3require_once(dirname(__FILE__)."/../pfccommand.class.php");
4
5class pfcCommand_privmsg extends pfcCommand
6{
7  var $usage = "/privmsg {nickname}";
8
9  function run(&$xml_reponse, $p)
10  {
11    $clientid    = $p["clientid"];
12    $params      = $p["params"];
13    $sender      = $p["sender"];
14    $recipient   = $p["recipient"];
15    $recipientid = $p["recipientid"];
16
17    $c =& pfcGlobalConfig::Instance();
18    $u =& pfcUserConfig::Instance();
19    $ct =& pfcContainer::Instance();
20
21    if (count($params) == 0)
22    {
23      // error
24      $cmdp = $p;
25      $cmdp["param"] = _pfc("Missing parameter");
26      $cmdp["param"] .= " (".$this->usage.")";
27      $cmd =& pfcCommand::Factory("error");
28      $cmd->run($xml_reponse, $cmdp);
29      return false;
30    }
31
32    // check the pvname exists on the server
33    $pvname = '';
34    $pvnickid = '';
35    if ($this->name == 'privmsg2')
36    {
37      $pvnickid = $params[0];
38      $pvname   = $ct->getNickname($pvnickid);
39    }
40    else
41    {
42      $pvname   = $params[0];
43      $pvnickid = $ct->getNickId($pvname);
44    }
45    $nickid   = $u->nickid;
46    $nick     = $ct->getNickname($u->nickid);
47
48    // error: can't speak to myself
49    if ($pvnickid == $nickid)
50    {
51      $xml_reponse->script("pfc.handleResponse('".$this->name."','speak_to_myself');");
52      return;
53    }
54
55    //$this->trace($xml_reponse, $this->name, $sender);
56
57    // error: can't speak to unknown
58    if ($pvnickid == '')
59    {
60      // remove this old pv from the privmsg list
61      $pvid_to_remove = "";
62      foreach( $u->privmsg as $pv_k => $pv_v )
63      {
64        if ($pv_v["name"] == $pvname)
65          $pvid_to_remove = $pv_k;
66      }
67      if ($pvid_to_remove != "")
68      {
69        unset($u->privmsg[$pvid_to_remove]);
70        $u->saveInCache();
71      }
72
73      $xml_reponse->script("pfc.handleResponse('".$this->name."', 'unknown', Array('".addslashes($pvname)."','speak to unknown'));");
74      return;
75    }
76
77    // generate a pvid from the two nicknames ids
78    $a = array($pvnickid, $nickid); sort($a);
79    $pvrecipient = "pv_".$a[0]."_".$a[1];
80    $pvrecipientid = md5($pvrecipient);
81
82    //    $xml_reponse->script("alert('privmsg: pvnickid=".$pvnickid."');");
83    //    $xml_reponse->script("alert('privmsg: pvname=".$pvname." pvrecipient=".$pvrecipient."');");
84
85    // update the private message list
86    // in the sessions
87    if (!isset($u->privmsg[$pvrecipientid]))
88    {
89      if ($c->max_privmsg <= count($u->privmsg))
90      {
91        // the maximum number of private messages has been reached
92        $xml_reponse->script("pfc.handleResponse('".$this->name."', 'max_privmsg', Array());");
93        return;
94      }
95
96      $u->privmsg[$pvrecipientid]["recipient"] = $pvrecipient;
97      $u->privmsg[$pvrecipientid]["name"]      = $pvname;
98      $u->privmsg[$pvrecipientid]["pvnickid"]  = $pvnickid;
99      $u->saveInCache();
100
101      // reset the message id indicator
102      // i.e. be ready to re-get all last posted messages
103      $from_id_sid = "pfc_from_id_".$c->getId()."_".$clientid."_".$pvrecipientid;
104      $from_id     = $ct->getLastId($pvrecipient)-$c->max_msg-1;
105      $_SESSION[$from_id_sid] = ($from_id<0) ? 0 : $from_id;
106    }
107
108    // register the user (and his metadata) in this pv
109    //    $ct->createNick($pvrecipient, $u->nick, $u->nickid);
110    $ct->joinChan($nickid, $pvrecipient);
111    $this->forceWhoisReload($nickid);
112
113    // return ok to the client
114    // then the client will create a new tab
115    $xml_reponse->script("pfc.handleResponse('".$this->name."', 'ok', Array('".$pvrecipientid."','".addslashes($pvname)."'));");
116  }
117}
118
119?>
120