[5ebff60] | 1 | /********************************************************************\ |
---|
[b7d3cc34] | 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; |
---|
[6f10697] | 22 | if not, write to the Free Software Foundation, Inc., 51 Franklin St., |
---|
| 23 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
[b7d3cc34] | 24 | */ |
---|
| 25 | |
---|
| 26 | #define BITLBEE_CORE |
---|
| 27 | #include "bitlbee.h" |
---|
| 28 | |
---|
[5ebff60] | 29 | static void query_display(irc_t *irc, query_t *q); |
---|
| 30 | static query_t *query_default(irc_t *irc); |
---|
[b7d3cc34] | 31 | |
---|
[5ebff60] | 32 | query_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) |
---|
[b7d3cc34] | 35 | { |
---|
[5ebff60] | 36 | query_t *q = g_new0(query_t, 1); |
---|
| 37 | |
---|
[0da65d5] | 38 | q->ic = ic; |
---|
[5ebff60] | 39 | q->question = g_strdup(question); |
---|
[b7d3cc34] | 40 | q->yes = yes; |
---|
| 41 | q->no = no; |
---|
[1e52e1f] | 42 | q->free = free; |
---|
[b7d3cc34] | 43 | q->data = data; |
---|
[5ebff60] | 44 | |
---|
| 45 | if (strchr(irc->umode, 'b') != NULL) { |
---|
[aefa533e] | 46 | char *s; |
---|
[5ebff60] | 47 | |
---|
[aefa533e] | 48 | /* At least for the machine-parseable version, get rid of |
---|
| 49 | newlines to make "parsing" easier. */ |
---|
[5ebff60] | 50 | for (s = q->question; *s; s++) { |
---|
| 51 | if (*s == '\r' || *s == '\n') { |
---|
[aefa533e] | 52 | *s = ' '; |
---|
[5ebff60] | 53 | } |
---|
| 54 | } |
---|
[aefa533e] | 55 | } |
---|
[5ebff60] | 56 | |
---|
| 57 | if (irc->queries) { |
---|
[b7d3cc34] | 58 | query_t *l = irc->queries; |
---|
[5ebff60] | 59 | |
---|
| 60 | while (l->next) { |
---|
| 61 | l = l->next; |
---|
| 62 | } |
---|
[b7d3cc34] | 63 | l->next = q; |
---|
[5ebff60] | 64 | } else { |
---|
[b7d3cc34] | 65 | irc->queries = q; |
---|
| 66 | } |
---|
[5ebff60] | 67 | |
---|
| 68 | if (g_strcasecmp(set_getstr(&irc->b->set, "query_order"), "lifo") == 0 || irc->queries == q) { |
---|
| 69 | query_display(irc, q); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | return(q); |
---|
[b7d3cc34] | 73 | } |
---|
| 74 | |
---|
[5ebff60] | 75 | void query_del(irc_t *irc, query_t *q) |
---|
[b7d3cc34] | 76 | { |
---|
| 77 | query_t *l; |
---|
[5ebff60] | 78 | |
---|
| 79 | if (irc->queries == q) { |
---|
[b7d3cc34] | 80 | irc->queries = q->next; |
---|
[5ebff60] | 81 | } else { |
---|
| 82 | for (l = irc->queries; l; l = l->next) { |
---|
| 83 | if (l->next == q) { |
---|
[b7d3cc34] | 84 | l->next = q->next; |
---|
| 85 | break; |
---|
| 86 | } |
---|
| 87 | } |
---|
[5ebff60] | 88 | |
---|
| 89 | if (!l) { |
---|
[b7d3cc34] | 90 | return; /* Hrmmm... */ |
---|
[5ebff60] | 91 | } |
---|
[b7d3cc34] | 92 | } |
---|
[5ebff60] | 93 | |
---|
| 94 | g_free(q->question); |
---|
| 95 | if (q->free && q->data) { |
---|
| 96 | q->free(q->data); |
---|
| 97 | } |
---|
| 98 | g_free(q); |
---|
[b7d3cc34] | 99 | } |
---|
| 100 | |
---|
[5ebff60] | 101 | void query_del_by_conn(irc_t *irc, struct im_connection *ic) |
---|
[b7d3cc34] | 102 | { |
---|
| 103 | query_t *q, *n, *def; |
---|
| 104 | int count = 0; |
---|
[5ebff60] | 105 | |
---|
| 106 | if (!ic) { |
---|
[8bd697c] | 107 | return; |
---|
[5ebff60] | 108 | } |
---|
| 109 | |
---|
[b7d3cc34] | 110 | q = irc->queries; |
---|
[5ebff60] | 111 | def = query_default(irc); |
---|
| 112 | |
---|
| 113 | while (q) { |
---|
| 114 | if (q->ic == ic) { |
---|
[b7d3cc34] | 115 | n = q->next; |
---|
[5ebff60] | 116 | query_del(irc, q); |
---|
[b7d3cc34] | 117 | q = n; |
---|
[5ebff60] | 118 | |
---|
| 119 | count++; |
---|
| 120 | } else { |
---|
[b7d3cc34] | 121 | q = q->next; |
---|
| 122 | } |
---|
| 123 | } |
---|
[5ebff60] | 124 | |
---|
| 125 | if (count > 0) { |
---|
| 126 | imcb_log(ic, "Flushed %d unanswered question(s) for this connection.", count); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | q = query_default(irc); |
---|
| 130 | if (q && q != def) { |
---|
| 131 | query_display(irc, q); |
---|
| 132 | } |
---|
[b7d3cc34] | 133 | } |
---|
| 134 | |
---|
[5ebff60] | 135 | void query_answer(irc_t *irc, query_t *q, int ans) |
---|
[b7d3cc34] | 136 | { |
---|
| 137 | int disp = 0; |
---|
[5ebff60] | 138 | |
---|
| 139 | if (!q) { |
---|
| 140 | q = query_default(irc); |
---|
[b7d3cc34] | 141 | disp = 1; |
---|
| 142 | } |
---|
[5ebff60] | 143 | if (ans) { |
---|
| 144 | if (q->ic) { |
---|
| 145 | imcb_log(q->ic, "Accepted: %s", q->question); |
---|
| 146 | } else { |
---|
| 147 | irc_rootmsg(irc, "Accepted: %s", q->question); |
---|
| 148 | } |
---|
| 149 | if (q->yes) { |
---|
| 150 | q->yes(q->data); |
---|
| 151 | } |
---|
| 152 | } else { |
---|
| 153 | if (q->ic) { |
---|
| 154 | imcb_log(q->ic, "Rejected: %s", q->question); |
---|
| 155 | } else { |
---|
| 156 | irc_rootmsg(irc, "Rejected: %s", q->question); |
---|
| 157 | } |
---|
| 158 | if (q->no) { |
---|
| 159 | q->no(q->data); |
---|
| 160 | } |
---|
[b7d3cc34] | 161 | } |
---|
| 162 | q->data = NULL; |
---|
[5ebff60] | 163 | |
---|
| 164 | query_del(irc, q); |
---|
| 165 | |
---|
| 166 | if (disp && (q = query_default(irc))) { |
---|
| 167 | query_display(irc, q); |
---|
| 168 | } |
---|
[b7d3cc34] | 169 | } |
---|
| 170 | |
---|
[5ebff60] | 171 | static void query_display(irc_t *irc, query_t *q) |
---|
[b7d3cc34] | 172 | { |
---|
[5ebff60] | 173 | if (q->ic) { |
---|
| 174 | imcb_log(q->ic, |
---|
| 175 | "New request: %s\nYou can use the \2yes\2/\2no\2 commands to accept/reject this request.", |
---|
| 176 | q->question); |
---|
| 177 | } else { |
---|
| 178 | irc_rootmsg(irc, |
---|
| 179 | "New request: %s\nYou can use the \2yes\2/\2no\2 commands to accept/reject this request.", |
---|
| 180 | q->question); |
---|
[5c09a59] | 181 | } |
---|
[b7d3cc34] | 182 | } |
---|
| 183 | |
---|
[5ebff60] | 184 | static query_t *query_default(irc_t *irc) |
---|
[b7d3cc34] | 185 | { |
---|
| 186 | query_t *q; |
---|
[5ebff60] | 187 | |
---|
| 188 | if (g_strcasecmp(set_getstr(&irc->b->set, "query_order"), "fifo") == 0) { |
---|
[b7d3cc34] | 189 | q = irc->queries; |
---|
[5ebff60] | 190 | } else { |
---|
| 191 | for (q = irc->queries; q && q->next; q = q->next) { |
---|
| 192 | ; |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | return(q); |
---|
[b7d3cc34] | 197 | } |
---|