comparison src/protocols/silc/util.c @ 12217:029802981b81

[gaim-migrate @ 14519] SILC IM Image and cipher selection support from Pekka Riikonen. Modified by me so the SILC prpl continues to work with SILC Toolkit < 1.0.1, but without images. Any breakage is my fault. committer: Tailor Script <tailor@pidgin.im>
author Stu Tomlinson <stu@nosnilmot.com>
date Thu, 24 Nov 2005 21:07:12 +0000
parents 9c7ca8a8c4b8
children cfc2f7fcb3dd
comparison
equal deleted inserted replaced
12216:4d3119205a33 12217:029802981b81
18 */ 18 */
19 19
20 #include "silcincludes.h" 20 #include "silcincludes.h"
21 #include "silcclient.h" 21 #include "silcclient.h"
22 #include "silcgaim.h" 22 #include "silcgaim.h"
23 #include "imgstore.h"
23 24
24 /**************************** Utility Routines *******************************/ 25 /**************************** Utility Routines *******************************/
25 26
26 static char str[256], str2[256]; 27 static char str[256], str2[256];
27 28
567 geo.longitude ? geo.longitude : "", 568 geo.longitude ? geo.longitude : "",
568 geo.latitude ? geo.latitude : "", 569 geo.latitude ? geo.latitude : "",
569 geo.altitude ? geo.altitude : "", 570 geo.altitude ? geo.altitude : "",
570 geo.accuracy ? geo.accuracy : ""); 571 geo.accuracy ? geo.accuracy : "");
571 } 572 }
573
574 #ifdef HAVE_SILCMIME_H
575 /* Returns MIME type of filetype */
576
577 char *silcgaim_file2mime(const char *filename)
578 {
579 const char *ct;
580
581 ct = strrchr(filename, '.');
582 if (!ct)
583 return NULL;
584 else if (!strcasecmp(".png", ct))
585 return strdup("image/png");
586 else if (!strcasecmp(".jpg", ct))
587 return strdup("image/jpeg");
588 else if (!strcasecmp(".jpeg", ct))
589 return strdup("image/jpeg");
590 else if (!strcasecmp(".gif", ct))
591 return strdup("image/gif");
592 else if (!strcasecmp(".tiff", ct))
593 return strdup("image/tiff");
594
595 return NULL;
596 }
597
598 /* Checks if message has images, and assembles MIME message if it has.
599 If only one image is present, creates simple MIME image message. If
600 there are multiple images and/or text with images multipart MIME
601 message is created. */
602
603 SilcDList silcgaim_image_message(const char *msg, SilcUInt32 *mflags)
604 {
605 SilcMime mime = NULL, p;
606 SilcDList list, parts = NULL;
607 const char *start, *end, *last;
608 GData *attribs;
609 char *type;
610 gboolean images = FALSE;
611
612 last = msg;
613 while (last && *last && gaim_markup_find_tag("img", last, &start,
614 &end, &attribs)) {
615 GaimStoredImage *image = NULL;
616 const char *id;
617
618 /* Check if there is text before image */
619 if (start - last) {
620 char *text, *tmp;
621 p = silc_mime_alloc();
622
623 /* Add content type */
624 silc_mime_add_field(p, "Content-Type",
625 "text/plain; charset=utf-8");
626
627 tmp = g_strndup(last, start - last);
628 text = gaim_unescape_html(tmp);
629 g_free(tmp);
630 /* Add text */
631 silc_mime_add_data(p, text, strlen(text));
632 g_free(text);
633
634 if (!parts)
635 parts = silc_dlist_init();
636 silc_dlist_add(parts, p);
637 }
638
639 id = g_datalist_get_data(&attribs, "id");
640 if (id && (image = gaim_imgstore_get(atoi(id)))) {
641 unsigned long imglen = gaim_imgstore_get_size(image);
642 gpointer img = gaim_imgstore_get_data(image);
643
644 p = silc_mime_alloc();
645
646 /* Add content type */
647 type = silcgaim_file2mime(gaim_imgstore_get_filename(image));
648 if (!type) {
649 g_datalist_clear(&attribs);
650 last = end + 1;
651 continue;
652 }
653 silc_mime_add_field(p, "Content-Type", type);
654 silc_free(type);
655
656 /* Add content transfer encoding */
657 silc_mime_add_field(p, "Content-Transfer-Encoding", "binary");
658
659 /* Add image data */
660 silc_mime_add_data(p, img, imglen);
661
662 if (!parts)
663 parts = silc_dlist_init();
664 silc_dlist_add(parts, p);
665 images = TRUE;
666 }
667
668 g_datalist_clear(&attribs);
669
670 /* Continue after tag */
671 last = end + 1;
672 }
673
674 /* Check for text after the image(s) */
675 if (images && last && *last) {
676 char *tmp = gaim_unescape_html(last);
677 p = silc_mime_alloc();
678
679 /* Add content type */
680 silc_mime_add_field(p, "Content-Type",
681 "text/plain; charset=utf-8");
682
683 /* Add text */
684 silc_mime_add_data(p, tmp, strlen(tmp));
685 g_free(tmp);
686
687 if (!parts)
688 parts = silc_dlist_init();
689 silc_dlist_add(parts, p);
690 }
691
692 /* If there weren't any images, don't return anything. */
693 if (!images) {
694 if (parts)
695 silc_dlist_uninit(parts);
696 return NULL;
697 }
698
699 if (silc_dlist_count(parts) > 1) {
700 /* Multipart MIME message */
701 char b[32];
702 mime = silc_mime_alloc();
703 silc_mime_add_field(mime, "MIME-Version", "1.0");
704 g_snprintf(b, sizeof(b), "b%4X%4X",
705 (unsigned int)time(NULL),
706 silc_dlist_count(parts));
707 silc_mime_set_multipart(mime, "mixed", b);
708 silc_dlist_start(parts);
709 while ((p = silc_dlist_get(parts)) != SILC_LIST_END)
710 silc_mime_add_multipart(mime, p);
711 } else {
712 /* Simple MIME message */
713 silc_dlist_start(parts);
714 mime = silc_dlist_get(parts);
715 silc_mime_add_field(mime, "MIME-Version", "1.0");
716 }
717
718 *mflags &= ~SILC_MESSAGE_FLAG_UTF8;
719 *mflags |= SILC_MESSAGE_FLAG_DATA;
720
721 /* Encode message. Fragment if it is too large */
722 list = silc_mime_encode_partial(mime, 0xfc00);
723
724 silc_dlist_uninit(parts);
725
726 /* Added multiparts gets freed here */
727 silc_mime_free(mime);
728
729 return list;
730 }
731
732 #endif /* HAVE_SILCMIME_H */