give Arch a little more variety
[reactos.git] / rosapps / games / ArchBlackmann / ArchBlackmann.cpp
1 // irc_test.cpp
2
3 #ifdef _MSC_VER
4 #pragma warning ( disable : 4786 )
5 #endif//_MSC_VER
6
7 #include <time.h>
8 #include <stdio.h>
9
10 #include "File.h"
11 #include "ssprintf.h"
12
13 #include "IRCClient.h"
14
15 using std::string;
16 using std::vector;
17
18 const char* ArchBlackmann = "ArchBlackmann";
19
20 vector<string> tech, module, dev, stru, period;
21
22 void ImportList ( vector<string>& list, const char* filename )
23 {
24 File f ( filename, "r" );
25 string line;
26 while ( f.next_line ( line, true ) )
27 list.push_back ( line );
28 }
29
30 const char* ListRand ( const vector<string>& list )
31 {
32 return list[rand()%list.size()].c_str();
33 }
34
35 string TechReply()
36 {
37 string t = ListRand(tech);
38 string out;
39 const char* p = t.c_str();
40 while ( *p )
41 {
42 if ( *p == '%' )
43 {
44 if ( !strnicmp ( p, "%dev%", 5 ) )
45 {
46 out += ListRand(dev);
47 p += 5;
48 }
49 else if ( !strnicmp ( p, "%period%", 8 ) )
50 {
51 out += ListRand(period);
52 p += 8;
53 }
54 else if ( !strnicmp ( p, "%module%", 8 ) )
55 {
56 out += ListRand(module);
57 p += 8;
58 }
59 else if ( !strnicmp ( p, "%stru%", 6 ) )
60 {
61 out += ListRand(stru);
62 p += 6;
63 }
64 else
65 out += *p++;
66 }
67 const char* p2 = strchr ( p, '%' );
68 if ( !p2 )
69 p2 = p + strlen(p);
70 if ( p2 > p )
71 {
72 out += string ( p, p2-p );
73 p = p2;
74 }
75 }
76 return out;
77 }
78
79 // do custom stuff with the IRCClient from your subclass via the provided callbacks...
80 class MyIRCClient : public IRCClient
81 {
82 File flog;
83 public:
84 MyIRCClient()
85 {
86 flog.open ( "arch.log", "w+" );
87 }
88 // see IRCClient.h for documentation on these callbacks...
89 bool OnConnected()
90 {
91 return true;
92 }
93 bool OnJoin ( const string& user, const string& channel )
94 {
95 return true;
96 }
97 bool OnEndChannelUsers ( const string& channel )
98 {
99 return true;
100 }
101 bool OnPrivMsg ( const string& from, const string& text )
102 {
103 printf ( "<%s> %s\n", from.c_str(), text.c_str() );
104 flog.printf ( "<%s> %s\n", from.c_str(), text.c_str() );
105 return PrivMsg ( from, "hey, your tongue doesn't belong there!" );
106 }
107 bool OnChannelMsg ( const string& channel, const string& from, const string& text )
108 {
109 printf ( "%s <%s> %s\n", channel.c_str(), from.c_str(), text.c_str() );
110 flog.printf ( "%s <%s> %s\n", channel.c_str(), from.c_str(), text.c_str() );
111 string text2(text);
112 strlwr ( &text2[0] );
113 if ( !strnicmp ( text2.c_str(), ArchBlackmann, strlen(ArchBlackmann) ) )
114 {
115 string reply = ssprintf("%s: %s", from.c_str(), TechReply().c_str());
116 flog.printf ( "TECH-REPLY: %s\n", reply.c_str() );
117 return PrivMsg ( channel, reply );
118 }
119 return true;
120 }
121 bool OnChannelMode ( const string& channel, const string& mode )
122 {
123 //printf ( "OnChannelMode(%s,%s)\n", channel.c_str(), mode.c_str() );
124 return true;
125 }
126 bool OnUserModeInChannel ( const string& src, const string& channel, const string& user, const string& mode )
127 {
128 //printf ( "OnUserModeInChannel(%s,%s%s,%s)\n", src.c_str(), channel.c_str(), user.c_str(), mode.c_str() );
129 return true;
130 }
131 bool OnMode ( const string& user, const string& mode )
132 {
133 //printf ( "OnMode(%s,%s)\n", user.c_str(), mode.c_str() );
134 return true;
135 }
136 bool OnChannelUsers ( const string& channel, const vector<string>& users )
137 {
138 printf ( "[%s has %i users]: ", channel.c_str(), users.size() );
139 for ( int i = 0; i < users.size(); i++ )
140 {
141 if ( i )
142 printf ( ", " );
143 printf ( "%s", users[i].c_str() );
144 }
145 printf ( "\n" );
146 return true;
147 }
148 };
149
150 int main ( int argc, char** argv )
151 {
152 srand ( time(NULL) );
153 ImportList ( tech, "tech.txt" );
154 ImportList ( stru, "stru.txt" );
155 ImportList ( dev, "dev.txt" );
156 ImportList ( period, "period.txt" );
157 ImportList ( module, "module.txt" );
158
159 printf ( "initializing IRCClient debugging\n" );
160 IRCClient::SetDebug ( true );
161 printf ( "calling suStartup()\n" );
162 suStartup();
163 printf ( "creating IRCClient object\n" );
164 MyIRCClient irc;
165 printf ( "connecting to freenode\n" );
166 if ( !irc.Connect ( "212.204.214.114" ) ) // irc.freenode.net
167 {
168 printf ( "couldn't connect to server\n" );
169 return -1;
170 }
171 printf ( "sending user command\n" );
172 if ( !irc.User ( "ArchBlackmann", "", "irc.freenode.net", "Arch Blackmann" ) )
173 {
174 printf ( "USER command failed\n" );
175 return -1;
176 }
177 printf ( "sending nick\n" );
178 if ( !irc.Nick ( "ArchBlackmann" ) )
179 {
180 printf ( "NICK command failed\n" );
181 return -1;
182 }
183 printf ( "setting mode\n" );
184 if ( !irc.Mode ( "+i" ) )
185 {
186 printf ( "MODE command failed\n" );
187 return -1;
188 }
189 printf ( "joining #ReactOS\n" );
190 if ( !irc.Join ( "#ReactOS" ) )
191 {
192 printf ( "JOIN command failed\n" );
193 return -1;
194 }
195 printf ( "entering irc client processor\n" );
196 irc.Run ( false ); // do the processing in this thread...
197 return 0;
198 }