]> git.babelmonkeys.de Git - mpdbot.git/blob - src/mpdbot.m
ea84079c19eaf7591280e762e886d11744f7ae24
[mpdbot.git] / src / mpdbot.m
1 /*
2  * Copyright (c) 2010, 2011, Jonathan Schleifer <js@webkeks.org>
3  * Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
4  *
5  * https://webkeks.org/hg/objxmpp/
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice is present in all copies.
10  *
11  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
12  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
15  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
16  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
17  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
18  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
19  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21  * POSSIBILITY OF SUCH DAMAGE.
22  */
23
24 #include <assert.h>
25
26 #import <ObjFW/ObjFW.h>
27 #import <ObjXMPP/ObjXMPP.h>
28
29 #import "PEPThread.h"
30
31 #define NS_DISCO_INFO @"http://jabber.org/protocol/disco#info"
32
33 @interface AppDelegate: OFObject
34 #ifdef OF_HAVE_OPTIONAL_PROTOCOLS
35     <OFApplicationDelegate, XMPPConnectionDelegate>
36 #endif
37
38 OFString *discoID;
39 PEPThread *pepper;
40 @end
41
42 OF_APPLICATION_DELEGATE(AppDelegate)
43
44 @implementation AppDelegate
45 - (void)applicationDidFinishLaunching
46 {
47         XMPPConnection *conn;
48         OFArray *arguments = [OFApplication arguments];
49
50         conn = [[XMPPConnection alloc] init];
51         [conn setDelegate: self];
52
53         if ([arguments count] != 3) {
54                 of_log(@"Invalid count of command line arguments!");
55                 [OFApplication terminateWithStatus: 1];
56         }
57
58         [conn setDomain: [arguments objectAtIndex: 0]];
59         [conn setUsername: [arguments objectAtIndex: 1]];
60         [conn setPassword: [arguments objectAtIndex: 2]];
61         [conn setResource: @"ObjXMPP"];
62
63         @try {
64                 [conn connect];
65                 [conn handleConnection];
66         } @catch (id e) {
67                 of_log(@"%@", e);
68         }
69 }
70
71 - (void)connectionWasAuthenticated: (XMPPConnection*)conn
72 {
73         of_log(@"Auth successful");
74 }
75
76 - (void)connection: (XMPPConnection*)conn
77      wasBoundToJID: (XMPPJID*)jid
78 {
79         XMPPPresence *pres;
80         XMPPIQ *disco;
81
82         of_log(@"Bound to JID: %@", [jid fullJID]);
83
84         pres = [XMPPPresence presence];
85         [pres addPriority: 0];
86         [pres addStatus: @"Hello I'm MPDbot!"];
87         [conn sendStanza: pres];
88
89         discoID = [[conn generateStanzaID] retain];
90         disco = [XMPPIQ IQWithType: @"get"
91                                 ID: discoID];
92         disco.to = [XMPPJID JIDWithString: [[conn JID] bareJID]];
93         [disco addChild: [OFXMLElement
94         elementWithName: @"query"
95               namespace: NS_DISCO_INFO]];
96
97         [conn sendStanza: disco];
98 }
99
100 - (BOOL)connection: (XMPPConnection*)conn
101       didReceiveIQ: (XMPPIQ*)iq
102 {
103         OFXMLElement *query = [iq elementForName: @"query"
104                                        namespace: NS_DISCO_INFO];
105         if ([[iq ID] isEqual: discoID]) {
106                 for (OFXMLElement *identity
107                     in [query elementsForName: @"identity"
108                                     namespace: NS_DISCO_INFO]) {
109                         if ([[[identity attributeForName: @"category"]
110                             stringValue] isEqual: @"pubsub"] &&
111                             [[[identity attributeForName: @"type"] stringValue]
112                             isEqual: @"pep"]) {
113                                 pepper = [[PEPThread alloc]
114                                     initWithObject: conn];
115                                 [pepper start];
116                                 return YES;
117                         }
118                 }
119         }
120
121         return NO;
122 }
123
124 -  (void)connection: (XMPPConnection*)conn
125   didReceiveMessage: (XMPPMessage*)msg
126 {
127         of_log(@"Message: %@", msg);
128 }
129
130 -   (void)connection: (XMPPConnection*)conn
131   didReceivePresence: (XMPPPresence*)pres
132 {
133         of_log(@"Presence: %@", pres);
134 }
135
136 - (void)connectionWasClosed: (XMPPConnection*)conn
137 {
138         of_log(@"Connection was closed!");
139 }
140 @end