[go: up one dir, main page]

Menu

[ab7a0e]: / test / testbot.cc  Maximize  Restore  History

Download this file

226 lines (188 with data), 4.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
* testbot.cc
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*
* $Id: testbot.cc,v 1.5 2005/01/12 04:36:47 dan_karrels Exp $
*/
#include <list>
#include <string>
#include <iostream>
#include <cstdlib> // atoi()
#include "testbot.h"
#include "ELog.h"
#include "ConnectionManager.h"
#include "ConnectionHandler.h"
#include "EConfig.h"
#include "StringTokenizer.h"
using namespace gnuworld ;
using std::string ;
using std::cout ;
using std::endl ;
void usage( const string& progName )
{
cout << "Usage: "
<< progName
<< " <conf file>"
<< endl ;
}
int main( int argc, const char** argv )
{
if( argc != 2 )
{
usage( argv[ 0 ] ) ;
return 0 ;
}
testBot bot( argv[ 1 ] ) ;
bot.Run() ;
return 0 ;
}
testBot::testBot( const string& fileName )
: keepRunning( true ),
uplinkPort( 0 ),
myConnect( 0 )
{
EConfig conf( fileName ) ;
setUplinkName( conf.Require( "uplinkname" )->second ) ;
setUplinkPort( static_cast< unsigned short int >(
atoi( conf.Require( "uplinkport" )->second.c_str() ) ) ) ;
setUserName( conf.Require( "username" )->second ) ;
setNickName( conf.Require( "nickName" )->second ) ;
setRealName( conf.Require( "realname" )->second ) ;
for( EConfig::const_iterator chanItr = conf.Find( "channel" ) ;
(chanItr != conf.end()) &&
(chanItr->first == "channel") ; ++chanItr )
{
channels.push_back( chanItr->second ) ;
}
elog.setStream( &cout ) ;
}
testBot::~testBot()
{}
void testBot::Run()
{
myConnect = cm.Connect( this, uplinkName, uplinkPort ) ;
if( !myConnect )
{
cout << "Run> Failed to connect"
<< endl ;
return ;
}
login() ;
// This is the main loop of the program
// Pretty complicated eh?
while( keepRunning )
{
// Poll() will release control to this method at least
// every 100milliseconds.
cm.Poll( 100 ) ;
}
cout << "Run> Out of main loop, shutting down"
<< endl ;
}
void testBot::OnDisconnect( Connection* )
{
keepRunning = false ;
}
void testBot::OnConnectFail( Connection* )
{
keepRunning = false ;
}
void testBot::OnTimeout( Connection* )
{
keepRunning = false ;
}
void testBot::OnConnect( Connection* )
{
cout << "Connected"
<< endl ;
}
void testBot::OnRead( Connection*, const string& line )
{
// New data from the uplink arrives here
// It is a complete \n delimited command
// Trim any trailing \r or \n
size_t len = line.size() - 1 ;
while( ('\r' == line[ len ]) ||
('\n' == line[ len ]) )
{
--len ;
}
// Prepare the data to be processed
string processMe = line.substr( 0, len + 1 ) ;
// Output what was read
cout << "[IN]: "
<< processMe
<< endl ;
// Handle the command
processInput( processMe ) ;
}
void testBot::login()
{
// This is called when a new connection is established
// NICK <nick>
// USER ident host server :realname
string writeMe( "NICK " ) ;
writeMe += getNickName() ;
writeMe += "\r\n" ;
myConnect->Write( writeMe ) ;
writeMe = "USER " ;
writeMe += getUserName() ;
writeMe += " dummy.host.name dummy.server.name :" ;
writeMe += getRealName() ;
writeMe += "\r\n" ;
myConnect->Write( writeMe ) ;
}
void testBot::processInput( const string& line )
{
// PING :1834576261
StringTokenizer st( line ) ;
if( st.empty() )
{
cout << "processInput> Empty tokenizer"
<< endl ;
return ;
}
if( st[ 0 ] == "PING" )
{
string writeMe( "PONG " ) ;
writeMe += st[ 1 ] + "\r\n" ;
myConnect->Write( writeMe ) ;
return ;
}
// :Princeton.NJ.US.Undernet.org 376 _ripper_ :End of /MOTD command.
if( st.size() < 2 )
{
return ;
}
if( st[ 1 ] == "376" )
{
// Fully connected, MOTD and other stuff has completed
// sending.
joinChannels() ;
return ;
}
}
void testBot::joinChannels()
{
for( std::list< string >::const_iterator chanItr = channels.begin() ;
chanItr != channels.end() ; ++chanItr )
{
string writeMe( "JOIN " ) ;
writeMe += *chanItr + "\r\n" ;
myConnect->Write( writeMe ) ;
}
}