[go: up one dir, main page]

File: fakepop.c

package info (click to toggle)
fakepop 10
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 124 kB
  • ctags: 39
  • sloc: ansic: 397; makefile: 82; sh: 26
file content (143 lines) | stat: -rw-r--r-- 3,040 bytes parent folder | download | duplicates (3)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* $Id: fakepop.c,v 1.3 2004/12/01 17:13:02 pzn Exp $ */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>

#include "msg.h"
#include "header.h"
#include "id.h"
#include "retrieve.h"
#include "common.h"

/* process a POP3 command. return 0 if command is "quit" */
int process_cmd (char * cmd) {
  int ret = 1;
  int known = 0;

  if ((strncasecmp ("dele", cmd, 4) == 0) ||
      (strncasecmp ("rset", cmd, 4) == 0) ||
      (strncasecmp ("user", cmd, 4) == 0) ||
      (strncasecmp ("pass", cmd, 4) == 0) ||
      (strncasecmp ("apop", cmd, 4) == 0) ||
      (strncasecmp ("noop", cmd, 4) == 0)){
    known = 1;
    printf("+OK\r\n");
  }
  
  if (strcasecmp ("stat", cmd) == 0) {
    known = 1;
    printf("+OK %d %d\r\n", msg_gettotalmsgs(), msg_gettotalsize());
  }
  
  if (strncasecmp ("list", cmd, 4) == 0) {
    known = 1;
    if (strlen(cmd) > 5) {
      /* single message list */
      int i;
      i = atoi(cmd+5);
      if (msg_exist(i)) {
	printf("+OK %d %d\r\n", i, msg_getsize(i));
      } else {
	printf("-ERR no such message\r\n");
      }
    } else {
      /* all messages list */
      int i, j;
      j = msg_gettotalmsgs();
      printf("+OK\r\n");
      for (i = 1; i <= j; i++) {
	printf("%d %d\r\n", i, msg_getsize(i));
      }
      printf(".\r\n");
    }
  }

  if (strncasecmp ("uidl", cmd, 4) == 0) {
    char s[71];
    int i;
    known = 1;
    if (strlen(cmd) > 5) {
      /* single message uidl */
      i = atoi(cmd+5);
      id_get_uidl (i, s);
      printf("+OK %d %s\r\n",i,s);
    } else {
      /* all messages uidl */
      int j;
      j = msg_gettotalmsgs();
      printf("+OK\r\n");
      for (i = 1; i <= j; i++) {
	id_get_uidl (i, s);
	printf("%d %s\r\n",i,s);
      }
      printf(".\r\n");
    }
  }
  
  if (strncasecmp ("retr", cmd, 4) == 0) {
    int i;
    known = 1;
    i = atoi(cmd+5);
    if (msg_exist(i)) {
      printf("+OK\r\n");
      retrieve_full (i, stdout);
      printf(".\r\n");
    } else {
      printf("-ERR no such message\r\n");
    }
  }

  if (strncasecmp ("top", cmd, 3) == 0) {
    int i = 0, lines = 0;
    known = 1;
    sscanf(cmd+4, "%d %d", &i, &lines);
    if (msg_exist(i)) {
      printf("+OK\r\n");
      retrieve_partial (i, stdout, lines);
      printf(".\r\n");
    } else {
      printf("-ERR no such message\r\n");
    }
  }
  
  if (strcasecmp ("quit", cmd) == 0) {
    known = 1;
    printf("+OK\r\n");
    ret = 0;
  }
  
  if (! known) {
    printf("-ERR Unknown command: \"%s\".\r\n", cmd);
  }
  
  return ret;
}

int main (void) {
  char cmd[CMD_MAX_SIZE+1];
  int i, j, running = 1;

  msg_init();

  printf ("+OK fakepop V%s %s <1234.5678901234@fakepop.invalid>\r\n",
	  PROGRAM_VERSION, URL);
  fflush(stdout);

  while ((running == 1) && (fgets(cmd, CMD_MAX_SIZE, stdin) != NULL)) {
    cmd[CMD_MAX_SIZE-1] = 0;
    j = strlen(cmd);
    for (i=0; i<j; i++) {
      if (cmd[i] < ' ') {
	cmd[i] = 0;
	break;
      }
    }
    running = process_cmd(cmd);
    fflush(stdout);
  }

  return 0;
}