File: userlist.cpp

package info (click to toggle)
postal 0.62.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 476 kB
  • ctags: 445
  • sloc: cpp: 2,902; sh: 289; ansic: 258; makefile: 86
file content (96 lines) | stat: -rw-r--r-- 1,774 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "userlist.h"
#include <stdio.h>
#include "expand.h"

UserList::UserList(const char *userListFile
                 , const char *conversionFile
                 , bool usePassword)
 : m_users(new STR_VEC)
 , m_passwords(new STR_VEC)
 , m_exp(new NameExpand(conversionFile))
 , m_index(0)
 , m_maxNameLen(0)
 , m_primary(true)
{
  FILE *fp = fopen(userListFile, "r");
  if(!fp)
  {
    printf("Can't open \"%s\".\n", userListFile);
    exit(1);
  }
  char buf[1024];
  while(fgets(buf, sizeof(buf), fp) )
  {
    strtok(buf, "\n");
    if(buf[0] && buf[0] != '#' && buf[0] != '\n')
    {
      if(usePassword)
      {
        size_t len = strlen(buf);
        strtok(buf, " ");
        if(len > strlen(buf))
        {
          m_users->push_back(buf);
          m_passwords->push_back(buf + strlen(buf) + 1);
        }
      }
      else
      {
        m_users->push_back(buf);
      }
      if(strlen(buf) > m_maxNameLen)
        m_maxNameLen = strlen(buf);
    }
  }
  if(m_users->size() == 0)
  {
    printf("No users in file.\n");
    exit(1);
  }
  fclose(fp);
}

UserList::UserList(UserList &list)
 : m_users(list.m_users)
 , m_passwords(list.m_passwords)
 , m_exp(list.m_exp)
 , m_index(0)
 , m_maxNameLen(0)
 , m_primary(false)
{
}

UserList::~UserList()
{
  if(m_primary)
  {
    delete m_users;
    delete m_passwords;
    delete m_exp;
  }
}

string UserList::randomUser()
{
  m_index = random() % m_users->size();
  string str;
  m_exp->expand(str, m_users[0][m_index]);
  return str;
}

string UserList::sequentialUser()
{
  string str;
  while(m_exp->expand(str, m_users[0][m_index], true))
  {
    m_index++;
    if(m_index == m_users->size())
      m_index = 0;
  }
  return str;
}

string UserList::password()
{
  return m_passwords[0][m_index];
}