source: query.c @ 7db65b7

Last change on this file since 7db65b7 was 1e52e1f, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-07-11T10:30:27Z

When cleaning up queries, q->data is free()d. Even if it turns out to be
the "struct irc" containing all data belonging to a session. Sanitise
memory management a little bit here. (There are some memory leaks in here
too that need to be fixed at some point.)

  • Property mode set to 100644
File size: 4.1 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/* Questions to the user (mainly authorization requests from IM)        */
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
29static void query_display( irc_t *irc, query_t *q );
30static query_t *query_default( irc_t *irc );
31
32query_t *query_add( irc_t *irc, struct im_connection *ic, char *question,
33                    query_callback yes, query_callback no, query_callback free,
34                    void *data )
35{
36        query_t *q = g_new0( query_t, 1 );
37       
38        q->ic = ic;
39        q->question = g_strdup( question );
40        q->yes = yes;
41        q->no = no;
42        q->free = free;
43        q->data = data;
44       
45        if( strchr( irc->umode, 'b' ) != NULL )
46        {
47                char *s;
48               
49                /* At least for the machine-parseable version, get rid of
50                   newlines to make "parsing" easier. */
51                for( s = q->question; *s; s ++ )
52                        if( *s == '\r' || *s == '\n' )
53                                *s = ' ';
54        }
55       
56        if( irc->queries )
57        {
58                query_t *l = irc->queries;
59               
60                while( l->next ) l = l->next;
61                l->next = q;
62        }
63        else
64        {
65                irc->queries = q;
66        }
67       
68        if( g_strcasecmp( set_getstr( &irc->b->set, "query_order" ), "lifo" ) == 0 || irc->queries == q )
69                query_display( irc, q );
70       
71        return( q );
72}
73
74void query_del( irc_t *irc, query_t *q )
75{
76        query_t *l;
77       
78        if( irc->queries == q )
79        {
80                irc->queries = q->next;
81        }
82        else
83        {
84                for( l = irc->queries; l; l = l->next )
85                {
86                        if( l->next == q )
87                        {
88                                l->next = q->next;
89                                break;
90                        }
91                }
92               
93                if( !l )
94                        return; /* Hrmmm... */
95        }
96       
97        g_free( q->question );
98        if( q->free && q->data )
99                q->free( q->data );
100        g_free( q );
101}
102
103void query_del_by_conn( irc_t *irc, struct im_connection *ic )
104{
105        query_t *q, *n, *def;
106        int count = 0;
107       
108        q = irc->queries;
109        def = query_default( irc );
110       
111        while( q )
112        {
113                if( q->ic == ic )
114                {
115                        n = q->next;
116                        query_del( irc, q );
117                        q = n;
118                       
119                        count ++;
120                }
121                else
122                {
123                        q = q->next;
124                }
125        }
126       
127        if( count > 0 )
128                imcb_log( ic, "Flushed %d unanswered question(s) for this connection.", count );
129       
130        q = query_default( irc );
131        if( q && q != def )
132                query_display( irc, q );
133}
134
135void query_answer( irc_t *irc, query_t *q, int ans )
136{
137        int disp = 0;
138       
139        if( !q )
140        {
141                q = query_default( irc );
142                disp = 1;
143        }
144        if( ans )
145        {
146                if( q->ic )
147                        imcb_log( q->ic, "Accepted: %s", q->question );
148                else
149                        irc_usermsg( irc, "Accepted: %s", q->question );
150                q->yes( q->data );
151        }
152        else
153        {
154                if( q->ic )
155                        imcb_log( q->ic, "Rejected: %s", q->question );
156                else
157                        irc_usermsg( irc, "Rejected: %s", q->question );
158                q->no( q->data );
159        }
160        q->data = NULL;
161       
162        query_del( irc, q );
163       
164        if( disp && ( q = query_default( irc ) ) )
165                query_display( irc, q );
166}
167
168static void query_display( irc_t *irc, query_t *q )
169{
170        if( q->ic )
171        {
172                imcb_log( q->ic, "New request: %s\nYou can use the \2yes\2/\2no\2 commands to accept/reject this request.", q->question );
173        }
174        else
175        {
176                irc_usermsg( irc, "New request: %s\nYou can use the \2yes\2/\2no\2 commands to accept/reject this request.", q->question );
177        }
178}
179
180static query_t *query_default( irc_t *irc )
181{
182        query_t *q;
183       
184        if( g_strcasecmp( set_getstr( &irc->b->set, "query_order" ), "fifo" ) == 0 )
185                q = irc->queries;
186        else
187                for( q = irc->queries; q && q->next; q = q->next );
188       
189        return( q );
190}
Note: See TracBrowser for help on using the repository browser.