Updated Changelog
[centerim/davrieb.git] / firetalk / aim.c
blob270b8773a5d96b8e6515c51fcf0ceafb64b24696
1 /*
2 aim.c - FireTalk generic AIM definitions
3 Copyright (C) 2000 Ian Gulliver
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of version 2 of the GNU General Public License as
7 published by the Free Software Foundation.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "firetalk-int.h"
20 #include "firetalk.h"
21 #include "aim.h"
23 #include <stdio.h>
24 #include <time.h>
25 #include <string.h>
27 char *aim_interpolate_variables(const char *const input, const char *const nickname) {
28 static char output[16384]; /* 2048 / 2 * 16 + 1 (max size with a string full of %n's, a 16-char nick and a null at the end) */
29 int o = 0,gotpercent = 0;
30 size_t nl,dl,tl,l,i;
31 char date[15],tim[15];
33 { /* build the date and time */
34 int hour;
35 int am = 1;
36 struct tm *t;
37 time_t b;
38 b = time(NULL);
39 t = localtime(&b);
40 if (t == NULL)
41 return NULL;
42 hour = t->tm_hour;
43 if (hour >= 12)
44 am = 0;
45 if (hour > 12)
46 hour -= 12;
47 if (hour == 0)
48 hour = 12;
49 snprintf(tim, sizeof(tim), "%d:%02d:%02d %s", hour, t->tm_min, t->tm_sec, (am == 1)?"AM":"PM");
50 snprintf(date, sizeof(date), "%d/%d/%d", t->tm_mon+1, t->tm_mday, t->tm_year+1900);
52 nl = strlen(nickname);
53 dl = strlen(date);
54 tl = strlen(tim);
55 l = strlen(input);
56 for (i = 0; i < l; i++)
57 switch (input[i]) {
58 case '%':
59 if (gotpercent == 1) {
60 gotpercent = 0;
61 output[o++] = '%';
62 output[o++] = '%';
63 } else
64 gotpercent = 1;
65 break;
66 case 'n':
67 if (gotpercent == 1) {
68 gotpercent = 0;
69 memcpy(&output[o],nickname,nl);
70 o += nl;
71 } else
72 output[o++] = 'n';
73 break;
74 case 'd':
75 if (gotpercent == 1) {
76 gotpercent = 0;
77 memcpy(&output[o],date,dl);
78 o += dl;
79 } else
80 output[o++] = 'd';
81 break;
82 case 't':
83 if (gotpercent == 1) {
84 gotpercent = 0;
85 memcpy(&output[o],tim,tl);
86 o += tl;
87 } else
88 output[o++] = 't';
89 break;
90 default:
91 if (gotpercent == 1) {
92 gotpercent = 0;
93 output[o++] = '%';
95 output[o++] = input[i];
97 output[o] = '\0';
98 return output;
101 const char *aim_normalize_room_name(const char *const name) {
102 static char newname[2048];
104 if (name == NULL)
105 return(NULL);
106 if (strchr(name+1, ':') != NULL)
107 return name;
108 if (strlen(name) >= (sizeof(newname)-2))
109 return(NULL);
111 strcpy(newname, "4:");
112 strcpy(newname+2, name);
114 return(newname);
117 #define STRNCMP(x,y) (strncmp((x), (y), sizeof(y)-1))
118 static char
119 *htmlclean(const char *str) {
120 static char
121 buf[2048];
122 int i, b = 0;
124 for (i = 0; (str[i] != 0) && (b < sizeof(buf)-1); i++)
125 if (STRNCMP(str+i, "&gt;") == 0) {
126 buf[b++] = '>';
127 i += sizeof("&gt;")-2;
128 } else if (STRNCMP(str+i, "&lt;") == 0) {
129 buf[b++] = '<';
130 i += sizeof("&lt;")-2;
131 } else if (STRNCMP(str+i, "&quot;") == 0) {
132 buf[b++] = '"';
133 i += sizeof("&quot;")-2;
134 } else if (STRNCMP(str+i, "&nbsp;") == 0) {
135 buf[b++] = ' ';
136 i += sizeof("&nbsp;")-2;
137 } else if (STRNCMP(str+i, "&amp;") == 0) {
138 buf[b++] = '&';
139 i += sizeof("&amp;")-2;
140 } else
141 buf[b++] = str[i];
142 buf[b] = 0;
144 return(buf);
146 #undef STRNCMP
148 #define ECT_TOKEN "<font ECT=\""
149 #define ECT_ENDING "\"></font>"
150 char *aim_handle_ect(void *conn, const char *const from,
151 char *message, const int reply) {
152 char *ectbegin, *ectend, *textbegin, *textend;
154 while ((ectbegin = strstr(message, ECT_TOKEN)) != NULL) {
155 textbegin = ectbegin+sizeof(ECT_TOKEN)-1;
157 if ((textend = strstr(textbegin, ECT_ENDING)) != NULL) {
158 char *arg;
160 *textend = 0;
161 ectend = textend+sizeof(ECT_ENDING)-1;
163 if ((arg = strchr(textbegin, ' ')) != NULL) {
164 *arg = '\0';
165 arg++;
166 if (reply == 1)
167 firetalk_callback_subcode_reply(conn, from, textbegin, htmlclean(arg));
168 else
169 firetalk_callback_subcode_request(conn, from, textbegin, htmlclean(arg));
170 } else {
171 if (reply == 1)
172 firetalk_callback_subcode_reply(conn, from, textbegin, NULL);
173 else
174 firetalk_callback_subcode_request(conn, from, textbegin, NULL);
176 memmove(ectbegin, ectend, strlen(ectend)+1);
177 } else
178 break;
180 return message;
183 const char *firetalk_nhtmlentities(const char *str, int len) {
184 static char buf[1024];
185 int i, b = 0;
187 for (i = 0; (str[i] != 0) && (b < sizeof(buf)-6-1) && ((len < 0) || (i < len)); i++)
188 switch (str[i]) {
189 case '<':
190 buf[b++] = '&';
191 buf[b++] = 'l';
192 buf[b++] = 't';
193 buf[b++] = ';';
194 break;
195 case '>':
196 buf[b++] = '&';
197 buf[b++] = 'g';
198 buf[b++] = 't';
199 buf[b++] = ';';
200 break;
201 case '&':
202 buf[b++] = '&';
203 buf[b++] = 'a';
204 buf[b++] = 'm';
205 buf[b++] = 'p';
206 buf[b++] = ';';
207 break;
208 case '"':
209 buf[b++] = '&';
210 buf[b++] = 'q';
211 buf[b++] = 'u';
212 buf[b++] = 'o';
213 buf[b++] = 't';
214 buf[b++] = ';';
215 break;
216 case '\n':
217 buf[b++] = '<';
218 buf[b++] = 'b';
219 buf[b++] = 'r';
220 buf[b++] = '>';
221 break;
222 default:
223 buf[b++] = str[i];
224 break;
226 buf[b] = 0;
227 return(buf);
230 const char *firetalk_htmlentities(const char *str) {
231 return(firetalk_nhtmlentities(str, -1));