comparison libnnchat.c @ 106:c587a99e2096

Drop internal use and storage of encoded usernames.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 22 Oct 2010 12:07:15 +0300
parents eaa524e153f9
children f323b137ca08
comparison
equal deleted inserted replaced
105:a094a9b8cad9 106:c587a99e2096
631 631
632 return NULL; 632 return NULL;
633 } 633 }
634 634
635 635
636 nn_user_t *nn_user_find_enc(const nn_userhash_t *list, const char *encname) 636 nn_user_t *nn_user_find(const nn_userhash_t *list, const char *name)
637 { 637 {
638 uint8_t hash; 638 uint8_t hash;
639 639
640 if (list == NULL) return NULL; 640 if (list == NULL) return NULL;
641 641
642 hash = nn_hash_user(encname); 642 hash = nn_hash_user(name);
643 if (list->buckets[hash] != NULL) { 643 if (list->buckets[hash] != NULL) {
644 nn_user_t *curr = list->buckets[hash]; 644 nn_user_t *curr = list->buckets[hash];
645 while (curr != NULL) { 645 while (curr != NULL) {
646 if (strcasecmp(curr->encname, encname) == 0) 646 if (strcasecmp(curr->name, name) == 0)
647 return curr; 647 return curr;
648 curr = curr->next; 648 curr = curr->next;
649 } 649 }
650 } 650 }
651 651
652 return NULL; 652 return NULL;
653 } 653 }
654 654
655 655
656 nn_user_t *nn_user_match(const nn_userhash_t *list, const char *str, const char *current) 656 nn_user_t *nn_user_match(const nn_userhash_t *list, const char *pattern, const char *current)
657 { 657 {
658 uint8_t hash; 658 uint8_t hash;
659 659
660 if (list == NULL) return NULL; 660 if (list == NULL) return NULL;
661 661
662 hash = nn_hash_user(str); 662 hash = nn_hash_user(pattern);
663 if (list->buckets[hash] != NULL) { 663 if (list->buckets[hash] != NULL) {
664 nn_user_t *curr = list->buckets[hash]; 664 nn_user_t *curr = list->buckets[hash];
665 int len = strlen(str); 665 size_t len = strlen(pattern);
666 666
667 if (current != NULL) { 667 if (current != NULL) {
668 nn_user_t *found = NULL; 668 nn_user_t *found = NULL;
669 while (curr != NULL) { 669 while (curr != NULL) {
670 if (strcasecmp(curr->name, current) == 0) { 670 if (strcasecmp(curr->name, current) == 0) {
678 else 678 else
679 curr = list->buckets[hash]; 679 curr = list->buckets[hash];
680 } 680 }
681 681
682 while (curr != NULL) { 682 while (curr != NULL) {
683 if (strncasecmp(curr->name, str, len) == 0) 683 if (len <= strlen(curr->name) && strncasecmp(curr->name, pattern, len) == 0)
684 return curr; 684 return curr;
685 curr = curr->next; 685 curr = curr->next;
686 } 686 }
687 } 687 }
688 688
694 { 694 {
695 return th_calloc(1, sizeof(nn_userhash_t)); 695 return th_calloc(1, sizeof(nn_userhash_t));
696 } 696 }
697 697
698 698
699 int nn_userhash_insert(nn_userhash_t *list, const char *encname) 699 int nn_userhash_insert(nn_userhash_t *list, const char *name)
700 { 700 {
701 uint8_t hash; 701 uint8_t hash;
702 nn_user_t *user; 702 nn_user_t *user;
703 703
704 /* Check arguments */ 704 /* Check arguments */
705 if (list == NULL || encname == NULL) 705 if (list == NULL || name == NULL)
706 return -1; 706 return -1;
707 707
708 /* Check if username is already there */ 708 /* Check if username is already there */
709 if (nn_user_find_enc(list, encname) != NULL) 709 if (nn_user_find(list, name) != NULL)
710 return 1; 710 return 1;
711 711
712 /* No, we'll add it */ 712 /* No, we'll add it */
713 if ((user = th_calloc(1, sizeof(nn_user_t))) == NULL) 713 if ((user = th_calloc(1, sizeof(nn_user_t))) == NULL)
714 return -3; 714 return -3;
715 715
716 user->encname = th_strdup(encname); 716 user->name = th_strdup(name);
717 user->name = nn_dbldecode_str(encname); 717 if (user->name == NULL)
718 if (user->encname == NULL || user->name == NULL)
719 return -4; 718 return -4;
720 719
721 hash = nn_hash_user(encname); 720 hash = nn_hash_user(name);
722 nn_user_insert(&(list->buckets[hash]), user); 721 nn_user_insert(&(list->buckets[hash]), user);
723 722
724 return 0; 723 return 0;
725 } 724 }
726 725
727 726
728 int nn_userhash_delete(nn_userhash_t *list, const char *encname) 727 int nn_userhash_delete(nn_userhash_t *list, const char *name)
729 { 728 {
730 uint8_t hash; 729 uint8_t hash;
731 730
732 /* Check arguments */ 731 /* Check arguments */
733 if (list == NULL || encname == NULL) 732 if (list == NULL || name == NULL)
734 return -1; 733 return -1;
735 734
736 /* Check if username is already there */ 735 /* Check if username is already there */
737 hash = nn_hash_user(encname); 736 hash = nn_hash_user(name);
738 if (list->buckets[hash] != NULL) { 737 if (list->buckets[hash] != NULL) {
739 nn_user_t *curr, *prev; 738 nn_user_t *curr, *prev;
740 curr = list->buckets[hash]; 739 curr = list->buckets[hash];
741 prev = NULL; 740 prev = NULL;
742 while (curr != NULL) { 741 while (curr != NULL) {
743 if (strcasecmp(curr->encname, encname) == 0) { 742 if (strcasecmp(curr->name, name) == 0) {
744 if (prev) 743 if (prev)
745 prev->next = curr->next; 744 prev->next = curr->next;
746 else 745 else
747 list->buckets[hash] = curr->next; 746 list->buckets[hash] = curr->next;
748 747
768 767
769 if ((user = th_calloc(1, sizeof(nn_user_t))) == NULL) 768 if ((user = th_calloc(1, sizeof(nn_user_t))) == NULL)
770 return NULL; 769 return NULL;
771 770
772 /* Copy relevant data */ 771 /* Copy relevant data */
773 user->encname = th_strdup(src->encname);
774 user->name = th_strdup(src->name); 772 user->name = th_strdup(src->name);
775 user->lastspoke = src->lastspoke; 773 user->lastspoke = src->lastspoke;
776 user->joined = src->joined; 774 user->joined = src->joined;
777 775
778 return user; 776 return user;
779 } 777 }
780 778
781 779
782 void nn_user_free(nn_user_t *user) 780 void nn_user_free(nn_user_t *user)
783 { 781 {
784 th_free(user->encname);
785 th_free(user->name); 782 th_free(user->name);
786 th_free(user); 783 th_free(user);
787 } 784 }
788 785
789 786