session.c
changeset 37 e3dccf824f91
parent 34 a52328aa693e
child 38 058e5ad9ba79
equal deleted inserted replaced
34:a52328aa693e 37:e3dccf824f91
    92 static void pop3_debug(char *, ...);
    92 static void pop3_debug(char *, ...);
    93 static void session_write(struct session *, const char *, size_t);
    93 static void session_write(struct session *, const char *, size_t);
    94 static const char *strstate(enum state);
    94 static const char *strstate(enum state);
    95 
    95 
    96 struct session_tree	sessions;
    96 struct session_tree	sessions;
    97 static int		_pop3_debug = 1;
    97 static int		_pop3_debug = 0;
    98 
    98 
    99 void
    99 void
   100 session_init(struct listener *l, int fd)
   100 session_init(struct listener *l, int fd)
   101 {
   101 {
   102 	struct session	*s;
   102 	struct session	*s;
   145 			iobuf_flush(&entry->iobuf, entry->io.sock);
   145 			iobuf_flush(&entry->iobuf, entry->io.sock);
   146 	}
   146 	}
   147 
   147 
   148 	io_clear(&entry->io);
   148 	io_clear(&entry->io);
   149 	iobuf_clear(&entry->iobuf);
   149 	iobuf_clear(&entry->iobuf);
   150 	imsgev_clear(&entry->iev_maildrop);
   150 	imsgev_clear(entry->iev_maildrop);
       
   151 	entry->iev_maildrop->terminate = 1;
   151 	logit(LOG_INFO, "%u: session closed", entry->id);
   152 	logit(LOG_INFO, "%u: session closed", entry->id);
   152 	free(entry);
   153 	free(entry);
   153 }
   154 }
   154 
   155 
   155 static void
   156 static void
   371 		retr_req.ntop = n;
   372 		retr_req.ntop = n;
   372 		/* FALLTRHROUGH */
   373 		/* FALLTRHROUGH */
   373 	case CMD_RETR:
   374 	case CMD_RETR:
   374 		if (!get_index(s, args, &retr_req.idx))
   375 		if (!get_index(s, args, &retr_req.idx))
   375 			break;
   376 			break;
   376 		imsgev_xcompose(&s->iev_maildrop, IMSG_MAILDROP_RETR,
   377 		imsgev_xcompose(s->iev_maildrop, IMSG_MAILDROP_RETR,
   377 		    s->id, 0, -1, &retr_req, sizeof(retr_req), "trans_command");
   378 		    s->id, 0, -1, &retr_req, sizeof(retr_req), "trans_command");
   378 		return;
   379 		return;
   379 	case CMD_NOOP:
   380 	case CMD_NOOP:
   380 		session_reply(s, "%s", "+OK");
   381 		session_reply(s, "%s", "+OK");
   381 		break;
   382 		break;
   382 	case CMD_DELE:
   383 	case CMD_DELE:
   383 		if (!get_index(s, args, &idx))
   384 		if (!get_index(s, args, &idx))
   384 			break;
   385 			break;
   385 		imsgev_xcompose(&s->iev_maildrop, IMSG_MAILDROP_DELE,
   386 		imsgev_xcompose(s->iev_maildrop, IMSG_MAILDROP_DELE,
   386 		    s->id, 0, -1, &idx, sizeof(idx), "trans_command");
   387 		    s->id, 0, -1, &idx, sizeof(idx), "trans_command");
   387 		return;
   388 		return;
   388 	case CMD_RSET:
   389 	case CMD_RSET:
   389 		imsgev_xcompose(&s->iev_maildrop, IMSG_MAILDROP_RSET,
   390 		imsgev_xcompose(s->iev_maildrop, IMSG_MAILDROP_RSET,
   390 		    s->id, 0, -1, NULL, 0, "trans_command");
   391 		    s->id, 0, -1, NULL, 0, "trans_command");
   391 		return;
   392 		return;
   392 	case CMD_UIDL:
   393 	case CMD_UIDL:
   393 		uidl = 1;
   394 		uidl = 1;
   394 		/* FALLTHROUGH */
   395 		/* FALLTHROUGH */
   399 			get_list(s, idx, uidl);
   400 			get_list(s, idx, uidl);
   400 		} else
   401 		} else
   401 			get_list_all(s, uidl);
   402 			get_list_all(s, uidl);
   402 		return;
   403 		return;
   403 	case CMD_QUIT:
   404 	case CMD_QUIT:
   404 		imsgev_xcompose(&s->iev_maildrop, IMSG_MAILDROP_UPDATE,
   405 		imsgev_xcompose(s->iev_maildrop, IMSG_MAILDROP_UPDATE,
   405 		    s->id, 0, -1, NULL, 0, "trans_command");
   406 		    s->id, 0, -1, NULL, 0, "trans_command");
   406 		session_set_state(s, UPDATE);
   407 		session_set_state(s, UPDATE);
   407 		return;
   408 		return;
   408 	default:
   409 	default:
   409 		session_reply(s, "%s", "-ERR invalid command");
   410 		session_reply(s, "%s", "-ERR invalid command");
   416 static void
   417 static void
   417 get_list_all(struct session *s, int uidl)
   418 get_list_all(struct session *s, int uidl)
   418 {
   419 {
   419 	io_pause(&s->io, IO_PAUSE_IN);
   420 	io_pause(&s->io, IO_PAUSE_IN);
   420 	session_reply(s, "+OK");
   421 	session_reply(s, "+OK");
   421 	imsgev_xcompose(&s->iev_maildrop, IMSG_MAILDROP_LISTALL,
   422 	imsgev_xcompose(s->iev_maildrop, IMSG_MAILDROP_LISTALL,
   422 	    s->id, 0, -1, &uidl, sizeof(uidl), "list_all");
   423 	    s->id, 0, -1, &uidl, sizeof(uidl), "list_all");
   423 }
   424 }
   424 
   425 
   425 static void
   426 static void
   426 get_list(struct session *s, unsigned int i, int uidl)
   427 get_list(struct session *s, unsigned int i, int uidl)
   427 {
   428 {
   428 	struct list_req	req;
   429 	struct list_req	req;
   429 
   430 
   430 	req.idx = i;
   431 	req.idx = i;
   431 	req.uidl = uidl;
   432 	req.uidl = uidl;
   432 	imsgev_xcompose(&s->iev_maildrop, IMSG_MAILDROP_LIST,
   433 	imsgev_xcompose(s->iev_maildrop, IMSG_MAILDROP_LIST,
   433 	    s->id, 0, -1, &req, sizeof(req), "list");
   434 	    s->id, 0, -1, &req, sizeof(req), "list");
   434 }
   435 }
   435 
   436 
   436 void
   437 void
   437 session_imsgev_init(struct session *s, int fd)
   438 session_imsgev_init(struct session *s, int fd)
   438 {
   439 {
   439 	imsgev_init(&s->iev_maildrop, fd, NULL, maildrop_imsgev, needfd);
   440 	s->iev_maildrop = xcalloc(1, sizeof(struct imsgev),
       
   441 	    "session_imsgev_init");
       
   442 	imsgev_init(s->iev_maildrop, fd, NULL, maildrop_imsgev, needfd);
   440 }
   443 }
   441 
   444 
   442 static void
   445 static void
   443 maildrop_imsgev(struct imsgev *iev, int code, struct imsg *imsg)
   446 maildrop_imsgev(struct imsgev *iev, int code, struct imsg *imsg)
   444 {
   447 {
   483 		break;
   486 		break;
   484 	case IMSGEV_EREAD:
   487 	case IMSGEV_EREAD:
   485 	case IMSGEV_EWRITE:
   488 	case IMSGEV_EWRITE:
   486 	case IMSGEV_EIMSG:
   489 	case IMSGEV_EIMSG:
   487 		fatal("session: imsgev read/write error");
   490 		fatal("session: imsgev read/write error");
       
   491 		break;
       
   492 	case IMSGEV_DONE:
       
   493 		free(iev);
   488 		break;
   494 		break;
   489 	}
   495 	}
   490 }
   496 }
   491 
   497 
   492 static void
   498 static void