summaryrefslogtreecommitdiffstats
path: root/external/rcclibtranslate.c
blob: fb83222756944e3c72aa4718b20c31c6c10a09d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
  LibRCC - external module interfacying libtranslate library

  Copyright (C) 2005-2008 Suren A. Chilingaryan <csa@dside.dyndns.org>

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License version 2.1 or later
  as published by the Free Software Foundation.

  This library is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License 
  for more details.

  You should have received a copy of the GNU Lesser General Public License 
  along with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#include "../config.h"

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif /* HAVE_SYS_TYPES_H */
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif /* HAVE_SYS_STAT_H */

#ifdef HAVE_LIBTRANSLATE
#include <translate.h>
#endif /* HAVE_LIBTRANSLATE */

#include "../src/rccexternal.h"
#include "../src/rcctranslate.h"
#include "../src/rccdb4.h"
#include "rcclibtranslate.h"
#include "compat.h"

#ifdef HAVE_LIBTRANSLATE
static TranslateSession *session = NULL;
static db4_context db4ctx = NULL;

static int exitflag = 0;
static GMutex *mutex = NULL;
static GCond *cond = NULL;
static GQueue *queue = NULL;
static GThread *thread = NULL;

extern char rcc_external_offline;

static char *rccCreateKey(const char *from, const char *to, const char *data, size_t *keysize) {
    char *res;

    *keysize = strlen(data) + 4;
    res = (char*)malloc((*keysize+1)*sizeof(char));
    if (res) sprintf(res,"%s%s%s",from,to,data);
    return res;
}

static char *rccTranslateFixEOL(char *result, const char *text) {
    size_t i,j;
    char *res;
    
    if (!result) return result;
    if (strstr(text, "\r\n")) return result;
    
    res = (char*)malloc((strlen(result)+1)*sizeof(char));
    if (!res) {
	free(result);
	return NULL;
    }
    
    for (i=0, j=0;result[i];i++) {
	if ((result[i]=='\r')&&(result[i+1]=='\n')) i++;
	else res[j++] = result[i];
    }
    res[j] = 0;
    free(result);
    return res;
}

static void *rccLibPostponed(void *info) {
    char *result;
    char *data;
    char from[3];
    char to[3];
    size_t datalen;
    
    from[2] = 0;
    to[2] = 0;
    
    g_mutex_lock(mutex);
    while ((!exitflag)||(rcc_external_offline)) {
	data = (char*)g_queue_pop_head(queue);
	if (data) {
	    g_mutex_unlock(mutex);
	    
	    datalen = strlen(data);
	    
	    memcpy(from, data, 2);
	    memcpy(to, data + 2, 2);

	    result = rccDb4GetKey(db4ctx, data, datalen);
	    if (result) free(result);
	    else {
		result = translate_session_translate_text(session, data + 4, from, to, NULL, NULL, NULL);

		if (result) {
		    result = rccTranslateFixEOL(result, data+4);
		    rccDb4SetKey(db4ctx, data, datalen, result);
		    free(result);
		}
	    }

	    free(data);
	    g_mutex_lock(mutex);
	} else {
	    if (exitflag) break;
	    g_cond_wait(cond, mutex);
	}
    }
    g_mutex_unlock(mutex);
    return NULL;
}

#endif /* HAVE_LIBTRANSLATE */


int rccLibTranslateInit(const char *rcc_home_dir) {
#ifdef HAVE_LIBTRANSLATE
    size_t size;
    char *dbname;
    GSList *list = NULL;

    const char *http_proxy;

    if (!translate_init(NULL)) return -1;
    
    http_proxy = getenv("HTTP_PROXY");
    if (!http_proxy) http_proxy = getenv("http_proxy");
    translate_set_proxy(http_proxy);

    list = translate_get_services();

    session = translate_session_new(list);

    g_slist_foreach(list, (GFunc) g_object_unref, NULL);
    g_slist_free(list);
    

    size = strlen(rcc_home_dir) + 32;
    dbname = (char*)malloc(size*sizeof(char));
    if (dbname) {
        sprintf(dbname,"%s/.rcc/",rcc_home_dir);
	mkdir(dbname, 00755);
    
	sprintf(dbname,"%s/.rcc/libtranslate.db/",rcc_home_dir);
	mkdir(dbname, 00755);

	db4ctx = rccDb4CreateContext(dbname, 0);
	free(dbname);
    }
    if (db4ctx) {
	mutex = g_mutex_new_compat();
	cond = g_cond_new_compat();
	if ((mutex)&&(cond))
	    queue = g_queue_new();
	if (queue)
	    thread = g_thread_create_compat(rccLibPostponed, NULL, TRUE);
    }
#endif /* HAVE_LIBTRANSLATE */

    return 0;
}

void rccLibTranslateFree() {
#ifdef HAVE_LIBTRANSLATE
    char *ptr;
    if  (session) {
	if (thread) {
	    exitflag = 1;
	    g_mutex_lock(mutex);
	    g_cond_signal(cond);
	    g_mutex_unlock(mutex);
	    g_thread_join(thread);
	    thread = NULL;
	    exitflag = 0;
	}
	if (queue) {
	    do {
		ptr = g_queue_pop_head(queue);
		if (ptr) free(ptr);
	    } while (ptr);
	    g_queue_free(queue);
	    queue = NULL;
	}
	if (mutex) {
	    g_mutex_free_compat(mutex);
	    mutex = NULL;
	}
	if (cond) {
	    g_cond_free_compat(cond);
	    cond = NULL;
	}
	if (db4ctx) rccDb4FreeContext(db4ctx);
	g_object_unref(session);
	session = NULL;
    }
#endif /* HAVE_LIBTRANSLATE */
}


