[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 ); |
---|
| 37 | a = a->next = g_new0 ( account_t, 1 ); |
---|
| 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; |
---|
| 55 | int nr; |
---|
| 56 | |
---|
| 57 | if( sscanf( id, "%d", &nr ) == 1 && nr < 1000 ) |
---|
| 58 | { |
---|
| 59 | for( a = irc->accounts; a; a = a->next ) |
---|
| 60 | if( ( nr-- ) == 0 ) |
---|
| 61 | return( a ); |
---|
| 62 | |
---|
| 63 | return( NULL ); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | for( a = irc->accounts; a; a = a->next ) |
---|
| 67 | { |
---|
[7b23afd] | 68 | if( g_strcasecmp( id, a->prpl->name ) == 0 ) |
---|
[b7d3cc34] | 69 | { |
---|
| 70 | if( !ret ) |
---|
| 71 | ret = a; |
---|
| 72 | else |
---|
| 73 | return( NULL ); /* We don't want to match more than one... */ |
---|
| 74 | } |
---|
| 75 | else if( strstr( a->user, id ) ) |
---|
| 76 | { |
---|
| 77 | if( !ret ) |
---|
| 78 | ret = a; |
---|
| 79 | else |
---|
| 80 | return( NULL ); |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | return( ret ); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | void account_del( irc_t *irc, account_t *acc ) |
---|
| 88 | { |
---|
| 89 | account_t *a, *l = NULL; |
---|
| 90 | |
---|
| 91 | for( a = irc->accounts; a; a = (l=a)->next ) |
---|
| 92 | if( a == acc ) |
---|
| 93 | { |
---|
| 94 | if( a->gc ) return; /* Caller should have checked, accounts still in use can't be deleted. */ |
---|
| 95 | |
---|
| 96 | if( l ) |
---|
| 97 | { |
---|
| 98 | l->next = a->next; |
---|
| 99 | } |
---|
| 100 | else |
---|
| 101 | { |
---|
| 102 | irc->accounts = a->next; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | g_free( a->user ); |
---|
| 106 | g_free( a->pass ); |
---|
| 107 | if( a->server ) g_free( a->server ); |
---|
| 108 | if( a->reconnect ) /* This prevents any reconnect still queued to happen */ |
---|
| 109 | cancel_auto_reconnect( a ); |
---|
| 110 | g_free( a ); |
---|
| 111 | |
---|
| 112 | break; |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | void account_on( irc_t *irc, account_t *a ) |
---|
| 117 | { |
---|
| 118 | struct aim_user *u; |
---|
| 119 | |
---|
| 120 | if( a->gc ) |
---|
| 121 | { |
---|
| 122 | /* Trying to enable an already-enabled account */ |
---|
| 123 | return; |
---|
| 124 | } |
---|
| 125 | |
---|
[7b23afd] | 126 | if (a->prpl == NULL ) |
---|
[b7d3cc34] | 127 | { |
---|
[7b23afd] | 128 | irc_usermsg( irc, "Support for protocol %s is not included in this BitlBee", a->prpl->name ); |
---|
[b7d3cc34] | 129 | return; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | cancel_auto_reconnect( a ); |
---|
| 133 | |
---|
| 134 | u = g_new0 ( struct aim_user, 1 ); |
---|
| 135 | u->irc = irc; |
---|
[7b23afd] | 136 | u->prpl = a->prpl; |
---|
[b7d3cc34] | 137 | strncpy( u->username, a->user, sizeof( u->username ) - 1 ); |
---|
| 138 | strncpy( u->password, a->pass, sizeof( u->password ) - 1 ); |
---|
| 139 | if( a->server) strncpy( u->proto_opt[0], a->server, sizeof( u->proto_opt[0] ) - 1 ); |
---|
| 140 | |
---|
| 141 | a->gc = (struct gaim_connection *) u; /* Bit hackish :-/ */ |
---|
| 142 | a->reconnect = 0; |
---|
| 143 | |
---|
[7b23afd] | 144 | a->prpl->login( u ); |
---|
[b7d3cc34] | 145 | } |
---|
| 146 | |
---|
| 147 | void account_off( irc_t *irc, account_t *a ) |
---|
| 148 | { |
---|
| 149 | account_offline( a->gc ); |
---|
| 150 | a->gc = NULL; |
---|
| 151 | if( a->reconnect ) |
---|
| 152 | { |
---|
| 153 | /* Shouldn't happen */ |
---|
| 154 | cancel_auto_reconnect( a ); |
---|
| 155 | } |
---|
| 156 | } |
---|