source: ipc.c @ 48721c3

Last change on this file since 48721c3 was 48721c3, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-01-17T21:15:42Z

A KILL command. Unfortunately the user doesn't see the KILL message yet. :-(

  • Property mode set to 100644
File size: 7.4 KB
RevLine 
[0431ea1]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2004 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* IPC - communication between BitlBee processes                        */
8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
22  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23  Suite 330, Boston, MA  02111-1307  USA
24*/
25
26#define BITLBEE_CORE
27#include "bitlbee.h"
28#include "ipc.h"
29#include "commands.h"
30
31GSList *child_list = NULL;
32
[74c119d]33
[48721c3]34static int ipc_master_cmd_die( irc_t *irc, char **cmd )
[0431ea1]35{
[74c119d]36        if( global.conf->runmode == RUNMODE_FORKDAEMON )
37                ipc_to_children_str( "DIE\r\n" );
38       
[13caf0a]39        bitlbee_shutdown( NULL );
[74c119d]40       
41        return 1;
[0431ea1]42}
43
[48721c3]44static int ipc_master_cmd_rehash( irc_t *irc, char **cmd )
[0431ea1]45{
[f4a5940]46        runmode_t oldmode;
47       
48        oldmode = global.conf->runmode;
49       
50        g_free( global.conf );
51        global.conf = conf_load( 0, NULL );
52       
53        if( global.conf->runmode != oldmode )
54        {
55                log_message( LOGLVL_WARNING, "Can't change RunMode setting at runtime, restoring original setting" );
56                global.conf->runmode = oldmode;
57        }
58       
59        if( global.conf->runmode == RUNMODE_FORKDAEMON )
60                ipc_to_children( cmd );
[0431ea1]61       
62        return 1;
63}
64
65static const command_t ipc_master_commands[] = {
66        { "die",        0, ipc_master_cmd_die,        0 },
[f4a5940]67        { "wallops",    1, NULL,                      IPC_CMD_TO_CHILDREN },
68        { "lilo",       1, NULL,                      IPC_CMD_TO_CHILDREN },
69        { "rehash",     0, ipc_master_cmd_rehash,     0 },
[48721c3]70        { "kill",       2, NULL,                      IPC_CMD_TO_CHILDREN },
[0431ea1]71        { NULL }
72};
73
[74c119d]74
[48721c3]75static int ipc_child_cmd_die( irc_t *irc, char **cmd )
[74c119d]76{
77        bitlbee_shutdown( NULL );
78       
79        return 1;
80}
81
[48721c3]82static int ipc_child_cmd_wallops( irc_t *irc, char **cmd )
[0431ea1]83{
[daa9e02]84        if( irc->status < USTATUS_LOGGED_IN )
85                return 1;
86       
[0431ea1]87        if( strchr( irc->umode, 'w' ) )
[e0ca412]88                irc_write( irc, ":%s WALLOPS :%s", irc->myhost, cmd[1] );
89       
90        return 1;
91}
92
[48721c3]93static int ipc_child_cmd_lilo( irc_t *irc, char **cmd )
[e0ca412]94{
[daa9e02]95        if( irc->status < USTATUS_LOGGED_IN )
96                return 1;
97       
[74c119d]98        if( strchr( irc->umode, 's' ) )
99                irc_write( irc, ":%s NOTICE %s :%s", irc->myhost, irc->nick, cmd[1] );
[0431ea1]100       
101        return 1;
102}
103
[48721c3]104static int ipc_child_cmd_rehash( irc_t *irc, char **cmd )
[f4a5940]105{
106        runmode_t oldmode;
107       
108        oldmode = global.conf->runmode;
109       
110        g_free( global.conf );
111        global.conf = conf_load( 0, NULL );
112       
113        global.conf->runmode = oldmode;
114       
115        return 1;
116}
117
[48721c3]118static int ipc_child_cmd_kill( irc_t *irc, char **cmd )
119{
120        if( irc->status < USTATUS_LOGGED_IN )
121                return 1;
122       
123        if( nick_cmp( cmd[1], irc->nick ) != 0 )
124                return 1;       /* It's not for us. */
125       
126        irc_write( irc, ":%s!%s@%s KILL %s :%s", irc->mynick, irc->mynick, irc->myhost, irc->nick, cmd[2] );
127        g_io_channel_close( irc->io_channel );
128       
129        return 0;
130}
131
[0431ea1]132static const command_t ipc_child_commands[] = {
[74c119d]133        { "die",        0, ipc_child_cmd_die,         0 },
134        { "wallops",    1, ipc_child_cmd_wallops,     0 },
135        { "lilo",       1, ipc_child_cmd_lilo,        0 },
[48721c3]136        { "rehash",     0, ipc_child_cmd_rehash,      0 },
137        { "kill",       2, ipc_child_cmd_kill,        0 },
[0431ea1]138        { NULL }
139};
140
[74c119d]141
[0431ea1]142static void ipc_command_exec( void *data, char **cmd, const command_t *commands )
143{
144        int i;
145       
146        if( !cmd[0] )
147                return;
148       
149        for( i = 0; commands[i].command; i ++ )
150                if( g_strcasecmp( commands[i].command, cmd[0] ) == 0 )
151                {
[f4a5940]152                        if( commands[i].flags & IPC_CMD_TO_CHILDREN )
153                                ipc_to_children( cmd );
154                        else
155                                commands[i].execute( data, cmd );
156                       
[0431ea1]157                        return;
158                }
159}
160
161static char *ipc_readline( int fd )
162{
163        char *buf, *eol;
164        int size;
165       
166        buf = g_new0( char, 513 );
167       
168        /* Because this is internal communication, it should be pretty safe
169           to just peek at the message, find its length (by searching for the
170           end-of-line) and then just read that message. With internal
171           sockets and limites message length, messages should always be
172           complete. Saves us quite a lot of code and buffering. */
173        size = recv( fd, buf, 512, MSG_PEEK );
174        if( size == 0 || ( size < 0 && !sockerr_again() ) )
175                return NULL;
176        else
177                buf[size] = 0;
178       
179        eol = strstr( buf, "\r\n" );
180        if( eol == NULL )
181                return NULL;
182        else
183                size = eol - buf + 2;
184       
185        g_free( buf );
186        buf = g_new0( char, size + 1 );
187       
188        if( recv( fd, buf, size, 0 ) != size )
189                return NULL;
190        else
191                buf[size-2] = 0;
192       
193        return buf;
194}
195
196void ipc_master_read( gpointer data, gint source, GaimInputCondition cond )
197{
198        char *buf, **cmd;
199       
200        if( ( buf = ipc_readline( source ) ) )
201        {
202                cmd = irc_parse_line( buf );
203                if( cmd )
204                        ipc_command_exec( data, cmd, ipc_master_commands );
205        }
206        else
207        {
208                GSList *l;
209                struct bitlbee_child *c;
210               
211                for( l = child_list; l; l = l->next )
212                {
213                        c = l->data;
214                        if( c->ipc_fd == source )
215                        {
216                                close( c->ipc_fd );
217                                gaim_input_remove( c->ipc_inpa );
218                                g_free( c );
219                               
220                                child_list = g_slist_remove( child_list, l );
221                               
222                                break;
223                        }
224                }
225        }
226}
227
228void ipc_child_read( gpointer data, gint source, GaimInputCondition cond )
229{
230        char *buf, **cmd;
231       
232        if( ( buf = ipc_readline( source ) ) )
233        {
234                cmd = irc_parse_line( buf );
235                if( cmd )
236                        ipc_command_exec( data, cmd, ipc_child_commands );
237        }
238        else
239        {
240                gaim_input_remove( global.listen_watch_source_id );
241                close( global.listen_socket );
242               
243                global.listen_socket = -1;
244        }
245}
246
247void ipc_to_master( char **cmd )
248{
249        if( global.conf->runmode == RUNMODE_FORKDAEMON )
250        {
[74c119d]251                char *s = irc_build_line( cmd );
[f4a5940]252                ipc_to_master_str( s );
253                g_free( s );
254        }
255        else if( global.conf->runmode == RUNMODE_DAEMON )
256        {
257                ipc_command_exec( NULL, cmd, ipc_master_commands );
258        }
259}
260
261void ipc_to_master_str( char *msg_buf )
262{
263        if( global.conf->runmode == RUNMODE_FORKDAEMON )
264        {
265                write( global.listen_socket, msg_buf, strlen( msg_buf ) );
266        }
267        else if( global.conf->runmode == RUNMODE_DAEMON )
268        {
269                char *s, **cmd;
270               
271                /* irc_parse_line() wants a read-write string, so get it one: */
272                s = g_strdup( msg_buf );
273                cmd = irc_parse_line( s );
274               
275                ipc_command_exec( NULL, cmd, ipc_master_commands );
276               
277                g_free( cmd );
[74c119d]278                g_free( s );
279        }
280}
281
282void ipc_to_children( char **cmd )
283{
284        if( global.conf->runmode == RUNMODE_FORKDAEMON )
285        {
286                char *msg_buf = irc_build_line( cmd );
287                ipc_to_children_str( msg_buf );
288                g_free( msg_buf );
289        }
[f4a5940]290        else if( global.conf->runmode == RUNMODE_DAEMON )
291        {
292                GSList *l;
293               
294                for( l = irc_connection_list; l; l = l->next )
295                        ipc_command_exec( l->data, cmd, ipc_child_commands );
296        }
[74c119d]297}
298
299void ipc_to_children_str( char *msg_buf )
300{
301        if( global.conf->runmode == RUNMODE_FORKDAEMON )
302        {
303                int msg_len = strlen( msg_buf );
304                GSList *l;
[0431ea1]305               
[74c119d]306                for( l = child_list; l; l = l->next )
[0431ea1]307                {
[74c119d]308                        struct bitlbee_child *c = l->data;
309                        write( c->ipc_fd, msg_buf, msg_len );
[0431ea1]310                }
311        }
[f4a5940]312        else if( global.conf->runmode == RUNMODE_DAEMON )
313        {
314                char *s, **cmd;
315               
316                /* irc_parse_line() wants a read-write string, so get it one: */
317                s = g_strdup( msg_buf );
318                cmd = irc_parse_line( s );
319               
320                ipc_to_children( cmd );
321               
322                g_free( cmd );
323                g_free( s );
324        }
[0431ea1]325}
Note: See TracBrowser for help on using the repository browser.