]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc.c
- Added "HELP" command.
[ngircd-alex.git] / src / ngircd / irc.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  *
11  * IRC commands
12  */
13
14
15 #include "portab.h"
16
17 static char UNUSED id[] = "$Id: irc.c,v 1.118 2003/01/15 13:49:20 alex Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "ngircd.h"
25 #include "conn.h"
26 #include "resolve.h"
27 #include "conf.h"
28 #include "conn-func.h"
29 #include "client.h"
30 #include "channel.h"
31 #include "defines.h"
32 #include "irc-write.h"
33 #include "log.h"
34 #include "messages.h"
35 #include "parse.h"
36
37 #include "exp.h"
38 #include "irc.h"
39
40
41 GLOBAL BOOLEAN
42 IRC_ERROR( CLIENT *Client, REQUEST *Req )
43 {
44         assert( Client != NULL );
45         assert( Req != NULL );
46
47         if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
48         else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
49
50         return CONNECTED;
51 } /* IRC_ERROR */
52
53
54 GLOBAL BOOLEAN
55 IRC_KILL( CLIENT *Client, REQUEST *Req )
56 {
57         CLIENT *prefix, *c;
58         CHAR reason[COMMAND_LEN];
59         CONN_ID my_conn, conn;
60
61         assert( Client != NULL );
62         assert( Req != NULL );
63
64         /* Is the user an IRC operator? */
65         if(( Client_Type( Client ) != CLIENT_SERVER ) && ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
66
67         /* Bad number of parameters? */
68         if(( Req->argc != 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
69
70         if( Req->prefix ) prefix = Client_Search( Req->prefix );
71         else prefix = Client;
72         if( ! prefix )
73         {
74                 Log( LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!", Req->prefix );
75                 prefix = Client_ThisServer( );
76         }
77
78         if( Client != Client_ThisServer( )) Log( LOG_NOTICE|LOG_snotice, "Got KILL command from \"%s\" for \"%s\": %s", Client_Mask( prefix ), Req->argv[0], Req->argv[1] );
79
80         /* Build reason string */
81         if( Client_Type( Client ) == CLIENT_USER ) snprintf( reason, sizeof( reason ), "KILLed by %s: %s", Client_ID( Client ), Req->argv[1] );
82         else strlcpy( reason, Req->argv[1], sizeof( reason ));
83
84         /* Inform other servers */
85         IRC_WriteStrServersPrefix( Client, prefix, "KILL %s :%s", Req->argv[0], reason );
86
87         /* Save ID of this connection */
88         my_conn = Client_Conn( Client );
89         
90         /* Do we host such a client? */
91         c = Client_Search( Req->argv[0] );
92         if( c )
93         {
94                 /* Yes, there is such a client -- but is it a valid user? */
95                 if( Client_Type( c ) == CLIENT_SERVER ) IRC_WriteStrClient( Client, ERR_CANTKILLSERVER_MSG, Client_ID( Client ));
96                 else if( Client_Type( c ) != CLIENT_USER  )IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
97                 else
98                 {
99                         /* Kill user NOW! */
100                         conn = Client_Conn( c );
101                         Client_Destroy( c, NULL, reason, FALSE );
102                         if( conn != NONE ) Conn_Close( conn, NULL, reason, TRUE );
103                 }
104         }
105         else Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
106
107         /* Are we still connected or were we killed, too? */
108         if(( my_conn > NONE ) && ( Client_GetFromConn( my_conn ))) return CONNECTED;
109         else return DISCONNECTED;
110 } /* IRC_KILL */
111
112
113 GLOBAL BOOLEAN
114 IRC_NOTICE( CLIENT *Client, REQUEST *Req )
115 {
116         CLIENT *to, *from;
117
118         assert( Client != NULL );
119         assert( Req != NULL );
120
121         if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return CONNECTED;
122
123         /* Falsche Anzahl Parameter? */
124         if( Req->argc != 2 ) return CONNECTED;
125
126         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
127         else from = Client;
128         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
129
130         to = Client_Search( Req->argv[0] );
131         if(( to ) && ( Client_Type( to ) == CLIENT_USER ))
132         {
133                 /* Okay, Ziel ist ein User */
134                 return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
135         }
136         else return CONNECTED;
137 } /* IRC_NOTICE */
138
139
140 GLOBAL BOOLEAN
141 IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
142 {
143         CLIENT *cl, *from;
144         CHANNEL *chan;
145         
146         assert( Client != NULL );
147         assert( Req != NULL );
148
149         /* Falsche Anzahl Parameter? */
150         if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
151         if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
152         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
153
154         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
155         else from = Client;
156         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
157
158         cl = Client_Search( Req->argv[0] );
159         if( cl )
160         {
161                 /* Okay, Ziel ist ein Client. Aber ist es auch ein User? */
162                 if( Client_Type( cl ) != CLIENT_USER ) return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
163
164                 /* Okay, Ziel ist ein User */
165                 if(( Client_Type( Client ) != CLIENT_SERVER ) && ( strchr( Client_Modes( cl ), 'a' )))
166                 {
167                         /* Ziel-User ist AWAY: Meldung verschicken */
168                         if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( cl ), Client_Away( cl ))) return DISCONNECTED;
169                 }
170
171                 /* Text senden */
172                 if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
173                 return IRC_WriteStrClientPrefix( cl, from, "PRIVMSG %s :%s", Client_ID( cl ), Req->argv[1] );
174         }
175
176         chan = Channel_Search( Req->argv[0] );
177         if( chan ) return Channel_Write( chan, from, Client, Req->argv[1] );
178
179         return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
180 } /* IRC_PRIVMSG */
181
182
183 GLOBAL BOOLEAN
184 IRC_TRACE( CLIENT *Client, REQUEST *Req )
185 {
186         CLIENT *from, *target;
187         CONN_ID idx, idx2;
188         CHAR ver[64], *ptr;
189
190         assert( Client != NULL );
191         assert( Req != NULL );
192
193         /* Bad number of arguments? */
194         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
195
196         /* Search sender */
197         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
198         else from = Client;
199         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
200
201         /* Search target */
202         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
203         else target = Client_ThisServer( );
204         
205         strlcpy( ver, NGIRCd_VersionAddition( ), sizeof( ver ));
206         ptr = strchr( ver, '-' );
207         if( ptr ) *ptr = '\0';
208
209         /* Forward command to other server? */
210         if( target != Client_ThisServer( ))
211         {
212                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
213
214                 /* Send RPL_TRACELINK back to initiator */
215                 idx = Client_Conn( Client ); assert( idx > NONE );
216                 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
217                 if( ! IRC_WriteStrClient( from, RPL_TRACELINK_MSG, Client_ID( from ), PACKAGE, VERSION, Client_ID( target ), Client_ID( Client_NextHop( target )), ver, time( NULL ) - Conn_StartTime( idx ), Conn_SendQ( idx ), Conn_SendQ( idx2 ))) return DISCONNECTED;
218
219                 /* Forward command */
220                 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
221                 return CONNECTED;
222         }
223
224         if( ! IRC_WriteStrClient( from, RPL_TRACESERVER_MSG, Client_ID( from ), Conf_ServerName, Client_Mask( Client_ThisServer( )), ver )) return DISCONNECTED;
225         return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE, VERSION, NGIRCd_DebugLevel );
226 } /* IRC_TRACE */
227
228
229 GLOBAL BOOLEAN
230 IRC_HELP( CLIENT *Client, REQUEST *Req )
231 {
232         COMMAND *cmd;
233
234         assert( Client != NULL );
235         assert( Req != NULL );
236
237         /* Bad number of arguments? */
238         if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
239
240         cmd = Parse_GetCommandStruct( );
241         while( cmd->name )
242         {
243                 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
244                 cmd++;
245         }
246         return CONNECTED;
247 } /* IRC_HELP */
248
249
250 /* -eof- */