Forum » Advanced Programming » IRC bot class
What else?
Berichten: 1180
avatar
Offline Stuur privebericht
Code | Selecteer Alles
minimaliseren
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
Class ViiIrcBot {
    
    protected 
$BotSocket;
    protected 
$BotConfig;
    protected 
$BotData;
    
    public function 
__construct () {
        
        
        
$this->BotConfig = array(
        
            
/**
             * Bot configuration for information
             */
             
            
'nickname' => 'ViiBot2',
            
'realname' => 'Vii personal bot',
            
'ident'    => 'ViiBot',
            
            
/**
             * Bot server configration
             */
             
            
'hostname' => 0,
            
'server'   => 'ogn1.ogamenet.net',
            
'port'    => 6667,
            
            
/**
             * Bot channel configration
             */
             
            
'channel'  => array('#Vii')
        );
         
         
         
/**
          * Prepare for bot connection
          */
          
          
if(!$this->ConnectViiBot()) {
                die(
'Connection failed.');
          }
          else {
            
                
$this->ConnectToIrc();
                
$this->BotMainLoop();
          }
    }
    
    protected function 
ConnectViiBot() {
        
        
/**
         * Create a socket for bot connection
         */
         
        
$this->BotSocket socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
        if (!
$this->BotSocket) {
            return 
false;
        }
        
        
/**
         * Bind the socket to make it irc'able 
         */
         
        
if(!socket_bind($this->BotSocket$this->BotConfig['hostname'])) { 
            return 
false;
        } 
        if(!
socket_connect($this->BotSocket$this->BotConfig['server'], $this->BotConfig['port'])) { 
            return 
false;
        } 
        
        
/**
         * Return positive result
         */
        
return $this->BotSocket;
    }
    
    protected function 
ConnectToIrc () {
        
        
/**
         * Utilize connection
         */
        
        
$this->RawWrite('USER ' $this->BotConfig['ident']. ' ' $this->BotConfig['hostname'] . ' ' $this->BotConfig['server'] . ' :'$this->BotConfig['realname']); 
                        
        
/**
         * Define bot nickname to use
         * on the server
         */
                        
        
$this->RawWrite('NICK ' $this->BotConfig['nickname']);
        return 
true
    }
    
    protected function 
RawWrite ($Parameter) {
        
        
/**
         * Write raw data to server
         */
        
        
echo $Parameter;
        if(!
socket_write($this->BotSocket$Parameter 'rn')){
            die(
'error!');
        }
    }
    
    protected function 
BotMainLoop () {
        
$inKanaal false
        while(
$data socket_read($this->BotSocket,65000,PHP_NORMAL_READ)) { 
            if(
$data == "n") continue; 
            
            if (
$data 'NOTICE AUTH :*** No ident response') {
                
$this->RawWrite('USER ' $this->BotConfig['ident']. ' ' $this->BotConfig['hostname'] . ' ' $this->BotConfig['server'] . ' :'$this->BotConfig['realname']); 
            }
            
            
$write fopen('log.txt''w');
            
$writenow fwrite($write$data);
        }
    }
}

$ViiBot = new ViiIrcBot();
?>



probleem is dat hij blijft haken op dat hij geen ident, toe krijgt gestuurd daar in tegen doe ik wel sturen met de "nick" en "user" writing...

op 1 of andere manier pakt hij die niet >_>

en btw let niet op de loop
11-11-2010 15:10
Dit topic is 201 keer bekeken door 52 verschillende leden
Reacties op: "IRC bot class"
1
What else?
Berichten: 1180
avatar
Offline Stuur privébericht
voor de gene die het willen weten wat de fout was:

