3ea012bbe3979e6a4bfad6c073421f0e05f41d62
[clinton/bobotpp.git] / source / DCCManager.C
1 // DCCManager.H -*- C++ -*-
2 // Copyright (c) 2002 Clinton Ebadi
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
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
17
18 #include "DCCManager.H"
19
20 void
21 DCCManager::addConnection (DCCConnection *cnx)
22 {
23 DCCPerson *person = new DCCPerson (cnx);
24 String temp = person->getAddress ();
25 cnx->get_bot()->logLine (temp);
26 if (dcc_map[temp])
27 delete dcc_map[temp];
28 dcc_map[person->getAddress ()] = person;
29 }
30
31 bool
32 DCCManager::sendMessage (String to, String message)
33 {
34 DCCPerson *person = dcc_map[to];
35 if (!person)
36 return false;
37
38 person->sendNotice (message);
39 return true;
40 return false;
41 }
42
43 // checks for stale connections and removes them
44 void
45 DCCManager::checkStale ()
46 {
47 DCC_MAP::iterator it, it2;
48 DCCConnection const* temp_cnx;
49
50 for (it = dcc_map.begin (),it2 = dcc_map.end ();
51 it != it2; ++it)
52 {
53 temp_cnx = it->second->dcc;
54 if (temp_cnx->get_autoRemove () && std::time (0) >=
55 (std::time_t)(temp_cnx->get_lastSpoken () + Bot::DCC_DELAY))
56 {
57 delete it->second;
58 dcc_map.erase (it);
59 }
60 }
61 }
62
63 void
64 #ifdef _HPUX_SOURCE
65 DCCManager::checkInput (int rd)
66 #else
67 DCCManager::checkInput (fd_set rd)
68 #endif
69 {
70
71 DCC_MAP::iterator it = dcc_map.begin();
72 DCC_MAP::iterator it2;
73
74 while (it != dcc_map.end ())
75 {
76 it2 = it;
77 ++it;
78 #ifdef _HPUX_SOURCE
79 if (rd & it2->second->dcc->getFileDescriptor())
80 #else
81 if (FD_ISSET(it2->second->dcc->getFileDescriptor(), &rd))
82 #endif
83 {
84 if (it2->second->handleInput())
85 {
86 delete it2->second->dcc;
87 delete it2->second;
88 dcc_map.erase(it2);
89 }
90 }
91 }
92 }
93
94 DCCManager::~DCCManager ()
95 {
96 DCC_MAP::iterator it, it2;
97 it = dcc_map.begin ();
98 it2 = dcc_map.end();
99
100 for (; it != it2; ++it)
101 {
102 delete it->second->dcc;
103 delete it->second;
104 }
105 }