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