]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc.c
- Adjusted includes for new "conn-func.h" header.
[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.113 2002/12/30 17:15:42 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 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                 /* Yes, there is such a client -- but is it a valid user? */
88                 if( Client_Type( c ) == CLIENT_SERVER ) IRC_WriteStrClient( Client, ERR_CANTKILLSERVER_MSG, Client_ID( Client ));
89                 else if( Client_Type( c ) != CLIENT_USER  )IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
90                 else
91                 {
92                         /* Kill user NOW! */
93                         conn = Client_Conn( c );
94                         Client_Destroy( c, NULL, reason, FALSE );
95                         if( conn != NONE ) Conn_Close( Client_Conn( c ), NULL, reason, TRUE );
96                 }
97         }
98         else Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
99
100         /* Are we still connected or were we killed, too? */
101         if( Client_Search( Req->argv[0] )) return CONNECTED;
102         else return DISCONNECTED;
103 } /* IRC_KILL */
104
105
106 GLOBAL BOOLEAN
107 IRC_NOTICE( CLIENT *Client, REQUEST *Req )
108 {
109         CLIENT *to, *from;
110
111         assert( Client != NULL );
112         assert( Req != NULL );
113
114         if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return CONNECTED;
115
116         /* Falsche Anzahl Parameter? */
117         if( Req->argc != 2 ) return CONNECTED;
118
119         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
120         else from = Client;
121         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
122
123         to = Client_Search( Req->argv[0] );
124         if(( to ) && ( Client_Type( to ) == CLIENT_USER ))
125         {
126                 /* Okay, Ziel ist ein User */
127                 return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
128         }
129         else return CONNECTED;
130 } /* IRC_NOTICE */
131
132
133 GLOBAL BOOLEAN
134 IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
135 {
136         CLIENT *cl, *from;
137         CHANNEL *chan;
138         
139         assert( Client != NULL );
140         assert( Req != NULL );
141
142         /* Falsche Anzahl Parameter? */
143         if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
144         if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
145         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
146
147         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
148         else from = Client;
149         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
150
151         cl = Client_Search( Req->argv[0] );
152         if( cl )
153         {
154                 /* Okay, Ziel ist ein Client. Aber ist es auch ein User? */
155                 if( Client_Type( cl ) != CLIENT_USER ) return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
156
157                 /* Okay, Ziel ist ein User */
158                 if(( Client_Type( Client ) != CLIENT_SERVER ) && ( strchr( Client_Modes( cl ), 'a' )))
159                 {
160                         /* Ziel-User ist AWAY: Meldung verschicken */
161                         if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( cl ), Client_Away( cl ))) return DISCONNECTED;
162                 }
163
164                 /* Text senden */
165                 if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
166                 return IRC_WriteStrClientPrefix( cl, from, "PRIVMSG %s :%s", Client_ID( cl ), Req->argv[1] );
167         }
168
169         chan = Channel_Search( Req->argv[0] );
170         if( chan ) return Channel_Write( chan, from, Client, Req->argv[1] );
171
172         return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
173 } /* IRC_PRIVMSG */
174
175
176 /* -eof- */