source: query.c @ fbc7844

Last change on this file since fbc7844 was b7d3cc34, checked in by Wilmer van der Gaast <wilmer@…>, at 2005-11-06T18:23:18Z

Initial repository (0.99 release tree)

  • Property mode set to 100644
File size: 3.6 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 gaim_connection *gc, char *question, void *yes, void *no, void *data )
33{
34        query_t *q = g_new0( query_t, 1 );
35       
36        q->gc = gc;
37        q->question = g_strdup( question );
38        q->yes = yes;
39        q->no = no;
40        q->data = data;
41       
42        if( irc->queries )
43        {
44                query_t *l = irc->queries;
45               
46                while( l->next ) l = l->next;
47                l->next = q;
48        }
49        else
50        {
51                irc->queries = q;
52        }
53       
54        if( g_strcasecmp( set_getstr( irc, "query_order" ), "lifo" ) == 0 || irc->queries == q )
55                query_display( irc, q );
56       
57        return( q );
58}
59
60void query_del( irc_t *irc, query_t *q )
61{
62        query_t *l;
63       
64        if( irc->queries == q )
65        {
66                irc->queries = q->next;
67        }
68        else
69        {
70                for( l = irc->queries; l; l = l->next )
71                {
72                        if( l->next == q )
73                        {
74                                l->next = q->next;
75                                break;
76                        }
77                }
78               
79                if( !l )
80                        return; /* Hrmmm... */
81        }
82       
83        g_free( q->question );
84        if( q->data ) g_free( q->data ); /* Memory leak... */
85        g_free( q );
86}
87
88void query_del_by_gc( irc_t *irc, struct gaim_connection *gc )
89{
90        query_t *q, *n, *def;
91        int count = 0;
92       
93        q = irc->queries;
94        def = query_default( irc );
95       
96        while( q )
97        {
98                if( q->gc == gc )
99                {
100                        n = q->next;
101                        query_del( irc, q );
102                        q = n;
103                       
104                        count ++;
105                }
106                else
107                {
108                        q = q->next;
109                }
110        }
111       
112        if( count > 0 )
113                serv_got_crap( gc, "Flushed %d unanswered question(s) for this connection.", count );
114       
115        q = query_default( irc );
116        if( q && q != def )
117                query_display( irc, q );
118}
119
120void query_answer( irc_t *irc, query_t *q, int ans )
121{
122        int disp = 0;
123       
124        if( !q )
125        {
126                q = query_default( irc );
127                disp = 1;
128        }
129       
130        if( ans )
131        {
132                q->yes( NULL, q->data );
133                irc_usermsg( irc, "Accepted: %s", q->question );
134        }
135        else
136        {
137                q->no( NULL, q->data );
138                irc_usermsg( irc, "Rejected: %s", q->question );
139        }
140        q->data = NULL;
141       
142        query_del( irc, q );
143       
144        if( disp && ( q = query_default( irc ) ) )
145                query_display( irc, q );
146}
147
148static void query_display( irc_t *irc, query_t *q )
149{
150        if( q->gc )
151                irc_usermsg( irc, "Question on %s connection (handle %s):", proto_name[q->gc->protocol], q->gc->username );
152        else
153                irc_usermsg( irc, "Question:" );
154       
155        irc_usermsg( irc, "%s\nYou can use the yes/no commands to answer this question.", q->question );
156}
157
158static query_t *query_default( irc_t *irc )
159{
160        query_t *q;
161       
162        if( g_strcasecmp( set_getstr( irc, "query_order" ), "fifo" ) == 0 )
163                q = irc->queries;
164        else
165                for( q = irc->queries; q && q->next; q = q->next );
166       
167        return( q );
168}
Note: See TracBrowser for help on using the repository browser.