[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 | /* 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 | |
---|
[7b23afd] | 30 | account_t *account_add( irc_t *irc, struct prpl *prpl, char *user, char *pass ) |
---|
[b7d3cc34] | 31 | { |
---|
| 32 | account_t *a; |
---|
| 33 | |
---|
| 34 | if( irc->accounts ) |
---|
| 35 | { |
---|
| 36 | for( a = irc->accounts; a->next; a = a->next ); |
---|
[a312b6b] | 37 | a = a->next = g_new0( account_t, 1 ); |
---|
[b7d3cc34] | 38 | } |
---|
| 39 | else |
---|
| 40 | { |
---|
| 41 | irc->accounts = a = g_new0 ( account_t, 1 ); |
---|
| 42 | } |
---|
| 43 | |
---|
[7b23afd] | 44 | a->prpl = prpl; |
---|
[b7d3cc34] | 45 | a->user = g_strdup( user ); |
---|
| 46 | a->pass = g_strdup( pass ); |
---|
| 47 | a->irc = irc; |
---|
| 48 | |
---|
| 49 | return( a ); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | account_t *account_get( irc_t *irc, char *id ) |
---|
| 53 | { |
---|
| 54 | account_t *a, *ret = NULL; |
---|
[85616c3] | 55 | char *handle, *s; |
---|
[b7d3cc34] | 56 | int nr; |
---|
| 57 | |
---|
[85616c3] | 58 | /* This checks if the id string ends with (...) */ |
---|
| 59 | if( ( handle = strchr( id, '(' ) ) && ( s = strchr( handle, ')' ) ) && s[1] == 0 ) |
---|
| 60 | { |
---|
| 61 | struct prpl *proto; |
---|
| 62 | |
---|
| 63 | *s = *handle = 0; |
---|
| 64 | handle ++; |
---|
| 65 | |
---|
| 66 | if( ( proto = find_protocol( id ) ) ) |
---|
| 67 | { |
---|
| 68 | for( a = irc->accounts; a; a = a->next ) |
---|
| 69 | if( a->prpl == proto && |
---|
| 70 | a->prpl->cmp_buddynames( handle, a->user ) == 0 ) |
---|
| 71 | ret = a; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | /* Restore the string. */ |
---|
| 75 | handle --; |
---|
| 76 | *handle = '('; |
---|
| 77 | *s = ')'; |
---|
| 78 | |
---|
| 79 | if( ret ) |
---|
| 80 | return ret; |
---|
| 81 | } |
---|
| 82 | |
---|
[b7d3cc34] | 83 | if( sscanf( id, "%d", &nr ) == 1 && nr < 1000 ) |
---|
| 84 | { |
---|
| 85 | for( a = irc->accounts; a; a = a->next ) |
---|
| 86 | if( ( nr-- ) == 0 ) |
---|
| 87 | return( a ); |
---|
| 88 | |
---|
| 89 | return( NULL ); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | for( a = irc->accounts; a; a = a->next ) |
---|
| 93 | { |
---|
[7b23afd] | 94 | if( g_strcasecmp( id, a->prpl->name ) == 0 ) |
---|
[b7d3cc34] | 95 | { |
---|
| 96 | if( !ret ) |
---|
| 97 | ret = a; |
---|
| 98 | else |
---|
| 99 | return( NULL ); /* We don't want to match more than one... */ |
---|
| 100 | } |
---|
| 101 | else if( strstr( a->user, id ) ) |
---|
| 102 | { |
---|
| 103 | if( !ret ) |
---|
| 104 | ret = a; |
---|
| 105 | else |
---|
| 106 | return( NULL ); |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | return( ret ); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | void account_del( irc_t *irc, account_t *acc ) |
---|
| 114 | { |
---|
| 115 | account_t *a, *l = NULL; |
---|
| 116 | |
---|
| 117 | for( a = irc->accounts; a; a = (l=a)->next ) |
---|
| 118 | if( a == acc ) |
---|
| 119 | { |
---|
| 120 | if( a->gc ) return; /* Caller should have checked, accounts still in use can't be deleted. */ |
---|
| 121 | |
---|
| 122 | if( l ) |
---|
| 123 | { |
---|
| 124 | l->next = a->next; |
---|
| 125 | } |
---|
| 126 | else |
---|
| 127 | { |
---|
| 128 | irc->accounts = a->next; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | g_free( a->user ); |
---|
| 132 | g_free( a->pass ); |
---|
| 133 | if( a->server ) g_free( a->server ); |
---|
| 134 | if( a->reconnect ) /* This prevents any reconnect still queued to happen */ |
---|
| 135 | cancel_auto_reconnect( a ); |
---|
| 136 | g_free( a ); |
---|
| 137 | |
---|
| 138 | break; |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | void account_on( irc_t *irc, account_t *a ) |
---|
| 143 | { |
---|
| 144 | struct aim_user *u; |
---|
| 145 | |
---|
| 146 | if( a->gc ) |
---|
| 147 | { |
---|
| 148 | /* Trying to enable an already-enabled account */ |
---|
| 149 | return; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | cancel_auto_reconnect( a ); |
---|
| 153 | |
---|
| 154 | u = g_new0 ( struct aim_user, 1 ); |
---|
| 155 | u->irc = irc; |
---|
[7b23afd] | 156 | u->prpl = a->prpl; |
---|
[b7d3cc34] | 157 | strncpy( u->username, a->user, sizeof( u->username ) - 1 ); |
---|
| 158 | strncpy( u->password, a->pass, sizeof( u->password ) - 1 ); |
---|
| 159 | if( a->server) strncpy( u->proto_opt[0], a->server, sizeof( u->proto_opt[0] ) - 1 ); |
---|
| 160 | |
---|
| 161 | a->gc = (struct gaim_connection *) u; /* Bit hackish :-/ */ |
---|
| 162 | a->reconnect = 0; |
---|
| 163 | |
---|
[7b23afd] | 164 | a->prpl->login( u ); |
---|
[b7d3cc34] | 165 | } |
---|
| 166 | |
---|
| 167 | void account_off( irc_t *irc, account_t *a ) |
---|
| 168 | { |
---|
[c99af3a] | 169 | a->gc->wants_to_die = TRUE; |
---|
| 170 | signoff( a->gc ); |
---|
[b7d3cc34] | 171 | a->gc = NULL; |
---|
| 172 | if( a->reconnect ) |
---|
| 173 | { |
---|
| 174 | /* Shouldn't happen */ |
---|
| 175 | cancel_auto_reconnect( a ); |
---|
| 176 | } |
---|
| 177 | } |
---|