[b7d3cc34] | 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 | |
---|
[aefa533e] | 42 | if( strchr( irc->umode, 'b' ) != NULL ) |
---|
| 43 | { |
---|
| 44 | char *s; |
---|
| 45 | |
---|
| 46 | /* At least for the machine-parseable version, get rid of |
---|
| 47 | newlines to make "parsing" easier. */ |
---|
| 48 | for( s = q->question; *s; s ++ ) |
---|
| 49 | if( *s == '\r' || *s == '\n' ) |
---|
| 50 | *s = ' '; |
---|
| 51 | } |
---|
| 52 | |
---|
[b7d3cc34] | 53 | if( irc->queries ) |
---|
| 54 | { |
---|
| 55 | query_t *l = irc->queries; |
---|
| 56 | |
---|
| 57 | while( l->next ) l = l->next; |
---|
| 58 | l->next = q; |
---|
| 59 | } |
---|
| 60 | else |
---|
| 61 | { |
---|
| 62 | irc->queries = q; |
---|
| 63 | } |
---|
| 64 | |
---|
[5c9512f] | 65 | if( g_strcasecmp( set_getstr( &irc->set, "query_order" ), "lifo" ) == 0 || irc->queries == q ) |
---|
[b7d3cc34] | 66 | query_display( irc, q ); |
---|
| 67 | |
---|
| 68 | return( q ); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | void query_del( irc_t *irc, query_t *q ) |
---|
| 72 | { |
---|
| 73 | query_t *l; |
---|
| 74 | |
---|
| 75 | if( irc->queries == q ) |
---|
| 76 | { |
---|
| 77 | irc->queries = q->next; |
---|
| 78 | } |
---|
| 79 | else |
---|
| 80 | { |
---|
| 81 | for( l = irc->queries; l; l = l->next ) |
---|
| 82 | { |
---|
| 83 | if( l->next == q ) |
---|
| 84 | { |
---|
| 85 | l->next = q->next; |
---|
| 86 | break; |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | if( !l ) |
---|
| 91 | return; /* Hrmmm... */ |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | g_free( q->question ); |
---|
| 95 | if( q->data ) g_free( q->data ); /* Memory leak... */ |
---|
| 96 | g_free( q ); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | void query_del_by_gc( irc_t *irc, struct gaim_connection *gc ) |
---|
| 100 | { |
---|
| 101 | query_t *q, *n, *def; |
---|
| 102 | int count = 0; |
---|
| 103 | |
---|
| 104 | q = irc->queries; |
---|
| 105 | def = query_default( irc ); |
---|
| 106 | |
---|
| 107 | while( q ) |
---|
| 108 | { |
---|
| 109 | if( q->gc == gc ) |
---|
| 110 | { |
---|
| 111 | n = q->next; |
---|
| 112 | query_del( irc, q ); |
---|
| 113 | q = n; |
---|
| 114 | |
---|
| 115 | count ++; |
---|
| 116 | } |
---|
| 117 | else |
---|
| 118 | { |
---|
| 119 | q = q->next; |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | if( count > 0 ) |
---|
| 124 | serv_got_crap( gc, "Flushed %d unanswered question(s) for this connection.", count ); |
---|
| 125 | |
---|
| 126 | q = query_default( irc ); |
---|
| 127 | if( q && q != def ) |
---|
| 128 | query_display( irc, q ); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | void query_answer( irc_t *irc, query_t *q, int ans ) |
---|
| 132 | { |
---|
| 133 | int disp = 0; |
---|
| 134 | |
---|
| 135 | if( !q ) |
---|
| 136 | { |
---|
| 137 | q = query_default( irc ); |
---|
| 138 | disp = 1; |
---|
| 139 | } |
---|
| 140 | if( ans ) |
---|
| 141 | { |
---|
[ea85a0b] | 142 | serv_got_crap( q->gc, "Accepted: %s", q->question ); |
---|
[7bf0f5f0] | 143 | q->yes( NULL, q->data ); |
---|
[b7d3cc34] | 144 | } |
---|
| 145 | else |
---|
| 146 | { |
---|
[ea85a0b] | 147 | serv_got_crap( q->gc, "Rejected: %s", q->question ); |
---|
[7bf0f5f0] | 148 | q->no( NULL, q->data ); |
---|
[b7d3cc34] | 149 | } |
---|
| 150 | q->data = NULL; |
---|
| 151 | |
---|
| 152 | query_del( irc, q ); |
---|
| 153 | |
---|
| 154 | if( disp && ( q = query_default( irc ) ) ) |
---|
| 155 | query_display( irc, q ); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | static void query_display( irc_t *irc, query_t *q ) |
---|
| 159 | { |
---|
| 160 | if( q->gc ) |
---|
[5c09a59] | 161 | { |
---|
[ea85a0b] | 162 | serv_got_crap( q->gc, "New request: %s\nYou can use the \2yes\2/\2no\2 commands to accept/reject this request.", q->question ); |
---|
[5c09a59] | 163 | } |
---|
[b7d3cc34] | 164 | else |
---|
[5c09a59] | 165 | { |
---|
[ea85a0b] | 166 | irc_usermsg( irc, "New request: %s\nYou can use the \2yes\2/\2no\2 commands to accept/reject this request.", q->question ); |
---|
[5c09a59] | 167 | } |
---|
[b7d3cc34] | 168 | } |
---|
| 169 | |
---|
| 170 | static query_t *query_default( irc_t *irc ) |
---|
| 171 | { |
---|
| 172 | query_t *q; |
---|
| 173 | |
---|
[5c9512f] | 174 | if( g_strcasecmp( set_getstr( &irc->set, "query_order" ), "fifo" ) == 0 ) |
---|
[b7d3cc34] | 175 | q = irc->queries; |
---|
| 176 | else |
---|
| 177 | for( q = irc->queries; q && q->next; q = q->next ); |
---|
| 178 | |
---|
| 179 | return( q ); |
---|
| 180 | } |
---|