Skip to content
This repository has been archived by the owner on Nov 18, 2024. It is now read-only.

Commit

Permalink
Primo commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vlemazza committed Mar 8, 2023
1 parent 4d4f2e1 commit c92257e
Show file tree
Hide file tree
Showing 13 changed files with 307 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
CC = gcc
CFLAGS = -Wall

DEPS = src/utils/list.h src/email.h src/load_fileitem.h src/validation.h
OBJ = src/utils/list.o src/email.o src/load_fileitem.o src/validation.o src/main.o

%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)

lugbar: $(OBJ)
$(CC) -o $@ $^ $(CFLAGS)

.PHONY: clean

clean:
rm -f $(OBJ) lugbar *~

install:
mkdir -p /etc/lugbar
wget https://raw.githubusercontent.com/Roma2Lug-Projects/NFC-Emulator/main/src/config/products.txt -O /etc/lugbar/item.txt
cp lugbar /usr/local/bin/
chmod u+x /usr/local/bin/lugbar

uninstall:
rm -rf /usr/local/bin/lugbar
rm -rf /etc/lugbar/item.txt
8 changes: 8 additions & 0 deletions lugbar_0.0-1/DEBIAN/control
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Package: lugbar
Version: 0.0-1
Section: base
Priority: optional
Architecture: amd64
Depends: msmtp, wget
Maintainer: Valerio
Description: lugbar
7 changes: 7 additions & 0 deletions lugbar_0.0-1/DEBIAN/postinst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

mkdir -p /etc/lugbar
wget https://raw.githubusercontent.com/Roma2Lug-Projects/NFC-Emulator/main/src/config/products.txt -O /etc/lugbar/item.txt

chmod u+x /usr/local/bin/lugbar
chmod 775 /etc/lugbar/item.txt
Binary file added lugbar_0.0-1/usr/local/bin/lugbar
Binary file not shown.
13 changes: 13 additions & 0 deletions src/email.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void sendemail(char *item) {

char command[100];

sprintf(command, "printf 'Subject:%s' | msmtp -a lugbar [email protected]", item);
if(system(command) == -1) {
perror("Error: ");
}
}
1 change: 1 addition & 0 deletions src/email.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int sendemail(char *item);
22 changes: 22 additions & 0 deletions src/load_fileitem.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils/list.h"

struct head_node *loadfile(){

char const* const file_name = "/etc/lugbar/item.txt";
FILE* file = fopen(file_name, "r");
char line[32];

struct head_node *item_list = create_list();

while (fgets(line, sizeof(line), file)) {

insert_node(&item_list, line);
}

fclose(file);

return item_list;
}
1 change: 1 addition & 0 deletions src/load_fileitem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
struct head_node *loadfile();
70 changes: 70 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "load_fileitem.h"
#include "email.h"
#include "validation.h"
#include "utils/list.h"

int main(int argc, char **argv) {

int i = 0;

if (valid() == -1) {
return 0;
}

struct head_node *item_list = loadfile();
struct node *current = item_list->next;
int max_item = item_list->count;

if(argc == 1) {
printf("Uso: %s [OPZIONE]...[NUMERO] ...\n\nPer ordinare usa %s [NUMERO]\nOpzioni disponibili:\n--lista\tlista gli elementi col corrispettivo numero di ordinazione\n", argv[0], argv[0]);
return 0;
}

else if (strcmp(argv[1], "--lista") == 0) {
while(i++ < max_item){
printf("%d. %s\n", i, current->item_name);
current = current->next;
}
return 0;
}

else if (strcmp(argv[1], "versamento") == 0) {
if (argc < 3) {
printf("Assente l'importo del versamento\n");
}
else {
char subject[16];
sprintf(subject, "versamento %s", argv[2]);

if(!sendemail(subject)) {
printf("Versato %s\n", argv[2]);
}
}
}

else if (atoi(argv[1]) > max_item) {
puts("Acquisto non valido. Usa lugbar -lista per listare gli elementi");
return 0;
}

else if (atoi(argv[1]) < max_item && atoi(argv[1]) > 0) {
char *choice_item = lookup_list(&current, atoi(argv[1])-1);
char subject[16];
sprintf(subject, "acquisto %s", choice_item);
if(!sendemail(subject)) {
printf("Acquistato %s\n", choice_item);

}
}

else {
printf("Uso: %s [OPZIONE]...[NUMERO] ...\n\nPer ordinare usa %s [NUMERO]\n\nOpzioni disponibili:\n--lista lista gli elementi col corrispettivo numero di ordinazione\n", argv[0], argv[0]);
return 0;
}

return 0;
}
58 changes: 58 additions & 0 deletions src/utils/list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"

char *lookup_list(struct node **first_node, int p) {
struct node *cursor;
cursor = *first_node;
while(cursor) {
if(cursor->pos != p) {
cursor = cursor->next;
}
else {
return cursor->item_name;
}
}

return NULL;
}

struct head_node *create_list(){

struct head_node *temp;
temp = malloc(sizeof(struct head_node));
temp->count = 0;
temp->next = NULL;

return temp;
}

void insert_node(struct head_node **head, char *item) {

struct node *temp = malloc(sizeof(struct node));
strcpy(temp->item_name, item);
temp->next=NULL;
int i = 1;

if((*head)->next == NULL) {
(*head)->next = temp;
(*head)->count = 1;
}

else {

struct node *current;
current = (*head)->next;
while(current->next!=NULL) {
i++;
current=current->next;
}

temp->pos = i;
current->next=temp;
(*head)->count = ((*head)->count) + 1;


}
}
15 changes: 15 additions & 0 deletions src/utils/list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
struct node {
int pos;
char item_name[32];
struct node* next;
};

struct head_node {
int count;
struct node* next;
};

void insert_node(struct head_node **head, char *item);
struct head_node *create_list();
char *lookup_list(struct node **first_node, int p);

82 changes: 82 additions & 0 deletions src/validation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

int check_fileitem(){

char* fullpath = "/etc/lugbar/item.txt";

FILE *file;

if ((file = fopen(fullpath, "r")) == NULL) {
if (errno == ENOENT) {
return -1;
}
}

fclose(file);
return 1;
}

int check_msmtp(){
if (system("which msmtp > /dev/null 2>&1")){
return -1;
}
return 1;
}

int check_config(){
char* fullpath = "/etc/msmtprc";

FILE *file;

if ((file = fopen(fullpath, "r")) == NULL) {
if (errno == ENOENT) {
return -1;
}
}

fclose(file);
return 1;
}



int valid(){

if(check_fileitem() == -1) {
printf("File /etc/lugbar/item.txt assente\n");
return -1;
}
if(check_msmtp() == -1) {
printf("Programma msmtp non installato\n");
return -1;
}

if(check_config() == -1) {
printf("File in /etc/msmtprc assente\n");
printf("Esempio file:\n\n"
"defaults\n"
"syslog LOG_MAIL\n"
"auth on\n"
"tls on\n"
"aliases /etc/aliases\n"
"# Example with GMAIL\n"
"account lugbar\n"
"host smtp.gmail.com\n"
"port 465\n"
"tls on\n"
"tls_starttls off\n"
"from \"tuo nome\"\n"
"user [email protected]\n"
"password \"password\"""\n"
"set_from_header on\n\n"
"Se usi Gmail devi generare una password per app https://myaccount.google.com/apppasswords\n");
return -1;
}

return 1;

}
4 changes: 4 additions & 0 deletions src/validation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int check_fileitem();
int check_msmtp();
int check_config();
int valid();

0 comments on commit c92257e

Please sign in to comment.