Fatal error: Function name must be a string i
Ik ben nu met een irc bot bezig en heb de ondestaande code:
Code (php)
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
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
<?php
class Ircbot
{
private $config = array();
private $actions = array();
public function __construct($server, $port, $channel)
{
$this->config['server'] = $server; //Set $this->config['server'] to the server
$this->config['port'] = $port; //Set $this->config['port'] to the port
$this->config['channel'] = $channel;
}
public function setIdentity($nickname, $realname, $ident)
{
$this->config['identity'] = array('nickname'=>$nickname, 'realname'=>$realname, 'ident'=>$ident); // Set $this->config['identity'] to a array with the identity
}
public function setOwners()
{
$args = func_get_args(); //Get args
$this->config['owners'] = array(); //set $this->config['owners'] to array
foreach($args as $name)
{
array_push($this->config['owners'], $name);
}
}
public function addAction($name, $function, $owner = false)
{
$this->actions[$name] = array('function'=>$function, 'owner'=>$owner); //$this->actions[$name] to $function
}
private function write($data)
{
socket_write($this->socket,$data."\r\n");
}
public function nickname($str)
{
return substr(array_shift(explode('!',$str)),1);
}
public function connect()
{
if(!$this->socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP))
{
echo 'De socket kan niet worden aangemaakt.';
exit();
}
echo 'Socket is aangamaakt.'."\n";
if(isset($this->config['hostname'])) { $hostname = $this->config['hostname']; } else { $hostname = 0; }
if(!socket_bind($this->socket,$hostname))
{
echo 'Verbinding kon niet worden gekoppeld aan hostname '.$hostname;
exit();
}
echo 'Verbinding gekoppeld aan hostname'."\n";
if(!isset($this->config['server']) || !isset($this->config['port']))
{
echo 'De gegevens voor de sever of de poort missen.';
exit();
}
if(!socket_connect($this->socket,$this->config['server'],$this->config['port']))
{
echo 'Verbinding met server kon niet tot stand worden gebracht.';
exit();
}
echo 'Maken van verbinding...'."\n";
if(!isset($this->config['identity']['ident']) || !isset($this->config['identity']['realname']) || !isset($this->config['identity']['nickname']))
{
echo 'Er missen vereiste gegevens. Weet u zeker dat u setIdentity(nickname, realname, ident) heeft gebruikt?';
exit();
}
$this->write('USER '.$this->config['identity']['ident'].' 0 '.$this->config['server'].' :'.$this->config['identity']['realname']);
$this->write('NICK '.$this->config['identity']['nickname']);
$this->inChannel = false;
while($data = socket_read($this->socket,65000,PHP_NORMAL_READ))
{
if($data == "\n") continue;
$eData = explode(" ",$data);
for($i = 0; isset($eData[$i]); $i++) {
$eData[$i] = trim($eData[$i]);
}
echo $data."\n";
if($this->inChannel == false && strstr($data,'MOTD'))
{
$this->write('JOIN '.$this->config['channel']);
$this->inChannel = true;
}
if($eData[0] == 'PING') {
$this->write('PONG '.$eData[1]);
}
if(isset($eData[3]) && preg_match('#:!(.*)#', $eData[3], $matches))
{
$name = $matches[1];
if(isset($this->actions[$name]))
{
$action = $this->actions[$name];
$name = $action['function'];
$name($eData);
}
}
}
}
}
$bot = new Ircbot('irc.chatfactory.net', 6667, '#devhouse');
$bot->setOwners('tomkeim', 'nogiemand', 'iemand anders');
$bot->setIdentity('Test Bot', 'Test Bot', 'testbot');
$action = create_function('$data',"$u = 'Laatste Nieuws op DevHouse:'.\"\n\n\".'Nog niks...';$self->write('NOTICE '.$self->nickname($eData[0]).' : '.$u);");
$bot->addAction('lol', $action);
$bot->connect();
[/code]
Dit werkt opzich goed. Totdat ik !lol intik in mijn irc client dan krijg ik oppeens: Fatal error: Function name must be a string in C:\bot\start.php on line 95.
Ik snap de fout niet want $name is toch een string?
Weet iemand hoe ik dit op kan lossen??
Tom
class Ircbot
{
private $config = array();
private $actions = array();
public function __construct($server, $port, $channel)
{
$this->config['server'] = $server; //Set $this->config['server'] to the server
$this->config['port'] = $port; //Set $this->config['port'] to the port
$this->config['channel'] = $channel;
}
public function setIdentity($nickname, $realname, $ident)
{
$this->config['identity'] = array('nickname'=>$nickname, 'realname'=>$realname, 'ident'=>$ident); // Set $this->config['identity'] to a array with the identity
}
public function setOwners()
{
$args = func_get_args(); //Get args
$this->config['owners'] = array(); //set $this->config['owners'] to array
foreach($args as $name)
{
array_push($this->config['owners'], $name);
}
}
public function addAction($name, $function, $owner = false)
{
$this->actions[$name] = array('function'=>$function, 'owner'=>$owner); //$this->actions[$name] to $function
}
private function write($data)
{
socket_write($this->socket,$data."\r\n");
}
public function nickname($str)
{
return substr(array_shift(explode('!',$str)),1);
}
public function connect()
{
if(!$this->socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP))
{
echo 'De socket kan niet worden aangemaakt.';
exit();
}
echo 'Socket is aangamaakt.'."\n";
if(isset($this->config['hostname'])) { $hostname = $this->config['hostname']; } else { $hostname = 0; }
if(!socket_bind($this->socket,$hostname))
{
echo 'Verbinding kon niet worden gekoppeld aan hostname '.$hostname;
exit();
}
echo 'Verbinding gekoppeld aan hostname'."\n";
if(!isset($this->config['server']) || !isset($this->config['port']))
{
echo 'De gegevens voor de sever of de poort missen.';
exit();
}
if(!socket_connect($this->socket,$this->config['server'],$this->config['port']))
{
echo 'Verbinding met server kon niet tot stand worden gebracht.';
exit();
}
echo 'Maken van verbinding...'."\n";
if(!isset($this->config['identity']['ident']) || !isset($this->config['identity']['realname']) || !isset($this->config['identity']['nickname']))
{
echo 'Er missen vereiste gegevens. Weet u zeker dat u setIdentity(nickname, realname, ident) heeft gebruikt?';
exit();
}
$this->write('USER '.$this->config['identity']['ident'].' 0 '.$this->config['server'].' :'.$this->config['identity']['realname']);
$this->write('NICK '.$this->config['identity']['nickname']);
$this->inChannel = false;
while($data = socket_read($this->socket,65000,PHP_NORMAL_READ))
{
if($data == "\n") continue;
$eData = explode(" ",$data);
for($i = 0; isset($eData[$i]); $i++) {
$eData[$i] = trim($eData[$i]);
}
echo $data."\n";
if($this->inChannel == false && strstr($data,'MOTD'))
{
$this->write('JOIN '.$this->config['channel']);
$this->inChannel = true;
}
if($eData[0] == 'PING') {
$this->write('PONG '.$eData[1]);
}
if(isset($eData[3]) && preg_match('#:!(.*)#', $eData[3], $matches))
{
$name = $matches[1];
if(isset($this->actions[$name]))
{
$action = $this->actions[$name];
$name = $action['function'];
$name($eData);
}
}
}
}
}
$bot = new Ircbot('irc.chatfactory.net', 6667, '#devhouse');
$bot->setOwners('tomkeim', 'nogiemand', 'iemand anders');
$bot->setIdentity('Test Bot', 'Test Bot', 'testbot');
$action = create_function('$data',"$u = 'Laatste Nieuws op DevHouse:'.\"\n\n\".'Nog niks...';$self->write('NOTICE '.$self->nickname($eData[0]).' : '.$u);");
$bot->addAction('lol', $action);
$bot->connect();
[/code]
Dit werkt opzich goed. Totdat ik !lol intik in mijn irc client dan krijg ik oppeens: Fatal error: Function name must be a string in C:\bot\start.php on line 95.
Ik snap de fout niet want $name is toch een string?
Weet iemand hoe ik dit op kan lossen??
Tom
Gesponsorde koppelingen:
Ja, maar wat wil je hier mee:
Een functie, wat jij nu impliceert, mag niet beginnen met een dollar-teken. Maar volgens mij wil je het niet als functie gebruiken, maar door die haakjes zeg je toch dat 't een functie is.
Een functie, wat jij nu impliceert, mag niet beginnen met een dollar-teken. Maar volgens mij wil je het niet als functie gebruiken, maar door die haakjes zeg je toch dat 't een functie is.
ik heb nu hetvolgende:
Code (php)
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
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
<?php
class Ircbot
{
private $config = array();
private $actions = array();
public function __construct($server, $port, $channel)
{
$this->config['server'] = $server; //Set $this->config['server'] to the server
$this->config['port'] = $port; //Set $this->config['port'] to the port
$this->config['channel'] = $channel;
}
public function setIdentity($nickname, $realname, $ident)
{
$this->config['identity'] = array('nickname'=>$nickname, 'realname'=>$realname, 'ident'=>$ident); // Set $this->config['identity'] to a array with the identity
}
public function setOwners()
{
$args = func_get_args(); //Get args
$this->config['owners'] = array(); //set $this->config['owners'] to array
foreach($args as $name)
{
array_push($this->config['owners'], $name);
}
}
public function addAction($name, $function, $owner = false)
{
$this->actions[$name] = array('function'=>$function, 'owner'=>$owner); //$this->actions[$name] to $function
}
public function write($data)
{
socket_write($this->socket,$data."\r\n");
}
public function nickname($str)
{
return substr(array_shift(explode('!',$str)),1);
}
public function connect()
{
if(!$this->socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP))
{
echo 'De socket kan niet worden aangemaakt.';
exit();
}
echo 'Socket is aangamaakt.'."\n";
if(isset($this->config['hostname'])) { $hostname = $this->config['hostname']; } else { $hostname = 0; }
if(!socket_bind($this->socket,$hostname))
{
echo 'Verbinding kon niet worden gekoppeld aan hostname '.$hostname;
exit();
}
echo 'Verbinding gekoppeld aan hostname'."\n";
if(!isset($this->config['server']) || !isset($this->config['port']))
{
echo 'De gegevens voor de sever of de poort missen.';
exit();
}
if(!socket_connect($this->socket,$this->config['server'],$this->config['port']))
{
echo 'Verbinding met server kon niet tot stand worden gebracht.';
exit();
}
echo 'Maken van verbinding...'."\n";
if(!isset($this->config['identity']['ident']) || !isset($this->config['identity']['realname']) || !isset($this->config['identity']['nickname']))
{
echo 'Er missen vereiste gegevens. Weet u zeker dat u setIdentity(nickname, realname, ident) heeft gebruikt?';
exit();
}
$this->write('USER '.$this->config['identity']['ident'].' 0 '.$this->config['server'].' :'.$this->config['identity']['realname']);
$this->write('NICK '.$this->config['identity']['nickname']);
$this->inChannel = false;
while($data = socket_read($this->socket,65000,PHP_NORMAL_READ))
{
if($data == "\n") continue;
$eData = explode(" ",$data);
for($i = 0; isset($eData[$i]); $i++) {
$eData[$i] = trim($eData[$i]);
}
echo $data."\n";
if($this->inChannel == false && strstr($data,'MOTD'))
{
$this->write('JOIN '.$this->config['channel']);
$this->inChannel = true;
}
if($eData[0] == 'PING') {
$this->write('PONG '.$eData[1]);
}
if(isset($eData[3]) && preg_match('#:!(.*)#', $eData[3], $matches))
{
$name = $matches[1];
var_dump($name);
if(isset($this->actions[$name]))
{
$action = $this->actions[$name];
$name = $this->actions[$name]['function'];
$name($eData);
}
}
}
}
}
$bot = new Ircbot('irc.chatfactory.net', 6667, '#devhouse');
$bot->setOwners('tomkeim', 'nogiemand', 'iemand anders');
$bot->setIdentity('Test Bot', 'Test Bot', 'testbot');
$function = '$self->write(\'NOTICE \'.$self->nickname($data[0]).\' : Dit is een test:P\');';
$action = create_function('$data',$function);
$bot->addAction('lol', $action);
$bot->connect();
[/code]
dit werkt nu beter alleen als ik nu !lol tegen mijn irc bot zal zeggen, gaat hij uit, en zie ik in cmd de volgende error: Fatal error: Call to a member function write() on a non-object in C:\bot\start.p
hp(110) : runtime-created function on line 1
Als ik $self vervang door $this werkt het ook niet...
edit; sorry voor de bump, dacht dat er wat tussenstond
class Ircbot
{
private $config = array();
private $actions = array();
public function __construct($server, $port, $channel)
{
$this->config['server'] = $server; //Set $this->config['server'] to the server
$this->config['port'] = $port; //Set $this->config['port'] to the port
$this->config['channel'] = $channel;
}
public function setIdentity($nickname, $realname, $ident)
{
$this->config['identity'] = array('nickname'=>$nickname, 'realname'=>$realname, 'ident'=>$ident); // Set $this->config['identity'] to a array with the identity
}
public function setOwners()
{
$args = func_get_args(); //Get args
$this->config['owners'] = array(); //set $this->config['owners'] to array
foreach($args as $name)
{
array_push($this->config['owners'], $name);
}
}
public function addAction($name, $function, $owner = false)
{
$this->actions[$name] = array('function'=>$function, 'owner'=>$owner); //$this->actions[$name] to $function
}
public function write($data)
{
socket_write($this->socket,$data."\r\n");
}
public function nickname($str)
{
return substr(array_shift(explode('!',$str)),1);
}
public function connect()
{
if(!$this->socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP))
{
echo 'De socket kan niet worden aangemaakt.';
exit();
}
echo 'Socket is aangamaakt.'."\n";
if(isset($this->config['hostname'])) { $hostname = $this->config['hostname']; } else { $hostname = 0; }
if(!socket_bind($this->socket,$hostname))
{
echo 'Verbinding kon niet worden gekoppeld aan hostname '.$hostname;
exit();
}
echo 'Verbinding gekoppeld aan hostname'."\n";
if(!isset($this->config['server']) || !isset($this->config['port']))
{
echo 'De gegevens voor de sever of de poort missen.';
exit();
}
if(!socket_connect($this->socket,$this->config['server'],$this->config['port']))
{
echo 'Verbinding met server kon niet tot stand worden gebracht.';
exit();
}
echo 'Maken van verbinding...'."\n";
if(!isset($this->config['identity']['ident']) || !isset($this->config['identity']['realname']) || !isset($this->config['identity']['nickname']))
{
echo 'Er missen vereiste gegevens. Weet u zeker dat u setIdentity(nickname, realname, ident) heeft gebruikt?';
exit();
}
$this->write('USER '.$this->config['identity']['ident'].' 0 '.$this->config['server'].' :'.$this->config['identity']['realname']);
$this->write('NICK '.$this->config['identity']['nickname']);
$this->inChannel = false;
while($data = socket_read($this->socket,65000,PHP_NORMAL_READ))
{
if($data == "\n") continue;
$eData = explode(" ",$data);
for($i = 0; isset($eData[$i]); $i++) {
$eData[$i] = trim($eData[$i]);
}
echo $data."\n";
if($this->inChannel == false && strstr($data,'MOTD'))
{
$this->write('JOIN '.$this->config['channel']);
$this->inChannel = true;
}
if($eData[0] == 'PING') {
$this->write('PONG '.$eData[1]);
}
if(isset($eData[3]) && preg_match('#:!(.*)#', $eData[3], $matches))
{
$name = $matches[1];
var_dump($name);
if(isset($this->actions[$name]))
{
$action = $this->actions[$name];
$name = $this->actions[$name]['function'];
$name($eData);
}
}
}
}
}
$bot = new Ircbot('irc.chatfactory.net', 6667, '#devhouse');
$bot->setOwners('tomkeim', 'nogiemand', 'iemand anders');
$bot->setIdentity('Test Bot', 'Test Bot', 'testbot');
$function = '$self->write(\'NOTICE \'.$self->nickname($data[0]).\' : Dit is een test:P\');';
$action = create_function('$data',$function);
$bot->addAction('lol', $action);
$bot->connect();
[/code]
dit werkt nu beter alleen als ik nu !lol tegen mijn irc bot zal zeggen, gaat hij uit, en zie ik in cmd de volgende error: Fatal error: Call to a member function write() on a non-object in C:\bot\start.p
hp(110) : runtime-created function on line 1
Als ik $self vervang door $this werkt het ook niet...
edit; sorry voor de bump, dacht dat er wat tussenstond
Gewijzigd op 01/01/1970 01:00:00 door Tom K



