Add config dir to default load path
[clinton/bobotpp.git] / source / DCCManager.C
1 // DCCManager.H -*- C++ -*-
2 // Copyright (c) 2002,2005 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., 51 Franklin Street, Fifth Floor, Boston, MA
17 // 02110-1301, USA.
18
19 #include "DCCManager.H"
20
21 #include "DCCConnection.H"
22 #include "DCCPerson.H"
23 #include "UserCommands.H"
24 #include "Utils.H"
25
26 #ifdef USESCRIPTS
27 #include "BotInterp.H"
28 #endif
29
30 void
31 DCCManager::addConnection (DCCConnection *cnx)
32 {
33 DCCPerson *person = new DCCPerson (cnx);
34 String temp = person->getAddress ();
35
36 if (dcc_map[temp])
37 {
38 delete dcc_map[temp];
39 }
40
41 dcc_map[person->getAddress ()] = person;
42 }
43
44 bool
45 DCCManager::sendMessage (String to, String message)
46 {
47 DCCPerson *person = dcc_map[to];
48 if (!person)
49 return false;
50
51 person->sendNotice (message);
52 return true;
53 return false;
54 }
55
56 // checks for stale connections and removes them
57 void
58 DCCManager::checkStale ()
59 {
60 DCC_MAP::iterator it, it2;
61 DCCConnection const* temp_cnx;
62
63 for (it = dcc_map.begin (),it2 = dcc_map.end ();
64 it != it2; ++it)
65 {
66 temp_cnx = it->second->dcc;
67 if (temp_cnx->get_autoRemove () && std::time (0) >=
68 (std::time_t)(temp_cnx->get_lastSpoken () + Bot::DCC_DELAY))
69 {
70 #ifdef USESCRIPTS
71 DCCPerson *dp = new DCCPerson ((DCCConnection *) temp_cnx);
72
73 // run hooks/dcc/chat-end
74 dp->bot->botInterp->RunHooks
75 (Hook::DCC_CHAT_END,
76 dp->getAddress (),
77 scm_list_n (Utils::
78 str2scm (dp->getAddress ()),
79 SCM_UNDEFINED));
80 delete dp;
81 #endif
82 delete it->second;
83 dcc_map.erase (it);
84 }
85 }
86 }
87
88 void
89 #ifdef _HPUX_SOURCE
90 DCCManager::checkInput (int rd)
91 #else
92 DCCManager::checkInput (fd_set rd)
93 #endif
94 {
95
96 DCC_MAP::iterator it = dcc_map.begin();
97 DCC_MAP::iterator it2;
98
99 while (it != dcc_map.end ())
100 {
101 it2 = it;
102 ++it;
103 #ifdef _HPUX_SOURCE
104 if (rd & it2->second->dcc->getFileDescriptor())
105 #else
106 if (FD_ISSET(it2->second->dcc->getFileDescriptor(), &rd))
107 #endif
108 {
109 if (it2->second->handleInput())
110 {
111 #ifdef USESCRIPTS
112 DCCPerson *dp = new DCCPerson (*it2->second);
113
114 // run hooks/dcc/chat-end
115 dp->bot->botInterp->RunHooks
116 (Hook::DCC_CHAT_END,
117 dp->getAddress (),
118 scm_list_n (Utils::
119 str2scm (dp->getAddress ()),
120 SCM_UNDEFINED));
121 delete dp;
122 #endif
123
124 delete it2->second->dcc;
125 delete it2->second;
126 dcc_map.erase(it2);
127 }
128 }
129 }
130 }
131
132 DCCManager::~DCCManager ()
133 {
134 DCC_MAP::iterator it, it2;
135 it = dcc_map.begin ();
136 it2 = dcc_map.end();
137
138 for (; it != it2; ++it)
139 {
140 delete it->second->dcc;
141 delete it->second;
142 }
143 }