Changeset 5ebff60 for irc_user.c
- Timestamp:
- 2015-02-20T22:50:54Z (9 years ago)
- Branches:
- master
- Children:
- 0b9daac, 3d45471, 7733b8c
- Parents:
- af359b4
- git-author:
- Indent <please@…> (19-02-15 05:47:20)
- git-committer:
- dequis <dx@…> (20-02-15 22:50:54)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
irc_user.c
raf359b4 r5ebff60 1 1 /********************************************************************\ 2 2 * BitlBee -- An IRC to other IM-networks gateway * 3 3 * * … … 27 27 #include "ipc.h" 28 28 29 irc_user_t *irc_user_new( irc_t *irc, const char *nick)30 { 31 irc_user_t *iu = g_new0( irc_user_t, 1);32 29 irc_user_t *irc_user_new(irc_t *irc, const char *nick) 30 { 31 irc_user_t *iu = g_new0(irc_user_t, 1); 32 33 33 iu->irc = irc; 34 iu->nick = g_strdup( nick);34 iu->nick = g_strdup(nick); 35 35 iu->user = iu->host = iu->fullname = iu->nick; 36 37 iu->key = g_strdup( nick);38 nick_lc( irc, iu->key);36 37 iu->key = g_strdup(nick); 38 nick_lc(irc, iu->key); 39 39 /* Using the hash table for speed and irc->users for easy iteration 40 40 through the list (since the GLib API doesn't have anything sane 41 41 for that.) */ 42 g_hash_table_insert( irc->nick_user_hash, iu->key, iu);43 irc->users = g_slist_insert_sorted( irc->users, iu, irc_user_cmp);44 42 g_hash_table_insert(irc->nick_user_hash, iu->key, iu); 43 irc->users = g_slist_insert_sorted(irc->users, iu, irc_user_cmp); 44 45 45 return iu; 46 46 } 47 47 48 int irc_user_free( irc_t *irc, irc_user_t *iu)48 int irc_user_free(irc_t *irc, irc_user_t *iu) 49 49 { 50 50 static struct im_connection *last_ic; 51 51 static char *msg; 52 53 if ( !iu )52 53 if (!iu) { 54 54 return 0; 55 56 if( iu->bu && 57 ( iu->bu->ic->flags & OPT_LOGGING_OUT )&&58 iu->bu->ic != last_ic )59 {55 } 56 57 if (iu->bu && 58 (iu->bu->ic->flags & OPT_LOGGING_OUT) && 59 iu->bu->ic != last_ic) { 60 60 char host_prefix[] = "bitlbee."; 61 61 char *s; 62 62 63 63 /* Irssi recognises netsplits by quitmsgs with two 64 64 hostnames, where a hostname is a "word" with one 65 65 of more dots. Mangle no-dot hostnames a bit. */ 66 if ( strchr( irc->root->host, '.' ) )66 if (strchr(irc->root->host, '.')) { 67 67 *host_prefix = '\0'; 68 68 } 69 69 70 last_ic = iu->bu->ic; 70 g_free( msg ); 71 if( !set_getbool( &irc->b->set, "simulate_netsplit" ) ) 72 msg = g_strdup( "Account off-line" ); 73 else if( ( s = strchr( iu->bu->ic->acc->user, '@' ) ) ) 74 msg = g_strdup_printf( "%s%s %s", host_prefix, 75 irc->root->host, s + 1 ); 76 else 77 msg = g_strdup_printf( "%s%s %s.%s", 78 host_prefix, irc->root->host, 79 iu->bu->ic->acc->prpl->name, irc->root->host ); 80 } 81 else if( !iu->bu || !( iu->bu->ic->flags & OPT_LOGGING_OUT ) ) 82 { 83 g_free( msg ); 84 msg = g_strdup( "Removed" ); 71 g_free(msg); 72 if (!set_getbool(&irc->b->set, "simulate_netsplit")) { 73 msg = g_strdup("Account off-line"); 74 } else if ((s = strchr(iu->bu->ic->acc->user, '@'))) { 75 msg = g_strdup_printf("%s%s %s", host_prefix, 76 irc->root->host, s + 1); 77 } else { 78 msg = g_strdup_printf("%s%s %s.%s", 79 host_prefix, irc->root->host, 80 iu->bu->ic->acc->prpl->name, irc->root->host); 81 } 82 } else if (!iu->bu || !(iu->bu->ic->flags & OPT_LOGGING_OUT)) { 83 g_free(msg); 84 msg = g_strdup("Removed"); 85 85 last_ic = NULL; 86 86 } 87 irc_user_quit( iu, msg ); 88 89 irc->users = g_slist_remove( irc->users, iu ); 90 g_hash_table_remove( irc->nick_user_hash, iu->key ); 91 92 g_free( iu->nick ); 93 if( iu->nick != iu->user ) g_free( iu->user ); 94 if( iu->nick != iu->host ) g_free( iu->host ); 95 if( iu->nick != iu->fullname ) g_free( iu->fullname ); 96 g_free( iu->pastebuf ); 97 if( iu->pastebuf_timer ) b_event_remove( iu->pastebuf_timer ); 98 g_free( iu->key ); 99 g_free( iu ); 100 87 irc_user_quit(iu, msg); 88 89 irc->users = g_slist_remove(irc->users, iu); 90 g_hash_table_remove(irc->nick_user_hash, iu->key); 91 92 g_free(iu->nick); 93 if (iu->nick != iu->user) { 94 g_free(iu->user); 95 } 96 if (iu->nick != iu->host) { 97 g_free(iu->host); 98 } 99 if (iu->nick != iu->fullname) { 100 g_free(iu->fullname); 101 } 102 g_free(iu->pastebuf); 103 if (iu->pastebuf_timer) { 104 b_event_remove(iu->pastebuf_timer); 105 } 106 g_free(iu->key); 107 g_free(iu); 108 101 109 return 1; 102 110 } 103 111 104 irc_user_t *irc_user_by_name( irc_t *irc, const char *nick)105 { 106 char key[strlen(nick) +1];107 108 strcpy( key, nick);109 if ( nick_lc( irc, key ) )110 return g_hash_table_lookup( irc->nick_user_hash, key);111 else112 irc_user_t *irc_user_by_name(irc_t *irc, const char *nick) 113 { 114 char key[strlen(nick) + 1]; 115 116 strcpy(key, nick); 117 if (nick_lc(irc, key)) { 118 return g_hash_table_lookup(irc->nick_user_hash, key); 119 } else { 112 120 return NULL; 113 } 114 115 int irc_user_set_nick( irc_user_t *iu, const char *new ) 121 } 122 } 123 124 int irc_user_set_nick(irc_user_t *iu, const char *new) 116 125 { 117 126 irc_t *irc = iu->irc; 118 127 irc_user_t *new_iu; 119 char key[strlen(new) +1];128 char key[strlen(new) + 1]; 120 129 GSList *cl; 121 122 strcpy( key, new);123 if ( iu == NULL || !nick_lc( irc, key) ||124 ( ( new_iu = irc_user_by_name( irc, new ) ) && new_iu != iu ) )130 131 strcpy(key, new); 132 if (iu == NULL || !nick_lc(irc, key) || 133 ((new_iu = irc_user_by_name(irc, new)) && new_iu != iu)) { 125 134 return 0; 126 127 for( cl = irc->channels; cl; cl = cl->next ) 128 {135 } 136 137 for (cl = irc->channels; cl; cl = cl->next) { 129 138 irc_channel_t *ic = cl->data; 130 139 131 140 /* Send a NICK update if we're renaming our user, or someone 132 141 who's in the same channel like our user. */ 133 if( iu == irc->user || 134 ( ( ic->flags & IRC_CHANNEL_JOINED ) && 135 irc_channel_has_user( ic, iu ) ) ) 136 { 137 irc_send_nick( iu, new ); 142 if (iu == irc->user || 143 ((ic->flags & IRC_CHANNEL_JOINED) && 144 irc_channel_has_user(ic, iu))) { 145 irc_send_nick(iu, new); 138 146 break; 139 147 } 140 148 } 141 142 irc->users = g_slist_remove( irc->users, iu ); 143 g_hash_table_remove( irc->nick_user_hash, iu->key ); 144 145 if( iu->nick == iu->user ) iu->user = NULL; 146 if( iu->nick == iu->host ) iu->host = NULL; 147 if( iu->nick == iu->fullname ) iu->fullname = NULL; 148 g_free( iu->nick ); 149 iu->nick = g_strdup( new ); 150 if( iu->user == NULL ) iu->user = g_strdup( iu->nick ); 151 if( iu->host == NULL ) iu->host = g_strdup( iu->nick ); 152 if( iu->fullname == NULL ) iu->fullname = g_strdup( iu->nick ); 153 154 g_free( iu->key ); 155 iu->key = g_strdup( key ); 156 g_hash_table_insert( irc->nick_user_hash, iu->key, iu ); 157 irc->users = g_slist_insert_sorted( irc->users, iu, irc_user_cmp ); 158 159 if( iu == irc->user ) 160 ipc_to_master_str( "NICK :%s\r\n", new ); 161 149 150 irc->users = g_slist_remove(irc->users, iu); 151 g_hash_table_remove(irc->nick_user_hash, iu->key); 152 153 if (iu->nick == iu->user) { 154 iu->user = NULL; 155 } 156 if (iu->nick == iu->host) { 157 iu->host = NULL; 158 } 159 if (iu->nick == iu->fullname) { 160 iu->fullname = NULL; 161 } 162 g_free(iu->nick); 163 iu->nick = g_strdup(new); 164 if (iu->user == NULL) { 165 iu->user = g_strdup(iu->nick); 166 } 167 if (iu->host == NULL) { 168 iu->host = g_strdup(iu->nick); 169 } 170 if (iu->fullname == NULL) { 171 iu->fullname = g_strdup(iu->nick); 172 } 173 174 g_free(iu->key); 175 iu->key = g_strdup(key); 176 g_hash_table_insert(irc->nick_user_hash, iu->key, iu); 177 irc->users = g_slist_insert_sorted(irc->users, iu, irc_user_cmp); 178 179 if (iu == irc->user) { 180 ipc_to_master_str("NICK :%s\r\n", new); 181 } 182 162 183 return 1; 163 184 } 164 185 165 gint irc_user_cmp( gconstpointer a_, gconstpointer b_)186 gint irc_user_cmp(gconstpointer a_, gconstpointer b_) 166 187 { 167 188 const irc_user_t *a = a_, *b = b_; 168 169 return strcmp( a->key, b->key);170 } 171 172 const char *irc_user_get_away( irc_user_t *iu)189 190 return strcmp(a->key, b->key); 191 } 192 193 const char *irc_user_get_away(irc_user_t *iu) 173 194 { 174 195 irc_t *irc = iu->irc; 175 196 bee_user_t *bu = iu->bu; 176 177 if( iu == irc->user ) 178 return set_getstr( &irc->b->set, "away" ); 179 else if( bu ) 180 { 181 if( !bu->flags & BEE_USER_ONLINE ) 197 198 if (iu == irc->user) { 199 return set_getstr(&irc->b->set, "away"); 200 } else if (bu) { 201 if (!bu->flags & BEE_USER_ONLINE) { 182 202 return "Offline"; 183 else if( bu->flags & BEE_USER_AWAY ) 184 { 185 if( bu->status_msg ) 186 { 203 } else if (bu->flags & BEE_USER_AWAY) { 204 if (bu->status_msg) { 187 205 static char ret[MAX_STRING]; 188 g_snprintf( 189 bu->status ? : "Away", bu->status_msg);206 g_snprintf(ret, MAX_STRING - 1, "%s (%s)", 207 bu->status ? : "Away", bu->status_msg); 190 208 return ret; 209 } else { 210 return bu->status ? : "Away"; 191 211 } 192 else 193 return bu->status ? : "Away"; 194 } 195 } 196 212 } 213 } 214 197 215 return NULL; 198 216 } 199 217 200 void irc_user_quit( irc_user_t *iu, const char *msg)218 void irc_user_quit(irc_user_t *iu, const char *msg) 201 219 { 202 220 GSList *l; 203 221 gboolean send_quit = FALSE; 204 205 if ( !iu )222 223 if (!iu) { 206 224 return; 207 208 for( l = iu->irc->channels; l; l = l->next ) 209 {225 } 226 227 for (l = iu->irc->channels; l; l = l->next) { 210 228 irc_channel_t *ic = l->data; 211 send_quit |= irc_channel_del_user( ic, iu, IRC_CDU_SILENT, NULL ) && 212 ( ic->flags & IRC_CHANNEL_JOINED ); 213 } 214 215 if( send_quit ) 216 irc_send_quit( iu, msg ); 229 send_quit |= irc_channel_del_user(ic, iu, IRC_CDU_SILENT, NULL) && 230 (ic->flags & IRC_CHANNEL_JOINED); 231 } 232 233 if (send_quit) { 234 irc_send_quit(iu, msg); 235 } 217 236 } 218 237 219 238 /* User-type dependent functions, for root/NickServ: */ 220 static gboolean root_privmsg( irc_user_t *iu, const char *msg)221 { 222 char cmd[strlen(msg) +1];223 224 strcpy( cmd, msg);225 root_command_string( iu->irc, cmd);226 239 static gboolean root_privmsg(irc_user_t *iu, const char *msg) 240 { 241 char cmd[strlen(msg) + 1]; 242 243 strcpy(cmd, msg); 244 root_command_string(iu->irc, cmd); 245 227 246 return TRUE; 228 247 } 229 248 230 static gboolean root_ctcp( irc_user_t *iu, char * const *ctcp ) 231 { 232 if( g_strcasecmp( ctcp[0], "VERSION" ) == 0 ) 233 { 234 irc_send_msg_f( iu, "NOTICE", iu->irc->user->nick, "\001%s %s\001", 235 ctcp[0], PACKAGE " " BITLBEE_VERSION " " ARCH "/" CPU ); 236 } 237 else if( g_strcasecmp( ctcp[0], "PING" ) == 0 ) 238 { 239 irc_send_msg_f( iu, "NOTICE", iu->irc->user->nick, "\001%s %s\001", 240 ctcp[0], ctcp[1] ? : "" ); 241 } 242 249 static gboolean root_ctcp(irc_user_t *iu, char * const *ctcp) 250 { 251 if (g_strcasecmp(ctcp[0], "VERSION") == 0) { 252 irc_send_msg_f(iu, "NOTICE", iu->irc->user->nick, "\001%s %s\001", 253 ctcp[0], PACKAGE " " BITLBEE_VERSION " " ARCH "/" CPU); 254 } else if (g_strcasecmp(ctcp[0], "PING") == 0) { 255 irc_send_msg_f(iu, "NOTICE", iu->irc->user->nick, "\001%s %s\001", 256 ctcp[0], ctcp[1] ? : ""); 257 } 258 243 259 return TRUE; 244 260 } … … 250 266 251 267 /* Echo to yourself: */ 252 static gboolean self_privmsg( irc_user_t *iu, const char *msg)253 { 254 irc_send_msg( iu, "PRIVMSG", iu->nick, msg, NULL);255 268 static gboolean self_privmsg(irc_user_t *iu, const char *msg) 269 { 270 irc_send_msg(iu, "PRIVMSG", iu->nick, msg, NULL); 271 256 272 return TRUE; 257 273 }
Note: See TracChangeset
for help on using the changeset viewer.