1<?php
2/**
3 * invite.class.php
4 *
5 * Copyright � 2006 Stephane Gully <stephane.gully@gmail.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, 51 Franklin St, Fifth Floor,
20 * Boston, MA  02110-1301  USA
21 */
22
23require_once(dirname(__FILE__)."/../pfccommand.class.php");
24require_once(dirname(__FILE__)."/../commands/join.class.php");
25
26/**
27 * /invite command
28 *
29 * Invites other users into a channel
30 * Currently this is implemented as a "autojoin", so the invited user joins automatically.
31 * The parameter "target channel" is optional, if not set it defaults to the current channel
32 *
33 * @author Benedikt Hallinger <beni@php.net>
34 * @author Stephane Gully <stephane.gully@gmail.com>
35 */
36class pfcCommand_invite extends pfcCommand
37{
38  var $usage = "/invite {nickname to invite} [ {target channel} ]";
39
40  function run(&$xml_reponse, $p)
41  {
42    $clientid    = $p["clientid"];
43    $param       = $p["param"];
44    $params      = $p["params"];
45    $sender      = $p["sender"];
46    $recipient   = $p["recipient"];
47    $recipientid = $p["recipientid"];
48
49    $c =& pfcGlobalConfig::Instance();   // pfcGlobalConfig
50    $u =& pfcUserConfig::Instance();   // pfcUserConfig
51    $ct =& pfcContainer::Instance(); // Connection to the chatbackend
52
53    $nicktoinvite  = isset($params[0]) ? $params[0] : '';
54    $channeltarget = isset($params[1]) ? $params[1] : $u->channels[$recipientid]["name"]; // Default: current channel
55
56    if ($nicktoinvite == '' || $channeltarget == '')
57    {
58      // Parameters are not ok
59      $cmdp = $p;
60      $cmdp["params"] = array();
61      $cmdp["param"] = _pfc("Missing parameter");
62      $cmdp["param"] .= " (".$this->usage.")";
63      $cmd =& pfcCommand::Factory("error");
64      $cmd->run($xml_reponse, $cmdp);
65      return;
66    }
67
68    // check that the inviter is already in the channeltarget
69    if (!$ct->isNickOnline(pfcCommand_join::GetRecipient($channeltarget),$u->nickid))
70    {
71      $cmdp = $p;
72      $cmdp["params"] = array();
73      $cmdp["param"] = _pfc("You must join %s to invite users in this channel",$channeltarget);
74      $cmd =& pfcCommand::Factory("error");
75      $cmd->run($xml_reponse, $cmdp);
76      return;
77    }
78
79    // inviting a user: just add a join command to play to the aimed user metadata.
80    $nicktoinvite_id = $ct->getNickId($nicktoinvite);
81    $cmdstr = 'join2';
82    $cmdp = array();
83    $cmdp['param'] = $channeltarget; // channel target name
84    $cmdp['params'][] = $channeltarget; // channel target name
85    pfcCommand::AppendCmdToPlay($nicktoinvite_id, $cmdstr, $cmdp);
86
87    // notify the aimed channel that a user has been invited
88    $cmdp = array();
89    $cmdp["param"] = _pfc("%s was invited by %s",$nicktoinvite,$sender);
90    $cmdp["flag"]  = 1;
91    $cmdp["recipient"]   = pfcCommand_join::GetRecipient($channeltarget);
92    $cmdp["recipientid"] = pfcCommand_join::GetRecipientId($channeltarget);
93    $cmd =& pfcCommand::Factory("notice");
94    $cmd->run($xml_reponse, $cmdp);
95  }
96}
97?>
98