1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2004 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* Account management functions */ |
---|
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 | #include "account.h" |
---|
29 | |
---|
30 | account_t *account_add( irc_t *irc, struct prpl *prpl, char *user, char *pass ) |
---|
31 | { |
---|
32 | account_t *a; |
---|
33 | set_t *s; |
---|
34 | |
---|
35 | if( irc->accounts ) |
---|
36 | { |
---|
37 | for( a = irc->accounts; a->next; a = a->next ); |
---|
38 | a = a->next = g_new0( account_t, 1 ); |
---|
39 | } |
---|
40 | else |
---|
41 | { |
---|
42 | irc->accounts = a = g_new0 ( account_t, 1 ); |
---|
43 | } |
---|
44 | |
---|
45 | a->prpl = prpl; |
---|
46 | a->user = g_strdup( user ); |
---|
47 | a->pass = g_strdup( pass ); |
---|
48 | a->auto_connect = 1; |
---|
49 | a->irc = irc; |
---|
50 | |
---|
51 | s = set_add( &a->set, "auto_connect", "true", set_eval_account, a ); |
---|
52 | s->flags |= ACC_SET_NOSAVE; |
---|
53 | |
---|
54 | s = set_add( &a->set, "auto_reconnect", "true", set_eval_bool, a ); |
---|
55 | |
---|
56 | s = set_add( &a->set, "password", NULL, set_eval_account, a ); |
---|
57 | s->flags |= ACC_SET_NOSAVE; |
---|
58 | |
---|
59 | s = set_add( &a->set, "username", NULL, set_eval_account, a ); |
---|
60 | s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY; |
---|
61 | set_setstr( &a->set, "username", user ); |
---|
62 | |
---|
63 | a->nicks = g_hash_table_new_full( g_str_hash, g_str_equal, g_free, g_free ); |
---|
64 | |
---|
65 | /* This function adds some more settings (and might want to do more |
---|
66 | things that have to be done now, although I can't think of anything. */ |
---|
67 | if( prpl->init ) |
---|
68 | prpl->init( a ); |
---|
69 | |
---|
70 | return( a ); |
---|
71 | } |
---|
72 | |
---|
73 | char *set_eval_account( set_t *set, char *value ) |
---|
74 | { |
---|
75 | account_t *acc = set->data; |
---|
76 | |
---|
77 | /* Double-check: We refuse to edit on-line accounts. */ |
---|
78 | if( set->flags & ACC_SET_OFFLINE_ONLY && acc->ic ) |
---|
79 | return NULL; |
---|
80 | |
---|
81 | if( strcmp( set->key, "username" ) == 0 ) |
---|
82 | { |
---|
83 | g_free( acc->user ); |
---|
84 | acc->user = g_strdup( value ); |
---|
85 | return value; |
---|
86 | } |
---|
87 | else if( strcmp( set->key, "password" ) == 0 ) |
---|
88 | { |
---|
89 | g_free( acc->pass ); |
---|
90 | acc->pass = g_strdup( value ); |
---|
91 | return NULL; /* password shouldn't be visible in plaintext! */ |
---|
92 | } |
---|
93 | else if( strcmp( set->key, "server" ) == 0 ) |
---|
94 | { |
---|
95 | g_free( acc->server ); |
---|
96 | if( *value ) |
---|
97 | { |
---|
98 | acc->server = g_strdup( value ); |
---|
99 | return value; |
---|
100 | } |
---|
101 | else |
---|
102 | { |
---|
103 | acc->server = NULL; |
---|
104 | return g_strdup( set->def ); |
---|
105 | } |
---|
106 | } |
---|
107 | else if( strcmp( set->key, "auto_connect" ) == 0 ) |
---|
108 | { |
---|
109 | if( !is_bool( value ) ) |
---|
110 | return NULL; |
---|
111 | |
---|
112 | acc->auto_connect = bool2int( value ); |
---|
113 | return value; |
---|
114 | } |
---|
115 | |
---|
116 | return NULL; |
---|
117 | } |
---|
118 | |
---|
119 | account_t *account_get( irc_t *irc, char *id ) |
---|
120 | { |
---|
121 | account_t *a, *ret = NULL; |
---|
122 | char *handle, *s; |
---|
123 | int nr; |
---|
124 | |
---|
125 | /* This checks if the id string ends with (...) */ |
---|
126 | if( ( handle = strchr( id, '(' ) ) && ( s = strchr( handle, ')' ) ) && s[1] == 0 ) |
---|
127 | { |
---|
128 | struct prpl *proto; |
---|
129 | |
---|
130 | *s = *handle = 0; |
---|
131 | handle ++; |
---|
132 | |
---|
133 | if( ( proto = find_protocol( id ) ) ) |
---|
134 | { |
---|
135 | for( a = irc->accounts; a; a = a->next ) |
---|
136 | if( a->prpl == proto && |
---|
137 | a->prpl->handle_cmp( handle, a->user ) == 0 ) |
---|
138 | ret = a; |
---|
139 | } |
---|
140 | |
---|
141 | /* Restore the string. */ |
---|
142 | handle --; |
---|
143 | *handle = '('; |
---|
144 | *s = ')'; |
---|
145 | |
---|
146 | if( ret ) |
---|
147 | return ret; |
---|
148 | } |
---|
149 | |
---|
150 | if( sscanf( id, "%d", &nr ) == 1 && nr < 1000 ) |
---|
151 | { |
---|
152 | for( a = irc->accounts; a; a = a->next ) |
---|
153 | if( ( nr-- ) == 0 ) |
---|
154 | return( a ); |
---|
155 | |
---|
156 | return( NULL ); |
---|
157 | } |
---|
158 | |
---|
159 | for( a = irc->accounts; a; a = a->next ) |
---|
160 | { |
---|
161 | if( g_strcasecmp( id, a->prpl->name ) == 0 ) |
---|
162 | { |
---|
163 | if( !ret ) |
---|
164 | ret = a; |
---|
165 | else |
---|
166 | return( NULL ); /* We don't want to match more than one... */ |
---|
167 | } |
---|
168 | else if( strstr( a->user, id ) ) |
---|
169 | { |
---|
170 | if( !ret ) |
---|
171 | ret = a; |
---|
172 | else |
---|
173 | return( NULL ); |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | return( ret ); |
---|
178 | } |
---|
179 | |
---|
180 | void account_del( irc_t *irc, account_t *acc ) |
---|
181 | { |
---|
182 | account_t *a, *l = NULL; |
---|
183 | |
---|
184 | for( a = irc->accounts; a; a = (l=a)->next ) |
---|
185 | if( a == acc ) |
---|
186 | { |
---|
187 | if( a->ic ) return; /* Caller should have checked, accounts still in use can't be deleted. */ |
---|
188 | |
---|
189 | if( l ) |
---|
190 | { |
---|
191 | l->next = a->next; |
---|
192 | } |
---|
193 | else |
---|
194 | { |
---|
195 | irc->accounts = a->next; |
---|
196 | } |
---|
197 | |
---|
198 | while( a->set ) |
---|
199 | set_del( &a->set, a->set->key ); |
---|
200 | |
---|
201 | g_hash_table_destroy( a->nicks ); |
---|
202 | |
---|
203 | g_free( a->user ); |
---|
204 | g_free( a->pass ); |
---|
205 | if( a->server ) g_free( a->server ); |
---|
206 | if( a->reconnect ) /* This prevents any reconnect still queued to happen */ |
---|
207 | cancel_auto_reconnect( a ); |
---|
208 | g_free( a ); |
---|
209 | |
---|
210 | break; |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | void account_on( irc_t *irc, account_t *a ) |
---|
215 | { |
---|
216 | if( a->ic ) |
---|
217 | { |
---|
218 | /* Trying to enable an already-enabled account */ |
---|
219 | return; |
---|
220 | } |
---|
221 | |
---|
222 | cancel_auto_reconnect( a ); |
---|
223 | |
---|
224 | a->reconnect = 0; |
---|
225 | a->prpl->login( a ); |
---|
226 | } |
---|
227 | |
---|
228 | void account_off( irc_t *irc, account_t *a ) |
---|
229 | { |
---|
230 | imc_logout( a->ic, FALSE ); |
---|
231 | a->ic = NULL; |
---|
232 | if( a->reconnect ) |
---|
233 | { |
---|
234 | /* Shouldn't happen */ |
---|
235 | cancel_auto_reconnect( a ); |
---|
236 | } |
---|
237 | } |
---|