-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcbapi.php
143 lines (129 loc) · 4.33 KB
/
cbapi.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?PHP
$port = 58945;
function getdb() {
return unserialize( file_get_contents( '/home/phpserv/phpserv/cb_users.db' ) );
}
function printheader() {
return 'Nick:Points:IgnoreFlag:AdminFlag:VerboseSetting:VDeductionsSetting:VLogSetting:FirstSeenTime:NormalSentences:AbnormalSentences:NoVowels:OLDLameAbbreviations:AdministrativelyChanged:CluefulSentences:AllCaps:LameAbbreviations:LowercaseI:Profanity:NonprintableASCII' . "\n";
}
function printusage() {
return "Usage:\n"
. " cluebot dump [Nick1,Nick2,...]\n"
. " cluebot points [Nick1,Nick2,...]\n"
. " cluebot shortpoints <Nick>\n"
. " cluebot dumpheader\n";
}
function printfullentry( $nick, $entry ) {
return $nick
. ':' . $entry[ 'points' ]
. ':' . $entry[ 'ignore' ]
. ':' . $entry[ 'admin' ]
. ':' . $entry[ 'verbose' ]
. ':' . $entry[ 'vdedo' ]
. ':' . $entry[ 'vlog' ]
. ':' . $entry[ 'creation' ]
. ':' . $entry[ 'log' ][ 'Normal sentence +1' ]
. ':' . $entry[ 'log' ][ 'Abnormal sentence -1' ]
. ':' . $entry[ 'log' ][ 'No vowels -30' ]
. ':' . $entry[ 'log' ][ 'Use of i, r, R, u, or U -40' ]
. ':' . $entry[ 'log' ][ 'Administratively changed' ]
. ':' . $entry[ 'log' ][ 'Clueful sentence +2' ]
. ':' . $entry[ 'log' ][ 'All caps -20' ]
. ':' . $entry[ 'log' ][ 'Use of r, R, u, or U -40' ]
. ':' . $entry[ 'log' ][ 'Lower-case personal pronoun -5' ]
. ':' . $entry[ 'log' ][ 'Use of profanity -20' ]
. ':' . $entry[ 'log' ][ 'Use of non-printable ascii characters -5' ]
. "\n";
}
function printpointsentry( $nick, $entry ) {
return $nick
. ':' . $entry[ 'points' ]
. "\n";
}
function printshortpointsentry( $nick, $entry ) {
$p = $entry[ 'points' ];
if( !isset( $entry[ 'points' ] ) or !is_numeric( $entry[ 'points' ] ) or $entry[ 'points' ] == '')
$p = 0;
return $p . "\n";
}
function printentry( $nick, $what, $entry ) {
switch( $what ) {
case 'full':
return printfullentry( $nick, $entry );
case 'points':
return printpointsentry( $nick, $entry );
case 'shortpoints':
return printshortpointsentry( $nick, $entry );
case 'header':
return printheader();
case 'usage':
return printusage();
}
}
function dumpentries( $sock, $what, $nicks ) {
$data = getdb();
if( $nicks == NULL )
foreach( $data as $nick => $entry )
fwrite( $sock, printentry( $nick, $what, $entry ) );
elseif( is_array( $nicks ) )
foreach( $nicks as $nick )
fwrite( $sock, printentry( $nick, $what, $data[ strtolower( $nick ) ] ) );
else
dumpentries( $sock, $what, explode( ',', $nicks ) );
}
$serversock = stream_socket_server( 'tcp://0.0.0.0:'.$port, $errno, $errstr );
$clients = array();
while( true ) {
$read = array_merge( array( $serversock ), $clients );
$waiting = stream_select( $read, $write = NULL, $except = NULL, NULL );
if( $waiting !== FALSE ) {
foreach( $read as $key => $sock ) {
if( ( !is_resource( $sock ) or feof( $sock ) ) and in_array( $sock, $clients, true ) ) {
fclose( $sock );
unset( $read[ $key ] );
foreach( $clients as $k => $client )
if( $sock === $client )
unset( $clients[ $k ] );
}
if( $sock === $serversock )
$clients[] = stream_socket_accept( $sock );
elseif( in_array( $sock, $clients, true ) ) {
$line = str_replace( array( "\r", "\n" ), '', fgets( $sock, 4096 ) );
$parts = explode( ' ', $line );
$command = $parts[ 0 ];
switch( strtolower( $command ) ) {
case 'dump':
if( isset( $parts[ 1 ] ) )
dumpentries( $sock, 'full', $parts[ 1 ] );
else
dumpentries( $sock, 'full', NULL );
break;
case 'points':
if( isset( $parts[ 1 ] ) )
dumpentries( $sock, 'points', $parts[ 1 ] );
else
dumpentries( $sock, 'points', NULL );
break;
case 'shortpoints':
if( isset( $parts[ 1 ] ) )
dumpentries( $sock, 'shortpoints', $parts[ 1 ] );
else
fwrite( $sock, printentry( NULL, 'usage', NULL ) );
break;
case 'dumpheader':
fwrite( $sock, printentry( NULL, 'header', NULL ) );
break;
default:
fwrite( $sock, printentry( NULL, 'usage', NULL ) );
break;
}
foreach( $clients as $k => $client )
if( $sock === $client )
unset( $clients[ $k ] );
fclose( $sock );
unset( $read[ $key ] );
}
}
}
}
?>