File: userlist.cpp

package info (click to toggle)
postal 0.76%2Bnmu2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 552 kB
  • sloc: cpp: 4,201; sh: 289; ansic: 259; makefile: 136
file content (89 lines) | stat: -rw-r--r-- 1,654 bytes parent folder | download | duplicates (6)
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
#include "userlist.h"
#include <cstdlib>
#include <cstring>
#include <stdio.h>
#include "expand.h"

UserList::UserList(const char *userListFile, bool usePass, bool stripDom)
 : m_users(new STR_VEC)
 , m_passwords(NULL)
 , m_index(0)
 , m_maxNameLen(0)
 , m_primary(true)
{
  char buf[1024];
  FILE *fp = fopen(userListFile, "r");
  if(!fp)
  {
    printf("Can't open \"%s\".\n", userListFile);
    exit(1);
  }

  if(usePass)
    m_passwords = new STR_VEC;

  while(fgets(buf, sizeof(buf), fp) )
  {
    strtok(buf, "\n");
    if(buf[0] != '\n' && buf[0] != '\r' && buf[0] != '#')
    {
      strtok(buf, " \t");
      char *pass = strtok(NULL, " \t");
      if(!pass && usePass)
      {
        printf("Need a password for \"%s\".", buf);
        continue;
      }
      if(usePass)
        m_passwords->push_back(buf + strlen(buf) + 1);
      if(stripDom)
        strtok(buf, "@");
      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_index(0)
 , m_maxNameLen(0)
 , m_primary(false)
{
}

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

const string &UserList::randomUser()
{
  m_index = random() % m_users->size();
  return m_users[0][m_index];
}

string UserList::sequentialUser()
{
  m_index++;
  if(m_index == m_users->size())
    m_index = 0;
  return m_users[0][m_index];
}

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