in de raw_write functie:
Code | Selecteer Alles
minimaliseren
1
if(!socket_write($this->BotSocket$Parameter "rm"


moet met dubbele quote tekens enkele doet hij het niet ^_^

Nieuwe reactie samengevoegd met originele reactie op 12.11.10 00:53:10:
en voor de mensen die een opstart willen hebben voor een irc bot:
Code | Selecteer Alles
minimaliseren
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php

/**
 * @filesource bot.php
 * @version 0.1-alpha
 * @author Patrick Rennings
 * @copyright 2010
 * 
 * @usage Basic IRC Bot class
 */

Class ViiIrcBot {
    
    protected 
$BotSocket;
    protected 
$BotConfig;
    protected 
$BotData;
    
    protected 
$Parameter;
    
    
/**
     * @name    ViiIrcBot::__construct
     * @param   void
     * @return  void
     * 
     * @usage  Main part for creating the bot
     */
    
    
public function __construct () {
        
        
        
$this->BotConfig = array(
        
            
/**
             * Bot configuration for information
             */
             
            
'nickname' => 'ViiBot4',
            
'realname' => 'Vii personal bot',
            
'ident'    => 'ViiBot',
            
            
/**
             * Bot server configration
             */
             
            
'hostname' => 0,
            
'server'   => 'ogn1.ogamenet.net',
            
'port'    => 6667,
            
            
/**
             * Bot channel configration
             */
             
            
'channel'  => array('#Vii')
        );
         
         
         
/**
          * Prepare for bot connection
          */
          
          
if(!$this->ConnectViiBot()) {
                die(
'Connection failed.');
          }
          else {
            
                
$this->ConnectToIrc();
                
$this->BotMainLoop();
          }
    }
    
    
/**
     * @name    ViiIrcBot::ConnectViiBot
     * @param   void
     * @return  boolean
     * 
     * @usage  login to the irc server
     */
    
    
protected function ConnectViiBot() {
        
        
/**
         * Create a socket for bot connection
         */
         
        
$this->BotSocket socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
        if (!
$this->BotSocket) {
            return 
false;
        }
        
        
/**
         * Bind the socket to make it irc'able 
         */
         
        
if(!socket_bind($this->BotSocket$this->BotConfig['hostname'])) { 
            return 
false;
        } 
        if(!
socket_connect($this->BotSocket$this->BotConfig['server'], $this->BotConfig['port'])) { 
            return 
false;
        } 
        
        
/**
         * Return positive result
         */
        
return $this->BotSocket;
    }
    
    
/**
     * @name    ViiIrcBot::ConnectToIrc
     * @param   void
     * @return  boolean
     * 
     * @usage  Connect to the irc server
     */
    
    
protected function ConnectToIrc () {
        
        
/**
         * Utilize connection
         */
        
        
$this->RawWrite('USER ' $this->BotConfig['ident']. ' ' $this->BotConfig['hostname'] . ' ' $this->BotConfig['server'] . ' :'$this->BotConfig['realname']); 
                        
        
/**
         * Define bot nickname to use
         * on the server
         */
                        
        
$this->RawWrite('NICK ' $this->BotConfig['nickname']);
        return 
true
    }
    
    
/**
     * @name    ViiIrcBot::RawWrite
     * @param   $Parameter
     * @return  void
     * 
     * @usage   dump an raw line in to the server
     */  
    
    
protected function RawWrite ($Parameter) {
        
        
socket_write($this->BotSocket$Parameter "rn");
        
    }

    
/**
     * @name    ViiIrcBot::DefineRawLine
     * @param   void
     * @return  Void
     * 
     * @usage   Defining usable parameters for commands
     */ 
    
    
protected function DefineRawLine($RawLine) {
        
$this->Parameter['hostmask'] = $RawLine[0];
        
$this->Parameter['servercmd'] = $RawLine[1];
        
$this->Parameter['location'] = $RawLine[2];
        
$this->Parameter['command'] = trim(substr($RawLine[3], 1));
                
        if (!empty(
$this->Parameter['command'])) {
            
$ExplodingPar explode($RawLine[3], $this->BotData);
            
$this->Parameter['parameters'] = $ExplodingPar[1];
        }
        
        
/**
         * Splitting hostmask for usable things
         * $this->Parameter['hostmask'] = Vii`!Vii@Vii.gameoperator.OGameNl
         */
         
         
if (preg_match('/^([^!@]+)!(?:[ni]=)?([^@]+)@([^ ]+)/'$this->Parameter['hostmask'])) {
            
$TempStorage preg_match('/^([^!@]+)!(?:[ni]=)?([^@]+)@([^ ]+)/'$this->Parameter['hostmask'], $HostArray);
            
$this->Parameter['hostmask'] = array();
            
            
$this->Parameter['hostmask']['hostmask'] = substr($HostArray[0], 1);
            
$this->Parameter['hostmask']['nickname'] = substr($HostArray[1], 1);
            
$this->Parameter['hostmask']['ident'] = $HostArray[2];
            
$this->Parameter['hostmask']['banmask'] = '*!*@' $HostArray[3];
         }
         
    }
    
    
/**
     * @name    ViiIrcBot::BotMainLoop
     * @param   void
     * @return  boolean
     * 
     * @usage   Main loop of usage of the bot
     */ 
    
    
protected function BotMainLoop () {
        while(
$this->BotData socket_read($this->BotSocket,65000,PHP_NORMAL_READ)) { 
            
            
/**
             * Even when the line is empty, just continue
             */
             
            
if($this->BotData == "n") { continue; } 
            
            
/**
             * Get usable data from the raw output line of irc
             */
            
            
$ConfiguratingData explode(' '$this->BotData);
            
$this->DefineRawLine($ConfiguratingData);
            
            
/**
             * Go play ping pong with the server
             */
             
            
if($this->Parameter['hostmask'] == 'PING') { 
                
$this->RawWrite('PONG '$this->Parameter['servercmd']);  
            } 
            
            
            
/**
             * Other commands
             */
            
            
if ($this->Parameter['command'] == '!raw') {
                
$this->RawWrite($this->Parameter['parameters']);
            }
            
            if (
$this->Parameter['command'] == '!hostmask') {
                
$this->RawWrite('PRIVMSG ' $this->Parameter['location'] . ' :Hostmask:' $this->Parameter['hostmask']['hostmask']);
                
$this->RawWrite('PRIVMSG ' $this->Parameter['location'] . ' :Nickname: ' $this->Parameter['hostmask']['nickname']);
                
$this->RawWrite('PRIVMSG ' $this->Parameter['location'] . ' :Banmask: ' $this->Parameter['hostmask']['banmask']);
                
$this->RawWrite('PRIVMSG ' $this->Parameter['location'] . ' :Identity: ' $this->Parameter['hostmask']['ident']);
            }
                        
            if (
$this->Parameter['command'] == '!parameter') {
                
$this->RawWrite('PRIVMSG ' $this->Parameter['location'] . ' :Server command: ' $this->Parameter['servercmd']);
                
$this->RawWrite('PRIVMSG ' $this->Parameter['location'] . ' :Localation:' $this->Parameter['location']);
                
$this->RawWrite('PRIVMSG ' $this->Parameter['location'] . ' :Command: ' $this->Parameter['command']);
                
$this->RawWrite('PRIVMSG ' $this->Parameter['location'] . ' :Parameters: ' $this->Parameter['parameters']);
            }
                                                            
        }
    }
}

$ViiBot = new ViiIrcBot();
?>


en ja dit is omdat ik niks te doen heb
11-11-2010 17:33
Reageer op: "IRC bot class"
1
Je kan niet reageren omdat je niet bent ingelogd. Inloggen of Aanmelden