[project @ 2005-07-04 08:15:08 by unknown_lamer]
[clinton/bobotpp.git] / source / DCCManager.C
CommitLineData
6530edbf 1// DCCManager.H -*- C++ -*-
133eff7a 2// Copyright (c) 2002,2005 Clinton Ebadi
6530edbf 3
4// This program is free software; you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation; either version 2 of the License, or
7// any later version.
8
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with this program; if not, write to the Free Software
133eff7a 16// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17// 02110-1301, USA.
6530edbf 18
19#include "DCCManager.H"
133eff7a 20#include "Utils.H"
6530edbf 21
22void
23DCCManager::addConnection (DCCConnection *cnx)
24{
25 DCCPerson *person = new DCCPerson (cnx);
26 String temp = person->getAddress ();
0316e2c1 27
6530edbf 28 if (dcc_map[temp])
0316e2c1 29 {
30 delete dcc_map[temp];
31 }
32
6530edbf 33 dcc_map[person->getAddress ()] = person;
34}
35
36bool
37DCCManager::sendMessage (String to, String message)
38{
39 DCCPerson *person = dcc_map[to];
40 if (!person)
41 return false;
42
43 person->sendNotice (message);
44 return true;
45 return false;
46}
47
48// checks for stale connections and removes them
49void
50DCCManager::checkStale ()
51{
52 DCC_MAP::iterator it, it2;
53 DCCConnection const* temp_cnx;
54
55 for (it = dcc_map.begin (),it2 = dcc_map.end ();
56 it != it2; ++it)
57 {
c3ecc559 58 temp_cnx = it->second->dcc;
4edefeb6 59 if (temp_cnx->get_autoRemove () && std::time (0) >=
60 (std::time_t)(temp_cnx->get_lastSpoken () + Bot::DCC_DELAY))
6530edbf 61 {
133eff7a 62#ifdef USESCRIPTS
63 DCCPerson *dp = new DCCPerson ((DCCConnection *) temp_cnx);
64
65 // run hooks/dcc/chat-end
66 dp->bot->botInterp->RunHooks
67 (Hook::DCC_CHAT_END,
68 dp->getAddress (),
69 scm_list_n (Utils::
70 str2scm (dp->getAddress ()),
71 SCM_UNDEFINED));
72 delete dp;
73#endif
6530edbf 74 delete it->second;
75 dcc_map.erase (it);
76 }
77 }
78}
79
80void
81#ifdef _HPUX_SOURCE
82DCCManager::checkInput (int rd)
83#else
84DCCManager::checkInput (fd_set rd)
85#endif
86{
87
88 DCC_MAP::iterator it = dcc_map.begin();
89 DCC_MAP::iterator it2;
90
91 while (it != dcc_map.end ())
92 {
93 it2 = it;
94 ++it;
95#ifdef _HPUX_SOURCE
c3ecc559 96 if (rd & it2->second->dcc->getFileDescriptor())
6530edbf 97#else
c3ecc559 98 if (FD_ISSET(it2->second->dcc->getFileDescriptor(), &rd))
6530edbf 99#endif
100 {
101 if (it2->second->handleInput())
102 {
133eff7a 103#ifdef USESCRIPTS
104 DCCPerson *dp = new DCCPerson (*it2->second);
105
106 // run hooks/dcc/chat-end
107 dp->bot->botInterp->RunHooks
108 (Hook::DCC_CHAT_END,
109 dp->getAddress (),
110 scm_list_n (Utils::
111 str2scm (dp->getAddress ()),
112 SCM_UNDEFINED));
113 delete dp;
114#endif
115
c3ecc559 116 delete it2->second->dcc;
6530edbf 117 delete it2->second;
118 dcc_map.erase(it2);
119 }
120 }
121 }
122}
123
124DCCManager::~DCCManager ()
125{
126 DCC_MAP::iterator it, it2;
127 it = dcc_map.begin ();
128 it2 = dcc_map.end();
129
130 for (; it != it2; ++it)
131 {
c3ecc559 132 delete it->second->dcc;
6530edbf 133 delete it->second;
134 }
135}