Redbrick User management tool
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

284 lines
4.9 KiB

  1. #!/usr/bin/perl
  2. # Perl Sucks The Proverbial Donkey Dick.
  3. use POSIX;
  4. $USERDB = "./users";
  5. $AUTOFLUSH = 1;
  6. $CLEAR = `clear`;
  7. $LINE = "===========================================\n";
  8. $LOG = "./voters.log";
  9. $COLORS = 1;
  10. my %stats;
  11. print $CLEAR;
  12. print "Redbrick voting system\n";
  13. print $LINE;
  14. print "Reading userdb...";
  15. open(IN, $USERDB) or error("Could not open userdb");
  16. my %users;
  17. my %registered;
  18. %usertypes = (100 => 'committe', 103 => 'member', 109 => 'staff', 107 => 'associat', 108 => 'guest');
  19. $total = 0;
  20. $user = "";
  21. $usertype = "";
  22. $yearsPaid = "";
  23. $id = "";
  24. $email = "";
  25. $cn = "";
  26. foreach $line (<IN>)
  27. {
  28. if ($line =~ /^dn: uid=(.+),ou=accounts,o=redbrick/) {
  29. $user = $1;
  30. #print "\n$1|";
  31. } elsif ($line =~ /^\s*$/) {
  32. if ($user and $yearsPaid > 0 and ($usertype eq "member" or $usertype eq "staff" or $usertype eq "committe")) {
  33. $total++;
  34. #print "\n|$user|$yearsPaid|$id|$usertype|$cn|$email\n";
  35. my @tmp = ($user, $usertype, $cn, '', $email, $id, '', '', $yearsPaid);
  36. $users{$id} = \@tmp;
  37. }
  38. $user = "";
  39. } elsif ($user) {
  40. if ($line =~ /^gidNumber: (.+)$/) {
  41. #print "$1|";
  42. $usertype = $usertypes{$1} or "non-voter";
  43. } elsif ($line =~ /^yearsPaid: (.+)$/) {
  44. #print "$1|";
  45. $yearsPaid = $1;
  46. } elsif ($line =~ /^cn: (.+)$/) {
  47. #print "$1|";
  48. $cn = $1;
  49. } elsif ($line =~ /^id: (.+)$/) {
  50. #print "$1|";
  51. $id = $1;
  52. } elsif ($line =~ /^altmail: (.+)$/) {
  53. #print "$1|";
  54. $email = $1;
  55. }
  56. }
  57. }
  58. $quorom = ceil(sqrt($total));
  59. close IN;
  60. print "done\n";
  61. if(-e $LOG)
  62. {
  63. while(!($ans =~ /^[yY]/) and !($ans =~ /^[nN]/))
  64. {
  65. print "\nA voting log has been found, do you want to register all the voters\nfrom this log? [Y/N]: ";
  66. $ans = <STDIN>
  67. }
  68. if($ans =~ /^[yY]/)
  69. {
  70. readStatus();
  71. }
  72. else
  73. {
  74. system("rm $LOG") and warning("Could not remove voter log");
  75. }
  76. }
  77. do
  78. {
  79. print "Please enter a student number/card-swipe/username: ";
  80. $input = <STDIN>;
  81. chomp $input;
  82. print $CLEAR;
  83. if($input =~ /^\;\d{13}\?$/)
  84. {
  85. # Read the format the Redbrick Magreader produces
  86. $input = substr($input, 3, 8);
  87. }
  88. elsif(!($input =~ /^\d{8}$/))
  89. {
  90. # Read a username
  91. # Search database for the user
  92. foreach $key (keys %users)
  93. {
  94. if($users{$key}[0] eq "$input")
  95. {
  96. $input = $key;
  97. last;
  98. }
  99. }
  100. }
  101. # At this stage if I don't have the student number something bad
  102. # has happened
  103. if($input =~ /^\d{8}$/)
  104. {
  105. printDetails($input);
  106. checkEligibility($input);
  107. if(!exists($registered{$input}))
  108. {
  109. saveStatus($input);
  110. doStats($input);
  111. }
  112. else
  113. {
  114. warning("Duplicate entry");
  115. }
  116. printStats();
  117. $registered{"$input"} = 1;
  118. }
  119. else
  120. {
  121. warning("Invalid entry or entry not found");
  122. printStats();
  123. }
  124. }
  125. while(1);
  126. #================ SUBROUTINES ==================#
  127. sub trim($)
  128. {
  129. $input = $_[0];
  130. $input =~ s/^\s+//;
  131. $input =~ s/\s+$//;
  132. return $input;
  133. }
  134. sub printStats()
  135. {
  136. print $LINE;
  137. foreach $type ("member", "committe", "staff", "associat", "guest")
  138. {
  139. print ("$type: " . ($stats{"$type"} or 0). "\n");
  140. }
  141. print $LINE;
  142. print ("Paid eligible voters: " . (($stats{"member"} + $stats{"staff"} + $stats{"committe"}) or 0) . "\n");
  143. print ("Quorom: $quorom\n");
  144. }
  145. sub doStats($)
  146. {
  147. $input = $_[0];
  148. if(exists($users{"$input"}))
  149. {
  150. @bits = @{$users{"$input"}};
  151. $usertype = lc($bits[1]);
  152. $stats{"$usertype"}++ if($usertype ne "");
  153. }
  154. }
  155. sub printDetails($)
  156. {
  157. unless(exists $users{$_[0]} and ref($users{$_[0]}) eq "ARRAY")
  158. {
  159. warning("User is not a redbrick member");
  160. return 0;
  161. }
  162. @bits = @{$users{$_[0]}};
  163. print $LINE;
  164. print "Username: $bits[0]\n";
  165. print "Realname: $bits[2]\n";
  166. print "Usertype: $bits[1]\n";
  167. print "Email: $bits[4]\n";
  168. print "Years paid: $bits[8]\n";
  169. print "Number: $bits[5]\n";
  170. print $LINE;
  171. }
  172. sub saveStatus($)
  173. {
  174. $id = "$_[0]";
  175. if(exists($users{$id}))
  176. {
  177. @bits = @{$users{$id}};
  178. open(OUT, ">>$LOG");
  179. print OUT "$bits[5] $bits[0]\n";
  180. close OUT;
  181. }
  182. }
  183. sub readStatus
  184. {
  185. open(IN, "<$LOG") or return 0;
  186. foreach $line (<IN>)
  187. {
  188. chomp $line;
  189. if($line =~ /\s*(\d{8})\s/)
  190. {
  191. if(!exists($registered{$1}))
  192. {
  193. doStats($1);
  194. $registered{$1} = 1;
  195. }
  196. else
  197. {
  198. warning("Duplicate student number found in log file!");
  199. }
  200. }
  201. }
  202. close IN;
  203. printStats();
  204. return 1;
  205. }
  206. sub checkEligibility($)
  207. {
  208. @details = @{$users{$_[0]}};
  209. $failure = "";
  210. if(!(exists($users{$_[0]})))
  211. {
  212. $failure .= "(Not member) "
  213. }
  214. else
  215. {
  216. if($details[8] <= 0)
  217. {
  218. $failure .= "(Not paid up) ";
  219. }
  220. unless((lc($details[1]) eq "member" or lc($details[1]) eq "staff" or lc($details[1]) eq "committe"))
  221. {
  222. $failure .= "(Wrong usertype)";
  223. }
  224. }
  225. if($COLORS) {
  226. print ("Vote: " . ($failure eq "" ? "YES\n" : "NO $failure\n"));
  227. }
  228. else {
  229. print ("Vote: " . ($failure eq "" ? "YES\n" : "NO $failure\n"));
  230. }
  231. }
  232. sub error($)
  233. {
  234. die($_[0]);
  235. }
  236. sub warning($)
  237. {
  238. if($COLORS)
  239. {
  240. print "WARNING: $_[0]\n";
  241. }
  242. else
  243. {
  244. print "WARNING: $_[0]\n";
  245. }
  246. }