1<?php
2/**
3 * noflood.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 */
22require_once dirname(__FILE__)."/../pfci18n.class.php";
23require_once dirname(__FILE__)."/../pfcuserconfig.class.php";
24require_once dirname(__FILE__)."/../pfcproxycommand.class.php";
25require_once dirname(__FILE__)."/../../lib/utf8/utf8_strlen.php";
26
27/**
28 * pfcProxyCommand_noflood
29 * this proxy will protect the chat from flooders
30 * @author Stephane Gully <stephane.gully@gmail.com>
31 */
32class pfcProxyCommand_noflood extends pfcProxyCommand
33{
34  function run(&$xml_reponse, $p)
35  {
36    $clientid    = $p["clientid"];
37    $param       = $p["param"];
38    $sender      = $p["sender"];
39    $recipient   = $p["recipient"];
40    $recipientid = $p["recipientid"];
41
42    $c =& pfcGlobalConfig::Instance();
43    $u =& pfcUserConfig::Instance();
44
45    $cmdtocheck = array("send", "nick", "me");
46    if ( in_array($this->name, $cmdtocheck) )
47    {
48      $container =& pfcContainer::Instance();
49      $nickid        = $u->nickid;
50      $isadmin       = $container->getUserMeta($nickid, 'isadmin');
51      $lastfloodtime = $container->getUserMeta($nickid, 'floodtime');
52      $flood_nbmsg   = $container->getUserMeta($nickid, 'flood_nbmsg');
53      $flood_nbchar  = $container->getUserMeta($nickid, 'flood_nbchar');
54      $floodtime     = time();
55
56      if ($floodtime - $lastfloodtime <= $c->proxies_cfg[$this->proxyname]["delay"])
57      {
58        // update the number of posted message indicator
59        $flood_nbmsg++;
60        // update the number of posted characteres indicator
61        $flood_nbchar += utf8_strlen($param);
62      }
63      else
64      {
65        $flood_nbmsg = 0;
66        $flood_nbchar = 0;
67      }
68
69      if (!$isadmin &&
70          ($flood_nbmsg>$c->proxies_cfg[$this->proxyname]["msglimit"] ||
71           $flood_nbchar>$c->proxies_cfg[$this->proxyname]["charlimit"])
72          )
73      {
74        // warn the flooder
75        $msg = _pfc("Please don't post so many message, flood is not tolerated");
76        $xml_reponse->script("alert('".addslashes($msg)."');");
77
78        // kick the flooder
79        $cmdp = $p;
80        $cmdp["param"] = null;
81        $cmdp["params"][0] = "ch";
82        $cmdp["params"][1] = $u->channels[$recipientid]["name"];
83        $cmdp["params"][2] .=_pfc("kicked from %s by %s", $u->channels[$recipientid]["name"], "noflood");
84        $cmd =& pfcCommand::Factory("leave");
85        $cmd->run($xml_reponse, $cmdp);
86        return false;
87      }
88
89      if ($flood_nbmsg == 0)
90        $container->setUserMeta($nickid, 'floodtime', $floodtime);
91      $container->setUserMeta($nickid,   'flood_nbmsg',  $flood_nbmsg);
92      $container->setUserMeta($nickid,   'flood_nbchar', $flood_nbchar);
93    }
94
95    // forward the command to the next proxy or to the final command
96    $p["clientid"]    = $clientid;
97    $p["param"]       = $param;
98    $p["sender"]      = $sender;
99    $p["recipient"]   = $recipient;
100    $p["recipientid"] = $recipientid;
101    return $this->next->run($xml_reponse, $p);
102  }
103}
104
105?>
106