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., 51 Franklin St., |
---|
23 | Fifth Floor, Boston, MA 02110-1301 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 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 | char *s; |
---|
47 | |
---|
48 | /* At least for the machine-parseable version, get rid of |
---|
49 | newlines to make "parsing" easier. */ |
---|
50 | for (s = q->question; *s; s++) { |
---|
51 | if (*s == '\r' || *s == '\n') { |
---|
52 | *s = ' '; |
---|
53 | } |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | if (irc->queries) { |
---|
58 | query_t *l = irc->queries; |
---|
59 | |
---|
60 | while (l->next) { |
---|
61 | l = l->next; |
---|
62 | } |
---|
63 | l->next = q; |
---|
64 | } else { |
---|
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 | |
---|
72 | return(q); |
---|
73 | } |
---|
74 | |
---|
75 | void query_del(irc_t *irc, query_t *q) |
---|
76 | { |
---|
77 | query_t *l; |
---|
78 | |
---|
79 | if (irc->queries == q) { |
---|
80 | irc->queries = q->next; |
---|
81 | } else { |
---|
82 | for (l = irc->queries; l; l = l->next) { |
---|
83 | if (l->next == q) { |
---|
84 | l->next = q->next; |
---|
85 | break; |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | if (!l) { |
---|
90 | return; /* Hrmmm... */ |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | g_free(q->question); |
---|
95 | if (q->free && q->data) { |
---|
96 | q->free(q->data); |
---|
97 | } |
---|
98 | g_free(q); |
---|
99 | } |
---|
100 | |
---|
101 | void query_del_by_conn(irc_t *irc, struct im_connection *ic) |
---|
102 | { |
---|
103 | query_t *q, *n, *def; |
---|
104 | int count = 0; |
---|
105 | |
---|
106 | if (!ic) { |
---|
107 | return; |
---|
108 | } |
---|
109 | |
---|
110 | q = irc->queries; |
---|
111 | def = query_default(irc); |
---|
112 | |
---|
113 | while (q) { |
---|
114 | if (q->ic == ic) { |
---|
115 | n = q->next; |
---|
116 | query_del(irc, q); |
---|
117 | q = n; |
---|
118 | |
---|
119 | count++; |
---|
120 | } else { |
---|
121 | q = q->next; |
---|
122 | } |
---|
123 | } |
---|
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 | } |
---|
133 | } |
---|
134 | |
---|
135 | void query_answer(irc_t *irc, query_t *q, int ans) |
---|
136 | { |
---|
137 | int disp = 0; |
---|
138 | |
---|
139 | if (!q) { |
---|
140 | q = query_default(irc); |
---|
141 | disp = 1; |
---|
142 | } |
---|
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 | } |
---|
161 | } |
---|
162 | q->data = NULL; |
---|
163 | |
---|
164 | query_del(irc, q); |
---|
165 | |
---|
166 | if (disp && (q = query_default(irc))) { |
---|
167 | query_display(irc, q); |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | static void query_display(irc_t *irc, query_t *q) |
---|
172 | { |
---|
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); |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | static query_t *query_default(irc_t *irc) |
---|
185 | { |
---|
186 | query_t *q; |
---|
187 | |
---|
188 | if (g_strcasecmp(set_getstr(&irc->b->set, "query_order"), "fifo") == 0) { |
---|
189 | q = irc->queries; |
---|
190 | } else { |
---|
191 | for (q = irc->queries; q && q->next; q = q->next) { |
---|
192 | ; |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | return(q); |
---|
197 | } |
---|