]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc.c
- fixed KILL: you can kill yourself now without crashing the server;
[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.110 2002/12/26 18:41:00 alex Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "conn.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 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         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         /* Do we host such a client? */
84         c = Client_Search( Req->argv[0] );
85         if( c )
86         {
87                 /* Ja, wir haben einen solchen Client */
88                 conn = Client_Conn( c );
89                 Client_Destroy( c, NULL, reason, FALSE );
90                 if( conn != NONE ) Conn_Close( Client_Conn( c ), NULL, reason, TRUE );
91         }
92         else Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
93
94         /* Are we still connected or were we killed, too? */
95         if( Client_Search( Req->argv[0] )) return CONNECTED;
96         else return DISCONNECTED;
97 } /* IRC_KILL */
98
99
100 GLOBAL BOOLEAN
101 IRC_NOTICE( CLIENT *Client, REQUEST *Req )
102 {
103         CLIENT *to, *from;
104
105         assert( Client != NULL );
106         assert( Req != NULL );
107
108         if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return CONNECTED;
109
110         /* Falsche Anzahl Parameter? */
111         if( Req->argc != 2 ) return CONNECTED;
112
113         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
114         else from = Client;
115         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
116
117         to = Client_Search( Req->argv[0] );
118         if(( to ) && ( Client_Type( to ) == CLIENT_USER ))
119         {
120                 /* Okay, Ziel ist ein User */
121                 return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
122         }
123         else return CONNECTED;
124 } /* IRC_NOTICE */
125
126
127 GLOBAL BOOLEAN
128 IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
129 {
130         CLIENT *cl, *from;
131         CHANNEL *chan;
132         
133         assert( Client != NULL );
134         assert( Req != NULL );
135
136         /* Falsche Anzahl Parameter? */
137         if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
138         if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
139         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
140
141         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
142         else from = Client;
143         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
144
145         cl = Client_Search( Req->argv[0] );
146         if( cl )
147         {
148                 /* Okay, Ziel ist ein Client. Aber ist es auch ein User? */
149                 if( Client_Type( cl ) != CLIENT_USER ) return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
150
151                 /* Okay, Ziel ist ein User */
152                 if(( Client_Type( Client ) != CLIENT_SERVER ) && ( strchr( Client_Modes( cl ), 'a' )))
153                 {
154                         /* Ziel-User ist AWAY: Meldung verschicken */
155                         if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( cl ), Client_Away( cl ))) return DISCONNECTED;
156                 }
157
158                 /* Text senden */
159                 if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
160                 return IRC_WriteStrClientPrefix( cl, from, "PRIVMSG %s :%s", Client_ID( cl ), Req->argv[1] );
161         }
162
163         chan = Channel_Search( Req->argv[0] );
164         if( chan ) return Channel_Write( chan, from, Client, Req->argv[1] );
165
166         return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
167 } /* IRC_PRIVMSG */
168
169
170 /* -eof- */