1<?php
2
3require_once(dirname(__FILE__)."/../pfccommand.class.php");
4
5class pfcCommand_nick extends pfcCommand
6{
7  var $usage = "/nick {newnickname}";
8
9  function run(&$xml_reponse, $p)
10  {
11    $clientid    = $p["clientid"];
12    $param       = $p["param"];
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 (trim($param) == '')
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    $newnick = phpFreeChat::FilterNickname($param);
33    $oldnick = $ct->getNickname($u->nickid);
34
35    $newnickid = $ct->getNickId($newnick);
36    $oldnickid = $u->nickid;
37
38    // new nickname is undefined (not used) and
39    // current nickname (oldnick) is mine and
40    // oldnick is different from new nick
41    // -> this is a nickname change
42    if ($oldnick != $newnick &&
43        $oldnick != '')
44    {
45      // really change the nick (rename it)
46      $ct->changeNick($newnick, $oldnick);
47      $u->nick = $newnick;
48      $u->saveInCache();
49      $this->forceWhoisReload($u->nickid);
50
51      // notify all the joined channels/privmsg
52      $cmdp = $p;
53      $cmdp["param"] = _pfc("%s changes his nickname to %s",$oldnick,$newnick);
54      $cmdp["flag"]  = 1;
55      $cmd =& pfcCommand::Factory("notice");
56      foreach($u->channels as $id => $chan)
57      {
58        $cmdp["recipient"]   = $chan["recipient"];
59        $cmdp["recipientid"] = $id;
60        $cmd->run($xml_reponse, $cmdp);
61      }
62      foreach( $u->privmsg as $id => $pv )
63      {
64        $cmdp["recipient"]   = $pv["recipient"];
65        $cmdp["recipientid"] = $id;
66        $cmd->run($xml_reponse, $cmdp);
67      }
68      $xml_reponse->script("pfc.handleResponse('nick', 'changed', '".addslashes($newnick)."');");
69      return true;
70    }
71
72    // new nickname is undefined (not used)
73    // -> this is a first connection (this piece of code is called by /connect command)
74    if ($newnickid == '')
75    {
76      // this is a first connection : create the nickname on the server
77      $ct->createNick($u->nickid, $newnick);
78      $u->nick = $newnick;
79      $u->saveInCache();
80
81      $this->forceWhoisReload($u->nickid);
82
83      $xml_reponse->script("pfc.handleResponse('nick', 'connected', '".addslashes($newnick)."');");
84
85      return true;
86    }
87
88    return false;
89  }
90}
91
92?>
93