Changeset 5ebff60 for protocols/bee_chat.c
- Timestamp:
- 2015-02-20T22:50:54Z (10 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
-
protocols/bee_chat.c
raf359b4 r5ebff60 1 1 /********************************************************************\ 2 2 * BitlBee -- An IRC to other IM-networks gateway * 3 3 * * … … 27 27 #include "bitlbee.h" 28 28 29 struct groupchat *imcb_chat_new( struct im_connection *ic, const char *handle)30 { 31 struct groupchat *c = g_new0( struct groupchat, 1);32 bee_t *bee = ic->bee; 33 29 struct groupchat *imcb_chat_new(struct im_connection *ic, const char *handle) 30 { 31 struct groupchat *c = g_new0(struct groupchat, 1); 32 bee_t *bee = ic->bee; 33 34 34 /* This one just creates the conversation structure, user won't see 35 35 anything yet until s/he is joined to the conversation. (This 36 36 allows you to add other already present participants first.) */ 37 38 ic->groupchats = g_slist_prepend( ic->groupchats, c);37 38 ic->groupchats = g_slist_prepend(ic->groupchats, c); 39 39 c->ic = ic; 40 c->title = g_strdup( handle ); 41 c->topic = g_strdup_printf( "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", c->title ); 42 43 if( set_getbool( &ic->bee->set, "debug" ) ) 44 imcb_log( ic, "Creating new conversation: (id=%p,handle=%s)", c, handle ); 45 46 if( bee->ui->chat_new ) 47 bee->ui->chat_new( bee, c ); 48 40 c->title = g_strdup(handle); 41 c->topic = g_strdup_printf( 42 "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", 43 c->title); 44 45 if (set_getbool(&ic->bee->set, "debug")) { 46 imcb_log(ic, "Creating new conversation: (id=%p,handle=%s)", c, handle); 47 } 48 49 if (bee->ui->chat_new) { 50 bee->ui->chat_new(bee, c); 51 } 52 49 53 return c; 50 54 } 51 55 52 void imcb_chat_name_hint( struct groupchat *c, const char *name)56 void imcb_chat_name_hint(struct groupchat *c, const char *name) 53 57 { 54 58 bee_t *bee = c->ic->bee; 55 56 if( bee->ui->chat_name_hint ) 57 bee->ui->chat_name_hint( bee, c, name ); 58 } 59 60 void imcb_chat_free( struct groupchat *c ) 59 60 if (bee->ui->chat_name_hint) { 61 bee->ui->chat_name_hint(bee, c, name); 62 } 63 } 64 65 void imcb_chat_free(struct groupchat *c) 61 66 { 62 67 struct im_connection *ic = c->ic; 63 68 bee_t *bee = ic->bee; 64 69 GList *ir; 65 66 if( bee->ui->chat_free ) 67 bee->ui->chat_free( bee, c ); 68 69 if( set_getbool( &ic->bee->set, "debug" ) ) 70 imcb_log( ic, "You were removed from conversation %p", c ); 71 72 ic->groupchats = g_slist_remove( ic->groupchats, c ); 73 74 for( ir = c->in_room; ir; ir = ir->next ) 75 g_free( ir->data ); 76 g_list_free( c->in_room ); 77 g_free( c->title ); 78 g_free( c->topic ); 79 g_free( c ); 80 } 81 82 static gboolean handle_is_self( struct im_connection *ic, const char *handle ) 83 { 84 return ( ic->acc->prpl->handle_is_self ) ? 85 ic->acc->prpl->handle_is_self( ic, handle ) : 86 ( ic->acc->prpl->handle_cmp( ic->acc->user, handle ) == 0 ); 87 } 88 89 void imcb_chat_msg( struct groupchat *c, const char *who, char *msg, uint32_t flags, time_t sent_at ) 70 71 if (bee->ui->chat_free) { 72 bee->ui->chat_free(bee, c); 73 } 74 75 if (set_getbool(&ic->bee->set, "debug")) { 76 imcb_log(ic, "You were removed from conversation %p", c); 77 } 78 79 ic->groupchats = g_slist_remove(ic->groupchats, c); 80 81 for (ir = c->in_room; ir; ir = ir->next) { 82 g_free(ir->data); 83 } 84 g_list_free(c->in_room); 85 g_free(c->title); 86 g_free(c->topic); 87 g_free(c); 88 } 89 90 static gboolean handle_is_self(struct im_connection *ic, const char *handle) 91 { 92 return (ic->acc->prpl->handle_is_self) ? 93 ic->acc->prpl->handle_is_self(ic, handle) : 94 (ic->acc->prpl->handle_cmp(ic->acc->user, handle) == 0); 95 } 96 97 void imcb_chat_msg(struct groupchat *c, const char *who, char *msg, uint32_t flags, time_t sent_at) 90 98 { 91 99 struct im_connection *ic = c->ic; … … 94 102 gboolean temp; 95 103 char *s; 96 104 97 105 /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */ 98 if ( handle_is_self( ic, who ) )106 if (handle_is_self(ic, who)) { 99 107 return; 100 101 bu = bee_user_by_handle( bee, ic, who ); 102 temp = ( bu == NULL ); 103 104 if( temp ) 105 bu = bee_user_new( bee, ic, who, BEE_USER_ONLINE ); 106 107 s = set_getstr( &ic->bee->set, "strip_html" ); 108 if( ( g_strcasecmp( s, "always" ) == 0 ) || 109 ( ( ic->flags & OPT_DOES_HTML ) && s ) ) 110 strip_html( msg ); 111 112 if( bee->ui->chat_msg ) 113 bee->ui->chat_msg( bee, c, bu, msg, sent_at ); 114 115 if( temp ) 116 bee_user_free( bee, bu ); 117 } 118 119 void imcb_chat_log( struct groupchat *c, char *format, ... ) 108 } 109 110 bu = bee_user_by_handle(bee, ic, who); 111 temp = (bu == NULL); 112 113 if (temp) { 114 bu = bee_user_new(bee, ic, who, BEE_USER_ONLINE); 115 } 116 117 s = set_getstr(&ic->bee->set, "strip_html"); 118 if ((g_strcasecmp(s, "always") == 0) || 119 ((ic->flags & OPT_DOES_HTML) && s)) { 120 strip_html(msg); 121 } 122 123 if (bee->ui->chat_msg) { 124 bee->ui->chat_msg(bee, c, bu, msg, sent_at); 125 } 126 127 if (temp) { 128 bee_user_free(bee, bu); 129 } 130 } 131 132 void imcb_chat_log(struct groupchat *c, char *format, ...) 120 133 { 121 134 struct im_connection *ic = c->ic; … … 123 136 va_list params; 124 137 char *text; 125 126 if ( !bee->ui->chat_log )138 139 if (!bee->ui->chat_log) { 127 140 return; 128 129 va_start( params, format ); 130 text = g_strdup_vprintf( format, params ); 131 va_end( params ); 132 133 bee->ui->chat_log( bee, c, text ); 134 g_free( text ); 135 } 136 137 void imcb_chat_topic( struct groupchat *c, char *who, char *topic, time_t set_at ) 141 } 142 143 va_start(params, format); 144 text = g_strdup_vprintf(format, params); 145 va_end(params); 146 147 bee->ui->chat_log(bee, c, text); 148 g_free(text); 149 } 150 151 void imcb_chat_topic(struct groupchat *c, char *who, char *topic, time_t set_at) 138 152 { 139 153 struct im_connection *ic = c->ic; 140 154 bee_t *bee = ic->bee; 141 155 bee_user_t *bu; 142 143 if ( !bee->ui->chat_topic )156 157 if (!bee->ui->chat_topic) { 144 158 return; 145 146 if( who == NULL) 159 } 160 161 if (who == NULL) { 147 162 bu = NULL; 148 else if( handle_is_self( ic, who ) )163 } else if (handle_is_self(ic, who)) { 149 164 bu = bee->user; 150 else 151 bu = bee_user_by_handle( bee, ic, who ); 152 153 if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) || 154 ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) ) 155 strip_html( topic ); 156 157 bee->ui->chat_topic( bee, c, topic, bu ); 158 } 159 160 void imcb_chat_add_buddy( struct groupchat *c, const char *handle ) 161 { 162 struct im_connection *ic = c->ic; 163 bee_t *bee = ic->bee; 164 bee_user_t *bu = bee_user_by_handle( bee, ic, handle ); 165 } else { 166 bu = bee_user_by_handle(bee, ic, who); 167 } 168 169 if ((g_strcasecmp(set_getstr(&ic->bee->set, "strip_html"), "always") == 0) || 170 ((ic->flags & OPT_DOES_HTML) && set_getbool(&ic->bee->set, "strip_html"))) { 171 strip_html(topic); 172 } 173 174 bee->ui->chat_topic(bee, c, topic, bu); 175 } 176 177 void imcb_chat_add_buddy(struct groupchat *c, const char *handle) 178 { 179 struct im_connection *ic = c->ic; 180 bee_t *bee = ic->bee; 181 bee_user_t *bu = bee_user_by_handle(bee, ic, handle); 165 182 gboolean me; 166 167 if( set_getbool( &c->ic->bee->set, "debug" ) ) 168 imcb_log( c->ic, "User %s added to conversation %p", handle, c ); 169 170 me = handle_is_self( ic, handle ); 171 183 184 if (set_getbool(&c->ic->bee->set, "debug")) { 185 imcb_log(c->ic, "User %s added to conversation %p", handle, c); 186 } 187 188 me = handle_is_self(ic, handle); 189 172 190 /* Most protocols allow people to join, even when they're not in 173 191 your contact list. Try to handle that here */ 174 if( !me && !bu ) 175 bu = bee_user_new( bee, ic, handle, BEE_USER_LOCAL ); 176 192 if (!me && !bu) { 193 bu = bee_user_new(bee, ic, handle, BEE_USER_LOCAL); 194 } 195 177 196 /* Add the handle to the room userlist */ 178 197 /* TODO: Use bu instead of a string */ 179 c->in_room = g_list_append( c->in_room, g_strdup( handle ) ); 180 181 if( bee->ui->chat_add_user ) 182 bee->ui->chat_add_user( bee, c, me ? bee->user : bu ); 183 184 if( me ) 198 c->in_room = g_list_append(c->in_room, g_strdup(handle)); 199 200 if (bee->ui->chat_add_user) { 201 bee->ui->chat_add_user(bee, c, me ? bee->user : bu); 202 } 203 204 if (me) { 185 205 c->joined = 1; 186 } 187 188 void imcb_chat_remove_buddy( struct groupchat *c, const char *handle, const char *reason ) 206 } 207 } 208 209 void imcb_chat_remove_buddy(struct groupchat *c, const char *handle, const char *reason) 189 210 { 190 211 struct im_connection *ic = c->ic; 191 212 bee_t *bee = ic->bee; 192 213 bee_user_t *bu = NULL; 193 194 if( set_getbool( &bee->set, "debug" ) ) 195 imcb_log( ic, "User %s removed from conversation %p (%s)", handle, c, reason ? reason : "" ); 196 214 215 if (set_getbool(&bee->set, "debug")) { 216 imcb_log(ic, "User %s removed from conversation %p (%s)", handle, c, reason ? reason : ""); 217 } 218 197 219 /* It might be yourself! */ 198 if( handle_is_self( ic, handle ) ) 199 { 200 if( c->joined == 0 ) 220 if (handle_is_self(ic, handle)) { 221 if (c->joined == 0) { 201 222 return; 202 223 } 224 203 225 bu = bee->user; 204 226 c->joined = 0; 205 } 206 else 207 { 208 bu = bee_user_by_handle( bee, ic, handle ); 209 } 210 211 if( bee->ui->chat_remove_user && bu ) 212 bee->ui->chat_remove_user( bee, c, bu ); 213 } 214 215 int bee_chat_msg( bee_t *bee, struct groupchat *c, const char *msg, int flags ) 227 } else { 228 bu = bee_user_by_handle(bee, ic, handle); 229 } 230 231 if (bee->ui->chat_remove_user && bu) { 232 bee->ui->chat_remove_user(bee, c, bu); 233 } 234 } 235 236 int bee_chat_msg(bee_t *bee, struct groupchat *c, const char *msg, int flags) 216 237 { 217 238 struct im_connection *ic = c->ic; 218 239 char *buf = NULL; 219 220 if( ( ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) ) 221 { 222 buf = escape_html( msg ); 240 241 if ((ic->flags & OPT_DOES_HTML) && (g_strncasecmp(msg, "<html>", 6) != 0)) { 242 buf = escape_html(msg); 223 243 msg = buf; 224 } 225 else226 buf = g_strdup( msg );227 228 ic->acc->prpl->chat_msg( c, buf, flags);229 g_free( buf);230 244 } else { 245 buf = g_strdup(msg); 246 } 247 248 ic->acc->prpl->chat_msg(c, buf, flags); 249 g_free(buf); 250 231 251 return 1; 232 252 } 233 253 234 struct groupchat *bee_chat_by_title( bee_t *bee, struct im_connection *ic, const char *title)254 struct groupchat *bee_chat_by_title(bee_t *bee, struct im_connection *ic, const char *title) 235 255 { 236 256 struct groupchat *c; 237 257 GSList *l; 238 239 for( l = ic->groupchats; l; l = l->next ) 240 { 258 259 for (l = ic->groupchats; l; l = l->next) { 241 260 c = l->data; 242 if ( strcmp( c->title, title ) == 0 )261 if (strcmp(c->title, title) == 0) { 243 262 return c; 244 } 245 263 } 264 } 265 246 266 return NULL; 247 267 } 248 268 249 void imcb_chat_invite( struct im_connection *ic, const char *name, const char *who, const char *msg ) 250 { 251 bee_user_t *bu = bee_user_by_handle( ic->bee, ic, who ); 252 253 if( bu && ic->bee->ui->chat_invite ) 254 ic->bee->ui->chat_invite( ic->bee, bu, name, msg ); 255 } 269 void imcb_chat_invite(struct im_connection *ic, const char *name, const char *who, const char *msg) 270 { 271 bee_user_t *bu = bee_user_by_handle(ic->bee, ic, who); 272 273 if (bu && ic->bee->ui->chat_invite) { 274 ic->bee->ui->chat_invite(ic->bee, bu, name, msg); 275 } 276 }
Note: See TracChangeset
for help on using the changeset viewer.