source: chat.c @ 549545b

Last change on this file since 549545b was 549545b, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-12-13T20:02:55Z

Checking if acc->prpl->chat_join actually exists before using it. :-/

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[ad9feec]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2008 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* Keep track of chatrooms the user is interested in                    */
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#include "bitlbee.h"
27
28struct chat *chat_add( irc_t *irc, account_t *acc, char *handle, char *channel )
29{
30        struct chat *c, *l;
[e7bc722]31        set_t *s;
[549545b]32
33        if( acc->prpl->chat_join == NULL )
34                return NULL;
[ad9feec]35       
36        if( !chat_chanok( channel ) )
37                return NULL;
38       
[0e639f5]39        if( chat_chancmp( channel, irc->channel ) == 0 )
40                return NULL;
41       
[ad9feec]42        for( c = irc->chatrooms; c; c = c->next )
43        {
44                if( chat_chancmp( channel, c->channel ) == 0 )
45                        return NULL;
46               
47                if( acc == c->acc && g_strcasecmp( handle, c->handle ) == 0 )
48                        return NULL;
49               
50                l = c;
51        }
52       
53        if( irc->chatrooms == NULL )
54                irc->chatrooms = c = g_new0( struct chat, 1 );
55        else
56                l->next = c = g_new0( struct chat, 1 );
57       
58        c->acc = acc;
59        c->handle = g_strdup( handle );
60        c->channel = g_strdup( channel );
61       
[e7bc722]62        s = set_add( &c->set, "auto_join", "false", set_eval_bool, c );
[3611717]63        /* s = set_add( &c->set, "auto_rejoin", "false", set_eval_bool, c ); */
[e7bc722]64        s = set_add( &c->set, "nick", NULL, NULL, c );
65        s->flags |= SET_NULL_OK;
[ad9feec]66       
67        return c;
68}
69
70struct chat *chat_byhandle( irc_t *irc, account_t *acc, char *handle )
71{
72        struct chat *c;
73       
74        for( c = irc->chatrooms; c; c = c->next )
75        {
76                if( acc == c->acc && g_strcasecmp( handle, c->handle ) == 0 )
77                        break;
78        }
79       
80        return c;
81}
82
83struct chat *chat_bychannel( irc_t *irc, char *channel )
84{
85        struct chat *c;
86       
87        for( c = irc->chatrooms; c; c = c->next )
88        {
89                if( chat_chancmp( channel, c->channel ) == 0 )
90                        break;
91        }
92       
93        return c;
94}
95
[131c6b6]96struct chat *chat_get( irc_t *irc, char *id )
97{
98        struct chat *c, *ret = NULL;
99        int nr;
100       
101        if( sscanf( id, "%d", &nr ) == 1 && nr < 1000 )
102        {
103                for( c = irc->chatrooms; c; c = c->next )
104                        if( ( nr-- ) == 0 )
105                                return c;
106               
107                return NULL;
108        }
109       
110        for( c = irc->chatrooms; c; c = c->next )
111        {
112                if( strstr( c->handle, id ) )
113                {
114                        if( !ret )
115                                ret = c;
116                        else
117                                return NULL;
118                }
119                else if( strstr( c->channel, id ) )
120                {
121                        if( !ret )
122                                ret = c;
123                        else
124                                return NULL;
125                }
126        }
127       
128        return ret;
129}
130
[d995c9b]131int chat_del( irc_t *irc, struct chat *chat )
132{
133        struct chat *c, *l = NULL;
134       
135        for( c = irc->chatrooms; c; c = (l=c)->next )
136                if( c == chat )
137                        break;
138       
139        if( c == NULL )
140                return 0;
141        else if( l == NULL )
142                irc->chatrooms = c->next;
143        else
144                l->next = c->next;
145       
146        while( c->set )
147                set_del( &c->set, c->set->key );
148       
149        g_free( c->handle );
150        g_free( c->channel );
151        g_free( c );
152       
153        return 1;
154}
155
[ad9feec]156int chat_chancmp( char *a, char *b )
157{
158        if( !chat_chanok( a ) || !chat_chanok( b ) )
159                return 0;
160       
161        if( a[0] == b[0] )
162                return nick_cmp( a + 1, b + 1 );
163        else
164                return -1;
165}
166
167int chat_chanok( char *a )
168{
[39f93f0]169        if( strchr( CTYPES, a[0] ) != NULL )
[ad9feec]170                return nick_ok( a + 1 );
171        else
172                return 0;
173}
[3611717]174
[94acdd0]175int chat_join( irc_t *irc, struct chat *c, const char *password )
[3611717]176{
177        struct groupchat *gc;
178        char *nick = set_getstr( &c->set, "nick" );
179       
180        if( nick == NULL )
181                nick = irc->nick;
182       
[549545b]183        if( c->acc->prpl->chat_join &&
184            ( gc = c->acc->prpl->chat_join( c->acc->ic, c->handle, nick, password ) ) )
[3611717]185        {
186                g_free( gc->channel );
187                gc->channel = g_strdup( c->channel );
188                return 1;
189        }
190       
191        return 0;
192}
Note: See TracBrowser for help on using the repository browser.