source: ipc.c @ 13caf0a

Last change on this file since 13caf0a was 13caf0a, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-01-15T14:16:49Z

Better implementation of /DIE

  • Property mode set to 100644
File size: 5.0 KB
Line 
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
33static int ipc_master_cmd_die( irc_t *data, char **cmd )
34{
35        bitlbee_shutdown( NULL );
36}
37
38static int ipc_master_cmd_wallops( irc_t *data, char **cmd )
39{
40        GSList *l;
41        char msg_buf[513];
42        int msg_len;
43       
44        if( ( msg_len = g_snprintf( msg_buf, sizeof( msg_buf ) - 1, "%s :%s\r\n", cmd[0], cmd[1] ) ) > ( sizeof( msg_buf ) - 1 ) )
45                return 0;
46       
47        for( l = child_list; l; l = l->next )
48        {
49                struct bitlbee_child *c = l->data;
50                write( c->ipc_fd, msg_buf, msg_len );
51        }
52       
53        return 1;
54}
55
56static const command_t ipc_master_commands[] = {
57        { "die",        0, ipc_master_cmd_die,        0 },
58        { "wallops",    1, ipc_master_cmd_wallops,    1 },
59        { "lilo",       1, ipc_master_cmd_wallops,    1 },
60        { NULL }
61};
62
63static int ipc_child_cmd_wallops( irc_t *data, char **cmd )
64{
65        irc_t *irc = data;
66       
67        if( strchr( irc->umode, 'w' ) )
68                irc_write( irc, ":%s WALLOPS :%s", irc->myhost, cmd[1] );
69       
70        return 1;
71}
72
73static int ipc_child_cmd_lilo( irc_t *data, char **cmd )
74{
75        irc_t *irc = data;
76       
77        irc_write( irc, ":%s NOTICE %s :%s", irc->myhost, irc->nick, cmd[1] );
78       
79        return 1;
80}
81
82static const command_t ipc_child_commands[] = {
83        { "wallops",    1, ipc_child_cmd_wallops,     1 },
84        { "lilo",       1, ipc_child_cmd_lilo,        1 },
85        { NULL }
86};
87
88static void ipc_command_exec( void *data, char **cmd, const command_t *commands )
89{
90        int i;
91       
92        if( !cmd[0] )
93                return;
94       
95        for( i = 0; commands[i].command; i ++ )
96                if( g_strcasecmp( commands[i].command, cmd[0] ) == 0 )
97                {
98                        commands[i].execute( data, cmd );
99                        return;
100                }
101}
102
103static char *ipc_readline( int fd )
104{
105        char *buf, *eol;
106        int size;
107       
108        buf = g_new0( char, 513 );
109       
110        /* Because this is internal communication, it should be pretty safe
111           to just peek at the message, find its length (by searching for the
112           end-of-line) and then just read that message. With internal
113           sockets and limites message length, messages should always be
114           complete. Saves us quite a lot of code and buffering. */
115        size = recv( fd, buf, 512, MSG_PEEK );
116        if( size == 0 || ( size < 0 && !sockerr_again() ) )
117                return NULL;
118        else
119                buf[size] = 0;
120       
121        eol = strstr( buf, "\r\n" );
122        if( eol == NULL )
123                return NULL;
124        else
125                size = eol - buf + 2;
126       
127        g_free( buf );
128        buf = g_new0( char, size + 1 );
129       
130        if( recv( fd, buf, size, 0 ) != size )
131                return NULL;
132        else
133                buf[size-2] = 0;
134       
135        return buf;
136}
137
138void ipc_master_read( gpointer data, gint source, GaimInputCondition cond )
139{
140        char *buf, **cmd;
141       
142        if( ( buf = ipc_readline( source ) ) )
143        {
144                cmd = irc_parse_line( buf );
145                if( cmd )
146                        ipc_command_exec( data, cmd, ipc_master_commands );
147        }
148        else
149        {
150                GSList *l;
151                struct bitlbee_child *c;
152               
153                for( l = child_list; l; l = l->next )
154                {
155                        c = l->data;
156                        if( c->ipc_fd == source )
157                        {
158                                close( c->ipc_fd );
159                                gaim_input_remove( c->ipc_inpa );
160                                g_free( c );
161                               
162                                child_list = g_slist_remove( child_list, l );
163                               
164                                break;
165                        }
166                }
167        }
168}
169
170void ipc_child_read( gpointer data, gint source, GaimInputCondition cond )
171{
172        char *buf, **cmd;
173       
174        if( ( buf = ipc_readline( source ) ) )
175        {
176                cmd = irc_parse_line( buf );
177                if( cmd )
178                        ipc_command_exec( data, cmd, ipc_child_commands );
179        }
180        else
181        {
182                gaim_input_remove( global.listen_watch_source_id );
183                close( global.listen_socket );
184               
185                global.listen_socket = -1;
186        }
187}
188
189void ipc_to_master( char **cmd )
190{
191        if( global.conf->runmode == RUNMODE_FORKDAEMON )
192        {
193                int i, len;
194                char *s;
195               
196                len = 1;
197                for( i = 0; cmd[i]; i ++ )
198                        len += strlen( cmd[i] ) + 1;
199               
200                if( strchr( cmd[i-1], ' ' ) != NULL )
201                        len ++;
202               
203                s = g_new0( char, len + 1 );
204                for( i = 0; cmd[i]; i ++ )
205                {
206                        if( cmd[i+1] == NULL && strchr( cmd[i], ' ' ) != NULL )
207                                strcat( s, ":" );
208                       
209                        strcat( s, cmd[i] );
210                       
211                        if( cmd[i+1] )
212                                strcat( s, " " );
213                }
214                strcat( s, "\r\n" );
215               
216                write( global.listen_socket, s, len );
217        }
218}
Note: See TracBrowser for help on using the repository browser.