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 | |
---|
29 | static void query_display( irc_t *irc, query_t *q ); |
---|
30 | static query_t *query_default( irc_t *irc ); |
---|
31 | |
---|
32 | query_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 | |
---|
60 | void 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 | |
---|
88 | void 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 | |
---|
120 | void 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 | //Using irc_usermsg instead of serv_got_crap because \x02A is a char too, so a SPACE is needed. |
---|
130 | if( ans ) |
---|
131 | { |
---|
132 | q->yes( NULL, q->data ); |
---|
133 | serv_got_crap( q->gc, "Accepted: %s", q->question ); |
---|
134 | } |
---|
135 | else |
---|
136 | { |
---|
137 | q->no( NULL, q->data ); |
---|
138 | serv_got_crap( q->gc, "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 | |
---|
148 | static void query_display( irc_t *irc, query_t *q ) |
---|
149 | { |
---|
150 | if( q->gc ) |
---|
151 | { |
---|
152 | serv_got_crap( q->gc, "New request: %s\nYou can use the \2yes\2/\2no\2 commands to accept/reject this request.", q->question ); |
---|
153 | } |
---|
154 | else |
---|
155 | { |
---|
156 | irc_usermsg( irc, "New request: %s\nYou can use the \2yes\2/\2no\2 commands to accept/reject this request.", q->question ); |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | static query_t *query_default( irc_t *irc ) |
---|
161 | { |
---|
162 | query_t *q; |
---|
163 | |
---|
164 | if( g_strcasecmp( set_getstr( irc, "query_order" ), "fifo" ) == 0 ) |
---|
165 | q = irc->queries; |
---|
166 | else |
---|
167 | for( q = irc->queries; q && q->next; q = q->next ); |
---|
168 | |
---|
169 | return( q ); |
---|
170 | } |
---|