]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc.c
- Enhanced IRC_KILL to get along with Client==Client_ThisServer() for "fake KILLs".
[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.116 2003/01/08 22:27:13 alex Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "conn-func.h"
25 #include "client.h"
26 #include "channel.h"
27 #include "defines.h"
28 #include "irc-write.h"
29 #include "log.h"
30 #include "messages.h"
31 #include "parse.h"
32
33 #include "exp.h"
34 #include "irc.h"
35
36
37 GLOBAL BOOLEAN
38 IRC_ERROR( CLIENT *Client, REQUEST *Req )
39 {
40         assert( Client != NULL );
41         assert( Req != NULL );
42
43         if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
44         else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
45
46         return CONNECTED;
47 } /* IRC_ERROR */
48
49
50 GLOBAL BOOLEAN
51 IRC_KILL( CLIENT *Client, REQUEST *Req )
52 {
53         CLIENT *prefix, *c;
54         CHAR reason[COMMAND_LEN];
55         CONN_ID my_conn, conn;
56
57         assert( Client != NULL );
58         assert( Req != NULL );
59
60         /* Is the user an IRC operator? */
61         if(( Client_Type( Client ) != CLIENT_SERVER ) && ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
62
63         /* Bad number of parameters? */
64         if(( Req->argc != 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
65
66         if( Req->prefix ) prefix = Client_Search( Req->prefix );
67         else prefix = Client;
68         if( ! prefix )
69         {
70                 Log( LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!", Req->prefix );
71                 prefix = Client_ThisServer( );
72         }
73
74         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] );
75
76         /* Build reason string */
77         if( Client_Type( Client ) == CLIENT_USER ) snprintf( reason, sizeof( reason ), "KILLed by %s: %s", Client_ID( Client ), Req->argv[1] );
78         else strlcpy( reason, Req->argv[1], sizeof( reason ));
79
80         /* Inform other servers */
81         IRC_WriteStrServersPrefix( Client, prefix, "KILL %s :%s", Req->argv[0], reason );
82
83         /* Save ID of this connection */
84         my_conn = Client_Conn( Client );
85         
86         /* Do we host such a client? */
87         c = Client_Search( Req->argv[0] );
88         if( c )
89         {
90                 /* Yes, there is such a client -- but is it a valid user? */
91                 if( Client_Type( c ) == CLIENT_SERVER ) IRC_WriteStrClient( Client, ERR_CANTKILLSERVER_MSG, Client_ID( Client ));
92                 else if( Client_Type( c ) != CLIENT_USER  )IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
93                 else
94                 {
95                         /* Kill user NOW! */
96                         conn = Client_Conn( c );
97                         Client_Destroy( c, NULL, reason, FALSE );
98                         if( conn != NONE ) Conn_Close( conn, NULL, reason, TRUE );
99                 }
100         }
101         else Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
102
103         /* Are we still connected or were we killed, too? */
104         if(( my_conn > NONE ) && ( Client_GetFromConn( my_conn ))) return CONNECTED;
105         else return DISCONNECTED;
106 } /* IRC_KILL */
107
108
109 GLOBAL BOOLEAN
110 IRC_NOTICE( CLIENT *Client, REQUEST *Req )
111 {
112         CLIENT *to, *from;
113
114         assert( Client != NULL );
115         assert( Req != NULL );
116
117         if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return CONNECTED;
118
119         /* Falsche Anzahl Parameter? */
120         if( Req->argc != 2 ) return CONNECTED;
121
122         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
123         else from = Client;
124         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
125
126         to = Client_Search( Req->argv[0] );
127         if(( to ) && ( Client_Type( to ) == CLIENT_USER ))
128         {
129                 /* Okay, Ziel ist ein User */
130                 return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
131         }
132         else return CONNECTED;
133 } /* IRC_NOTICE */
134
135
136 GLOBAL BOOLEAN
137 IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
138 {
139         CLIENT *cl, *from;
140         CHANNEL *chan;
141         
142         assert( Client != NULL );
143         assert( Req != NULL );
144
145         /* Falsche Anzahl Parameter? */
146         if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
147         if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
148         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
149
150         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
151         else from = Client;
152         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
153
154         cl = Client_Search( Req->argv[0] );
155         if( cl )
156         {
157                 /* Okay, Ziel ist ein Client. Aber ist es auch ein User? */
158                 if( Client_Type( cl ) != CLIENT_USER ) return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
159
160                 /* Okay, Ziel ist ein User */
161                 if(( Client_Type( Client ) != CLIENT_SERVER ) && ( strchr( Client_Modes( cl ), 'a' )))
162                 {
163                         /* Ziel-User ist AWAY: Meldung verschicken */
164                         if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( cl ), Client_Away( cl ))) return DISCONNECTED;
165                 }
166
167                 /* Text senden */
168                 if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
169                 return IRC_WriteStrClientPrefix( cl, from, "PRIVMSG %s :%s", Client_ID( cl ), Req->argv[1] );
170         }
171
172         chan = Channel_Search( Req->argv[0] );
173         if( chan ) return Channel_Write( chan, from, Client, Req->argv[1] );
174
175         return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
176 } /* IRC_PRIVMSG */
177
178
179 /* -eof- */