Commit 45541d41 45541d41ae6bec2dfacb23e957c40816f4c2a43a by Sergey Poznyakoff

util_expand_msglist(): return 1 and set msglist[0]=cursor if argc == 1.

util_do_command(): Added missing va_end().
util_get_sender(): Returns sender email for the given msgno.
		Optionally strips off domain part.
util_slist_.*(): "string lists" used by alias,alt,retain,ignore.
util_strcat(): Concat two strings, reallocating first to the necessary length
(Rather clumsy, we'd be better off using obstack I guess).
util_escape_percent(): Excape % signs in a string, so it can be used as
format in .*printf calls.
1 parent f56551c4
Showing 1 changed file with 230 additions and 8 deletions
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ 16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17 17
18 #include "mail.h" 18 #include "mail.h"
19 #include <mailutils/mutil.h>
19 20
20 typedef struct _node { 21 typedef struct _node {
21 /* for the msglist expander */ 22 /* for the msglist expander */
...@@ -204,16 +205,35 @@ util_expand_msglist (const int argc, char **argv, int **list) ...@@ -204,16 +205,35 @@ util_expand_msglist (const int argc, char **argv, int **list)
204 } 205 }
205 } 206 }
206 207
207 for (current = first; current != NULL; current = current->next) 208 for (current = first; current->next != NULL; current = current->next)
208 lc++; 209 lc++;
209 210
210 ret = malloc (lc * sizeof (int)); 211 if (!lc)
211 lc = 0; 212 {
212 for (current = first; current != NULL; current = current->next) 213 ret = calloc (1, sizeof (int));
213 ret [lc++] = current->data; 214 if (!ret)
215 {
216 fprintf (ofile, "not enough memory\n");
217 exit (1);
218 }
219 ret [0] = cursor;
220 lc = 1;
221 }
222 else
223 {
224 ret = malloc (lc * sizeof (int));
225 if (!ret)
226 {
227 fprintf (ofile, "not enough memory\n");
228 exit (1);
229 }
230 lc = 0;
231 for (current = first; current->next != NULL; current = current->next)
232 ret [lc++] = current->data;
233 }
214 util_ll_free (first); 234 util_ll_free (first);
215 *list = ret; 235 *list = ret;
216 return lc-1; 236 return lc;
217 } 237 }
218 238
219 /* 239 /*
...@@ -230,10 +250,13 @@ util_do_command (const char *c, ...) ...@@ -230,10 +250,13 @@ util_do_command (const char *c, ...)
230 function_t *command; 250 function_t *command;
231 char *cmd = NULL; 251 char *cmd = NULL;
232 va_list ap; 252 va_list ap;
253
233 va_start (ap, c); 254 va_start (ap, c);
234 if (vasprintf (&cmd, c, ap) < 1) 255 status = vasprintf (&cmd, c, ap);
256 va_end (ap);
257 if (status < 0)
235 return 0; 258 return 0;
236 259
237 if (cmd) 260 if (cmd)
238 { 261 {
239 struct mail_command_entry entry; 262 struct mail_command_entry entry;
...@@ -567,4 +590,203 @@ util_fullpath(char *inpath) ...@@ -567,4 +590,203 @@ util_fullpath(char *inpath)
567 return mu_tilde_expansion(inpath, "/", NULL); 590 return mu_tilde_expansion(inpath, "/", NULL);
568 } 591 }
569 592
593 char *
594 util_get_sender(int msgno, int strip)
595 {
596 header_t header = NULL;
597 address_t addr = NULL;
598 message_t msg = NULL;
599 char buffer[512], *p;
600
601 mailbox_get_message (mbox, msgno, &msg);
602 message_get_header (msg, &header);
603 if (header_get_value (header, MU_HEADER_FROM, buffer, sizeof(buffer), NULL)
604 || address_create (&addr, buffer))
605 {
606 envelope_t env = NULL;
607 message_get_envelope (msg, &env);
608 if (envelope_sender (env, buffer, sizeof (buffer), NULL)
609 || address_create (&addr, buffer))
610 {
611 fprintf (ofile, "can't determine sender name (msg %d)\n", msgno);
612 return NULL;
613 }
614 }
615
616 if (address_get_email (addr, 1, buffer, sizeof(buffer), NULL))
617 {
618 fprintf (ofile, "can't determine sender name (msg %d)\n", msgno);
619 address_destroy (&addr);
620 return NULL;
621 }
622
623 if (strip)
624 {
625 p = strchr (buffer, '@');
626 if (p)
627 *p = 0;
628 }
629
630 p = strdup (buffer);
631 address_destroy (&addr);
632 return p;
633 }
634
635 void
636 util_slist_print(list_t list, int nl)
637 {
638 iterator_t itr;
639 char *name;
640
641 if (!list || iterator_create (&itr, list))
642 return;
643
644 for (iterator_first (itr); !iterator_is_done (itr); iterator_next (itr))
645 {
646 iterator_current (itr, (void **)&name);
647 fprintf (ofile, "%s%c", name, nl ? '\n' : ' ');
648
649 }
650 iterator_destroy (&itr);
651 }
652
653 int
654 util_slist_lookup(list_t list, char *str)
655 {
656 iterator_t itr;
657 char *name;
658 int rc = 0;
659
660 if (!list || iterator_create (&itr, list))
661 return 0;
662
663 for (iterator_first (itr); !iterator_is_done (itr); iterator_next (itr))
664 {
665 iterator_current (itr, (void **)&name);
666 if (strcasecmp (name, str) == 0)
667 {
668 rc = 1;
669 break;
670 }
671 }
672 iterator_destroy (&itr);
673 return rc;
674 }
675
676 void
677 util_slist_add (list_t *list, char *value)
678 {
679 char *p;
680
681 if (!*list && list_create (list))
682 return;
683
684 if ((p = strdup(value)) == NULL)
685 {
686 fprintf (ofile, "not enough memory\n");
687 return;
688 }
689 list_append (*list, p);
690 }
691
692 void
693 util_slist_destroy (list_t *list)
694 {
695 iterator_t itr;
696 char *name;
697
698 if (!*list || iterator_create (&itr, *list))
699 return;
700
701 for (iterator_first (itr); !iterator_is_done (itr); iterator_next (itr))
702 {
703 iterator_current (itr, (void **)&name);
704 free (name);
705 }
706 iterator_destroy (&itr);
707 list_destroy (list);
708 }
709
710 char *
711 util_slist_to_string (list_t list, char *delim)
712 {
713 iterator_t itr;
714 char *name;
715 char *str = NULL;
716
717 if (!list || iterator_create (&itr, list))
718 return NULL;
719
720 for (iterator_first (itr); !iterator_is_done (itr); iterator_next (itr))
721 {
722 iterator_current (itr, (void **)&name);
723 if (str && delim)
724 util_strcat(&str, delim);
725 util_strcat(&str, name);
726 }
727 iterator_destroy (&itr);
728 return str;
729 }
730
731 void
732 util_strcat(char **dest, char *str)
733 {
734 if (!*dest)
735 *dest = strdup (str);
736 else
737 {
738 int dlen = strlen (*dest) + 1;
739 int slen = strlen (str) + 1;
740 char *newp = realloc (*dest, dlen + slen);
741
742 if (!newp)
743 return;
744
745 *dest = newp;
746 memcpy (newp + dlen - 1, str, slen);
747 }
748 }
749
750 void
751 util_escape_percent (char **str)
752 {
753 int count;
754 char *p, *q;
755 char *newstr;
756
757 /* Count ocurrences of % in the string */
758 count = 0;
759 for (p = *str; *p; p++)
760 if (*p == '%')
761 count++;
762
763 if (!count)
764 return; /* nothing to do */
765
766 /* expand the string */
767 newstr = malloc (strlen (*str) + 1 + count);
768 if (!newstr)
769 {
770 fprintf (ofile, "not enough memory\n");
771 exit (1); /* be on the safe side */
772 }
773
774 /* and escape percent signs */
775 p = newstr;
776 q = *str;
777 while (*p = *q++)
778 {
779 if (*p == '%')
780 *++p = '%';
781 p++;
782 }
783 *str = newstr;
784 }
785
786
787
788
789
790
791
570 792
......