source: irc_send.c @ aea22cd

Last change on this file since aea22cd was e67e513, checked in by unknown <pesco@…>, at 2011-10-03T14:56:58Z

rename irc_usermsg to irc_rootmsg.
add new irc_usermsg, irc_usernotice.
deliver user-specific messages from libotr as notices to that user.

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