2.10.0 unleashed
[claws.git] / src / pine.c
blobe60db2c0687f8d2a0bc652f7749928476b3832f9
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2007 Match Grun and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * Functions necessary to access Pine address book file.
24 #include <sys/stat.h>
25 #include <glib.h>
26 #include <string.h>
28 #include "utils.h"
29 #include "mgutils.h"
30 #include "pine.h"
31 #include "addritem.h"
32 #include "addrcache.h"
34 #define PINE_HOME_FILE ".addressbook"
35 #define PINEBUFSIZE 2048
36 #define CHAR_QUOTE '\"'
37 #define CHAR_APOS '\''
38 #define CHAR_COMMA ','
39 #define CHAR_AT '@'
42 * Create new object.
44 PineFile *pine_create() {
45 PineFile *pineFile;
46 pineFile = g_new0( PineFile, 1 );
47 pineFile->path = NULL;
48 pineFile->file = NULL;
49 pineFile->retVal = MGU_SUCCESS;
50 pineFile->uniqTable = g_hash_table_new( g_str_hash, g_str_equal );
51 pineFile->cbProgress = NULL;
52 return pineFile;
56 * Properties...
58 void pine_set_file( PineFile* pineFile, const gchar *value ) {
59 g_return_if_fail( pineFile != NULL );
60 pineFile->path = mgu_replace_string( pineFile->path, value );
61 g_strstrip( pineFile->path );
65 * Free key in table.
67 static gint pine_free_table_vis( gpointer key, gpointer value, gpointer data ) {
68 g_free( key );
69 key = NULL;
70 value = NULL;
71 return TRUE;
75 * Free up object by releasing internal memory.
77 void pine_free( PineFile *pineFile ) {
78 g_return_if_fail( pineFile != NULL );
80 /* Close file */
81 if( pineFile->file ) fclose( pineFile->file );
83 /* Free internal stuff */
84 g_free( pineFile->path );
86 /* Free unique address table */
87 g_hash_table_foreach_remove( pineFile->uniqTable, pine_free_table_vis, NULL );
88 g_hash_table_destroy( pineFile->uniqTable );
90 /* Clear pointers */
91 pineFile->file = NULL;
92 pineFile->path = NULL;
93 pineFile->retVal = MGU_SUCCESS;
94 pineFile->uniqTable = NULL;
95 pineFile->cbProgress = NULL;
97 /* Now release file object */
98 g_free( pineFile );
102 * Open file for read.
103 * Enter: pineFile File object.
104 * return: TRUE if file opened successfully.
106 static gint pine_open_file( PineFile* pineFile ) {
107 if( pineFile->path ) {
108 pineFile->file = g_fopen( pineFile->path, "rb" );
109 if( ! pineFile->file ) {
110 pineFile->retVal = MGU_OPEN_FILE;
111 return pineFile->retVal;
114 else {
115 /* printf( "file not specified\n" ); */
116 pineFile->retVal = MGU_NO_FILE;
117 return pineFile->retVal;
120 /* Setup a buffer area */
121 pineFile->retVal = MGU_SUCCESS;
122 return pineFile->retVal;
126 * Close file.
127 * Enter: pineFile File object.
129 static void pine_close_file( PineFile *pineFile ) {
130 g_return_if_fail( pineFile != NULL );
131 if( pineFile->file ) fclose( pineFile->file );
132 pineFile->file = NULL;
136 * Read line of text from file.
137 * Enter: pineFile File object.
138 * Return: Copy of buffer. Should be g_free'd when done.
140 static gchar *pine_read_line( PineFile *pineFile ) {
141 gchar buf[ PINEBUFSIZE ];
142 int c, i = 0;
143 gchar ch;
145 if( feof( pineFile->file ) )
146 return NULL;
148 while( i < PINEBUFSIZE-1 ) {
149 c = fgetc( pineFile->file );
150 if( c == EOF ) {
151 if( i == 0 )
152 return NULL;
153 break;
155 ch = (gchar) c;
156 if( ch == '\0' ) {
157 if( i == 0 )
158 return NULL;
159 break;
161 if( ch == '\n' ) {
162 break;
164 buf[i] = ch;
165 i++;
167 buf[i] = '\0';
169 /* Copy into private buffer */
170 return g_strdup( buf );
174 * Parsed address data.
176 typedef struct _Pine_ParsedRec_ Pine_ParsedRec;
177 struct _Pine_ParsedRec_ {
178 gchar *nickName;
179 gchar *name;
180 gchar *address;
181 gchar *fcc;
182 gchar *comments;
183 gboolean isGroup;
184 GSList *listName;
185 GSList *listAddr;
189 * Free data record.
190 * Enter: rec Data record.
192 static void pine_free_rec( Pine_ParsedRec *rec ) {
193 if( rec ) {
194 g_free( rec->nickName );
195 g_free( rec->name );
196 g_free( rec->address );
197 g_free( rec->fcc );
198 g_free( rec->comments );
199 mgu_clear_slist( rec->listName );
200 mgu_clear_slist( rec->listAddr );
201 g_slist_free( rec->listName );
202 g_slist_free( rec->listAddr );
203 rec->nickName = NULL;
204 rec->name = NULL;
205 rec->address = NULL;
206 rec->fcc = NULL;
207 rec->comments = NULL;
208 rec->isGroup = FALSE;
209 g_free( rec );
214 * Clean name.
216 static void pine_clean_name( Pine_ParsedRec *rec ) {
217 gchar *p;
219 p = rec->name;
220 if( p == NULL ) return;
221 if( *p == '\0' ) return;
223 g_strstrip( rec->name );
224 if( *p == CHAR_APOS || *p == CHAR_QUOTE ) {
225 return;
228 /* If embedded comma present, surround match with quotes */
229 while( *p ) {
230 if( *p == CHAR_COMMA ) {
231 p = g_strdup_printf( "\"%s\"", rec->name );
232 g_free( rec->name );
233 rec->name = p;
234 return;
236 p++;
241 * Parse pine address record.
242 * Enter: buf Address record buffer.
243 * Return: Data record.
245 static Pine_ParsedRec *pine_parse_record( gchar *buf ) {
246 Pine_ParsedRec *rec;
247 gchar *p, *f;
248 gint pos, len, i;
249 gchar *tmp[5];
251 for( i = 0; i < 5; i++ )
252 tmp[i] = NULL;
254 /* Extract tab separated values */
255 rec = NULL;
256 pos = 0;
257 p = f = buf;
258 while( *p ) {
259 if( *p == '\t' ) {
260 len = p - f;
261 if( len > 0 ) {
262 tmp[ pos ] = g_strndup( f, len );
263 f = p;
264 f++;
266 pos++;
268 p++;
271 /* Extract last value */
272 len = p - f;
273 if( len > 0 ) {
274 tmp[ pos++ ] = g_strndup( f, len );
277 /* Populate record */
278 if( pos > 0 ) {
279 rec = g_new0( Pine_ParsedRec, 1 );
280 rec->isGroup = FALSE;
281 for( i = 0; i < pos; i++ ) {
282 f = tmp[i];
283 if( f ) {
284 g_strstrip( f );
286 if( i == 0 ) rec->nickName = f;
287 else if( i == 1 ) rec->name = f;
288 else if( i == 2 ) rec->address = f;
289 else if( i == 3 ) rec->fcc = f;
290 else if( i == 4 ) rec->comments = f;
291 tmp[i] = NULL;
294 if( rec->address != NULL ) {
295 /* Strip leading/trailing parens */
296 p = rec->address;
297 if( *p == '(' ) {
298 len = strlen( p ) - 1;
299 *p = ' ';
300 *(p + len) = ' ';
301 rec->isGroup = TRUE;
306 return rec;
310 * Parse name from email address string.
311 * Enter: buf Start address of buffer to process (not modified).
312 * atp Pointer to email at (@) character.
313 * ap Pointer to start of email address returned.
314 * ep Pointer to end of email address returned.
315 * Return: Parsed name or NULL if not present. This should be g_free'd
316 * when done.
318 static gchar *pine_parse_name(
319 const gchar *buf, const gchar *atp, const gchar **ap,
320 const gchar **ep )
322 gchar *name;
323 const gchar *pos;
324 const gchar *tmp;
325 const gchar *bp;
326 gint ilen;
328 name = NULL;
329 *ap = NULL;
330 *ep = NULL;
332 /* Find first non-separator char */
333 bp = buf;
334 while( TRUE ) {
335 if( strchr( ",; \n\r", *bp ) == NULL ) break;
336 bp++;
339 /* Search back for start of name */
340 tmp = atp;
341 pos = atp;
342 while( pos >= bp ) {
343 tmp = pos;
344 if( *pos == '<' ) {
345 /* Found start of address/end of name part */
346 ilen = -1 + ( size_t ) ( pos - bp );
347 name = g_strndup( bp, ilen + 1 );
348 *(name + ilen + 1) = '\0';
350 /* Remove leading trailing quotes and spaces */
351 mgu_str_ltc2space( name, '\"', '\"' );
352 mgu_str_ltc2space( name, '\'', '\'' );
353 mgu_str_ltc2space( name, '\"', '\"' );
354 mgu_str_unescape( name );
355 g_strstrip( name );
356 break;
358 pos--;
360 *ap = tmp;
362 /* Search forward for end of address */
363 pos = atp + 1;
364 while( TRUE ) {
365 if( *pos == '>' ) {
366 pos++;
367 break;
369 if( strchr( ",; \'\n\r", *pos ) ) break;
370 pos++;
372 *ep = pos;
374 return name;
378 * Parse address list.
379 * Enter: pineFile Pine control data.
380 * cache Address cache.
381 * rec Data record.
383 static void pine_parse_address( PineFile *pineFile, AddressCache *cache, Pine_ParsedRec *rec ) {
384 const gchar *buf;
385 gchar addr[ PINEBUFSIZE ];
386 const gchar *bp;
387 const gchar *ep;
388 gchar *atCh;
389 gchar *name;
390 gint len;
392 g_return_if_fail( rec->address != NULL );
394 buf = rec->address;
395 while((atCh = strchr( buf, CHAR_AT )) != NULL) {
396 name = pine_parse_name( buf, atCh, &bp, &ep );
397 len = ( size_t ) ( ep - bp );
398 strncpy( addr, bp, len );
399 addr[ len ] = '\0';
400 extract_address( addr );
402 if( name == NULL ) name = g_strdup( "" );
403 rec->listName = g_slist_append( rec->listName, name );
404 rec->listAddr = g_slist_append( rec->listAddr, g_strdup( addr ) );
406 buf = ep;
407 if( atCh == ep ) {
408 buf++;
414 * Insert person and address into address cache.
415 * Enter: pineFile Pine control data.
416 * cache Address cache.
417 * address E-Mail address.
418 * name Name.
419 * remarks Remarks.
420 * Return: E-Mail object, either inserted or found in hash table.
422 static ItemEMail *pine_insert_table(
423 PineFile *pineFile, AddressCache *cache, gchar *address,
424 gchar *name, gchar *remarks )
426 ItemPerson *person;
427 ItemEMail *email;
428 gchar *key;
430 g_return_val_if_fail( address != NULL, NULL );
432 /* create an entry with empty name if needed */
433 if ( name == NULL )
434 name = "";
436 /* Test whether address already in hash table */
437 key = g_strdup( address );
438 g_strdown( key );
439 email = g_hash_table_lookup( pineFile->uniqTable, key );
441 if( email == NULL ) {
442 /* No - create person */
443 person = addritem_create_item_person();
444 addritem_person_set_common_name( person, name );
445 addrcache_id_person( cache, person );
446 addrcache_add_person( cache, person );
448 /* Add email for person */
449 email = addritem_create_item_email();
450 addritem_email_set_address( email, address );
451 addritem_email_set_remarks( email, remarks );
452 addrcache_id_email( cache, email );
453 addrcache_person_add_email( cache, person, email );
455 /* Insert entry */
456 g_hash_table_insert( pineFile->uniqTable, key, email );
458 else {
459 /* Yes - update person with longest name */
460 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
461 if( strlen( name ) > strlen( ADDRITEM_NAME(person) ) ) {
462 addritem_person_set_common_name( person, name );
465 /* Free up */
466 g_free( key );
469 return email;
473 * Parse address line adn build address items.
474 * Enter: pineFile Pine control data.
475 * cache Address cache to load.
476 * line Address record.
478 static void pine_build_items( PineFile *pineFile, AddressCache *cache, gchar *line ) {
479 Pine_ParsedRec *rec;
480 GSList *nodeAddr, *nodeName;
481 ItemGroup *group;
482 ItemEMail *email;
484 rec = pine_parse_record( line );
485 if( rec ) {
486 pine_clean_name( rec );
487 pine_parse_address( pineFile, cache, rec );
488 /* pine_print_rec( rec, stdout ); */
489 /* printf( "=========\n" ); */
491 if( rec->isGroup ) {
492 /* Create group */
493 group = addritem_create_item_group();
494 addritem_group_set_name( group, rec->nickName );
495 addrcache_id_group( cache, group );
496 addrcache_add_group( cache, group );
498 /* Add email to group */
499 nodeName = rec->listName;
500 nodeAddr = rec->listAddr;
501 while( nodeAddr ) {
502 email = pine_insert_table(
503 pineFile, cache, nodeAddr->data,
504 nodeName->data, "" );
506 /* Add email to group */
507 addritem_group_add_email( group, email );
509 nodeAddr = g_slist_next( nodeAddr );
510 nodeName = g_slist_next( nodeName );
513 else {
514 email = pine_insert_table(
515 pineFile, cache, rec->address,
516 rec->name, rec->comments );
519 pine_free_rec( rec );
524 * Read file data into address cache.
525 * Enter: pineFile Pine control data.
526 * cache Address cache to load.
528 static void pine_read_file( PineFile *pineFile, AddressCache *cache ) {
529 GSList *listValue = NULL;
530 gboolean flagEOF = FALSE, flagProc = FALSE, flagDone = FALSE;
531 gchar *line = NULL, *lineValue = NULL;
532 long posEnd = 0L;
533 long posCur = 0L;
535 /* Find EOF for progress indicator */
536 fseek( pineFile->file, 0L, SEEK_END );
537 posEnd = ftell( pineFile->file );
538 fseek( pineFile->file, 0L, SEEK_SET );
540 flagProc = FALSE;
541 while( ! flagDone ) {
542 if( flagEOF ) {
543 flagDone = TRUE;
544 flagProc = TRUE;
546 else {
547 line = pine_read_line( pineFile );
550 posCur = ftell( pineFile->file );
551 if( pineFile->cbProgress ) {
552 /* Call progress indicator */
553 ( pineFile->cbProgress ) ( pineFile, & posEnd, & posCur );
556 /* Add line to list */
557 if( line == NULL ) {
558 flagEOF = TRUE;
560 else {
561 /* Check for continuation line (1 space only) */
562 if( *line == ' ' ) {
563 g_strchug( line );
564 listValue = g_slist_append(
565 listValue, g_strdup( line ) );
566 flagProc = FALSE;
568 else {
569 flagProc = TRUE;
573 if( flagProc ) {
574 if( listValue != NULL ) {
575 /* Process list */
576 lineValue = mgu_list_coalesce( listValue );
577 if( lineValue ) {
578 pine_build_items(
579 pineFile, cache, lineValue );
581 g_free( lineValue );
582 lineValue = NULL;
583 mgu_free_list( listValue );
584 listValue = NULL;
586 if( line != NULL ) {
587 /* Append to list */
588 listValue = g_slist_append(
589 listValue, g_strdup( line ) );
593 g_free( line );
594 line = NULL;
597 /* Release data */
598 mgu_free_list( listValue );
599 listValue = NULL;
603 * ============================================================================================
604 * Read file into list. Main entry point
605 * Enter: pineFile Pine control data.
606 * cache Address cache to load.
607 * Return: Status code.
608 * ============================================================================================
610 gint pine_import_data( PineFile *pineFile, AddressCache *cache ) {
611 g_return_val_if_fail( pineFile != NULL, MGU_BAD_ARGS );
612 g_return_val_if_fail( cache != NULL, MGU_BAD_ARGS );
614 pineFile->retVal = MGU_SUCCESS;
615 addrcache_clear( cache );
616 cache->dataRead = FALSE;
617 pine_open_file( pineFile );
618 if( pineFile->retVal == MGU_SUCCESS ) {
619 /* Read data into the cache */
620 pine_read_file( pineFile, cache );
621 pine_close_file( pineFile );
623 /* Mark cache */
624 cache->modified = FALSE;
625 cache->dataRead = TRUE;
627 return pineFile->retVal;
630 #define WORK_BUFLEN 1024
633 * Attempt to find a Pine addressbook file.
634 * Return: Filename, or home directory if not found, or empty string if
635 * no home. Filename should be g_free() when done.
637 gchar *pine_find_file( void ) {
638 const gchar *homedir;
639 gchar str[ WORK_BUFLEN ];
640 gint len;
641 FILE *fp;
643 homedir = get_home_dir();
644 if( ! homedir ) return g_strdup( "" );
646 strcpy( str, homedir );
647 len = strlen( str );
648 if( len > 0 ) {
649 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
650 str[ len ] = G_DIR_SEPARATOR;
651 str[ ++len ] = '\0';
654 strcat( str, PINE_HOME_FILE );
656 /* Attempt to open */
657 if( ( fp = g_fopen( str, "rb" ) ) != NULL ) {
658 fclose( fp );
660 else {
661 /* Truncate filename */
662 str[ len ] = '\0';
664 return g_strdup( str );
668 * End of Source.