|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/emersion/go-imap" |
| 7 | + "github.com/emersion/go-imap/client" |
| 8 | + "github.com/julien040/anyquery/rpc" |
| 9 | +) |
| 10 | + |
| 11 | +// A constructor to create a new table instance |
| 12 | +// This function is called everytime a new connection is made to the plugin |
| 13 | +// |
| 14 | +// It should return a new table instance, the database schema and if there is an error |
| 15 | +func foldersCreator(args rpc.TableCreatorArgs) (rpc.Table, *rpc.DatabaseSchema, error) { |
| 16 | + config, err := getArgs(args.UserConfig) |
| 17 | + if err != nil { |
| 18 | + return nil, nil, err |
| 19 | + } |
| 20 | + |
| 21 | + dialer, err := client.DialTLS(fmt.Sprintf("%s:%d", config.Host, config.Port), nil) |
| 22 | + if err != nil { |
| 23 | + return nil, nil, fmt.Errorf("failed to connect to imap server: %v", err) |
| 24 | + } |
| 25 | + |
| 26 | + err = dialer.Login(config.Username, config.Password) |
| 27 | + if err != nil { |
| 28 | + return nil, nil, fmt.Errorf("failed to login to imap server: %v", err) |
| 29 | + } |
| 30 | + |
| 31 | + return &foldersTable{ |
| 32 | + dialer: dialer, |
| 33 | + }, &rpc.DatabaseSchema{ |
| 34 | + PrimaryKey: -1, |
| 35 | + Columns: []rpc.DatabaseSchemaColumn{ |
| 36 | + { |
| 37 | + Name: "folder", |
| 38 | + Type: rpc.ColumnTypeString, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, nil |
| 42 | +} |
| 43 | + |
| 44 | +type foldersTable struct { |
| 45 | + dialer *client.Client |
| 46 | +} |
| 47 | + |
| 48 | +type foldersCursor struct { |
| 49 | + dialer *client.Client |
| 50 | +} |
| 51 | + |
| 52 | +// Return a slice of rows that will be returned to Anyquery and filtered. |
| 53 | +// The second return value is true if the cursor has no more rows to return |
| 54 | +// |
| 55 | +// The constraints are used for optimization purposes to "pre-filter" the rows |
| 56 | +// If the rows returned don't match the constraints, it's not an issue. Anyquery will filter them out |
| 57 | +func (t *foldersCursor) Query(constraints rpc.QueryConstraint) ([][]interface{}, bool, error) { |
| 58 | + folders := make([]string, 0) |
| 59 | + // Get the list of folders |
| 60 | + mailboxes := make(chan *imap.MailboxInfo, 10) |
| 61 | + done := make(chan error, 1) |
| 62 | + go func() { |
| 63 | + done <- t.dialer.List("", "*", mailboxes) |
| 64 | + }() |
| 65 | + |
| 66 | + for m := range mailboxes { |
| 67 | + if m == nil { |
| 68 | + continue |
| 69 | + } |
| 70 | + folders = append(folders, m.Name) |
| 71 | + } |
| 72 | + err := <-done |
| 73 | + if err != nil { |
| 74 | + return nil, true, fmt.Errorf("failed to get folders: %v", err) |
| 75 | + } |
| 76 | + |
| 77 | + // Convert the list of folders to a slice of rows |
| 78 | + rows := make([][]interface{}, 0, len(folders)) |
| 79 | + for _, folder := range folders { |
| 80 | + rows = append(rows, []interface{}{folder}) |
| 81 | + } |
| 82 | + |
| 83 | + return rows, true, nil |
| 84 | +} |
| 85 | + |
| 86 | +// Create a new cursor that will be used to read rows |
| 87 | +func (t *foldersTable) CreateReader() rpc.ReaderInterface { |
| 88 | + return &foldersCursor{ |
| 89 | + dialer: t.dialer, |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// A slice of rows to insert |
| 94 | +func (t *foldersTable) Insert(rows [][]interface{}) error { |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +// A slice of rows to update |
| 99 | +// The first element of each row is the primary key |
| 100 | +// while the rest are the values to update |
| 101 | +// The primary key is therefore present twice |
| 102 | +func (t *foldersTable) Update(rows [][]interface{}) error { |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +// A slice of primary keys to delete |
| 107 | +func (t *foldersTable) Delete(primaryKeys []interface{}) error { |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +// A destructor to clean up resources |
| 112 | +func (t *foldersTable) Close() error { |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +type userConfig struct { |
| 117 | + Username string |
| 118 | + Password string |
| 119 | + Port int |
| 120 | + Host string |
| 121 | +} |
| 122 | + |
| 123 | +func getArgs(args rpc.PluginConfig) (userConfig, error) { |
| 124 | + var config userConfig |
| 125 | + var ok bool |
| 126 | + var rawString string |
| 127 | + var rawFloat float64 |
| 128 | + |
| 129 | + if rawString, ok = args["username"].(string); !ok { |
| 130 | + return config, fmt.Errorf("username is not a string") |
| 131 | + } else if config.Username = rawString; config.Username == "" { |
| 132 | + return config, fmt.Errorf("username is empty") |
| 133 | + } |
| 134 | + |
| 135 | + if rawString, ok = args["password"].(string); !ok { |
| 136 | + return config, fmt.Errorf("password is not a string") |
| 137 | + } else if config.Password = rawString; config.Password == "" { |
| 138 | + return config, fmt.Errorf("password is empty") |
| 139 | + } |
| 140 | + |
| 141 | + if rawString, ok = args["host"].(string); !ok { |
| 142 | + return config, fmt.Errorf("host is not a string") |
| 143 | + } else if config.Host = rawString; config.Host == "" { |
| 144 | + return config, fmt.Errorf("host is empty") |
| 145 | + } |
| 146 | + |
| 147 | + if rawFloat, ok = args["port"].(float64); !ok { |
| 148 | + return config, fmt.Errorf("port is not a number") |
| 149 | + } else if config.Port = int(rawFloat); config.Port == 0 { |
| 150 | + return config, fmt.Errorf("port is 0") |
| 151 | + } |
| 152 | + |
| 153 | + return config, nil |
| 154 | +} |
0 commit comments