static void rccLibTranslateQueue(const char *from, const char *to, const char *text) {
#ifdef HAVE_LIBTRANSLATE
    char *key = NULL;
    size_t keysize;
    
    if ((!session)||(!from)||(!to)||(!text)) return;
    if ((strlen(from)!=2)||(strlen(to)!=2)) return;

    if (db4ctx) {
	key = rccCreateKey(from,to,text,&keysize);
	if (key) {
	    g_mutex_lock(mutex);
	    g_queue_push_tail(queue, key);
	    g_mutex_unlock(mutex);
	    g_cond_signal(cond);
	}
    }
#endif /* HAVE_LIBTRANSLATE */
}

static char *rccLibTranslateDo(const char *from, const char *to, const char *text, unsigned long timeout) {
#ifdef HAVE_LIBTRANSLATE
    char *result;
    char *key = NULL;
    size_t keysize;
    
    if ((!session)||(!from)||(!to)||(!text)) return NULL;
    if ((strlen(from)!=2)||(strlen(to)!=2)) return NULL;

    if (db4ctx) {
	key = rccCreateKey(from,to,text,&keysize);
	if (key) {
	    result = rccDb4GetKey(db4ctx, key, keysize);
	    if (result) {
		free(key);
		return result;
	    }
	}
    }
# ifdef HAVE_LIBTRANSLATE_TIMED_TRANSLATE    
    result = translate_session_timed_translate_text(session, text, from, to, timeout, NULL, NULL, NULL);
# else
    result = translate_session_translate_text(session, text, from, to, NULL, NULL, NULL);
# endif /* HAVE_LIBTRANSLATE_TIMED_TRANSLATE */

    result = rccTranslateFixEOL(result, text);
    
    if ((db4ctx)&&(key)) {
	if (result) {
	    rccDb4SetKey(db4ctx, key, keysize, result);
	    free(key);
	}
	else {
	    g_mutex_lock(mutex);
	    g_queue_push_tail(queue, key);
	    g_mutex_unlock(mutex);
	    g_cond_signal(cond);
	}
    }

    return result;
#else
    return NULL;
#endif /* HAVE_LIBTRANSLATE */
}


void *rccLibTranslate(void *info) {
    int s;
#ifdef HAVE_LIBTRANSLATE
    unsigned char connected = 1;
    rcc_translate_prefix_s prefix;
    ssize_t readed, writed, res;
    char *buffer;
    size_t size;
    char *translated = NULL;
#endif /* HAVE_LIBTRANSLATE */

    if (!info) return NULL;
    
    s = ((rcc_external_info)info)->s;
    free(info);
    
#ifdef HAVE_LIBTRANSLATE
    while (connected) {
	readed = read(s, &prefix, sizeof(rcc_translate_prefix_s)-1);
	if ((readed<=0)||(readed != (sizeof(rcc_translate_prefix_s)-1))) break;

	switch (prefix.cmd.cmd) {
	    case RCC_EXTERNAL_COMMAND_CLOSE:
		connected = 0;
	    break;
	    case RCC_EXTERNAL_COMMAND_TRANSLATE:
		size = 1 + prefix.cmd.size + sizeof(rcc_external_command_s) - sizeof(rcc_translate_prefix_s);
		buffer = (char*)malloc(size);
		if (buffer) {
		    for (readed = 0; (readed < size)&&(connected); readed += res) {
			res = read(s, buffer + readed, size - readed);
			if (res<=0) connected = 0;
		    }
		    if (!connected)  goto clear;
		    
		    prefix.cmd.cmd = 0;
		    prefix.cmd.size = 0;
		    
		    if ((prefix.from[2])||(prefix.to[2])||(buffer[readed-1])) goto respond;
		    
		    translated = rccLibTranslateDo(prefix.from, prefix.to, buffer, prefix.timeout);
		    if (translated) {
			prefix.cmd.cmd = RCC_EXTERNAL_COMMAND_TRANSLATE;
			prefix.cmd.size = strlen(translated) + 1;
		    }
		    
respond:
		    res = write(s, &prefix.cmd, sizeof(rcc_external_command_s));
		    if (res == sizeof(rcc_external_command_s)) {
			for (writed = 0; (writed < prefix.cmd.size)&&(connected); writed += res) {
			    res = write(s, translated + writed, prefix.cmd.size - writed);
			    if (res<=0) connected = 0;
			}
		    } else connected = 0;
		    
		    if (prefix.cmd.size) free(translated);		    
clear:
		    free(buffer);
		} else connected = 0;
	    break;
	    case RCC_EXTERNAL_COMMAND_TRANSLATE_QUEUE:
		size = 1 + prefix.cmd.size + sizeof(rcc_external_command_s) - sizeof(rcc_translate_prefix_s);
		buffer = (char*)malloc(size);
		if (buffer) {
		    for (readed = 0; (readed < size)&&(connected); readed += res) {
			res = read(s, buffer + readed, size - readed);
			if (res<=0) connected = 0;
		    }
		    if ((connected)&&(!prefix.from[2])&&(!prefix.to[2])&&(!buffer[readed-1])) {
			rccLibTranslateQueue(prefix.from, prefix.to, buffer);
		    }
		    free(buffer);
		} else connected = 0;
	    break;
	    default:
		size = 1 + prefix.cmd.size + sizeof(rcc_external_command_s) - sizeof(rcc_translate_prefix_s);
		buffer = (char*)malloc(size);
		if (buffer) {
		    for (readed = 0; (readed < size)&&(connected); readed += res) {
			res = read(s, buffer + readed, size - readed);
			if (res<=0) connected = 0;
		    }
		    free(buffer);
		} else connected = 0;
	}
    }    
#endif /* HAVE_LIBTRANSLATE */
    
    close(s);
    return NULL;
}