[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 | /* Some stuff to fetch, save and handle nicknames for your buddies */ |
---|
| 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 | |
---|
[7b23afd] | 29 | void nick_set( irc_t *irc, char *handle, struct prpl *proto, char *nick ) |
---|
[b7d3cc34] | 30 | { |
---|
| 31 | nick_t *m = NULL, *n = irc->nicks; |
---|
| 32 | |
---|
| 33 | while( n ) |
---|
| 34 | { |
---|
| 35 | if( ( g_strcasecmp( n->handle, handle ) == 0 ) && n->proto == proto ) |
---|
| 36 | { |
---|
| 37 | g_free( n->nick ); |
---|
| 38 | n->nick = nick_dup( nick ); |
---|
| 39 | nick_strip( n->nick ); |
---|
| 40 | |
---|
| 41 | return; |
---|
| 42 | } |
---|
| 43 | n = ( m = n )->next; // :-P |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | if( m ) |
---|
| 47 | n = m->next = g_new0( nick_t, 1 ); |
---|
| 48 | else |
---|
| 49 | n = irc->nicks = g_new0( nick_t, 1 ); |
---|
| 50 | |
---|
| 51 | n->handle = g_strdup( handle ); |
---|
| 52 | n->proto = proto; |
---|
| 53 | n->nick = nick_dup( nick ); |
---|
| 54 | |
---|
| 55 | nick_strip( n->nick ); |
---|
| 56 | } |
---|
| 57 | |
---|
[7b23afd] | 58 | char *nick_get( irc_t *irc, char *handle, struct prpl *proto, const char *realname ) |
---|
[b7d3cc34] | 59 | { |
---|
| 60 | static char nick[MAX_NICK_LENGTH+1]; |
---|
| 61 | nick_t *n = irc->nicks; |
---|
| 62 | int inf_protection = 256; |
---|
| 63 | |
---|
| 64 | memset( nick, 0, MAX_NICK_LENGTH + 1 ); |
---|
| 65 | |
---|
| 66 | while( n && !*nick ) |
---|
| 67 | if( ( n->proto == proto ) && ( g_strcasecmp( n->handle, handle ) == 0 ) ) |
---|
| 68 | strcpy( nick, n->nick ); |
---|
| 69 | else |
---|
| 70 | n = n->next; |
---|
| 71 | |
---|
| 72 | if( !n ) |
---|
| 73 | { |
---|
| 74 | char *s; |
---|
| 75 | |
---|
| 76 | g_snprintf( nick, MAX_NICK_LENGTH, "%s", handle ); |
---|
| 77 | if( ( s = strchr( nick, '@' ) ) ) |
---|
| 78 | while( *s ) |
---|
| 79 | *(s++) = 0; |
---|
| 80 | |
---|
| 81 | /* All-digit handles (mainly ICQ UINs) aren't cool, try to |
---|
| 82 | use the realname instead. */ |
---|
| 83 | for( s = nick; *s && isdigit( *s ); s ++ ); |
---|
| 84 | if( !*s && realname && *realname ) |
---|
| 85 | g_snprintf( nick, MAX_NICK_LENGTH, "%s", realname ); |
---|
| 86 | |
---|
| 87 | nick_strip( nick ); |
---|
| 88 | if (set_getint(irc, "lcnicks")) |
---|
| 89 | nick_lc( nick ); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | while( !nick_ok( nick ) || user_find( irc, nick ) ) |
---|
| 93 | { |
---|
| 94 | if( strlen( nick ) < ( MAX_NICK_LENGTH - 1 ) ) |
---|
| 95 | { |
---|
| 96 | nick[strlen(nick)+1] = 0; |
---|
| 97 | nick[strlen(nick)] = '_'; |
---|
| 98 | } |
---|
| 99 | else |
---|
| 100 | { |
---|
| 101 | nick[0] ++; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | if( inf_protection-- == 0 ) |
---|
| 105 | { |
---|
| 106 | int i; |
---|
| 107 | |
---|
| 108 | irc_usermsg( irc, "WARNING: Almost had an infinite loop in nick_get()! " |
---|
| 109 | "This used to be a fatal BitlBee bug, but we tried to fix it. " |
---|
| 110 | "This message should *never* appear anymore. " |
---|
| 111 | "If it does, please *do* send us a bug report! " |
---|
| 112 | "Please send all the following lines in your report:" ); |
---|
| 113 | |
---|
| 114 | irc_usermsg( irc, "Trying to get a sane nick for handle %s", handle ); |
---|
| 115 | for( i = 0; i < MAX_NICK_LENGTH; i ++ ) |
---|
| 116 | irc_usermsg( irc, "Char %d: %c/%d", i, nick[i], nick[i] ); |
---|
| 117 | |
---|
| 118 | irc_usermsg( irc, "FAILED. Returning an insane nick now. Things might break. " |
---|
| 119 | "Good luck, and please don't forget to paste the lines up here " |
---|
| 120 | "in #bitlbee on OFTC or in a mail to wilmer@gaast.net" ); |
---|
| 121 | |
---|
| 122 | g_snprintf( nick, MAX_NICK_LENGTH + 1, "xx%x", rand() ); |
---|
| 123 | |
---|
| 124 | break; |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | return( nick ); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | void nick_del( irc_t *irc, char *nick ) |
---|
| 132 | { |
---|
| 133 | nick_t *l = NULL, *n = irc->nicks; |
---|
| 134 | |
---|
| 135 | while( n ) |
---|
| 136 | { |
---|
| 137 | if( g_strcasecmp( n->nick, nick ) == 0 ) |
---|
| 138 | { |
---|
| 139 | if( l ) |
---|
| 140 | l->next = n->next; |
---|
| 141 | else |
---|
| 142 | irc->nicks = n->next; |
---|
| 143 | |
---|
| 144 | g_free( n->handle ); |
---|
| 145 | g_free( n->nick ); |
---|
| 146 | g_free( n ); |
---|
| 147 | |
---|
| 148 | break; |
---|
| 149 | } |
---|
| 150 | n = (l=n)->next; |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | /* Character maps, _lc_[x] == _uc_[x] (but uppercase), according to the RFC's. |
---|
| 156 | With one difference, we allow dashes. */ |
---|
| 157 | |
---|
| 158 | static char *nick_lc_chars = "0123456789abcdefghijklmnopqrstuvwxyz{}^-_|"; |
---|
| 159 | static char *nick_uc_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ[]~-_\\"; |
---|
| 160 | |
---|
| 161 | void nick_strip( char * nick ) |
---|
| 162 | { |
---|
| 163 | int i, j; |
---|
| 164 | |
---|
| 165 | for( i = j = 0; nick[i] && i < MAX_NICK_LENGTH; i++ ) |
---|
| 166 | { |
---|
| 167 | if( strchr( nick_lc_chars, nick[i] ) || |
---|
| 168 | strchr( nick_uc_chars, nick[i] ) ) |
---|
| 169 | { |
---|
| 170 | nick[j] = nick[i]; |
---|
| 171 | j++; |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | while( j < MAX_NICK_LENGTH ) |
---|
| 175 | nick[j++] = '\0'; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | int nick_ok( char *nick ) |
---|
| 179 | { |
---|
| 180 | char *s; |
---|
| 181 | |
---|
| 182 | /* Empty/long nicks are not allowed */ |
---|
| 183 | if( !*nick || strlen( nick ) > MAX_NICK_LENGTH ) |
---|
| 184 | return( 0 ); |
---|
| 185 | |
---|
| 186 | for( s = nick; *s; s ++ ) |
---|
| 187 | if( !strchr( nick_lc_chars, *s ) && !strchr( nick_uc_chars, *s ) ) |
---|
| 188 | return( 0 ); |
---|
| 189 | |
---|
| 190 | return( 1 ); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | int nick_lc( char *nick ) |
---|
| 194 | { |
---|
| 195 | static char tab[256] = { 0 }; |
---|
| 196 | int i; |
---|
| 197 | |
---|
| 198 | if( tab['A'] == 0 ) |
---|
| 199 | for( i = 0; nick_lc_chars[i]; i ++ ) |
---|
| 200 | { |
---|
| 201 | tab[(int)nick_uc_chars[i]] = nick_lc_chars[i]; |
---|
| 202 | tab[(int)nick_lc_chars[i]] = nick_lc_chars[i]; |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | for( i = 0; nick[i]; i ++ ) |
---|
| 206 | { |
---|
| 207 | if( !tab[(int)nick[i]] ) |
---|
| 208 | return( 0 ); |
---|
| 209 | |
---|
| 210 | nick[i] = tab[(int)nick[i]]; |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | return( 1 ); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | int nick_uc( char *nick ) |
---|
| 217 | { |
---|
| 218 | static char tab[128] = { 0 }; |
---|
| 219 | int i; |
---|
| 220 | |
---|
| 221 | if( tab['A'] == 0 ) |
---|
| 222 | for( i = 0; nick_lc_chars[i]; i ++ ) |
---|
| 223 | { |
---|
| 224 | tab[(int)nick_uc_chars[i]] = nick_uc_chars[i]; |
---|
| 225 | tab[(int)nick_lc_chars[i]] = nick_uc_chars[i]; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | for( i = 0; nick[i]; i ++ ) |
---|
| 229 | { |
---|
| 230 | if( !tab[(int)nick[i]] ) |
---|
| 231 | return( 0 ); |
---|
| 232 | |
---|
| 233 | nick[i] = tab[(int)nick[i]]; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | return( 1 ); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | int nick_cmp( char *a, char *b ) |
---|
| 240 | { |
---|
| 241 | char aa[1024] = "", bb[1024] = ""; |
---|
| 242 | |
---|
| 243 | strncpy( aa, a, sizeof( aa ) - 1 ); |
---|
| 244 | strncpy( bb, b, sizeof( bb ) - 1 ); |
---|
| 245 | if( nick_lc( aa ) && nick_lc( bb ) ) |
---|
| 246 | { |
---|
| 247 | return( strcmp( aa, bb ) ); |
---|
| 248 | } |
---|
| 249 | else |
---|
| 250 | { |
---|
| 251 | return( -1 ); /* Hmm... Not a clear answer.. :-/ */ |
---|
| 252 | } |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | char *nick_dup( char *nick ) |
---|
| 256 | { |
---|
| 257 | char *cp; |
---|
| 258 | |
---|
| 259 | cp = g_new0 ( char, MAX_NICK_LENGTH + 1 ); |
---|
| 260 | strncpy( cp, nick, MAX_NICK_LENGTH ); |
---|
| 261 | |
---|
| 262 | return( cp ); |
---|
| 263 | } |
---|