source: irc_send.c @ 7db65b7

Last change on this file since 7db65b7 was f7ca587, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-07-29T18:18:54Z

Restore default_target setting, kill last_root_cmd variable and just use
the last_channel variable, like for any other user.

  • Property mode set to 100644
File size: 11.0 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2010 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* The IRC-based UI - Sending responses to commands/etc.                */
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
28void irc_send_num( irc_t *irc, int code, char *format, ... )
29{
30        char text[IRC_MAX_LINE];
31        va_list params;
32       
33        va_start( params, format );
34        g_vsnprintf( text, IRC_MAX_LINE, format, params );
35        va_end( params );
36       
37        irc_write( irc, ":%s %03d %s %s", irc->root->host, code, irc->user->nick ? : "*", text );
38}
39
40void irc_send_login( irc_t *irc )
41{
42        irc_send_num( irc,   1, ":Welcome to the BitlBee gateway, %s", irc->user->nick );
43        irc_send_num( irc,   2, ":Host %s is running BitlBee " BITLBEE_VERSION " " ARCH "/" CPU ".", irc->root->host );
44        irc_send_num( irc,   3, ":%s", IRCD_INFO );
45        irc_send_num( irc,   4, "%s %s %s %s", irc->root->host, BITLBEE_VERSION, UMODES UMODES_PRIV, CMODES );
46        irc_send_num( irc,   5, "PREFIX=(ov)@+ CHANTYPES=%s CHANMODES=,,,%s NICKLEN=%d NETWORK=BitlBee "
47                                "CASEMAPPING=rfc1459 MAXTARGETS=1 WATCH=128 :are supported by this server",
48                                CTYPES, CMODES, MAX_NICK_LENGTH - 1 );
49        irc_send_motd( irc );
50}
51
52void irc_send_motd( irc_t *irc )
53{
54        int fd;
55       
56        fd = open( global.conf->motdfile, O_RDONLY );
57        if( fd == -1 )
58        {
59                irc_send_num( irc, 422, ":We don't need MOTDs." );
60        }
61        else
62        {
63                char linebuf[80];       /* Max. line length for MOTD's is 79 chars. It's what most IRC networks seem to do. */
64                char *add, max;
65                int len;
66               
67                linebuf[79] = len = 0;
68                max = sizeof( linebuf ) - 1;
69               
70                irc_send_num( irc, 375, ":- %s Message Of The Day - ", irc->root->host );
71                while( read( fd, linebuf + len, 1 ) == 1 )
72                {
73                        if( linebuf[len] == '\n' || len == max )
74                        {
75                                linebuf[len] = 0;
76                                irc_send_num( irc, 372, ":- %s", linebuf );
77                                len = 0;
78                        }
79                        else if( linebuf[len] == '%' )
80                        {
81                                read( fd, linebuf + len, 1 );
82                                if( linebuf[len] == 'h' )
83                                        add = irc->root->host;
84                                else if( linebuf[len] == 'v' )
85                                        add = BITLBEE_VERSION;
86                                else if( linebuf[len] == 'n' )
87                                        add = irc->user->nick;
88                                else
89                                        add = "%";
90                               
91                                strncpy( linebuf + len, add, max - len );
92                                while( linebuf[++len] );
93                        }
94                        else if( len < max )
95                        {
96                                len ++;
97                        }
98                }
99                irc_send_num( irc, 376, ":End of MOTD" );
100                close( fd );
101        }
102}
103
104void irc_usermsg( irc_t *irc, char *format, ... )
105{
106        irc_channel_t *ic = NULL;
107        irc_user_t *iu = irc->root;
108        char text[1024];
109        va_list params;
110        char *dst;
111       
112        va_start( params, format );
113        g_vsnprintf( text, sizeof( text ), format, params );
114        va_end( params );
115       
116        /* Too similar to bee_irc_user_msg()... */
117        if( iu->last_channel )
118        {
119                if( iu->last_channel->flags & IRC_CHANNEL_JOINED )
120                        ic = iu->last_channel;
121                else if( irc->default_channel->flags & IRC_CHANNEL_JOINED )
122                        ic = irc->default_channel;
123        }
124       
125        if( ic )
126                dst = ic->name;
127        else
128                dst = irc->user->nick;
129       
130        irc_send_msg( irc->root, "PRIVMSG", dst, text, NULL );
131}
132
133void irc_send_join( irc_channel_t *ic, irc_user_t *iu )
134{
135        irc_t *irc = ic->irc;
136       
137        irc_write( irc, ":%s!%s@%s JOIN :%s", iu->nick, iu->user, iu->host, ic->name );
138       
139        if( iu == irc->user )
140        {
141                irc_write( irc, ":%s MODE %s +%s", irc->root->host, ic->name, ic->mode );
142                irc_send_names( ic );
143                if( ic->topic && *ic->topic )
144                        irc_send_topic( ic, FALSE );
145        }
146}
147
148void irc_send_part( irc_channel_t *ic, irc_user_t *iu, const char *reason )
149{
150        irc_write( ic->irc, ":%s!%s@%s PART %s :%s", iu->nick, iu->user, iu->host, ic->name, reason ? : "" );
151}
152
153void irc_send_quit( irc_user_t *iu, const char *reason )
154{
155        irc_write( iu->irc, ":%s!%s@%s QUIT :%s", iu->nick, iu->user, iu->host, reason ? : "" );
156}
157
158void irc_send_kick( irc_channel_t *ic, irc_user_t *iu, irc_user_t *kicker, const char *reason )
159{
160        irc_write( ic->irc, ":%s!%s@%s KICK %s %s :%s", kicker->nick, kicker->user,
161                   kicker->host, ic->name, iu->nick, reason ? : "" );
162}
163
164void irc_send_names( irc_channel_t *ic )
165{
166        GSList *l;
167        char namelist[385] = "";
168       
169        /* RFCs say there is no error reply allowed on NAMES, so when the
170           channel is invalid, just give an empty reply. */
171        for( l = ic->users; l; l = l->next )
172        {
173                irc_channel_user_t *icu = l->data;
174                irc_user_t *iu = icu->iu;
175               
176                if( strlen( namelist ) + strlen( iu->nick ) > sizeof( namelist ) - 4 )
177                {
178                        irc_send_num( ic->irc, 353, "= %s :%s", ic->name, namelist );
179                        *namelist = 0;
180                }
181               
182                if( icu->flags & IRC_CHANNEL_USER_OP )
183                        strcat( namelist, "@" );
184                else if( icu->flags & IRC_CHANNEL_USER_HALFOP )
185                        strcat( namelist, "%" );
186                else if( icu->flags & IRC_CHANNEL_USER_VOICE )
187                        strcat( namelist, "+" );
188               
189                strcat( namelist, iu->nick );
190                strcat( namelist, " " );
191        }
192       
193        if( *namelist )
194                irc_send_num( ic->irc, 353, "= %s :%s", ic->name, namelist );
195       
196        irc_send_num( ic->irc, 366, "%s :End of /NAMES list", ic->name );
197}
198
199void irc_send_topic( irc_channel_t *ic, gboolean topic_change )
200{
201        if( topic_change && ic->topic_who )
202        {
203                irc_write( ic->irc, ":%s TOPIC %s :%s", ic->topic_who, 
204                           ic->name, ic->topic && *ic->topic ? ic->topic : "" );
205        }
206        else if( ic->topic )
207        {
208                irc_send_num( ic->irc, 332, "%s :%s", ic->name, ic->topic );
209                if( ic->topic_who )
210                        irc_send_num( ic->irc, 333, "%s %s %d",
211                                      ic->name, ic->topic_who, (int) ic->topic_time );
212        }
213        else
214                irc_send_num( ic->irc, 331, "%s :No topic for this channel", ic->name );
215}
216
217void irc_send_whois( irc_user_t *iu )
218{
219        irc_t *irc = iu->irc;
220       
221        irc_send_num( irc, 311, "%s %s %s * :%s",
222                      iu->nick, iu->user, iu->host, iu->fullname );
223       
224        if( iu->bu )
225        {
226                bee_user_t *bu = iu->bu;
227               
228                irc_send_num( irc, 312, "%s %s.%s :%s network", iu->nick, bu->ic->acc->user,
229                           bu->ic->acc->server && *bu->ic->acc->server ? bu->ic->acc->server : "",
230                           bu->ic->acc->prpl->name );
231               
232                if( ( bu->status && *bu->status ) ||
233                    ( bu->status_msg && *bu->status_msg ) )
234                {
235                        int num = bu->flags & BEE_USER_AWAY ? 301 : 320;
236                       
237                        if( bu->status && bu->status_msg )
238                                irc_send_num( irc, num, "%s :%s (%s)", iu->nick, bu->status, bu->status_msg );
239                        else
240                                irc_send_num( irc, num, "%s :%s", iu->nick, bu->status ? : bu->status_msg );
241                }
242                else if( !( bu->flags & BEE_USER_ONLINE ) )
243                {
244                        irc_send_num( irc, 301, "%s :%s", iu->nick, "User is offline" );
245                }
246               
247                if( bu->idle_time || bu->login_time )
248                {
249                        irc_send_num( irc, 317, "%s %d %d :seconds idle, signon time",
250                                      iu->nick,
251                                      bu->idle_time ? (int) ( time( NULL ) - bu->idle_time ) : 0,
252                                      (int) bu->login_time );
253                }
254        }
255        else
256        {
257                irc_send_num( irc, 312, "%s %s :%s", iu->nick, irc->root->host, IRCD_INFO );
258        }
259       
260        irc_send_num( irc, 318, "%s :End of /WHOIS list", iu->nick );
261}
262
263void irc_send_who( irc_t *irc, GSList *l, const char *channel )
264{
265        gboolean is_channel = strcmp( channel, "**" ) != 0;
266       
267        while( l )
268        {
269                irc_user_t *iu = l->data;
270                if( is_channel )
271                        iu = ((irc_channel_user_t*)iu)->iu;
272                /* TODO(wilmer): Restore away/channel information here */
273                irc_send_num( irc, 352, "%s %s %s %s %s %c :0 %s",
274                              channel ? : "*", iu->user, iu->host, irc->root->host,
275                              iu->nick, iu->flags & IRC_USER_AWAY ? 'G' : 'H',
276                              iu->fullname );
277                l = l->next;
278        }
279       
280        irc_send_num( irc, 315, "%s :End of /WHO list", channel );
281}
282
283void irc_send_msg( irc_user_t *iu, const char *type, const char *dst, const char *msg, const char *prefix )
284{
285        char last = 0;
286        const char *s = msg, *line = msg;
287        char raw_msg[strlen(msg)+1024];
288       
289        while( !last )
290        {
291                if( *s == '\r' && *(s+1) == '\n' )
292                        s++;
293                if( *s == '\n' )
294                {
295                        last = s[1] == 0;
296                }
297                else
298                {
299                        last = s[0] == 0;
300                }
301                if( *s == 0 || *s == '\n' )
302                {
303                        if( g_strncasecmp( line, "/me ", 4 ) == 0 && ( !prefix || !*prefix ) &&
304                            g_strcasecmp( type, "PRIVMSG" ) == 0 )
305                        {
306                                strcpy( raw_msg, "\001ACTION " );
307                                strncat( raw_msg, line + 4, s - line - 4 );
308                                strcat( raw_msg, "\001" );
309                                irc_send_msg_raw( iu, type, dst, raw_msg );
310                        }
311                        else
312                        {
313                                *raw_msg = '\0';
314                                if( prefix && *prefix )
315                                        strcpy( raw_msg, prefix );
316                                strncat( raw_msg, line, s - line );
317                                irc_send_msg_raw( iu, type, dst, raw_msg );
318                        }
319                        line = s + 1;
320                }
321                s ++;
322        }
323}
324
325void irc_send_msg_raw( irc_user_t *iu, const char *type, const char *dst, const char *msg )
326{
327        irc_write( iu->irc, ":%s!%s@%s %s %s :%s",
328                   iu->nick, iu->user, iu->host, type, dst, msg );
329}
330
331void irc_send_msg_f( irc_user_t *iu, const char *type, const char *dst, const char *format, ... )
332{
333        char text[IRC_MAX_LINE];
334        va_list params;
335       
336        va_start( params, format );
337        g_vsnprintf( text, IRC_MAX_LINE, format, params );
338        va_end( params );
339       
340        irc_write( iu->irc, ":%s!%s@%s %s %s :%s",
341                   iu->nick, iu->user, iu->host, type, dst, text );
342}
343
344void irc_send_nick( irc_user_t *iu, const char *new )
345{
346        irc_write( iu->irc, ":%s!%s@%s NICK %s",
347                   iu->nick, iu->user, iu->host, new );
348}
349
350/* Send an update of a user's mode inside a channel, compared to what it was. */
351void irc_send_channel_user_mode_diff( irc_channel_t *ic, irc_user_t *iu,
352        irc_channel_user_flags_t old, irc_channel_user_flags_t new )
353{
354        char changes[3*(5+strlen(iu->nick))];
355        char from[strlen(ic->irc->root->nick)+strlen(ic->irc->root->user)+strlen(ic->irc->root->host)+3];
356        int n;
357       
358        *changes = '\0'; n = 0;
359        if( ( old & IRC_CHANNEL_USER_OP ) != ( new & IRC_CHANNEL_USER_OP ) )
360        {
361                n ++;
362                if( new & IRC_CHANNEL_USER_OP )
363                        strcat( changes, "+o" );
364                else
365                        strcat( changes, "-o" );
366        }
367        if( ( old & IRC_CHANNEL_USER_HALFOP ) != ( new & IRC_CHANNEL_USER_HALFOP ) )
368        {
369                n ++;
370                if( new & IRC_CHANNEL_USER_HALFOP )
371                        strcat( changes, "+h" );
372                else
373                        strcat( changes, "-h" );
374        }
375        if( ( old & IRC_CHANNEL_USER_VOICE ) != ( new & IRC_CHANNEL_USER_VOICE ) )
376        {
377                n ++;
378                if( new & IRC_CHANNEL_USER_VOICE )
379                        strcat( changes, "+v" );
380                else
381                        strcat( changes, "-v" );
382        }
383        while( n )
384        {
385                strcat( changes, " " );
386                strcat( changes, iu->nick );
387                n --;
388        }
389       
390        if( set_getbool( &ic->irc->b->set, "simulate_netsplit" ) )
391                g_snprintf( from, sizeof( from ), "%s", ic->irc->root->host );
392        else
393                g_snprintf( from, sizeof( from ), "%s!%s@%s", ic->irc->root->nick,
394                            ic->irc->root->user, ic->irc->root->host );
395       
396        if( *changes )
397                irc_write( ic->irc, ":%s MODE %s %s", from, ic->name, changes );
398}
Note: See TracBrowser for help on using the repository browser.