]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/UserInfo.pl
* Accidentally left in a debug output
[infobot.git] / src / Modules / UserInfo.pl
1 #
2 # UserInfo.pl: User Information Services
3 #      Author: dms
4 #     Version: v0.1 (20000509).
5 #     Created: 20000509
6 #        NOTE: Idea from Flugh. Originally written in tcl for eggdrop by
7 #               unknown.
8 #
9
10 use strict;
11
12 my $orderOfInfo = 'RN,J,C,W,D';
13 my %infoDesc    = (
14     'RN' => 'Real Name',
15     'J'  => 'Occupation',
16     'C'  => 'Contact',
17     'W'  => 'URL',
18     'D'  => 'Description',
19 );
20
21 sub UserInfo2Hash {
22     my ($text) = @_;
23     my %hash;
24
25     foreach ( split /\|/, $text ) {
26         if (/^\s*(\S+):\s*(.*)\s*$/) {
27             $hash{$1} = $2;
28         }
29     }
30
31     return %hash;
32 }
33
34 sub Hash2UserInfo {
35     my (%hash) = @_;
36     my @array;
37
38     foreach ( sort keys %hash ) {
39         push( @array, "$_: $hash{$_}" );
40     }
41
42     join( '|', @array );
43 }
44
45 ###
46 ###
47 ###
48
49 sub UserInfoGet {
50     my ($query) = @_;
51     $query =~ s/^\s+|\s+$//g if ( defined $query );
52
53     if ( !defined $query or $query =~ /^$/ ) {
54         &help('userinfo');
55         return;
56     }
57
58     if ( $query !~ /^$mask{nick}$/ ) {
59         &msg( $who, "Invalid query of '$query'." );
60         return;
61     }
62
63     my $result;
64     if ( $result = &getFactoid( $query . ' info' ) ) {
65
66         # good.
67     }
68     else {    # bad.
69         &performReply("No User Information on \002$query\002");
70         return;
71     }
72
73     if ( $result !~ /\|/ ) {
74         &msg( $who, "Invalid User Information for '$query'." );
75         return;
76     }
77
78     my %userInfo = &UserInfo2Hash($result);
79
80     my @reply;
81     foreach ( split ',', $orderOfInfo ) {
82         next unless ( exists $userInfo{$_} );
83         push( @reply, "$infoDesc{$_}: $userInfo{$_}" );
84     }
85
86     &performStrictReply(
87         "User Information on $userInfo{'N'} -- " . join( ', ', @reply ) );
88 }
89
90 sub UserInfoSet {
91     my ( $type, $what ) = @_;
92     my %userInfo;
93     my $info;
94
95     if ( &IsLocked("$who info") ) {
96         &DEBUG("UIS: IsLocked('$who info') == 1.");
97         return;
98     }
99
100     my $new = 0;
101     if ( my $result = &getFactoid("$who info") ) {
102         %userInfo = &UserInfo2Hash($result);
103     }
104     else {
105         &DEBUG("UIS: new = 1!");
106         $userInfo{'N'} = $who;
107         $new = 1;
108     }
109
110     ### TODO: hash for %infoS2L.
111     if ( $type =~ /^(RN|real\s*name)$/i ) {
112         $info = 'RN';
113     }
114     elsif ( $type =~ /^(J|job|occupation|school|life)$/i ) {
115         $info = 'J';
116     }
117     elsif ( $type =~ /^(C|contact|email|phone)$/i ) {
118         $info = 'C';
119     }
120     elsif ( $type =~ /^(W|www|url|web\s*page|home\s*page)$/i ) {
121         $info = 'W';
122     }
123     elsif ( $type =~ /^(D|desc\S+)$/i ) {
124         $info = 'D';
125     }
126     elsif ( $type =~ /^(O|opt\S+)$/i ) {
127         $info = 'O';
128     }
129     else {
130         &msg( $who, "Unknown type '$type'." );
131         return;
132     }
133
134     if ( !defined $what ) {    # !defined.
135         if ( exists $userInfo{$info} ) {
136             &msg( $who,
137                 "Current \002$infoDesc{$info}\002 is: '$userInfo{$info}'." );
138         }
139         else {
140             &msg( $who, "No current \002$infoDesc{$info}\002." );
141         }
142
143         my @remain;
144         foreach ( split ',', $orderOfInfo ) {
145             next if ( exists $userInfo{$_} );
146             push( @remain, $infoDesc{$_} );
147         }
148         if ( scalar @remain ) {
149             ### TODO: show short-cut (identifier) aswell.
150             &msg( $who, "Remaining slots to fill: " . join( ' ', @remain ) );
151         }
152         else {
153 ###         &msg($who, "Personal Information completely filled. Good.");
154         }
155
156         return;
157     }
158     elsif ( $what =~ /^$/ ) {    # defined but NULL. UNSET
159         if ( exists $userInfo{$info} ) {
160             &msg( $who,
161                 "Unsetting \002$infoDesc{$info}\002 ($userInfo{$info})." );
162             delete $userInfo{$info};
163         }
164         else {
165             &msg( $who, "\002$infoDesc{$info}\002 is already empty!" );
166             return;
167         }
168     }
169     else {                       # defined.
170         if ( exists $userInfo{$info} ) {
171             &msg( $who, "\002$infoDesc{$info}\002 was '$userInfo{$info}'." );
172             &msg( $who, "Now is: '$what'." );
173         }
174         else {
175             &msg( $who, "\002$infoDesc{$info}\002 is now '$what'." );
176         }
177         $userInfo{$info} = $what;
178     }
179
180     &setFactInfo( $who . ' info', 'factoid_value', &Hash2UserInfo(%userInfo) );
181     if ($new) {
182         &DEBUG("UIS: locking '$who info'.");
183         &DEBUG("UIS: nuh => '$nuh'.");
184         &setFactInfo( "$who info", "locked_by",   $nuh );
185         &setFactInfo( "$who info", "locked_time", time() );
186     }
187 }
188
189 1;
190
191 # vim:ts=4:sw=4:expandtab:tw=80