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 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
|
/* Spruce
* Copyright (C) 1999 Susixware
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "mailbox.h"
static gchar module_path[] = ".spruce/modules";
static gchar mailbox_dir[] = ".spruce";
guint32 *mailpos;
gchar *get_mailbox(gchar *mailbox)
{
guint memsize;
gchar *home_dir;
gchar *buffer;
gchar *ptr;
home_dir = g_strdup(getenv("HOME"));
if (home_dir == NULL)
return NULL;
memsize = strlen(home_dir) + 1 + strlen(mailbox_dir) + 1 + strlen(mailbox) + 6;
buffer = g_malloc0(memsize);
sprintf(buffer, "%s/%s/%s.mbx", home_dir, mailbox_dir, mailbox);
/* replace " "'s with "_" so that we can have mailboxes with spaces in the names */
for (ptr = buffer; *ptr != '\0'; ptr++)
{
if (*ptr == ' ')
*ptr = '_';
}
g_free(home_dir);
return buffer;
}
gboolean mailbox_is_mbox_format(gchar *mbx)
{
gchar buffer[512];
FILE *fp;
fp = fopen(mbx, "rt");
if (fp == NULL)
return FALSE;
memset(buffer, 0, 512);
fgets(buffer, 511, fp);
if (!g_strncasecmp("From ", buffer, strlen("From ")))
{
fclose(fp);
return TRUE;
}
fclose(fp);
return FALSE;
}
gint mailbox_make_uid_list(GtkWidget *clistBoxes, guint num)
{
/* gchar filename[256];
guint mbox;
guint mesgid;
gchar *mesg;
gchar *mailbox;
FILE *fp, *tmp;
g_snprintf(filename, 255, "%s/.spruce/uid.list", getenv("HOME"));
fp = fopen(filename, "wt");
if (fp == NULL)
return 0;
for (mbox = 0; mbox < num; mbox++)
{
}
fclose(fp);*/
return 1;
}
gint mailbox_append_uid(gchar *uid)
{
gchar filename[256];
FILE *fp;
g_snprintf(filename, 255, "%s/.spruce/uid.list", getenv("HOME"));
fp = fopen(filename, "at");
if (fp == NULL)
return 0;
trim_whtspc(uid);
fprintf(fp, "%s\n", uid);
fflush(fp);
fclose(fp);
return 1;
}
gboolean mailbox_have_uid(gchar *uid)
{
gchar filename[256];
gchar line[256];
FILE *fp;
trim_whtspc(uid);
g_snprintf(filename, 255, "%s/.spruce/uid.list", getenv("HOME"));
fp = fopen(filename, "rt");
if (fp == NULL)
return FALSE;
while (!feof(fp))
{
memset(line, 0, 255);
fgets(line, 255, fp);
trim_whtspc(line);
if (!strncmp(line, uid, strlen(uid)))
{
fclose(fp);
return TRUE;
}
}
fclose(fp);
return FALSE;
}
guint mailbox_get_num_mesgs (gchar *mailbox)
{
gchar *mbx = get_mailbox(mailbox);
gchar *p, *q;
struct stat st;
gint fd;
guint num = 0;
if (mbx == NULL)
return 0;
fd = open(mbx, O_RDONLY);
if (mailpos != NULL)
g_free(mailpos);
mailpos = g_malloc0(sizeof(guint32));
mailpos[0] = 0;
if (fd != -1)
{
if (stat(mbx, &st) != -1)
{
if (st.st_size)
{
p = q = (gchar*) mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (p == (gchar*)NULL)
{
close(fd);
perror("mmap()");
return 0;
}
while ((p = strstr(p, "\n.\r\n")))
{
num++;
mailpos = g_realloc(mailpos, (num + 1) * sizeof(guint32));
p += 4;
mailpos[num] = (guint32)(p - q); /* yuk */
}
munmap(q, st.st_size);
}
}
close(fd);
}
if (!num) /* if there weren't any messages... */
mailbox_create(mailbox); /* ...recreate the mailbox */
g_free(mbx);
fprintf(stderr, "Total messages for %s = %d\n", mailbox, num);
return num;
}
gchar *mailbox_get_mesg (guint mesg_num, gchar *mailbox)
{
gchar *mbx = get_mailbox(mailbox);
gchar *data = NULL;
gchar *p, *q;
struct stat st;
gint fd;
guint32 n, offset, pagesize;
fd = open(mbx, O_RDONLY);
if (fd != -1)
{
if (stat(mbx, &st) != -1)
{
pagesize = getpagesize();
offset = (mailpos[mesg_num - 1] / pagesize) * pagesize;
p = q = (gchar *)mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, offset);
if (p == (gchar*)NULL)
{
close(fd);
perror("mmap()");
return NULL;
}
p += mailpos[mesg_num - 1] % pagesize;
n = (guint32)(strstr(p, "\n.\r\n") - p); /* yuk */
if (n > 0)
{
data = g_malloc0(n + 1);
strncpy(data, p, n);
/* we should just strip the message here, makes more sense */
strip(data, '\r');
}
else
fprintf(stderr, "strstr returned null in mailbox_get_mesg\n");
munmap(q, st.st_size);
}
else
fprintf(stderr, "Failed to stat %s.\n", mbx);
close(fd);
}
else
fprintf(stderr, "Failed to open %s.\n", mbx);
g_free(mbx);
return data;
}
gchar *mailbox_get_mesg_subject (gchar *message)
{
gchar buffer[512], ch, *subject;
int index, i;
index = find_string(message, "Subject:");
if (index == -1)
return NULL;
/* advance to the start of the subject */
index += strlen("Subject:") + 1;
i = 0;
ch = message[index];
memset(buffer, 0, sizeof(buffer));
while(ch != '\r' && ch != '\n')
{
buffer[i] = ch;
index++;
i++;
ch = message[index];
}
buffer[i] = '\0';
trim_whtspc(buffer);
if (buffer[0] != '\0')
parse_8bit(buffer);
subject = buffer;
return subject;
}
gchar *mailbox_get_mesg_from (gchar *message)
{
gchar buffer[512], ch, *from;
gint index, i;
index = find_string(message, "From:");
if (index == -1)
return NULL;
/* advance to the start of From: */
index += strlen("From:") + 1;
i = 0;
ch = message[index];
memset(buffer, 0, sizeof(buffer));
while(ch != '\r' && ch != '\n')
{
buffer[i] = ch;
index++;
i++;
ch = message[index];
}
buffer[i] = '\0';
trim_whtspc(buffer);
parse_8bit(buffer);
from = buffer;
return from;
}
gchar *mailbox_get_mesg_replyto (gchar *message)
{
gchar buffer[512], ch, *replyto;
gint index, i;
index = find_string(message, "\nReply-To");
if (index == -1)
return NULL;
/* advance to the start of Reply-To: */
index += strlen("\nReply-To:") + 1;
i = 0;
ch = message[index];
memset(buffer, 0, sizeof(buffer));
while(ch != '\r' && ch != '\n')
{
buffer[i] = ch;
index++;
i++;
ch = message[index];
}
buffer[i] = '\0';
trim_whtspc(buffer);
replyto = buffer;
return replyto;
}
gchar *mailbox_get_mesg_date (gchar *message)
{
gchar buffer[512], ch, *date;
gint index, i;
index = find_string(message, "Date:");
if (index == -1)
return NULL;
/* advance to the start of the date */
index += strlen("Date:") + 1;
i = 0;
ch = message[index];
memset(buffer, 0, sizeof(buffer));
while(ch != '\r' && ch != '\n')
{
buffer[i] = ch;
index++;
i++;
ch = message[index];
}
buffer[i] = '\0';
trim_whtspc(buffer);
date = buffer;
return date;
}
gchar *mailbox_get_mesg_xmailer (gchar *message)
{
gchar buffer[512], ch, *xmailer;
gint index, i;
index = find_string(message, "X-Mailer:");
if (index == -1)
return NULL;
/* advance to the start of the xmailer */
index += strlen("X-Mailer:") + 1;
i = 0;
ch = message[index];
memset(buffer, 0, sizeof(buffer));
while(ch != '\r' && ch != '\n')
{
buffer[i] = ch;
index++;
i++;
ch = message[index];
}
buffer[i] = '\0';
trim_whtspc(buffer);
xmailer = buffer;
return xmailer;
}
gchar *mailbox_get_mesg_header_field (gchar *message, gchar *field)
{
gchar buffer[512], *header, *index, temp_line[512];
gchar *mesg_header;
gint i, done, first;
/*strip(message, '\r');*/
/* lets find the end of the header info */
index = strstr(message,"\n\n");
if(index == NULL)
return (gchar *)NULL;
index += 2;
/* now lets make a copy of the header info */
mesg_header = g_malloc0(index - message + 1);
strncpy(mesg_header, message, index - message);
mesg_header[index - message] = '\0';
index = strstrcase(mesg_header, field);
if (index == NULL)
return NULL;
/* NULL out out buffer */
memset(buffer, 0, 512);
/* advance to the start of the header field */
index += strlen(field) + 1;
done = 0;
first = 1;
/* we'll get data until the first char in the line (except the first line)
* is not a tab (indicates a new header field) */
while (done == 0)
{
i = 0;
while(*index != '\n')
{
temp_line[i] = *index;
i++;
index++;
}
temp_line[i] = '\0';
if (!first)
{
if (temp_line[0] != '\t' && temp_line[0] != ' ')
done = 1;
}
if(!done)
{
strcat(buffer, temp_line);
index++;
first = 0;
}
}
trim_whtspc(buffer);
if (!strcmp(field, "Subject:") || !strcmp(field, "From:"))
parse_8bit(buffer);
header = buffer;
g_free(mesg_header);
return header;
}
/* Must sort messages BEFORE calling this function */
gint mailbox_del_mesgs (guint mesgs, guint *mesg_num, gchar *mailbox)
{
/* delete the specified message(s) by first copying the entire mailbox
* to a temp file (except the message to be deleted) and then copying
* it back. */
gchar *mbx = get_mailbox(mailbox);
gchar *tmbx = get_mailbox("_TempDel_");
FILE *fp, *tfp;
gchar line[513];
gint num = 1, last_was_end, i = 0;
if (mesg_num <= 0)
return 0;
fp = fopen(mbx, "rt");
if (fp == NULL)
return 0;
tfp = fopen(tmbx, "wt");
if (tfp == NULL)
return 0;
chmod(tmbx, S_IREAD | S_IWRITE);
while (i < mesgs)
{
/* get all the messages up to the one to be deleted */
while(num < mesg_num[i])
{
fgets(line, 512, fp);
fprintf(tfp, "%s", line);
if(!strcmp(line, ".\r\n"))
num++;
}
/* skip this next message (one to be deleted) */
while(num == mesg_num[i])
{
fgets(line, 512, fp);
if (!strcmp(line, ".\r\n"))
num++;
}
i++;
}
/* get the rest... */
last_was_end = 1;
while(!feof(fp))
{
fgets(line, 512, fp);
if (last_was_end && !strcmp(line, ".\r\n"))
continue;
fprintf(tfp, "%s", line);
if (!strcmp(line, ".\r\n"))
last_was_end = 1;
else
last_was_end = 0;
}
fflush(fp);
fflush(tfp);
fclose(fp); /* close the file */
fclose(tfp);
/* new method...maybe this'll make it faster */
rename(tmbx, mbx);
g_free(mbx);
g_free(tmbx);
return 1;
}
/* Must sort messages BEFORE calling this function */
gint mailbox_copy_mesgs(guint mesgs, guint *mesg_num, gchar *orig_mailbox, gchar *new_mailbox)
{
gchar *mesg;
gchar *newmbx = get_mailbox(new_mailbox);
FILE *fp;
guint i = 0;
fp = fopen(newmbx, "at");
if (fp == NULL)
return 0;
while(i < mesgs)
{
mesg = mailbox_get_mesg(mesg_num[i], orig_mailbox);
if (mesg == NULL)
return 0;
fprintf(fp, "%s\n.\r\n", mesg);
g_free(mesg);
i++;
}
fflush(fp);
fclose(fp);
g_free(newmbx);
return 1;
}
/* Must sort messages BEFORE calling this function */
gint mailbox_move_mesgs(guint mesgs, guint *mesg_num, gchar *orig_mailbox, gchar *new_mailbox)
{
if (!mailbox_copy_mesgs(mesgs, mesg_num, orig_mailbox, new_mailbox))
return 0;
if (!mailbox_del_mesgs(mesgs, mesg_num, orig_mailbox))
return 0;
return 1;
}
gint mailbox_delete(gchar *mailbox)
{
/* delete the mailbox */
gchar *mbx = get_mailbox(mailbox);
remove(mbx);
g_free(mbx);
return 1;
}
gint mailbox_create(gchar *mailbox)
{
/* create a new mailbox or at least clean it out. */
gchar *mbx = get_mailbox(mailbox);
gint fd;
fd = open(mbx, O_WRONLY | O_TRUNC | O_CREAT, 00600);
if (fd == -1)
{
g_free(mbx);
return 0;
}
close(fd);
g_free(mbx);
return 1;
}
gint get_header_len(gchar *mesg)
{
gchar *start;
/* advance to the point where there are at least 2x end-lines
* which should mark the end of the header info and the beginning
* of the body of the message. */
for (start = mesg; *start != '\0'; start++)
if (*start == '\n' && *(start - 1) == '\n')
break;
return (start - mesg);
}
gchar *get_trimmed_header(gchar *mesg, gint trim_level, gint hdrlen)
{
gchar *hdrinfo, *hdrstripped;
gchar *hdrptr, *fieldptr;
gchar showfields[6][16] = { "Date:", "\nTo:", "\nFrom:", "\nReply-To:", "\nSubject:", "\0" };
gint i;
hdrinfo = g_malloc0(hdrlen + 1);
strncpy(hdrinfo, mesg, hdrlen);
if (trim_level == 2)
return hdrinfo;
hdrstripped = g_malloc0(hdrlen + 1);
i = 0;
hdrptr = hdrstripped;
while (*showfields[i] != '\0')
{
if ((fieldptr = strstrcase(hdrinfo, showfields[i])) != NULL)
{
*hdrptr = *fieldptr;
hdrptr++;
fieldptr++;
for ( ; *fieldptr != '\n' && *fieldptr != '\0'; hdrptr++, fieldptr++)
*hdrptr = *fieldptr;
*hdrptr = '\n';
}
i++;
}
*hdrptr = '\0';
g_free(hdrinfo);
hdrinfo = g_strdup(hdrstripped);
g_free(hdrstripped);
return(hdrinfo);
}
void trim_header(gchar *mesg, gint trim_level)
{
gchar *start, *ptr, *ptr_mesg;
gchar *hdrstripped;
gint hdrlen;
/*strip(mesg, '\r');*/
start = mesg;
hdrlen = get_header_len(mesg);
if (trim_level != 2)
start += hdrlen;
if (trim_level == 1) /* show partial header only */
{
hdrstripped = get_trimmed_header(mesg, trim_level, hdrlen);
start -= strlen(hdrstripped);
strncpy(start, hdrstripped, strlen(hdrstripped));
g_free(hdrstripped);
}
/* advance to the actual beginning of the message (gets rid of any
* extra line feeds that might be between the header and the actual
* message). */
for ( ; *start != '\0'; start++)
if (*start != '\n')
break;
for (ptr_mesg = mesg, ptr = start; *ptr != '\0'; ptr_mesg++, ptr++)
*ptr_mesg = *ptr;
*ptr_mesg = '\0';
}
/* please update me to handle multiple ids */
void mailbox_refresh_mailpos (guint num_ids, guint *ids, guint num_mesgs, gchar *mailbox)
{
/* num_ids = number of ids */
/* ids = message id of the message we deleted (1 thru x) */
/* num_mesgs = number of messages after we deleted 1 */
/* mailbox = we're gonna try to do away with this var, but until I work
out the bug in the code before mailbox_get_num_mesgs() we're gonna
have to keep it around as our failsafe */
guint32 mesg_size;
guint i, mesgid;
/* if we have messages left and we didn't just delete the last message... */
if (num_mesgs > 0 && ids[0] != (num_mesgs + 1) )
{
mesgid = ids[0] - 1; /* set the mesg id to the array id */
mesg_size = mailpos[ids[0]] - mailpos[mesgid]; /* calc deleted mesg size */
/* fprintf(stderr, "mesg_size = %d\n", mesg_size);
fflush(stderr);*/
/* mailpos[mesgid] stays the same byte offset */
for (i = mesgid; i < num_mesgs - ids[0]; i++)
mailpos[i] = mailpos[i + 1] - mesg_size;
}
mailbox_get_num_mesgs(mailbox); /* temp fix cuz thus function bombs */
}
|