4 #pragma warning ( disable : 4786 )
14 #include "IRCClient.h"
20 //vector<string> tech, module, dev, stru, period, status, type, func, irql, curse, cursecop;
27 std::vector
<std::string
> list
;
31 List ( const char* _name
, bool _macro
) : name(_name
), macro(_macro
)
33 tag
= ssprintf("%%%s%%",_name
);
41 void ImportList ( const char* listname
, bool macro
)
43 lists
.push_back ( List ( listname
, macro
) );
44 List
& list
= lists
.back();
45 File
f ( ssprintf("%s.txt",listname
).c_str(), "r" );
47 while ( f
.next_line ( line
, true ) )
48 list
.list
.push_back ( line
);
51 const char* ListRand ( List
& list
)
53 vector
<string
>& l
= list
.list
;
56 static string nothing
;
57 nothing
= ssprintf ( "<list '%s' empty>", list
.name
.c_str() );
58 return nothing
.c_str();
60 else if ( l
.size() == 1 )
63 while ( sel
== list
.last
)
64 sel
= rand()%l
.size();
66 return l
[sel
].c_str();
69 const char* ListRand ( int i
)
71 return ListRand ( lists
[i
] );
74 int GetListIndex ( const char* listname
)
76 for ( int i
= 0; i
< lists
.size(); i
++ )
78 if ( !stricmp ( lists
[i
].name
.c_str(), listname
) )
84 List
& GetList ( const char* listname
)
86 return lists
[GetListIndex(listname
)];
89 const char* ListRand ( const char* list
)
91 int i
= GetListIndex ( list
);
97 string
TaggedReply ( const char* listname
)
99 string t
= ListRand(listname
);
101 const char* p
= t
.c_str();
107 for ( int i
= 0; i
< lists
.size() && !found
; i
++ )
109 if ( lists
[i
].macro
&& !strnicmp ( p
, lists
[i
].tag
.c_str(), lists
[i
].tag
.size() ) )
112 p
+= lists
[i
].tag
.size();
119 const char* p2
= strchr ( p
, '%' );
124 out
+= string ( p
, p2
-p
);
131 string
gobble ( string
& s
, const char* delim
)
133 const char* p
= s
.c_str();
134 p
+= strspn ( p
, delim
);
135 const char* p2
= strpbrk ( p
, delim
);
136 if ( !p2
) p2
= p
+ strlen(p
);
137 string
out ( p
, p2
-p
);
138 p2
+= strspn ( p2
, delim
);
143 bool isop ( const string
& who
)
145 for ( int i
= 0; i
< ops
.size(); i
++ )
154 // do custom stuff with the IRCClient from your subclass via the provided callbacks...
155 class MyIRCClient
: public IRCClient
158 clock_t brake_silence
;
160 // wait another 30 mins to brake the silence
161 #define NOIDLE brake_silence = clock () + 30 * CLK_TCK * 60
163 void CheckIdle ( void )
165 while (true) // _inRun
167 while (clock() < brake_silence
)
170 string out
= TaggedReply("idle");
172 if ( !strnicmp ( out
.c_str(), "/me ", 4 ) )
173 Action ( CHANNEL
, &out
[4] );
175 PrivMsg ( CHANNEL
, out
);
181 static void THREADAPI
CallMe ( MyIRCClient
* irc
)
191 ThreadPool::Instance().Launch ( (ThreadPoolFunc
*)MyIRCClient::CallMe
, this );
192 flog
.open ( "arch.log", "r+" );
194 // see IRCClient.h for documentation on these callbacks...
199 bool OnJoin ( const string
& user
, const string
& channel
)
201 //printf ( "user '%s' joined channel '%s'\n", user.c_str(), channel.c_str() );
204 bool OnPart ( const std::string
& user
, const std::string
& channel
)
206 std::vector
<std::string
>::iterator it
= ops
.begin();
207 for ( ; it
!= ops
.end(); it
++ )
211 printf ( "remove '%s' to ops list\n", user
.c_str() );
217 bool OnNick ( const std::string
& oldNick
, const std::string
& newNick
)
219 for ( int i
= 0; i
< ops
.size(); i
++ )
221 if ( ops
[i
] == oldNick
)
223 printf ( "op '%s' changed nick to '%s'\n", oldNick
.c_str(), newNick
.c_str() );
230 bool OnEndChannelUsers ( const string
& channel
)
234 bool OnPrivMsg ( const string
& from
, const string
& text
)
237 printf ( "<%s> %s\n", from
.c_str(), text
.c_str() );
238 flog
.printf ( "<%s> %s\n", from
.c_str(), text
.c_str() );
241 return PrivMsg ( from
, "hey, your tongue doesn't belong there!" );
243 else if ( strnicmp ( text
.c_str(), "!say ", 5 ) )
244 return PrivMsg ( from
, "Talk to me on normal Chanel" );
246 string say
= trim(&text
[5]);
248 if ( !strnicmp ( say
.c_str(), "/me ", 4 ) )
249 return Action ( CHANNEL
, trim(&say
[4]) );
251 return PrivMsg ( CHANNEL
, trim(say
) );
253 bool OnChannelMsg ( const string
& channel
, const string
& from
, const string
& text
)
256 printf ( "%s <%s> %s\n", channel
.c_str(), from
.c_str(), text
.c_str() );
257 flog
.printf ( "%s <%s> %s\n", channel
.c_str(), from
.c_str(), text
.c_str() );
258 NOIDLE
; // add 30 mins till idle
260 bool found_name
= false;
261 string
text2 ( text
);
262 strlwr ( &text2
[0] );
264 if ( !strnicmp ( text
.c_str(), BOTNAME
, strlen(BOTNAME
) ) )
270 gobble ( s
, " \t" ); // remove bot name
275 bool from_op
= isop(from
);
276 string cmd
= gobble ( s
, " \t" );
279 if ( false && cmd
== "!svn" && from
== "TechBot" ) // || cmd == "!help" && !TechBotOnline
281 PrivMsg ( channel
, "For my help try !what." );
287 if ( cmd
== "!grovel" )
289 string out
= ssprintf(TaggedReply("nogrovel").c_str(),from
.c_str());
290 if ( !strnicmp ( out
.c_str(), "/me ", 4 ) )
291 return Action ( channel
, &out
[4] );
293 return PrivMsg ( channel
, out
);
296 else if ( cmd
== "!what" )
298 return PrivMsg ( channel
, ssprintf("For you, %s, I only support the \"!grovel\" command.", from
.c_str()).c_str() );
301 else if ( cmd
== "!grovel" || cmd
== "!kiss" || cmd
== "!hug"
302 || cmd
== "!give" || cmd
== "!what" || cmd
== "!add" || cmd
== "!remove" )
304 PrivMsg ( channel
, ssprintf("%s: I only take commands from ops",from
.c_str()) );
309 else if ( cmd
== "!grovel" )
311 string out
= ssprintf(TaggedReply("grovel").c_str(),from
.c_str());
312 if ( !strnicmp ( out
.c_str(), "/me ", 4 ) )
313 return Action ( channel
, &out
[4] );
315 return PrivMsg ( channel
, out
);
317 else if ( cmd
== "!kiss" )
320 return Action ( channel
, ssprintf("kisses %s",s
.c_str()) );
322 return PrivMsg ( channel
, ssprintf("%s: huh?",from
.c_str()) );
324 else if ( cmd
== "!hug" )
327 return Action ( channel
, ssprintf("hugs %s",s
.c_str()) );
329 return PrivMsg ( channel
, ssprintf("%s: huh?",from
.c_str()) );
331 else if ( cmd
== "!give" )
333 string who
= gobble(s
," \t");
334 if ( who
.size() && s
.size() )
335 return Action ( channel
, ssprintf("gives %s a %s",who
.c_str(),s
.c_str()) );
337 return PrivMsg ( channel
, ssprintf("%s: huh?",from
.c_str()) );
339 else if ( cmd
== "!what" )
341 PrivMsg ( channel
, "For ops I support the following commands:" );
342 PrivMsg ( channel
, "!grovel" );
343 PrivMsg ( channel
, "!kiss" );
344 PrivMsg ( channel
, "!hug" );
345 PrivMsg ( channel
, "!give" );
346 PrivMsg ( channel
, "!say (the input is a private message)" );
347 PrivMsg ( channel
, "!add" );
348 PrivMsg ( channel
, "!remove" );
349 PrivMsg ( channel
, " - for more info see wiki" );
351 else if ( cmd
== "!add" )
353 string listname
= gobble ( s
, " \t" );
354 int i
= GetListIndex ( listname
.c_str() );
356 return PrivMsg ( channel
, ssprintf("%s: I don't have a list named '%s'",from
.c_str(),listname
.c_str()) );
357 List
& list
= lists
[i
];
358 if ( s
[0] == '\"' || s
[0] == '\'' )
361 const char* p
= &s
[1];
362 const char* p2
= strchr ( p
, delim
);
364 return PrivMsg ( channel
, ssprintf("%s: Couldn't add, unmatched quotes",from
.c_str()) );
365 s
= string ( p
, p2
-p
);
367 for ( i
= 0; i
< list
.list
.size(); i
++ )
369 if ( list
.list
[i
] == s
)
370 return PrivMsg ( channel
, ssprintf("%s: entry already exists in list '%s'",from
.c_str(),listname
.c_str()) );
372 if ( !stricmp ( listname
.c_str(), "curse" ) )
374 list
.list
.push_back ( s
);
376 File
f ( ssprintf("%s.txt",list
.name
.c_str()), "w" );
377 for ( i
= 0; i
< list
.list
.size(); i
++ )
378 f
.printf ( "%s\n", list
.list
[i
].c_str() );
380 return PrivMsg ( channel
, ssprintf("%s: entry added to list '%s'",from
.c_str(),listname
.c_str()) );
382 else if ( cmd
== "!remove" )
384 string listname
= gobble ( s
, " \t" );
385 int i
= GetListIndex ( listname
.c_str() );
387 return PrivMsg ( channel
, ssprintf("%s: I don't have a list named '%s'",from
.c_str(),listname
.c_str()) );
388 List
& list
= lists
[i
];
389 if ( s
[0] == '\"' || s
[0] == '\'' )
392 const char* p
= &s
[1];
393 const char* p2
= strchr ( p
, delim
);
395 return PrivMsg ( channel
, ssprintf("%s: Couldn't add, unmatched quotes",from
.c_str()) );
396 s
= string ( p
, p2
-p
);
398 std::vector
<std::string
>::iterator it
= list
.list
.begin();
399 for ( ; it
!= list
.list
.end(); it
++ )
403 list
.list
.erase ( it
);
405 File
f ( ssprintf("%s.txt",list
.name
.c_str()), "w" );
406 it
= list
.list
.begin();
407 for ( ; it
< list
.list
.end(); it
++ )
408 f
.printf ( "%s\n", it
->c_str() );
410 return PrivMsg ( channel
, ssprintf("%s: entry removed from list '%s'",from
.c_str(),listname
.c_str()) );
413 return PrivMsg ( channel
, ssprintf("%s: entry doesn't exist in list '%s'",from
.c_str(),listname
.c_str()) );
418 return PrivMsg ( channel
, ssprintf("%s: huh?",from
.c_str()) );
423 bool found_curse
= false;
424 static vector
<string
>& curse
= GetList("curse").list
;
425 text2
= " " + text2
+ " ";
427 for ( int i
= 0; i
< curse
.size() && !found_curse
; i
++ )
429 if ( strstr ( text2
.c_str(), curse
[i
].c_str() ) )
434 static List
& cursecop
= GetList("cursecop");
435 return PrivMsg ( channel
, ssprintf("%s: %s", from
.c_str(), ListRand(cursecop
)) );
438 string
botname (BOTNAME
);
439 strlwr ( &botname
[0] );
440 //botname = " " + botname + " ";
442 if ( strstr(text2
.c_str(), botname
.c_str()) || strstr(text2
.c_str(), " arch ") || found_name
)
444 string out
= ssprintf("%s: %s", from
.c_str(), TaggedReply("tech").c_str());
445 flog
.printf ( "TECH-REPLY: %s\n", out
.c_str() );
446 if ( !strnicmp ( out
.c_str(), "/me ", 4 ) )
447 return Action ( channel
, &out
[4] );
449 return PrivMsg ( channel
, out
);
453 } // On Chanel Message
455 bool OnChannelMode ( const string
& channel
, const string
& mode
)
457 //printf ( "OnChannelMode(%s,%s)\n", channel.c_str(), mode.c_str() );
460 bool OnUserModeInChannel ( const string
& src
, const string
& channel
, const string
& mode
, const string
& target
)
462 printf ( "OnUserModeInChannel(%s,%s,%s,%s)\n", src
.c_str(), channel
.c_str(), mode
.c_str(), target
.c_str() );
463 const char* p
= mode
.c_str();
471 while ( *p
!= 0 && *p
!= ' ' )
475 printf ( "adding '%s' to ops list\n", target
.c_str() );
476 ops
.push_back ( target
);
482 while ( *p
!= 0 && *p
!= ' ' )
486 std::vector
<std::string
>::iterator it
= ops
.begin();
487 for ( ; it
!= ops
.end(); it
++ )
491 printf ( "remove '%s' to ops list\n", target
.c_str() );
502 bool OnMode ( const string
& user
, const string
& mode
)
504 //printf ( "OnMode(%s,%s)\n", user.c_str(), mode.c_str() );
507 bool OnChannelUsers ( const string
& channel
, const vector
<string
>& users
)
509 //printf ( "[%s has %i users]: ", channel.c_str(), users.size() );
510 for ( int i
= 0; i
< users
.size(); i
++ )
512 if ( users
[i
][0] == '@' )
513 ops
.push_back ( &users
[i
][1] );
516 printf ( "%s", users[i].c_str() );*/
526 bool OnBanned ( const std::string
& channel
)
529 return Join(CHANNEL
);
533 int main ( int argc
, char** argv
)
535 srand ( time(NULL
) );
537 ImportList ( "dev", true );
538 ImportList ( "func", true );
539 ImportList ( "dev", true );
540 ImportList ( "func", true );
541 ImportList ( "irql", true );
542 ImportList ( "module", true );
543 ImportList ( "period", true );
544 ImportList ( "status", true );
545 ImportList ( "stru", true );
546 ImportList ( "type", true );
548 ImportList ( "tech", false );
549 ImportList ( "curse", false );
550 ImportList ( "cursecop", false );
551 ImportList ( "grovel", false );
552 ImportList ( "nogrovel", false );
553 ImportList ( "idle", false );
556 printf ( "initializing IRCClient debugging\n" );
557 IRCClient::SetDebug ( true );
562 printf ( "calling suStartup()\n" );
564 printf ( "creating IRCClient object\n" );
566 printf ( "connecting to freenode\n" );
568 if ( !irc
.Connect ( SERVER
) ) // irc.freenode.net
570 printf ( "couldn't connect to server\n" );
574 printf ( "sending user command\n" );
575 if ( !irc
.User ( BOTNAME
, "reactos.com", SERVER
, "ArchBlackmann" ) )
577 printf ( "USER command failed, retying ...\n" );
581 printf ( "sending nick\n" );
582 if ( !irc
.Nick ( BOTNAME
) )
584 printf ( "NICK command failed, retying ...\n" );
588 printf ( "setting mode\n" );
589 if ( !irc
.Mode ( MODE
) )
591 printf ( "MODE command failed, retying ...\n" );
595 printf ( "joining channel\n" );
596 if ( !irc
.Join ( CHANNEL
) )
598 printf ( "JOIN command failed, retying ...\n" );
603 printf ( "entering irc client processor\n" );
604 irc
.Run ( false ); // do the processing in this thread...