Scan media entities as well, not just url entities. This should expand more
[bitlbee.git] / irc_util.c
blobf664a835b8f3289f70a7a2441341ee85b68b0f08
1 /********************************************************************\
2 * BitlBee -- An IRC to other IM-networks gateway *
3 * *
4 * Copyright 2002-2010 Wilmer van der Gaast and others *
5 \********************************************************************/
7 /* Some stuff that doesn't belong anywhere else. */
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.
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.
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
26 #include "bitlbee.h"
28 char *set_eval_timezone( set_t *set, char *value )
30 char *s;
32 if( strcmp( value, "local" ) == 0 ||
33 strcmp( value, "gmt" ) == 0 || strcmp( value, "utc" ) == 0 )
34 return value;
36 /* Otherwise: +/- at the beginning optional, then one or more numbers,
37 possibly followed by a colon and more numbers. Don't bother bound-
38 checking them since users are free to shoot themselves in the foot. */
39 s = value;
40 if( *s == '+' || *s == '-' )
41 s ++;
43 /* \d+ */
44 if( !isdigit( *s ) )
45 return SET_INVALID;
46 while( *s && isdigit( *s ) ) s ++;
48 /* EOS? */
49 if( *s == '\0' )
50 return value;
52 /* Otherwise, colon */
53 if( *s != ':' )
54 return SET_INVALID;
55 s ++;
57 /* \d+ */
58 if( !isdigit( *s ) )
59 return SET_INVALID;
60 while( *s && isdigit( *s ) ) s ++;
62 /* EOS */
63 return *s == '\0' ? value : SET_INVALID;
66 char *irc_format_timestamp( irc_t *irc, time_t msg_ts )
68 time_t now_ts = time( NULL );
69 struct tm now, msg;
70 char *set;
72 /* If the timestamp is <= 0 or less than a minute ago, discard it as
73 it doesn't seem to add to much useful info and/or might be noise. */
74 if( msg_ts <= 0 || msg_ts > now_ts - 60 )
75 return NULL;
77 set = set_getstr( &irc->b->set, "timezone" );
78 if( strcmp( set, "local" ) == 0 )
80 localtime_r( &now_ts, &now );
81 localtime_r( &msg_ts, &msg );
83 else
85 int hr, min = 0, sign = 60;
87 if( set[0] == '-' )
89 sign *= -1;
90 set ++;
92 else if( set[0] == '+' )
94 set ++;
97 if( sscanf( set, "%d:%d", &hr, &min ) >= 1 )
99 msg_ts += sign * ( hr * 60 + min );
100 now_ts += sign * ( hr * 60 + min );
103 gmtime_r( &now_ts, &now );
104 gmtime_r( &msg_ts, &msg );
107 if( msg.tm_year == now.tm_year && msg.tm_yday == now.tm_yday )
108 return g_strdup_printf( "\x02[\x02\x02\x02%02d:%02d:%02d\x02]\x02 ",
109 msg.tm_hour, msg.tm_min, msg.tm_sec );
110 else
111 return g_strdup_printf( "\x02[\x02\x02\x02%04d-%02d-%02d "
112 "%02d:%02d:%02d\x02]\x02 ",
113 msg.tm_year + 1900, msg.tm_mon + 1, msg.tm_mday,
114 msg.tm_hour, msg.tm_min, msg.tm_sec );