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 stuff that doesn't belong anywhere else. */ |
---|
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 | |
---|
28 | char *set_eval_timezone( set_t *set, char *value ) |
---|
29 | { |
---|
30 | char *s; |
---|
31 | |
---|
32 | if( strcmp( value, "local" ) == 0 || |
---|
33 | strcmp( value, "gmt" ) == 0 || strcmp( value, "utc" ) == 0 ) |
---|
34 | return value; |
---|
35 | |
---|
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 ++; |
---|
42 | |
---|
43 | /* \d+ */ |
---|
44 | if( !isdigit( *s ) ) |
---|
45 | return SET_INVALID; |
---|
46 | while( *s && isdigit( *s ) ) s ++; |
---|
47 | |
---|
48 | /* EOS? */ |
---|
49 | if( *s == '\0' ) |
---|
50 | return value; |
---|
51 | |
---|
52 | /* Otherwise, colon */ |
---|
53 | if( *s != ':' ) |
---|
54 | return SET_INVALID; |
---|
55 | s ++; |
---|
56 | |
---|
57 | /* \d+ */ |
---|
58 | if( !isdigit( *s ) ) |
---|
59 | return SET_INVALID; |
---|
60 | while( *s && isdigit( *s ) ) s ++; |
---|
61 | |
---|
62 | /* EOS */ |
---|
63 | return *s == '\0' ? value : SET_INVALID; |
---|
64 | } |
---|
65 | |
---|
66 | char *irc_format_timestamp( irc_t *irc, time_t msg_ts ) |
---|
67 | { |
---|
68 | time_t now_ts = time( NULL ); |
---|
69 | struct tm now, msg; |
---|
70 | char *set; |
---|
71 | |
---|
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; |
---|
76 | |
---|
77 | set = set_getstr( &irc->b->set, "timezone" ); |
---|
78 | if( strcmp( set, "local" ) == 0 ) |
---|
79 | { |
---|
80 | localtime_r( &now_ts, &now ); |
---|
81 | localtime_r( &msg_ts, &msg ); |
---|
82 | } |
---|
83 | else |
---|
84 | { |
---|
85 | int hr, min = 0, sign = 60; |
---|
86 | |
---|
87 | if( set[0] == '-' ) |
---|
88 | { |
---|
89 | sign *= -1; |
---|
90 | set ++; |
---|
91 | } |
---|
92 | else if( set[0] == '+' ) |
---|
93 | { |
---|
94 | set ++; |
---|
95 | } |
---|
96 | |
---|
97 | if( sscanf( set, "%d:%d", &hr, &min ) >= 1 ) |
---|
98 | { |
---|
99 | msg_ts += sign * ( hr * 60 + min ); |
---|
100 | now_ts += sign * ( hr * 60 + min ); |
---|
101 | } |
---|
102 | |
---|
103 | gmtime_r( &now_ts, &now ); |
---|
104 | gmtime_r( &msg_ts, &msg ); |
---|
105 | } |
---|
106 | |
---|
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 ); |
---|
115 | } |
---|