1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2010 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* Some glue to put the IRC and the IM stuff together. */ |
---|
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 "dcc.h" |
---|
28 | |
---|
29 | /* IM->IRC callbacks */ |
---|
30 | |
---|
31 | static const struct irc_user_funcs irc_user_im_funcs; |
---|
32 | |
---|
33 | static gboolean bee_irc_user_new( bee_t *bee, bee_user_t *bu ) |
---|
34 | { |
---|
35 | irc_user_t *iu; |
---|
36 | char nick[MAX_NICK_LENGTH+1], *s; |
---|
37 | |
---|
38 | memset( nick, 0, MAX_NICK_LENGTH + 1 ); |
---|
39 | strcpy( nick, nick_get( bu->ic->acc, bu->handle ) ); |
---|
40 | |
---|
41 | bu->ui_data = iu = irc_user_new( (irc_t*) bee->ui_data, nick ); |
---|
42 | iu->bu = bu; |
---|
43 | |
---|
44 | if( ( s = strchr( bu->handle, '@' ) ) ) |
---|
45 | { |
---|
46 | iu->host = g_strdup( s + 1 ); |
---|
47 | iu->user = g_strndup( bu->handle, s - bu->handle ); |
---|
48 | } |
---|
49 | else if( bu->ic->acc->server ) |
---|
50 | { |
---|
51 | iu->host = g_strdup( bu->ic->acc->server ); |
---|
52 | iu->user = g_strdup( bu->handle ); |
---|
53 | |
---|
54 | /* s/ /_/ ... important for AOL screennames */ |
---|
55 | for( s = iu->user; *s; s ++ ) |
---|
56 | if( *s == ' ' ) |
---|
57 | *s = '_'; |
---|
58 | } |
---|
59 | else |
---|
60 | { |
---|
61 | iu->host = g_strdup( bu->ic->acc->prpl->name ); |
---|
62 | iu->user = g_strdup( bu->handle ); |
---|
63 | } |
---|
64 | |
---|
65 | if( set_getbool( &bee->set, "private" ) ) |
---|
66 | iu->flags |= IRC_USER_PRIVATE; |
---|
67 | |
---|
68 | iu->f = &irc_user_im_funcs; |
---|
69 | //iu->last_typing_notice = 0; |
---|
70 | |
---|
71 | return TRUE; |
---|
72 | } |
---|
73 | |
---|
74 | static gboolean bee_irc_user_free( bee_t *bee, bee_user_t *bu ) |
---|
75 | { |
---|
76 | return irc_user_free( bee->ui_data, (irc_user_t *) bu->ui_data ); |
---|
77 | } |
---|
78 | |
---|
79 | static gboolean bee_irc_user_status( bee_t *bee, bee_user_t *bu, bee_user_t *old ) |
---|
80 | { |
---|
81 | irc_t *irc = bee->ui_data; |
---|
82 | irc_user_t *iu = bu->ui_data; |
---|
83 | irc_channel_t *ic = irc->channels->data; /* For now, just pick the first channel. */ |
---|
84 | |
---|
85 | if( ( bu->flags & BEE_USER_ONLINE ) != ( old->flags & BEE_USER_ONLINE ) ) |
---|
86 | { |
---|
87 | if( bu->flags & BEE_USER_ONLINE ) |
---|
88 | { |
---|
89 | if( g_hash_table_lookup( irc->watches, iu->key ) ) |
---|
90 | irc_send_num( irc, 600, "%s %s %s %d :%s", iu->nick, iu->user, |
---|
91 | iu->host, (int) time( NULL ), "logged online" ); |
---|
92 | |
---|
93 | irc_channel_add_user( ic, iu ); |
---|
94 | } |
---|
95 | else |
---|
96 | { |
---|
97 | if( g_hash_table_lookup( irc->watches, iu->key ) ) |
---|
98 | irc_send_num( irc, 601, "%s %s %s %d :%s", iu->nick, iu->user, |
---|
99 | iu->host, (int) time( NULL ), "logged offline" ); |
---|
100 | |
---|
101 | irc_channel_del_user( ic, iu ); |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | return TRUE; |
---|
106 | } |
---|
107 | |
---|
108 | static gboolean bee_irc_user_msg( bee_t *bee, bee_user_t *bu, const char *msg, time_t sent_at ) |
---|
109 | { |
---|
110 | irc_t *irc = bee->ui_data; |
---|
111 | irc_channel_t *ic = irc->channels->data; |
---|
112 | irc_user_t *iu = (irc_user_t *) bu->ui_data; |
---|
113 | char *dst, *prefix = NULL; |
---|
114 | char *wrapped, *ts = NULL; |
---|
115 | |
---|
116 | if( sent_at > 0 && set_getbool( &irc->b->set, "display_timestamps" ) ) |
---|
117 | ts = irc_format_timestamp( irc, sent_at ); |
---|
118 | |
---|
119 | if( iu->flags & IRC_USER_PRIVATE ) |
---|
120 | { |
---|
121 | dst = irc->user->nick; |
---|
122 | prefix = ts; |
---|
123 | ts = NULL; |
---|
124 | } |
---|
125 | else |
---|
126 | { |
---|
127 | dst = ic->name; |
---|
128 | prefix = g_strdup_printf( "%s%s%s", irc->user->nick, set_getstr( &bee->set, "to_char" ), ts ); |
---|
129 | } |
---|
130 | |
---|
131 | wrapped = word_wrap( msg, 425 ); |
---|
132 | irc_send_msg( iu, "PRIVMSG", dst, wrapped, prefix ); |
---|
133 | |
---|
134 | g_free( wrapped ); |
---|
135 | g_free( prefix ); |
---|
136 | g_free( ts ); |
---|
137 | |
---|
138 | return TRUE; |
---|
139 | } |
---|
140 | |
---|
141 | static gboolean bee_irc_user_typing( bee_t *bee, bee_user_t *bu, uint32_t flags ) |
---|
142 | { |
---|
143 | irc_t *irc = (irc_t *) bee->ui_data; |
---|
144 | |
---|
145 | if( set_getbool( &bee->set, "typing_notice" ) ) |
---|
146 | irc_send_msg_f( (irc_user_t *) bu->ui_data, "PRIVMSG", irc->user->nick, |
---|
147 | "\001TYPING %d\001", ( flags >> 8 ) & 3 ); |
---|
148 | else |
---|
149 | return FALSE; |
---|
150 | |
---|
151 | return TRUE; |
---|
152 | } |
---|
153 | |
---|
154 | static gboolean bee_irc_user_fullname( bee_t *bee, bee_user_t *bu ) |
---|
155 | { |
---|
156 | irc_user_t *iu = (irc_user_t *) bu->ui_data; |
---|
157 | irc_t *irc = (irc_t *) bee->ui_data; |
---|
158 | char *s; |
---|
159 | |
---|
160 | if( iu->fullname != iu->nick ) |
---|
161 | g_free( iu->fullname ); |
---|
162 | iu->fullname = g_strdup( bu->fullname ); |
---|
163 | |
---|
164 | /* Strip newlines (unlikely, but IRC-unfriendly so they must go) |
---|
165 | TODO(wilmer): Do the same with away msgs again! */ |
---|
166 | for( s = iu->fullname; *s; s ++ ) |
---|
167 | if( isspace( *s ) ) *s = ' '; |
---|
168 | |
---|
169 | if( ( bu->ic->flags & OPT_LOGGED_IN ) && set_getbool( &bee->set, "display_namechanges" ) ) |
---|
170 | { |
---|
171 | char *msg = g_strdup_printf( "<< \002BitlBee\002 - Changed name to `%s' >>", iu->fullname ); |
---|
172 | irc_send_msg( iu, "NOTICE", irc->user->nick, msg, NULL ); |
---|
173 | } |
---|
174 | |
---|
175 | s = set_getstr( &bu->ic->acc->set, "nick_source" ); |
---|
176 | if( strcmp( s, "handle" ) != 0 ) |
---|
177 | { |
---|
178 | char *name = g_strdup( bu->fullname ); |
---|
179 | |
---|
180 | if( strcmp( s, "first_name" ) == 0 ) |
---|
181 | { |
---|
182 | int i; |
---|
183 | for( i = 0; name[i] && !isspace( name[i] ); i ++ ) {} |
---|
184 | name[i] = '\0'; |
---|
185 | } |
---|
186 | |
---|
187 | imcb_buddy_nick_hint( bu->ic, bu->handle, name ); |
---|
188 | |
---|
189 | g_free( name ); |
---|
190 | } |
---|
191 | |
---|
192 | return TRUE; |
---|
193 | } |
---|
194 | |
---|
195 | /* File transfers */ |
---|
196 | static file_transfer_t *bee_irc_ft_in_start( bee_t *bee, bee_user_t *bu, const char *file_name, size_t file_size ) |
---|
197 | { |
---|
198 | return dccs_send_start( bu->ic, (irc_user_t *) bu->ui_data, file_name, file_size ); |
---|
199 | } |
---|
200 | |
---|
201 | gboolean bee_irc_ft_out_start( struct im_connection *ic, file_transfer_t *ft ) |
---|
202 | { |
---|
203 | return dccs_recv_start( ft ); |
---|
204 | } |
---|
205 | |
---|
206 | void bee_irc_ft_close( struct im_connection *ic, file_transfer_t *ft ) |
---|
207 | { |
---|
208 | return dcc_close( ft ); |
---|
209 | } |
---|
210 | |
---|
211 | void bee_irc_ft_finished( struct im_connection *ic, file_transfer_t *file ) |
---|
212 | { |
---|
213 | dcc_file_transfer_t *df = file->priv; |
---|
214 | |
---|
215 | if( file->bytes_transferred >= file->file_size ) |
---|
216 | dcc_finish( file ); |
---|
217 | else |
---|
218 | df->proto_finished = TRUE; |
---|
219 | } |
---|
220 | |
---|
221 | const struct bee_ui_funcs irc_ui_funcs = { |
---|
222 | bee_irc_user_new, |
---|
223 | bee_irc_user_free, |
---|
224 | bee_irc_user_fullname, |
---|
225 | bee_irc_user_status, |
---|
226 | bee_irc_user_msg, |
---|
227 | bee_irc_user_typing, |
---|
228 | |
---|
229 | bee_irc_ft_in_start, |
---|
230 | bee_irc_ft_out_start, |
---|
231 | bee_irc_ft_close, |
---|
232 | bee_irc_ft_finished, |
---|
233 | }; |
---|
234 | |
---|
235 | |
---|
236 | /* IRC->IM calls */ |
---|
237 | |
---|
238 | static gboolean bee_irc_user_privmsg( irc_user_t *iu, const char *msg ) |
---|
239 | { |
---|
240 | if( iu->bu ) |
---|
241 | return bee_user_msg( iu->irc->b, iu->bu, msg, 0 ); |
---|
242 | else |
---|
243 | return FALSE; |
---|
244 | } |
---|
245 | |
---|
246 | static gboolean bee_irc_user_ctcp( irc_user_t *iu, char *const *ctcp ) |
---|
247 | { |
---|
248 | if( ctcp[1] && g_strcasecmp( ctcp[0], "DCC" ) == 0 |
---|
249 | && g_strcasecmp( ctcp[1], "SEND" ) == 0 ) |
---|
250 | { |
---|
251 | if( iu->bu && iu->bu->ic && iu->bu->ic->acc->prpl->transfer_request ) |
---|
252 | { |
---|
253 | file_transfer_t *ft = dcc_request( iu->bu->ic, ctcp ); |
---|
254 | if ( ft ) |
---|
255 | iu->bu->ic->acc->prpl->transfer_request( iu->bu->ic, ft, iu->bu->handle ); |
---|
256 | |
---|
257 | return TRUE; |
---|
258 | } |
---|
259 | } |
---|
260 | else if( g_strcasecmp( ctcp[0], "TYPING" ) == 0 ) |
---|
261 | { |
---|
262 | if( iu->bu && iu->bu->ic && iu->bu->ic->acc->prpl->send_typing && ctcp[1] ) |
---|
263 | { |
---|
264 | int st = ctcp[1][0]; |
---|
265 | if( st >= '0' && st <= '2' ) |
---|
266 | { |
---|
267 | st <<= 8; |
---|
268 | iu->bu->ic->acc->prpl->send_typing( iu->bu->ic, iu->bu->handle, st ); |
---|
269 | } |
---|
270 | |
---|
271 | return TRUE; |
---|
272 | } |
---|
273 | } |
---|
274 | |
---|
275 | return FALSE; |
---|
276 | } |
---|
277 | |
---|
278 | static const struct irc_user_funcs irc_user_im_funcs = { |
---|
279 | bee_irc_user_privmsg, |
---|
280 | bee_irc_user_ctcp, |
---|
281 | }; |
---|