summaryrefslogtreecommitdiffstats
path: root/src/gui/CoreConnector.vala
blob: 1030428d6ae78f71989bac227af852978ee43114 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
public class CoreConnector {
  unowned Thread thread;
  bool running;
  
  Erl.Node node;
  Erl.Connection con;
  Erl.Term self;
  
  Roster roster;
  
  private class TermStore {
    public Erl.Term term;
  }
  
  static construct {
    Erl.init();
  }
  
  public CoreConnector(Roster roster0) {
    running = false;
    roster = roster0;
  }
  
  public bool start() {
    if(running)
      return true;
    
    running = true;
    
    node = Erl.Node("ephraim-gtk", "magiccookie", 0);
    con = node.connect("ephraim-core@avalon.local");
    
    self = Erl.mk_self_pid(node);
    
    con.reg_send("ephraim", Erl.format("{register_ui,~w}", self));
    
    try {
      thread = Thread.create(receive, true);
      return true;
    } catch(ThreadError e) {
      return false;
    }
  }
  
  public void stop() {
    if(!running)
      return;
    
    running = false;
    thread.join();
    
    con.reg_send("ephraim", Erl.format("{unregister_ui,~w}", self));
  }
  
  
  private void* receive() {
    while(running) {
      TermStore response = new TermStore();
      Erl.ReceiveType ret = con.receive(out response.term, 1000);
      
      switch(ret) {
      case Erl.ReceiveType.ERROR:
        if(Erl.errno == Erl.Error.TIMEDOUT)
          break;
        
        running = false;
        break;
      case Erl.ReceiveType.TICK:
        // Do nothing
        break;
      case Erl.ReceiveType.MSG:
        Idle.add(() => {handle_term(response); return false;});
        break;
      }
    }
    
    return null;
  }
  
  private Erl.Term? match(string pattern, Erl.Term term) {
    Erl.Term pattern_term = Erl.format(pattern);
    
    if(pattern_term.match(term) != 0)
      return pattern_term;
    else
      return null;
  }
  
  private void handle_term(TermStore store) {
    unowned Erl.Term term = store.term; 
    Erl.Term match_term;
    
    if((match_term = match("{roster_update,JID,Name,Subscription,Groups,Resources}", term)) != null) {
      Erl.Term jid_term = match_term.var_content("JID");
      if(!jid_term.is_binary())
        // TODO Debug output
        return;
      string jid = ((string)jid_term.bin_ptr()).ndup(jid_term.bin_size());
      
      Erl.Term name_term = match_term.var_content("Name");
      string? name;
      if (name_term.is_binary())
        name = ((string)name_term.bin_ptr()).ndup(name_term.bin_size());
      else
        name = null;
      
      Contact contact = new Contact(jid, name);
      
      Erl.Term resources = match_term.var_content("Resources");
      
      while(resources.is_cons()) {
        Erl.Term r;
        
        if((r = match("{Name,{resource_entry,Priority,Show,Status}}", resources.hd())) != null) {
          Erl.Term rname_term = r.var_content("Name");
          Erl.Term prio_term = r.var_content("Priority");
          Erl.Term show_term = r.var_content("Show");
          Erl.Term status_term = r.var_content("Status");
          
          if(rname_term.is_binary() && prio_term.is_integer() && show_term.is_atom()) {
            string rname = ((string)rname_term.bin_ptr()).ndup(rname_term.bin_size());
            int prio = prio_term.int_value();
            
            Contact.Show show = Contact.Show.UNDEFINED;
            switch((string)show_term.atom_ptr()) {
            case "online":
              show = Contact.Show.ONLINE;
              break;
            case "away":
              show = Contact.Show.AWAY;
              break;
            case "chat":
              show = Contact.Show.CHAT;
              break;
            case "dnd":
              show = Contact.Show.DND;
              break;
            case "xa":
              show = Contact.Show.XA;
              break;
            }
            
            string? status = null;
            if(status_term.is_binary())
              status = ((string)status_term.bin_ptr()).ndup(status_term.bin_size());
            
            contact.update_resource(rname, new Contact.Resource(prio, show, status));
          }
        }
        
        resources = resources.tl();
      }
      
      roster.update_contact(contact);
    }
    else {
      Erl.print_term(stdout, term);
      stdout.printf("\n");
    }
  }
}