]> cgit.babelmonkeys.de Git - mpdbot.git/blob - src/mpdbot.m
Properly declare ivars
[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  * http://cgit.babelmonkeys.de/cgit.cgi/mpdbot/
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 #import <ObjFW/ObjFW.h>
25 #import <ObjXMPP/ObjXMPP.h>
26
27 #import "PEPThread.h"
28
29 #define NS_DISCO_INFO @"http://jabber.org/protocol/disco#info"
30
31 @interface AppDelegate: OFObject <OFApplicationDelegate, XMPPConnectionDelegate>
32 {
33 OFString *discoID;
34 PEPThread *pepper;
35 }
36 @end
37
38 OF_APPLICATION_DELEGATE(AppDelegate)
39
40 @implementation AppDelegate
41 - (void)applicationDidFinishLaunching
42 {
43         XMPPConnection *conn;
44         OFArray *arguments = [OFApplication arguments];
45
46         conn = [[XMPPConnection alloc] init];
47         conn.delegate = self;
48
49         if (arguments.count != 3) {
50                 [of_stdout writeFormat: @"Usage: %@ <server> <user> <passwd>\n",
51                     [OFApplication programName]];
52                 [OFApplication terminateWithStatus: 1];
53         }
54
55         conn.domain = [arguments objectAtIndex: 0];
56         conn.username = [arguments objectAtIndex: 1];
57         conn.password = [arguments objectAtIndex: 2];
58         conn.resource = @"ObjXMPP";
59
60         @try {
61                 [conn connect];
62                 [conn handleConnection];
63         } @catch (id e) {
64                 of_log(@"%@", e);
65         }
66 }
67
68 - (void)connectionWasAuthenticated: (XMPPConnection*)conn
69 {
70         [of_stdout writeLine: @"Auth successful"];
71 }
72
73 - (void)connection: (XMPPConnection*)conn
74      wasBoundToJID: (XMPPJID*)jid
75 {
76         XMPPPresence *pres;
77         XMPPIQ *disco;
78
79         [of_stdout writeFormat: @"Bound to JID: %@\n", [jid fullJID]];
80
81         pres = [XMPPPresence presence];
82         [pres addPriority: 0];
83         [pres addStatus: @"Hello I'm MPDbot!"];
84         [conn sendStanza: pres];
85
86         discoID = [[conn generateStanzaID] retain];
87         disco = [XMPPIQ IQWithType: @"get"
88                                 ID: discoID];
89         disco.to = [XMPPJID JIDWithString: [[conn JID] bareJID]];
90         [disco addChild: [OFXMLElement
91             elementWithName: @"query"
92                   namespace: NS_DISCO_INFO]];
93
94         [conn sendStanza: disco];
95 }
96
97 - (BOOL)connection: (XMPPConnection*)conn
98       didReceiveIQ: (XMPPIQ*)iq
99 {
100         OFXMLElement *query = [iq elementForName: @"query"
101                                        namespace: NS_DISCO_INFO];
102         if ([iq.ID isEqual: discoID]) {
103                 for (OFXMLElement *identity
104                     in [query elementsForName: @"identity"
105                                     namespace: NS_DISCO_INFO]) {
106                         if ([[[identity attributeForName: @"category"]
107                             stringValue] isEqual: @"pubsub"] &&
108                             [[[identity attributeForName: @"type"] stringValue]
109                             isEqual: @"pep"]) {
110                                 pepper = [[PEPThread alloc]
111                                     initWithObject: conn];
112                                 [pepper start];
113
114                                 return YES;
115                         }
116                 }
117         }
118
119         return NO;
120 }
121
122 -  (void)connection: (XMPPConnection*)conn
123   didReceiveMessage: (XMPPMessage*)msg
124 {
125         of_log(@"Message: %@", msg);
126 }
127
128 -   (void)connection: (XMPPConnection*)conn
129   didReceivePresence: (XMPPPresence*)pres
130 {
131         if ([pres.type isEqual: @"subscribe"]) {
132                 XMPPPresence *answer;
133                 answer = [XMPPPresence presenceWithType: @"subscribed"
134                                                      ID: pres.ID];
135                 answer.to = [XMPPJID JIDWithString: [pres.from bareJID]];
136                 [conn sendStanza: answer];
137         }
138 }
139
140 - (void)connectionWasClosed: (XMPPConnection*)conn
141 {
142         [of_stdout writeLine: @"Connection was closed!"];
143 }
144 @end